blob: 74ea70f5f3584f2b7c689a0faadd2d5329de1f3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Herbert Xu1a6509d2008-01-28 19:37:29 -080035static inline int aead_len(struct xfrm_algo_aead *alg)
36{
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
38}
39
Thomas Graf5424f322007-08-22 14:01:33 -070040static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Thomas Graf5424f322007-08-22 14:01:33 -070042 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct xfrm_algo *algp;
44
45 if (!rt)
46 return 0;
47
Thomas Graf5424f322007-08-22 14:01:33 -070048 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080049 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070050 return -EINVAL;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (type) {
53 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 break;
57
58 default:
59 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
63 return 0;
64}
65
Martin Willi4447bb32009-11-25 00:29:52 +000066static int verify_auth_trunc(struct nlattr **attrs)
67{
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
70
71 if (!rt)
72 return 0;
73
74 algp = nla_data(rt);
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
76 return -EINVAL;
77
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79 return 0;
80}
81
Herbert Xu1a6509d2008-01-28 19:37:29 -080082static int verify_aead(struct nlattr **attrs)
83{
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
86
87 if (!rt)
88 return 0;
89
90 algp = nla_data(rt);
91 if (nla_len(rt) < aead_len(algp))
92 return -EINVAL;
93
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
95 return 0;
96}
97
Thomas Graf5424f322007-08-22 14:01:33 -070098static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070099 xfrm_address_t **addrp)
100{
Thomas Graf5424f322007-08-22 14:01:33 -0700101 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700102
Thomas Grafcf5cb792007-08-22 13:59:04 -0700103 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700104 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700105}
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
Thomas Graf5424f322007-08-22 14:01:33 -0700107static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800108{
Thomas Graf5424f322007-08-22 14:01:33 -0700109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800110 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800111
112 if (!rt)
113 return 0;
114
Thomas Graf5424f322007-08-22 14:01:33 -0700115 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800117 return -EINVAL;
118
119 return 0;
120}
121
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
124{
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
126
Steffen Klassert7833aa02011-04-25 19:41:21 +0000127 if ((p->flags & XFRM_STATE_ESN) && !rt)
128 return -EINVAL;
129
Steffen Klassertd8647b72011-03-08 00:10:27 +0000130 if (!rt)
131 return 0;
132
Steffen Klassert02aadf72011-03-28 19:48:09 +0000133 if (p->id.proto != IPPROTO_ESP)
134 return -EINVAL;
135
Steffen Klassertd8647b72011-03-08 00:10:27 +0000136 if (p->replay_window != 0)
137 return -EINVAL;
138
139 return 0;
140}
Trent Jaegerdf718372005-12-13 23:12:27 -0800141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700143 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int err;
146
147 err = -EINVAL;
148 switch (p->family) {
149 case AF_INET:
150 break;
151
152 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000153#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 break;
155#else
156 err = -EAFNOSUPPORT;
157 goto out;
158#endif
159
160 default:
161 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 err = -EINVAL;
165 switch (p->id.proto) {
166 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000167 if ((!attrs[XFRMA_ALG_AUTH] &&
168 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800169 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700170 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000171 attrs[XFRMA_ALG_COMP] ||
172 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 goto out;
174 break;
175
176 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800177 if (attrs[XFRMA_ALG_COMP])
178 goto out;
179 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000180 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800181 !attrs[XFRMA_ALG_CRYPT] &&
182 !attrs[XFRMA_ALG_AEAD])
183 goto out;
184 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000185 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800186 attrs[XFRMA_ALG_CRYPT]) &&
187 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000189 if (attrs[XFRMA_TFCPAD] &&
190 p->mode != XFRM_MODE_TUNNEL)
191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193
194 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700195 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800196 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700197 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000198 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000199 attrs[XFRMA_ALG_CRYPT] ||
200 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto out;
202 break;
203
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000204#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700205 case IPPROTO_DSTOPTS:
206 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 if (attrs[XFRMA_ALG_COMP] ||
208 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000209 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800210 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700211 attrs[XFRMA_ALG_CRYPT] ||
212 attrs[XFRMA_ENCAP] ||
213 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000214 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700215 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700216 goto out;
217 break;
218#endif
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 default:
221 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Herbert Xu1a6509d2008-01-28 19:37:29 -0800224 if ((err = verify_aead(attrs)))
225 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000226 if ((err = verify_auth_trunc(attrs)))
227 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700228 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700230 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700232 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700234 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800235 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000236 if ((err = verify_replay(p, attrs)))
237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 err = -EINVAL;
240 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700241 case XFRM_MODE_TRANSPORT:
242 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700243 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700244 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246
247 default:
248 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 err = 0;
252
253out:
254 return err;
255}
256
257static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800258 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700259 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct xfrm_algo *p, *ualg;
262 struct xfrm_algo_desc *algo;
263
264 if (!rta)
265 return 0;
266
Thomas Graf5424f322007-08-22 14:01:33 -0700267 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 algo = get_byname(ualg->alg_name, 1);
270 if (!algo)
271 return -ENOSYS;
272 *props = algo->desc.sadb_alg_id;
273
Eric Dumazet0f99be02008-01-08 23:39:06 -0800274 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!p)
276 return -ENOMEM;
277
Herbert Xu04ff1262006-08-13 08:50:00 +1000278 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *algpp = p;
280 return 0;
281}
282
Martin Willi4447bb32009-11-25 00:29:52 +0000283static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
284 struct nlattr *rta)
285{
286 struct xfrm_algo *ualg;
287 struct xfrm_algo_auth *p;
288 struct xfrm_algo_desc *algo;
289
290 if (!rta)
291 return 0;
292
293 ualg = nla_data(rta);
294
295 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
296 if (!algo)
297 return -ENOSYS;
298 *props = algo->desc.sadb_alg_id;
299
300 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
301 if (!p)
302 return -ENOMEM;
303
304 strcpy(p->alg_name, algo->name);
305 p->alg_key_len = ualg->alg_key_len;
306 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
307 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
308
309 *algpp = p;
310 return 0;
311}
312
313static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
314 struct nlattr *rta)
315{
316 struct xfrm_algo_auth *p, *ualg;
317 struct xfrm_algo_desc *algo;
318
319 if (!rta)
320 return 0;
321
322 ualg = nla_data(rta);
323
324 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
325 if (!algo)
326 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000327 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
328 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000329 return -EINVAL;
330 *props = algo->desc.sadb_alg_id;
331
332 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
333 if (!p)
334 return -ENOMEM;
335
336 strcpy(p->alg_name, algo->name);
337 if (!p->alg_trunc_len)
338 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
339
340 *algpp = p;
341 return 0;
342}
343
Herbert Xu1a6509d2008-01-28 19:37:29 -0800344static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
345 struct nlattr *rta)
346{
347 struct xfrm_algo_aead *p, *ualg;
348 struct xfrm_algo_desc *algo;
349
350 if (!rta)
351 return 0;
352
353 ualg = nla_data(rta);
354
355 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
356 if (!algo)
357 return -ENOSYS;
358 *props = algo->desc.sadb_alg_id;
359
360 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
361 if (!p)
362 return -ENOMEM;
363
364 strcpy(p->alg_name, algo->name);
365 *algpp = p;
366 return 0;
367}
368
Steffen Klasserte2b19122011-03-28 19:47:30 +0000369static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
370 struct nlattr *rp)
371{
372 struct xfrm_replay_state_esn *up;
373
374 if (!replay_esn || !rp)
375 return 0;
376
377 up = nla_data(rp);
378
379 if (xfrm_replay_state_esn_len(replay_esn) !=
380 xfrm_replay_state_esn_len(up))
381 return -EINVAL;
382
383 return 0;
384}
385
Steffen Klassertd8647b72011-03-08 00:10:27 +0000386static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
387 struct xfrm_replay_state_esn **preplay_esn,
388 struct nlattr *rta)
389{
390 struct xfrm_replay_state_esn *p, *pp, *up;
391
392 if (!rta)
393 return 0;
394
395 up = nla_data(rta);
396
397 p = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
398 if (!p)
399 return -ENOMEM;
400
401 pp = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
402 if (!pp) {
403 kfree(p);
404 return -ENOMEM;
405 }
406
407 *replay_esn = p;
408 *preplay_esn = pp;
409
410 return 0;
411}
412
Joy Latten661697f2007-04-13 16:14:35 -0700413static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800414{
Trent Jaegerdf718372005-12-13 23:12:27 -0800415 int len = 0;
416
417 if (xfrm_ctx) {
418 len += sizeof(struct xfrm_user_sec_ctx);
419 len += xfrm_ctx->ctx_len;
420 }
421 return len;
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
425{
426 memcpy(&x->id, &p->id, sizeof(x->id));
427 memcpy(&x->sel, &p->sel, sizeof(x->sel));
428 memcpy(&x->lft, &p->lft, sizeof(x->lft));
429 x->props.mode = p->mode;
430 x->props.replay_window = p->replay_window;
431 x->props.reqid = p->reqid;
432 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700433 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700435
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700436 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700437 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800440/*
441 * someday when pfkey also has support, we could have the code
442 * somehow made shareable and move it to xfrm_state.c - JHS
443 *
444*/
Thomas Graf5424f322007-08-22 14:01:33 -0700445static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800446{
Thomas Graf5424f322007-08-22 14:01:33 -0700447 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +0000448 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -0700449 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
450 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
451 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800452
Steffen Klassertd8647b72011-03-08 00:10:27 +0000453 if (re) {
454 struct xfrm_replay_state_esn *replay_esn;
455 replay_esn = nla_data(re);
456 memcpy(x->replay_esn, replay_esn,
457 xfrm_replay_state_esn_len(replay_esn));
458 memcpy(x->preplay_esn, replay_esn,
459 xfrm_replay_state_esn_len(replay_esn));
460 }
461
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800462 if (rp) {
463 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700464 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800465 memcpy(&x->replay, replay, sizeof(*replay));
466 memcpy(&x->preplay, replay, sizeof(*replay));
467 }
468
469 if (lt) {
470 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700471 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800472 x->curlft.bytes = ltime->bytes;
473 x->curlft.packets = ltime->packets;
474 x->curlft.add_time = ltime->add_time;
475 x->curlft.use_time = ltime->use_time;
476 }
477
Thomas Grafcf5cb792007-08-22 13:59:04 -0700478 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700479 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800480
Thomas Grafcf5cb792007-08-22 13:59:04 -0700481 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700482 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800483}
484
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800485static struct xfrm_state *xfrm_state_construct(struct net *net,
486 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700487 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int *errp)
489{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800490 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int err = -ENOMEM;
492
493 if (!x)
494 goto error_no_put;
495
496 copy_from_user_state(x, p);
497
Herbert Xu1a6509d2008-01-28 19:37:29 -0800498 if ((err = attach_aead(&x->aead, &x->props.ealgo,
499 attrs[XFRMA_ALG_AEAD])))
500 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000501 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
502 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000504 if (!x->props.aalgo) {
505 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
506 attrs[XFRMA_ALG_AUTH])))
507 goto error;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
510 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700511 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto error;
513 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
514 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700515 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700517
518 if (attrs[XFRMA_ENCAP]) {
519 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
520 sizeof(*x->encap), GFP_KERNEL);
521 if (x->encap == NULL)
522 goto error;
523 }
524
Martin Willi35d28562010-12-08 04:37:49 +0000525 if (attrs[XFRMA_TFCPAD])
526 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
527
Thomas Graffd211502007-09-06 03:28:08 -0700528 if (attrs[XFRMA_COADDR]) {
529 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
530 sizeof(*x->coaddr), GFP_KERNEL);
531 if (x->coaddr == NULL)
532 goto error;
533 }
534
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000535 xfrm_mark_get(attrs, &x->mark);
536
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700537 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (err)
539 goto error;
540
Thomas Graffd211502007-09-06 03:28:08 -0700541 if (attrs[XFRMA_SEC_CTX] &&
542 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800543 goto error;
544
Steffen Klassertd8647b72011-03-08 00:10:27 +0000545 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
546 attrs[XFRMA_REPLAY_ESN_VAL])))
547 goto error;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800550 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800551 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800552 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800553
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000554 if ((err = xfrm_init_replay(x)))
555 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800556
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000557 /* override default values from above */
Thomas Graf5424f322007-08-22 14:01:33 -0700558 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return x;
561
562error:
563 x->km.state = XFRM_STATE_DEAD;
564 xfrm_state_put(x);
565error_no_put:
566 *errp = err;
567 return NULL;
568}
569
Christoph Hellwig22e70052007-01-02 15:22:30 -0800570static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700571 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800573 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700574 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct xfrm_state *x;
576 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700577 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800578 uid_t loginuid = audit_get_loginuid(current);
579 u32 sessionid = audit_get_sessionid(current);
580 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Thomas Graf35a7aa02007-08-22 14:00:40 -0700582 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (err)
584 return err;
585
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800586 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!x)
588 return err;
589
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700590 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
592 err = xfrm_state_add(x);
593 else
594 err = xfrm_state_update(x);
595
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800596 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400597 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (err < 0) {
600 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800601 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700605 c.seq = nlh->nlmsg_seq;
606 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700607 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700608
609 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700610out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700611 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return err;
613}
614
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800615static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
616 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700617 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700618 int *errp)
619{
620 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000621 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700622 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000623 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700624
625 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
626 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000627 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700628 } else {
629 xfrm_address_t *saddr = NULL;
630
Thomas Graf35a7aa02007-08-22 14:00:40 -0700631 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700632 if (!saddr) {
633 err = -EINVAL;
634 goto out;
635 }
636
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800637 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000638 x = xfrm_state_lookup_byaddr(net, mark,
639 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800640 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700641 }
642
643 out:
644 if (!x && errp)
645 *errp = err;
646 return x;
647}
648
Christoph Hellwig22e70052007-01-02 15:22:30 -0800649static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700650 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800652 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700654 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700655 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700656 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800657 uid_t loginuid = audit_get_loginuid(current);
658 u32 sessionid = audit_get_sessionid(current);
659 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800661 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
David S. Miller6f68dc32006-06-08 23:58:52 -0700665 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700666 goto out;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700669 err = -EPERM;
670 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700673 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600674
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700675 if (err < 0)
676 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700677
678 c.seq = nlh->nlmsg_seq;
679 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700680 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700681 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700683out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800684 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400685 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700686 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700687 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
691{
Mathias Kraused5f1f7c2012-09-19 11:33:39 +0000692 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 memcpy(&p->id, &x->id, sizeof(p->id));
694 memcpy(&p->sel, &x->sel, sizeof(p->sel));
695 memcpy(&p->lft, &x->lft, sizeof(p->lft));
696 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
697 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700698 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 p->mode = x->props.mode;
700 p->replay_window = x->props.replay_window;
701 p->reqid = x->props.reqid;
702 p->family = x->props.family;
703 p->flags = x->props.flags;
704 p->seq = x->km.seq;
705}
706
707struct xfrm_dump_info {
708 struct sk_buff *in_skb;
709 struct sk_buff *out_skb;
710 u32 nlmsg_seq;
711 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712};
713
Thomas Grafc0144be2007-08-22 13:55:43 -0700714static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
715{
Thomas Grafc0144be2007-08-22 13:55:43 -0700716 struct xfrm_user_sec_ctx *uctx;
717 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700718 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700719
720 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
721 if (attr == NULL)
722 return -EMSGSIZE;
723
724 uctx = nla_data(attr);
725 uctx->exttype = XFRMA_SEC_CTX;
726 uctx->len = ctx_size;
727 uctx->ctx_doi = s->ctx_doi;
728 uctx->ctx_alg = s->ctx_alg;
729 uctx->ctx_len = s->ctx_len;
730 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
731
732 return 0;
733}
734
Martin Willi4447bb32009-11-25 00:29:52 +0000735static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
736{
737 struct xfrm_algo *algo;
738 struct nlattr *nla;
739
740 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
741 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
742 if (!nla)
743 return -EMSGSIZE;
744
745 algo = nla_data(nla);
Mathias Krause37d61a22012-09-19 11:33:38 +0000746 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000747 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
748 algo->alg_key_len = auth->alg_key_len;
749
750 return 0;
751}
752
Herbert Xu68325d32007-10-09 13:30:57 -0700753/* Don't change this without updating xfrm_sa_len! */
754static int copy_to_user_state_extra(struct xfrm_state *x,
755 struct xfrm_usersa_info *p,
756 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 copy_to_user_state(x, p);
759
Herbert Xu050f0092007-10-09 13:31:47 -0700760 if (x->coaddr)
761 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
762
763 if (x->lastused)
764 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700765
Herbert Xu1a6509d2008-01-28 19:37:29 -0800766 if (x->aead)
767 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000768 if (x->aalg) {
769 if (copy_to_user_auth(x->aalg, skb))
770 goto nla_put_failure;
771
772 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
773 xfrm_alg_auth_len(x->aalg), x->aalg);
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800776 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700778 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700781 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Martin Willi35d28562010-12-08 04:37:49 +0000783 if (x->tfcpad)
784 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
785
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000786 if (xfrm_mark_put(skb, &x->mark))
787 goto nla_put_failure;
788
Steffen Klassertd8647b72011-03-08 00:10:27 +0000789 if (x->replay_esn)
790 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
791 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
792
Thomas Grafc0144be2007-08-22 13:55:43 -0700793 if (x->security && copy_sec_ctx(x->security, skb) < 0)
794 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700795
Herbert Xu68325d32007-10-09 13:30:57 -0700796 return 0;
797
798nla_put_failure:
799 return -EMSGSIZE;
800}
801
802static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
803{
804 struct xfrm_dump_info *sp = ptr;
805 struct sk_buff *in_skb = sp->in_skb;
806 struct sk_buff *skb = sp->out_skb;
807 struct xfrm_usersa_info *p;
808 struct nlmsghdr *nlh;
809 int err;
810
Herbert Xu68325d32007-10-09 13:30:57 -0700811 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
812 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
813 if (nlh == NULL)
814 return -EMSGSIZE;
815
816 p = nlmsg_data(nlh);
817
818 err = copy_to_user_state_extra(x, p, skb);
819 if (err)
820 goto nla_put_failure;
821
Thomas Graf98250692007-08-22 12:47:26 -0700822 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824
Thomas Grafc0144be2007-08-22 13:55:43 -0700825nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700826 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700827 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Timo Teras4c563f72008-02-28 21:31:08 -0800830static int xfrm_dump_sa_done(struct netlink_callback *cb)
831{
832 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
833 xfrm_state_walk_done(walk);
834 return 0;
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
838{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800839 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800840 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct xfrm_dump_info info;
842
Timo Teras4c563f72008-02-28 21:31:08 -0800843 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
844 sizeof(cb->args) - sizeof(cb->args[0]));
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 info.in_skb = cb->skb;
847 info.out_skb = skb;
848 info.nlmsg_seq = cb->nlh->nlmsg_seq;
849 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800850
851 if (!cb->args[0]) {
852 cb->args[0] = 1;
853 xfrm_state_walk_init(walk, 0);
854 }
855
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800856 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 return skb->len;
859}
860
861static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
862 struct xfrm_state *x, u32 seq)
863{
864 struct xfrm_dump_info info;
865 struct sk_buff *skb;
Mathias Krause555144b2012-09-13 11:41:26 +0000866 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Thomas Graf7deb2262007-08-22 13:57:39 -0700868 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!skb)
870 return ERR_PTR(-ENOMEM);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 info.in_skb = in_skb;
873 info.out_skb = skb;
874 info.nlmsg_seq = seq;
875 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Mathias Krause555144b2012-09-13 11:41:26 +0000877 err = dump_one_state(x, 0, &info);
878 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 kfree_skb(skb);
Mathias Krause555144b2012-09-13 11:41:26 +0000880 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
883 return skb;
884}
885
Thomas Graf7deb2262007-08-22 13:57:39 -0700886static inline size_t xfrm_spdinfo_msgsize(void)
887{
888 return NLMSG_ALIGN(4)
889 + nla_total_size(sizeof(struct xfrmu_spdinfo))
890 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
891}
892
Alexey Dobriyane0710412010-01-23 13:37:10 +0000893static int build_spdinfo(struct sk_buff *skb, struct net *net,
894 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700895{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700896 struct xfrmk_spdinfo si;
897 struct xfrmu_spdinfo spc;
898 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700899 struct nlmsghdr *nlh;
900 u32 *f;
901
902 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300903 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700904 return -EMSGSIZE;
905
906 f = nlmsg_data(nlh);
907 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000908 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700909 spc.incnt = si.incnt;
910 spc.outcnt = si.outcnt;
911 spc.fwdcnt = si.fwdcnt;
912 spc.inscnt = si.inscnt;
913 spc.outscnt = si.outscnt;
914 spc.fwdscnt = si.fwdscnt;
915 sph.spdhcnt = si.spdhcnt;
916 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700917
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700918 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
919 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700920
921 return nlmsg_end(skb, nlh);
922
923nla_put_failure:
924 nlmsg_cancel(skb, nlh);
925 return -EMSGSIZE;
926}
927
928static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700929 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700930{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800931 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700932 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700933 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700934 u32 spid = NETLINK_CB(skb).pid;
935 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700936
Thomas Graf7deb2262007-08-22 13:57:39 -0700937 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700938 if (r_skb == NULL)
939 return -ENOMEM;
940
Alexey Dobriyane0710412010-01-23 13:37:10 +0000941 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700942 BUG();
943
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800944 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700945}
946
Thomas Graf7deb2262007-08-22 13:57:39 -0700947static inline size_t xfrm_sadinfo_msgsize(void)
948{
949 return NLMSG_ALIGN(4)
950 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
951 + nla_total_size(4); /* XFRMA_SAD_CNT */
952}
953
Alexey Dobriyane0710412010-01-23 13:37:10 +0000954static int build_sadinfo(struct sk_buff *skb, struct net *net,
955 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700956{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700957 struct xfrmk_sadinfo si;
958 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700959 struct nlmsghdr *nlh;
960 u32 *f;
961
962 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300963 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700964 return -EMSGSIZE;
965
966 f = nlmsg_data(nlh);
967 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000968 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700969
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700970 sh.sadhmcnt = si.sadhmcnt;
971 sh.sadhcnt = si.sadhcnt;
972
973 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
974 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700975
976 return nlmsg_end(skb, nlh);
977
978nla_put_failure:
979 nlmsg_cancel(skb, nlh);
980 return -EMSGSIZE;
981}
982
983static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700984 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700985{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800986 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700987 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700988 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700989 u32 spid = NETLINK_CB(skb).pid;
990 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700991
Thomas Graf7deb2262007-08-22 13:57:39 -0700992 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700993 if (r_skb == NULL)
994 return -ENOMEM;
995
Alexey Dobriyane0710412010-01-23 13:37:10 +0000996 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700997 BUG();
998
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800999 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001000}
1001
Christoph Hellwig22e70052007-01-02 15:22:30 -08001002static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001003 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001005 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001006 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct xfrm_state *x;
1008 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001009 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001011 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (x == NULL)
1013 goto out_noput;
1014
1015 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1016 if (IS_ERR(resp_skb)) {
1017 err = PTR_ERR(resp_skb);
1018 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001019 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 xfrm_state_put(x);
1022out_noput:
1023 return err;
1024}
1025
1026static int verify_userspi_info(struct xfrm_userspi_info *p)
1027{
1028 switch (p->info.id.proto) {
1029 case IPPROTO_AH:
1030 case IPPROTO_ESP:
1031 break;
1032
1033 case IPPROTO_COMP:
1034 /* IPCOMP spi is 16-bits. */
1035 if (p->max >= 0x10000)
1036 return -EINVAL;
1037 break;
1038
1039 default:
1040 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (p->min > p->max)
1044 return -EINVAL;
1045
1046 return 0;
1047}
1048
Christoph Hellwig22e70052007-01-02 15:22:30 -08001049static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001050 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001052 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct xfrm_state *x;
1054 struct xfrm_userspi_info *p;
1055 struct sk_buff *resp_skb;
1056 xfrm_address_t *daddr;
1057 int family;
1058 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001059 u32 mark;
1060 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Thomas Graf7b67c852007-08-22 13:53:52 -07001062 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 err = verify_userspi_info(p);
1064 if (err)
1065 goto out_noput;
1066
1067 family = p->info.family;
1068 daddr = &p->info.id.daddr;
1069
1070 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001071
1072 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001074 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1076 xfrm_state_put(x);
1077 x = NULL;
1078 }
1079 }
1080
1081 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001082 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 p->info.id.proto, daddr,
1084 &p->info.saddr, 1,
1085 family);
1086 err = -ENOENT;
1087 if (x == NULL)
1088 goto out_noput;
1089
Herbert Xu658b2192007-10-09 13:29:52 -07001090 err = xfrm_alloc_spi(x, p->min, p->max);
1091 if (err)
1092 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Herbert Xu658b2192007-10-09 13:29:52 -07001094 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (IS_ERR(resp_skb)) {
1096 err = PTR_ERR(resp_skb);
1097 goto out;
1098 }
1099
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001100 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102out:
1103 xfrm_state_put(x);
1104out_noput:
1105 return err;
1106}
1107
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001108static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 switch (dir) {
1111 case XFRM_POLICY_IN:
1112 case XFRM_POLICY_OUT:
1113 case XFRM_POLICY_FWD:
1114 break;
1115
1116 default:
1117 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 return 0;
1121}
1122
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001123static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001124{
1125 switch (type) {
1126 case XFRM_POLICY_TYPE_MAIN:
1127#ifdef CONFIG_XFRM_SUB_POLICY
1128 case XFRM_POLICY_TYPE_SUB:
1129#endif
1130 break;
1131
1132 default:
1133 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001134 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001135
1136 return 0;
1137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1140{
1141 switch (p->share) {
1142 case XFRM_SHARE_ANY:
1143 case XFRM_SHARE_SESSION:
1144 case XFRM_SHARE_USER:
1145 case XFRM_SHARE_UNIQUE:
1146 break;
1147
1148 default:
1149 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 switch (p->action) {
1153 case XFRM_POLICY_ALLOW:
1154 case XFRM_POLICY_BLOCK:
1155 break;
1156
1157 default:
1158 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 switch (p->sel.family) {
1162 case AF_INET:
1163 break;
1164
1165 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001166#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168#else
1169 return -EAFNOSUPPORT;
1170#endif
1171
1172 default:
1173 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 return verify_policy_dir(p->dir);
1177}
1178
Thomas Graf5424f322007-08-22 14:01:33 -07001179static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001180{
Thomas Graf5424f322007-08-22 14:01:33 -07001181 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001182 struct xfrm_user_sec_ctx *uctx;
1183
1184 if (!rt)
1185 return 0;
1186
Thomas Graf5424f322007-08-22 14:01:33 -07001187 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001188 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1192 int nr)
1193{
1194 int i;
1195
1196 xp->xfrm_nr = nr;
1197 for (i = 0; i < nr; i++, ut++) {
1198 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1199
1200 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1201 memcpy(&t->saddr, &ut->saddr,
1202 sizeof(xfrm_address_t));
1203 t->reqid = ut->reqid;
1204 t->mode = ut->mode;
1205 t->share = ut->share;
1206 t->optional = ut->optional;
1207 t->aalgos = ut->aalgos;
1208 t->ealgos = ut->ealgos;
1209 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001210 /* If all masks are ~0, then we allow all algorithms. */
1211 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001212 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
1215
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001216static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1217{
1218 int i;
1219
1220 if (nr > XFRM_MAX_DEPTH)
1221 return -EINVAL;
1222
1223 for (i = 0; i < nr; i++) {
1224 /* We never validated the ut->family value, so many
1225 * applications simply leave it at zero. The check was
1226 * never made and ut->family was ignored because all
1227 * templates could be assumed to have the same family as
1228 * the policy itself. Now that we will have ipv4-in-ipv6
1229 * and ipv6-in-ipv4 tunnels, this is no longer true.
1230 */
1231 if (!ut[i].family)
1232 ut[i].family = family;
1233
1234 switch (ut[i].family) {
1235 case AF_INET:
1236 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001237#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001238 case AF_INET6:
1239 break;
1240#endif
1241 default:
1242 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001243 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001244 }
1245
1246 return 0;
1247}
1248
Thomas Graf5424f322007-08-22 14:01:33 -07001249static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Thomas Graf5424f322007-08-22 14:01:33 -07001251 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (!rt) {
1254 pol->xfrm_nr = 0;
1255 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001256 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1257 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001260 err = validate_tmpl(nr, utmpl, pol->family);
1261 if (err)
1262 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Thomas Graf5424f322007-08-22 14:01:33 -07001264 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 return 0;
1267}
1268
Thomas Graf5424f322007-08-22 14:01:33 -07001269static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001270{
Thomas Graf5424f322007-08-22 14:01:33 -07001271 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001272 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001273 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001274 int err;
1275
1276 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001277 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001278 type = upt->type;
1279 }
1280
1281 err = verify_policy_type(type);
1282 if (err)
1283 return err;
1284
1285 *tp = type;
1286 return 0;
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1290{
1291 xp->priority = p->priority;
1292 xp->index = p->index;
1293 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1294 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1295 xp->action = p->action;
1296 xp->flags = p->flags;
1297 xp->family = p->sel.family;
1298 /* XXX xp->share = p->share; */
1299}
1300
1301static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1302{
Mathias Krause97f96ea2012-09-19 11:33:40 +00001303 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1305 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1306 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1307 p->priority = xp->priority;
1308 p->index = xp->index;
1309 p->sel.family = xp->family;
1310 p->dir = dir;
1311 p->action = xp->action;
1312 p->flags = xp->flags;
1313 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1314}
1315
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001316static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001318 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int err;
1320
1321 if (!xp) {
1322 *errp = -ENOMEM;
1323 return NULL;
1324 }
1325
1326 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001327
Thomas Graf35a7aa02007-08-22 14:00:40 -07001328 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001329 if (err)
1330 goto error;
1331
Thomas Graf35a7aa02007-08-22 14:00:40 -07001332 if (!(err = copy_from_user_tmpl(xp, attrs)))
1333 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001334 if (err)
1335 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001337 xfrm_mark_get(attrs, &xp->mark);
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001340 error:
1341 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001342 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001343 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001344 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Christoph Hellwig22e70052007-01-02 15:22:30 -08001347static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001348 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001350 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001351 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001353 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int err;
1355 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001356 uid_t loginuid = audit_get_loginuid(current);
1357 u32 sessionid = audit_get_sessionid(current);
1358 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 err = verify_newpolicy_info(p);
1361 if (err)
1362 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001363 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001364 if (err)
1365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001367 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (!xp)
1369 return err;
1370
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001371 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001372 * Aha! this is anti-netlink really i.e more pfkey derived
1373 * in netlink excl is a flag and you wouldnt need
1374 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1376 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001377 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001378 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001381 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 kfree(xp);
1383 return err;
1384 }
1385
Herbert Xuf60f6b82005-06-18 22:44:37 -07001386 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001387 c.seq = nlh->nlmsg_seq;
1388 c.pid = nlh->nlmsg_pid;
1389 km_policy_notify(xp, p->dir, &c);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 xfrm_pol_put(xp);
1392
1393 return 0;
1394}
1395
1396static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1397{
1398 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1399 int i;
1400
1401 if (xp->xfrm_nr == 0)
1402 return 0;
1403
1404 for (i = 0; i < xp->xfrm_nr; i++) {
1405 struct xfrm_user_tmpl *up = &vec[i];
1406 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1407
Mathias Krause0c5e3752012-09-19 11:33:41 +00001408 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001410 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1412 up->reqid = kp->reqid;
1413 up->mode = kp->mode;
1414 up->share = kp->share;
1415 up->optional = kp->optional;
1416 up->aalgos = kp->aalgos;
1417 up->ealgos = kp->ealgos;
1418 up->calgos = kp->calgos;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Thomas Grafc0144be2007-08-22 13:55:43 -07001421 return nla_put(skb, XFRMA_TMPL,
1422 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001423}
1424
Serge Hallyn0d681622006-07-24 23:30:44 -07001425static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1426{
1427 if (x->security) {
1428 return copy_sec_ctx(x->security, skb);
1429 }
1430 return 0;
1431}
1432
1433static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1434{
1435 if (xp->security) {
1436 return copy_sec_ctx(xp->security, skb);
1437 }
1438 return 0;
1439}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001440static inline size_t userpolicy_type_attrsize(void)
1441{
1442#ifdef CONFIG_XFRM_SUB_POLICY
1443 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1444#else
1445 return 0;
1446#endif
1447}
Serge Hallyn0d681622006-07-24 23:30:44 -07001448
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001449#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001450static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001451{
Thomas Grafc0144be2007-08-22 13:55:43 -07001452 struct xfrm_userpolicy_type upt = {
1453 .type = type,
1454 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001455
Thomas Grafc0144be2007-08-22 13:55:43 -07001456 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001457}
1458
1459#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001460static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001461{
1462 return 0;
1463}
1464#endif
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1467{
1468 struct xfrm_dump_info *sp = ptr;
1469 struct xfrm_userpolicy_info *p;
1470 struct sk_buff *in_skb = sp->in_skb;
1471 struct sk_buff *skb = sp->out_skb;
1472 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001474 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1475 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1476 if (nlh == NULL)
1477 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Thomas Graf7b67c852007-08-22 13:53:52 -07001479 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 copy_to_user_policy(xp, p, dir);
1481 if (copy_to_user_tmpl(xp, skb) < 0)
1482 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001483 if (copy_to_user_sec_ctx(xp, skb))
1484 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001485 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001486 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001487 if (xfrm_mark_put(skb, &xp->mark))
1488 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Thomas Graf98250692007-08-22 12:47:26 -07001490 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return 0;
1492
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001493nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001495 nlmsg_cancel(skb, nlh);
1496 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Timo Teras4c563f72008-02-28 21:31:08 -08001499static int xfrm_dump_policy_done(struct netlink_callback *cb)
1500{
1501 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1502
1503 xfrm_policy_walk_done(walk);
1504 return 0;
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1508{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001509 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001510 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 struct xfrm_dump_info info;
1512
Timo Teras4c563f72008-02-28 21:31:08 -08001513 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1514 sizeof(cb->args) - sizeof(cb->args[0]));
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 info.in_skb = cb->skb;
1517 info.out_skb = skb;
1518 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1519 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001520
1521 if (!cb->args[0]) {
1522 cb->args[0] = 1;
1523 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1524 }
1525
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001526 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 return skb->len;
1529}
1530
1531static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1532 struct xfrm_policy *xp,
1533 int dir, u32 seq)
1534{
1535 struct xfrm_dump_info info;
1536 struct sk_buff *skb;
Mathias Krausef38b3342012-09-14 09:58:32 +00001537 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Thomas Graf7deb2262007-08-22 13:57:39 -07001539 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (!skb)
1541 return ERR_PTR(-ENOMEM);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 info.in_skb = in_skb;
1544 info.out_skb = skb;
1545 info.nlmsg_seq = seq;
1546 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Mathias Krausef38b3342012-09-14 09:58:32 +00001548 err = dump_one_policy(xp, dir, 0, &info);
1549 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 kfree_skb(skb);
Mathias Krausef38b3342012-09-14 09:58:32 +00001551 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553
1554 return skb;
1555}
1556
Christoph Hellwig22e70052007-01-02 15:22:30 -08001557static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001558 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001560 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 struct xfrm_policy *xp;
1562 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001563 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001565 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001567 struct xfrm_mark m;
1568 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Thomas Graf7b67c852007-08-22 13:53:52 -07001570 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1572
Thomas Graf35a7aa02007-08-22 14:00:40 -07001573 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001574 if (err)
1575 return err;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 err = verify_policy_dir(p->dir);
1578 if (err)
1579 return err;
1580
1581 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001582 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001583 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001584 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001585 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001586
Thomas Graf35a7aa02007-08-22 14:00:40 -07001587 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001588 if (err)
1589 return err;
1590
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001591 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001592 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001593 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001594
Paul Moore03e1ad72008-04-12 19:07:52 -07001595 err = security_xfrm_policy_alloc(&ctx, uctx);
1596 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001597 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001598 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001599 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001600 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001601 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (xp == NULL)
1604 return -ENOENT;
1605
1606 if (!delete) {
1607 struct sk_buff *resp_skb;
1608
1609 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1610 if (IS_ERR(resp_skb)) {
1611 err = PTR_ERR(resp_skb);
1612 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001613 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001614 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001616 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001617 uid_t loginuid = audit_get_loginuid(current);
1618 u32 sessionid = audit_get_sessionid(current);
1619 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001620
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001621 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001622 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1623 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001624
1625 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001626 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001627
Herbert Xue7443892005-06-18 22:44:18 -07001628 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001629 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001630 c.seq = nlh->nlmsg_seq;
1631 c.pid = nlh->nlmsg_pid;
1632 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001635out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001636 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return err;
1638}
1639
Christoph Hellwig22e70052007-01-02 15:22:30 -08001640static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001641 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001643 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001644 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001645 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001646 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001647 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001649 audit_info.loginuid = audit_get_loginuid(current);
1650 audit_info.sessionid = audit_get_sessionid(current);
1651 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001652 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001653 if (err) {
1654 if (err == -ESRCH) /* empty table */
1655 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001656 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001657 }
Herbert Xubf088672005-06-18 22:44:00 -07001658 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001659 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001660 c.seq = nlh->nlmsg_seq;
1661 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001662 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001663 km_state_notify(NULL, &c);
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return 0;
1666}
1667
Steffen Klassertd8647b72011-03-08 00:10:27 +00001668static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001669{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001670 size_t replay_size = x->replay_esn ?
1671 xfrm_replay_state_esn_len(x->replay_esn) :
1672 sizeof(struct xfrm_replay_state);
1673
Thomas Graf7deb2262007-08-22 13:57:39 -07001674 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001675 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001676 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001677 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001678 + nla_total_size(4) /* XFRM_AE_RTHR */
1679 + nla_total_size(4); /* XFRM_AE_ETHR */
1680}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001681
David S. Miller214e0052011-02-24 00:02:38 -05001682static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001683{
1684 struct xfrm_aevent_id *id;
1685 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001686
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001687 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1688 if (nlh == NULL)
1689 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001690
Thomas Graf7b67c852007-08-22 13:53:52 -07001691 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001692 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001693 id->sa_id.spi = x->id.spi;
1694 id->sa_id.family = x->props.family;
1695 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001696 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1697 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001698 id->flags = c->data.aevent;
1699
Steffen Klassertd8647b72011-03-08 00:10:27 +00001700 if (x->replay_esn)
1701 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1702 xfrm_replay_state_esn_len(x->replay_esn),
1703 x->replay_esn);
1704 else
1705 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1706
Thomas Grafc0144be2007-08-22 13:55:43 -07001707 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001708
Thomas Grafc0144be2007-08-22 13:55:43 -07001709 if (id->flags & XFRM_AE_RTHR)
1710 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001711
Thomas Grafc0144be2007-08-22 13:55:43 -07001712 if (id->flags & XFRM_AE_ETHR)
1713 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1714 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001715
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001716 if (xfrm_mark_put(skb, &x->mark))
1717 goto nla_put_failure;
1718
Thomas Graf98250692007-08-22 12:47:26 -07001719 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001720
Thomas Grafc0144be2007-08-22 13:55:43 -07001721nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001722 nlmsg_cancel(skb, nlh);
1723 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001724}
1725
Christoph Hellwig22e70052007-01-02 15:22:30 -08001726static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001727 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001728{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001729 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001730 struct xfrm_state *x;
1731 struct sk_buff *r_skb;
1732 int err;
1733 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001734 u32 mark;
1735 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001736 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001737 struct xfrm_usersa_id *id = &p->sa_id;
1738
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001739 mark = xfrm_mark_get(attrs, &m);
1740
1741 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001742 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001743 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001744
1745 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1746 if (r_skb == NULL) {
1747 xfrm_state_put(x);
1748 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001749 }
1750
1751 /*
1752 * XXX: is this lock really needed - none of the other
1753 * gets lock (the concern is things getting updated
1754 * while we are still reading) - jhs
1755 */
1756 spin_lock_bh(&x->lock);
1757 c.data.aevent = p->flags;
1758 c.seq = nlh->nlmsg_seq;
1759 c.pid = nlh->nlmsg_pid;
1760
1761 if (build_aevent(r_skb, x, &c) < 0)
1762 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001763 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001764 spin_unlock_bh(&x->lock);
1765 xfrm_state_put(x);
1766 return err;
1767}
1768
Christoph Hellwig22e70052007-01-02 15:22:30 -08001769static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001770 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001771{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001772 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001773 struct xfrm_state *x;
1774 struct km_event c;
1775 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001776 u32 mark = 0;
1777 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001778 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001779 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001780 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001781 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001782
Steffen Klassertd8647b72011-03-08 00:10:27 +00001783 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001784 return err;
1785
1786 /* pedantic mode - thou shalt sayeth replaceth */
1787 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1788 return err;
1789
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001790 mark = xfrm_mark_get(attrs, &m);
1791
1792 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 -08001793 if (x == NULL)
1794 return -ESRCH;
1795
1796 if (x->km.state != XFRM_STATE_VALID)
1797 goto out;
1798
Steffen Klasserte2b19122011-03-28 19:47:30 +00001799 err = xfrm_replay_verify_len(x->replay_esn, rp);
1800 if (err)
1801 goto out;
1802
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001803 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001804 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001805 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806
1807 c.event = nlh->nlmsg_type;
1808 c.seq = nlh->nlmsg_seq;
1809 c.pid = nlh->nlmsg_pid;
1810 c.data.aevent = XFRM_AE_CU;
1811 km_state_notify(x, &c);
1812 err = 0;
1813out:
1814 xfrm_state_put(x);
1815 return err;
1816}
1817
Christoph Hellwig22e70052007-01-02 15:22:30 -08001818static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001819 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001821 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001822 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001823 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001824 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001825 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001826
Thomas Graf35a7aa02007-08-22 14:00:40 -07001827 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001828 if (err)
1829 return err;
1830
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001831 audit_info.loginuid = audit_get_loginuid(current);
1832 audit_info.sessionid = audit_get_sessionid(current);
1833 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001834 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001835 if (err) {
1836 if (err == -ESRCH) /* empty table */
1837 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001838 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001839 }
1840
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001841 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001842 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001843 c.seq = nlh->nlmsg_seq;
1844 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001845 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001846 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return 0;
1848}
1849
Christoph Hellwig22e70052007-01-02 15:22:30 -08001850static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001851 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001852{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001853 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001854 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001855 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001856 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001857 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001858 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001859 struct xfrm_mark m;
1860 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001861
Thomas Graf35a7aa02007-08-22 14:00:40 -07001862 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001863 if (err)
1864 return err;
1865
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001866 err = verify_policy_dir(p->dir);
1867 if (err)
1868 return err;
1869
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001870 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001871 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001872 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001873 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001874 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001875
Thomas Graf35a7aa02007-08-22 14:00:40 -07001876 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001877 if (err)
1878 return err;
1879
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001880 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001881 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001882 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001883
Paul Moore03e1ad72008-04-12 19:07:52 -07001884 err = security_xfrm_policy_alloc(&ctx, uctx);
1885 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001886 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001887 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001888 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001889 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001890 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001891 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001892 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001893 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001894
Timo Teräsea2dea92010-03-31 00:17:05 +00001895 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001896 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001897
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001898 err = 0;
1899 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001900 uid_t loginuid = audit_get_loginuid(current);
1901 u32 sessionid = audit_get_sessionid(current);
1902 u32 sid;
1903
1904 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001905 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001906 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001907
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001908 } else {
1909 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001910 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001911 }
1912 km_policy_expired(xp, p->dir, up->hard, current->pid);
1913
1914out:
1915 xfrm_pol_put(xp);
1916 return err;
1917}
1918
Christoph Hellwig22e70052007-01-02 15:22:30 -08001919static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001920 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001921{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001922 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001923 struct xfrm_state *x;
1924 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001925 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001926 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001927 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001928 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001929
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001930 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 -08001931
David S. Miller3a765aa2007-02-26 14:52:21 -08001932 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001933 if (x == NULL)
1934 return err;
1935
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001936 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001937 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001938 if (x->km.state != XFRM_STATE_VALID)
1939 goto out;
1940 km_state_expired(x, ue->hard, current->pid);
1941
Joy Latten161a09e2006-11-27 13:11:54 -06001942 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001943 uid_t loginuid = audit_get_loginuid(current);
1944 u32 sessionid = audit_get_sessionid(current);
1945 u32 sid;
1946
1947 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001948 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001949 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001950 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001951 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001952out:
1953 spin_unlock_bh(&x->lock);
1954 xfrm_state_put(x);
1955 return err;
1956}
1957
Christoph Hellwig22e70052007-01-02 15:22:30 -08001958static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001959 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001960{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001961 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001962 struct xfrm_policy *xp;
1963 struct xfrm_user_tmpl *ut;
1964 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001965 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001966 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001967
Thomas Graf7b67c852007-08-22 13:53:52 -07001968 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001969 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001970 int err = -ENOMEM;
1971
1972 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001973 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001974
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001975 xfrm_mark_get(attrs, &mark);
1976
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001977 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001978 if (err)
1979 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001980
1981 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001982 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001983 if (!xp)
1984 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001985
1986 memcpy(&x->id, &ua->id, sizeof(ua->id));
1987 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1988 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001989 xp->mark.m = x->mark.m = mark.m;
1990 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001991 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001992 /* extract the templates and for each call km_key */
1993 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1994 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1995 memcpy(&x->id, &t->id, sizeof(x->id));
1996 x->props.mode = t->mode;
1997 x->props.reqid = t->reqid;
1998 x->props.family = ut->family;
1999 t->aalgos = ua->aalgos;
2000 t->ealgos = ua->ealgos;
2001 t->calgos = ua->calgos;
2002 err = km_query(x, t, xp);
2003
2004 }
2005
2006 kfree(x);
2007 kfree(xp);
2008
2009 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002010
2011bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002012 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002013free_state:
2014 kfree(x);
2015nomem:
2016 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002017}
2018
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002019#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002020static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002021 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002022 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002023{
Thomas Graf5424f322007-08-22 14:01:33 -07002024 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002025 struct xfrm_user_migrate *um;
2026 int i, num_migrate;
2027
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002028 if (k != NULL) {
2029 struct xfrm_user_kmaddress *uk;
2030
2031 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2032 memcpy(&k->local, &uk->local, sizeof(k->local));
2033 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2034 k->family = uk->family;
2035 k->reserved = uk->reserved;
2036 }
2037
Thomas Graf5424f322007-08-22 14:01:33 -07002038 um = nla_data(rt);
2039 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002040
2041 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2042 return -EINVAL;
2043
2044 for (i = 0; i < num_migrate; i++, um++, ma++) {
2045 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2046 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2047 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2048 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2049
2050 ma->proto = um->proto;
2051 ma->mode = um->mode;
2052 ma->reqid = um->reqid;
2053
2054 ma->old_family = um->old_family;
2055 ma->new_family = um->new_family;
2056 }
2057
2058 *num = i;
2059 return 0;
2060}
2061
2062static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002063 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002064{
Thomas Graf7b67c852007-08-22 13:53:52 -07002065 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002066 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002067 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002068 u8 type;
2069 int err;
2070 int n = 0;
2071
Thomas Graf35a7aa02007-08-22 14:00:40 -07002072 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002073 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002074
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002075 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2076
Thomas Graf5424f322007-08-22 14:01:33 -07002077 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002078 if (err)
2079 return err;
2080
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002081 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002082 if (err)
2083 return err;
2084
2085 if (!n)
2086 return 0;
2087
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002088 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002089
2090 return 0;
2091}
2092#else
2093static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002094 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002095{
2096 return -ENOPROTOOPT;
2097}
2098#endif
2099
2100#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002101static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002102{
2103 struct xfrm_user_migrate um;
2104
2105 memset(&um, 0, sizeof(um));
2106 um.proto = m->proto;
2107 um.mode = m->mode;
2108 um.reqid = m->reqid;
2109 um.old_family = m->old_family;
2110 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2111 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2112 um.new_family = m->new_family;
2113 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2114 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2115
Thomas Grafc0144be2007-08-22 13:55:43 -07002116 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002117}
2118
David S. Miller183cad12011-02-24 00:28:01 -05002119static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002120{
2121 struct xfrm_user_kmaddress uk;
2122
2123 memset(&uk, 0, sizeof(uk));
2124 uk.family = k->family;
2125 uk.reserved = k->reserved;
2126 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002127 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002128
2129 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2130}
2131
2132static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002133{
2134 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002135 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2136 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2137 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002138}
2139
David S. Miller183cad12011-02-24 00:28:01 -05002140static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2141 int num_migrate, const struct xfrm_kmaddress *k,
2142 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002143{
David S. Miller183cad12011-02-24 00:28:01 -05002144 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002145 struct xfrm_userpolicy_id *pol_id;
2146 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002147 int i;
2148
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002149 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2150 if (nlh == NULL)
2151 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002152
Thomas Graf7b67c852007-08-22 13:53:52 -07002153 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002154 /* copy data from selector, dir, and type to the pol_id */
2155 memset(pol_id, 0, sizeof(*pol_id));
2156 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2157 pol_id->dir = dir;
2158
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002159 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2160 goto nlmsg_failure;
2161
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002162 if (copy_to_user_policy_type(type, skb) < 0)
2163 goto nlmsg_failure;
2164
2165 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2166 if (copy_to_user_migrate(mp, skb) < 0)
2167 goto nlmsg_failure;
2168 }
2169
Thomas Graf98250692007-08-22 12:47:26 -07002170 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002171nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002172 nlmsg_cancel(skb, nlh);
2173 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002174}
2175
David S. Miller183cad12011-02-24 00:28:01 -05002176static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2177 const struct xfrm_migrate *m, int num_migrate,
2178 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002179{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002180 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002181 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002182
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002183 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002184 if (skb == NULL)
2185 return -ENOMEM;
2186
2187 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002188 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002189 BUG();
2190
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002191 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002192}
2193#else
David S. Miller183cad12011-02-24 00:28:01 -05002194static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2195 const struct xfrm_migrate *m, int num_migrate,
2196 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002197{
2198 return -ENOPROTOOPT;
2199}
2200#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002201
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002202#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002203
2204static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002205 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002206 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2207 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2208 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2209 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2210 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2211 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002212 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002213 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002214 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002215 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002216 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002217 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002218 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002219 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2220 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002221 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002222 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002223 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2224 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225};
2226
Thomas Graf492b5582005-05-03 14:26:40 -07002227#undef XMSGSIZE
2228
Thomas Grafcf5cb792007-08-22 13:59:04 -07002229static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002230 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2231 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2232 [XFRMA_LASTUSED] = { .type = NLA_U64},
2233 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002234 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002235 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2236 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2237 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2238 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2239 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2240 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2241 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2242 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2243 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2244 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2245 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2246 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2247 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2248 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002249 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002250 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002251 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002252 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002253};
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002256 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002258 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002259} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2260 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2261 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2262 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002263 .dump = xfrm_dump_sa,
2264 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002265 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2266 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2267 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002268 .dump = xfrm_dump_policy,
2269 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002270 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002271 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002272 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002273 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2274 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002275 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002276 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2277 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002278 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2279 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002280 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002281 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002282 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283};
2284
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002285static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002287 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002288 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002290 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 type -= XFRM_MSG_BASE;
2297 link = &xfrm_dispatch[type];
2298
2299 /* All operations require privileges, even GET */
Eric Parisfd778462012-01-03 12:25:16 -05002300 if (!capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002301 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Thomas Graf492b5582005-05-03 14:26:40 -07002303 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2304 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002305 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002307 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002309 {
2310 struct netlink_dump_control c = {
2311 .dump = link->dump,
2312 .done = link->done,
2313 };
2314 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317
Thomas Graf35a7aa02007-08-22 14:00:40 -07002318 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002319 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002320 if (err < 0)
2321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002324 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Thomas Graf5424f322007-08-22 14:01:33 -07002326 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002329static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002331 mutex_lock(&xfrm_cfg_mutex);
2332 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2333 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
Thomas Graf7deb2262007-08-22 13:57:39 -07002336static inline size_t xfrm_expire_msgsize(void)
2337{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002338 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2339 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002340}
2341
David S. Miller214e0052011-02-24 00:02:38 -05002342static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 struct xfrm_user_expire *ue;
2345 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002347 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2348 if (nlh == NULL)
2349 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Thomas Graf7b67c852007-08-22 13:53:52 -07002351 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002353 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002355 if (xfrm_mark_put(skb, &x->mark))
2356 goto nla_put_failure;
2357
Thomas Graf98250692007-08-22 12:47:26 -07002358 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002359
2360nla_put_failure:
2361 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
David S. Miller214e0052011-02-24 00:02:38 -05002364static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002366 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct sk_buff *skb;
2368
Thomas Graf7deb2262007-08-22 13:57:39 -07002369 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (skb == NULL)
2371 return -ENOMEM;
2372
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002373 if (build_expire(skb, x, c) < 0) {
2374 kfree_skb(skb);
2375 return -EMSGSIZE;
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002378 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
David S. Miller214e0052011-02-24 00:02:38 -05002381static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002382{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002383 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002384 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002385
Steffen Klassertd8647b72011-03-08 00:10:27 +00002386 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002387 if (skb == NULL)
2388 return -ENOMEM;
2389
2390 if (build_aevent(skb, x, c) < 0)
2391 BUG();
2392
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002393 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002394}
2395
David S. Miller214e0052011-02-24 00:02:38 -05002396static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002398 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399 struct xfrm_usersa_flush *p;
2400 struct nlmsghdr *nlh;
2401 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002402 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403
Thomas Graf7deb2262007-08-22 13:57:39 -07002404 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002405 if (skb == NULL)
2406 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002407
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002408 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2409 if (nlh == NULL) {
2410 kfree_skb(skb);
2411 return -EMSGSIZE;
2412 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413
Thomas Graf7b67c852007-08-22 13:53:52 -07002414 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002415 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002416
Thomas Graf98250692007-08-22 12:47:26 -07002417 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002419 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002420}
2421
Thomas Graf7deb2262007-08-22 13:57:39 -07002422static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002423{
Thomas Graf7deb2262007-08-22 13:57:39 -07002424 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002425 if (x->aead)
2426 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002427 if (x->aalg) {
2428 l += nla_total_size(sizeof(struct xfrm_algo) +
2429 (x->aalg->alg_key_len + 7) / 8);
2430 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2431 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002432 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002433 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002434 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002435 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002436 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002437 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002438 if (x->tfcpad)
2439 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002440 if (x->replay_esn)
2441 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002442 if (x->security)
2443 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2444 x->security->ctx_len);
2445 if (x->coaddr)
2446 l += nla_total_size(sizeof(*x->coaddr));
2447
Herbert Xud26f3982007-11-13 21:47:08 -08002448 /* Must count x->lastused as it may become non-zero behind our back. */
2449 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002450
2451 return l;
2452}
2453
David S. Miller214e0052011-02-24 00:02:38 -05002454static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002455{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002456 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002457 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002458 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002459 struct nlmsghdr *nlh;
2460 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002461 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002462 int headlen;
2463
2464 headlen = sizeof(*p);
2465 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002466 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002467 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002468 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002469 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002470 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002471
Thomas Graf7deb2262007-08-22 13:57:39 -07002472 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002473 if (skb == NULL)
2474 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002475
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002476 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2477 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002478 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002479
Thomas Graf7b67c852007-08-22 13:53:52 -07002480 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002481 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002482 struct nlattr *attr;
2483
Thomas Graf7b67c852007-08-22 13:53:52 -07002484 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002485 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2486 id->spi = x->id.spi;
2487 id->family = x->props.family;
2488 id->proto = x->id.proto;
2489
Thomas Grafc0144be2007-08-22 13:55:43 -07002490 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2491 if (attr == NULL)
2492 goto nla_put_failure;
2493
2494 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002495 }
2496
Herbert Xu68325d32007-10-09 13:30:57 -07002497 if (copy_to_user_state_extra(x, p, skb))
2498 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002499
Thomas Graf98250692007-08-22 12:47:26 -07002500 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002501
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002502 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002503
Thomas Grafc0144be2007-08-22 13:55:43 -07002504nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002505 /* Somebody screwed up with xfrm_sa_len! */
2506 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002507 kfree_skb(skb);
2508 return -1;
2509}
2510
David S. Miller214e0052011-02-24 00:02:38 -05002511static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002512{
2513
2514 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002515 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002516 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002517 case XFRM_MSG_NEWAE:
2518 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002519 case XFRM_MSG_DELSA:
2520 case XFRM_MSG_UPDSA:
2521 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002522 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002523 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002524 return xfrm_notify_sa_flush(c);
2525 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002526 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2527 c->event);
2528 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002529 }
2530
2531 return 0;
2532
2533}
2534
Thomas Graf7deb2262007-08-22 13:57:39 -07002535static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2536 struct xfrm_policy *xp)
2537{
2538 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2539 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002540 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002541 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2542 + userpolicy_type_attrsize();
2543}
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2546 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2547 int dir)
2548{
2549 struct xfrm_user_acquire *ua;
2550 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 __u32 seq = xfrm_get_acqseq();
2552
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002553 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2554 if (nlh == NULL)
2555 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Thomas Graf7b67c852007-08-22 13:53:52 -07002557 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 memcpy(&ua->id, &x->id, sizeof(ua->id));
2559 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2560 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2561 copy_to_user_policy(xp, &ua->policy, dir);
2562 ua->aalgos = xt->aalgos;
2563 ua->ealgos = xt->ealgos;
2564 ua->calgos = xt->calgos;
2565 ua->seq = x->km.seq = seq;
2566
2567 if (copy_to_user_tmpl(xp, skb) < 0)
2568 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002569 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002570 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002571 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002572 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002573 if (xfrm_mark_put(skb, &xp->mark))
2574 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Thomas Graf98250692007-08-22 12:47:26 -07002576 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002578nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002580 nlmsg_cancel(skb, nlh);
2581 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
2584static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2585 struct xfrm_policy *xp, int dir)
2586{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002587 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Thomas Graf7deb2262007-08-22 13:57:39 -07002590 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (skb == NULL)
2592 return -ENOMEM;
2593
2594 if (build_acquire(skb, x, xt, xp, dir) < 0)
2595 BUG();
2596
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002597 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
2600/* User gives us xfrm_user_policy_info followed by an array of 0
2601 * or more templates.
2602 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002603static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 u8 *data, int len, int *dir)
2605{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002606 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2608 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2609 struct xfrm_policy *xp;
2610 int nr;
2611
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002612 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 case AF_INET:
2614 if (opt != IP_XFRM_POLICY) {
2615 *dir = -EOPNOTSUPP;
2616 return NULL;
2617 }
2618 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002619#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 case AF_INET6:
2621 if (opt != IPV6_XFRM_POLICY) {
2622 *dir = -EOPNOTSUPP;
2623 return NULL;
2624 }
2625 break;
2626#endif
2627 default:
2628 *dir = -EINVAL;
2629 return NULL;
2630 }
2631
2632 *dir = -EINVAL;
2633
2634 if (len < sizeof(*p) ||
2635 verify_newpolicy_info(p))
2636 return NULL;
2637
2638 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002639 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return NULL;
2641
Herbert Xua4f1bac2005-07-26 15:43:17 -07002642 if (p->dir > XFRM_POLICY_OUT)
2643 return NULL;
2644
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002645 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (xp == NULL) {
2647 *dir = -ENOBUFS;
2648 return NULL;
2649 }
2650
2651 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002652 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 copy_templates(xp, ut, nr);
2654
2655 *dir = p->dir;
2656
2657 return xp;
2658}
2659
Thomas Graf7deb2262007-08-22 13:57:39 -07002660static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2661{
2662 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2663 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2664 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002665 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002666 + userpolicy_type_attrsize();
2667}
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002670 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 struct xfrm_user_polexpire *upe;
2673 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002674 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002676 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2677 if (nlh == NULL)
2678 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Thomas Graf7b67c852007-08-22 13:53:52 -07002680 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 copy_to_user_policy(xp, &upe->pol, dir);
2682 if (copy_to_user_tmpl(xp, skb) < 0)
2683 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002684 if (copy_to_user_sec_ctx(xp, skb))
2685 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002686 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002687 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002688 if (xfrm_mark_put(skb, &xp->mark))
2689 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 upe->hard = !!hard;
2691
Thomas Graf98250692007-08-22 12:47:26 -07002692 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002694nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002696 nlmsg_cancel(skb, nlh);
2697 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
David S. Miller214e0052011-02-24 00:02:38 -05002700static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002702 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Thomas Graf7deb2262007-08-22 13:57:39 -07002705 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (skb == NULL)
2707 return -ENOMEM;
2708
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002709 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 BUG();
2711
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002712 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
David S. Miller214e0052011-02-24 00:02:38 -05002715static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002716{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002717 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002718 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002719 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002720 struct nlmsghdr *nlh;
2721 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002722 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002723 int headlen;
2724
2725 headlen = sizeof(*p);
2726 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002727 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002728 headlen = sizeof(*id);
2729 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002730 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002731 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002732 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002733
Thomas Graf7deb2262007-08-22 13:57:39 -07002734 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002735 if (skb == NULL)
2736 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002737
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002738 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2739 if (nlh == NULL)
2740 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002741
Thomas Graf7b67c852007-08-22 13:53:52 -07002742 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002743 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002744 struct nlattr *attr;
2745
Thomas Graf7b67c852007-08-22 13:53:52 -07002746 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002747 memset(id, 0, sizeof(*id));
2748 id->dir = dir;
2749 if (c->data.byid)
2750 id->index = xp->index;
2751 else
2752 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2753
Thomas Grafc0144be2007-08-22 13:55:43 -07002754 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2755 if (attr == NULL)
2756 goto nlmsg_failure;
2757
2758 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002759 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002760
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002761 copy_to_user_policy(xp, p, dir);
2762 if (copy_to_user_tmpl(xp, skb) < 0)
2763 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002764 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002765 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002766
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002767 if (xfrm_mark_put(skb, &xp->mark))
2768 goto nla_put_failure;
2769
Thomas Graf98250692007-08-22 12:47:26 -07002770 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002771
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002772 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002773
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002774nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002775nlmsg_failure:
2776 kfree_skb(skb);
2777 return -1;
2778}
2779
David S. Miller214e0052011-02-24 00:02:38 -05002780static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002781{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002782 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002783 struct nlmsghdr *nlh;
2784 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002785
Thomas Graf7deb2262007-08-22 13:57:39 -07002786 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002787 if (skb == NULL)
2788 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002789
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002790 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2791 if (nlh == NULL)
2792 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002793 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2794 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002795
Thomas Graf98250692007-08-22 12:47:26 -07002796 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002797
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002798 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002799
2800nlmsg_failure:
2801 kfree_skb(skb);
2802 return -1;
2803}
2804
David S. Miller214e0052011-02-24 00:02:38 -05002805static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002806{
2807
2808 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002809 case XFRM_MSG_NEWPOLICY:
2810 case XFRM_MSG_UPDPOLICY:
2811 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002812 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002813 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002814 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002815 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002816 return xfrm_exp_policy_notify(xp, dir, c);
2817 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002818 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2819 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002820 }
2821
2822 return 0;
2823
2824}
2825
Thomas Graf7deb2262007-08-22 13:57:39 -07002826static inline size_t xfrm_report_msgsize(void)
2827{
2828 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2829}
2830
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002831static int build_report(struct sk_buff *skb, u8 proto,
2832 struct xfrm_selector *sel, xfrm_address_t *addr)
2833{
2834 struct xfrm_user_report *ur;
2835 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002836
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002837 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2838 if (nlh == NULL)
2839 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002840
Thomas Graf7b67c852007-08-22 13:53:52 -07002841 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002842 ur->proto = proto;
2843 memcpy(&ur->sel, sel, sizeof(ur->sel));
2844
2845 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002846 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002847
Thomas Graf98250692007-08-22 12:47:26 -07002848 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002849
Thomas Grafc0144be2007-08-22 13:55:43 -07002850nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002851 nlmsg_cancel(skb, nlh);
2852 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002853}
2854
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002855static int xfrm_send_report(struct net *net, u8 proto,
2856 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002857{
2858 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002859
Thomas Graf7deb2262007-08-22 13:57:39 -07002860 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002861 if (skb == NULL)
2862 return -ENOMEM;
2863
2864 if (build_report(skb, proto, sel, addr) < 0)
2865 BUG();
2866
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002867 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002868}
2869
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002870static inline size_t xfrm_mapping_msgsize(void)
2871{
2872 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2873}
2874
2875static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2876 xfrm_address_t *new_saddr, __be16 new_sport)
2877{
2878 struct xfrm_user_mapping *um;
2879 struct nlmsghdr *nlh;
2880
2881 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2882 if (nlh == NULL)
2883 return -EMSGSIZE;
2884
2885 um = nlmsg_data(nlh);
2886
2887 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2888 um->id.spi = x->id.spi;
2889 um->id.family = x->props.family;
2890 um->id.proto = x->id.proto;
2891 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2892 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2893 um->new_sport = new_sport;
2894 um->old_sport = x->encap->encap_sport;
2895 um->reqid = x->props.reqid;
2896
2897 return nlmsg_end(skb, nlh);
2898}
2899
2900static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2901 __be16 sport)
2902{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002903 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002904 struct sk_buff *skb;
2905
2906 if (x->id.proto != IPPROTO_ESP)
2907 return -EINVAL;
2908
2909 if (!x->encap)
2910 return -EINVAL;
2911
2912 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2913 if (skb == NULL)
2914 return -ENOMEM;
2915
2916 if (build_mapping(skb, x, ipaddr, sport) < 0)
2917 BUG();
2918
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002919 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002920}
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922static struct xfrm_mgr netlink_mgr = {
2923 .id = "netlink",
2924 .notify = xfrm_send_state_notify,
2925 .acquire = xfrm_send_acquire,
2926 .compile_policy = xfrm_compile_policy,
2927 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002928 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002929 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002930 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931};
2932
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002933static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Patrick McHardybe336902006-03-20 22:40:54 -08002935 struct sock *nlsk;
2936
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002937 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002938 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002939 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002941 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00002942 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return 0;
2944}
2945
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002946static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002947{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002948 struct net *net;
2949 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002950 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002951 synchronize_net();
2952 list_for_each_entry(net, net_exit_list, exit_list)
2953 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002954}
2955
2956static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002957 .init = xfrm_user_net_init,
2958 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002959};
2960
2961static int __init xfrm_user_init(void)
2962{
2963 int rv;
2964
2965 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2966
2967 rv = register_pernet_subsys(&xfrm_user_net_ops);
2968 if (rv < 0)
2969 return rv;
2970 rv = xfrm_register_km(&netlink_mgr);
2971 if (rv < 0)
2972 unregister_pernet_subsys(&xfrm_user_net_ops);
2973 return rv;
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976static void __exit xfrm_user_exit(void)
2977{
2978 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002979 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
2982module_init(xfrm_user_init);
2983module_exit(xfrm_user_exit);
2984MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002985MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002986