blob: e0972b26a5980bedc9ddd79c4e332ba9b38cb6fc [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*/
Mathias Krause743b9112012-09-19 11:33:43 +0000445static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
446 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800447{
Thomas Graf5424f322007-08-22 14:01:33 -0700448 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krause743b9112012-09-19 11:33:43 +0000449 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700450 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
451 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
452 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800453
Steffen Klassertd8647b72011-03-08 00:10:27 +0000454 if (re) {
455 struct xfrm_replay_state_esn *replay_esn;
456 replay_esn = nla_data(re);
457 memcpy(x->replay_esn, replay_esn,
458 xfrm_replay_state_esn_len(replay_esn));
459 memcpy(x->preplay_esn, replay_esn,
460 xfrm_replay_state_esn_len(replay_esn));
461 }
462
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800463 if (rp) {
464 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700465 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800466 memcpy(&x->replay, replay, sizeof(*replay));
467 memcpy(&x->preplay, replay, sizeof(*replay));
468 }
469
470 if (lt) {
471 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700472 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800473 x->curlft.bytes = ltime->bytes;
474 x->curlft.packets = ltime->packets;
475 x->curlft.add_time = ltime->add_time;
476 x->curlft.use_time = ltime->use_time;
477 }
478
Thomas Grafcf5cb792007-08-22 13:59:04 -0700479 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700480 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800481
Thomas Grafcf5cb792007-08-22 13:59:04 -0700482 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700483 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800484}
485
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800486static struct xfrm_state *xfrm_state_construct(struct net *net,
487 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700488 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int *errp)
490{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800491 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int err = -ENOMEM;
493
494 if (!x)
495 goto error_no_put;
496
497 copy_from_user_state(x, p);
498
Herbert Xu1a6509d2008-01-28 19:37:29 -0800499 if ((err = attach_aead(&x->aead, &x->props.ealgo,
500 attrs[XFRMA_ALG_AEAD])))
501 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000502 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
503 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000505 if (!x->props.aalgo) {
506 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
507 attrs[XFRMA_ALG_AUTH])))
508 goto error;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
511 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700512 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto error;
514 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
515 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700516 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700518
519 if (attrs[XFRMA_ENCAP]) {
520 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
521 sizeof(*x->encap), GFP_KERNEL);
522 if (x->encap == NULL)
523 goto error;
524 }
525
Martin Willi35d28562010-12-08 04:37:49 +0000526 if (attrs[XFRMA_TFCPAD])
527 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
528
Thomas Graffd211502007-09-06 03:28:08 -0700529 if (attrs[XFRMA_COADDR]) {
530 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
531 sizeof(*x->coaddr), GFP_KERNEL);
532 if (x->coaddr == NULL)
533 goto error;
534 }
535
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000536 xfrm_mark_get(attrs, &x->mark);
537
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700538 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (err)
540 goto error;
541
Thomas Graffd211502007-09-06 03:28:08 -0700542 if (attrs[XFRMA_SEC_CTX] &&
543 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800544 goto error;
545
Steffen Klassertd8647b72011-03-08 00:10:27 +0000546 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
547 attrs[XFRMA_REPLAY_ESN_VAL])))
548 goto error;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800551 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800552 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800553 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800554
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000555 if ((err = xfrm_init_replay(x)))
556 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800557
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000558 /* override default values from above */
Mathias Krause743b9112012-09-19 11:33:43 +0000559 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 return x;
562
563error:
564 x->km.state = XFRM_STATE_DEAD;
565 xfrm_state_put(x);
566error_no_put:
567 *errp = err;
568 return NULL;
569}
570
Christoph Hellwig22e70052007-01-02 15:22:30 -0800571static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700572 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800574 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700575 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct xfrm_state *x;
577 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700578 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800579 uid_t loginuid = audit_get_loginuid(current);
580 u32 sessionid = audit_get_sessionid(current);
581 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Thomas Graf35a7aa02007-08-22 14:00:40 -0700583 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (err)
585 return err;
586
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800587 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (!x)
589 return err;
590
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700591 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
593 err = xfrm_state_add(x);
594 else
595 err = xfrm_state_update(x);
596
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800597 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400598 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (err < 0) {
601 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800602 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700606 c.seq = nlh->nlmsg_seq;
607 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700608 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700609
610 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700611out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700612 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return err;
614}
615
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800616static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
617 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700618 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700619 int *errp)
620{
621 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000622 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700623 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000624 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700625
626 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
627 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000628 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700629 } else {
630 xfrm_address_t *saddr = NULL;
631
Thomas Graf35a7aa02007-08-22 14:00:40 -0700632 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700633 if (!saddr) {
634 err = -EINVAL;
635 goto out;
636 }
637
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800638 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000639 x = xfrm_state_lookup_byaddr(net, mark,
640 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800641 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700642 }
643
644 out:
645 if (!x && errp)
646 *errp = err;
647 return x;
648}
649
Christoph Hellwig22e70052007-01-02 15:22:30 -0800650static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700651 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800653 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700655 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700656 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700657 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800658 uid_t loginuid = audit_get_loginuid(current);
659 u32 sessionid = audit_get_sessionid(current);
660 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800662 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
David S. Miller6f68dc32006-06-08 23:58:52 -0700666 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700667 goto out;
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700670 err = -EPERM;
671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700674 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600675
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700676 if (err < 0)
677 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700678
679 c.seq = nlh->nlmsg_seq;
680 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700681 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700682 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700684out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800685 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400686 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700687 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
692{
Mathias Kraused5f1f7c2012-09-19 11:33:39 +0000693 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 memcpy(&p->id, &x->id, sizeof(p->id));
695 memcpy(&p->sel, &x->sel, sizeof(p->sel));
696 memcpy(&p->lft, &x->lft, sizeof(p->lft));
697 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
698 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700699 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 p->mode = x->props.mode;
701 p->replay_window = x->props.replay_window;
702 p->reqid = x->props.reqid;
703 p->family = x->props.family;
704 p->flags = x->props.flags;
705 p->seq = x->km.seq;
706}
707
708struct xfrm_dump_info {
709 struct sk_buff *in_skb;
710 struct sk_buff *out_skb;
711 u32 nlmsg_seq;
712 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713};
714
Thomas Grafc0144be2007-08-22 13:55:43 -0700715static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
716{
Thomas Grafc0144be2007-08-22 13:55:43 -0700717 struct xfrm_user_sec_ctx *uctx;
718 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700719 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700720
721 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
722 if (attr == NULL)
723 return -EMSGSIZE;
724
725 uctx = nla_data(attr);
726 uctx->exttype = XFRMA_SEC_CTX;
727 uctx->len = ctx_size;
728 uctx->ctx_doi = s->ctx_doi;
729 uctx->ctx_alg = s->ctx_alg;
730 uctx->ctx_len = s->ctx_len;
731 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
732
733 return 0;
734}
735
Martin Willi4447bb32009-11-25 00:29:52 +0000736static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
737{
738 struct xfrm_algo *algo;
739 struct nlattr *nla;
740
741 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
742 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
743 if (!nla)
744 return -EMSGSIZE;
745
746 algo = nla_data(nla);
Mathias Krause37d61a22012-09-19 11:33:38 +0000747 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000748 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
749 algo->alg_key_len = auth->alg_key_len;
750
751 return 0;
752}
753
Herbert Xu68325d32007-10-09 13:30:57 -0700754/* Don't change this without updating xfrm_sa_len! */
755static int copy_to_user_state_extra(struct xfrm_state *x,
756 struct xfrm_usersa_info *p,
757 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 copy_to_user_state(x, p);
760
Herbert Xu050f0092007-10-09 13:31:47 -0700761 if (x->coaddr)
762 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
763
764 if (x->lastused)
765 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700766
Herbert Xu1a6509d2008-01-28 19:37:29 -0800767 if (x->aead)
768 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000769 if (x->aalg) {
770 if (copy_to_user_auth(x->aalg, skb))
771 goto nla_put_failure;
772
773 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
774 xfrm_alg_auth_len(x->aalg), x->aalg);
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800777 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700779 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700782 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Martin Willi35d28562010-12-08 04:37:49 +0000784 if (x->tfcpad)
785 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
786
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000787 if (xfrm_mark_put(skb, &x->mark))
788 goto nla_put_failure;
789
Steffen Klassertd8647b72011-03-08 00:10:27 +0000790 if (x->replay_esn)
791 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
792 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
793
Thomas Grafc0144be2007-08-22 13:55:43 -0700794 if (x->security && copy_sec_ctx(x->security, skb) < 0)
795 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700796
Herbert Xu68325d32007-10-09 13:30:57 -0700797 return 0;
798
799nla_put_failure:
800 return -EMSGSIZE;
801}
802
803static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
804{
805 struct xfrm_dump_info *sp = ptr;
806 struct sk_buff *in_skb = sp->in_skb;
807 struct sk_buff *skb = sp->out_skb;
808 struct xfrm_usersa_info *p;
809 struct nlmsghdr *nlh;
810 int err;
811
Herbert Xu68325d32007-10-09 13:30:57 -0700812 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
813 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
814 if (nlh == NULL)
815 return -EMSGSIZE;
816
817 p = nlmsg_data(nlh);
818
819 err = copy_to_user_state_extra(x, p, skb);
820 if (err)
821 goto nla_put_failure;
822
Thomas Graf98250692007-08-22 12:47:26 -0700823 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825
Thomas Grafc0144be2007-08-22 13:55:43 -0700826nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700827 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700828 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Timo Teras4c563f72008-02-28 21:31:08 -0800831static int xfrm_dump_sa_done(struct netlink_callback *cb)
832{
833 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
834 xfrm_state_walk_done(walk);
835 return 0;
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
839{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800840 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800841 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct xfrm_dump_info info;
843
Timo Teras4c563f72008-02-28 21:31:08 -0800844 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
845 sizeof(cb->args) - sizeof(cb->args[0]));
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 info.in_skb = cb->skb;
848 info.out_skb = skb;
849 info.nlmsg_seq = cb->nlh->nlmsg_seq;
850 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800851
852 if (!cb->args[0]) {
853 cb->args[0] = 1;
854 xfrm_state_walk_init(walk, 0);
855 }
856
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800857 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 return skb->len;
860}
861
862static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
863 struct xfrm_state *x, u32 seq)
864{
865 struct xfrm_dump_info info;
866 struct sk_buff *skb;
Mathias Krause555144b2012-09-13 11:41:26 +0000867 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Thomas Graf7deb2262007-08-22 13:57:39 -0700869 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!skb)
871 return ERR_PTR(-ENOMEM);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 info.in_skb = in_skb;
874 info.out_skb = skb;
875 info.nlmsg_seq = seq;
876 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Mathias Krause555144b2012-09-13 11:41:26 +0000878 err = dump_one_state(x, 0, &info);
879 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 kfree_skb(skb);
Mathias Krause555144b2012-09-13 11:41:26 +0000881 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
884 return skb;
885}
886
Thomas Graf7deb2262007-08-22 13:57:39 -0700887static inline size_t xfrm_spdinfo_msgsize(void)
888{
889 return NLMSG_ALIGN(4)
890 + nla_total_size(sizeof(struct xfrmu_spdinfo))
891 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
892}
893
Alexey Dobriyane0710412010-01-23 13:37:10 +0000894static int build_spdinfo(struct sk_buff *skb, struct net *net,
895 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700896{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700897 struct xfrmk_spdinfo si;
898 struct xfrmu_spdinfo spc;
899 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700900 struct nlmsghdr *nlh;
901 u32 *f;
902
903 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300904 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700905 return -EMSGSIZE;
906
907 f = nlmsg_data(nlh);
908 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000909 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700910 spc.incnt = si.incnt;
911 spc.outcnt = si.outcnt;
912 spc.fwdcnt = si.fwdcnt;
913 spc.inscnt = si.inscnt;
914 spc.outscnt = si.outscnt;
915 spc.fwdscnt = si.fwdscnt;
916 sph.spdhcnt = si.spdhcnt;
917 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700918
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700919 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
920 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700921
922 return nlmsg_end(skb, nlh);
923
924nla_put_failure:
925 nlmsg_cancel(skb, nlh);
926 return -EMSGSIZE;
927}
928
929static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700930 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700931{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800932 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700933 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700934 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700935 u32 spid = NETLINK_CB(skb).pid;
936 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700937
Thomas Graf7deb2262007-08-22 13:57:39 -0700938 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700939 if (r_skb == NULL)
940 return -ENOMEM;
941
Alexey Dobriyane0710412010-01-23 13:37:10 +0000942 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700943 BUG();
944
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800945 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700946}
947
Thomas Graf7deb2262007-08-22 13:57:39 -0700948static inline size_t xfrm_sadinfo_msgsize(void)
949{
950 return NLMSG_ALIGN(4)
951 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
952 + nla_total_size(4); /* XFRMA_SAD_CNT */
953}
954
Alexey Dobriyane0710412010-01-23 13:37:10 +0000955static int build_sadinfo(struct sk_buff *skb, struct net *net,
956 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700957{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700958 struct xfrmk_sadinfo si;
959 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700960 struct nlmsghdr *nlh;
961 u32 *f;
962
963 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300964 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700965 return -EMSGSIZE;
966
967 f = nlmsg_data(nlh);
968 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000969 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700970
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700971 sh.sadhmcnt = si.sadhmcnt;
972 sh.sadhcnt = si.sadhcnt;
973
974 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
975 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700976
977 return nlmsg_end(skb, nlh);
978
979nla_put_failure:
980 nlmsg_cancel(skb, nlh);
981 return -EMSGSIZE;
982}
983
984static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700985 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700986{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800987 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700988 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700989 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700990 u32 spid = NETLINK_CB(skb).pid;
991 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700992
Thomas Graf7deb2262007-08-22 13:57:39 -0700993 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700994 if (r_skb == NULL)
995 return -ENOMEM;
996
Alexey Dobriyane0710412010-01-23 13:37:10 +0000997 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700998 BUG();
999
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001000 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001001}
1002
Christoph Hellwig22e70052007-01-02 15:22:30 -08001003static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001004 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001006 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001007 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct xfrm_state *x;
1009 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001010 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001012 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (x == NULL)
1014 goto out_noput;
1015
1016 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1017 if (IS_ERR(resp_skb)) {
1018 err = PTR_ERR(resp_skb);
1019 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001020 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 xfrm_state_put(x);
1023out_noput:
1024 return err;
1025}
1026
1027static int verify_userspi_info(struct xfrm_userspi_info *p)
1028{
1029 switch (p->info.id.proto) {
1030 case IPPROTO_AH:
1031 case IPPROTO_ESP:
1032 break;
1033
1034 case IPPROTO_COMP:
1035 /* IPCOMP spi is 16-bits. */
1036 if (p->max >= 0x10000)
1037 return -EINVAL;
1038 break;
1039
1040 default:
1041 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (p->min > p->max)
1045 return -EINVAL;
1046
1047 return 0;
1048}
1049
Christoph Hellwig22e70052007-01-02 15:22:30 -08001050static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001051 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001053 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct xfrm_state *x;
1055 struct xfrm_userspi_info *p;
1056 struct sk_buff *resp_skb;
1057 xfrm_address_t *daddr;
1058 int family;
1059 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001060 u32 mark;
1061 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Thomas Graf7b67c852007-08-22 13:53:52 -07001063 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 err = verify_userspi_info(p);
1065 if (err)
1066 goto out_noput;
1067
1068 family = p->info.family;
1069 daddr = &p->info.id.daddr;
1070
1071 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001072
1073 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001075 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1077 xfrm_state_put(x);
1078 x = NULL;
1079 }
1080 }
1081
1082 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001083 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 p->info.id.proto, daddr,
1085 &p->info.saddr, 1,
1086 family);
1087 err = -ENOENT;
1088 if (x == NULL)
1089 goto out_noput;
1090
Herbert Xu658b2192007-10-09 13:29:52 -07001091 err = xfrm_alloc_spi(x, p->min, p->max);
1092 if (err)
1093 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Herbert Xu658b2192007-10-09 13:29:52 -07001095 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (IS_ERR(resp_skb)) {
1097 err = PTR_ERR(resp_skb);
1098 goto out;
1099 }
1100
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001101 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103out:
1104 xfrm_state_put(x);
1105out_noput:
1106 return err;
1107}
1108
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001109static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 switch (dir) {
1112 case XFRM_POLICY_IN:
1113 case XFRM_POLICY_OUT:
1114 case XFRM_POLICY_FWD:
1115 break;
1116
1117 default:
1118 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 return 0;
1122}
1123
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001124static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001125{
1126 switch (type) {
1127 case XFRM_POLICY_TYPE_MAIN:
1128#ifdef CONFIG_XFRM_SUB_POLICY
1129 case XFRM_POLICY_TYPE_SUB:
1130#endif
1131 break;
1132
1133 default:
1134 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001135 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001136
1137 return 0;
1138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1141{
1142 switch (p->share) {
1143 case XFRM_SHARE_ANY:
1144 case XFRM_SHARE_SESSION:
1145 case XFRM_SHARE_USER:
1146 case XFRM_SHARE_UNIQUE:
1147 break;
1148
1149 default:
1150 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 switch (p->action) {
1154 case XFRM_POLICY_ALLOW:
1155 case XFRM_POLICY_BLOCK:
1156 break;
1157
1158 default:
1159 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 switch (p->sel.family) {
1163 case AF_INET:
1164 break;
1165
1166 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001167#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 break;
1169#else
1170 return -EAFNOSUPPORT;
1171#endif
1172
1173 default:
1174 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 return verify_policy_dir(p->dir);
1178}
1179
Thomas Graf5424f322007-08-22 14:01:33 -07001180static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001181{
Thomas Graf5424f322007-08-22 14:01:33 -07001182 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001183 struct xfrm_user_sec_ctx *uctx;
1184
1185 if (!rt)
1186 return 0;
1187
Thomas Graf5424f322007-08-22 14:01:33 -07001188 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001189 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1193 int nr)
1194{
1195 int i;
1196
1197 xp->xfrm_nr = nr;
1198 for (i = 0; i < nr; i++, ut++) {
1199 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1200
1201 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1202 memcpy(&t->saddr, &ut->saddr,
1203 sizeof(xfrm_address_t));
1204 t->reqid = ut->reqid;
1205 t->mode = ut->mode;
1206 t->share = ut->share;
1207 t->optional = ut->optional;
1208 t->aalgos = ut->aalgos;
1209 t->ealgos = ut->ealgos;
1210 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001211 /* If all masks are ~0, then we allow all algorithms. */
1212 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001213 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215}
1216
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001217static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1218{
1219 int i;
1220
1221 if (nr > XFRM_MAX_DEPTH)
1222 return -EINVAL;
1223
1224 for (i = 0; i < nr; i++) {
1225 /* We never validated the ut->family value, so many
1226 * applications simply leave it at zero. The check was
1227 * never made and ut->family was ignored because all
1228 * templates could be assumed to have the same family as
1229 * the policy itself. Now that we will have ipv4-in-ipv6
1230 * and ipv6-in-ipv4 tunnels, this is no longer true.
1231 */
1232 if (!ut[i].family)
1233 ut[i].family = family;
1234
1235 switch (ut[i].family) {
1236 case AF_INET:
1237 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001238#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001239 case AF_INET6:
1240 break;
1241#endif
1242 default:
1243 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001244 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001245 }
1246
1247 return 0;
1248}
1249
Thomas Graf5424f322007-08-22 14:01:33 -07001250static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Thomas Graf5424f322007-08-22 14:01:33 -07001252 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (!rt) {
1255 pol->xfrm_nr = 0;
1256 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001257 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1258 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001259 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001261 err = validate_tmpl(nr, utmpl, pol->family);
1262 if (err)
1263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Thomas Graf5424f322007-08-22 14:01:33 -07001265 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 return 0;
1268}
1269
Thomas Graf5424f322007-08-22 14:01:33 -07001270static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001271{
Thomas Graf5424f322007-08-22 14:01:33 -07001272 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001273 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001274 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001275 int err;
1276
1277 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001278 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001279 type = upt->type;
1280 }
1281
1282 err = verify_policy_type(type);
1283 if (err)
1284 return err;
1285
1286 *tp = type;
1287 return 0;
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1291{
1292 xp->priority = p->priority;
1293 xp->index = p->index;
1294 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1295 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1296 xp->action = p->action;
1297 xp->flags = p->flags;
1298 xp->family = p->sel.family;
1299 /* XXX xp->share = p->share; */
1300}
1301
1302static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1303{
Mathias Krause97f96ea2012-09-19 11:33:40 +00001304 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1306 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1307 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1308 p->priority = xp->priority;
1309 p->index = xp->index;
1310 p->sel.family = xp->family;
1311 p->dir = dir;
1312 p->action = xp->action;
1313 p->flags = xp->flags;
1314 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1315}
1316
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001317static 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 -07001318{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001319 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int err;
1321
1322 if (!xp) {
1323 *errp = -ENOMEM;
1324 return NULL;
1325 }
1326
1327 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001328
Thomas Graf35a7aa02007-08-22 14:00:40 -07001329 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001330 if (err)
1331 goto error;
1332
Thomas Graf35a7aa02007-08-22 14:00:40 -07001333 if (!(err = copy_from_user_tmpl(xp, attrs)))
1334 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001335 if (err)
1336 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001338 xfrm_mark_get(attrs, &xp->mark);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001341 error:
1342 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001343 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001344 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001345 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Christoph Hellwig22e70052007-01-02 15:22:30 -08001348static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001349 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001351 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001352 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001354 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int err;
1356 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001357 uid_t loginuid = audit_get_loginuid(current);
1358 u32 sessionid = audit_get_sessionid(current);
1359 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 err = verify_newpolicy_info(p);
1362 if (err)
1363 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001364 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001365 if (err)
1366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001368 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (!xp)
1370 return err;
1371
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001372 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001373 * Aha! this is anti-netlink really i.e more pfkey derived
1374 * in netlink excl is a flag and you wouldnt need
1375 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1377 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001378 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001379 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001382 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 kfree(xp);
1384 return err;
1385 }
1386
Herbert Xuf60f6b82005-06-18 22:44:37 -07001387 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001388 c.seq = nlh->nlmsg_seq;
1389 c.pid = nlh->nlmsg_pid;
1390 km_policy_notify(xp, p->dir, &c);
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 xfrm_pol_put(xp);
1393
1394 return 0;
1395}
1396
1397static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1398{
1399 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1400 int i;
1401
1402 if (xp->xfrm_nr == 0)
1403 return 0;
1404
1405 for (i = 0; i < xp->xfrm_nr; i++) {
1406 struct xfrm_user_tmpl *up = &vec[i];
1407 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1408
Mathias Krause0c5e3752012-09-19 11:33:41 +00001409 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001411 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1413 up->reqid = kp->reqid;
1414 up->mode = kp->mode;
1415 up->share = kp->share;
1416 up->optional = kp->optional;
1417 up->aalgos = kp->aalgos;
1418 up->ealgos = kp->ealgos;
1419 up->calgos = kp->calgos;
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Thomas Grafc0144be2007-08-22 13:55:43 -07001422 return nla_put(skb, XFRMA_TMPL,
1423 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001424}
1425
Serge Hallyn0d681622006-07-24 23:30:44 -07001426static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1427{
1428 if (x->security) {
1429 return copy_sec_ctx(x->security, skb);
1430 }
1431 return 0;
1432}
1433
1434static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1435{
1436 if (xp->security) {
1437 return copy_sec_ctx(xp->security, skb);
1438 }
1439 return 0;
1440}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001441static inline size_t userpolicy_type_attrsize(void)
1442{
1443#ifdef CONFIG_XFRM_SUB_POLICY
1444 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1445#else
1446 return 0;
1447#endif
1448}
Serge Hallyn0d681622006-07-24 23:30:44 -07001449
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001450#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001451static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001452{
Thomas Grafc0144be2007-08-22 13:55:43 -07001453 struct xfrm_userpolicy_type upt = {
1454 .type = type,
1455 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001456
Thomas Grafc0144be2007-08-22 13:55:43 -07001457 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001458}
1459
1460#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001461static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001462{
1463 return 0;
1464}
1465#endif
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1468{
1469 struct xfrm_dump_info *sp = ptr;
1470 struct xfrm_userpolicy_info *p;
1471 struct sk_buff *in_skb = sp->in_skb;
1472 struct sk_buff *skb = sp->out_skb;
1473 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001475 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1476 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1477 if (nlh == NULL)
1478 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Thomas Graf7b67c852007-08-22 13:53:52 -07001480 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 copy_to_user_policy(xp, p, dir);
1482 if (copy_to_user_tmpl(xp, skb) < 0)
1483 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001484 if (copy_to_user_sec_ctx(xp, skb))
1485 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001486 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001487 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001488 if (xfrm_mark_put(skb, &xp->mark))
1489 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Thomas Graf98250692007-08-22 12:47:26 -07001491 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
1493
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001494nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001496 nlmsg_cancel(skb, nlh);
1497 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Timo Teras4c563f72008-02-28 21:31:08 -08001500static int xfrm_dump_policy_done(struct netlink_callback *cb)
1501{
1502 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1503
1504 xfrm_policy_walk_done(walk);
1505 return 0;
1506}
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1509{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001510 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001511 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct xfrm_dump_info info;
1513
Timo Teras4c563f72008-02-28 21:31:08 -08001514 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1515 sizeof(cb->args) - sizeof(cb->args[0]));
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 info.in_skb = cb->skb;
1518 info.out_skb = skb;
1519 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1520 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001521
1522 if (!cb->args[0]) {
1523 cb->args[0] = 1;
1524 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1525 }
1526
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001527 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 return skb->len;
1530}
1531
1532static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1533 struct xfrm_policy *xp,
1534 int dir, u32 seq)
1535{
1536 struct xfrm_dump_info info;
1537 struct sk_buff *skb;
Mathias Krausef38b3342012-09-14 09:58:32 +00001538 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Thomas Graf7deb2262007-08-22 13:57:39 -07001540 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (!skb)
1542 return ERR_PTR(-ENOMEM);
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 info.in_skb = in_skb;
1545 info.out_skb = skb;
1546 info.nlmsg_seq = seq;
1547 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Mathias Krausef38b3342012-09-14 09:58:32 +00001549 err = dump_one_policy(xp, dir, 0, &info);
1550 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 kfree_skb(skb);
Mathias Krausef38b3342012-09-14 09:58:32 +00001552 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554
1555 return skb;
1556}
1557
Christoph Hellwig22e70052007-01-02 15:22:30 -08001558static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001559 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001561 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 struct xfrm_policy *xp;
1563 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001564 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001566 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001568 struct xfrm_mark m;
1569 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Thomas Graf7b67c852007-08-22 13:53:52 -07001571 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1573
Thomas Graf35a7aa02007-08-22 14:00:40 -07001574 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001575 if (err)
1576 return err;
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 err = verify_policy_dir(p->dir);
1579 if (err)
1580 return err;
1581
1582 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001583 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001584 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001585 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001586 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001587
Thomas Graf35a7aa02007-08-22 14:00:40 -07001588 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001589 if (err)
1590 return err;
1591
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001592 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001593 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001594 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001595
Paul Moore03e1ad72008-04-12 19:07:52 -07001596 err = security_xfrm_policy_alloc(&ctx, uctx);
1597 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001598 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001599 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001600 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001601 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001602 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (xp == NULL)
1605 return -ENOENT;
1606
1607 if (!delete) {
1608 struct sk_buff *resp_skb;
1609
1610 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1611 if (IS_ERR(resp_skb)) {
1612 err = PTR_ERR(resp_skb);
1613 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001614 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001615 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001617 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001618 uid_t loginuid = audit_get_loginuid(current);
1619 u32 sessionid = audit_get_sessionid(current);
1620 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001621
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001622 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001623 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1624 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001625
1626 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001627 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001628
Herbert Xue7443892005-06-18 22:44:18 -07001629 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001630 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001631 c.seq = nlh->nlmsg_seq;
1632 c.pid = nlh->nlmsg_pid;
1633 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001636out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001637 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return err;
1639}
1640
Christoph Hellwig22e70052007-01-02 15:22:30 -08001641static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001642 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001644 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001645 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001646 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001647 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001648 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001650 audit_info.loginuid = audit_get_loginuid(current);
1651 audit_info.sessionid = audit_get_sessionid(current);
1652 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001653 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001654 if (err) {
1655 if (err == -ESRCH) /* empty table */
1656 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001657 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001658 }
Herbert Xubf088672005-06-18 22:44:00 -07001659 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001660 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001661 c.seq = nlh->nlmsg_seq;
1662 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001663 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001664 km_state_notify(NULL, &c);
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return 0;
1667}
1668
Steffen Klassertd8647b72011-03-08 00:10:27 +00001669static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001670{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001671 size_t replay_size = x->replay_esn ?
1672 xfrm_replay_state_esn_len(x->replay_esn) :
1673 sizeof(struct xfrm_replay_state);
1674
Thomas Graf7deb2262007-08-22 13:57:39 -07001675 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001676 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001677 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001678 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001679 + nla_total_size(4) /* XFRM_AE_RTHR */
1680 + nla_total_size(4); /* XFRM_AE_ETHR */
1681}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001682
David S. Miller214e0052011-02-24 00:02:38 -05001683static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001684{
1685 struct xfrm_aevent_id *id;
1686 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001687
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001688 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1689 if (nlh == NULL)
1690 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001691
Thomas Graf7b67c852007-08-22 13:53:52 -07001692 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001693 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001694 id->sa_id.spi = x->id.spi;
1695 id->sa_id.family = x->props.family;
1696 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001697 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1698 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001699 id->flags = c->data.aevent;
1700
Steffen Klassertd8647b72011-03-08 00:10:27 +00001701 if (x->replay_esn)
1702 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1703 xfrm_replay_state_esn_len(x->replay_esn),
1704 x->replay_esn);
1705 else
1706 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1707
Thomas Grafc0144be2007-08-22 13:55:43 -07001708 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001709
Thomas Grafc0144be2007-08-22 13:55:43 -07001710 if (id->flags & XFRM_AE_RTHR)
1711 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001712
Thomas Grafc0144be2007-08-22 13:55:43 -07001713 if (id->flags & XFRM_AE_ETHR)
1714 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1715 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001716
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001717 if (xfrm_mark_put(skb, &x->mark))
1718 goto nla_put_failure;
1719
Thomas Graf98250692007-08-22 12:47:26 -07001720 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001721
Thomas Grafc0144be2007-08-22 13:55:43 -07001722nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001723 nlmsg_cancel(skb, nlh);
1724 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001725}
1726
Christoph Hellwig22e70052007-01-02 15:22:30 -08001727static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001728 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001729{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001730 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001731 struct xfrm_state *x;
1732 struct sk_buff *r_skb;
1733 int err;
1734 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001735 u32 mark;
1736 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001737 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001738 struct xfrm_usersa_id *id = &p->sa_id;
1739
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001740 mark = xfrm_mark_get(attrs, &m);
1741
1742 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001743 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001744 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001745
1746 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1747 if (r_skb == NULL) {
1748 xfrm_state_put(x);
1749 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001750 }
1751
1752 /*
1753 * XXX: is this lock really needed - none of the other
1754 * gets lock (the concern is things getting updated
1755 * while we are still reading) - jhs
1756 */
1757 spin_lock_bh(&x->lock);
1758 c.data.aevent = p->flags;
1759 c.seq = nlh->nlmsg_seq;
1760 c.pid = nlh->nlmsg_pid;
1761
1762 if (build_aevent(r_skb, x, &c) < 0)
1763 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001764 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001765 spin_unlock_bh(&x->lock);
1766 xfrm_state_put(x);
1767 return err;
1768}
1769
Christoph Hellwig22e70052007-01-02 15:22:30 -08001770static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001771 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001772{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001773 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001774 struct xfrm_state *x;
1775 struct km_event c;
1776 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001777 u32 mark = 0;
1778 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001779 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001780 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001781 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001782 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001783
Steffen Klassertd8647b72011-03-08 00:10:27 +00001784 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001785 return err;
1786
1787 /* pedantic mode - thou shalt sayeth replaceth */
1788 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1789 return err;
1790
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001791 mark = xfrm_mark_get(attrs, &m);
1792
1793 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 -08001794 if (x == NULL)
1795 return -ESRCH;
1796
1797 if (x->km.state != XFRM_STATE_VALID)
1798 goto out;
1799
Steffen Klasserte2b19122011-03-28 19:47:30 +00001800 err = xfrm_replay_verify_len(x->replay_esn, rp);
1801 if (err)
1802 goto out;
1803
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001804 spin_lock_bh(&x->lock);
Mathias Krause743b9112012-09-19 11:33:43 +00001805 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001807
1808 c.event = nlh->nlmsg_type;
1809 c.seq = nlh->nlmsg_seq;
1810 c.pid = nlh->nlmsg_pid;
1811 c.data.aevent = XFRM_AE_CU;
1812 km_state_notify(x, &c);
1813 err = 0;
1814out:
1815 xfrm_state_put(x);
1816 return err;
1817}
1818
Christoph Hellwig22e70052007-01-02 15:22:30 -08001819static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001820 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001822 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001823 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001824 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001825 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001826 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001827
Thomas Graf35a7aa02007-08-22 14:00:40 -07001828 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001829 if (err)
1830 return err;
1831
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001832 audit_info.loginuid = audit_get_loginuid(current);
1833 audit_info.sessionid = audit_get_sessionid(current);
1834 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001835 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001836 if (err) {
1837 if (err == -ESRCH) /* empty table */
1838 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001839 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001840 }
1841
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001842 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001843 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001844 c.seq = nlh->nlmsg_seq;
1845 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001846 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001847 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return 0;
1849}
1850
Christoph Hellwig22e70052007-01-02 15:22:30 -08001851static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001852 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001853{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001854 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001855 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001856 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001857 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001858 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001859 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001860 struct xfrm_mark m;
1861 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001862
Thomas Graf35a7aa02007-08-22 14:00:40 -07001863 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001864 if (err)
1865 return err;
1866
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001867 err = verify_policy_dir(p->dir);
1868 if (err)
1869 return err;
1870
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001871 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001872 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001873 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001874 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001875 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001876
Thomas Graf35a7aa02007-08-22 14:00:40 -07001877 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001878 if (err)
1879 return err;
1880
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001881 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001882 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001883 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001884
Paul Moore03e1ad72008-04-12 19:07:52 -07001885 err = security_xfrm_policy_alloc(&ctx, uctx);
1886 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001887 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001888 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001889 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001890 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001891 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001892 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001893 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001894 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001895
Timo Teräsea2dea92010-03-31 00:17:05 +00001896 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001897 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001898
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001899 err = 0;
1900 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001901 uid_t loginuid = audit_get_loginuid(current);
1902 u32 sessionid = audit_get_sessionid(current);
1903 u32 sid;
1904
1905 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001906 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001907 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001908
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001909 } else {
1910 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001911 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001912 }
1913 km_policy_expired(xp, p->dir, up->hard, current->pid);
1914
1915out:
1916 xfrm_pol_put(xp);
1917 return err;
1918}
1919
Christoph Hellwig22e70052007-01-02 15:22:30 -08001920static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001921 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001922{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001923 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001924 struct xfrm_state *x;
1925 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001926 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001927 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001928 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001929 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001930
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001931 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 -08001932
David S. Miller3a765aa2007-02-26 14:52:21 -08001933 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001934 if (x == NULL)
1935 return err;
1936
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001937 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001938 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001939 if (x->km.state != XFRM_STATE_VALID)
1940 goto out;
1941 km_state_expired(x, ue->hard, current->pid);
1942
Joy Latten161a09e2006-11-27 13:11:54 -06001943 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001944 uid_t loginuid = audit_get_loginuid(current);
1945 u32 sessionid = audit_get_sessionid(current);
1946 u32 sid;
1947
1948 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001949 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001950 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001951 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001952 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001953out:
1954 spin_unlock_bh(&x->lock);
1955 xfrm_state_put(x);
1956 return err;
1957}
1958
Christoph Hellwig22e70052007-01-02 15:22:30 -08001959static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001960 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001961{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001962 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001963 struct xfrm_policy *xp;
1964 struct xfrm_user_tmpl *ut;
1965 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001966 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001967 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001968
Thomas Graf7b67c852007-08-22 13:53:52 -07001969 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001970 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001971 int err = -ENOMEM;
1972
1973 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001974 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001975
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001976 xfrm_mark_get(attrs, &mark);
1977
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001978 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001979 if (err)
1980 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001981
1982 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001983 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001984 if (!xp)
1985 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001986
1987 memcpy(&x->id, &ua->id, sizeof(ua->id));
1988 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1989 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001990 xp->mark.m = x->mark.m = mark.m;
1991 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001992 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001993 /* extract the templates and for each call km_key */
1994 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1995 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1996 memcpy(&x->id, &t->id, sizeof(x->id));
1997 x->props.mode = t->mode;
1998 x->props.reqid = t->reqid;
1999 x->props.family = ut->family;
2000 t->aalgos = ua->aalgos;
2001 t->ealgos = ua->ealgos;
2002 t->calgos = ua->calgos;
2003 err = km_query(x, t, xp);
2004
2005 }
2006
2007 kfree(x);
2008 kfree(xp);
2009
2010 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002011
2012bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002013 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002014free_state:
2015 kfree(x);
2016nomem:
2017 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002018}
2019
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002020#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002021static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002022 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002023 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002024{
Thomas Graf5424f322007-08-22 14:01:33 -07002025 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002026 struct xfrm_user_migrate *um;
2027 int i, num_migrate;
2028
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002029 if (k != NULL) {
2030 struct xfrm_user_kmaddress *uk;
2031
2032 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2033 memcpy(&k->local, &uk->local, sizeof(k->local));
2034 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2035 k->family = uk->family;
2036 k->reserved = uk->reserved;
2037 }
2038
Thomas Graf5424f322007-08-22 14:01:33 -07002039 um = nla_data(rt);
2040 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002041
2042 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2043 return -EINVAL;
2044
2045 for (i = 0; i < num_migrate; i++, um++, ma++) {
2046 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2047 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2048 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2049 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2050
2051 ma->proto = um->proto;
2052 ma->mode = um->mode;
2053 ma->reqid = um->reqid;
2054
2055 ma->old_family = um->old_family;
2056 ma->new_family = um->new_family;
2057 }
2058
2059 *num = i;
2060 return 0;
2061}
2062
2063static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002064 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002065{
Thomas Graf7b67c852007-08-22 13:53:52 -07002066 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002067 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002068 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002069 u8 type;
2070 int err;
2071 int n = 0;
2072
Thomas Graf35a7aa02007-08-22 14:00:40 -07002073 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002074 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002075
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002076 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2077
Thomas Graf5424f322007-08-22 14:01:33 -07002078 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002079 if (err)
2080 return err;
2081
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002082 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002083 if (err)
2084 return err;
2085
2086 if (!n)
2087 return 0;
2088
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002089 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002090
2091 return 0;
2092}
2093#else
2094static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002095 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002096{
2097 return -ENOPROTOOPT;
2098}
2099#endif
2100
2101#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002102static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002103{
2104 struct xfrm_user_migrate um;
2105
2106 memset(&um, 0, sizeof(um));
2107 um.proto = m->proto;
2108 um.mode = m->mode;
2109 um.reqid = m->reqid;
2110 um.old_family = m->old_family;
2111 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2112 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2113 um.new_family = m->new_family;
2114 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2115 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2116
Thomas Grafc0144be2007-08-22 13:55:43 -07002117 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002118}
2119
David S. Miller183cad12011-02-24 00:28:01 -05002120static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002121{
2122 struct xfrm_user_kmaddress uk;
2123
2124 memset(&uk, 0, sizeof(uk));
2125 uk.family = k->family;
2126 uk.reserved = k->reserved;
2127 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002128 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002129
2130 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2131}
2132
2133static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002134{
2135 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002136 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2137 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2138 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002139}
2140
David S. Miller183cad12011-02-24 00:28:01 -05002141static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2142 int num_migrate, const struct xfrm_kmaddress *k,
2143 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002144{
David S. Miller183cad12011-02-24 00:28:01 -05002145 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002146 struct xfrm_userpolicy_id *pol_id;
2147 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002148 int i;
2149
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002150 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2151 if (nlh == NULL)
2152 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002153
Thomas Graf7b67c852007-08-22 13:53:52 -07002154 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002155 /* copy data from selector, dir, and type to the pol_id */
2156 memset(pol_id, 0, sizeof(*pol_id));
2157 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2158 pol_id->dir = dir;
2159
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002160 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2161 goto nlmsg_failure;
2162
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002163 if (copy_to_user_policy_type(type, skb) < 0)
2164 goto nlmsg_failure;
2165
2166 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2167 if (copy_to_user_migrate(mp, skb) < 0)
2168 goto nlmsg_failure;
2169 }
2170
Thomas Graf98250692007-08-22 12:47:26 -07002171 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002172nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002173 nlmsg_cancel(skb, nlh);
2174 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002175}
2176
David S. Miller183cad12011-02-24 00:28:01 -05002177static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2178 const struct xfrm_migrate *m, int num_migrate,
2179 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002180{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002181 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002182 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002183
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002184 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002185 if (skb == NULL)
2186 return -ENOMEM;
2187
2188 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002189 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002190 BUG();
2191
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002192 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002193}
2194#else
David S. Miller183cad12011-02-24 00:28:01 -05002195static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2196 const struct xfrm_migrate *m, int num_migrate,
2197 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002198{
2199 return -ENOPROTOOPT;
2200}
2201#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002202
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002203#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002204
2205static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002206 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002207 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2208 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2209 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2210 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2211 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2212 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002213 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002214 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002215 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002216 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002217 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002218 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002219 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002220 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2221 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002222 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002223 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002224 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2225 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226};
2227
Thomas Graf492b5582005-05-03 14:26:40 -07002228#undef XMSGSIZE
2229
Thomas Grafcf5cb792007-08-22 13:59:04 -07002230static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002231 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2232 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2233 [XFRMA_LASTUSED] = { .type = NLA_U64},
2234 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002235 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002236 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2237 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2238 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2239 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2240 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2241 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2242 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2243 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2244 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2245 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2246 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2247 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2248 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2249 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002250 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002251 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002252 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002253 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002254};
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002257 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002259 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002260} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2261 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2262 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2263 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002264 .dump = xfrm_dump_sa,
2265 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002266 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2267 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2268 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002269 .dump = xfrm_dump_policy,
2270 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002271 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002272 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002273 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002274 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2275 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002276 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002277 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2278 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002279 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2280 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002281 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002282 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002283 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284};
2285
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002286static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002288 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002289 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002291 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 type -= XFRM_MSG_BASE;
2298 link = &xfrm_dispatch[type];
2299
2300 /* All operations require privileges, even GET */
Eric Parisfd778462012-01-03 12:25:16 -05002301 if (!capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002302 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Thomas Graf492b5582005-05-03 14:26:40 -07002304 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2305 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002306 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002308 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002310 {
2311 struct netlink_dump_control c = {
2312 .dump = link->dump,
2313 .done = link->done,
2314 };
2315 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318
Thomas Graf35a7aa02007-08-22 14:00:40 -07002319 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002320 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002321 if (err < 0)
2322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Thomas Graf5424f322007-08-22 14:01:33 -07002327 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002330static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002332 mutex_lock(&xfrm_cfg_mutex);
2333 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2334 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Thomas Graf7deb2262007-08-22 13:57:39 -07002337static inline size_t xfrm_expire_msgsize(void)
2338{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002339 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2340 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002341}
2342
David S. Miller214e0052011-02-24 00:02:38 -05002343static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 struct xfrm_user_expire *ue;
2346 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002348 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2349 if (nlh == NULL)
2350 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Thomas Graf7b67c852007-08-22 13:53:52 -07002352 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002354 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002356 if (xfrm_mark_put(skb, &x->mark))
2357 goto nla_put_failure;
2358
Thomas Graf98250692007-08-22 12:47:26 -07002359 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002360
2361nla_put_failure:
2362 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
David S. Miller214e0052011-02-24 00:02:38 -05002365static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002367 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 struct sk_buff *skb;
2369
Thomas Graf7deb2262007-08-22 13:57:39 -07002370 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (skb == NULL)
2372 return -ENOMEM;
2373
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002374 if (build_expire(skb, x, c) < 0) {
2375 kfree_skb(skb);
2376 return -EMSGSIZE;
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002379 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
David S. Miller214e0052011-02-24 00:02:38 -05002382static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002383{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002384 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002385 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002386
Steffen Klassertd8647b72011-03-08 00:10:27 +00002387 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002388 if (skb == NULL)
2389 return -ENOMEM;
2390
2391 if (build_aevent(skb, x, c) < 0)
2392 BUG();
2393
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002394 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002395}
2396
David S. Miller214e0052011-02-24 00:02:38 -05002397static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002398{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002399 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002400 struct xfrm_usersa_flush *p;
2401 struct nlmsghdr *nlh;
2402 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002403 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002404
Thomas Graf7deb2262007-08-22 13:57:39 -07002405 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002406 if (skb == NULL)
2407 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002408
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002409 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2410 if (nlh == NULL) {
2411 kfree_skb(skb);
2412 return -EMSGSIZE;
2413 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002414
Thomas Graf7b67c852007-08-22 13:53:52 -07002415 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002416 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417
Thomas Graf98250692007-08-22 12:47:26 -07002418 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002419
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002420 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002421}
2422
Thomas Graf7deb2262007-08-22 13:57:39 -07002423static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002424{
Thomas Graf7deb2262007-08-22 13:57:39 -07002425 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002426 if (x->aead)
2427 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002428 if (x->aalg) {
2429 l += nla_total_size(sizeof(struct xfrm_algo) +
2430 (x->aalg->alg_key_len + 7) / 8);
2431 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2432 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002433 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002434 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002435 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002436 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002437 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002438 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002439 if (x->tfcpad)
2440 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002441 if (x->replay_esn)
2442 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002443 if (x->security)
2444 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2445 x->security->ctx_len);
2446 if (x->coaddr)
2447 l += nla_total_size(sizeof(*x->coaddr));
2448
Herbert Xud26f3982007-11-13 21:47:08 -08002449 /* Must count x->lastused as it may become non-zero behind our back. */
2450 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002451
2452 return l;
2453}
2454
David S. Miller214e0052011-02-24 00:02:38 -05002455static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002457 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002458 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002459 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460 struct nlmsghdr *nlh;
2461 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002462 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002463 int headlen;
2464
2465 headlen = sizeof(*p);
2466 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002467 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002468 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002469 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002470 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002471 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002472
Thomas Graf7deb2262007-08-22 13:57:39 -07002473 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002474 if (skb == NULL)
2475 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002476
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002477 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2478 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002479 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002480
Thomas Graf7b67c852007-08-22 13:53:52 -07002481 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002482 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002483 struct nlattr *attr;
2484
Thomas Graf7b67c852007-08-22 13:53:52 -07002485 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002486 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2487 id->spi = x->id.spi;
2488 id->family = x->props.family;
2489 id->proto = x->id.proto;
2490
Thomas Grafc0144be2007-08-22 13:55:43 -07002491 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2492 if (attr == NULL)
2493 goto nla_put_failure;
2494
2495 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002496 }
2497
Herbert Xu68325d32007-10-09 13:30:57 -07002498 if (copy_to_user_state_extra(x, p, skb))
2499 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002500
Thomas Graf98250692007-08-22 12:47:26 -07002501 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002502
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002503 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002504
Thomas Grafc0144be2007-08-22 13:55:43 -07002505nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002506 /* Somebody screwed up with xfrm_sa_len! */
2507 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002508 kfree_skb(skb);
2509 return -1;
2510}
2511
David S. Miller214e0052011-02-24 00:02:38 -05002512static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002513{
2514
2515 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002516 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002517 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002518 case XFRM_MSG_NEWAE:
2519 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002520 case XFRM_MSG_DELSA:
2521 case XFRM_MSG_UPDSA:
2522 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002523 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002524 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002525 return xfrm_notify_sa_flush(c);
2526 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002527 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2528 c->event);
2529 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002530 }
2531
2532 return 0;
2533
2534}
2535
Thomas Graf7deb2262007-08-22 13:57:39 -07002536static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2537 struct xfrm_policy *xp)
2538{
2539 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2540 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002541 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002542 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2543 + userpolicy_type_attrsize();
2544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2547 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2548 int dir)
2549{
2550 struct xfrm_user_acquire *ua;
2551 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 __u32 seq = xfrm_get_acqseq();
2553
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002554 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2555 if (nlh == NULL)
2556 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Thomas Graf7b67c852007-08-22 13:53:52 -07002558 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 memcpy(&ua->id, &x->id, sizeof(ua->id));
2560 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2561 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2562 copy_to_user_policy(xp, &ua->policy, dir);
2563 ua->aalgos = xt->aalgos;
2564 ua->ealgos = xt->ealgos;
2565 ua->calgos = xt->calgos;
2566 ua->seq = x->km.seq = seq;
2567
2568 if (copy_to_user_tmpl(xp, skb) < 0)
2569 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002570 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002571 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002572 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002573 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002574 if (xfrm_mark_put(skb, &xp->mark))
2575 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Thomas Graf98250692007-08-22 12:47:26 -07002577 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002579nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002581 nlmsg_cancel(skb, nlh);
2582 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
2585static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2586 struct xfrm_policy *xp, int dir)
2587{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002588 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Thomas Graf7deb2262007-08-22 13:57:39 -07002591 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (skb == NULL)
2593 return -ENOMEM;
2594
2595 if (build_acquire(skb, x, xt, xp, dir) < 0)
2596 BUG();
2597
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002598 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
2601/* User gives us xfrm_user_policy_info followed by an array of 0
2602 * or more templates.
2603 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002604static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 u8 *data, int len, int *dir)
2606{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002607 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2609 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2610 struct xfrm_policy *xp;
2611 int nr;
2612
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002613 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 case AF_INET:
2615 if (opt != IP_XFRM_POLICY) {
2616 *dir = -EOPNOTSUPP;
2617 return NULL;
2618 }
2619 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002620#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 case AF_INET6:
2622 if (opt != IPV6_XFRM_POLICY) {
2623 *dir = -EOPNOTSUPP;
2624 return NULL;
2625 }
2626 break;
2627#endif
2628 default:
2629 *dir = -EINVAL;
2630 return NULL;
2631 }
2632
2633 *dir = -EINVAL;
2634
2635 if (len < sizeof(*p) ||
2636 verify_newpolicy_info(p))
2637 return NULL;
2638
2639 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002640 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return NULL;
2642
Herbert Xua4f1bac2005-07-26 15:43:17 -07002643 if (p->dir > XFRM_POLICY_OUT)
2644 return NULL;
2645
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002646 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (xp == NULL) {
2648 *dir = -ENOBUFS;
2649 return NULL;
2650 }
2651
2652 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002653 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 copy_templates(xp, ut, nr);
2655
2656 *dir = p->dir;
2657
2658 return xp;
2659}
2660
Thomas Graf7deb2262007-08-22 13:57:39 -07002661static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2662{
2663 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2664 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2665 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002666 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002667 + userpolicy_type_attrsize();
2668}
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002671 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 struct xfrm_user_polexpire *upe;
2674 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002675 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002677 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2678 if (nlh == NULL)
2679 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Thomas Graf7b67c852007-08-22 13:53:52 -07002681 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 copy_to_user_policy(xp, &upe->pol, dir);
2683 if (copy_to_user_tmpl(xp, skb) < 0)
2684 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002685 if (copy_to_user_sec_ctx(xp, skb))
2686 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002687 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002688 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002689 if (xfrm_mark_put(skb, &xp->mark))
2690 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 upe->hard = !!hard;
2692
Thomas Graf98250692007-08-22 12:47:26 -07002693 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002695nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002697 nlmsg_cancel(skb, nlh);
2698 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699}
2700
David S. Miller214e0052011-02-24 00:02:38 -05002701static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002703 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Thomas Graf7deb2262007-08-22 13:57:39 -07002706 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (skb == NULL)
2708 return -ENOMEM;
2709
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002710 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 BUG();
2712
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002713 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
David S. Miller214e0052011-02-24 00:02:38 -05002716static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002717{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002718 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002719 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002720 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002721 struct nlmsghdr *nlh;
2722 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002723 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002724 int headlen;
2725
2726 headlen = sizeof(*p);
2727 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002728 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002729 headlen = sizeof(*id);
2730 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002731 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002732 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002733 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002734
Thomas Graf7deb2262007-08-22 13:57:39 -07002735 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002736 if (skb == NULL)
2737 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002738
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002739 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2740 if (nlh == NULL)
2741 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002742
Thomas Graf7b67c852007-08-22 13:53:52 -07002743 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002744 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002745 struct nlattr *attr;
2746
Thomas Graf7b67c852007-08-22 13:53:52 -07002747 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002748 memset(id, 0, sizeof(*id));
2749 id->dir = dir;
2750 if (c->data.byid)
2751 id->index = xp->index;
2752 else
2753 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2754
Thomas Grafc0144be2007-08-22 13:55:43 -07002755 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2756 if (attr == NULL)
2757 goto nlmsg_failure;
2758
2759 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002760 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002761
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002762 copy_to_user_policy(xp, p, dir);
2763 if (copy_to_user_tmpl(xp, skb) < 0)
2764 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002765 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002766 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002767
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002768 if (xfrm_mark_put(skb, &xp->mark))
2769 goto nla_put_failure;
2770
Thomas Graf98250692007-08-22 12:47:26 -07002771 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002772
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002773 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002774
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002775nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002776nlmsg_failure:
2777 kfree_skb(skb);
2778 return -1;
2779}
2780
David S. Miller214e0052011-02-24 00:02:38 -05002781static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002782{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002783 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002784 struct nlmsghdr *nlh;
2785 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002786
Thomas Graf7deb2262007-08-22 13:57:39 -07002787 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002788 if (skb == NULL)
2789 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002790
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002791 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2792 if (nlh == NULL)
2793 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002794 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2795 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002796
Thomas Graf98250692007-08-22 12:47:26 -07002797 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002798
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002799 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002800
2801nlmsg_failure:
2802 kfree_skb(skb);
2803 return -1;
2804}
2805
David S. Miller214e0052011-02-24 00:02:38 -05002806static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002807{
2808
2809 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002810 case XFRM_MSG_NEWPOLICY:
2811 case XFRM_MSG_UPDPOLICY:
2812 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002813 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002814 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002815 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002816 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002817 return xfrm_exp_policy_notify(xp, dir, c);
2818 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002819 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2820 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002821 }
2822
2823 return 0;
2824
2825}
2826
Thomas Graf7deb2262007-08-22 13:57:39 -07002827static inline size_t xfrm_report_msgsize(void)
2828{
2829 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2830}
2831
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002832static int build_report(struct sk_buff *skb, u8 proto,
2833 struct xfrm_selector *sel, xfrm_address_t *addr)
2834{
2835 struct xfrm_user_report *ur;
2836 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002837
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002838 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2839 if (nlh == NULL)
2840 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002841
Thomas Graf7b67c852007-08-22 13:53:52 -07002842 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002843 ur->proto = proto;
2844 memcpy(&ur->sel, sel, sizeof(ur->sel));
2845
2846 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002847 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002848
Thomas Graf98250692007-08-22 12:47:26 -07002849 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002850
Thomas Grafc0144be2007-08-22 13:55:43 -07002851nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002852 nlmsg_cancel(skb, nlh);
2853 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002854}
2855
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002856static int xfrm_send_report(struct net *net, u8 proto,
2857 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002858{
2859 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002860
Thomas Graf7deb2262007-08-22 13:57:39 -07002861 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002862 if (skb == NULL)
2863 return -ENOMEM;
2864
2865 if (build_report(skb, proto, sel, addr) < 0)
2866 BUG();
2867
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002868 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002869}
2870
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002871static inline size_t xfrm_mapping_msgsize(void)
2872{
2873 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2874}
2875
2876static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2877 xfrm_address_t *new_saddr, __be16 new_sport)
2878{
2879 struct xfrm_user_mapping *um;
2880 struct nlmsghdr *nlh;
2881
2882 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2883 if (nlh == NULL)
2884 return -EMSGSIZE;
2885
2886 um = nlmsg_data(nlh);
2887
2888 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2889 um->id.spi = x->id.spi;
2890 um->id.family = x->props.family;
2891 um->id.proto = x->id.proto;
2892 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2893 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2894 um->new_sport = new_sport;
2895 um->old_sport = x->encap->encap_sport;
2896 um->reqid = x->props.reqid;
2897
2898 return nlmsg_end(skb, nlh);
2899}
2900
2901static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2902 __be16 sport)
2903{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002904 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002905 struct sk_buff *skb;
2906
2907 if (x->id.proto != IPPROTO_ESP)
2908 return -EINVAL;
2909
2910 if (!x->encap)
2911 return -EINVAL;
2912
2913 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2914 if (skb == NULL)
2915 return -ENOMEM;
2916
2917 if (build_mapping(skb, x, ipaddr, sport) < 0)
2918 BUG();
2919
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002920 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002921}
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923static struct xfrm_mgr netlink_mgr = {
2924 .id = "netlink",
2925 .notify = xfrm_send_state_notify,
2926 .acquire = xfrm_send_acquire,
2927 .compile_policy = xfrm_compile_policy,
2928 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002929 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002930 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002931 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932};
2933
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002934static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Patrick McHardybe336902006-03-20 22:40:54 -08002936 struct sock *nlsk;
2937
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002938 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002939 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002940 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002942 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00002943 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return 0;
2945}
2946
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002947static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002948{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002949 struct net *net;
2950 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002951 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002952 synchronize_net();
2953 list_for_each_entry(net, net_exit_list, exit_list)
2954 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002955}
2956
2957static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002958 .init = xfrm_user_net_init,
2959 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002960};
2961
2962static int __init xfrm_user_init(void)
2963{
2964 int rv;
2965
2966 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2967
2968 rv = register_pernet_subsys(&xfrm_user_net_ops);
2969 if (rv < 0)
2970 return rv;
2971 rv = xfrm_register_km(&netlink_mgr);
2972 if (rv < 0)
2973 unregister_pernet_subsys(&xfrm_user_net_ops);
2974 return rv;
2975}
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977static void __exit xfrm_user_exit(void)
2978{
2979 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002980 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
2983module_init(xfrm_user_init);
2984module_exit(xfrm_user_exit);
2985MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002986MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002987