blob: 9e5c07eb292bfe0a31c4f7270fb70bf74af23443 [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{
1303 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1304 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1305 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1306 p->priority = xp->priority;
1307 p->index = xp->index;
1308 p->sel.family = xp->family;
1309 p->dir = dir;
1310 p->action = xp->action;
1311 p->flags = xp->flags;
1312 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1313}
1314
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001315static 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 -07001316{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001317 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int err;
1319
1320 if (!xp) {
1321 *errp = -ENOMEM;
1322 return NULL;
1323 }
1324
1325 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001326
Thomas Graf35a7aa02007-08-22 14:00:40 -07001327 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001328 if (err)
1329 goto error;
1330
Thomas Graf35a7aa02007-08-22 14:00:40 -07001331 if (!(err = copy_from_user_tmpl(xp, attrs)))
1332 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001333 if (err)
1334 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001336 xfrm_mark_get(attrs, &xp->mark);
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001339 error:
1340 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001341 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001342 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001343 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Christoph Hellwig22e70052007-01-02 15:22:30 -08001346static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001347 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001349 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001350 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001352 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int err;
1354 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001355 uid_t loginuid = audit_get_loginuid(current);
1356 u32 sessionid = audit_get_sessionid(current);
1357 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 err = verify_newpolicy_info(p);
1360 if (err)
1361 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001362 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001363 if (err)
1364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001366 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (!xp)
1368 return err;
1369
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001370 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001371 * Aha! this is anti-netlink really i.e more pfkey derived
1372 * in netlink excl is a flag and you wouldnt need
1373 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1375 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001376 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001377 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001380 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 kfree(xp);
1382 return err;
1383 }
1384
Herbert Xuf60f6b82005-06-18 22:44:37 -07001385 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001386 c.seq = nlh->nlmsg_seq;
1387 c.pid = nlh->nlmsg_pid;
1388 km_policy_notify(xp, p->dir, &c);
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 xfrm_pol_put(xp);
1391
1392 return 0;
1393}
1394
1395static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1396{
1397 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1398 int i;
1399
1400 if (xp->xfrm_nr == 0)
1401 return 0;
1402
1403 for (i = 0; i < xp->xfrm_nr; i++) {
1404 struct xfrm_user_tmpl *up = &vec[i];
1405 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1406
1407 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001408 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1410 up->reqid = kp->reqid;
1411 up->mode = kp->mode;
1412 up->share = kp->share;
1413 up->optional = kp->optional;
1414 up->aalgos = kp->aalgos;
1415 up->ealgos = kp->ealgos;
1416 up->calgos = kp->calgos;
1417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Thomas Grafc0144be2007-08-22 13:55:43 -07001419 return nla_put(skb, XFRMA_TMPL,
1420 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001421}
1422
Serge Hallyn0d681622006-07-24 23:30:44 -07001423static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1424{
1425 if (x->security) {
1426 return copy_sec_ctx(x->security, skb);
1427 }
1428 return 0;
1429}
1430
1431static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1432{
1433 if (xp->security) {
1434 return copy_sec_ctx(xp->security, skb);
1435 }
1436 return 0;
1437}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001438static inline size_t userpolicy_type_attrsize(void)
1439{
1440#ifdef CONFIG_XFRM_SUB_POLICY
1441 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1442#else
1443 return 0;
1444#endif
1445}
Serge Hallyn0d681622006-07-24 23:30:44 -07001446
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001447#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001448static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001449{
Thomas Grafc0144be2007-08-22 13:55:43 -07001450 struct xfrm_userpolicy_type upt = {
1451 .type = type,
1452 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001453
Thomas Grafc0144be2007-08-22 13:55:43 -07001454 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001455}
1456
1457#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001458static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001459{
1460 return 0;
1461}
1462#endif
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1465{
1466 struct xfrm_dump_info *sp = ptr;
1467 struct xfrm_userpolicy_info *p;
1468 struct sk_buff *in_skb = sp->in_skb;
1469 struct sk_buff *skb = sp->out_skb;
1470 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001472 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1473 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1474 if (nlh == NULL)
1475 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Thomas Graf7b67c852007-08-22 13:53:52 -07001477 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 copy_to_user_policy(xp, p, dir);
1479 if (copy_to_user_tmpl(xp, skb) < 0)
1480 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001481 if (copy_to_user_sec_ctx(xp, skb))
1482 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001483 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001484 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001485 if (xfrm_mark_put(skb, &xp->mark))
1486 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Thomas Graf98250692007-08-22 12:47:26 -07001488 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return 0;
1490
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001491nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001493 nlmsg_cancel(skb, nlh);
1494 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Timo Teras4c563f72008-02-28 21:31:08 -08001497static int xfrm_dump_policy_done(struct netlink_callback *cb)
1498{
1499 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1500
1501 xfrm_policy_walk_done(walk);
1502 return 0;
1503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1506{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001507 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001508 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 struct xfrm_dump_info info;
1510
Timo Teras4c563f72008-02-28 21:31:08 -08001511 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1512 sizeof(cb->args) - sizeof(cb->args[0]));
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 info.in_skb = cb->skb;
1515 info.out_skb = skb;
1516 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1517 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001518
1519 if (!cb->args[0]) {
1520 cb->args[0] = 1;
1521 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1522 }
1523
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001524 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 return skb->len;
1527}
1528
1529static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1530 struct xfrm_policy *xp,
1531 int dir, u32 seq)
1532{
1533 struct xfrm_dump_info info;
1534 struct sk_buff *skb;
Mathias Krausef38b3342012-09-14 09:58:32 +00001535 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Thomas Graf7deb2262007-08-22 13:57:39 -07001537 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (!skb)
1539 return ERR_PTR(-ENOMEM);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 info.in_skb = in_skb;
1542 info.out_skb = skb;
1543 info.nlmsg_seq = seq;
1544 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Mathias Krausef38b3342012-09-14 09:58:32 +00001546 err = dump_one_policy(xp, dir, 0, &info);
1547 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 kfree_skb(skb);
Mathias Krausef38b3342012-09-14 09:58:32 +00001549 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551
1552 return skb;
1553}
1554
Christoph Hellwig22e70052007-01-02 15:22:30 -08001555static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001556 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001558 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct xfrm_policy *xp;
1560 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001561 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001563 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001565 struct xfrm_mark m;
1566 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Thomas Graf7b67c852007-08-22 13:53:52 -07001568 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1570
Thomas Graf35a7aa02007-08-22 14:00:40 -07001571 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001572 if (err)
1573 return err;
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 err = verify_policy_dir(p->dir);
1576 if (err)
1577 return err;
1578
1579 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001580 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001581 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001582 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001583 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001584
Thomas Graf35a7aa02007-08-22 14:00:40 -07001585 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001586 if (err)
1587 return err;
1588
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001589 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001590 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001591 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001592
Paul Moore03e1ad72008-04-12 19:07:52 -07001593 err = security_xfrm_policy_alloc(&ctx, uctx);
1594 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001595 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001596 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001597 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001598 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001599 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (xp == NULL)
1602 return -ENOENT;
1603
1604 if (!delete) {
1605 struct sk_buff *resp_skb;
1606
1607 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1608 if (IS_ERR(resp_skb)) {
1609 err = PTR_ERR(resp_skb);
1610 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001611 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001612 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001614 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001615 uid_t loginuid = audit_get_loginuid(current);
1616 u32 sessionid = audit_get_sessionid(current);
1617 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001618
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001619 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001620 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1621 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001622
1623 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001624 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001625
Herbert Xue7443892005-06-18 22:44:18 -07001626 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001627 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001628 c.seq = nlh->nlmsg_seq;
1629 c.pid = nlh->nlmsg_pid;
1630 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001633out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001634 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return err;
1636}
1637
Christoph Hellwig22e70052007-01-02 15:22:30 -08001638static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001639 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001641 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001642 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001643 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001644 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001645 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001647 audit_info.loginuid = audit_get_loginuid(current);
1648 audit_info.sessionid = audit_get_sessionid(current);
1649 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001650 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001651 if (err) {
1652 if (err == -ESRCH) /* empty table */
1653 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001654 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001655 }
Herbert Xubf088672005-06-18 22:44:00 -07001656 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001657 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001658 c.seq = nlh->nlmsg_seq;
1659 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001660 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001661 km_state_notify(NULL, &c);
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return 0;
1664}
1665
Steffen Klassertd8647b72011-03-08 00:10:27 +00001666static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001667{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001668 size_t replay_size = x->replay_esn ?
1669 xfrm_replay_state_esn_len(x->replay_esn) :
1670 sizeof(struct xfrm_replay_state);
1671
Thomas Graf7deb2262007-08-22 13:57:39 -07001672 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001673 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001674 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001675 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001676 + nla_total_size(4) /* XFRM_AE_RTHR */
1677 + nla_total_size(4); /* XFRM_AE_ETHR */
1678}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001679
David S. Miller214e0052011-02-24 00:02:38 -05001680static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001681{
1682 struct xfrm_aevent_id *id;
1683 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001684
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001685 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1686 if (nlh == NULL)
1687 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001688
Thomas Graf7b67c852007-08-22 13:53:52 -07001689 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001690 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001691 id->sa_id.spi = x->id.spi;
1692 id->sa_id.family = x->props.family;
1693 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001694 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1695 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001696 id->flags = c->data.aevent;
1697
Steffen Klassertd8647b72011-03-08 00:10:27 +00001698 if (x->replay_esn)
1699 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1700 xfrm_replay_state_esn_len(x->replay_esn),
1701 x->replay_esn);
1702 else
1703 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1704
Thomas Grafc0144be2007-08-22 13:55:43 -07001705 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001706
Thomas Grafc0144be2007-08-22 13:55:43 -07001707 if (id->flags & XFRM_AE_RTHR)
1708 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001709
Thomas Grafc0144be2007-08-22 13:55:43 -07001710 if (id->flags & XFRM_AE_ETHR)
1711 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1712 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001713
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001714 if (xfrm_mark_put(skb, &x->mark))
1715 goto nla_put_failure;
1716
Thomas Graf98250692007-08-22 12:47:26 -07001717 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001718
Thomas Grafc0144be2007-08-22 13:55:43 -07001719nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001720 nlmsg_cancel(skb, nlh);
1721 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001722}
1723
Christoph Hellwig22e70052007-01-02 15:22:30 -08001724static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001725 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001726{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001727 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001728 struct xfrm_state *x;
1729 struct sk_buff *r_skb;
1730 int err;
1731 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001732 u32 mark;
1733 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001734 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001735 struct xfrm_usersa_id *id = &p->sa_id;
1736
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001737 mark = xfrm_mark_get(attrs, &m);
1738
1739 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001740 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001741 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001742
1743 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1744 if (r_skb == NULL) {
1745 xfrm_state_put(x);
1746 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001747 }
1748
1749 /*
1750 * XXX: is this lock really needed - none of the other
1751 * gets lock (the concern is things getting updated
1752 * while we are still reading) - jhs
1753 */
1754 spin_lock_bh(&x->lock);
1755 c.data.aevent = p->flags;
1756 c.seq = nlh->nlmsg_seq;
1757 c.pid = nlh->nlmsg_pid;
1758
1759 if (build_aevent(r_skb, x, &c) < 0)
1760 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001761 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001762 spin_unlock_bh(&x->lock);
1763 xfrm_state_put(x);
1764 return err;
1765}
1766
Christoph Hellwig22e70052007-01-02 15:22:30 -08001767static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001768 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001769{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001770 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001771 struct xfrm_state *x;
1772 struct km_event c;
1773 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001774 u32 mark = 0;
1775 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001776 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001777 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001778 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001779 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001780
Steffen Klassertd8647b72011-03-08 00:10:27 +00001781 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001782 return err;
1783
1784 /* pedantic mode - thou shalt sayeth replaceth */
1785 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1786 return err;
1787
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001788 mark = xfrm_mark_get(attrs, &m);
1789
1790 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 -08001791 if (x == NULL)
1792 return -ESRCH;
1793
1794 if (x->km.state != XFRM_STATE_VALID)
1795 goto out;
1796
Steffen Klasserte2b19122011-03-28 19:47:30 +00001797 err = xfrm_replay_verify_len(x->replay_esn, rp);
1798 if (err)
1799 goto out;
1800
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001801 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001802 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001803 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001804
1805 c.event = nlh->nlmsg_type;
1806 c.seq = nlh->nlmsg_seq;
1807 c.pid = nlh->nlmsg_pid;
1808 c.data.aevent = XFRM_AE_CU;
1809 km_state_notify(x, &c);
1810 err = 0;
1811out:
1812 xfrm_state_put(x);
1813 return err;
1814}
1815
Christoph Hellwig22e70052007-01-02 15:22:30 -08001816static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001817 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001819 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001820 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001821 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001822 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001823 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001824
Thomas Graf35a7aa02007-08-22 14:00:40 -07001825 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001826 if (err)
1827 return err;
1828
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001829 audit_info.loginuid = audit_get_loginuid(current);
1830 audit_info.sessionid = audit_get_sessionid(current);
1831 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001832 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001833 if (err) {
1834 if (err == -ESRCH) /* empty table */
1835 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001836 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001837 }
1838
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001839 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001840 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001841 c.seq = nlh->nlmsg_seq;
1842 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001843 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001844 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return 0;
1846}
1847
Christoph Hellwig22e70052007-01-02 15:22:30 -08001848static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001849 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001850{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001851 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001852 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001853 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001854 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001855 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001856 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001857 struct xfrm_mark m;
1858 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001859
Thomas Graf35a7aa02007-08-22 14:00:40 -07001860 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001861 if (err)
1862 return err;
1863
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001864 err = verify_policy_dir(p->dir);
1865 if (err)
1866 return err;
1867
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001868 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001869 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001870 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001871 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001872 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001873
Thomas Graf35a7aa02007-08-22 14:00:40 -07001874 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001875 if (err)
1876 return err;
1877
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001878 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001879 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001880 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001881
Paul Moore03e1ad72008-04-12 19:07:52 -07001882 err = security_xfrm_policy_alloc(&ctx, uctx);
1883 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001884 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001885 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001886 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001887 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001888 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001889 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001890 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001891 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001892
Timo Teräsea2dea92010-03-31 00:17:05 +00001893 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001894 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001895
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001896 err = 0;
1897 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001898 uid_t loginuid = audit_get_loginuid(current);
1899 u32 sessionid = audit_get_sessionid(current);
1900 u32 sid;
1901
1902 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001903 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001904 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001905
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001906 } else {
1907 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001908 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001909 }
1910 km_policy_expired(xp, p->dir, up->hard, current->pid);
1911
1912out:
1913 xfrm_pol_put(xp);
1914 return err;
1915}
1916
Christoph Hellwig22e70052007-01-02 15:22:30 -08001917static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001918 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001919{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001920 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001921 struct xfrm_state *x;
1922 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001923 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001924 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001925 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001926 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001927
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001928 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 -08001929
David S. Miller3a765aa2007-02-26 14:52:21 -08001930 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001931 if (x == NULL)
1932 return err;
1933
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001934 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001935 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001936 if (x->km.state != XFRM_STATE_VALID)
1937 goto out;
1938 km_state_expired(x, ue->hard, current->pid);
1939
Joy Latten161a09e2006-11-27 13:11:54 -06001940 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001941 uid_t loginuid = audit_get_loginuid(current);
1942 u32 sessionid = audit_get_sessionid(current);
1943 u32 sid;
1944
1945 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001946 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001947 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001948 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001949 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001950out:
1951 spin_unlock_bh(&x->lock);
1952 xfrm_state_put(x);
1953 return err;
1954}
1955
Christoph Hellwig22e70052007-01-02 15:22:30 -08001956static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001957 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001958{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001959 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001960 struct xfrm_policy *xp;
1961 struct xfrm_user_tmpl *ut;
1962 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001963 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001964 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001965
Thomas Graf7b67c852007-08-22 13:53:52 -07001966 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001967 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001968 int err = -ENOMEM;
1969
1970 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001971 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001972
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001973 xfrm_mark_get(attrs, &mark);
1974
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001975 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001976 if (err)
1977 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001978
1979 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001980 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001981 if (!xp)
1982 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001983
1984 memcpy(&x->id, &ua->id, sizeof(ua->id));
1985 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1986 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001987 xp->mark.m = x->mark.m = mark.m;
1988 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001989 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001990 /* extract the templates and for each call km_key */
1991 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1992 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1993 memcpy(&x->id, &t->id, sizeof(x->id));
1994 x->props.mode = t->mode;
1995 x->props.reqid = t->reqid;
1996 x->props.family = ut->family;
1997 t->aalgos = ua->aalgos;
1998 t->ealgos = ua->ealgos;
1999 t->calgos = ua->calgos;
2000 err = km_query(x, t, xp);
2001
2002 }
2003
2004 kfree(x);
2005 kfree(xp);
2006
2007 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002008
2009bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002010 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002011free_state:
2012 kfree(x);
2013nomem:
2014 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002015}
2016
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002017#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002018static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002019 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002020 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002021{
Thomas Graf5424f322007-08-22 14:01:33 -07002022 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002023 struct xfrm_user_migrate *um;
2024 int i, num_migrate;
2025
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002026 if (k != NULL) {
2027 struct xfrm_user_kmaddress *uk;
2028
2029 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2030 memcpy(&k->local, &uk->local, sizeof(k->local));
2031 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2032 k->family = uk->family;
2033 k->reserved = uk->reserved;
2034 }
2035
Thomas Graf5424f322007-08-22 14:01:33 -07002036 um = nla_data(rt);
2037 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002038
2039 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2040 return -EINVAL;
2041
2042 for (i = 0; i < num_migrate; i++, um++, ma++) {
2043 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2044 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2045 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2046 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2047
2048 ma->proto = um->proto;
2049 ma->mode = um->mode;
2050 ma->reqid = um->reqid;
2051
2052 ma->old_family = um->old_family;
2053 ma->new_family = um->new_family;
2054 }
2055
2056 *num = i;
2057 return 0;
2058}
2059
2060static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002061 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002062{
Thomas Graf7b67c852007-08-22 13:53:52 -07002063 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002064 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002065 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002066 u8 type;
2067 int err;
2068 int n = 0;
2069
Thomas Graf35a7aa02007-08-22 14:00:40 -07002070 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002071 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002073 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2074
Thomas Graf5424f322007-08-22 14:01:33 -07002075 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002076 if (err)
2077 return err;
2078
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002079 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002080 if (err)
2081 return err;
2082
2083 if (!n)
2084 return 0;
2085
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002086 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002087
2088 return 0;
2089}
2090#else
2091static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002092 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002093{
2094 return -ENOPROTOOPT;
2095}
2096#endif
2097
2098#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002099static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002100{
2101 struct xfrm_user_migrate um;
2102
2103 memset(&um, 0, sizeof(um));
2104 um.proto = m->proto;
2105 um.mode = m->mode;
2106 um.reqid = m->reqid;
2107 um.old_family = m->old_family;
2108 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2109 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2110 um.new_family = m->new_family;
2111 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2112 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2113
Thomas Grafc0144be2007-08-22 13:55:43 -07002114 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002115}
2116
David S. Miller183cad12011-02-24 00:28:01 -05002117static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002118{
2119 struct xfrm_user_kmaddress uk;
2120
2121 memset(&uk, 0, sizeof(uk));
2122 uk.family = k->family;
2123 uk.reserved = k->reserved;
2124 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002125 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002126
2127 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2128}
2129
2130static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002131{
2132 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002133 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2134 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2135 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002136}
2137
David S. Miller183cad12011-02-24 00:28:01 -05002138static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2139 int num_migrate, const struct xfrm_kmaddress *k,
2140 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002141{
David S. Miller183cad12011-02-24 00:28:01 -05002142 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002143 struct xfrm_userpolicy_id *pol_id;
2144 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002145 int i;
2146
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002147 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2148 if (nlh == NULL)
2149 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002150
Thomas Graf7b67c852007-08-22 13:53:52 -07002151 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002152 /* copy data from selector, dir, and type to the pol_id */
2153 memset(pol_id, 0, sizeof(*pol_id));
2154 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2155 pol_id->dir = dir;
2156
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002157 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2158 goto nlmsg_failure;
2159
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002160 if (copy_to_user_policy_type(type, skb) < 0)
2161 goto nlmsg_failure;
2162
2163 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2164 if (copy_to_user_migrate(mp, skb) < 0)
2165 goto nlmsg_failure;
2166 }
2167
Thomas Graf98250692007-08-22 12:47:26 -07002168 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002169nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002170 nlmsg_cancel(skb, nlh);
2171 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002172}
2173
David S. Miller183cad12011-02-24 00:28:01 -05002174static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2175 const struct xfrm_migrate *m, int num_migrate,
2176 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002177{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002178 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002179 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002180
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002181 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002182 if (skb == NULL)
2183 return -ENOMEM;
2184
2185 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002186 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002187 BUG();
2188
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002189 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002190}
2191#else
David S. Miller183cad12011-02-24 00:28:01 -05002192static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2193 const struct xfrm_migrate *m, int num_migrate,
2194 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002195{
2196 return -ENOPROTOOPT;
2197}
2198#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002199
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002200#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002201
2202static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002203 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002204 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2205 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2206 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2207 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2208 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2209 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002210 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002211 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002212 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002213 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002214 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002215 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002216 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002217 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2218 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002219 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002220 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002221 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2222 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223};
2224
Thomas Graf492b5582005-05-03 14:26:40 -07002225#undef XMSGSIZE
2226
Thomas Grafcf5cb792007-08-22 13:59:04 -07002227static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002228 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2229 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2230 [XFRMA_LASTUSED] = { .type = NLA_U64},
2231 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002232 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002233 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2234 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2235 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2236 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2237 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2238 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2239 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2240 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2241 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2242 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2243 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2244 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2245 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2246 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002247 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002248 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002249 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002250 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002251};
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002254 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002256 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002257} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2258 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2259 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2260 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002261 .dump = xfrm_dump_sa,
2262 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002263 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2264 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2265 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002266 .dump = xfrm_dump_policy,
2267 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002268 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002269 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002270 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002271 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2272 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002273 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002274 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2275 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002276 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2277 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002278 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002279 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002280 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281};
2282
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002283static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002285 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002286 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002288 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002292 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 type -= XFRM_MSG_BASE;
2295 link = &xfrm_dispatch[type];
2296
2297 /* All operations require privileges, even GET */
Eric Parisfd778462012-01-03 12:25:16 -05002298 if (!capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002299 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Thomas Graf492b5582005-05-03 14:26:40 -07002301 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2302 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002303 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002305 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002307 {
2308 struct netlink_dump_control c = {
2309 .dump = link->dump,
2310 .done = link->done,
2311 };
2312 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315
Thomas Graf35a7aa02007-08-22 14:00:40 -07002316 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002317 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002318 if (err < 0)
2319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Thomas Graf5424f322007-08-22 14:01:33 -07002324 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002327static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002329 mutex_lock(&xfrm_cfg_mutex);
2330 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2331 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
Thomas Graf7deb2262007-08-22 13:57:39 -07002334static inline size_t xfrm_expire_msgsize(void)
2335{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002336 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2337 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002338}
2339
David S. Miller214e0052011-02-24 00:02:38 -05002340static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 struct xfrm_user_expire *ue;
2343 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002345 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2346 if (nlh == NULL)
2347 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Thomas Graf7b67c852007-08-22 13:53:52 -07002349 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002351 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002353 if (xfrm_mark_put(skb, &x->mark))
2354 goto nla_put_failure;
2355
Thomas Graf98250692007-08-22 12:47:26 -07002356 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002357
2358nla_put_failure:
2359 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
David S. Miller214e0052011-02-24 00:02:38 -05002362static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002364 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct sk_buff *skb;
2366
Thomas Graf7deb2262007-08-22 13:57:39 -07002367 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (skb == NULL)
2369 return -ENOMEM;
2370
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002371 if (build_expire(skb, x, c) < 0) {
2372 kfree_skb(skb);
2373 return -EMSGSIZE;
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002376 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
David S. Miller214e0052011-02-24 00:02:38 -05002379static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002380{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002381 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002382 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002383
Steffen Klassertd8647b72011-03-08 00:10:27 +00002384 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002385 if (skb == NULL)
2386 return -ENOMEM;
2387
2388 if (build_aevent(skb, x, c) < 0)
2389 BUG();
2390
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002391 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002392}
2393
David S. Miller214e0052011-02-24 00:02:38 -05002394static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002395{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002396 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397 struct xfrm_usersa_flush *p;
2398 struct nlmsghdr *nlh;
2399 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002400 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401
Thomas Graf7deb2262007-08-22 13:57:39 -07002402 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403 if (skb == NULL)
2404 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002405
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002406 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2407 if (nlh == NULL) {
2408 kfree_skb(skb);
2409 return -EMSGSIZE;
2410 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002411
Thomas Graf7b67c852007-08-22 13:53:52 -07002412 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002413 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002414
Thomas Graf98250692007-08-22 12:47:26 -07002415 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002416
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002417 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418}
2419
Thomas Graf7deb2262007-08-22 13:57:39 -07002420static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002421{
Thomas Graf7deb2262007-08-22 13:57:39 -07002422 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002423 if (x->aead)
2424 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002425 if (x->aalg) {
2426 l += nla_total_size(sizeof(struct xfrm_algo) +
2427 (x->aalg->alg_key_len + 7) / 8);
2428 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2429 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002430 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002431 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002432 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002433 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002434 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002435 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002436 if (x->tfcpad)
2437 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002438 if (x->replay_esn)
2439 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002440 if (x->security)
2441 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2442 x->security->ctx_len);
2443 if (x->coaddr)
2444 l += nla_total_size(sizeof(*x->coaddr));
2445
Herbert Xud26f3982007-11-13 21:47:08 -08002446 /* Must count x->lastused as it may become non-zero behind our back. */
2447 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002448
2449 return l;
2450}
2451
David S. Miller214e0052011-02-24 00:02:38 -05002452static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002453{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002454 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002455 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002456 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002457 struct nlmsghdr *nlh;
2458 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002459 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002460 int headlen;
2461
2462 headlen = sizeof(*p);
2463 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002464 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002465 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002466 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002467 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002468 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002469
Thomas Graf7deb2262007-08-22 13:57:39 -07002470 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002471 if (skb == NULL)
2472 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002473
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002474 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2475 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002476 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002477
Thomas Graf7b67c852007-08-22 13:53:52 -07002478 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002479 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002480 struct nlattr *attr;
2481
Thomas Graf7b67c852007-08-22 13:53:52 -07002482 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002483 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2484 id->spi = x->id.spi;
2485 id->family = x->props.family;
2486 id->proto = x->id.proto;
2487
Thomas Grafc0144be2007-08-22 13:55:43 -07002488 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2489 if (attr == NULL)
2490 goto nla_put_failure;
2491
2492 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002493 }
2494
Herbert Xu68325d32007-10-09 13:30:57 -07002495 if (copy_to_user_state_extra(x, p, skb))
2496 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002497
Thomas Graf98250692007-08-22 12:47:26 -07002498 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002499
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002500 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002501
Thomas Grafc0144be2007-08-22 13:55:43 -07002502nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002503 /* Somebody screwed up with xfrm_sa_len! */
2504 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002505 kfree_skb(skb);
2506 return -1;
2507}
2508
David S. Miller214e0052011-02-24 00:02:38 -05002509static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002510{
2511
2512 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002513 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002514 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002515 case XFRM_MSG_NEWAE:
2516 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002517 case XFRM_MSG_DELSA:
2518 case XFRM_MSG_UPDSA:
2519 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002520 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002521 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002522 return xfrm_notify_sa_flush(c);
2523 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002524 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2525 c->event);
2526 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002527 }
2528
2529 return 0;
2530
2531}
2532
Thomas Graf7deb2262007-08-22 13:57:39 -07002533static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2534 struct xfrm_policy *xp)
2535{
2536 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2537 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002538 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002539 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2540 + userpolicy_type_attrsize();
2541}
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2544 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2545 int dir)
2546{
2547 struct xfrm_user_acquire *ua;
2548 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 __u32 seq = xfrm_get_acqseq();
2550
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002551 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2552 if (nlh == NULL)
2553 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Thomas Graf7b67c852007-08-22 13:53:52 -07002555 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 memcpy(&ua->id, &x->id, sizeof(ua->id));
2557 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2558 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2559 copy_to_user_policy(xp, &ua->policy, dir);
2560 ua->aalgos = xt->aalgos;
2561 ua->ealgos = xt->ealgos;
2562 ua->calgos = xt->calgos;
2563 ua->seq = x->km.seq = seq;
2564
2565 if (copy_to_user_tmpl(xp, skb) < 0)
2566 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002567 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002568 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002569 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002570 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002571 if (xfrm_mark_put(skb, &xp->mark))
2572 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Thomas Graf98250692007-08-22 12:47:26 -07002574 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002576nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002578 nlmsg_cancel(skb, nlh);
2579 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
2582static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2583 struct xfrm_policy *xp, int dir)
2584{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002585 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Thomas Graf7deb2262007-08-22 13:57:39 -07002588 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (skb == NULL)
2590 return -ENOMEM;
2591
2592 if (build_acquire(skb, x, xt, xp, dir) < 0)
2593 BUG();
2594
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002595 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
2598/* User gives us xfrm_user_policy_info followed by an array of 0
2599 * or more templates.
2600 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002601static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 u8 *data, int len, int *dir)
2603{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002604 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2606 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2607 struct xfrm_policy *xp;
2608 int nr;
2609
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002610 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 case AF_INET:
2612 if (opt != IP_XFRM_POLICY) {
2613 *dir = -EOPNOTSUPP;
2614 return NULL;
2615 }
2616 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002617#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 case AF_INET6:
2619 if (opt != IPV6_XFRM_POLICY) {
2620 *dir = -EOPNOTSUPP;
2621 return NULL;
2622 }
2623 break;
2624#endif
2625 default:
2626 *dir = -EINVAL;
2627 return NULL;
2628 }
2629
2630 *dir = -EINVAL;
2631
2632 if (len < sizeof(*p) ||
2633 verify_newpolicy_info(p))
2634 return NULL;
2635
2636 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002637 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return NULL;
2639
Herbert Xua4f1bac2005-07-26 15:43:17 -07002640 if (p->dir > XFRM_POLICY_OUT)
2641 return NULL;
2642
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002643 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (xp == NULL) {
2645 *dir = -ENOBUFS;
2646 return NULL;
2647 }
2648
2649 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002650 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 copy_templates(xp, ut, nr);
2652
2653 *dir = p->dir;
2654
2655 return xp;
2656}
2657
Thomas Graf7deb2262007-08-22 13:57:39 -07002658static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2659{
2660 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2661 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2662 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002663 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002664 + userpolicy_type_attrsize();
2665}
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002668 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 struct xfrm_user_polexpire *upe;
2671 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002672 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002674 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2675 if (nlh == NULL)
2676 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Thomas Graf7b67c852007-08-22 13:53:52 -07002678 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 copy_to_user_policy(xp, &upe->pol, dir);
2680 if (copy_to_user_tmpl(xp, skb) < 0)
2681 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002682 if (copy_to_user_sec_ctx(xp, skb))
2683 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002684 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002685 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002686 if (xfrm_mark_put(skb, &xp->mark))
2687 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 upe->hard = !!hard;
2689
Thomas Graf98250692007-08-22 12:47:26 -07002690 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002692nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002694 nlmsg_cancel(skb, nlh);
2695 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
David S. Miller214e0052011-02-24 00:02:38 -05002698static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002700 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Thomas Graf7deb2262007-08-22 13:57:39 -07002703 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (skb == NULL)
2705 return -ENOMEM;
2706
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002707 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 BUG();
2709
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002710 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
David S. Miller214e0052011-02-24 00:02:38 -05002713static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002714{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002715 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002716 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002717 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002718 struct nlmsghdr *nlh;
2719 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002720 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002721 int headlen;
2722
2723 headlen = sizeof(*p);
2724 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002725 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002726 headlen = sizeof(*id);
2727 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002728 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002729 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002730 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002731
Thomas Graf7deb2262007-08-22 13:57:39 -07002732 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002733 if (skb == NULL)
2734 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002735
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002736 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2737 if (nlh == NULL)
2738 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002739
Thomas Graf7b67c852007-08-22 13:53:52 -07002740 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002741 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002742 struct nlattr *attr;
2743
Thomas Graf7b67c852007-08-22 13:53:52 -07002744 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002745 memset(id, 0, sizeof(*id));
2746 id->dir = dir;
2747 if (c->data.byid)
2748 id->index = xp->index;
2749 else
2750 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2751
Thomas Grafc0144be2007-08-22 13:55:43 -07002752 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2753 if (attr == NULL)
2754 goto nlmsg_failure;
2755
2756 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002757 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002758
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002759 copy_to_user_policy(xp, p, dir);
2760 if (copy_to_user_tmpl(xp, skb) < 0)
2761 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002762 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002763 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002764
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002765 if (xfrm_mark_put(skb, &xp->mark))
2766 goto nla_put_failure;
2767
Thomas Graf98250692007-08-22 12:47:26 -07002768 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002769
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002770 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002771
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002772nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002773nlmsg_failure:
2774 kfree_skb(skb);
2775 return -1;
2776}
2777
David S. Miller214e0052011-02-24 00:02:38 -05002778static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002779{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002780 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002781 struct nlmsghdr *nlh;
2782 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002783
Thomas Graf7deb2262007-08-22 13:57:39 -07002784 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002785 if (skb == NULL)
2786 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002787
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002788 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2789 if (nlh == NULL)
2790 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002791 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2792 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002793
Thomas Graf98250692007-08-22 12:47:26 -07002794 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002795
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002796 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002797
2798nlmsg_failure:
2799 kfree_skb(skb);
2800 return -1;
2801}
2802
David S. Miller214e0052011-02-24 00:02:38 -05002803static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002804{
2805
2806 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002807 case XFRM_MSG_NEWPOLICY:
2808 case XFRM_MSG_UPDPOLICY:
2809 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002810 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002811 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002812 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002813 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002814 return xfrm_exp_policy_notify(xp, dir, c);
2815 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002816 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2817 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002818 }
2819
2820 return 0;
2821
2822}
2823
Thomas Graf7deb2262007-08-22 13:57:39 -07002824static inline size_t xfrm_report_msgsize(void)
2825{
2826 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2827}
2828
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002829static int build_report(struct sk_buff *skb, u8 proto,
2830 struct xfrm_selector *sel, xfrm_address_t *addr)
2831{
2832 struct xfrm_user_report *ur;
2833 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002834
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002835 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2836 if (nlh == NULL)
2837 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002838
Thomas Graf7b67c852007-08-22 13:53:52 -07002839 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002840 ur->proto = proto;
2841 memcpy(&ur->sel, sel, sizeof(ur->sel));
2842
2843 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002844 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002845
Thomas Graf98250692007-08-22 12:47:26 -07002846 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002847
Thomas Grafc0144be2007-08-22 13:55:43 -07002848nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002849 nlmsg_cancel(skb, nlh);
2850 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002851}
2852
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002853static int xfrm_send_report(struct net *net, u8 proto,
2854 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002855{
2856 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002857
Thomas Graf7deb2262007-08-22 13:57:39 -07002858 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002859 if (skb == NULL)
2860 return -ENOMEM;
2861
2862 if (build_report(skb, proto, sel, addr) < 0)
2863 BUG();
2864
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002865 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002866}
2867
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002868static inline size_t xfrm_mapping_msgsize(void)
2869{
2870 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2871}
2872
2873static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2874 xfrm_address_t *new_saddr, __be16 new_sport)
2875{
2876 struct xfrm_user_mapping *um;
2877 struct nlmsghdr *nlh;
2878
2879 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2880 if (nlh == NULL)
2881 return -EMSGSIZE;
2882
2883 um = nlmsg_data(nlh);
2884
2885 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2886 um->id.spi = x->id.spi;
2887 um->id.family = x->props.family;
2888 um->id.proto = x->id.proto;
2889 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2890 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2891 um->new_sport = new_sport;
2892 um->old_sport = x->encap->encap_sport;
2893 um->reqid = x->props.reqid;
2894
2895 return nlmsg_end(skb, nlh);
2896}
2897
2898static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2899 __be16 sport)
2900{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002901 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002902 struct sk_buff *skb;
2903
2904 if (x->id.proto != IPPROTO_ESP)
2905 return -EINVAL;
2906
2907 if (!x->encap)
2908 return -EINVAL;
2909
2910 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2911 if (skb == NULL)
2912 return -ENOMEM;
2913
2914 if (build_mapping(skb, x, ipaddr, sport) < 0)
2915 BUG();
2916
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002917 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002918}
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920static struct xfrm_mgr netlink_mgr = {
2921 .id = "netlink",
2922 .notify = xfrm_send_state_notify,
2923 .acquire = xfrm_send_acquire,
2924 .compile_policy = xfrm_compile_policy,
2925 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002926 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002927 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002928 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929};
2930
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002931static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Patrick McHardybe336902006-03-20 22:40:54 -08002933 struct sock *nlsk;
2934
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002935 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002936 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002937 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002939 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00002940 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return 0;
2942}
2943
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002944static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002945{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002946 struct net *net;
2947 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002948 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002949 synchronize_net();
2950 list_for_each_entry(net, net_exit_list, exit_list)
2951 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002952}
2953
2954static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002955 .init = xfrm_user_net_init,
2956 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002957};
2958
2959static int __init xfrm_user_init(void)
2960{
2961 int rv;
2962
2963 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2964
2965 rv = register_pernet_subsys(&xfrm_user_net_ops);
2966 if (rv < 0)
2967 return rv;
2968 rv = xfrm_register_km(&netlink_mgr);
2969 if (rv < 0)
2970 unregister_pernet_subsys(&xfrm_user_net_ops);
2971 return rv;
2972}
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974static void __exit xfrm_user_exit(void)
2975{
2976 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002977 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980module_init(xfrm_user_init);
2981module_exit(xfrm_user_exit);
2982MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002983MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002984