blob: c1537f0b4062ad711f23a5be26584a99a28c81c5 [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{
692 memcpy(&p->id, &x->id, sizeof(p->id));
693 memcpy(&p->sel, &x->sel, sizeof(p->sel));
694 memcpy(&p->lft, &x->lft, sizeof(p->lft));
695 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
696 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700697 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 p->mode = x->props.mode;
699 p->replay_window = x->props.replay_window;
700 p->reqid = x->props.reqid;
701 p->family = x->props.family;
702 p->flags = x->props.flags;
703 p->seq = x->km.seq;
704}
705
706struct xfrm_dump_info {
707 struct sk_buff *in_skb;
708 struct sk_buff *out_skb;
709 u32 nlmsg_seq;
710 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711};
712
Thomas Grafc0144be2007-08-22 13:55:43 -0700713static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
714{
Thomas Grafc0144be2007-08-22 13:55:43 -0700715 struct xfrm_user_sec_ctx *uctx;
716 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700717 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700718
719 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
720 if (attr == NULL)
721 return -EMSGSIZE;
722
723 uctx = nla_data(attr);
724 uctx->exttype = XFRMA_SEC_CTX;
725 uctx->len = ctx_size;
726 uctx->ctx_doi = s->ctx_doi;
727 uctx->ctx_alg = s->ctx_alg;
728 uctx->ctx_len = s->ctx_len;
729 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
730
731 return 0;
732}
733
Martin Willi4447bb32009-11-25 00:29:52 +0000734static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
735{
736 struct xfrm_algo *algo;
737 struct nlattr *nla;
738
739 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
740 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
741 if (!nla)
742 return -EMSGSIZE;
743
744 algo = nla_data(nla);
745 strcpy(algo->alg_name, auth->alg_name);
746 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
747 algo->alg_key_len = auth->alg_key_len;
748
749 return 0;
750}
751
Herbert Xu68325d32007-10-09 13:30:57 -0700752/* Don't change this without updating xfrm_sa_len! */
753static int copy_to_user_state_extra(struct xfrm_state *x,
754 struct xfrm_usersa_info *p,
755 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 copy_to_user_state(x, p);
758
Herbert Xu050f0092007-10-09 13:31:47 -0700759 if (x->coaddr)
760 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
761
762 if (x->lastused)
763 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700764
Herbert Xu1a6509d2008-01-28 19:37:29 -0800765 if (x->aead)
766 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000767 if (x->aalg) {
768 if (copy_to_user_auth(x->aalg, skb))
769 goto nla_put_failure;
770
771 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
772 xfrm_alg_auth_len(x->aalg), x->aalg);
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800775 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700777 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700780 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Martin Willi35d28562010-12-08 04:37:49 +0000782 if (x->tfcpad)
783 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
784
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000785 if (xfrm_mark_put(skb, &x->mark))
786 goto nla_put_failure;
787
Steffen Klassertd8647b72011-03-08 00:10:27 +0000788 if (x->replay_esn)
789 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
790 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
791
Thomas Grafc0144be2007-08-22 13:55:43 -0700792 if (x->security && copy_sec_ctx(x->security, skb) < 0)
793 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700794
Herbert Xu68325d32007-10-09 13:30:57 -0700795 return 0;
796
797nla_put_failure:
798 return -EMSGSIZE;
799}
800
801static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
802{
803 struct xfrm_dump_info *sp = ptr;
804 struct sk_buff *in_skb = sp->in_skb;
805 struct sk_buff *skb = sp->out_skb;
806 struct xfrm_usersa_info *p;
807 struct nlmsghdr *nlh;
808 int err;
809
Herbert Xu68325d32007-10-09 13:30:57 -0700810 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
811 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
812 if (nlh == NULL)
813 return -EMSGSIZE;
814
815 p = nlmsg_data(nlh);
816
817 err = copy_to_user_state_extra(x, p, skb);
818 if (err)
819 goto nla_put_failure;
820
Thomas Graf98250692007-08-22 12:47:26 -0700821 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823
Thomas Grafc0144be2007-08-22 13:55:43 -0700824nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700825 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700826 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Timo Teras4c563f72008-02-28 21:31:08 -0800829static int xfrm_dump_sa_done(struct netlink_callback *cb)
830{
831 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
832 xfrm_state_walk_done(walk);
833 return 0;
834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
837{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800838 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800839 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct xfrm_dump_info info;
841
Timo Teras4c563f72008-02-28 21:31:08 -0800842 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
843 sizeof(cb->args) - sizeof(cb->args[0]));
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 info.in_skb = cb->skb;
846 info.out_skb = skb;
847 info.nlmsg_seq = cb->nlh->nlmsg_seq;
848 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800849
850 if (!cb->args[0]) {
851 cb->args[0] = 1;
852 xfrm_state_walk_init(walk, 0);
853 }
854
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800855 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 return skb->len;
858}
859
860static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
861 struct xfrm_state *x, u32 seq)
862{
863 struct xfrm_dump_info info;
864 struct sk_buff *skb;
Mathias Krause555144b2012-09-13 11:41:26 +0000865 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Thomas Graf7deb2262007-08-22 13:57:39 -0700867 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (!skb)
869 return ERR_PTR(-ENOMEM);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 info.in_skb = in_skb;
872 info.out_skb = skb;
873 info.nlmsg_seq = seq;
874 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Mathias Krause555144b2012-09-13 11:41:26 +0000876 err = dump_one_state(x, 0, &info);
877 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 kfree_skb(skb);
Mathias Krause555144b2012-09-13 11:41:26 +0000879 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
882 return skb;
883}
884
Thomas Graf7deb2262007-08-22 13:57:39 -0700885static inline size_t xfrm_spdinfo_msgsize(void)
886{
887 return NLMSG_ALIGN(4)
888 + nla_total_size(sizeof(struct xfrmu_spdinfo))
889 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
890}
891
Alexey Dobriyane0710412010-01-23 13:37:10 +0000892static int build_spdinfo(struct sk_buff *skb, struct net *net,
893 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700894{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700895 struct xfrmk_spdinfo si;
896 struct xfrmu_spdinfo spc;
897 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700898 struct nlmsghdr *nlh;
899 u32 *f;
900
901 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300902 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700903 return -EMSGSIZE;
904
905 f = nlmsg_data(nlh);
906 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000907 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700908 spc.incnt = si.incnt;
909 spc.outcnt = si.outcnt;
910 spc.fwdcnt = si.fwdcnt;
911 spc.inscnt = si.inscnt;
912 spc.outscnt = si.outscnt;
913 spc.fwdscnt = si.fwdscnt;
914 sph.spdhcnt = si.spdhcnt;
915 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700916
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700917 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
918 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700919
920 return nlmsg_end(skb, nlh);
921
922nla_put_failure:
923 nlmsg_cancel(skb, nlh);
924 return -EMSGSIZE;
925}
926
927static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700928 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700929{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800930 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700931 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700932 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700933 u32 spid = NETLINK_CB(skb).pid;
934 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700935
Thomas Graf7deb2262007-08-22 13:57:39 -0700936 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700937 if (r_skb == NULL)
938 return -ENOMEM;
939
Alexey Dobriyane0710412010-01-23 13:37:10 +0000940 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700941 BUG();
942
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800943 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700944}
945
Thomas Graf7deb2262007-08-22 13:57:39 -0700946static inline size_t xfrm_sadinfo_msgsize(void)
947{
948 return NLMSG_ALIGN(4)
949 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
950 + nla_total_size(4); /* XFRMA_SAD_CNT */
951}
952
Alexey Dobriyane0710412010-01-23 13:37:10 +0000953static int build_sadinfo(struct sk_buff *skb, struct net *net,
954 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700955{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700956 struct xfrmk_sadinfo si;
957 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700958 struct nlmsghdr *nlh;
959 u32 *f;
960
961 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300962 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700963 return -EMSGSIZE;
964
965 f = nlmsg_data(nlh);
966 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000967 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700968
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700969 sh.sadhmcnt = si.sadhmcnt;
970 sh.sadhcnt = si.sadhcnt;
971
972 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
973 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700974
975 return nlmsg_end(skb, nlh);
976
977nla_put_failure:
978 nlmsg_cancel(skb, nlh);
979 return -EMSGSIZE;
980}
981
982static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700983 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700984{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800985 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700986 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700987 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700988 u32 spid = NETLINK_CB(skb).pid;
989 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700990
Thomas Graf7deb2262007-08-22 13:57:39 -0700991 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700992 if (r_skb == NULL)
993 return -ENOMEM;
994
Alexey Dobriyane0710412010-01-23 13:37:10 +0000995 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700996 BUG();
997
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800998 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700999}
1000
Christoph Hellwig22e70052007-01-02 15:22:30 -08001001static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001002 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001004 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001005 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct xfrm_state *x;
1007 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001008 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001010 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (x == NULL)
1012 goto out_noput;
1013
1014 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1015 if (IS_ERR(resp_skb)) {
1016 err = PTR_ERR(resp_skb);
1017 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001018 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 xfrm_state_put(x);
1021out_noput:
1022 return err;
1023}
1024
1025static int verify_userspi_info(struct xfrm_userspi_info *p)
1026{
1027 switch (p->info.id.proto) {
1028 case IPPROTO_AH:
1029 case IPPROTO_ESP:
1030 break;
1031
1032 case IPPROTO_COMP:
1033 /* IPCOMP spi is 16-bits. */
1034 if (p->max >= 0x10000)
1035 return -EINVAL;
1036 break;
1037
1038 default:
1039 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (p->min > p->max)
1043 return -EINVAL;
1044
1045 return 0;
1046}
1047
Christoph Hellwig22e70052007-01-02 15:22:30 -08001048static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001049 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001051 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct xfrm_state *x;
1053 struct xfrm_userspi_info *p;
1054 struct sk_buff *resp_skb;
1055 xfrm_address_t *daddr;
1056 int family;
1057 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001058 u32 mark;
1059 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Thomas Graf7b67c852007-08-22 13:53:52 -07001061 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 err = verify_userspi_info(p);
1063 if (err)
1064 goto out_noput;
1065
1066 family = p->info.family;
1067 daddr = &p->info.id.daddr;
1068
1069 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001070
1071 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001073 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1075 xfrm_state_put(x);
1076 x = NULL;
1077 }
1078 }
1079
1080 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001081 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 p->info.id.proto, daddr,
1083 &p->info.saddr, 1,
1084 family);
1085 err = -ENOENT;
1086 if (x == NULL)
1087 goto out_noput;
1088
Herbert Xu658b2192007-10-09 13:29:52 -07001089 err = xfrm_alloc_spi(x, p->min, p->max);
1090 if (err)
1091 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Herbert Xu658b2192007-10-09 13:29:52 -07001093 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (IS_ERR(resp_skb)) {
1095 err = PTR_ERR(resp_skb);
1096 goto out;
1097 }
1098
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001099 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101out:
1102 xfrm_state_put(x);
1103out_noput:
1104 return err;
1105}
1106
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001107static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 switch (dir) {
1110 case XFRM_POLICY_IN:
1111 case XFRM_POLICY_OUT:
1112 case XFRM_POLICY_FWD:
1113 break;
1114
1115 default:
1116 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 return 0;
1120}
1121
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001122static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001123{
1124 switch (type) {
1125 case XFRM_POLICY_TYPE_MAIN:
1126#ifdef CONFIG_XFRM_SUB_POLICY
1127 case XFRM_POLICY_TYPE_SUB:
1128#endif
1129 break;
1130
1131 default:
1132 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001133 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001134
1135 return 0;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1139{
1140 switch (p->share) {
1141 case XFRM_SHARE_ANY:
1142 case XFRM_SHARE_SESSION:
1143 case XFRM_SHARE_USER:
1144 case XFRM_SHARE_UNIQUE:
1145 break;
1146
1147 default:
1148 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 switch (p->action) {
1152 case XFRM_POLICY_ALLOW:
1153 case XFRM_POLICY_BLOCK:
1154 break;
1155
1156 default:
1157 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 switch (p->sel.family) {
1161 case AF_INET:
1162 break;
1163
1164 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001165#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
1167#else
1168 return -EAFNOSUPPORT;
1169#endif
1170
1171 default:
1172 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 return verify_policy_dir(p->dir);
1176}
1177
Thomas Graf5424f322007-08-22 14:01:33 -07001178static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001179{
Thomas Graf5424f322007-08-22 14:01:33 -07001180 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001181 struct xfrm_user_sec_ctx *uctx;
1182
1183 if (!rt)
1184 return 0;
1185
Thomas Graf5424f322007-08-22 14:01:33 -07001186 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001187 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1191 int nr)
1192{
1193 int i;
1194
1195 xp->xfrm_nr = nr;
1196 for (i = 0; i < nr; i++, ut++) {
1197 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1198
1199 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1200 memcpy(&t->saddr, &ut->saddr,
1201 sizeof(xfrm_address_t));
1202 t->reqid = ut->reqid;
1203 t->mode = ut->mode;
1204 t->share = ut->share;
1205 t->optional = ut->optional;
1206 t->aalgos = ut->aalgos;
1207 t->ealgos = ut->ealgos;
1208 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001209 /* If all masks are ~0, then we allow all algorithms. */
1210 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001211 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213}
1214
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001215static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1216{
1217 int i;
1218
1219 if (nr > XFRM_MAX_DEPTH)
1220 return -EINVAL;
1221
1222 for (i = 0; i < nr; i++) {
1223 /* We never validated the ut->family value, so many
1224 * applications simply leave it at zero. The check was
1225 * never made and ut->family was ignored because all
1226 * templates could be assumed to have the same family as
1227 * the policy itself. Now that we will have ipv4-in-ipv6
1228 * and ipv6-in-ipv4 tunnels, this is no longer true.
1229 */
1230 if (!ut[i].family)
1231 ut[i].family = family;
1232
1233 switch (ut[i].family) {
1234 case AF_INET:
1235 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001236#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001237 case AF_INET6:
1238 break;
1239#endif
1240 default:
1241 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001242 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001243 }
1244
1245 return 0;
1246}
1247
Thomas Graf5424f322007-08-22 14:01:33 -07001248static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Thomas Graf5424f322007-08-22 14:01:33 -07001250 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (!rt) {
1253 pol->xfrm_nr = 0;
1254 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001255 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1256 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001257 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001259 err = validate_tmpl(nr, utmpl, pol->family);
1260 if (err)
1261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Thomas Graf5424f322007-08-22 14:01:33 -07001263 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 return 0;
1266}
1267
Thomas Graf5424f322007-08-22 14:01:33 -07001268static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001269{
Thomas Graf5424f322007-08-22 14:01:33 -07001270 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001271 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001272 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001273 int err;
1274
1275 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001276 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001277 type = upt->type;
1278 }
1279
1280 err = verify_policy_type(type);
1281 if (err)
1282 return err;
1283
1284 *tp = type;
1285 return 0;
1286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1289{
1290 xp->priority = p->priority;
1291 xp->index = p->index;
1292 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1293 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1294 xp->action = p->action;
1295 xp->flags = p->flags;
1296 xp->family = p->sel.family;
1297 /* XXX xp->share = p->share; */
1298}
1299
1300static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1301{
1302 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1303 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1304 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1305 p->priority = xp->priority;
1306 p->index = xp->index;
1307 p->sel.family = xp->family;
1308 p->dir = dir;
1309 p->action = xp->action;
1310 p->flags = xp->flags;
1311 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1312}
1313
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001314static 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 -07001315{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001316 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int err;
1318
1319 if (!xp) {
1320 *errp = -ENOMEM;
1321 return NULL;
1322 }
1323
1324 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001325
Thomas Graf35a7aa02007-08-22 14:00:40 -07001326 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001327 if (err)
1328 goto error;
1329
Thomas Graf35a7aa02007-08-22 14:00:40 -07001330 if (!(err = copy_from_user_tmpl(xp, attrs)))
1331 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001332 if (err)
1333 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001335 xfrm_mark_get(attrs, &xp->mark);
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001338 error:
1339 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001340 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001341 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001342 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Christoph Hellwig22e70052007-01-02 15:22:30 -08001345static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001346 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001348 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001349 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001351 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int err;
1353 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001354 uid_t loginuid = audit_get_loginuid(current);
1355 u32 sessionid = audit_get_sessionid(current);
1356 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 err = verify_newpolicy_info(p);
1359 if (err)
1360 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001361 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001362 if (err)
1363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001365 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!xp)
1367 return err;
1368
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001369 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001370 * Aha! this is anti-netlink really i.e more pfkey derived
1371 * in netlink excl is a flag and you wouldnt need
1372 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1374 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001375 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001376 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001379 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 kfree(xp);
1381 return err;
1382 }
1383
Herbert Xuf60f6b82005-06-18 22:44:37 -07001384 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001385 c.seq = nlh->nlmsg_seq;
1386 c.pid = nlh->nlmsg_pid;
1387 km_policy_notify(xp, p->dir, &c);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 xfrm_pol_put(xp);
1390
1391 return 0;
1392}
1393
1394static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1395{
1396 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1397 int i;
1398
1399 if (xp->xfrm_nr == 0)
1400 return 0;
1401
1402 for (i = 0; i < xp->xfrm_nr; i++) {
1403 struct xfrm_user_tmpl *up = &vec[i];
1404 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1405
1406 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001407 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1409 up->reqid = kp->reqid;
1410 up->mode = kp->mode;
1411 up->share = kp->share;
1412 up->optional = kp->optional;
1413 up->aalgos = kp->aalgos;
1414 up->ealgos = kp->ealgos;
1415 up->calgos = kp->calgos;
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Thomas Grafc0144be2007-08-22 13:55:43 -07001418 return nla_put(skb, XFRMA_TMPL,
1419 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001420}
1421
Serge Hallyn0d681622006-07-24 23:30:44 -07001422static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1423{
1424 if (x->security) {
1425 return copy_sec_ctx(x->security, skb);
1426 }
1427 return 0;
1428}
1429
1430static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1431{
1432 if (xp->security) {
1433 return copy_sec_ctx(xp->security, skb);
1434 }
1435 return 0;
1436}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001437static inline size_t userpolicy_type_attrsize(void)
1438{
1439#ifdef CONFIG_XFRM_SUB_POLICY
1440 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1441#else
1442 return 0;
1443#endif
1444}
Serge Hallyn0d681622006-07-24 23:30:44 -07001445
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001446#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001447static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001448{
Thomas Grafc0144be2007-08-22 13:55:43 -07001449 struct xfrm_userpolicy_type upt = {
1450 .type = type,
1451 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001452
Thomas Grafc0144be2007-08-22 13:55:43 -07001453 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001454}
1455
1456#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001457static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001458{
1459 return 0;
1460}
1461#endif
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1464{
1465 struct xfrm_dump_info *sp = ptr;
1466 struct xfrm_userpolicy_info *p;
1467 struct sk_buff *in_skb = sp->in_skb;
1468 struct sk_buff *skb = sp->out_skb;
1469 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001471 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1472 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1473 if (nlh == NULL)
1474 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Thomas Graf7b67c852007-08-22 13:53:52 -07001476 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 copy_to_user_policy(xp, p, dir);
1478 if (copy_to_user_tmpl(xp, skb) < 0)
1479 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001480 if (copy_to_user_sec_ctx(xp, skb))
1481 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001482 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001483 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001484 if (xfrm_mark_put(skb, &xp->mark))
1485 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Thomas Graf98250692007-08-22 12:47:26 -07001487 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001490nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001492 nlmsg_cancel(skb, nlh);
1493 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Timo Teras4c563f72008-02-28 21:31:08 -08001496static int xfrm_dump_policy_done(struct netlink_callback *cb)
1497{
1498 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1499
1500 xfrm_policy_walk_done(walk);
1501 return 0;
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1505{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001506 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001507 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct xfrm_dump_info info;
1509
Timo Teras4c563f72008-02-28 21:31:08 -08001510 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1511 sizeof(cb->args) - sizeof(cb->args[0]));
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 info.in_skb = cb->skb;
1514 info.out_skb = skb;
1515 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1516 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001517
1518 if (!cb->args[0]) {
1519 cb->args[0] = 1;
1520 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1521 }
1522
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001523 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 return skb->len;
1526}
1527
1528static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1529 struct xfrm_policy *xp,
1530 int dir, u32 seq)
1531{
1532 struct xfrm_dump_info info;
1533 struct sk_buff *skb;
1534
Thomas Graf7deb2262007-08-22 13:57:39 -07001535 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (!skb)
1537 return ERR_PTR(-ENOMEM);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 info.in_skb = in_skb;
1540 info.out_skb = skb;
1541 info.nlmsg_seq = seq;
1542 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1545 kfree_skb(skb);
1546 return NULL;
1547 }
1548
1549 return skb;
1550}
1551
Christoph Hellwig22e70052007-01-02 15:22:30 -08001552static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001553 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001555 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct xfrm_policy *xp;
1557 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001558 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001560 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001562 struct xfrm_mark m;
1563 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Thomas Graf7b67c852007-08-22 13:53:52 -07001565 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1567
Thomas Graf35a7aa02007-08-22 14:00:40 -07001568 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001569 if (err)
1570 return err;
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 err = verify_policy_dir(p->dir);
1573 if (err)
1574 return err;
1575
1576 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001577 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001578 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001579 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001580 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001581
Thomas Graf35a7aa02007-08-22 14:00:40 -07001582 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001583 if (err)
1584 return err;
1585
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001586 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001587 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001588 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001589
Paul Moore03e1ad72008-04-12 19:07:52 -07001590 err = security_xfrm_policy_alloc(&ctx, uctx);
1591 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001592 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001593 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001594 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001595 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001596 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (xp == NULL)
1599 return -ENOENT;
1600
1601 if (!delete) {
1602 struct sk_buff *resp_skb;
1603
1604 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1605 if (IS_ERR(resp_skb)) {
1606 err = PTR_ERR(resp_skb);
1607 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001608 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001609 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001611 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001612 uid_t loginuid = audit_get_loginuid(current);
1613 u32 sessionid = audit_get_sessionid(current);
1614 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001615
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001616 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001617 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1618 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001619
1620 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001621 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001622
Herbert Xue7443892005-06-18 22:44:18 -07001623 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001624 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001625 c.seq = nlh->nlmsg_seq;
1626 c.pid = nlh->nlmsg_pid;
1627 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001630out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001631 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return err;
1633}
1634
Christoph Hellwig22e70052007-01-02 15:22:30 -08001635static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001636 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001638 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001639 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001640 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001641 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001642 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001644 audit_info.loginuid = audit_get_loginuid(current);
1645 audit_info.sessionid = audit_get_sessionid(current);
1646 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001647 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001648 if (err) {
1649 if (err == -ESRCH) /* empty table */
1650 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001651 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001652 }
Herbert Xubf088672005-06-18 22:44:00 -07001653 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001654 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001655 c.seq = nlh->nlmsg_seq;
1656 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001657 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001658 km_state_notify(NULL, &c);
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return 0;
1661}
1662
Steffen Klassertd8647b72011-03-08 00:10:27 +00001663static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001664{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001665 size_t replay_size = x->replay_esn ?
1666 xfrm_replay_state_esn_len(x->replay_esn) :
1667 sizeof(struct xfrm_replay_state);
1668
Thomas Graf7deb2262007-08-22 13:57:39 -07001669 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001670 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001671 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001672 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001673 + nla_total_size(4) /* XFRM_AE_RTHR */
1674 + nla_total_size(4); /* XFRM_AE_ETHR */
1675}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001676
David S. Miller214e0052011-02-24 00:02:38 -05001677static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001678{
1679 struct xfrm_aevent_id *id;
1680 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001681
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001682 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1683 if (nlh == NULL)
1684 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001685
Thomas Graf7b67c852007-08-22 13:53:52 -07001686 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001687 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001688 id->sa_id.spi = x->id.spi;
1689 id->sa_id.family = x->props.family;
1690 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001691 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1692 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001693 id->flags = c->data.aevent;
1694
Steffen Klassertd8647b72011-03-08 00:10:27 +00001695 if (x->replay_esn)
1696 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1697 xfrm_replay_state_esn_len(x->replay_esn),
1698 x->replay_esn);
1699 else
1700 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1701
Thomas Grafc0144be2007-08-22 13:55:43 -07001702 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001703
Thomas Grafc0144be2007-08-22 13:55:43 -07001704 if (id->flags & XFRM_AE_RTHR)
1705 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001706
Thomas Grafc0144be2007-08-22 13:55:43 -07001707 if (id->flags & XFRM_AE_ETHR)
1708 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1709 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001710
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001711 if (xfrm_mark_put(skb, &x->mark))
1712 goto nla_put_failure;
1713
Thomas Graf98250692007-08-22 12:47:26 -07001714 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001715
Thomas Grafc0144be2007-08-22 13:55:43 -07001716nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001717 nlmsg_cancel(skb, nlh);
1718 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001719}
1720
Christoph Hellwig22e70052007-01-02 15:22:30 -08001721static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001722 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001723{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001724 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001725 struct xfrm_state *x;
1726 struct sk_buff *r_skb;
1727 int err;
1728 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001729 u32 mark;
1730 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001731 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001732 struct xfrm_usersa_id *id = &p->sa_id;
1733
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001734 mark = xfrm_mark_get(attrs, &m);
1735
1736 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001737 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001738 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001739
1740 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1741 if (r_skb == NULL) {
1742 xfrm_state_put(x);
1743 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001744 }
1745
1746 /*
1747 * XXX: is this lock really needed - none of the other
1748 * gets lock (the concern is things getting updated
1749 * while we are still reading) - jhs
1750 */
1751 spin_lock_bh(&x->lock);
1752 c.data.aevent = p->flags;
1753 c.seq = nlh->nlmsg_seq;
1754 c.pid = nlh->nlmsg_pid;
1755
1756 if (build_aevent(r_skb, x, &c) < 0)
1757 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001758 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001759 spin_unlock_bh(&x->lock);
1760 xfrm_state_put(x);
1761 return err;
1762}
1763
Christoph Hellwig22e70052007-01-02 15:22:30 -08001764static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001765 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001766{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001767 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001768 struct xfrm_state *x;
1769 struct km_event c;
1770 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001771 u32 mark = 0;
1772 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001773 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001774 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001775 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001776 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001777
Steffen Klassertd8647b72011-03-08 00:10:27 +00001778 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001779 return err;
1780
1781 /* pedantic mode - thou shalt sayeth replaceth */
1782 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1783 return err;
1784
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001785 mark = xfrm_mark_get(attrs, &m);
1786
1787 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 -08001788 if (x == NULL)
1789 return -ESRCH;
1790
1791 if (x->km.state != XFRM_STATE_VALID)
1792 goto out;
1793
Steffen Klasserte2b19122011-03-28 19:47:30 +00001794 err = xfrm_replay_verify_len(x->replay_esn, rp);
1795 if (err)
1796 goto out;
1797
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001798 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001799 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001800 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001801
1802 c.event = nlh->nlmsg_type;
1803 c.seq = nlh->nlmsg_seq;
1804 c.pid = nlh->nlmsg_pid;
1805 c.data.aevent = XFRM_AE_CU;
1806 km_state_notify(x, &c);
1807 err = 0;
1808out:
1809 xfrm_state_put(x);
1810 return err;
1811}
1812
Christoph Hellwig22e70052007-01-02 15:22:30 -08001813static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001814 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001816 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001817 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001818 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001819 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001820 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001821
Thomas Graf35a7aa02007-08-22 14:00:40 -07001822 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001823 if (err)
1824 return err;
1825
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001826 audit_info.loginuid = audit_get_loginuid(current);
1827 audit_info.sessionid = audit_get_sessionid(current);
1828 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001829 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001830 if (err) {
1831 if (err == -ESRCH) /* empty table */
1832 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001833 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001834 }
1835
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001836 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001837 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001838 c.seq = nlh->nlmsg_seq;
1839 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001840 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001841 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return 0;
1843}
1844
Christoph Hellwig22e70052007-01-02 15:22:30 -08001845static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001846 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001847{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001848 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001849 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001850 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001851 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001852 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001853 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001854 struct xfrm_mark m;
1855 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001856
Thomas Graf35a7aa02007-08-22 14:00:40 -07001857 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001858 if (err)
1859 return err;
1860
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001861 err = verify_policy_dir(p->dir);
1862 if (err)
1863 return err;
1864
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001865 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001866 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001867 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001868 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001869 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001870
Thomas Graf35a7aa02007-08-22 14:00:40 -07001871 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001872 if (err)
1873 return err;
1874
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001875 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001876 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001877 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001878
Paul Moore03e1ad72008-04-12 19:07:52 -07001879 err = security_xfrm_policy_alloc(&ctx, uctx);
1880 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001881 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001882 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001883 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001884 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001885 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001886 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001887 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001888 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001889
Timo Teräsea2dea92010-03-31 00:17:05 +00001890 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001891 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001892
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001893 err = 0;
1894 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001895 uid_t loginuid = audit_get_loginuid(current);
1896 u32 sessionid = audit_get_sessionid(current);
1897 u32 sid;
1898
1899 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001900 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001901 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001902
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001903 } else {
1904 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001905 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001906 }
1907 km_policy_expired(xp, p->dir, up->hard, current->pid);
1908
1909out:
1910 xfrm_pol_put(xp);
1911 return err;
1912}
1913
Christoph Hellwig22e70052007-01-02 15:22:30 -08001914static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001915 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001916{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001917 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001918 struct xfrm_state *x;
1919 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001920 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001921 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001922 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001923 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001924
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001925 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 -08001926
David S. Miller3a765aa2007-02-26 14:52:21 -08001927 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001928 if (x == NULL)
1929 return err;
1930
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001931 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001932 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001933 if (x->km.state != XFRM_STATE_VALID)
1934 goto out;
1935 km_state_expired(x, ue->hard, current->pid);
1936
Joy Latten161a09e2006-11-27 13:11:54 -06001937 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001938 uid_t loginuid = audit_get_loginuid(current);
1939 u32 sessionid = audit_get_sessionid(current);
1940 u32 sid;
1941
1942 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001943 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001944 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001945 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001946 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001947out:
1948 spin_unlock_bh(&x->lock);
1949 xfrm_state_put(x);
1950 return err;
1951}
1952
Christoph Hellwig22e70052007-01-02 15:22:30 -08001953static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001954 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001955{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001956 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001957 struct xfrm_policy *xp;
1958 struct xfrm_user_tmpl *ut;
1959 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001960 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001961 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001962
Thomas Graf7b67c852007-08-22 13:53:52 -07001963 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001964 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001965 int err = -ENOMEM;
1966
1967 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001968 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001969
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001970 xfrm_mark_get(attrs, &mark);
1971
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001972 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001973 if (err)
1974 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001975
1976 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001977 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001978 if (!xp)
1979 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001980
1981 memcpy(&x->id, &ua->id, sizeof(ua->id));
1982 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1983 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001984 xp->mark.m = x->mark.m = mark.m;
1985 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001986 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001987 /* extract the templates and for each call km_key */
1988 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1989 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1990 memcpy(&x->id, &t->id, sizeof(x->id));
1991 x->props.mode = t->mode;
1992 x->props.reqid = t->reqid;
1993 x->props.family = ut->family;
1994 t->aalgos = ua->aalgos;
1995 t->ealgos = ua->ealgos;
1996 t->calgos = ua->calgos;
1997 err = km_query(x, t, xp);
1998
1999 }
2000
2001 kfree(x);
2002 kfree(xp);
2003
2004 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002005
2006bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002007 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002008free_state:
2009 kfree(x);
2010nomem:
2011 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002012}
2013
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002014#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002015static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002016 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002017 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002018{
Thomas Graf5424f322007-08-22 14:01:33 -07002019 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002020 struct xfrm_user_migrate *um;
2021 int i, num_migrate;
2022
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002023 if (k != NULL) {
2024 struct xfrm_user_kmaddress *uk;
2025
2026 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2027 memcpy(&k->local, &uk->local, sizeof(k->local));
2028 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2029 k->family = uk->family;
2030 k->reserved = uk->reserved;
2031 }
2032
Thomas Graf5424f322007-08-22 14:01:33 -07002033 um = nla_data(rt);
2034 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002035
2036 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2037 return -EINVAL;
2038
2039 for (i = 0; i < num_migrate; i++, um++, ma++) {
2040 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2041 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2042 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2043 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2044
2045 ma->proto = um->proto;
2046 ma->mode = um->mode;
2047 ma->reqid = um->reqid;
2048
2049 ma->old_family = um->old_family;
2050 ma->new_family = um->new_family;
2051 }
2052
2053 *num = i;
2054 return 0;
2055}
2056
2057static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002058 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002059{
Thomas Graf7b67c852007-08-22 13:53:52 -07002060 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002061 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002062 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002063 u8 type;
2064 int err;
2065 int n = 0;
2066
Thomas Graf35a7aa02007-08-22 14:00:40 -07002067 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002068 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002069
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002070 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2071
Thomas Graf5424f322007-08-22 14:01:33 -07002072 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002073 if (err)
2074 return err;
2075
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002076 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002077 if (err)
2078 return err;
2079
2080 if (!n)
2081 return 0;
2082
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002083 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002084
2085 return 0;
2086}
2087#else
2088static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002089 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002090{
2091 return -ENOPROTOOPT;
2092}
2093#endif
2094
2095#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002096static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002097{
2098 struct xfrm_user_migrate um;
2099
2100 memset(&um, 0, sizeof(um));
2101 um.proto = m->proto;
2102 um.mode = m->mode;
2103 um.reqid = m->reqid;
2104 um.old_family = m->old_family;
2105 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2106 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2107 um.new_family = m->new_family;
2108 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2109 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2110
Thomas Grafc0144be2007-08-22 13:55:43 -07002111 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002112}
2113
David S. Miller183cad12011-02-24 00:28:01 -05002114static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002115{
2116 struct xfrm_user_kmaddress uk;
2117
2118 memset(&uk, 0, sizeof(uk));
2119 uk.family = k->family;
2120 uk.reserved = k->reserved;
2121 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002122 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002123
2124 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2125}
2126
2127static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002128{
2129 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002130 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2131 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2132 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002133}
2134
David S. Miller183cad12011-02-24 00:28:01 -05002135static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2136 int num_migrate, const struct xfrm_kmaddress *k,
2137 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002138{
David S. Miller183cad12011-02-24 00:28:01 -05002139 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002140 struct xfrm_userpolicy_id *pol_id;
2141 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002142 int i;
2143
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002144 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2145 if (nlh == NULL)
2146 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002147
Thomas Graf7b67c852007-08-22 13:53:52 -07002148 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002149 /* copy data from selector, dir, and type to the pol_id */
2150 memset(pol_id, 0, sizeof(*pol_id));
2151 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2152 pol_id->dir = dir;
2153
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002154 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2155 goto nlmsg_failure;
2156
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002157 if (copy_to_user_policy_type(type, skb) < 0)
2158 goto nlmsg_failure;
2159
2160 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2161 if (copy_to_user_migrate(mp, skb) < 0)
2162 goto nlmsg_failure;
2163 }
2164
Thomas Graf98250692007-08-22 12:47:26 -07002165 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002166nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002167 nlmsg_cancel(skb, nlh);
2168 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002169}
2170
David S. Miller183cad12011-02-24 00:28:01 -05002171static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2172 const struct xfrm_migrate *m, int num_migrate,
2173 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002174{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002175 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002176 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002177
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002178 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002179 if (skb == NULL)
2180 return -ENOMEM;
2181
2182 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002183 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002184 BUG();
2185
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002186 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002187}
2188#else
David S. Miller183cad12011-02-24 00:28:01 -05002189static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2190 const struct xfrm_migrate *m, int num_migrate,
2191 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002192{
2193 return -ENOPROTOOPT;
2194}
2195#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002196
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002197#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002198
2199static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002200 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002201 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2202 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2203 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2204 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2205 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2206 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002207 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002208 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002209 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002210 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002211 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002212 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002213 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002214 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2215 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002216 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002217 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002218 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2219 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220};
2221
Thomas Graf492b5582005-05-03 14:26:40 -07002222#undef XMSGSIZE
2223
Thomas Grafcf5cb792007-08-22 13:59:04 -07002224static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002225 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2226 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2227 [XFRMA_LASTUSED] = { .type = NLA_U64},
2228 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002229 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002230 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2231 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2232 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2233 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2234 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2235 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2236 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2237 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2238 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2239 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2240 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2241 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2242 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2243 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002244 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002245 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002246 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002247 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002248};
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002251 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002253 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002254} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2255 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2256 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2257 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002258 .dump = xfrm_dump_sa,
2259 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002260 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2261 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2262 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002263 .dump = xfrm_dump_policy,
2264 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002265 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002266 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002267 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002268 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2269 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002270 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002271 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2272 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002273 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2274 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002275 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002276 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002277 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278};
2279
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002280static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002282 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002283 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002285 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 type -= XFRM_MSG_BASE;
2292 link = &xfrm_dispatch[type];
2293
2294 /* All operations require privileges, even GET */
Eric Parisfd778462012-01-03 12:25:16 -05002295 if (!capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002296 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Thomas Graf492b5582005-05-03 14:26:40 -07002298 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2299 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002300 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002302 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002304 {
2305 struct netlink_dump_control c = {
2306 .dump = link->dump,
2307 .done = link->done,
2308 };
2309 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
Thomas Graf35a7aa02007-08-22 14:00:40 -07002313 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002314 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002315 if (err < 0)
2316 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002319 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Thomas Graf5424f322007-08-22 14:01:33 -07002321 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002324static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002326 mutex_lock(&xfrm_cfg_mutex);
2327 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2328 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Thomas Graf7deb2262007-08-22 13:57:39 -07002331static inline size_t xfrm_expire_msgsize(void)
2332{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002333 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2334 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002335}
2336
David S. Miller214e0052011-02-24 00:02:38 -05002337static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 struct xfrm_user_expire *ue;
2340 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002342 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2343 if (nlh == NULL)
2344 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Thomas Graf7b67c852007-08-22 13:53:52 -07002346 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002348 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002350 if (xfrm_mark_put(skb, &x->mark))
2351 goto nla_put_failure;
2352
Thomas Graf98250692007-08-22 12:47:26 -07002353 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002354
2355nla_put_failure:
2356 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
David S. Miller214e0052011-02-24 00:02:38 -05002359static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002361 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct sk_buff *skb;
2363
Thomas Graf7deb2262007-08-22 13:57:39 -07002364 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (skb == NULL)
2366 return -ENOMEM;
2367
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002368 if (build_expire(skb, x, c) < 0) {
2369 kfree_skb(skb);
2370 return -EMSGSIZE;
2371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002373 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
David S. Miller214e0052011-02-24 00:02:38 -05002376static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002377{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002378 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002379 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002380
Steffen Klassertd8647b72011-03-08 00:10:27 +00002381 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002382 if (skb == NULL)
2383 return -ENOMEM;
2384
2385 if (build_aevent(skb, x, c) < 0)
2386 BUG();
2387
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002388 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002389}
2390
David S. Miller214e0052011-02-24 00:02:38 -05002391static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002392{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002393 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394 struct xfrm_usersa_flush *p;
2395 struct nlmsghdr *nlh;
2396 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002397 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002398
Thomas Graf7deb2262007-08-22 13:57:39 -07002399 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002400 if (skb == NULL)
2401 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002402
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002403 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2404 if (nlh == NULL) {
2405 kfree_skb(skb);
2406 return -EMSGSIZE;
2407 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002408
Thomas Graf7b67c852007-08-22 13:53:52 -07002409 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002410 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002411
Thomas Graf98250692007-08-22 12:47:26 -07002412 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002414 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415}
2416
Thomas Graf7deb2262007-08-22 13:57:39 -07002417static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418{
Thomas Graf7deb2262007-08-22 13:57:39 -07002419 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002420 if (x->aead)
2421 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002422 if (x->aalg) {
2423 l += nla_total_size(sizeof(struct xfrm_algo) +
2424 (x->aalg->alg_key_len + 7) / 8);
2425 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2426 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002427 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002428 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002429 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002430 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002431 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002432 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002433 if (x->tfcpad)
2434 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002435 if (x->replay_esn)
2436 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002437 if (x->security)
2438 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2439 x->security->ctx_len);
2440 if (x->coaddr)
2441 l += nla_total_size(sizeof(*x->coaddr));
2442
Herbert Xud26f3982007-11-13 21:47:08 -08002443 /* Must count x->lastused as it may become non-zero behind our back. */
2444 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002445
2446 return l;
2447}
2448
David S. Miller214e0052011-02-24 00:02:38 -05002449static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002450{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002451 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002452 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002453 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002454 struct nlmsghdr *nlh;
2455 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002457 int headlen;
2458
2459 headlen = sizeof(*p);
2460 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002461 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002462 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002463 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002464 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002465 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002466
Thomas Graf7deb2262007-08-22 13:57:39 -07002467 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002468 if (skb == NULL)
2469 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002470
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002471 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2472 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002473 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002474
Thomas Graf7b67c852007-08-22 13:53:52 -07002475 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002476 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002477 struct nlattr *attr;
2478
Thomas Graf7b67c852007-08-22 13:53:52 -07002479 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002480 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2481 id->spi = x->id.spi;
2482 id->family = x->props.family;
2483 id->proto = x->id.proto;
2484
Thomas Grafc0144be2007-08-22 13:55:43 -07002485 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2486 if (attr == NULL)
2487 goto nla_put_failure;
2488
2489 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002490 }
2491
Herbert Xu68325d32007-10-09 13:30:57 -07002492 if (copy_to_user_state_extra(x, p, skb))
2493 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002494
Thomas Graf98250692007-08-22 12:47:26 -07002495 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002496
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002497 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002498
Thomas Grafc0144be2007-08-22 13:55:43 -07002499nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002500 /* Somebody screwed up with xfrm_sa_len! */
2501 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002502 kfree_skb(skb);
2503 return -1;
2504}
2505
David S. Miller214e0052011-02-24 00:02:38 -05002506static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002507{
2508
2509 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002510 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002511 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002512 case XFRM_MSG_NEWAE:
2513 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002514 case XFRM_MSG_DELSA:
2515 case XFRM_MSG_UPDSA:
2516 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002517 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002518 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002519 return xfrm_notify_sa_flush(c);
2520 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002521 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2522 c->event);
2523 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002524 }
2525
2526 return 0;
2527
2528}
2529
Thomas Graf7deb2262007-08-22 13:57:39 -07002530static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2531 struct xfrm_policy *xp)
2532{
2533 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2534 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002535 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002536 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2537 + userpolicy_type_attrsize();
2538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2541 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2542 int dir)
2543{
2544 struct xfrm_user_acquire *ua;
2545 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 __u32 seq = xfrm_get_acqseq();
2547
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002548 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2549 if (nlh == NULL)
2550 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Thomas Graf7b67c852007-08-22 13:53:52 -07002552 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 memcpy(&ua->id, &x->id, sizeof(ua->id));
2554 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2555 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2556 copy_to_user_policy(xp, &ua->policy, dir);
2557 ua->aalgos = xt->aalgos;
2558 ua->ealgos = xt->ealgos;
2559 ua->calgos = xt->calgos;
2560 ua->seq = x->km.seq = seq;
2561
2562 if (copy_to_user_tmpl(xp, skb) < 0)
2563 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002564 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002565 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002566 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002567 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002568 if (xfrm_mark_put(skb, &xp->mark))
2569 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Thomas Graf98250692007-08-22 12:47:26 -07002571 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002573nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002575 nlmsg_cancel(skb, nlh);
2576 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
2579static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2580 struct xfrm_policy *xp, int dir)
2581{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002582 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Thomas Graf7deb2262007-08-22 13:57:39 -07002585 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (skb == NULL)
2587 return -ENOMEM;
2588
2589 if (build_acquire(skb, x, xt, xp, dir) < 0)
2590 BUG();
2591
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002592 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
2595/* User gives us xfrm_user_policy_info followed by an array of 0
2596 * or more templates.
2597 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002598static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 u8 *data, int len, int *dir)
2600{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002601 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2603 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2604 struct xfrm_policy *xp;
2605 int nr;
2606
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002607 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 case AF_INET:
2609 if (opt != IP_XFRM_POLICY) {
2610 *dir = -EOPNOTSUPP;
2611 return NULL;
2612 }
2613 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002614#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 case AF_INET6:
2616 if (opt != IPV6_XFRM_POLICY) {
2617 *dir = -EOPNOTSUPP;
2618 return NULL;
2619 }
2620 break;
2621#endif
2622 default:
2623 *dir = -EINVAL;
2624 return NULL;
2625 }
2626
2627 *dir = -EINVAL;
2628
2629 if (len < sizeof(*p) ||
2630 verify_newpolicy_info(p))
2631 return NULL;
2632
2633 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002634 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return NULL;
2636
Herbert Xua4f1bac2005-07-26 15:43:17 -07002637 if (p->dir > XFRM_POLICY_OUT)
2638 return NULL;
2639
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002640 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (xp == NULL) {
2642 *dir = -ENOBUFS;
2643 return NULL;
2644 }
2645
2646 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002647 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 copy_templates(xp, ut, nr);
2649
2650 *dir = p->dir;
2651
2652 return xp;
2653}
2654
Thomas Graf7deb2262007-08-22 13:57:39 -07002655static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2656{
2657 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2658 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2659 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002660 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002661 + userpolicy_type_attrsize();
2662}
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002665 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
2667 struct xfrm_user_polexpire *upe;
2668 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002669 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002671 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2672 if (nlh == NULL)
2673 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Thomas Graf7b67c852007-08-22 13:53:52 -07002675 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 copy_to_user_policy(xp, &upe->pol, dir);
2677 if (copy_to_user_tmpl(xp, skb) < 0)
2678 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002679 if (copy_to_user_sec_ctx(xp, skb))
2680 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002681 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002682 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002683 if (xfrm_mark_put(skb, &xp->mark))
2684 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 upe->hard = !!hard;
2686
Thomas Graf98250692007-08-22 12:47:26 -07002687 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002689nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002691 nlmsg_cancel(skb, nlh);
2692 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
David S. Miller214e0052011-02-24 00:02:38 -05002695static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002697 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Thomas Graf7deb2262007-08-22 13:57:39 -07002700 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (skb == NULL)
2702 return -ENOMEM;
2703
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002704 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 BUG();
2706
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002707 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
David S. Miller214e0052011-02-24 00:02:38 -05002710static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002711{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002712 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002713 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002714 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002715 struct nlmsghdr *nlh;
2716 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002717 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002718 int headlen;
2719
2720 headlen = sizeof(*p);
2721 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002722 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002723 headlen = sizeof(*id);
2724 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002725 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002726 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002727 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002728
Thomas Graf7deb2262007-08-22 13:57:39 -07002729 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002730 if (skb == NULL)
2731 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002732
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002733 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2734 if (nlh == NULL)
2735 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002736
Thomas Graf7b67c852007-08-22 13:53:52 -07002737 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002738 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002739 struct nlattr *attr;
2740
Thomas Graf7b67c852007-08-22 13:53:52 -07002741 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002742 memset(id, 0, sizeof(*id));
2743 id->dir = dir;
2744 if (c->data.byid)
2745 id->index = xp->index;
2746 else
2747 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2748
Thomas Grafc0144be2007-08-22 13:55:43 -07002749 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2750 if (attr == NULL)
2751 goto nlmsg_failure;
2752
2753 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002754 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002755
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002756 copy_to_user_policy(xp, p, dir);
2757 if (copy_to_user_tmpl(xp, skb) < 0)
2758 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002759 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002760 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002761
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002762 if (xfrm_mark_put(skb, &xp->mark))
2763 goto nla_put_failure;
2764
Thomas Graf98250692007-08-22 12:47:26 -07002765 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002766
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002767 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002768
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002769nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002770nlmsg_failure:
2771 kfree_skb(skb);
2772 return -1;
2773}
2774
David S. Miller214e0052011-02-24 00:02:38 -05002775static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002776{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002777 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002778 struct nlmsghdr *nlh;
2779 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002780
Thomas Graf7deb2262007-08-22 13:57:39 -07002781 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002782 if (skb == NULL)
2783 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002784
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002785 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2786 if (nlh == NULL)
2787 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002788 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2789 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002790
Thomas Graf98250692007-08-22 12:47:26 -07002791 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002792
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002793 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002794
2795nlmsg_failure:
2796 kfree_skb(skb);
2797 return -1;
2798}
2799
David S. Miller214e0052011-02-24 00:02:38 -05002800static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002801{
2802
2803 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002804 case XFRM_MSG_NEWPOLICY:
2805 case XFRM_MSG_UPDPOLICY:
2806 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002807 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002808 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002809 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002810 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002811 return xfrm_exp_policy_notify(xp, dir, c);
2812 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002813 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2814 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002815 }
2816
2817 return 0;
2818
2819}
2820
Thomas Graf7deb2262007-08-22 13:57:39 -07002821static inline size_t xfrm_report_msgsize(void)
2822{
2823 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2824}
2825
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002826static int build_report(struct sk_buff *skb, u8 proto,
2827 struct xfrm_selector *sel, xfrm_address_t *addr)
2828{
2829 struct xfrm_user_report *ur;
2830 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002831
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002832 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2833 if (nlh == NULL)
2834 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002835
Thomas Graf7b67c852007-08-22 13:53:52 -07002836 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002837 ur->proto = proto;
2838 memcpy(&ur->sel, sel, sizeof(ur->sel));
2839
2840 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002841 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002842
Thomas Graf98250692007-08-22 12:47:26 -07002843 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002844
Thomas Grafc0144be2007-08-22 13:55:43 -07002845nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002846 nlmsg_cancel(skb, nlh);
2847 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002848}
2849
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002850static int xfrm_send_report(struct net *net, u8 proto,
2851 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002852{
2853 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002854
Thomas Graf7deb2262007-08-22 13:57:39 -07002855 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002856 if (skb == NULL)
2857 return -ENOMEM;
2858
2859 if (build_report(skb, proto, sel, addr) < 0)
2860 BUG();
2861
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002862 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002863}
2864
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002865static inline size_t xfrm_mapping_msgsize(void)
2866{
2867 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2868}
2869
2870static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2871 xfrm_address_t *new_saddr, __be16 new_sport)
2872{
2873 struct xfrm_user_mapping *um;
2874 struct nlmsghdr *nlh;
2875
2876 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2877 if (nlh == NULL)
2878 return -EMSGSIZE;
2879
2880 um = nlmsg_data(nlh);
2881
2882 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2883 um->id.spi = x->id.spi;
2884 um->id.family = x->props.family;
2885 um->id.proto = x->id.proto;
2886 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2887 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2888 um->new_sport = new_sport;
2889 um->old_sport = x->encap->encap_sport;
2890 um->reqid = x->props.reqid;
2891
2892 return nlmsg_end(skb, nlh);
2893}
2894
2895static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2896 __be16 sport)
2897{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002898 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002899 struct sk_buff *skb;
2900
2901 if (x->id.proto != IPPROTO_ESP)
2902 return -EINVAL;
2903
2904 if (!x->encap)
2905 return -EINVAL;
2906
2907 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2908 if (skb == NULL)
2909 return -ENOMEM;
2910
2911 if (build_mapping(skb, x, ipaddr, sport) < 0)
2912 BUG();
2913
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002914 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002915}
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917static struct xfrm_mgr netlink_mgr = {
2918 .id = "netlink",
2919 .notify = xfrm_send_state_notify,
2920 .acquire = xfrm_send_acquire,
2921 .compile_policy = xfrm_compile_policy,
2922 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002923 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002924 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002925 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926};
2927
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002928static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Patrick McHardybe336902006-03-20 22:40:54 -08002930 struct sock *nlsk;
2931
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002932 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002933 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002934 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002936 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00002937 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return 0;
2939}
2940
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002941static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002942{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002943 struct net *net;
2944 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002945 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002946 synchronize_net();
2947 list_for_each_entry(net, net_exit_list, exit_list)
2948 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002949}
2950
2951static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002952 .init = xfrm_user_net_init,
2953 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002954};
2955
2956static int __init xfrm_user_init(void)
2957{
2958 int rv;
2959
2960 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2961
2962 rv = register_pernet_subsys(&xfrm_user_net_ops);
2963 if (rv < 0)
2964 return rv;
2965 rv = xfrm_register_km(&netlink_mgr);
2966 if (rv < 0)
2967 unregister_pernet_subsys(&xfrm_user_net_ops);
2968 return rv;
2969}
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971static void __exit xfrm_user_exit(void)
2972{
2973 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002974 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
2977module_init(xfrm_user_init);
2978module_exit(xfrm_user_exit);
2979MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002980MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002981