blob: 3bea4ddc699d3751916be653d957947a97dca4ed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Graf5424f322007-08-22 14:01:33 -070035static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Thomas Graf5424f322007-08-22 14:01:33 -070037 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct xfrm_algo *algp;
39
40 if (!rt)
41 return 0;
42
Thomas Graf5424f322007-08-22 14:01:33 -070043 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080044 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070045 return -EINVAL;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 switch (type) {
48 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52
53 default:
54 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
58 return 0;
59}
60
Martin Willi4447bb32009-11-25 00:29:52 +000061static int verify_auth_trunc(struct nlattr **attrs)
62{
63 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
64 struct xfrm_algo_auth *algp;
65
66 if (!rt)
67 return 0;
68
69 algp = nla_data(rt);
70 if (nla_len(rt) < xfrm_alg_auth_len(algp))
71 return -EINVAL;
72
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 return 0;
75}
76
Herbert Xu1a6509d2008-01-28 19:37:29 -080077static int verify_aead(struct nlattr **attrs)
78{
79 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
80 struct xfrm_algo_aead *algp;
81
82 if (!rt)
83 return 0;
84
85 algp = nla_data(rt);
86 if (nla_len(rt) < aead_len(algp))
87 return -EINVAL;
88
89 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
90 return 0;
91}
92
Thomas Graf5424f322007-08-22 14:01:33 -070093static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070094 xfrm_address_t **addrp)
95{
Thomas Graf5424f322007-08-22 14:01:33 -070096 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070097
Thomas Grafcf5cb792007-08-22 13:59:04 -070098 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070099 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700100}
Trent Jaegerdf718372005-12-13 23:12:27 -0800101
Thomas Graf5424f322007-08-22 14:01:33 -0700102static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800103{
Thomas Graf5424f322007-08-22 14:01:33 -0700104 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800105 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
107 if (!rt)
108 return 0;
109
Thomas Graf5424f322007-08-22 14:01:33 -0700110 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700111 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800112 return -EINVAL;
113
114 return 0;
115}
116
Steffen Klassertd8647b72011-03-08 00:10:27 +0000117static inline int verify_replay(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
119{
120 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000121 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122
Mathias Krauseecd79182012-09-20 10:01:49 +0000123 if (p->flags & XFRM_STATE_ESN) {
124 if (!rt)
125 return -EINVAL;
126
127 rs = nla_data(rt);
128
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
131
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
135 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000136
Steffen Klassertd8647b72011-03-08 00:10:27 +0000137 if (!rt)
138 return 0;
139
Steffen Klassert02aadf72011-03-28 19:48:09 +0000140 if (p->id.proto != IPPROTO_ESP)
141 return -EINVAL;
142
Steffen Klassertd8647b72011-03-08 00:10:27 +0000143 if (p->replay_window != 0)
144 return -EINVAL;
145
146 return 0;
147}
Trent Jaegerdf718372005-12-13 23:12:27 -0800148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700150 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int err;
153
154 err = -EINVAL;
155 switch (p->family) {
156 case AF_INET:
157 break;
158
159 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000160#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162#else
163 err = -EAFNOSUPPORT;
164 goto out;
165#endif
166
167 default:
168 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 err = -EINVAL;
172 switch (p->id.proto) {
173 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000174 if ((!attrs[XFRMA_ALG_AUTH] &&
175 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800176 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700177 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000178 attrs[XFRMA_ALG_COMP] ||
Tobias Brunner2a363682014-06-26 15:12:45 +0200179 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out;
181 break;
182
183 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800184 if (attrs[XFRMA_ALG_COMP])
185 goto out;
186 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000187 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800188 !attrs[XFRMA_ALG_CRYPT] &&
189 !attrs[XFRMA_ALG_AEAD])
190 goto out;
191 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000192 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800193 attrs[XFRMA_ALG_CRYPT]) &&
194 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000196 if (attrs[XFRMA_TFCPAD] &&
197 p->mode != XFRM_MODE_TUNNEL)
198 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200
201 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700202 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800203 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700204 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000205 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000206 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunner2a363682014-06-26 15:12:45 +0200207 attrs[XFRMA_TFCPAD] ||
208 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto out;
210 break;
211
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000212#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700213 case IPPROTO_DSTOPTS:
214 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700215 if (attrs[XFRMA_ALG_COMP] ||
216 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000217 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800218 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700219 attrs[XFRMA_ALG_CRYPT] ||
220 attrs[XFRMA_ENCAP] ||
221 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000222 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700223 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700224 goto out;
225 break;
226#endif
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 default:
229 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Herbert Xu1a6509d2008-01-28 19:37:29 -0800232 if ((err = verify_aead(attrs)))
233 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000234 if ((err = verify_auth_trunc(attrs)))
235 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700236 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700238 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800243 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000244 if ((err = verify_replay(p, attrs)))
245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 err = -EINVAL;
248 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700249 case XFRM_MODE_TRANSPORT:
250 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700251 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700252 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 break;
254
255 default:
256 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 err = 0;
260
261out:
262 return err;
263}
264
265static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800266 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700267 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct xfrm_algo *p, *ualg;
270 struct xfrm_algo_desc *algo;
271
272 if (!rta)
273 return 0;
274
Thomas Graf5424f322007-08-22 14:01:33 -0700275 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 algo = get_byname(ualg->alg_name, 1);
278 if (!algo)
279 return -ENOSYS;
280 *props = algo->desc.sadb_alg_id;
281
Eric Dumazet0f99be02008-01-08 23:39:06 -0800282 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!p)
284 return -ENOMEM;
285
Herbert Xu04ff1262006-08-13 08:50:00 +1000286 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *algpp = p;
288 return 0;
289}
290
Martin Willi4447bb32009-11-25 00:29:52 +0000291static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
292 struct nlattr *rta)
293{
294 struct xfrm_algo *ualg;
295 struct xfrm_algo_auth *p;
296 struct xfrm_algo_desc *algo;
297
298 if (!rta)
299 return 0;
300
301 ualg = nla_data(rta);
302
303 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
304 if (!algo)
305 return -ENOSYS;
306 *props = algo->desc.sadb_alg_id;
307
308 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
309 if (!p)
310 return -ENOMEM;
311
312 strcpy(p->alg_name, algo->name);
313 p->alg_key_len = ualg->alg_key_len;
314 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
315 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
316
317 *algpp = p;
318 return 0;
319}
320
321static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
322 struct nlattr *rta)
323{
324 struct xfrm_algo_auth *p, *ualg;
325 struct xfrm_algo_desc *algo;
326
327 if (!rta)
328 return 0;
329
330 ualg = nla_data(rta);
331
332 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
333 if (!algo)
334 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000335 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
336 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000337 return -EINVAL;
338 *props = algo->desc.sadb_alg_id;
339
340 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
341 if (!p)
342 return -ENOMEM;
343
344 strcpy(p->alg_name, algo->name);
345 if (!p->alg_trunc_len)
346 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
347
348 *algpp = p;
349 return 0;
350}
351
Herbert Xu1a6509d2008-01-28 19:37:29 -0800352static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
353 struct nlattr *rta)
354{
355 struct xfrm_algo_aead *p, *ualg;
356 struct xfrm_algo_desc *algo;
357
358 if (!rta)
359 return 0;
360
361 ualg = nla_data(rta);
362
363 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
364 if (!algo)
365 return -ENOSYS;
366 *props = algo->desc.sadb_alg_id;
367
368 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
369 if (!p)
370 return -ENOMEM;
371
372 strcpy(p->alg_name, algo->name);
373 *algpp = p;
374 return 0;
375}
376
Steffen Klasserte2b19122011-03-28 19:47:30 +0000377static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
378 struct nlattr *rp)
379{
380 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000381 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000382
383 if (!replay_esn || !rp)
384 return 0;
385
386 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000387 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000388
Mathias Krauseecd79182012-09-20 10:01:49 +0000389 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000390 return -EINVAL;
391
392 return 0;
393}
394
Steffen Klassertd8647b72011-03-08 00:10:27 +0000395static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
396 struct xfrm_replay_state_esn **preplay_esn,
397 struct nlattr *rta)
398{
399 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000400 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000401
402 if (!rta)
403 return 0;
404
405 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000406 klen = xfrm_replay_state_esn_len(up);
407 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000408
Mathias Krauseecd79182012-09-20 10:01:49 +0000409 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000410 if (!p)
411 return -ENOMEM;
412
Mathias Krauseecd79182012-09-20 10:01:49 +0000413 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000414 if (!pp) {
415 kfree(p);
416 return -ENOMEM;
417 }
418
Mathias Krauseecd79182012-09-20 10:01:49 +0000419 memcpy(p, up, ulen);
420 memcpy(pp, up, ulen);
421
Steffen Klassertd8647b72011-03-08 00:10:27 +0000422 *replay_esn = p;
423 *preplay_esn = pp;
424
425 return 0;
426}
427
Joy Latten661697f2007-04-13 16:14:35 -0700428static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800429{
Trent Jaegerdf718372005-12-13 23:12:27 -0800430 int len = 0;
431
432 if (xfrm_ctx) {
433 len += sizeof(struct xfrm_user_sec_ctx);
434 len += xfrm_ctx->ctx_len;
435 }
436 return len;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
440{
441 memcpy(&x->id, &p->id, sizeof(x->id));
442 memcpy(&x->sel, &p->sel, sizeof(x->sel));
443 memcpy(&x->lft, &p->lft, sizeof(x->lft));
444 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800445 x->props.replay_window = min_t(unsigned int, p->replay_window,
446 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 x->props.reqid = p->reqid;
448 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700449 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700451
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700452 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700453 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800456/*
457 * someday when pfkey also has support, we could have the code
458 * somehow made shareable and move it to xfrm_state.c - JHS
459 *
460*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000461static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
462 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800463{
Thomas Graf5424f322007-08-22 14:01:33 -0700464 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000465 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700466 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
467 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
468 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800469
Steffen Klassertd8647b72011-03-08 00:10:27 +0000470 if (re) {
471 struct xfrm_replay_state_esn *replay_esn;
472 replay_esn = nla_data(re);
473 memcpy(x->replay_esn, replay_esn,
474 xfrm_replay_state_esn_len(replay_esn));
475 memcpy(x->preplay_esn, replay_esn,
476 xfrm_replay_state_esn_len(replay_esn));
477 }
478
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800479 if (rp) {
480 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700481 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800482 memcpy(&x->replay, replay, sizeof(*replay));
483 memcpy(&x->preplay, replay, sizeof(*replay));
484 }
485
486 if (lt) {
487 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700488 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800489 x->curlft.bytes = ltime->bytes;
490 x->curlft.packets = ltime->packets;
491 x->curlft.add_time = ltime->add_time;
492 x->curlft.use_time = ltime->use_time;
493 }
494
Thomas Grafcf5cb792007-08-22 13:59:04 -0700495 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700496 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800497
Thomas Grafcf5cb792007-08-22 13:59:04 -0700498 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700499 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800500}
501
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800502static struct xfrm_state *xfrm_state_construct(struct net *net,
503 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700504 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int *errp)
506{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800507 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int err = -ENOMEM;
509
510 if (!x)
511 goto error_no_put;
512
513 copy_from_user_state(x, p);
514
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100515 if (attrs[XFRMA_SA_EXTRA_FLAGS])
516 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
517
Herbert Xu1a6509d2008-01-28 19:37:29 -0800518 if ((err = attach_aead(&x->aead, &x->props.ealgo,
519 attrs[XFRMA_ALG_AEAD])))
520 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000521 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
522 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000524 if (!x->props.aalgo) {
525 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
526 attrs[XFRMA_ALG_AUTH])))
527 goto error;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
530 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700531 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto error;
533 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
534 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700535 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700537
538 if (attrs[XFRMA_ENCAP]) {
539 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
540 sizeof(*x->encap), GFP_KERNEL);
541 if (x->encap == NULL)
542 goto error;
543 }
544
Martin Willi35d28562010-12-08 04:37:49 +0000545 if (attrs[XFRMA_TFCPAD])
546 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
547
Thomas Graffd211502007-09-06 03:28:08 -0700548 if (attrs[XFRMA_COADDR]) {
549 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
550 sizeof(*x->coaddr), GFP_KERNEL);
551 if (x->coaddr == NULL)
552 goto error;
553 }
554
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000555 xfrm_mark_get(attrs, &x->mark);
556
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700557 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (err)
559 goto error;
560
Thomas Graffd211502007-09-06 03:28:08 -0700561 if (attrs[XFRMA_SEC_CTX] &&
562 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800563 goto error;
564
Steffen Klassertd8647b72011-03-08 00:10:27 +0000565 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
566 attrs[XFRMA_REPLAY_ESN_VAL])))
567 goto error;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800570 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800571 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800572 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800573
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000574 if ((err = xfrm_init_replay(x)))
575 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800576
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000577 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000578 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return x;
581
582error:
583 x->km.state = XFRM_STATE_DEAD;
584 xfrm_state_put(x);
585error_no_put:
586 *errp = err;
587 return NULL;
588}
589
Christoph Hellwig22e70052007-01-02 15:22:30 -0800590static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700591 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800593 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700594 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct xfrm_state *x;
596 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700597 struct km_event c;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700598 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500599 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800600 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Thomas Graf35a7aa02007-08-22 14:00:40 -0700602 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (err)
604 return err;
605
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800606 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!x)
608 return err;
609
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700610 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
612 err = xfrm_state_add(x);
613 else
614 err = xfrm_state_update(x);
615
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800616 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400617 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (err < 0) {
620 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800621 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700625 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000626 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700627 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700628
629 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700630out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700631 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return err;
633}
634
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800635static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
636 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700637 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700638 int *errp)
639{
640 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000641 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700642 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000643 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700644
645 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
646 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000647 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700648 } else {
649 xfrm_address_t *saddr = NULL;
650
Thomas Graf35a7aa02007-08-22 14:00:40 -0700651 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700652 if (!saddr) {
653 err = -EINVAL;
654 goto out;
655 }
656
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800657 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000658 x = xfrm_state_lookup_byaddr(net, mark,
659 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800660 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700661 }
662
663 out:
664 if (!x && errp)
665 *errp = err;
666 return x;
667}
668
Christoph Hellwig22e70052007-01-02 15:22:30 -0800669static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700670 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800672 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700674 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700675 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700676 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700677 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500678 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800679 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800681 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
David S. Miller6f68dc32006-06-08 23:58:52 -0700685 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700686 goto out;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700689 err = -EPERM;
690 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700693 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600694
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700695 if (err < 0)
696 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700697
698 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000699 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700700 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700701 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700703out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800704 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400705 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700706 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700707 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
711{
Mathias Krausef778a632012-09-19 11:33:39 +0000712 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 memcpy(&p->id, &x->id, sizeof(p->id));
714 memcpy(&p->sel, &x->sel, sizeof(p->sel));
715 memcpy(&p->lft, &x->lft, sizeof(p->lft));
716 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
717 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700718 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 p->mode = x->props.mode;
720 p->replay_window = x->props.replay_window;
721 p->reqid = x->props.reqid;
722 p->family = x->props.family;
723 p->flags = x->props.flags;
724 p->seq = x->km.seq;
725}
726
727struct xfrm_dump_info {
728 struct sk_buff *in_skb;
729 struct sk_buff *out_skb;
730 u32 nlmsg_seq;
731 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732};
733
Thomas Grafc0144be2007-08-22 13:55:43 -0700734static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
735{
Thomas Grafc0144be2007-08-22 13:55:43 -0700736 struct xfrm_user_sec_ctx *uctx;
737 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700738 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700739
740 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
741 if (attr == NULL)
742 return -EMSGSIZE;
743
744 uctx = nla_data(attr);
745 uctx->exttype = XFRMA_SEC_CTX;
746 uctx->len = ctx_size;
747 uctx->ctx_doi = s->ctx_doi;
748 uctx->ctx_alg = s->ctx_alg;
749 uctx->ctx_len = s->ctx_len;
750 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
751
752 return 0;
753}
754
Martin Willi4447bb32009-11-25 00:29:52 +0000755static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
756{
757 struct xfrm_algo *algo;
758 struct nlattr *nla;
759
760 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
761 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
762 if (!nla)
763 return -EMSGSIZE;
764
765 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000766 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000767 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
768 algo->alg_key_len = auth->alg_key_len;
769
770 return 0;
771}
772
Herbert Xu68325d32007-10-09 13:30:57 -0700773/* Don't change this without updating xfrm_sa_len! */
774static int copy_to_user_state_extra(struct xfrm_state *x,
775 struct xfrm_usersa_info *p,
776 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700778 int ret = 0;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 copy_to_user_state(x, p);
781
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100782 if (x->props.extra_flags) {
783 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
784 x->props.extra_flags);
785 if (ret)
786 goto out;
787 }
788
David S. Miller1d1e34d2012-06-27 21:57:03 -0700789 if (x->coaddr) {
790 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
791 if (ret)
792 goto out;
793 }
794 if (x->lastused) {
795 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
796 if (ret)
797 goto out;
798 }
799 if (x->aead) {
800 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
801 if (ret)
802 goto out;
803 }
804 if (x->aalg) {
805 ret = copy_to_user_auth(x->aalg, skb);
806 if (!ret)
807 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
808 xfrm_alg_auth_len(x->aalg), x->aalg);
809 if (ret)
810 goto out;
811 }
812 if (x->ealg) {
813 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
814 if (ret)
815 goto out;
816 }
817 if (x->calg) {
818 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
819 if (ret)
820 goto out;
821 }
822 if (x->encap) {
823 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
824 if (ret)
825 goto out;
826 }
827 if (x->tfcpad) {
828 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
829 if (ret)
830 goto out;
831 }
832 ret = xfrm_mark_put(skb, &x->mark);
833 if (ret)
834 goto out;
835 if (x->replay_esn) {
836 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
837 xfrm_replay_state_esn_len(x->replay_esn),
838 x->replay_esn);
839 if (ret)
840 goto out;
841 }
842 if (x->security)
843 ret = copy_sec_ctx(x->security, skb);
844out:
845 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700846}
847
848static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
849{
850 struct xfrm_dump_info *sp = ptr;
851 struct sk_buff *in_skb = sp->in_skb;
852 struct sk_buff *skb = sp->out_skb;
853 struct xfrm_usersa_info *p;
854 struct nlmsghdr *nlh;
855 int err;
856
Eric W. Biederman15e47302012-09-07 20:12:54 +0000857 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700858 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
859 if (nlh == NULL)
860 return -EMSGSIZE;
861
862 p = nlmsg_data(nlh);
863
864 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700865 if (err) {
866 nlmsg_cancel(skb, nlh);
867 return err;
868 }
Thomas Graf98250692007-08-22 12:47:26 -0700869 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Timo Teras4c563f72008-02-28 21:31:08 -0800873static int xfrm_dump_sa_done(struct netlink_callback *cb)
874{
875 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800876 struct sock *sk = cb->skb->sk;
877 struct net *net = sock_net(sk);
878
879 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800880 return 0;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
884{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800885 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800886 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct xfrm_dump_info info;
888
Timo Teras4c563f72008-02-28 21:31:08 -0800889 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
890 sizeof(cb->args) - sizeof(cb->args[0]));
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 info.in_skb = cb->skb;
893 info.out_skb = skb;
894 info.nlmsg_seq = cb->nlh->nlmsg_seq;
895 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800896
897 if (!cb->args[0]) {
898 cb->args[0] = 1;
899 xfrm_state_walk_init(walk, 0);
900 }
901
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800902 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return skb->len;
905}
906
907static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
908 struct xfrm_state *x, u32 seq)
909{
910 struct xfrm_dump_info info;
911 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000912 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Thomas Graf7deb2262007-08-22 13:57:39 -0700914 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!skb)
916 return ERR_PTR(-ENOMEM);
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 info.in_skb = in_skb;
919 info.out_skb = skb;
920 info.nlmsg_seq = seq;
921 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Mathias Krause864745d2012-09-13 11:41:26 +0000923 err = dump_one_state(x, 0, &info);
924 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000926 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929 return skb;
930}
931
Thomas Graf7deb2262007-08-22 13:57:39 -0700932static inline size_t xfrm_spdinfo_msgsize(void)
933{
934 return NLMSG_ALIGN(4)
935 + nla_total_size(sizeof(struct xfrmu_spdinfo))
936 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
937}
938
Alexey Dobriyane0710412010-01-23 13:37:10 +0000939static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000940 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700941{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700942 struct xfrmk_spdinfo si;
943 struct xfrmu_spdinfo spc;
944 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700945 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700946 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700947 u32 *f;
948
Eric W. Biederman15e47302012-09-07 20:12:54 +0000949 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300950 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700951 return -EMSGSIZE;
952
953 f = nlmsg_data(nlh);
954 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000955 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700956 spc.incnt = si.incnt;
957 spc.outcnt = si.outcnt;
958 spc.fwdcnt = si.fwdcnt;
959 spc.inscnt = si.inscnt;
960 spc.outscnt = si.outscnt;
961 spc.fwdscnt = si.fwdscnt;
962 sph.spdhcnt = si.spdhcnt;
963 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700964
David S. Miller1d1e34d2012-06-27 21:57:03 -0700965 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
966 if (!err)
967 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
968 if (err) {
969 nlmsg_cancel(skb, nlh);
970 return err;
971 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700972
973 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700974}
975
976static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700977 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700978{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800979 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700980 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700981 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000982 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700983 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700984
Thomas Graf7deb2262007-08-22 13:57:39 -0700985 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700986 if (r_skb == NULL)
987 return -ENOMEM;
988
Eric W. Biederman15e47302012-09-07 20:12:54 +0000989 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700990 BUG();
991
Eric W. Biederman15e47302012-09-07 20:12:54 +0000992 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700993}
994
Thomas Graf7deb2262007-08-22 13:57:39 -0700995static inline size_t xfrm_sadinfo_msgsize(void)
996{
997 return NLMSG_ALIGN(4)
998 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
999 + nla_total_size(4); /* XFRMA_SAD_CNT */
1000}
1001
Alexey Dobriyane0710412010-01-23 13:37:10 +00001002static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001003 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001004{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001005 struct xfrmk_sadinfo si;
1006 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001007 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001008 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001009 u32 *f;
1010
Eric W. Biederman15e47302012-09-07 20:12:54 +00001011 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001012 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001013 return -EMSGSIZE;
1014
1015 f = nlmsg_data(nlh);
1016 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001017 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001018
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001019 sh.sadhmcnt = si.sadhmcnt;
1020 sh.sadhcnt = si.sadhcnt;
1021
David S. Miller1d1e34d2012-06-27 21:57:03 -07001022 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1023 if (!err)
1024 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1025 if (err) {
1026 nlmsg_cancel(skb, nlh);
1027 return err;
1028 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001029
1030 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001031}
1032
1033static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001034 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001035{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001036 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001037 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001038 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001039 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001040 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001041
Thomas Graf7deb2262007-08-22 13:57:39 -07001042 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001043 if (r_skb == NULL)
1044 return -ENOMEM;
1045
Eric W. Biederman15e47302012-09-07 20:12:54 +00001046 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001047 BUG();
1048
Eric W. Biederman15e47302012-09-07 20:12:54 +00001049 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001050}
1051
Christoph Hellwig22e70052007-01-02 15:22:30 -08001052static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001053 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001055 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001056 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct xfrm_state *x;
1058 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001059 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001061 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (x == NULL)
1063 goto out_noput;
1064
1065 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1066 if (IS_ERR(resp_skb)) {
1067 err = PTR_ERR(resp_skb);
1068 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001069 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 xfrm_state_put(x);
1072out_noput:
1073 return err;
1074}
1075
Christoph Hellwig22e70052007-01-02 15:22:30 -08001076static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001077 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001079 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct xfrm_state *x;
1081 struct xfrm_userspi_info *p;
1082 struct sk_buff *resp_skb;
1083 xfrm_address_t *daddr;
1084 int family;
1085 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001086 u32 mark;
1087 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Thomas Graf7b67c852007-08-22 13:53:52 -07001089 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001090 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (err)
1092 goto out_noput;
1093
1094 family = p->info.family;
1095 daddr = &p->info.id.daddr;
1096
1097 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001098
1099 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001101 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001102 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 xfrm_state_put(x);
1104 x = NULL;
1105 }
1106 }
1107
1108 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001109 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 p->info.id.proto, daddr,
1111 &p->info.saddr, 1,
1112 family);
1113 err = -ENOENT;
1114 if (x == NULL)
1115 goto out_noput;
1116
Herbert Xu658b2192007-10-09 13:29:52 -07001117 err = xfrm_alloc_spi(x, p->min, p->max);
1118 if (err)
1119 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Herbert Xu658b2192007-10-09 13:29:52 -07001121 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (IS_ERR(resp_skb)) {
1123 err = PTR_ERR(resp_skb);
1124 goto out;
1125 }
1126
Eric W. Biederman15e47302012-09-07 20:12:54 +00001127 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129out:
1130 xfrm_state_put(x);
1131out_noput:
1132 return err;
1133}
1134
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001135static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 switch (dir) {
1138 case XFRM_POLICY_IN:
1139 case XFRM_POLICY_OUT:
1140 case XFRM_POLICY_FWD:
1141 break;
1142
1143 default:
1144 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 return 0;
1148}
1149
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001150static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001151{
1152 switch (type) {
1153 case XFRM_POLICY_TYPE_MAIN:
1154#ifdef CONFIG_XFRM_SUB_POLICY
1155 case XFRM_POLICY_TYPE_SUB:
1156#endif
1157 break;
1158
1159 default:
1160 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001161 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001162
1163 return 0;
1164}
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1167{
Fan Due682adf2013-11-07 17:47:48 +08001168 int ret;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 switch (p->share) {
1171 case XFRM_SHARE_ANY:
1172 case XFRM_SHARE_SESSION:
1173 case XFRM_SHARE_USER:
1174 case XFRM_SHARE_UNIQUE:
1175 break;
1176
1177 default:
1178 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 switch (p->action) {
1182 case XFRM_POLICY_ALLOW:
1183 case XFRM_POLICY_BLOCK:
1184 break;
1185
1186 default:
1187 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 switch (p->sel.family) {
1191 case AF_INET:
1192 break;
1193
1194 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001195#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
1197#else
1198 return -EAFNOSUPPORT;
1199#endif
1200
1201 default:
1202 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Fan Due682adf2013-11-07 17:47:48 +08001205 ret = verify_policy_dir(p->dir);
1206 if (ret)
1207 return ret;
1208 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1209 return -EINVAL;
1210
1211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Thomas Graf5424f322007-08-22 14:01:33 -07001214static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001215{
Thomas Graf5424f322007-08-22 14:01:33 -07001216 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001217 struct xfrm_user_sec_ctx *uctx;
1218
1219 if (!rt)
1220 return 0;
1221
Thomas Graf5424f322007-08-22 14:01:33 -07001222 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001223 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1227 int nr)
1228{
1229 int i;
1230
1231 xp->xfrm_nr = nr;
1232 for (i = 0; i < nr; i++, ut++) {
1233 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1234
1235 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1236 memcpy(&t->saddr, &ut->saddr,
1237 sizeof(xfrm_address_t));
1238 t->reqid = ut->reqid;
1239 t->mode = ut->mode;
1240 t->share = ut->share;
1241 t->optional = ut->optional;
1242 t->aalgos = ut->aalgos;
1243 t->ealgos = ut->ealgos;
1244 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001245 /* If all masks are ~0, then we allow all algorithms. */
1246 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001247 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249}
1250
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001251static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1252{
1253 int i;
1254
1255 if (nr > XFRM_MAX_DEPTH)
1256 return -EINVAL;
1257
1258 for (i = 0; i < nr; i++) {
1259 /* We never validated the ut->family value, so many
1260 * applications simply leave it at zero. The check was
1261 * never made and ut->family was ignored because all
1262 * templates could be assumed to have the same family as
1263 * the policy itself. Now that we will have ipv4-in-ipv6
1264 * and ipv6-in-ipv4 tunnels, this is no longer true.
1265 */
1266 if (!ut[i].family)
1267 ut[i].family = family;
1268
1269 switch (ut[i].family) {
1270 case AF_INET:
1271 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001272#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001273 case AF_INET6:
1274 break;
1275#endif
1276 default:
1277 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001278 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001279 }
1280
1281 return 0;
1282}
1283
Thomas Graf5424f322007-08-22 14:01:33 -07001284static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Thomas Graf5424f322007-08-22 14:01:33 -07001286 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (!rt) {
1289 pol->xfrm_nr = 0;
1290 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001291 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1292 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001293 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001295 err = validate_tmpl(nr, utmpl, pol->family);
1296 if (err)
1297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Thomas Graf5424f322007-08-22 14:01:33 -07001299 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 return 0;
1302}
1303
Thomas Graf5424f322007-08-22 14:01:33 -07001304static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001305{
Thomas Graf5424f322007-08-22 14:01:33 -07001306 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001307 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001308 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001309 int err;
1310
1311 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001312 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001313 type = upt->type;
1314 }
1315
1316 err = verify_policy_type(type);
1317 if (err)
1318 return err;
1319
1320 *tp = type;
1321 return 0;
1322}
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1325{
1326 xp->priority = p->priority;
1327 xp->index = p->index;
1328 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1329 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1330 xp->action = p->action;
1331 xp->flags = p->flags;
1332 xp->family = p->sel.family;
1333 /* XXX xp->share = p->share; */
1334}
1335
1336static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1337{
Mathias Krause7b789832012-09-19 11:33:40 +00001338 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1340 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1341 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1342 p->priority = xp->priority;
1343 p->index = xp->index;
1344 p->sel.family = xp->family;
1345 p->dir = dir;
1346 p->action = xp->action;
1347 p->flags = xp->flags;
1348 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1349}
1350
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001351static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001353 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int err;
1355
1356 if (!xp) {
1357 *errp = -ENOMEM;
1358 return NULL;
1359 }
1360
1361 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001362
Thomas Graf35a7aa02007-08-22 14:00:40 -07001363 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001364 if (err)
1365 goto error;
1366
Thomas Graf35a7aa02007-08-22 14:00:40 -07001367 if (!(err = copy_from_user_tmpl(xp, attrs)))
1368 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001369 if (err)
1370 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001372 xfrm_mark_get(attrs, &xp->mark);
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001375 error:
1376 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001377 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001378 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001379 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
Christoph Hellwig22e70052007-01-02 15:22:30 -08001382static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001383 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001385 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001386 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001388 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int err;
1390 int excl;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001391 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001392 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001393 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 err = verify_newpolicy_info(p);
1396 if (err)
1397 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001398 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001399 if (err)
1400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001402 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (!xp)
1404 return err;
1405
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001406 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001407 * Aha! this is anti-netlink really i.e more pfkey derived
1408 * in netlink excl is a flag and you wouldnt need
1409 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1411 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001412 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001413 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001416 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 kfree(xp);
1418 return err;
1419 }
1420
Herbert Xuf60f6b82005-06-18 22:44:37 -07001421 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001422 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001423 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001424 km_policy_notify(xp, p->dir, &c);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 xfrm_pol_put(xp);
1427
1428 return 0;
1429}
1430
1431static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1432{
1433 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1434 int i;
1435
1436 if (xp->xfrm_nr == 0)
1437 return 0;
1438
1439 for (i = 0; i < xp->xfrm_nr; i++) {
1440 struct xfrm_user_tmpl *up = &vec[i];
1441 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1442
Mathias Krause1f868402012-09-19 11:33:41 +00001443 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001445 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1447 up->reqid = kp->reqid;
1448 up->mode = kp->mode;
1449 up->share = kp->share;
1450 up->optional = kp->optional;
1451 up->aalgos = kp->aalgos;
1452 up->ealgos = kp->ealgos;
1453 up->calgos = kp->calgos;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Thomas Grafc0144be2007-08-22 13:55:43 -07001456 return nla_put(skb, XFRMA_TMPL,
1457 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001458}
1459
Serge Hallyn0d681622006-07-24 23:30:44 -07001460static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1461{
1462 if (x->security) {
1463 return copy_sec_ctx(x->security, skb);
1464 }
1465 return 0;
1466}
1467
1468static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1469{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001470 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001471 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001472 return 0;
1473}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001474static inline size_t userpolicy_type_attrsize(void)
1475{
1476#ifdef CONFIG_XFRM_SUB_POLICY
1477 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1478#else
1479 return 0;
1480#endif
1481}
Serge Hallyn0d681622006-07-24 23:30:44 -07001482
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001483#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001484static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001485{
Thomas Grafc0144be2007-08-22 13:55:43 -07001486 struct xfrm_userpolicy_type upt = {
1487 .type = type,
1488 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001489
Thomas Grafc0144be2007-08-22 13:55:43 -07001490 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001491}
1492
1493#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001494static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001495{
1496 return 0;
1497}
1498#endif
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1501{
1502 struct xfrm_dump_info *sp = ptr;
1503 struct xfrm_userpolicy_info *p;
1504 struct sk_buff *in_skb = sp->in_skb;
1505 struct sk_buff *skb = sp->out_skb;
1506 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001507 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Eric W. Biederman15e47302012-09-07 20:12:54 +00001509 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001510 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1511 if (nlh == NULL)
1512 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Thomas Graf7b67c852007-08-22 13:53:52 -07001514 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001516 err = copy_to_user_tmpl(xp, skb);
1517 if (!err)
1518 err = copy_to_user_sec_ctx(xp, skb);
1519 if (!err)
1520 err = copy_to_user_policy_type(xp->type, skb);
1521 if (!err)
1522 err = xfrm_mark_put(skb, &xp->mark);
1523 if (err) {
1524 nlmsg_cancel(skb, nlh);
1525 return err;
1526 }
Thomas Graf98250692007-08-22 12:47:26 -07001527 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Timo Teras4c563f72008-02-28 21:31:08 -08001531static int xfrm_dump_policy_done(struct netlink_callback *cb)
1532{
1533 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001534 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001535
Fan Du283bc9f2013-11-07 17:47:50 +08001536 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001537 return 0;
1538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1541{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001542 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001543 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 struct xfrm_dump_info info;
1545
Timo Teras4c563f72008-02-28 21:31:08 -08001546 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1547 sizeof(cb->args) - sizeof(cb->args[0]));
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 info.in_skb = cb->skb;
1550 info.out_skb = skb;
1551 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1552 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001553
1554 if (!cb->args[0]) {
1555 cb->args[0] = 1;
1556 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1557 }
1558
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001559 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 return skb->len;
1562}
1563
1564static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1565 struct xfrm_policy *xp,
1566 int dir, u32 seq)
1567{
1568 struct xfrm_dump_info info;
1569 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001570 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Thomas Graf7deb2262007-08-22 13:57:39 -07001572 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (!skb)
1574 return ERR_PTR(-ENOMEM);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 info.in_skb = in_skb;
1577 info.out_skb = skb;
1578 info.nlmsg_seq = seq;
1579 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Mathias Krausec2546372012-09-14 09:58:32 +00001581 err = dump_one_policy(xp, dir, 0, &info);
1582 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001584 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
1587 return skb;
1588}
1589
Christoph Hellwig22e70052007-01-02 15:22:30 -08001590static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001591 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001593 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct xfrm_policy *xp;
1595 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001596 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001598 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001600 struct xfrm_mark m;
1601 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Thomas Graf7b67c852007-08-22 13:53:52 -07001603 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1605
Thomas Graf35a7aa02007-08-22 14:00:40 -07001606 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001607 if (err)
1608 return err;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 err = verify_policy_dir(p->dir);
1611 if (err)
1612 return err;
1613
1614 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001615 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001616 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001617 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001618 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001619
Thomas Graf35a7aa02007-08-22 14:00:40 -07001620 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001621 if (err)
1622 return err;
1623
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001624 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001625 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001626 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001627
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001628 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001629 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001630 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001631 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001632 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001633 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001634 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (xp == NULL)
1637 return -ENOENT;
1638
1639 if (!delete) {
1640 struct sk_buff *resp_skb;
1641
1642 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1643 if (IS_ERR(resp_skb)) {
1644 err = PTR_ERR(resp_skb);
1645 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001646 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001647 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001649 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001650 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001651 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001652 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001653
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001654 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001655 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1656 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001657
1658 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001659 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001660
Herbert Xue7443892005-06-18 22:44:18 -07001661 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001662 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001663 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001664 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001665 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001668out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001669 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001670 if (delete && err == 0)
1671 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return err;
1673}
1674
Christoph Hellwig22e70052007-01-02 15:22:30 -08001675static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001676 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001678 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001679 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001680 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001681 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001682 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001684 audit_info.loginuid = audit_get_loginuid(current);
1685 audit_info.sessionid = audit_get_sessionid(current);
1686 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001687 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001688 if (err) {
1689 if (err == -ESRCH) /* empty table */
1690 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001691 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001692 }
Herbert Xubf088672005-06-18 22:44:00 -07001693 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001694 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001695 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001696 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001697 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001698 km_state_notify(NULL, &c);
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return 0;
1701}
1702
Steffen Klassertd8647b72011-03-08 00:10:27 +00001703static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001704{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001705 size_t replay_size = x->replay_esn ?
1706 xfrm_replay_state_esn_len(x->replay_esn) :
1707 sizeof(struct xfrm_replay_state);
1708
Thomas Graf7deb2262007-08-22 13:57:39 -07001709 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001710 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001711 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001712 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001713 + nla_total_size(4) /* XFRM_AE_RTHR */
1714 + nla_total_size(4); /* XFRM_AE_ETHR */
1715}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001716
David S. Miller214e0052011-02-24 00:02:38 -05001717static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001718{
1719 struct xfrm_aevent_id *id;
1720 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001721 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001722
Eric W. Biederman15e47302012-09-07 20:12:54 +00001723 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001724 if (nlh == NULL)
1725 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001726
Thomas Graf7b67c852007-08-22 13:53:52 -07001727 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001728 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001729 id->sa_id.spi = x->id.spi;
1730 id->sa_id.family = x->props.family;
1731 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001732 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001733 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001734 id->flags = c->data.aevent;
1735
David S. Millerd0fde792012-03-29 04:02:26 -04001736 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001737 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1738 xfrm_replay_state_esn_len(x->replay_esn),
1739 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001740 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001741 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1742 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001743 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001744 if (err)
1745 goto out_cancel;
1746 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1747 if (err)
1748 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001749
David S. Miller1d1e34d2012-06-27 21:57:03 -07001750 if (id->flags & XFRM_AE_RTHR) {
1751 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1752 if (err)
1753 goto out_cancel;
1754 }
1755 if (id->flags & XFRM_AE_ETHR) {
1756 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1757 x->replay_maxage * 10 / HZ);
1758 if (err)
1759 goto out_cancel;
1760 }
1761 err = xfrm_mark_put(skb, &x->mark);
1762 if (err)
1763 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001764
Thomas Graf98250692007-08-22 12:47:26 -07001765 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001766
David S. Miller1d1e34d2012-06-27 21:57:03 -07001767out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001768 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001769 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001770}
1771
Christoph Hellwig22e70052007-01-02 15:22:30 -08001772static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001773 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001774{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001775 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001776 struct xfrm_state *x;
1777 struct sk_buff *r_skb;
1778 int err;
1779 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001780 u32 mark;
1781 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001782 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001783 struct xfrm_usersa_id *id = &p->sa_id;
1784
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001785 mark = xfrm_mark_get(attrs, &m);
1786
1787 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001788 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001789 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001790
1791 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1792 if (r_skb == NULL) {
1793 xfrm_state_put(x);
1794 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001795 }
1796
1797 /*
1798 * XXX: is this lock really needed - none of the other
1799 * gets lock (the concern is things getting updated
1800 * while we are still reading) - jhs
1801 */
1802 spin_lock_bh(&x->lock);
1803 c.data.aevent = p->flags;
1804 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001805 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806
1807 if (build_aevent(r_skb, x, &c) < 0)
1808 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001809 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001810 spin_unlock_bh(&x->lock);
1811 xfrm_state_put(x);
1812 return err;
1813}
1814
Christoph Hellwig22e70052007-01-02 15:22:30 -08001815static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001816 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001817{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001818 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001819 struct xfrm_state *x;
1820 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001821 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001822 u32 mark = 0;
1823 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001824 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001825 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001826 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001827 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001828
Steffen Klassertd8647b72011-03-08 00:10:27 +00001829 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001830 return err;
1831
1832 /* pedantic mode - thou shalt sayeth replaceth */
1833 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1834 return err;
1835
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001836 mark = xfrm_mark_get(attrs, &m);
1837
1838 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001839 if (x == NULL)
1840 return -ESRCH;
1841
1842 if (x->km.state != XFRM_STATE_VALID)
1843 goto out;
1844
Steffen Klassert4479ff72013-09-09 09:39:01 +02001845 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001846 if (err)
1847 goto out;
1848
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001849 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001850 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001851 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001852
1853 c.event = nlh->nlmsg_type;
1854 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001855 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001856 c.data.aevent = XFRM_AE_CU;
1857 km_state_notify(x, &c);
1858 err = 0;
1859out:
1860 xfrm_state_put(x);
1861 return err;
1862}
1863
Christoph Hellwig22e70052007-01-02 15:22:30 -08001864static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001865 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001867 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001868 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001869 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001870 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001871 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001872
Thomas Graf35a7aa02007-08-22 14:00:40 -07001873 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001874 if (err)
1875 return err;
1876
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001877 audit_info.loginuid = audit_get_loginuid(current);
1878 audit_info.sessionid = audit_get_sessionid(current);
1879 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001880 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001881 if (err) {
1882 if (err == -ESRCH) /* empty table */
1883 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001884 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001885 }
1886
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001887 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001888 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001889 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001890 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001891 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001892 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return 0;
1894}
1895
Christoph Hellwig22e70052007-01-02 15:22:30 -08001896static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001897 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001898{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001899 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001900 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001901 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001902 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001903 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001904 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001905 struct xfrm_mark m;
1906 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001907
Thomas Graf35a7aa02007-08-22 14:00:40 -07001908 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001909 if (err)
1910 return err;
1911
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001912 err = verify_policy_dir(p->dir);
1913 if (err)
1914 return err;
1915
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001916 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001917 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001918 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001919 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001920 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001921
Thomas Graf35a7aa02007-08-22 14:00:40 -07001922 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001923 if (err)
1924 return err;
1925
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001926 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001927 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001928 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001929
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001930 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001931 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001932 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001933 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001934 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001935 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001936 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001937 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001938 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001939 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001940
Timo Teräsea2dea92010-03-31 00:17:05 +00001941 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001942 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001943
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001944 err = 0;
1945 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001946 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001947 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001948 u32 sid;
1949
1950 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001951 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001952 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001953
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001954 } else {
1955 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001956 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001957 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001958 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001959
1960out:
1961 xfrm_pol_put(xp);
1962 return err;
1963}
1964
Christoph Hellwig22e70052007-01-02 15:22:30 -08001965static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001966 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001967{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001968 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001969 struct xfrm_state *x;
1970 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001971 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001972 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001973 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001974 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001975
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001976 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001977
David S. Miller3a765aa2007-02-26 14:52:21 -08001978 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001979 if (x == NULL)
1980 return err;
1981
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001982 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001983 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001984 if (x->km.state != XFRM_STATE_VALID)
1985 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001986 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001987
Joy Latten161a09e2006-11-27 13:11:54 -06001988 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001989 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001990 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001991 u32 sid;
1992
1993 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001994 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001995 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001996 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001997 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001998out:
1999 spin_unlock_bh(&x->lock);
2000 xfrm_state_put(x);
2001 return err;
2002}
2003
Christoph Hellwig22e70052007-01-02 15:22:30 -08002004static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002005 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002006{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002007 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002008 struct xfrm_policy *xp;
2009 struct xfrm_user_tmpl *ut;
2010 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002011 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002012 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002013
Thomas Graf7b67c852007-08-22 13:53:52 -07002014 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002015 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002016 int err = -ENOMEM;
2017
2018 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002019 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002020
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002021 xfrm_mark_get(attrs, &mark);
2022
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002023 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002024 if (err)
2025 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002026
2027 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002028 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002029 if (!xp)
2030 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002031
2032 memcpy(&x->id, &ua->id, sizeof(ua->id));
2033 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2034 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002035 xp->mark.m = x->mark.m = mark.m;
2036 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002037 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002038 /* extract the templates and for each call km_key */
2039 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2040 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2041 memcpy(&x->id, &t->id, sizeof(x->id));
2042 x->props.mode = t->mode;
2043 x->props.reqid = t->reqid;
2044 x->props.family = ut->family;
2045 t->aalgos = ua->aalgos;
2046 t->ealgos = ua->ealgos;
2047 t->calgos = ua->calgos;
2048 err = km_query(x, t, xp);
2049
2050 }
2051
2052 kfree(x);
2053 kfree(xp);
2054
2055 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002056
2057bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002058 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002059free_state:
2060 kfree(x);
2061nomem:
2062 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002063}
2064
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002065#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002066static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002067 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002068 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002069{
Thomas Graf5424f322007-08-22 14:01:33 -07002070 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002071 struct xfrm_user_migrate *um;
2072 int i, num_migrate;
2073
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002074 if (k != NULL) {
2075 struct xfrm_user_kmaddress *uk;
2076
2077 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2078 memcpy(&k->local, &uk->local, sizeof(k->local));
2079 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2080 k->family = uk->family;
2081 k->reserved = uk->reserved;
2082 }
2083
Thomas Graf5424f322007-08-22 14:01:33 -07002084 um = nla_data(rt);
2085 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002086
2087 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2088 return -EINVAL;
2089
2090 for (i = 0; i < num_migrate; i++, um++, ma++) {
2091 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2092 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2093 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2094 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2095
2096 ma->proto = um->proto;
2097 ma->mode = um->mode;
2098 ma->reqid = um->reqid;
2099
2100 ma->old_family = um->old_family;
2101 ma->new_family = um->new_family;
2102 }
2103
2104 *num = i;
2105 return 0;
2106}
2107
2108static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002109 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002110{
Thomas Graf7b67c852007-08-22 13:53:52 -07002111 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002112 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002113 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002114 u8 type;
2115 int err;
2116 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002117 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002118
Thomas Graf35a7aa02007-08-22 14:00:40 -07002119 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002120 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002121
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002122 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2123
Thomas Graf5424f322007-08-22 14:01:33 -07002124 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002125 if (err)
2126 return err;
2127
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002128 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002129 if (err)
2130 return err;
2131
2132 if (!n)
2133 return 0;
2134
Fan Du8d549c42013-11-07 17:47:49 +08002135 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002136
2137 return 0;
2138}
2139#else
2140static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002141 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002142{
2143 return -ENOPROTOOPT;
2144}
2145#endif
2146
2147#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002148static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002149{
2150 struct xfrm_user_migrate um;
2151
2152 memset(&um, 0, sizeof(um));
2153 um.proto = m->proto;
2154 um.mode = m->mode;
2155 um.reqid = m->reqid;
2156 um.old_family = m->old_family;
2157 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2158 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2159 um.new_family = m->new_family;
2160 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2161 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2162
Thomas Grafc0144be2007-08-22 13:55:43 -07002163 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002164}
2165
David S. Miller183cad12011-02-24 00:28:01 -05002166static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002167{
2168 struct xfrm_user_kmaddress uk;
2169
2170 memset(&uk, 0, sizeof(uk));
2171 uk.family = k->family;
2172 uk.reserved = k->reserved;
2173 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002174 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002175
2176 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2177}
2178
2179static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002180{
2181 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002182 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2183 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2184 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002185}
2186
David S. Miller183cad12011-02-24 00:28:01 -05002187static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2188 int num_migrate, const struct xfrm_kmaddress *k,
2189 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002190{
David S. Miller183cad12011-02-24 00:28:01 -05002191 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002192 struct xfrm_userpolicy_id *pol_id;
2193 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002194 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002195
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002196 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2197 if (nlh == NULL)
2198 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002199
Thomas Graf7b67c852007-08-22 13:53:52 -07002200 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002201 /* copy data from selector, dir, and type to the pol_id */
2202 memset(pol_id, 0, sizeof(*pol_id));
2203 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2204 pol_id->dir = dir;
2205
David S. Miller1d1e34d2012-06-27 21:57:03 -07002206 if (k != NULL) {
2207 err = copy_to_user_kmaddress(k, skb);
2208 if (err)
2209 goto out_cancel;
2210 }
2211 err = copy_to_user_policy_type(type, skb);
2212 if (err)
2213 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002214 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002215 err = copy_to_user_migrate(mp, skb);
2216 if (err)
2217 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002218 }
2219
Thomas Graf98250692007-08-22 12:47:26 -07002220 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002221
2222out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002223 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002224 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002225}
2226
David S. Miller183cad12011-02-24 00:28:01 -05002227static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2228 const struct xfrm_migrate *m, int num_migrate,
2229 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002230{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002231 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002233
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002234 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002235 if (skb == NULL)
2236 return -ENOMEM;
2237
2238 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002239 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002240 BUG();
2241
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002242 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002243}
2244#else
David S. Miller183cad12011-02-24 00:28:01 -05002245static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2246 const struct xfrm_migrate *m, int num_migrate,
2247 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002248{
2249 return -ENOPROTOOPT;
2250}
2251#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002252
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002253#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002254
2255static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002256 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002257 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2258 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2259 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2260 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2261 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2262 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002263 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002264 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002265 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002266 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002267 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002268 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002269 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002270 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2271 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002272 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002273 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002274 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2275 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276};
2277
Thomas Graf492b5582005-05-03 14:26:40 -07002278#undef XMSGSIZE
2279
Thomas Grafcf5cb792007-08-22 13:59:04 -07002280static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002281 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2282 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2283 [XFRMA_LASTUSED] = { .type = NLA_U64},
2284 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002285 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002286 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2287 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2288 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2289 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2290 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2291 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2292 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2293 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2294 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2295 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2296 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2297 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2298 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2299 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002300 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002301 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002302 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002303 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002304 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002305};
2306
Mathias Krause05600a72013-02-24 14:10:27 +01002307static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002308 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002310 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002311} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2312 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2313 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2314 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002315 .dump = xfrm_dump_sa,
2316 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002317 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2318 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2319 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002320 .dump = xfrm_dump_policy,
2321 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002322 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002323 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002324 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002325 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2326 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002327 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002328 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2329 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002330 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2331 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002332 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002333 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002334 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335};
2336
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002337static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002339 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002340 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002341 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002342 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 type -= XFRM_MSG_BASE;
2349 link = &xfrm_dispatch[type];
2350
2351 /* All operations require privileges, even GET */
Eric W. Biedermanbe0ef852014-04-23 14:29:27 -07002352 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002353 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Thomas Graf492b5582005-05-03 14:26:40 -07002355 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2356 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002357 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002361 {
2362 struct netlink_dump_control c = {
2363 .dump = link->dump,
2364 .done = link->done,
2365 };
2366 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369
Thomas Graf35a7aa02007-08-22 14:00:40 -07002370 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002371 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002372 if (err < 0)
2373 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Thomas Graf5424f322007-08-22 14:01:33 -07002378 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002381static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Fan Du283bc9f2013-11-07 17:47:50 +08002383 struct net *net = sock_net(skb->sk);
2384
2385 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002386 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002387 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
Thomas Graf7deb2262007-08-22 13:57:39 -07002390static inline size_t xfrm_expire_msgsize(void)
2391{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002392 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2393 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002394}
2395
David S. Miller214e0052011-02-24 00:02:38 -05002396static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
2398 struct xfrm_user_expire *ue;
2399 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002400 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Eric W. Biederman15e47302012-09-07 20:12:54 +00002402 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002403 if (nlh == NULL)
2404 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Thomas Graf7b67c852007-08-22 13:53:52 -07002406 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002408 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
David S. Miller1d1e34d2012-06-27 21:57:03 -07002410 err = xfrm_mark_put(skb, &x->mark);
2411 if (err)
2412 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002413
Thomas Graf98250692007-08-22 12:47:26 -07002414 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
David S. Miller214e0052011-02-24 00:02:38 -05002417static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002419 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct sk_buff *skb;
2421
Thomas Graf7deb2262007-08-22 13:57:39 -07002422 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (skb == NULL)
2424 return -ENOMEM;
2425
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002426 if (build_expire(skb, x, c) < 0) {
2427 kfree_skb(skb);
2428 return -EMSGSIZE;
2429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002431 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
David S. Miller214e0052011-02-24 00:02:38 -05002434static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002435{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002436 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002437 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002438
Steffen Klassertd8647b72011-03-08 00:10:27 +00002439 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002440 if (skb == NULL)
2441 return -ENOMEM;
2442
2443 if (build_aevent(skb, x, c) < 0)
2444 BUG();
2445
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002446 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002447}
2448
David S. Miller214e0052011-02-24 00:02:38 -05002449static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002450{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002451 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002452 struct xfrm_usersa_flush *p;
2453 struct nlmsghdr *nlh;
2454 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002455 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456
Thomas Graf7deb2262007-08-22 13:57:39 -07002457 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002458 if (skb == NULL)
2459 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460
Eric W. Biederman15e47302012-09-07 20:12:54 +00002461 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002462 if (nlh == NULL) {
2463 kfree_skb(skb);
2464 return -EMSGSIZE;
2465 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002466
Thomas Graf7b67c852007-08-22 13:53:52 -07002467 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002468 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002469
Thomas Graf98250692007-08-22 12:47:26 -07002470 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002471
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002472 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002473}
2474
Thomas Graf7deb2262007-08-22 13:57:39 -07002475static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002476{
Thomas Graf7deb2262007-08-22 13:57:39 -07002477 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002478 if (x->aead)
2479 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002480 if (x->aalg) {
2481 l += nla_total_size(sizeof(struct xfrm_algo) +
2482 (x->aalg->alg_key_len + 7) / 8);
2483 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2484 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002485 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002486 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002487 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002488 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002489 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002490 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002491 if (x->tfcpad)
2492 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002493 if (x->replay_esn)
2494 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002495 if (x->security)
2496 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2497 x->security->ctx_len);
2498 if (x->coaddr)
2499 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002500 if (x->props.extra_flags)
2501 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002502
Herbert Xud26f3982007-11-13 21:47:08 -08002503 /* Must count x->lastused as it may become non-zero behind our back. */
2504 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002505
2506 return l;
2507}
2508
David S. Miller214e0052011-02-24 00:02:38 -05002509static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002510{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002511 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002512 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002513 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002514 struct nlmsghdr *nlh;
2515 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002516 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002517 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002518
2519 headlen = sizeof(*p);
2520 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002521 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002522 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002523 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002524 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002525 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002526
Thomas Graf7deb2262007-08-22 13:57:39 -07002527 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002528 if (skb == NULL)
2529 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002530
Eric W. Biederman15e47302012-09-07 20:12:54 +00002531 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002532 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002533 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002534 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002535
Thomas Graf7b67c852007-08-22 13:53:52 -07002536 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002537 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002538 struct nlattr *attr;
2539
Thomas Graf7b67c852007-08-22 13:53:52 -07002540 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002541 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2542 id->spi = x->id.spi;
2543 id->family = x->props.family;
2544 id->proto = x->id.proto;
2545
Thomas Grafc0144be2007-08-22 13:55:43 -07002546 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002547 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002548 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002549 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002550
2551 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002552 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002553 err = copy_to_user_state_extra(x, p, skb);
2554 if (err)
2555 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002556
Thomas Graf98250692007-08-22 12:47:26 -07002557 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002558
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002559 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002560
David S. Miller1d1e34d2012-06-27 21:57:03 -07002561out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002562 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002563 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002564}
2565
David S. Miller214e0052011-02-24 00:02:38 -05002566static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002567{
2568
2569 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002570 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002571 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002572 case XFRM_MSG_NEWAE:
2573 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002574 case XFRM_MSG_DELSA:
2575 case XFRM_MSG_UPDSA:
2576 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002577 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002578 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002579 return xfrm_notify_sa_flush(c);
2580 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002581 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2582 c->event);
2583 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002584 }
2585
2586 return 0;
2587
2588}
2589
Thomas Graf7deb2262007-08-22 13:57:39 -07002590static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2591 struct xfrm_policy *xp)
2592{
2593 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2594 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002595 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002596 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2597 + userpolicy_type_attrsize();
2598}
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002601 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002603 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 struct xfrm_user_acquire *ua;
2605 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002606 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002608 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2609 if (nlh == NULL)
2610 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Thomas Graf7b67c852007-08-22 13:53:52 -07002612 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 memcpy(&ua->id, &x->id, sizeof(ua->id));
2614 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2615 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002616 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 ua->aalgos = xt->aalgos;
2618 ua->ealgos = xt->ealgos;
2619 ua->calgos = xt->calgos;
2620 ua->seq = x->km.seq = seq;
2621
David S. Miller1d1e34d2012-06-27 21:57:03 -07002622 err = copy_to_user_tmpl(xp, skb);
2623 if (!err)
2624 err = copy_to_user_state_sec_ctx(x, skb);
2625 if (!err)
2626 err = copy_to_user_policy_type(xp->type, skb);
2627 if (!err)
2628 err = xfrm_mark_put(skb, &xp->mark);
2629 if (err) {
2630 nlmsg_cancel(skb, nlh);
2631 return err;
2632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Thomas Graf98250692007-08-22 12:47:26 -07002634 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
2637static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002638 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002640 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Thomas Graf7deb2262007-08-22 13:57:39 -07002643 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (skb == NULL)
2645 return -ENOMEM;
2646
Fan Du65e07362012-08-15 10:13:47 +08002647 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 BUG();
2649
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002650 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
2653/* User gives us xfrm_user_policy_info followed by an array of 0
2654 * or more templates.
2655 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002656static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 u8 *data, int len, int *dir)
2658{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002659 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2661 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2662 struct xfrm_policy *xp;
2663 int nr;
2664
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002665 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 case AF_INET:
2667 if (opt != IP_XFRM_POLICY) {
2668 *dir = -EOPNOTSUPP;
2669 return NULL;
2670 }
2671 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002672#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 case AF_INET6:
2674 if (opt != IPV6_XFRM_POLICY) {
2675 *dir = -EOPNOTSUPP;
2676 return NULL;
2677 }
2678 break;
2679#endif
2680 default:
2681 *dir = -EINVAL;
2682 return NULL;
2683 }
2684
2685 *dir = -EINVAL;
2686
2687 if (len < sizeof(*p) ||
2688 verify_newpolicy_info(p))
2689 return NULL;
2690
2691 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002692 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return NULL;
2694
Herbert Xua4f1bac2005-07-26 15:43:17 -07002695 if (p->dir > XFRM_POLICY_OUT)
2696 return NULL;
2697
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002698 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (xp == NULL) {
2700 *dir = -ENOBUFS;
2701 return NULL;
2702 }
2703
2704 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002705 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 copy_templates(xp, ut, nr);
2707
2708 *dir = p->dir;
2709
2710 return xp;
2711}
2712
Thomas Graf7deb2262007-08-22 13:57:39 -07002713static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2714{
2715 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2716 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2717 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002718 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002719 + userpolicy_type_attrsize();
2720}
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002723 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002726 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002727 struct nlmsghdr *nlh;
2728 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Eric W. Biederman15e47302012-09-07 20:12:54 +00002730 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002731 if (nlh == NULL)
2732 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Thomas Graf7b67c852007-08-22 13:53:52 -07002734 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002736 err = copy_to_user_tmpl(xp, skb);
2737 if (!err)
2738 err = copy_to_user_sec_ctx(xp, skb);
2739 if (!err)
2740 err = copy_to_user_policy_type(xp->type, skb);
2741 if (!err)
2742 err = xfrm_mark_put(skb, &xp->mark);
2743 if (err) {
2744 nlmsg_cancel(skb, nlh);
2745 return err;
2746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 upe->hard = !!hard;
2748
Thomas Graf98250692007-08-22 12:47:26 -07002749 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
David S. Miller214e0052011-02-24 00:02:38 -05002752static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002754 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Thomas Graf7deb2262007-08-22 13:57:39 -07002757 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (skb == NULL)
2759 return -ENOMEM;
2760
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002761 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 BUG();
2763
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002764 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
David S. Miller214e0052011-02-24 00:02:38 -05002767static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002768{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002769 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002770 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002771 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002772 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002773 struct nlmsghdr *nlh;
2774 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002775 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002776
2777 headlen = sizeof(*p);
2778 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002779 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002780 headlen = sizeof(*id);
2781 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002782 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002783 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002784 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002785
Thomas Graf7deb2262007-08-22 13:57:39 -07002786 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002787 if (skb == NULL)
2788 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002789
Eric W. Biederman15e47302012-09-07 20:12:54 +00002790 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002791 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002792 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002793 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002794
Thomas Graf7b67c852007-08-22 13:53:52 -07002795 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002796 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002797 struct nlattr *attr;
2798
Thomas Graf7b67c852007-08-22 13:53:52 -07002799 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002800 memset(id, 0, sizeof(*id));
2801 id->dir = dir;
2802 if (c->data.byid)
2803 id->index = xp->index;
2804 else
2805 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2806
Thomas Grafc0144be2007-08-22 13:55:43 -07002807 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002808 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002809 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002810 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002811
2812 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002813 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002814
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002815 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002816 err = copy_to_user_tmpl(xp, skb);
2817 if (!err)
2818 err = copy_to_user_policy_type(xp->type, skb);
2819 if (!err)
2820 err = xfrm_mark_put(skb, &xp->mark);
2821 if (err)
2822 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002823
Thomas Graf98250692007-08-22 12:47:26 -07002824 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002825
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002826 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002827
David S. Miller1d1e34d2012-06-27 21:57:03 -07002828out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002829 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002830 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002831}
2832
David S. Miller214e0052011-02-24 00:02:38 -05002833static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002834{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002835 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002836 struct nlmsghdr *nlh;
2837 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002838 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002839
Thomas Graf7deb2262007-08-22 13:57:39 -07002840 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002841 if (skb == NULL)
2842 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002843
Eric W. Biederman15e47302012-09-07 20:12:54 +00002844 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002845 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002846 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002847 goto out_free_skb;
2848 err = copy_to_user_policy_type(c->data.type, skb);
2849 if (err)
2850 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002851
Thomas Graf98250692007-08-22 12:47:26 -07002852 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002853
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002854 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002855
David S. Miller1d1e34d2012-06-27 21:57:03 -07002856out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002857 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002858 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002859}
2860
David S. Miller214e0052011-02-24 00:02:38 -05002861static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002862{
2863
2864 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002865 case XFRM_MSG_NEWPOLICY:
2866 case XFRM_MSG_UPDPOLICY:
2867 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002868 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002869 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002870 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002871 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002872 return xfrm_exp_policy_notify(xp, dir, c);
2873 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002874 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2875 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002876 }
2877
2878 return 0;
2879
2880}
2881
Thomas Graf7deb2262007-08-22 13:57:39 -07002882static inline size_t xfrm_report_msgsize(void)
2883{
2884 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2885}
2886
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002887static int build_report(struct sk_buff *skb, u8 proto,
2888 struct xfrm_selector *sel, xfrm_address_t *addr)
2889{
2890 struct xfrm_user_report *ur;
2891 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002892
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002893 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2894 if (nlh == NULL)
2895 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002896
Thomas Graf7b67c852007-08-22 13:53:52 -07002897 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002898 ur->proto = proto;
2899 memcpy(&ur->sel, sel, sizeof(ur->sel));
2900
David S. Miller1d1e34d2012-06-27 21:57:03 -07002901 if (addr) {
2902 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2903 if (err) {
2904 nlmsg_cancel(skb, nlh);
2905 return err;
2906 }
2907 }
Thomas Graf98250692007-08-22 12:47:26 -07002908 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002909}
2910
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002911static int xfrm_send_report(struct net *net, u8 proto,
2912 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002913{
2914 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002915
Thomas Graf7deb2262007-08-22 13:57:39 -07002916 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002917 if (skb == NULL)
2918 return -ENOMEM;
2919
2920 if (build_report(skb, proto, sel, addr) < 0)
2921 BUG();
2922
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002923 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002924}
2925
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002926static inline size_t xfrm_mapping_msgsize(void)
2927{
2928 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2929}
2930
2931static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2932 xfrm_address_t *new_saddr, __be16 new_sport)
2933{
2934 struct xfrm_user_mapping *um;
2935 struct nlmsghdr *nlh;
2936
2937 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2938 if (nlh == NULL)
2939 return -EMSGSIZE;
2940
2941 um = nlmsg_data(nlh);
2942
2943 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2944 um->id.spi = x->id.spi;
2945 um->id.family = x->props.family;
2946 um->id.proto = x->id.proto;
2947 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2948 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2949 um->new_sport = new_sport;
2950 um->old_sport = x->encap->encap_sport;
2951 um->reqid = x->props.reqid;
2952
2953 return nlmsg_end(skb, nlh);
2954}
2955
2956static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2957 __be16 sport)
2958{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002959 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002960 struct sk_buff *skb;
2961
2962 if (x->id.proto != IPPROTO_ESP)
2963 return -EINVAL;
2964
2965 if (!x->encap)
2966 return -EINVAL;
2967
2968 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2969 if (skb == NULL)
2970 return -ENOMEM;
2971
2972 if (build_mapping(skb, x, ipaddr, sport) < 0)
2973 BUG();
2974
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002975 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002976}
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978static struct xfrm_mgr netlink_mgr = {
2979 .id = "netlink",
2980 .notify = xfrm_send_state_notify,
2981 .acquire = xfrm_send_acquire,
2982 .compile_policy = xfrm_compile_policy,
2983 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002984 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002985 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002986 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987};
2988
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002989static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Patrick McHardybe336902006-03-20 22:40:54 -08002991 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002992 struct netlink_kernel_cfg cfg = {
2993 .groups = XFRMNLGRP_MAX,
2994 .input = xfrm_netlink_rcv,
2995 };
Patrick McHardybe336902006-03-20 22:40:54 -08002996
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002997 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08002998 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003000 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003001 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return 0;
3003}
3004
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003005static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003006{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003007 struct net *net;
3008 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003009 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003010 synchronize_net();
3011 list_for_each_entry(net, net_exit_list, exit_list)
3012 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003013}
3014
3015static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003016 .init = xfrm_user_net_init,
3017 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003018};
3019
3020static int __init xfrm_user_init(void)
3021{
3022 int rv;
3023
3024 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3025
3026 rv = register_pernet_subsys(&xfrm_user_net_ops);
3027 if (rv < 0)
3028 return rv;
3029 rv = xfrm_register_km(&netlink_mgr);
3030 if (rv < 0)
3031 unregister_pernet_subsys(&xfrm_user_net_ops);
3032 return rv;
3033}
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035static void __exit xfrm_user_exit(void)
3036{
3037 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003038 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
3041module_init(xfrm_user_init);
3042module_exit(xfrm_user_exit);
3043MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003044MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003045