blob: 4ec3ccd7d517a8e1792b69cf034d6b8dd88b6793 [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
Herbert Xu1a6509d2008-01-28 19:37:29 -080035static inline int aead_len(struct xfrm_algo_aead *alg)
36{
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
38}
39
Thomas Graf5424f322007-08-22 14:01:33 -070040static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Thomas Graf5424f322007-08-22 14:01:33 -070042 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct xfrm_algo *algp;
44
45 if (!rt)
46 return 0;
47
Thomas Graf5424f322007-08-22 14:01:33 -070048 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080049 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070050 return -EINVAL;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (type) {
53 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 break;
57
58 default:
59 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
63 return 0;
64}
65
Martin Willi4447bb32009-11-25 00:29:52 +000066static int verify_auth_trunc(struct nlattr **attrs)
67{
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
70
71 if (!rt)
72 return 0;
73
74 algp = nla_data(rt);
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
76 return -EINVAL;
77
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79 return 0;
80}
81
Herbert Xu1a6509d2008-01-28 19:37:29 -080082static int verify_aead(struct nlattr **attrs)
83{
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
86
87 if (!rt)
88 return 0;
89
90 algp = nla_data(rt);
91 if (nla_len(rt) < aead_len(algp))
92 return -EINVAL;
93
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
95 return 0;
96}
97
Thomas Graf5424f322007-08-22 14:01:33 -070098static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070099 xfrm_address_t **addrp)
100{
Thomas Graf5424f322007-08-22 14:01:33 -0700101 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700102
Thomas Grafcf5cb792007-08-22 13:59:04 -0700103 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700104 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700105}
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
Thomas Graf5424f322007-08-22 14:01:33 -0700107static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800108{
Thomas Graf5424f322007-08-22 14:01:33 -0700109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800110 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800111
112 if (!rt)
113 return 0;
114
Thomas Graf5424f322007-08-22 14:01:33 -0700115 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800117 return -EINVAL;
118
119 return 0;
120}
121
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
124{
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
126
Steffen Klassert7833aa02011-04-25 19:41:21 +0000127 if ((p->flags & XFRM_STATE_ESN) && !rt)
128 return -EINVAL;
129
Steffen Klassertd8647b72011-03-08 00:10:27 +0000130 if (!rt)
131 return 0;
132
Steffen Klassert02aadf72011-03-28 19:48:09 +0000133 if (p->id.proto != IPPROTO_ESP)
134 return -EINVAL;
135
Steffen Klassertd8647b72011-03-08 00:10:27 +0000136 if (p->replay_window != 0)
137 return -EINVAL;
138
139 return 0;
140}
Trent Jaegerdf718372005-12-13 23:12:27 -0800141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700143 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int err;
146
147 err = -EINVAL;
148 switch (p->family) {
149 case AF_INET:
150 break;
151
152 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000153#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 break;
155#else
156 err = -EAFNOSUPPORT;
157 goto out;
158#endif
159
160 default:
161 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 err = -EINVAL;
165 switch (p->id.proto) {
166 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000167 if ((!attrs[XFRMA_ALG_AUTH] &&
168 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800169 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700170 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000171 attrs[XFRMA_ALG_COMP] ||
172 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 goto out;
174 break;
175
176 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800177 if (attrs[XFRMA_ALG_COMP])
178 goto out;
179 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000180 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800181 !attrs[XFRMA_ALG_CRYPT] &&
182 !attrs[XFRMA_ALG_AEAD])
183 goto out;
184 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000185 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800186 attrs[XFRMA_ALG_CRYPT]) &&
187 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000189 if (attrs[XFRMA_TFCPAD] &&
190 p->mode != XFRM_MODE_TUNNEL)
191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193
194 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700195 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800196 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700197 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000198 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000199 attrs[XFRMA_ALG_CRYPT] ||
200 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto out;
202 break;
203
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000204#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700205 case IPPROTO_DSTOPTS:
206 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 if (attrs[XFRMA_ALG_COMP] ||
208 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000209 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800210 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700211 attrs[XFRMA_ALG_CRYPT] ||
212 attrs[XFRMA_ENCAP] ||
213 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000214 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700215 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700216 goto out;
217 break;
218#endif
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 default:
221 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Herbert Xu1a6509d2008-01-28 19:37:29 -0800224 if ((err = verify_aead(attrs)))
225 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000226 if ((err = verify_auth_trunc(attrs)))
227 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700228 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700230 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700232 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700234 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800235 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000236 if ((err = verify_replay(p, attrs)))
237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 err = -EINVAL;
240 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700241 case XFRM_MODE_TRANSPORT:
242 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700243 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700244 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246
247 default:
248 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 err = 0;
252
253out:
254 return err;
255}
256
257static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800258 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700259 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct xfrm_algo *p, *ualg;
262 struct xfrm_algo_desc *algo;
263
264 if (!rta)
265 return 0;
266
Thomas Graf5424f322007-08-22 14:01:33 -0700267 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 algo = get_byname(ualg->alg_name, 1);
270 if (!algo)
271 return -ENOSYS;
272 *props = algo->desc.sadb_alg_id;
273
Eric Dumazet0f99be02008-01-08 23:39:06 -0800274 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!p)
276 return -ENOMEM;
277
Herbert Xu04ff1262006-08-13 08:50:00 +1000278 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *algpp = p;
280 return 0;
281}
282
Martin Willi4447bb32009-11-25 00:29:52 +0000283static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
284 struct nlattr *rta)
285{
286 struct xfrm_algo *ualg;
287 struct xfrm_algo_auth *p;
288 struct xfrm_algo_desc *algo;
289
290 if (!rta)
291 return 0;
292
293 ualg = nla_data(rta);
294
295 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
296 if (!algo)
297 return -ENOSYS;
298 *props = algo->desc.sadb_alg_id;
299
300 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
301 if (!p)
302 return -ENOMEM;
303
304 strcpy(p->alg_name, algo->name);
305 p->alg_key_len = ualg->alg_key_len;
306 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
307 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
308
309 *algpp = p;
310 return 0;
311}
312
313static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
314 struct nlattr *rta)
315{
316 struct xfrm_algo_auth *p, *ualg;
317 struct xfrm_algo_desc *algo;
318
319 if (!rta)
320 return 0;
321
322 ualg = nla_data(rta);
323
324 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
325 if (!algo)
326 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000327 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
328 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000329 return -EINVAL;
330 *props = algo->desc.sadb_alg_id;
331
332 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
333 if (!p)
334 return -ENOMEM;
335
336 strcpy(p->alg_name, algo->name);
337 if (!p->alg_trunc_len)
338 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
339
340 *algpp = p;
341 return 0;
342}
343
Herbert Xu1a6509d2008-01-28 19:37:29 -0800344static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
345 struct nlattr *rta)
346{
347 struct xfrm_algo_aead *p, *ualg;
348 struct xfrm_algo_desc *algo;
349
350 if (!rta)
351 return 0;
352
353 ualg = nla_data(rta);
354
355 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
356 if (!algo)
357 return -ENOSYS;
358 *props = algo->desc.sadb_alg_id;
359
360 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
361 if (!p)
362 return -ENOMEM;
363
364 strcpy(p->alg_name, algo->name);
365 *algpp = p;
366 return 0;
367}
368
Steffen Klasserte2b19122011-03-28 19:47:30 +0000369static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
370 struct nlattr *rp)
371{
372 struct xfrm_replay_state_esn *up;
373
374 if (!replay_esn || !rp)
375 return 0;
376
377 up = nla_data(rp);
378
379 if (xfrm_replay_state_esn_len(replay_esn) !=
380 xfrm_replay_state_esn_len(up))
381 return -EINVAL;
382
383 return 0;
384}
385
Steffen Klassertd8647b72011-03-08 00:10:27 +0000386static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
387 struct xfrm_replay_state_esn **preplay_esn,
388 struct nlattr *rta)
389{
390 struct xfrm_replay_state_esn *p, *pp, *up;
391
392 if (!rta)
393 return 0;
394
395 up = nla_data(rta);
396
397 p = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
398 if (!p)
399 return -ENOMEM;
400
401 pp = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
402 if (!pp) {
403 kfree(p);
404 return -ENOMEM;
405 }
406
407 *replay_esn = p;
408 *preplay_esn = pp;
409
410 return 0;
411}
412
Joy Latten661697f2007-04-13 16:14:35 -0700413static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800414{
Trent Jaegerdf718372005-12-13 23:12:27 -0800415 int len = 0;
416
417 if (xfrm_ctx) {
418 len += sizeof(struct xfrm_user_sec_ctx);
419 len += xfrm_ctx->ctx_len;
420 }
421 return len;
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
425{
426 memcpy(&x->id, &p->id, sizeof(x->id));
427 memcpy(&x->sel, &p->sel, sizeof(x->sel));
428 memcpy(&x->lft, &p->lft, sizeof(x->lft));
429 x->props.mode = p->mode;
430 x->props.replay_window = p->replay_window;
431 x->props.reqid = p->reqid;
432 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700433 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700435
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700436 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700437 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800440/*
441 * someday when pfkey also has support, we could have the code
442 * somehow made shareable and move it to xfrm_state.c - JHS
443 *
444*/
Thomas Graf5424f322007-08-22 14:01:33 -0700445static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800446{
Thomas Graf5424f322007-08-22 14:01:33 -0700447 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +0000448 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -0700449 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
450 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
451 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800452
Steffen Klassertd8647b72011-03-08 00:10:27 +0000453 if (re) {
454 struct xfrm_replay_state_esn *replay_esn;
455 replay_esn = nla_data(re);
456 memcpy(x->replay_esn, replay_esn,
457 xfrm_replay_state_esn_len(replay_esn));
458 memcpy(x->preplay_esn, replay_esn,
459 xfrm_replay_state_esn_len(replay_esn));
460 }
461
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800462 if (rp) {
463 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700464 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800465 memcpy(&x->replay, replay, sizeof(*replay));
466 memcpy(&x->preplay, replay, sizeof(*replay));
467 }
468
469 if (lt) {
470 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700471 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800472 x->curlft.bytes = ltime->bytes;
473 x->curlft.packets = ltime->packets;
474 x->curlft.add_time = ltime->add_time;
475 x->curlft.use_time = ltime->use_time;
476 }
477
Thomas Grafcf5cb792007-08-22 13:59:04 -0700478 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700479 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800480
Thomas Grafcf5cb792007-08-22 13:59:04 -0700481 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700482 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800483}
484
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800485static struct xfrm_state *xfrm_state_construct(struct net *net,
486 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700487 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int *errp)
489{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800490 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int err = -ENOMEM;
492
493 if (!x)
494 goto error_no_put;
495
496 copy_from_user_state(x, p);
497
Herbert Xu1a6509d2008-01-28 19:37:29 -0800498 if ((err = attach_aead(&x->aead, &x->props.ealgo,
499 attrs[XFRMA_ALG_AEAD])))
500 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000501 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
502 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000504 if (!x->props.aalgo) {
505 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
506 attrs[XFRMA_ALG_AUTH])))
507 goto error;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
510 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700511 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto error;
513 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
514 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700515 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700517
518 if (attrs[XFRMA_ENCAP]) {
519 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
520 sizeof(*x->encap), GFP_KERNEL);
521 if (x->encap == NULL)
522 goto error;
523 }
524
Martin Willi35d28562010-12-08 04:37:49 +0000525 if (attrs[XFRMA_TFCPAD])
526 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
527
Thomas Graffd211502007-09-06 03:28:08 -0700528 if (attrs[XFRMA_COADDR]) {
529 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
530 sizeof(*x->coaddr), GFP_KERNEL);
531 if (x->coaddr == NULL)
532 goto error;
533 }
534
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000535 xfrm_mark_get(attrs, &x->mark);
536
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700537 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (err)
539 goto error;
540
Thomas Graffd211502007-09-06 03:28:08 -0700541 if (attrs[XFRMA_SEC_CTX] &&
542 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800543 goto error;
544
Steffen Klassertd8647b72011-03-08 00:10:27 +0000545 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
546 attrs[XFRMA_REPLAY_ESN_VAL])))
547 goto error;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800550 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800551 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800552 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800553
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000554 if ((err = xfrm_init_replay(x)))
555 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800556
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000557 /* override default values from above */
Thomas Graf5424f322007-08-22 14:01:33 -0700558 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return x;
561
562error:
563 x->km.state = XFRM_STATE_DEAD;
564 xfrm_state_put(x);
565error_no_put:
566 *errp = err;
567 return NULL;
568}
569
Christoph Hellwig22e70052007-01-02 15:22:30 -0800570static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700571 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800573 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700574 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct xfrm_state *x;
576 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700577 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800578 uid_t loginuid = audit_get_loginuid(current);
579 u32 sessionid = audit_get_sessionid(current);
580 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Thomas Graf35a7aa02007-08-22 14:00:40 -0700582 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (err)
584 return err;
585
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800586 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!x)
588 return err;
589
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700590 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
592 err = xfrm_state_add(x);
593 else
594 err = xfrm_state_update(x);
595
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800596 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400597 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (err < 0) {
600 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800601 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700605 c.seq = nlh->nlmsg_seq;
606 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700607 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700608
609 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700610out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700611 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return err;
613}
614
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800615static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
616 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700617 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700618 int *errp)
619{
620 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000621 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700622 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000623 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700624
625 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
626 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000627 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700628 } else {
629 xfrm_address_t *saddr = NULL;
630
Thomas Graf35a7aa02007-08-22 14:00:40 -0700631 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700632 if (!saddr) {
633 err = -EINVAL;
634 goto out;
635 }
636
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800637 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000638 x = xfrm_state_lookup_byaddr(net, mark,
639 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800640 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700641 }
642
643 out:
644 if (!x && errp)
645 *errp = err;
646 return x;
647}
648
Christoph Hellwig22e70052007-01-02 15:22:30 -0800649static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700650 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800652 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700654 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700655 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700656 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800657 uid_t loginuid = audit_get_loginuid(current);
658 u32 sessionid = audit_get_sessionid(current);
659 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800661 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
David S. Miller6f68dc32006-06-08 23:58:52 -0700665 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700666 goto out;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700669 err = -EPERM;
670 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700673 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600674
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700675 if (err < 0)
676 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700677
678 c.seq = nlh->nlmsg_seq;
679 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700680 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700681 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700683out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800684 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400685 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700686 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700687 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
691{
Mathias Kraused5f1f7c2012-09-19 11:33:39 +0000692 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 memcpy(&p->id, &x->id, sizeof(p->id));
694 memcpy(&p->sel, &x->sel, sizeof(p->sel));
695 memcpy(&p->lft, &x->lft, sizeof(p->lft));
696 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
697 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700698 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 p->mode = x->props.mode;
700 p->replay_window = x->props.replay_window;
701 p->reqid = x->props.reqid;
702 p->family = x->props.family;
703 p->flags = x->props.flags;
704 p->seq = x->km.seq;
705}
706
707struct xfrm_dump_info {
708 struct sk_buff *in_skb;
709 struct sk_buff *out_skb;
710 u32 nlmsg_seq;
711 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712};
713
Thomas Grafc0144be2007-08-22 13:55:43 -0700714static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
715{
Thomas Grafc0144be2007-08-22 13:55:43 -0700716 struct xfrm_user_sec_ctx *uctx;
717 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700718 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700719
720 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
721 if (attr == NULL)
722 return -EMSGSIZE;
723
724 uctx = nla_data(attr);
725 uctx->exttype = XFRMA_SEC_CTX;
726 uctx->len = ctx_size;
727 uctx->ctx_doi = s->ctx_doi;
728 uctx->ctx_alg = s->ctx_alg;
729 uctx->ctx_len = s->ctx_len;
730 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
731
732 return 0;
733}
734
Martin Willi4447bb32009-11-25 00:29:52 +0000735static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
736{
737 struct xfrm_algo *algo;
738 struct nlattr *nla;
739
740 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
741 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
742 if (!nla)
743 return -EMSGSIZE;
744
745 algo = nla_data(nla);
Mathias Krause37d61a22012-09-19 11:33:38 +0000746 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000747 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
748 algo->alg_key_len = auth->alg_key_len;
749
750 return 0;
751}
752
Herbert Xu68325d32007-10-09 13:30:57 -0700753/* Don't change this without updating xfrm_sa_len! */
754static int copy_to_user_state_extra(struct xfrm_state *x,
755 struct xfrm_usersa_info *p,
756 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 copy_to_user_state(x, p);
759
Herbert Xu050f0092007-10-09 13:31:47 -0700760 if (x->coaddr)
761 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
762
763 if (x->lastused)
764 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700765
Herbert Xu1a6509d2008-01-28 19:37:29 -0800766 if (x->aead)
767 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000768 if (x->aalg) {
769 if (copy_to_user_auth(x->aalg, skb))
770 goto nla_put_failure;
771
772 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
773 xfrm_alg_auth_len(x->aalg), x->aalg);
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800776 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700778 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700781 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Martin Willi35d28562010-12-08 04:37:49 +0000783 if (x->tfcpad)
784 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
785
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000786 if (xfrm_mark_put(skb, &x->mark))
787 goto nla_put_failure;
788
Steffen Klassertd8647b72011-03-08 00:10:27 +0000789 if (x->replay_esn)
790 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
791 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
792
Thomas Grafc0144be2007-08-22 13:55:43 -0700793 if (x->security && copy_sec_ctx(x->security, skb) < 0)
794 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700795
Herbert Xu68325d32007-10-09 13:30:57 -0700796 return 0;
797
798nla_put_failure:
799 return -EMSGSIZE;
800}
801
802static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
803{
804 struct xfrm_dump_info *sp = ptr;
805 struct sk_buff *in_skb = sp->in_skb;
806 struct sk_buff *skb = sp->out_skb;
807 struct xfrm_usersa_info *p;
808 struct nlmsghdr *nlh;
809 int err;
810
Herbert Xu68325d32007-10-09 13:30:57 -0700811 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
812 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
813 if (nlh == NULL)
814 return -EMSGSIZE;
815
816 p = nlmsg_data(nlh);
817
818 err = copy_to_user_state_extra(x, p, skb);
819 if (err)
820 goto nla_put_failure;
821
Thomas Graf98250692007-08-22 12:47:26 -0700822 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824
Thomas Grafc0144be2007-08-22 13:55:43 -0700825nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700826 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700827 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Timo Teras4c563f72008-02-28 21:31:08 -0800830static int xfrm_dump_sa_done(struct netlink_callback *cb)
831{
832 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
833 xfrm_state_walk_done(walk);
834 return 0;
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
838{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800839 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800840 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct xfrm_dump_info info;
842
Timo Teras4c563f72008-02-28 21:31:08 -0800843 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
844 sizeof(cb->args) - sizeof(cb->args[0]));
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 info.in_skb = cb->skb;
847 info.out_skb = skb;
848 info.nlmsg_seq = cb->nlh->nlmsg_seq;
849 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800850
851 if (!cb->args[0]) {
852 cb->args[0] = 1;
853 xfrm_state_walk_init(walk, 0);
854 }
855
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800856 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 return skb->len;
859}
860
861static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
862 struct xfrm_state *x, u32 seq)
863{
864 struct xfrm_dump_info info;
865 struct sk_buff *skb;
Mathias Krause555144b2012-09-13 11:41:26 +0000866 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Thomas Graf7deb2262007-08-22 13:57:39 -0700868 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!skb)
870 return ERR_PTR(-ENOMEM);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 info.in_skb = in_skb;
873 info.out_skb = skb;
874 info.nlmsg_seq = seq;
875 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Mathias Krause555144b2012-09-13 11:41:26 +0000877 err = dump_one_state(x, 0, &info);
878 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 kfree_skb(skb);
Mathias Krause555144b2012-09-13 11:41:26 +0000880 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
883 return skb;
884}
885
Thomas Graf7deb2262007-08-22 13:57:39 -0700886static inline size_t xfrm_spdinfo_msgsize(void)
887{
888 return NLMSG_ALIGN(4)
889 + nla_total_size(sizeof(struct xfrmu_spdinfo))
890 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
891}
892
Alexey Dobriyane0710412010-01-23 13:37:10 +0000893static int build_spdinfo(struct sk_buff *skb, struct net *net,
894 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700895{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700896 struct xfrmk_spdinfo si;
897 struct xfrmu_spdinfo spc;
898 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700899 struct nlmsghdr *nlh;
900 u32 *f;
901
902 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300903 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700904 return -EMSGSIZE;
905
906 f = nlmsg_data(nlh);
907 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000908 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700909 spc.incnt = si.incnt;
910 spc.outcnt = si.outcnt;
911 spc.fwdcnt = si.fwdcnt;
912 spc.inscnt = si.inscnt;
913 spc.outscnt = si.outscnt;
914 spc.fwdscnt = si.fwdscnt;
915 sph.spdhcnt = si.spdhcnt;
916 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700917
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700918 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
919 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700920
921 return nlmsg_end(skb, nlh);
922
923nla_put_failure:
924 nlmsg_cancel(skb, nlh);
925 return -EMSGSIZE;
926}
927
928static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700929 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700930{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800931 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700932 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700933 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700934 u32 spid = NETLINK_CB(skb).pid;
935 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700936
Thomas Graf7deb2262007-08-22 13:57:39 -0700937 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700938 if (r_skb == NULL)
939 return -ENOMEM;
940
Alexey Dobriyane0710412010-01-23 13:37:10 +0000941 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700942 BUG();
943
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800944 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700945}
946
Thomas Graf7deb2262007-08-22 13:57:39 -0700947static inline size_t xfrm_sadinfo_msgsize(void)
948{
949 return NLMSG_ALIGN(4)
950 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
951 + nla_total_size(4); /* XFRMA_SAD_CNT */
952}
953
Alexey Dobriyane0710412010-01-23 13:37:10 +0000954static int build_sadinfo(struct sk_buff *skb, struct net *net,
955 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700956{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700957 struct xfrmk_sadinfo si;
958 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700959 struct nlmsghdr *nlh;
960 u32 *f;
961
962 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300963 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700964 return -EMSGSIZE;
965
966 f = nlmsg_data(nlh);
967 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000968 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700969
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700970 sh.sadhmcnt = si.sadhmcnt;
971 sh.sadhcnt = si.sadhcnt;
972
973 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
974 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700975
976 return nlmsg_end(skb, nlh);
977
978nla_put_failure:
979 nlmsg_cancel(skb, nlh);
980 return -EMSGSIZE;
981}
982
983static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700984 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700985{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800986 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700987 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700988 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700989 u32 spid = NETLINK_CB(skb).pid;
990 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700991
Thomas Graf7deb2262007-08-22 13:57:39 -0700992 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700993 if (r_skb == NULL)
994 return -ENOMEM;
995
Alexey Dobriyane0710412010-01-23 13:37:10 +0000996 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700997 BUG();
998
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800999 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001000}
1001
Christoph Hellwig22e70052007-01-02 15:22:30 -08001002static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001003 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001005 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001006 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct xfrm_state *x;
1008 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001009 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001011 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (x == NULL)
1013 goto out_noput;
1014
1015 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1016 if (IS_ERR(resp_skb)) {
1017 err = PTR_ERR(resp_skb);
1018 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001019 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 xfrm_state_put(x);
1022out_noput:
1023 return err;
1024}
1025
1026static int verify_userspi_info(struct xfrm_userspi_info *p)
1027{
1028 switch (p->info.id.proto) {
1029 case IPPROTO_AH:
1030 case IPPROTO_ESP:
1031 break;
1032
1033 case IPPROTO_COMP:
1034 /* IPCOMP spi is 16-bits. */
1035 if (p->max >= 0x10000)
1036 return -EINVAL;
1037 break;
1038
1039 default:
1040 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (p->min > p->max)
1044 return -EINVAL;
1045
1046 return 0;
1047}
1048
Christoph Hellwig22e70052007-01-02 15:22:30 -08001049static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001050 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001052 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct xfrm_state *x;
1054 struct xfrm_userspi_info *p;
1055 struct sk_buff *resp_skb;
1056 xfrm_address_t *daddr;
1057 int family;
1058 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001059 u32 mark;
1060 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Thomas Graf7b67c852007-08-22 13:53:52 -07001062 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 err = verify_userspi_info(p);
1064 if (err)
1065 goto out_noput;
1066
1067 family = p->info.family;
1068 daddr = &p->info.id.daddr;
1069
1070 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001071
1072 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001074 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1076 xfrm_state_put(x);
1077 x = NULL;
1078 }
1079 }
1080
1081 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001082 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 p->info.id.proto, daddr,
1084 &p->info.saddr, 1,
1085 family);
1086 err = -ENOENT;
1087 if (x == NULL)
1088 goto out_noput;
1089
Herbert Xu658b2192007-10-09 13:29:52 -07001090 err = xfrm_alloc_spi(x, p->min, p->max);
1091 if (err)
1092 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Herbert Xu658b2192007-10-09 13:29:52 -07001094 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (IS_ERR(resp_skb)) {
1096 err = PTR_ERR(resp_skb);
1097 goto out;
1098 }
1099
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001100 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102out:
1103 xfrm_state_put(x);
1104out_noput:
1105 return err;
1106}
1107
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001108static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 switch (dir) {
1111 case XFRM_POLICY_IN:
1112 case XFRM_POLICY_OUT:
1113 case XFRM_POLICY_FWD:
1114 break;
1115
1116 default:
1117 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 return 0;
1121}
1122
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001123static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001124{
1125 switch (type) {
1126 case XFRM_POLICY_TYPE_MAIN:
1127#ifdef CONFIG_XFRM_SUB_POLICY
1128 case XFRM_POLICY_TYPE_SUB:
1129#endif
1130 break;
1131
1132 default:
1133 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001134 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001135
1136 return 0;
1137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1140{
1141 switch (p->share) {
1142 case XFRM_SHARE_ANY:
1143 case XFRM_SHARE_SESSION:
1144 case XFRM_SHARE_USER:
1145 case XFRM_SHARE_UNIQUE:
1146 break;
1147
1148 default:
1149 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 switch (p->action) {
1153 case XFRM_POLICY_ALLOW:
1154 case XFRM_POLICY_BLOCK:
1155 break;
1156
1157 default:
1158 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 switch (p->sel.family) {
1162 case AF_INET:
1163 break;
1164
1165 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001166#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168#else
1169 return -EAFNOSUPPORT;
1170#endif
1171
1172 default:
1173 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 return verify_policy_dir(p->dir);
1177}
1178
Thomas Graf5424f322007-08-22 14:01:33 -07001179static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001180{
Thomas Graf5424f322007-08-22 14:01:33 -07001181 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001182 struct xfrm_user_sec_ctx *uctx;
1183
1184 if (!rt)
1185 return 0;
1186
Thomas Graf5424f322007-08-22 14:01:33 -07001187 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001188 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1192 int nr)
1193{
1194 int i;
1195
1196 xp->xfrm_nr = nr;
1197 for (i = 0; i < nr; i++, ut++) {
1198 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1199
1200 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1201 memcpy(&t->saddr, &ut->saddr,
1202 sizeof(xfrm_address_t));
1203 t->reqid = ut->reqid;
1204 t->mode = ut->mode;
1205 t->share = ut->share;
1206 t->optional = ut->optional;
1207 t->aalgos = ut->aalgos;
1208 t->ealgos = ut->ealgos;
1209 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001210 /* If all masks are ~0, then we allow all algorithms. */
1211 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001212 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
1215
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001216static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1217{
1218 int i;
1219
1220 if (nr > XFRM_MAX_DEPTH)
1221 return -EINVAL;
1222
1223 for (i = 0; i < nr; i++) {
1224 /* We never validated the ut->family value, so many
1225 * applications simply leave it at zero. The check was
1226 * never made and ut->family was ignored because all
1227 * templates could be assumed to have the same family as
1228 * the policy itself. Now that we will have ipv4-in-ipv6
1229 * and ipv6-in-ipv4 tunnels, this is no longer true.
1230 */
1231 if (!ut[i].family)
1232 ut[i].family = family;
1233
1234 switch (ut[i].family) {
1235 case AF_INET:
1236 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001237#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001238 case AF_INET6:
1239 break;
1240#endif
1241 default:
1242 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001243 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001244 }
1245
1246 return 0;
1247}
1248
Thomas Graf5424f322007-08-22 14:01:33 -07001249static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Thomas Graf5424f322007-08-22 14:01:33 -07001251 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (!rt) {
1254 pol->xfrm_nr = 0;
1255 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001256 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1257 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001260 err = validate_tmpl(nr, utmpl, pol->family);
1261 if (err)
1262 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Thomas Graf5424f322007-08-22 14:01:33 -07001264 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 return 0;
1267}
1268
Thomas Graf5424f322007-08-22 14:01:33 -07001269static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001270{
Thomas Graf5424f322007-08-22 14:01:33 -07001271 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001272 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001273 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001274 int err;
1275
1276 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001277 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001278 type = upt->type;
1279 }
1280
1281 err = verify_policy_type(type);
1282 if (err)
1283 return err;
1284
1285 *tp = type;
1286 return 0;
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1290{
1291 xp->priority = p->priority;
1292 xp->index = p->index;
1293 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1294 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1295 xp->action = p->action;
1296 xp->flags = p->flags;
1297 xp->family = p->sel.family;
1298 /* XXX xp->share = p->share; */
1299}
1300
1301static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1302{
Mathias Krause97f96ea2012-09-19 11:33:40 +00001303 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1305 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1306 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1307 p->priority = xp->priority;
1308 p->index = xp->index;
1309 p->sel.family = xp->family;
1310 p->dir = dir;
1311 p->action = xp->action;
1312 p->flags = xp->flags;
1313 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1314}
1315
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001316static 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 -07001317{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001318 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int err;
1320
1321 if (!xp) {
1322 *errp = -ENOMEM;
1323 return NULL;
1324 }
1325
1326 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001327
Thomas Graf35a7aa02007-08-22 14:00:40 -07001328 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001329 if (err)
1330 goto error;
1331
Thomas Graf35a7aa02007-08-22 14:00:40 -07001332 if (!(err = copy_from_user_tmpl(xp, attrs)))
1333 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001334 if (err)
1335 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001337 xfrm_mark_get(attrs, &xp->mark);
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001340 error:
1341 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001342 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001343 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001344 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Christoph Hellwig22e70052007-01-02 15:22:30 -08001347static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001348 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001350 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001351 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001353 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int err;
1355 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001356 uid_t loginuid = audit_get_loginuid(current);
1357 u32 sessionid = audit_get_sessionid(current);
1358 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 err = verify_newpolicy_info(p);
1361 if (err)
1362 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001363 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001364 if (err)
1365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001367 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (!xp)
1369 return err;
1370
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001371 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001372 * Aha! this is anti-netlink really i.e more pfkey derived
1373 * in netlink excl is a flag and you wouldnt need
1374 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1376 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001377 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001378 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001381 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 kfree(xp);
1383 return err;
1384 }
1385
Herbert Xuf60f6b82005-06-18 22:44:37 -07001386 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001387 c.seq = nlh->nlmsg_seq;
1388 c.pid = nlh->nlmsg_pid;
1389 km_policy_notify(xp, p->dir, &c);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 xfrm_pol_put(xp);
1392
1393 return 0;
1394}
1395
1396static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1397{
1398 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1399 int i;
1400
1401 if (xp->xfrm_nr == 0)
1402 return 0;
1403
1404 for (i = 0; i < xp->xfrm_nr; i++) {
1405 struct xfrm_user_tmpl *up = &vec[i];
1406 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1407
1408 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001409 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1411 up->reqid = kp->reqid;
1412 up->mode = kp->mode;
1413 up->share = kp->share;
1414 up->optional = kp->optional;
1415 up->aalgos = kp->aalgos;
1416 up->ealgos = kp->ealgos;
1417 up->calgos = kp->calgos;
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Thomas Grafc0144be2007-08-22 13:55:43 -07001420 return nla_put(skb, XFRMA_TMPL,
1421 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001422}
1423
Serge Hallyn0d681622006-07-24 23:30:44 -07001424static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1425{
1426 if (x->security) {
1427 return copy_sec_ctx(x->security, skb);
1428 }
1429 return 0;
1430}
1431
1432static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1433{
1434 if (xp->security) {
1435 return copy_sec_ctx(xp->security, skb);
1436 }
1437 return 0;
1438}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001439static inline size_t userpolicy_type_attrsize(void)
1440{
1441#ifdef CONFIG_XFRM_SUB_POLICY
1442 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1443#else
1444 return 0;
1445#endif
1446}
Serge Hallyn0d681622006-07-24 23:30:44 -07001447
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001448#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001449static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001450{
Thomas Grafc0144be2007-08-22 13:55:43 -07001451 struct xfrm_userpolicy_type upt = {
1452 .type = type,
1453 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001454
Thomas Grafc0144be2007-08-22 13:55:43 -07001455 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001456}
1457
1458#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001459static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001460{
1461 return 0;
1462}
1463#endif
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1466{
1467 struct xfrm_dump_info *sp = ptr;
1468 struct xfrm_userpolicy_info *p;
1469 struct sk_buff *in_skb = sp->in_skb;
1470 struct sk_buff *skb = sp->out_skb;
1471 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001473 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1474 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1475 if (nlh == NULL)
1476 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Thomas Graf7b67c852007-08-22 13:53:52 -07001478 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 copy_to_user_policy(xp, p, dir);
1480 if (copy_to_user_tmpl(xp, skb) < 0)
1481 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001482 if (copy_to_user_sec_ctx(xp, skb))
1483 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001484 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001485 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001486 if (xfrm_mark_put(skb, &xp->mark))
1487 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Thomas Graf98250692007-08-22 12:47:26 -07001489 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return 0;
1491
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001492nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001494 nlmsg_cancel(skb, nlh);
1495 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Timo Teras4c563f72008-02-28 21:31:08 -08001498static int xfrm_dump_policy_done(struct netlink_callback *cb)
1499{
1500 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1501
1502 xfrm_policy_walk_done(walk);
1503 return 0;
1504}
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1507{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001508 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001509 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 struct xfrm_dump_info info;
1511
Timo Teras4c563f72008-02-28 21:31:08 -08001512 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1513 sizeof(cb->args) - sizeof(cb->args[0]));
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 info.in_skb = cb->skb;
1516 info.out_skb = skb;
1517 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1518 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001519
1520 if (!cb->args[0]) {
1521 cb->args[0] = 1;
1522 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1523 }
1524
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001525 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 return skb->len;
1528}
1529
1530static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1531 struct xfrm_policy *xp,
1532 int dir, u32 seq)
1533{
1534 struct xfrm_dump_info info;
1535 struct sk_buff *skb;
Mathias Krausef38b3342012-09-14 09:58:32 +00001536 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Thomas Graf7deb2262007-08-22 13:57:39 -07001538 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (!skb)
1540 return ERR_PTR(-ENOMEM);
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 info.in_skb = in_skb;
1543 info.out_skb = skb;
1544 info.nlmsg_seq = seq;
1545 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Mathias Krausef38b3342012-09-14 09:58:32 +00001547 err = dump_one_policy(xp, dir, 0, &info);
1548 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 kfree_skb(skb);
Mathias Krausef38b3342012-09-14 09:58:32 +00001550 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552
1553 return skb;
1554}
1555
Christoph Hellwig22e70052007-01-02 15:22:30 -08001556static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001557 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001559 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 struct xfrm_policy *xp;
1561 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001562 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001564 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001566 struct xfrm_mark m;
1567 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Thomas Graf7b67c852007-08-22 13:53:52 -07001569 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1571
Thomas Graf35a7aa02007-08-22 14:00:40 -07001572 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001573 if (err)
1574 return err;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 err = verify_policy_dir(p->dir);
1577 if (err)
1578 return err;
1579
1580 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001581 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001582 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001583 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001584 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001585
Thomas Graf35a7aa02007-08-22 14:00:40 -07001586 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001587 if (err)
1588 return err;
1589
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001590 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001591 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001592 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001593
Paul Moore03e1ad72008-04-12 19:07:52 -07001594 err = security_xfrm_policy_alloc(&ctx, uctx);
1595 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001596 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001597 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001598 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001599 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001600 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (xp == NULL)
1603 return -ENOENT;
1604
1605 if (!delete) {
1606 struct sk_buff *resp_skb;
1607
1608 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1609 if (IS_ERR(resp_skb)) {
1610 err = PTR_ERR(resp_skb);
1611 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001612 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001613 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001615 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001616 uid_t loginuid = audit_get_loginuid(current);
1617 u32 sessionid = audit_get_sessionid(current);
1618 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001619
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001620 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001621 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1622 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001623
1624 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001625 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001626
Herbert Xue7443892005-06-18 22:44:18 -07001627 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001628 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001629 c.seq = nlh->nlmsg_seq;
1630 c.pid = nlh->nlmsg_pid;
1631 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001634out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001635 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return err;
1637}
1638
Christoph Hellwig22e70052007-01-02 15:22:30 -08001639static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001640 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001642 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001643 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001644 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001645 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001646 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001648 audit_info.loginuid = audit_get_loginuid(current);
1649 audit_info.sessionid = audit_get_sessionid(current);
1650 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001651 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001652 if (err) {
1653 if (err == -ESRCH) /* empty table */
1654 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001655 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001656 }
Herbert Xubf088672005-06-18 22:44:00 -07001657 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001658 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001659 c.seq = nlh->nlmsg_seq;
1660 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001661 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001662 km_state_notify(NULL, &c);
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return 0;
1665}
1666
Steffen Klassertd8647b72011-03-08 00:10:27 +00001667static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001668{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001669 size_t replay_size = x->replay_esn ?
1670 xfrm_replay_state_esn_len(x->replay_esn) :
1671 sizeof(struct xfrm_replay_state);
1672
Thomas Graf7deb2262007-08-22 13:57:39 -07001673 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001674 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001675 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001676 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001677 + nla_total_size(4) /* XFRM_AE_RTHR */
1678 + nla_total_size(4); /* XFRM_AE_ETHR */
1679}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001680
David S. Miller214e0052011-02-24 00:02:38 -05001681static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001682{
1683 struct xfrm_aevent_id *id;
1684 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001685
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001686 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1687 if (nlh == NULL)
1688 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001689
Thomas Graf7b67c852007-08-22 13:53:52 -07001690 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001691 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001692 id->sa_id.spi = x->id.spi;
1693 id->sa_id.family = x->props.family;
1694 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001695 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1696 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001697 id->flags = c->data.aevent;
1698
Steffen Klassertd8647b72011-03-08 00:10:27 +00001699 if (x->replay_esn)
1700 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1701 xfrm_replay_state_esn_len(x->replay_esn),
1702 x->replay_esn);
1703 else
1704 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1705
Thomas Grafc0144be2007-08-22 13:55:43 -07001706 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001707
Thomas Grafc0144be2007-08-22 13:55:43 -07001708 if (id->flags & XFRM_AE_RTHR)
1709 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001710
Thomas Grafc0144be2007-08-22 13:55:43 -07001711 if (id->flags & XFRM_AE_ETHR)
1712 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1713 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001714
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001715 if (xfrm_mark_put(skb, &x->mark))
1716 goto nla_put_failure;
1717
Thomas Graf98250692007-08-22 12:47:26 -07001718 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001719
Thomas Grafc0144be2007-08-22 13:55:43 -07001720nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001721 nlmsg_cancel(skb, nlh);
1722 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001723}
1724
Christoph Hellwig22e70052007-01-02 15:22:30 -08001725static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001726 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001727{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001728 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001729 struct xfrm_state *x;
1730 struct sk_buff *r_skb;
1731 int err;
1732 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001733 u32 mark;
1734 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001735 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001736 struct xfrm_usersa_id *id = &p->sa_id;
1737
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001738 mark = xfrm_mark_get(attrs, &m);
1739
1740 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001741 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001742 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001743
1744 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1745 if (r_skb == NULL) {
1746 xfrm_state_put(x);
1747 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001748 }
1749
1750 /*
1751 * XXX: is this lock really needed - none of the other
1752 * gets lock (the concern is things getting updated
1753 * while we are still reading) - jhs
1754 */
1755 spin_lock_bh(&x->lock);
1756 c.data.aevent = p->flags;
1757 c.seq = nlh->nlmsg_seq;
1758 c.pid = nlh->nlmsg_pid;
1759
1760 if (build_aevent(r_skb, x, &c) < 0)
1761 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001762 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001763 spin_unlock_bh(&x->lock);
1764 xfrm_state_put(x);
1765 return err;
1766}
1767
Christoph Hellwig22e70052007-01-02 15:22:30 -08001768static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001769 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001770{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001771 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001772 struct xfrm_state *x;
1773 struct km_event c;
1774 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001775 u32 mark = 0;
1776 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001777 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001778 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001779 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001780 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001781
Steffen Klassertd8647b72011-03-08 00:10:27 +00001782 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001783 return err;
1784
1785 /* pedantic mode - thou shalt sayeth replaceth */
1786 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1787 return err;
1788
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001789 mark = xfrm_mark_get(attrs, &m);
1790
1791 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 -08001792 if (x == NULL)
1793 return -ESRCH;
1794
1795 if (x->km.state != XFRM_STATE_VALID)
1796 goto out;
1797
Steffen Klasserte2b19122011-03-28 19:47:30 +00001798 err = xfrm_replay_verify_len(x->replay_esn, rp);
1799 if (err)
1800 goto out;
1801
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001802 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001803 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001804 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001805
1806 c.event = nlh->nlmsg_type;
1807 c.seq = nlh->nlmsg_seq;
1808 c.pid = nlh->nlmsg_pid;
1809 c.data.aevent = XFRM_AE_CU;
1810 km_state_notify(x, &c);
1811 err = 0;
1812out:
1813 xfrm_state_put(x);
1814 return err;
1815}
1816
Christoph Hellwig22e70052007-01-02 15:22:30 -08001817static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001818 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001820 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001821 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001822 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001823 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001824 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001825
Thomas Graf35a7aa02007-08-22 14:00:40 -07001826 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001827 if (err)
1828 return err;
1829
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001830 audit_info.loginuid = audit_get_loginuid(current);
1831 audit_info.sessionid = audit_get_sessionid(current);
1832 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001833 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001834 if (err) {
1835 if (err == -ESRCH) /* empty table */
1836 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001837 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001838 }
1839
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001840 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001841 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001842 c.seq = nlh->nlmsg_seq;
1843 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001844 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001845 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 0;
1847}
1848
Christoph Hellwig22e70052007-01-02 15:22:30 -08001849static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001850 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001851{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001852 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001853 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001854 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001855 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001856 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001857 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001858 struct xfrm_mark m;
1859 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001860
Thomas Graf35a7aa02007-08-22 14:00:40 -07001861 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001862 if (err)
1863 return err;
1864
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001865 err = verify_policy_dir(p->dir);
1866 if (err)
1867 return err;
1868
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001869 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001870 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001871 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001872 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001873 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001874
Thomas Graf35a7aa02007-08-22 14:00:40 -07001875 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001876 if (err)
1877 return err;
1878
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001879 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001880 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001881 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001882
Paul Moore03e1ad72008-04-12 19:07:52 -07001883 err = security_xfrm_policy_alloc(&ctx, uctx);
1884 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001885 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001886 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001887 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001888 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001889 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001890 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001891 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001892 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001893
Timo Teräsea2dea92010-03-31 00:17:05 +00001894 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001895 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001896
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001897 err = 0;
1898 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001899 uid_t loginuid = audit_get_loginuid(current);
1900 u32 sessionid = audit_get_sessionid(current);
1901 u32 sid;
1902
1903 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001904 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001905 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001906
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001907 } else {
1908 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001909 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001910 }
1911 km_policy_expired(xp, p->dir, up->hard, current->pid);
1912
1913out:
1914 xfrm_pol_put(xp);
1915 return err;
1916}
1917
Christoph Hellwig22e70052007-01-02 15:22:30 -08001918static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001919 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001920{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001921 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001922 struct xfrm_state *x;
1923 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001924 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001925 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001926 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001927 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001928
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001929 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 -08001930
David S. Miller3a765aa2007-02-26 14:52:21 -08001931 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001932 if (x == NULL)
1933 return err;
1934
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001935 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001936 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001937 if (x->km.state != XFRM_STATE_VALID)
1938 goto out;
1939 km_state_expired(x, ue->hard, current->pid);
1940
Joy Latten161a09e2006-11-27 13:11:54 -06001941 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001942 uid_t loginuid = audit_get_loginuid(current);
1943 u32 sessionid = audit_get_sessionid(current);
1944 u32 sid;
1945
1946 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001947 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001948 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001949 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001950 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001951out:
1952 spin_unlock_bh(&x->lock);
1953 xfrm_state_put(x);
1954 return err;
1955}
1956
Christoph Hellwig22e70052007-01-02 15:22:30 -08001957static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001958 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001959{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001960 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001961 struct xfrm_policy *xp;
1962 struct xfrm_user_tmpl *ut;
1963 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001964 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001965 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001966
Thomas Graf7b67c852007-08-22 13:53:52 -07001967 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001968 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001969 int err = -ENOMEM;
1970
1971 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001972 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001973
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001974 xfrm_mark_get(attrs, &mark);
1975
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001976 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001977 if (err)
1978 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001979
1980 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001981 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001982 if (!xp)
1983 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001984
1985 memcpy(&x->id, &ua->id, sizeof(ua->id));
1986 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1987 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001988 xp->mark.m = x->mark.m = mark.m;
1989 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001990 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001991 /* extract the templates and for each call km_key */
1992 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1993 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1994 memcpy(&x->id, &t->id, sizeof(x->id));
1995 x->props.mode = t->mode;
1996 x->props.reqid = t->reqid;
1997 x->props.family = ut->family;
1998 t->aalgos = ua->aalgos;
1999 t->ealgos = ua->ealgos;
2000 t->calgos = ua->calgos;
2001 err = km_query(x, t, xp);
2002
2003 }
2004
2005 kfree(x);
2006 kfree(xp);
2007
2008 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002009
2010bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002011 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002012free_state:
2013 kfree(x);
2014nomem:
2015 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002016}
2017
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002018#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002019static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002020 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002021 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002022{
Thomas Graf5424f322007-08-22 14:01:33 -07002023 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002024 struct xfrm_user_migrate *um;
2025 int i, num_migrate;
2026
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002027 if (k != NULL) {
2028 struct xfrm_user_kmaddress *uk;
2029
2030 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2031 memcpy(&k->local, &uk->local, sizeof(k->local));
2032 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2033 k->family = uk->family;
2034 k->reserved = uk->reserved;
2035 }
2036
Thomas Graf5424f322007-08-22 14:01:33 -07002037 um = nla_data(rt);
2038 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002039
2040 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2041 return -EINVAL;
2042
2043 for (i = 0; i < num_migrate; i++, um++, ma++) {
2044 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2045 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2046 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2047 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2048
2049 ma->proto = um->proto;
2050 ma->mode = um->mode;
2051 ma->reqid = um->reqid;
2052
2053 ma->old_family = um->old_family;
2054 ma->new_family = um->new_family;
2055 }
2056
2057 *num = i;
2058 return 0;
2059}
2060
2061static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002062 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002063{
Thomas Graf7b67c852007-08-22 13:53:52 -07002064 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002065 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002066 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002067 u8 type;
2068 int err;
2069 int n = 0;
2070
Thomas Graf35a7aa02007-08-22 14:00:40 -07002071 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002072 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002073
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002074 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2075
Thomas Graf5424f322007-08-22 14:01:33 -07002076 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002077 if (err)
2078 return err;
2079
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002080 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002081 if (err)
2082 return err;
2083
2084 if (!n)
2085 return 0;
2086
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002087 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002088
2089 return 0;
2090}
2091#else
2092static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002093 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002094{
2095 return -ENOPROTOOPT;
2096}
2097#endif
2098
2099#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002100static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002101{
2102 struct xfrm_user_migrate um;
2103
2104 memset(&um, 0, sizeof(um));
2105 um.proto = m->proto;
2106 um.mode = m->mode;
2107 um.reqid = m->reqid;
2108 um.old_family = m->old_family;
2109 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2110 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2111 um.new_family = m->new_family;
2112 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2113 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2114
Thomas Grafc0144be2007-08-22 13:55:43 -07002115 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002116}
2117
David S. Miller183cad12011-02-24 00:28:01 -05002118static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002119{
2120 struct xfrm_user_kmaddress uk;
2121
2122 memset(&uk, 0, sizeof(uk));
2123 uk.family = k->family;
2124 uk.reserved = k->reserved;
2125 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002126 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002127
2128 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2129}
2130
2131static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002132{
2133 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002134 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2135 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2136 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002137}
2138
David S. Miller183cad12011-02-24 00:28:01 -05002139static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2140 int num_migrate, const struct xfrm_kmaddress *k,
2141 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002142{
David S. Miller183cad12011-02-24 00:28:01 -05002143 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002144 struct xfrm_userpolicy_id *pol_id;
2145 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002146 int i;
2147
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002148 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2149 if (nlh == NULL)
2150 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002151
Thomas Graf7b67c852007-08-22 13:53:52 -07002152 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002153 /* copy data from selector, dir, and type to the pol_id */
2154 memset(pol_id, 0, sizeof(*pol_id));
2155 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2156 pol_id->dir = dir;
2157
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002158 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2159 goto nlmsg_failure;
2160
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002161 if (copy_to_user_policy_type(type, skb) < 0)
2162 goto nlmsg_failure;
2163
2164 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2165 if (copy_to_user_migrate(mp, skb) < 0)
2166 goto nlmsg_failure;
2167 }
2168
Thomas Graf98250692007-08-22 12:47:26 -07002169 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002170nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002171 nlmsg_cancel(skb, nlh);
2172 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002173}
2174
David S. Miller183cad12011-02-24 00:28:01 -05002175static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2176 const struct xfrm_migrate *m, int num_migrate,
2177 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002178{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002179 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002180 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002181
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002182 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002183 if (skb == NULL)
2184 return -ENOMEM;
2185
2186 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002187 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002188 BUG();
2189
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002190 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002191}
2192#else
David S. Miller183cad12011-02-24 00:28:01 -05002193static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2194 const struct xfrm_migrate *m, int num_migrate,
2195 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002196{
2197 return -ENOPROTOOPT;
2198}
2199#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002200
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002201#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002202
2203static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002204 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002205 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2206 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2207 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2208 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2209 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2210 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002211 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002212 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002213 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002214 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002215 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002216 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002217 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002218 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2219 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002220 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002221 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002222 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2223 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224};
2225
Thomas Graf492b5582005-05-03 14:26:40 -07002226#undef XMSGSIZE
2227
Thomas Grafcf5cb792007-08-22 13:59:04 -07002228static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002229 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2230 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2231 [XFRMA_LASTUSED] = { .type = NLA_U64},
2232 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002233 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002234 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2235 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2236 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2237 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2238 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2239 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2240 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2241 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2242 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2243 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2244 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2245 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2246 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2247 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002248 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002249 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002250 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002251 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002252};
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002255 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002257 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002258} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2259 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2260 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2261 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002262 .dump = xfrm_dump_sa,
2263 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002264 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2265 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2266 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002267 .dump = xfrm_dump_policy,
2268 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002269 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002270 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002271 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002272 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2273 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002274 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002275 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2276 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002277 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2278 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002279 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002280 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002281 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282};
2283
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002284static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002286 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002287 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002289 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 type -= XFRM_MSG_BASE;
2296 link = &xfrm_dispatch[type];
2297
2298 /* All operations require privileges, even GET */
Eric Parisfd778462012-01-03 12:25:16 -05002299 if (!capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002300 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Thomas Graf492b5582005-05-03 14:26:40 -07002302 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2303 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002304 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002308 {
2309 struct netlink_dump_control c = {
2310 .dump = link->dump,
2311 .done = link->done,
2312 };
2313 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316
Thomas Graf35a7aa02007-08-22 14:00:40 -07002317 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002318 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002319 if (err < 0)
2320 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Thomas Graf5424f322007-08-22 14:01:33 -07002325 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002328static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002330 mutex_lock(&xfrm_cfg_mutex);
2331 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2332 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Thomas Graf7deb2262007-08-22 13:57:39 -07002335static inline size_t xfrm_expire_msgsize(void)
2336{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002337 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2338 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002339}
2340
David S. Miller214e0052011-02-24 00:02:38 -05002341static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 struct xfrm_user_expire *ue;
2344 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002346 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2347 if (nlh == NULL)
2348 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Thomas Graf7b67c852007-08-22 13:53:52 -07002350 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002352 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002354 if (xfrm_mark_put(skb, &x->mark))
2355 goto nla_put_failure;
2356
Thomas Graf98250692007-08-22 12:47:26 -07002357 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002358
2359nla_put_failure:
2360 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
David S. Miller214e0052011-02-24 00:02:38 -05002363static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002365 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct sk_buff *skb;
2367
Thomas Graf7deb2262007-08-22 13:57:39 -07002368 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (skb == NULL)
2370 return -ENOMEM;
2371
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002372 if (build_expire(skb, x, c) < 0) {
2373 kfree_skb(skb);
2374 return -EMSGSIZE;
2375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002377 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
David S. Miller214e0052011-02-24 00:02:38 -05002380static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002381{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002382 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002383 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002384
Steffen Klassertd8647b72011-03-08 00:10:27 +00002385 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002386 if (skb == NULL)
2387 return -ENOMEM;
2388
2389 if (build_aevent(skb, x, c) < 0)
2390 BUG();
2391
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002392 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002393}
2394
David S. Miller214e0052011-02-24 00:02:38 -05002395static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002396{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002397 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002398 struct xfrm_usersa_flush *p;
2399 struct nlmsghdr *nlh;
2400 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002401 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002402
Thomas Graf7deb2262007-08-22 13:57:39 -07002403 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002404 if (skb == NULL)
2405 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002406
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002407 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2408 if (nlh == NULL) {
2409 kfree_skb(skb);
2410 return -EMSGSIZE;
2411 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002412
Thomas Graf7b67c852007-08-22 13:53:52 -07002413 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002414 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415
Thomas Graf98250692007-08-22 12:47:26 -07002416 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002418 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002419}
2420
Thomas Graf7deb2262007-08-22 13:57:39 -07002421static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002422{
Thomas Graf7deb2262007-08-22 13:57:39 -07002423 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002424 if (x->aead)
2425 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002426 if (x->aalg) {
2427 l += nla_total_size(sizeof(struct xfrm_algo) +
2428 (x->aalg->alg_key_len + 7) / 8);
2429 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2430 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002431 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002432 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002433 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002434 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002435 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002436 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002437 if (x->tfcpad)
2438 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002439 if (x->replay_esn)
2440 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002441 if (x->security)
2442 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2443 x->security->ctx_len);
2444 if (x->coaddr)
2445 l += nla_total_size(sizeof(*x->coaddr));
2446
Herbert Xud26f3982007-11-13 21:47:08 -08002447 /* Must count x->lastused as it may become non-zero behind our back. */
2448 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002449
2450 return l;
2451}
2452
David S. Miller214e0052011-02-24 00:02:38 -05002453static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002454{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002455 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002457 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002458 struct nlmsghdr *nlh;
2459 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002461 int headlen;
2462
2463 headlen = sizeof(*p);
2464 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002465 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002466 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002467 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002468 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002469 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002470
Thomas Graf7deb2262007-08-22 13:57:39 -07002471 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002472 if (skb == NULL)
2473 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002474
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002475 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2476 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002477 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002478
Thomas Graf7b67c852007-08-22 13:53:52 -07002479 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002480 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002481 struct nlattr *attr;
2482
Thomas Graf7b67c852007-08-22 13:53:52 -07002483 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002484 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2485 id->spi = x->id.spi;
2486 id->family = x->props.family;
2487 id->proto = x->id.proto;
2488
Thomas Grafc0144be2007-08-22 13:55:43 -07002489 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2490 if (attr == NULL)
2491 goto nla_put_failure;
2492
2493 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002494 }
2495
Herbert Xu68325d32007-10-09 13:30:57 -07002496 if (copy_to_user_state_extra(x, p, skb))
2497 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002498
Thomas Graf98250692007-08-22 12:47:26 -07002499 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002500
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002501 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002502
Thomas Grafc0144be2007-08-22 13:55:43 -07002503nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002504 /* Somebody screwed up with xfrm_sa_len! */
2505 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002506 kfree_skb(skb);
2507 return -1;
2508}
2509
David S. Miller214e0052011-02-24 00:02:38 -05002510static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002511{
2512
2513 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002514 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002515 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002516 case XFRM_MSG_NEWAE:
2517 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002518 case XFRM_MSG_DELSA:
2519 case XFRM_MSG_UPDSA:
2520 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002521 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002522 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002523 return xfrm_notify_sa_flush(c);
2524 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002525 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2526 c->event);
2527 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002528 }
2529
2530 return 0;
2531
2532}
2533
Thomas Graf7deb2262007-08-22 13:57:39 -07002534static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2535 struct xfrm_policy *xp)
2536{
2537 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2538 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002539 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002540 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2541 + userpolicy_type_attrsize();
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2545 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2546 int dir)
2547{
2548 struct xfrm_user_acquire *ua;
2549 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 __u32 seq = xfrm_get_acqseq();
2551
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002552 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2553 if (nlh == NULL)
2554 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Thomas Graf7b67c852007-08-22 13:53:52 -07002556 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 memcpy(&ua->id, &x->id, sizeof(ua->id));
2558 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2559 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2560 copy_to_user_policy(xp, &ua->policy, dir);
2561 ua->aalgos = xt->aalgos;
2562 ua->ealgos = xt->ealgos;
2563 ua->calgos = xt->calgos;
2564 ua->seq = x->km.seq = seq;
2565
2566 if (copy_to_user_tmpl(xp, skb) < 0)
2567 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002568 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002569 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002570 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002571 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002572 if (xfrm_mark_put(skb, &xp->mark))
2573 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Thomas Graf98250692007-08-22 12:47:26 -07002575 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002577nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002579 nlmsg_cancel(skb, nlh);
2580 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
2583static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2584 struct xfrm_policy *xp, int dir)
2585{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002586 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Thomas Graf7deb2262007-08-22 13:57:39 -07002589 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (skb == NULL)
2591 return -ENOMEM;
2592
2593 if (build_acquire(skb, x, xt, xp, dir) < 0)
2594 BUG();
2595
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002596 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
2599/* User gives us xfrm_user_policy_info followed by an array of 0
2600 * or more templates.
2601 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002602static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 u8 *data, int len, int *dir)
2604{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002605 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2607 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2608 struct xfrm_policy *xp;
2609 int nr;
2610
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002611 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 case AF_INET:
2613 if (opt != IP_XFRM_POLICY) {
2614 *dir = -EOPNOTSUPP;
2615 return NULL;
2616 }
2617 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002618#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 case AF_INET6:
2620 if (opt != IPV6_XFRM_POLICY) {
2621 *dir = -EOPNOTSUPP;
2622 return NULL;
2623 }
2624 break;
2625#endif
2626 default:
2627 *dir = -EINVAL;
2628 return NULL;
2629 }
2630
2631 *dir = -EINVAL;
2632
2633 if (len < sizeof(*p) ||
2634 verify_newpolicy_info(p))
2635 return NULL;
2636
2637 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002638 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return NULL;
2640
Herbert Xua4f1bac2005-07-26 15:43:17 -07002641 if (p->dir > XFRM_POLICY_OUT)
2642 return NULL;
2643
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002644 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (xp == NULL) {
2646 *dir = -ENOBUFS;
2647 return NULL;
2648 }
2649
2650 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002651 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 copy_templates(xp, ut, nr);
2653
2654 *dir = p->dir;
2655
2656 return xp;
2657}
2658
Thomas Graf7deb2262007-08-22 13:57:39 -07002659static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2660{
2661 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2662 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2663 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002664 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002665 + userpolicy_type_attrsize();
2666}
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002669 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 struct xfrm_user_polexpire *upe;
2672 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002673 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002675 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2676 if (nlh == NULL)
2677 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Thomas Graf7b67c852007-08-22 13:53:52 -07002679 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 copy_to_user_policy(xp, &upe->pol, dir);
2681 if (copy_to_user_tmpl(xp, skb) < 0)
2682 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002683 if (copy_to_user_sec_ctx(xp, skb))
2684 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002685 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002686 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002687 if (xfrm_mark_put(skb, &xp->mark))
2688 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 upe->hard = !!hard;
2690
Thomas Graf98250692007-08-22 12:47:26 -07002691 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002693nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002695 nlmsg_cancel(skb, nlh);
2696 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
David S. Miller214e0052011-02-24 00:02:38 -05002699static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002701 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Thomas Graf7deb2262007-08-22 13:57:39 -07002704 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (skb == NULL)
2706 return -ENOMEM;
2707
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002708 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 BUG();
2710
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002711 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
David S. Miller214e0052011-02-24 00:02:38 -05002714static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002715{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002716 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002717 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002718 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002719 struct nlmsghdr *nlh;
2720 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002721 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002722 int headlen;
2723
2724 headlen = sizeof(*p);
2725 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002726 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002727 headlen = sizeof(*id);
2728 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002729 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002730 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002731 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002732
Thomas Graf7deb2262007-08-22 13:57:39 -07002733 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002734 if (skb == NULL)
2735 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002736
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002737 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2738 if (nlh == NULL)
2739 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002740
Thomas Graf7b67c852007-08-22 13:53:52 -07002741 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002742 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002743 struct nlattr *attr;
2744
Thomas Graf7b67c852007-08-22 13:53:52 -07002745 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002746 memset(id, 0, sizeof(*id));
2747 id->dir = dir;
2748 if (c->data.byid)
2749 id->index = xp->index;
2750 else
2751 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2752
Thomas Grafc0144be2007-08-22 13:55:43 -07002753 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2754 if (attr == NULL)
2755 goto nlmsg_failure;
2756
2757 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002758 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002759
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002760 copy_to_user_policy(xp, p, dir);
2761 if (copy_to_user_tmpl(xp, skb) < 0)
2762 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002763 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002764 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002765
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002766 if (xfrm_mark_put(skb, &xp->mark))
2767 goto nla_put_failure;
2768
Thomas Graf98250692007-08-22 12:47:26 -07002769 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002770
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002771 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002772
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002773nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002774nlmsg_failure:
2775 kfree_skb(skb);
2776 return -1;
2777}
2778
David S. Miller214e0052011-02-24 00:02:38 -05002779static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002780{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002781 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002782 struct nlmsghdr *nlh;
2783 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002784
Thomas Graf7deb2262007-08-22 13:57:39 -07002785 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002786 if (skb == NULL)
2787 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002788
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002789 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2790 if (nlh == NULL)
2791 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002792 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2793 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002794
Thomas Graf98250692007-08-22 12:47:26 -07002795 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002796
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002797 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002798
2799nlmsg_failure:
2800 kfree_skb(skb);
2801 return -1;
2802}
2803
David S. Miller214e0052011-02-24 00:02:38 -05002804static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002805{
2806
2807 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002808 case XFRM_MSG_NEWPOLICY:
2809 case XFRM_MSG_UPDPOLICY:
2810 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002811 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002812 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002813 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002814 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002815 return xfrm_exp_policy_notify(xp, dir, c);
2816 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002817 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2818 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002819 }
2820
2821 return 0;
2822
2823}
2824
Thomas Graf7deb2262007-08-22 13:57:39 -07002825static inline size_t xfrm_report_msgsize(void)
2826{
2827 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2828}
2829
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002830static int build_report(struct sk_buff *skb, u8 proto,
2831 struct xfrm_selector *sel, xfrm_address_t *addr)
2832{
2833 struct xfrm_user_report *ur;
2834 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002835
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002836 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2837 if (nlh == NULL)
2838 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002839
Thomas Graf7b67c852007-08-22 13:53:52 -07002840 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002841 ur->proto = proto;
2842 memcpy(&ur->sel, sel, sizeof(ur->sel));
2843
2844 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002845 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002846
Thomas Graf98250692007-08-22 12:47:26 -07002847 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002848
Thomas Grafc0144be2007-08-22 13:55:43 -07002849nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002850 nlmsg_cancel(skb, nlh);
2851 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002852}
2853
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002854static int xfrm_send_report(struct net *net, u8 proto,
2855 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002856{
2857 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002858
Thomas Graf7deb2262007-08-22 13:57:39 -07002859 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002860 if (skb == NULL)
2861 return -ENOMEM;
2862
2863 if (build_report(skb, proto, sel, addr) < 0)
2864 BUG();
2865
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002866 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002867}
2868
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002869static inline size_t xfrm_mapping_msgsize(void)
2870{
2871 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2872}
2873
2874static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2875 xfrm_address_t *new_saddr, __be16 new_sport)
2876{
2877 struct xfrm_user_mapping *um;
2878 struct nlmsghdr *nlh;
2879
2880 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2881 if (nlh == NULL)
2882 return -EMSGSIZE;
2883
2884 um = nlmsg_data(nlh);
2885
2886 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2887 um->id.spi = x->id.spi;
2888 um->id.family = x->props.family;
2889 um->id.proto = x->id.proto;
2890 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2891 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2892 um->new_sport = new_sport;
2893 um->old_sport = x->encap->encap_sport;
2894 um->reqid = x->props.reqid;
2895
2896 return nlmsg_end(skb, nlh);
2897}
2898
2899static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2900 __be16 sport)
2901{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002902 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002903 struct sk_buff *skb;
2904
2905 if (x->id.proto != IPPROTO_ESP)
2906 return -EINVAL;
2907
2908 if (!x->encap)
2909 return -EINVAL;
2910
2911 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2912 if (skb == NULL)
2913 return -ENOMEM;
2914
2915 if (build_mapping(skb, x, ipaddr, sport) < 0)
2916 BUG();
2917
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002918 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002919}
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921static struct xfrm_mgr netlink_mgr = {
2922 .id = "netlink",
2923 .notify = xfrm_send_state_notify,
2924 .acquire = xfrm_send_acquire,
2925 .compile_policy = xfrm_compile_policy,
2926 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002927 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002928 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002929 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930};
2931
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002932static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Patrick McHardybe336902006-03-20 22:40:54 -08002934 struct sock *nlsk;
2935
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002936 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002937 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002938 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002940 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00002941 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return 0;
2943}
2944
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002945static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002946{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002947 struct net *net;
2948 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002949 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002950 synchronize_net();
2951 list_for_each_entry(net, net_exit_list, exit_list)
2952 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002953}
2954
2955static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002956 .init = xfrm_user_net_init,
2957 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002958};
2959
2960static int __init xfrm_user_init(void)
2961{
2962 int rv;
2963
2964 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2965
2966 rv = register_pernet_subsys(&xfrm_user_net_ops);
2967 if (rv < 0)
2968 return rv;
2969 rv = xfrm_register_km(&netlink_mgr);
2970 if (rv < 0)
2971 unregister_pernet_subsys(&xfrm_user_net_ops);
2972 return rv;
2973}
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975static void __exit xfrm_user_exit(void)
2976{
2977 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002978 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
2981module_init(xfrm_user_init);
2982module_exit(xfrm_user_exit);
2983MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002984MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002985