blob: c8b903df943ff44b64f9e61d378837fb95386ba5 [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];
Mathias Krause53bf1462012-09-20 10:01:49 +0000126 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000127
Mathias Krause53bf1462012-09-20 10:01:49 +0000128 if (p->flags & XFRM_STATE_ESN) {
129 if (!rt)
130 return -EINVAL;
131
132 rs = nla_data(rt);
133
134 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
135 return -EINVAL;
136
137 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
138 nla_len(rt) != sizeof(*rs))
139 return -EINVAL;
140 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000141
Steffen Klassertd8647b72011-03-08 00:10:27 +0000142 if (!rt)
143 return 0;
144
Steffen Klassert02aadf72011-03-28 19:48:09 +0000145 if (p->id.proto != IPPROTO_ESP)
146 return -EINVAL;
147
Steffen Klassertd8647b72011-03-08 00:10:27 +0000148 if (p->replay_window != 0)
149 return -EINVAL;
150
151 return 0;
152}
Trent Jaegerdf718372005-12-13 23:12:27 -0800153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700155 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int err;
158
159 err = -EINVAL;
160 switch (p->family) {
161 case AF_INET:
162 break;
163
164 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000165#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break;
167#else
168 err = -EAFNOSUPPORT;
169 goto out;
170#endif
171
172 default:
173 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 err = -EINVAL;
177 switch (p->id.proto) {
178 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000179 if ((!attrs[XFRMA_ALG_AUTH] &&
180 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800181 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700182 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000183 attrs[XFRMA_ALG_COMP] ||
184 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto out;
186 break;
187
188 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800189 if (attrs[XFRMA_ALG_COMP])
190 goto out;
191 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000192 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800193 !attrs[XFRMA_ALG_CRYPT] &&
194 !attrs[XFRMA_ALG_AEAD])
195 goto out;
196 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000197 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800198 attrs[XFRMA_ALG_CRYPT]) &&
199 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000201 if (attrs[XFRMA_TFCPAD] &&
202 p->mode != XFRM_MODE_TUNNEL)
203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205
206 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800208 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700209 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000210 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000211 attrs[XFRMA_ALG_CRYPT] ||
212 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto out;
214 break;
215
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000216#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700217 case IPPROTO_DSTOPTS:
218 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700219 if (attrs[XFRMA_ALG_COMP] ||
220 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000221 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800222 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700223 attrs[XFRMA_ALG_CRYPT] ||
224 attrs[XFRMA_ENCAP] ||
225 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000226 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700227 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700228 goto out;
229 break;
230#endif
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 default:
233 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Herbert Xu1a6509d2008-01-28 19:37:29 -0800236 if ((err = verify_aead(attrs)))
237 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000238 if ((err = verify_auth_trunc(attrs)))
239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700246 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800247 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000248 if ((err = verify_replay(p, attrs)))
249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 err = -EINVAL;
252 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700253 case XFRM_MODE_TRANSPORT:
254 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700255 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700256 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
258
259 default:
260 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 err = 0;
264
265out:
266 return err;
267}
268
269static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800270 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700271 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct xfrm_algo *p, *ualg;
274 struct xfrm_algo_desc *algo;
275
276 if (!rta)
277 return 0;
278
Thomas Graf5424f322007-08-22 14:01:33 -0700279 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 algo = get_byname(ualg->alg_name, 1);
282 if (!algo)
283 return -ENOSYS;
284 *props = algo->desc.sadb_alg_id;
285
Eric Dumazet0f99be02008-01-08 23:39:06 -0800286 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!p)
288 return -ENOMEM;
289
Herbert Xu04ff1262006-08-13 08:50:00 +1000290 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *algpp = p;
292 return 0;
293}
294
Martin Willi4447bb32009-11-25 00:29:52 +0000295static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
296 struct nlattr *rta)
297{
298 struct xfrm_algo *ualg;
299 struct xfrm_algo_auth *p;
300 struct xfrm_algo_desc *algo;
301
302 if (!rta)
303 return 0;
304
305 ualg = nla_data(rta);
306
307 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
308 if (!algo)
309 return -ENOSYS;
310 *props = algo->desc.sadb_alg_id;
311
312 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
313 if (!p)
314 return -ENOMEM;
315
316 strcpy(p->alg_name, algo->name);
317 p->alg_key_len = ualg->alg_key_len;
318 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
319 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
320
321 *algpp = p;
322 return 0;
323}
324
325static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
326 struct nlattr *rta)
327{
328 struct xfrm_algo_auth *p, *ualg;
329 struct xfrm_algo_desc *algo;
330
331 if (!rta)
332 return 0;
333
334 ualg = nla_data(rta);
335
336 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
337 if (!algo)
338 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000339 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000341 return -EINVAL;
342 *props = algo->desc.sadb_alg_id;
343
344 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
345 if (!p)
346 return -ENOMEM;
347
348 strcpy(p->alg_name, algo->name);
349 if (!p->alg_trunc_len)
350 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
351
352 *algpp = p;
353 return 0;
354}
355
Herbert Xu1a6509d2008-01-28 19:37:29 -0800356static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
357 struct nlattr *rta)
358{
359 struct xfrm_algo_aead *p, *ualg;
360 struct xfrm_algo_desc *algo;
361
362 if (!rta)
363 return 0;
364
365 ualg = nla_data(rta);
366
367 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
368 if (!algo)
369 return -ENOSYS;
370 *props = algo->desc.sadb_alg_id;
371
372 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
373 if (!p)
374 return -ENOMEM;
375
376 strcpy(p->alg_name, algo->name);
377 *algpp = p;
378 return 0;
379}
380
Steffen Klasserte2b19122011-03-28 19:47:30 +0000381static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
382 struct nlattr *rp)
383{
384 struct xfrm_replay_state_esn *up;
Mathias Krause53bf1462012-09-20 10:01:49 +0000385 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000386
387 if (!replay_esn || !rp)
388 return 0;
389
390 up = nla_data(rp);
Mathias Krause53bf1462012-09-20 10:01:49 +0000391 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000392
Mathias Krause53bf1462012-09-20 10:01:49 +0000393 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000394 return -EINVAL;
395
396 return 0;
397}
398
Steffen Klassertd8647b72011-03-08 00:10:27 +0000399static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
400 struct xfrm_replay_state_esn **preplay_esn,
401 struct nlattr *rta)
402{
403 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krause53bf1462012-09-20 10:01:49 +0000404 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000405
406 if (!rta)
407 return 0;
408
409 up = nla_data(rta);
Mathias Krause53bf1462012-09-20 10:01:49 +0000410 klen = xfrm_replay_state_esn_len(up);
411 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000412
Mathias Krause53bf1462012-09-20 10:01:49 +0000413 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000414 if (!p)
415 return -ENOMEM;
416
Mathias Krause53bf1462012-09-20 10:01:49 +0000417 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000418 if (!pp) {
419 kfree(p);
420 return -ENOMEM;
421 }
422
Mathias Krause53bf1462012-09-20 10:01:49 +0000423 memcpy(p, up, ulen);
424 memcpy(pp, up, ulen);
425
Steffen Klassertd8647b72011-03-08 00:10:27 +0000426 *replay_esn = p;
427 *preplay_esn = pp;
428
429 return 0;
430}
431
Joy Latten661697f2007-04-13 16:14:35 -0700432static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800433{
Trent Jaegerdf718372005-12-13 23:12:27 -0800434 int len = 0;
435
436 if (xfrm_ctx) {
437 len += sizeof(struct xfrm_user_sec_ctx);
438 len += xfrm_ctx->ctx_len;
439 }
440 return len;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
444{
445 memcpy(&x->id, &p->id, sizeof(x->id));
446 memcpy(&x->sel, &p->sel, sizeof(x->sel));
447 memcpy(&x->lft, &p->lft, sizeof(x->lft));
448 x->props.mode = p->mode;
449 x->props.replay_window = p->replay_window;
450 x->props.reqid = p->reqid;
451 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700452 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700454
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700455 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700456 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800459/*
460 * someday when pfkey also has support, we could have the code
461 * somehow made shareable and move it to xfrm_state.c - JHS
462 *
463*/
Mathias Krause743b9112012-09-19 11:33:43 +0000464static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
465 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800466{
Thomas Graf5424f322007-08-22 14:01:33 -0700467 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krause743b9112012-09-19 11:33:43 +0000468 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700469 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
470 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
471 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800472
Steffen Klassertd8647b72011-03-08 00:10:27 +0000473 if (re) {
474 struct xfrm_replay_state_esn *replay_esn;
475 replay_esn = nla_data(re);
476 memcpy(x->replay_esn, replay_esn,
477 xfrm_replay_state_esn_len(replay_esn));
478 memcpy(x->preplay_esn, replay_esn,
479 xfrm_replay_state_esn_len(replay_esn));
480 }
481
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800482 if (rp) {
483 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700484 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800485 memcpy(&x->replay, replay, sizeof(*replay));
486 memcpy(&x->preplay, replay, sizeof(*replay));
487 }
488
489 if (lt) {
490 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700491 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800492 x->curlft.bytes = ltime->bytes;
493 x->curlft.packets = ltime->packets;
494 x->curlft.add_time = ltime->add_time;
495 x->curlft.use_time = ltime->use_time;
496 }
497
Thomas Grafcf5cb792007-08-22 13:59:04 -0700498 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700499 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800500
Thomas Grafcf5cb792007-08-22 13:59:04 -0700501 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700502 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800503}
504
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800505static struct xfrm_state *xfrm_state_construct(struct net *net,
506 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700507 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int *errp)
509{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800510 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int err = -ENOMEM;
512
513 if (!x)
514 goto error_no_put;
515
516 copy_from_user_state(x, p);
517
Herbert Xu1a6509d2008-01-28 19:37:29 -0800518 if ((err = attach_aead(&x->aead, &x->props.ealgo,
519 attrs[XFRMA_ALG_AEAD])))
520 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000521 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
522 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000524 if (!x->props.aalgo) {
525 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
526 attrs[XFRMA_ALG_AUTH])))
527 goto error;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
530 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700531 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto error;
533 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
534 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700535 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700537
538 if (attrs[XFRMA_ENCAP]) {
539 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
540 sizeof(*x->encap), GFP_KERNEL);
541 if (x->encap == NULL)
542 goto error;
543 }
544
Martin Willi35d28562010-12-08 04:37:49 +0000545 if (attrs[XFRMA_TFCPAD])
546 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
547
Thomas Graffd211502007-09-06 03:28:08 -0700548 if (attrs[XFRMA_COADDR]) {
549 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
550 sizeof(*x->coaddr), GFP_KERNEL);
551 if (x->coaddr == NULL)
552 goto error;
553 }
554
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000555 xfrm_mark_get(attrs, &x->mark);
556
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700557 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (err)
559 goto error;
560
Thomas Graffd211502007-09-06 03:28:08 -0700561 if (attrs[XFRMA_SEC_CTX] &&
562 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800563 goto error;
564
Steffen Klassertd8647b72011-03-08 00:10:27 +0000565 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
566 attrs[XFRMA_REPLAY_ESN_VAL])))
567 goto error;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800570 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800571 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800572 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800573
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000574 if ((err = xfrm_init_replay(x)))
575 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800576
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000577 /* override default values from above */
Mathias Krause743b9112012-09-19 11:33:43 +0000578 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return x;
581
582error:
583 x->km.state = XFRM_STATE_DEAD;
584 xfrm_state_put(x);
585error_no_put:
586 *errp = err;
587 return NULL;
588}
589
Christoph Hellwig22e70052007-01-02 15:22:30 -0800590static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700591 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800593 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700594 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct xfrm_state *x;
596 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700597 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800598 uid_t loginuid = audit_get_loginuid(current);
599 u32 sessionid = audit_get_sessionid(current);
600 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Thomas Graf35a7aa02007-08-22 14:00:40 -0700602 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (err)
604 return err;
605
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800606 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!x)
608 return err;
609
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700610 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
612 err = xfrm_state_add(x);
613 else
614 err = xfrm_state_update(x);
615
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800616 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400617 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (err < 0) {
620 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800621 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700625 c.seq = nlh->nlmsg_seq;
626 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700627 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700628
629 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700630out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700631 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return err;
633}
634
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800635static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
636 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700637 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700638 int *errp)
639{
640 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000641 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700642 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000643 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700644
645 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
646 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000647 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700648 } else {
649 xfrm_address_t *saddr = NULL;
650
Thomas Graf35a7aa02007-08-22 14:00:40 -0700651 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700652 if (!saddr) {
653 err = -EINVAL;
654 goto out;
655 }
656
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800657 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000658 x = xfrm_state_lookup_byaddr(net, mark,
659 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800660 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700661 }
662
663 out:
664 if (!x && errp)
665 *errp = err;
666 return x;
667}
668
Christoph Hellwig22e70052007-01-02 15:22:30 -0800669static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700670 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800672 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700674 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700675 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700676 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800677 uid_t loginuid = audit_get_loginuid(current);
678 u32 sessionid = audit_get_sessionid(current);
679 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800681 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
David S. Miller6f68dc32006-06-08 23:58:52 -0700685 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700686 goto out;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700689 err = -EPERM;
690 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700693 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600694
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700695 if (err < 0)
696 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700697
698 c.seq = nlh->nlmsg_seq;
699 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700700 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700701 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700703out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800704 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400705 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700706 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700707 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
711{
Mathias Kraused5f1f7c2012-09-19 11:33:39 +0000712 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 memcpy(&p->id, &x->id, sizeof(p->id));
714 memcpy(&p->sel, &x->sel, sizeof(p->sel));
715 memcpy(&p->lft, &x->lft, sizeof(p->lft));
716 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
717 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700718 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 p->mode = x->props.mode;
720 p->replay_window = x->props.replay_window;
721 p->reqid = x->props.reqid;
722 p->family = x->props.family;
723 p->flags = x->props.flags;
724 p->seq = x->km.seq;
725}
726
727struct xfrm_dump_info {
728 struct sk_buff *in_skb;
729 struct sk_buff *out_skb;
730 u32 nlmsg_seq;
731 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732};
733
Thomas Grafc0144be2007-08-22 13:55:43 -0700734static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
735{
Thomas Grafc0144be2007-08-22 13:55:43 -0700736 struct xfrm_user_sec_ctx *uctx;
737 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700738 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700739
740 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
741 if (attr == NULL)
742 return -EMSGSIZE;
743
744 uctx = nla_data(attr);
745 uctx->exttype = XFRMA_SEC_CTX;
746 uctx->len = ctx_size;
747 uctx->ctx_doi = s->ctx_doi;
748 uctx->ctx_alg = s->ctx_alg;
749 uctx->ctx_len = s->ctx_len;
750 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
751
752 return 0;
753}
754
Martin Willi4447bb32009-11-25 00:29:52 +0000755static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
756{
757 struct xfrm_algo *algo;
758 struct nlattr *nla;
759
760 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
761 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
762 if (!nla)
763 return -EMSGSIZE;
764
765 algo = nla_data(nla);
Mathias Krause37d61a22012-09-19 11:33:38 +0000766 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000767 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
768 algo->alg_key_len = auth->alg_key_len;
769
770 return 0;
771}
772
Herbert Xu68325d32007-10-09 13:30:57 -0700773/* Don't change this without updating xfrm_sa_len! */
774static int copy_to_user_state_extra(struct xfrm_state *x,
775 struct xfrm_usersa_info *p,
776 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 copy_to_user_state(x, p);
779
Herbert Xu050f0092007-10-09 13:31:47 -0700780 if (x->coaddr)
781 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
782
783 if (x->lastused)
784 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700785
Herbert Xu1a6509d2008-01-28 19:37:29 -0800786 if (x->aead)
787 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000788 if (x->aalg) {
789 if (copy_to_user_auth(x->aalg, skb))
790 goto nla_put_failure;
791
792 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
793 xfrm_alg_auth_len(x->aalg), x->aalg);
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800796 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700798 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700801 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Martin Willi35d28562010-12-08 04:37:49 +0000803 if (x->tfcpad)
804 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
805
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000806 if (xfrm_mark_put(skb, &x->mark))
807 goto nla_put_failure;
808
Steffen Klassertd8647b72011-03-08 00:10:27 +0000809 if (x->replay_esn)
810 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
811 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
812
Thomas Grafc0144be2007-08-22 13:55:43 -0700813 if (x->security && copy_sec_ctx(x->security, skb) < 0)
814 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700815
Herbert Xu68325d32007-10-09 13:30:57 -0700816 return 0;
817
818nla_put_failure:
819 return -EMSGSIZE;
820}
821
822static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
823{
824 struct xfrm_dump_info *sp = ptr;
825 struct sk_buff *in_skb = sp->in_skb;
826 struct sk_buff *skb = sp->out_skb;
827 struct xfrm_usersa_info *p;
828 struct nlmsghdr *nlh;
829 int err;
830
Herbert Xu68325d32007-10-09 13:30:57 -0700831 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
832 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
833 if (nlh == NULL)
834 return -EMSGSIZE;
835
836 p = nlmsg_data(nlh);
837
838 err = copy_to_user_state_extra(x, p, skb);
839 if (err)
840 goto nla_put_failure;
841
Thomas Graf98250692007-08-22 12:47:26 -0700842 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 0;
844
Thomas Grafc0144be2007-08-22 13:55:43 -0700845nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700846 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700847 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Timo Teras4c563f72008-02-28 21:31:08 -0800850static int xfrm_dump_sa_done(struct netlink_callback *cb)
851{
852 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
853 xfrm_state_walk_done(walk);
854 return 0;
855}
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
858{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800859 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800860 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct xfrm_dump_info info;
862
Timo Teras4c563f72008-02-28 21:31:08 -0800863 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
864 sizeof(cb->args) - sizeof(cb->args[0]));
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 info.in_skb = cb->skb;
867 info.out_skb = skb;
868 info.nlmsg_seq = cb->nlh->nlmsg_seq;
869 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800870
871 if (!cb->args[0]) {
872 cb->args[0] = 1;
873 xfrm_state_walk_init(walk, 0);
874 }
875
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800876 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return skb->len;
879}
880
881static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
882 struct xfrm_state *x, u32 seq)
883{
884 struct xfrm_dump_info info;
885 struct sk_buff *skb;
Mathias Krause555144b2012-09-13 11:41:26 +0000886 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Thomas Graf7deb2262007-08-22 13:57:39 -0700888 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!skb)
890 return ERR_PTR(-ENOMEM);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 info.in_skb = in_skb;
893 info.out_skb = skb;
894 info.nlmsg_seq = seq;
895 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Mathias Krause555144b2012-09-13 11:41:26 +0000897 err = dump_one_state(x, 0, &info);
898 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 kfree_skb(skb);
Mathias Krause555144b2012-09-13 11:41:26 +0000900 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 return skb;
904}
905
Thomas Graf7deb2262007-08-22 13:57:39 -0700906static inline size_t xfrm_spdinfo_msgsize(void)
907{
908 return NLMSG_ALIGN(4)
909 + nla_total_size(sizeof(struct xfrmu_spdinfo))
910 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
911}
912
Alexey Dobriyane0710412010-01-23 13:37:10 +0000913static int build_spdinfo(struct sk_buff *skb, struct net *net,
914 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700915{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700916 struct xfrmk_spdinfo si;
917 struct xfrmu_spdinfo spc;
918 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700919 struct nlmsghdr *nlh;
920 u32 *f;
921
922 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300923 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700924 return -EMSGSIZE;
925
926 f = nlmsg_data(nlh);
927 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000928 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700929 spc.incnt = si.incnt;
930 spc.outcnt = si.outcnt;
931 spc.fwdcnt = si.fwdcnt;
932 spc.inscnt = si.inscnt;
933 spc.outscnt = si.outscnt;
934 spc.fwdscnt = si.fwdscnt;
935 sph.spdhcnt = si.spdhcnt;
936 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700937
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700938 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
939 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700940
941 return nlmsg_end(skb, nlh);
942
943nla_put_failure:
944 nlmsg_cancel(skb, nlh);
945 return -EMSGSIZE;
946}
947
948static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700949 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700950{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800951 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700952 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700953 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700954 u32 spid = NETLINK_CB(skb).pid;
955 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700956
Thomas Graf7deb2262007-08-22 13:57:39 -0700957 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700958 if (r_skb == NULL)
959 return -ENOMEM;
960
Alexey Dobriyane0710412010-01-23 13:37:10 +0000961 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700962 BUG();
963
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800964 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700965}
966
Thomas Graf7deb2262007-08-22 13:57:39 -0700967static inline size_t xfrm_sadinfo_msgsize(void)
968{
969 return NLMSG_ALIGN(4)
970 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
971 + nla_total_size(4); /* XFRMA_SAD_CNT */
972}
973
Alexey Dobriyane0710412010-01-23 13:37:10 +0000974static int build_sadinfo(struct sk_buff *skb, struct net *net,
975 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700976{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700977 struct xfrmk_sadinfo si;
978 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700979 struct nlmsghdr *nlh;
980 u32 *f;
981
982 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300983 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700984 return -EMSGSIZE;
985
986 f = nlmsg_data(nlh);
987 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000988 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700989
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700990 sh.sadhmcnt = si.sadhmcnt;
991 sh.sadhcnt = si.sadhcnt;
992
993 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
994 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700995
996 return nlmsg_end(skb, nlh);
997
998nla_put_failure:
999 nlmsg_cancel(skb, nlh);
1000 return -EMSGSIZE;
1001}
1002
1003static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001004 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001005{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001006 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001007 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001008 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001009 u32 spid = NETLINK_CB(skb).pid;
1010 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001011
Thomas Graf7deb2262007-08-22 13:57:39 -07001012 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001013 if (r_skb == NULL)
1014 return -ENOMEM;
1015
Alexey Dobriyane0710412010-01-23 13:37:10 +00001016 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001017 BUG();
1018
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001019 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001020}
1021
Christoph Hellwig22e70052007-01-02 15:22:30 -08001022static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001023 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001025 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001026 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct xfrm_state *x;
1028 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001029 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001031 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (x == NULL)
1033 goto out_noput;
1034
1035 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1036 if (IS_ERR(resp_skb)) {
1037 err = PTR_ERR(resp_skb);
1038 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001039 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 xfrm_state_put(x);
1042out_noput:
1043 return err;
1044}
1045
1046static int verify_userspi_info(struct xfrm_userspi_info *p)
1047{
1048 switch (p->info.id.proto) {
1049 case IPPROTO_AH:
1050 case IPPROTO_ESP:
1051 break;
1052
1053 case IPPROTO_COMP:
1054 /* IPCOMP spi is 16-bits. */
1055 if (p->max >= 0x10000)
1056 return -EINVAL;
1057 break;
1058
1059 default:
1060 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (p->min > p->max)
1064 return -EINVAL;
1065
1066 return 0;
1067}
1068
Christoph Hellwig22e70052007-01-02 15:22:30 -08001069static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001070 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001072 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct xfrm_state *x;
1074 struct xfrm_userspi_info *p;
1075 struct sk_buff *resp_skb;
1076 xfrm_address_t *daddr;
1077 int family;
1078 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001079 u32 mark;
1080 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Thomas Graf7b67c852007-08-22 13:53:52 -07001082 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 err = verify_userspi_info(p);
1084 if (err)
1085 goto out_noput;
1086
1087 family = p->info.family;
1088 daddr = &p->info.id.daddr;
1089
1090 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001091
1092 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001094 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1096 xfrm_state_put(x);
1097 x = NULL;
1098 }
1099 }
1100
1101 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001102 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 p->info.id.proto, daddr,
1104 &p->info.saddr, 1,
1105 family);
1106 err = -ENOENT;
1107 if (x == NULL)
1108 goto out_noput;
1109
Herbert Xu658b2192007-10-09 13:29:52 -07001110 err = xfrm_alloc_spi(x, p->min, p->max);
1111 if (err)
1112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Herbert Xu658b2192007-10-09 13:29:52 -07001114 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (IS_ERR(resp_skb)) {
1116 err = PTR_ERR(resp_skb);
1117 goto out;
1118 }
1119
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001120 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122out:
1123 xfrm_state_put(x);
1124out_noput:
1125 return err;
1126}
1127
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001128static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 switch (dir) {
1131 case XFRM_POLICY_IN:
1132 case XFRM_POLICY_OUT:
1133 case XFRM_POLICY_FWD:
1134 break;
1135
1136 default:
1137 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 return 0;
1141}
1142
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001143static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001144{
1145 switch (type) {
1146 case XFRM_POLICY_TYPE_MAIN:
1147#ifdef CONFIG_XFRM_SUB_POLICY
1148 case XFRM_POLICY_TYPE_SUB:
1149#endif
1150 break;
1151
1152 default:
1153 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001154 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001155
1156 return 0;
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1160{
1161 switch (p->share) {
1162 case XFRM_SHARE_ANY:
1163 case XFRM_SHARE_SESSION:
1164 case XFRM_SHARE_USER:
1165 case XFRM_SHARE_UNIQUE:
1166 break;
1167
1168 default:
1169 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 switch (p->action) {
1173 case XFRM_POLICY_ALLOW:
1174 case XFRM_POLICY_BLOCK:
1175 break;
1176
1177 default:
1178 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 switch (p->sel.family) {
1182 case AF_INET:
1183 break;
1184
1185 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001186#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 break;
1188#else
1189 return -EAFNOSUPPORT;
1190#endif
1191
1192 default:
1193 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 return verify_policy_dir(p->dir);
1197}
1198
Thomas Graf5424f322007-08-22 14:01:33 -07001199static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001200{
Thomas Graf5424f322007-08-22 14:01:33 -07001201 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001202 struct xfrm_user_sec_ctx *uctx;
1203
1204 if (!rt)
1205 return 0;
1206
Thomas Graf5424f322007-08-22 14:01:33 -07001207 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001208 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001209}
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1212 int nr)
1213{
1214 int i;
1215
1216 xp->xfrm_nr = nr;
1217 for (i = 0; i < nr; i++, ut++) {
1218 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1219
1220 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1221 memcpy(&t->saddr, &ut->saddr,
1222 sizeof(xfrm_address_t));
1223 t->reqid = ut->reqid;
1224 t->mode = ut->mode;
1225 t->share = ut->share;
1226 t->optional = ut->optional;
1227 t->aalgos = ut->aalgos;
1228 t->ealgos = ut->ealgos;
1229 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001230 /* If all masks are ~0, then we allow all algorithms. */
1231 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001232 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234}
1235
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001236static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1237{
1238 int i;
1239
1240 if (nr > XFRM_MAX_DEPTH)
1241 return -EINVAL;
1242
1243 for (i = 0; i < nr; i++) {
1244 /* We never validated the ut->family value, so many
1245 * applications simply leave it at zero. The check was
1246 * never made and ut->family was ignored because all
1247 * templates could be assumed to have the same family as
1248 * the policy itself. Now that we will have ipv4-in-ipv6
1249 * and ipv6-in-ipv4 tunnels, this is no longer true.
1250 */
1251 if (!ut[i].family)
1252 ut[i].family = family;
1253
1254 switch (ut[i].family) {
1255 case AF_INET:
1256 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001257#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001258 case AF_INET6:
1259 break;
1260#endif
1261 default:
1262 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001263 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001264 }
1265
1266 return 0;
1267}
1268
Thomas Graf5424f322007-08-22 14:01:33 -07001269static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Thomas Graf5424f322007-08-22 14:01:33 -07001271 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 if (!rt) {
1274 pol->xfrm_nr = 0;
1275 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001276 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1277 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001278 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001280 err = validate_tmpl(nr, utmpl, pol->family);
1281 if (err)
1282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Thomas Graf5424f322007-08-22 14:01:33 -07001284 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 return 0;
1287}
1288
Thomas Graf5424f322007-08-22 14:01:33 -07001289static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001290{
Thomas Graf5424f322007-08-22 14:01:33 -07001291 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001292 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001293 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001294 int err;
1295
1296 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001297 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001298 type = upt->type;
1299 }
1300
1301 err = verify_policy_type(type);
1302 if (err)
1303 return err;
1304
1305 *tp = type;
1306 return 0;
1307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1310{
1311 xp->priority = p->priority;
1312 xp->index = p->index;
1313 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1314 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1315 xp->action = p->action;
1316 xp->flags = p->flags;
1317 xp->family = p->sel.family;
1318 /* XXX xp->share = p->share; */
1319}
1320
1321static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1322{
Mathias Krause97f96ea2012-09-19 11:33:40 +00001323 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1325 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1326 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1327 p->priority = xp->priority;
1328 p->index = xp->index;
1329 p->sel.family = xp->family;
1330 p->dir = dir;
1331 p->action = xp->action;
1332 p->flags = xp->flags;
1333 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1334}
1335
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001336static 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 -07001337{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001338 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int err;
1340
1341 if (!xp) {
1342 *errp = -ENOMEM;
1343 return NULL;
1344 }
1345
1346 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001347
Thomas Graf35a7aa02007-08-22 14:00:40 -07001348 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001349 if (err)
1350 goto error;
1351
Thomas Graf35a7aa02007-08-22 14:00:40 -07001352 if (!(err = copy_from_user_tmpl(xp, attrs)))
1353 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001354 if (err)
1355 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001357 xfrm_mark_get(attrs, &xp->mark);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001360 error:
1361 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001362 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001363 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001364 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Christoph Hellwig22e70052007-01-02 15:22:30 -08001367static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001368 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001370 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001371 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001373 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int err;
1375 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001376 uid_t loginuid = audit_get_loginuid(current);
1377 u32 sessionid = audit_get_sessionid(current);
1378 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 err = verify_newpolicy_info(p);
1381 if (err)
1382 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001383 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001384 if (err)
1385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001387 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!xp)
1389 return err;
1390
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001391 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001392 * Aha! this is anti-netlink really i.e more pfkey derived
1393 * in netlink excl is a flag and you wouldnt need
1394 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1396 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001397 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001398 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001401 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 kfree(xp);
1403 return err;
1404 }
1405
Herbert Xuf60f6b82005-06-18 22:44:37 -07001406 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001407 c.seq = nlh->nlmsg_seq;
1408 c.pid = nlh->nlmsg_pid;
1409 km_policy_notify(xp, p->dir, &c);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 xfrm_pol_put(xp);
1412
1413 return 0;
1414}
1415
1416static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1417{
1418 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1419 int i;
1420
1421 if (xp->xfrm_nr == 0)
1422 return 0;
1423
1424 for (i = 0; i < xp->xfrm_nr; i++) {
1425 struct xfrm_user_tmpl *up = &vec[i];
1426 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1427
Mathias Krause0c5e3752012-09-19 11:33:41 +00001428 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001430 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1432 up->reqid = kp->reqid;
1433 up->mode = kp->mode;
1434 up->share = kp->share;
1435 up->optional = kp->optional;
1436 up->aalgos = kp->aalgos;
1437 up->ealgos = kp->ealgos;
1438 up->calgos = kp->calgos;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Thomas Grafc0144be2007-08-22 13:55:43 -07001441 return nla_put(skb, XFRMA_TMPL,
1442 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001443}
1444
Serge Hallyn0d681622006-07-24 23:30:44 -07001445static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1446{
1447 if (x->security) {
1448 return copy_sec_ctx(x->security, skb);
1449 }
1450 return 0;
1451}
1452
1453static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1454{
1455 if (xp->security) {
1456 return copy_sec_ctx(xp->security, skb);
1457 }
1458 return 0;
1459}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001460static inline size_t userpolicy_type_attrsize(void)
1461{
1462#ifdef CONFIG_XFRM_SUB_POLICY
1463 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1464#else
1465 return 0;
1466#endif
1467}
Serge Hallyn0d681622006-07-24 23:30:44 -07001468
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001469#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001470static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001471{
Thomas Grafc0144be2007-08-22 13:55:43 -07001472 struct xfrm_userpolicy_type upt = {
1473 .type = type,
1474 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001475
Thomas Grafc0144be2007-08-22 13:55:43 -07001476 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001477}
1478
1479#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001480static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001481{
1482 return 0;
1483}
1484#endif
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1487{
1488 struct xfrm_dump_info *sp = ptr;
1489 struct xfrm_userpolicy_info *p;
1490 struct sk_buff *in_skb = sp->in_skb;
1491 struct sk_buff *skb = sp->out_skb;
1492 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001494 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1495 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1496 if (nlh == NULL)
1497 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Thomas Graf7b67c852007-08-22 13:53:52 -07001499 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 copy_to_user_policy(xp, p, dir);
1501 if (copy_to_user_tmpl(xp, skb) < 0)
1502 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001503 if (copy_to_user_sec_ctx(xp, skb))
1504 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001505 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001506 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001507 if (xfrm_mark_put(skb, &xp->mark))
1508 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Thomas Graf98250692007-08-22 12:47:26 -07001510 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return 0;
1512
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001513nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001515 nlmsg_cancel(skb, nlh);
1516 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Timo Teras4c563f72008-02-28 21:31:08 -08001519static int xfrm_dump_policy_done(struct netlink_callback *cb)
1520{
1521 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1522
1523 xfrm_policy_walk_done(walk);
1524 return 0;
1525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1528{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001529 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001530 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct xfrm_dump_info info;
1532
Timo Teras4c563f72008-02-28 21:31:08 -08001533 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1534 sizeof(cb->args) - sizeof(cb->args[0]));
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 info.in_skb = cb->skb;
1537 info.out_skb = skb;
1538 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1539 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001540
1541 if (!cb->args[0]) {
1542 cb->args[0] = 1;
1543 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1544 }
1545
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001546 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 return skb->len;
1549}
1550
1551static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1552 struct xfrm_policy *xp,
1553 int dir, u32 seq)
1554{
1555 struct xfrm_dump_info info;
1556 struct sk_buff *skb;
Mathias Krausef38b3342012-09-14 09:58:32 +00001557 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Thomas Graf7deb2262007-08-22 13:57:39 -07001559 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (!skb)
1561 return ERR_PTR(-ENOMEM);
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 info.in_skb = in_skb;
1564 info.out_skb = skb;
1565 info.nlmsg_seq = seq;
1566 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Mathias Krausef38b3342012-09-14 09:58:32 +00001568 err = dump_one_policy(xp, dir, 0, &info);
1569 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 kfree_skb(skb);
Mathias Krausef38b3342012-09-14 09:58:32 +00001571 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573
1574 return skb;
1575}
1576
Christoph Hellwig22e70052007-01-02 15:22:30 -08001577static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001578 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001580 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct xfrm_policy *xp;
1582 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001583 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001585 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001587 struct xfrm_mark m;
1588 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Thomas Graf7b67c852007-08-22 13:53:52 -07001590 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1592
Thomas Graf35a7aa02007-08-22 14:00:40 -07001593 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001594 if (err)
1595 return err;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 err = verify_policy_dir(p->dir);
1598 if (err)
1599 return err;
1600
1601 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001602 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001603 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001604 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001605 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001606
Thomas Graf35a7aa02007-08-22 14:00:40 -07001607 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001608 if (err)
1609 return err;
1610
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001611 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001612 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001613 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001614
Paul Moore03e1ad72008-04-12 19:07:52 -07001615 err = security_xfrm_policy_alloc(&ctx, uctx);
1616 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001617 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001618 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001619 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001620 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001621 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (xp == NULL)
1624 return -ENOENT;
1625
1626 if (!delete) {
1627 struct sk_buff *resp_skb;
1628
1629 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1630 if (IS_ERR(resp_skb)) {
1631 err = PTR_ERR(resp_skb);
1632 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001633 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001634 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001636 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001637 uid_t loginuid = audit_get_loginuid(current);
1638 u32 sessionid = audit_get_sessionid(current);
1639 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001640
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001641 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001642 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1643 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001644
1645 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001646 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001647
Herbert Xue7443892005-06-18 22:44:18 -07001648 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001649 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001650 c.seq = nlh->nlmsg_seq;
1651 c.pid = nlh->nlmsg_pid;
1652 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001655out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001656 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return err;
1658}
1659
Christoph Hellwig22e70052007-01-02 15:22:30 -08001660static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001661 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001663 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001664 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001665 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001666 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001667 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001669 audit_info.loginuid = audit_get_loginuid(current);
1670 audit_info.sessionid = audit_get_sessionid(current);
1671 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001672 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001673 if (err) {
1674 if (err == -ESRCH) /* empty table */
1675 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001676 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001677 }
Herbert Xubf088672005-06-18 22:44:00 -07001678 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001679 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680 c.seq = nlh->nlmsg_seq;
1681 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001682 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001683 km_state_notify(NULL, &c);
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return 0;
1686}
1687
Steffen Klassertd8647b72011-03-08 00:10:27 +00001688static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001689{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001690 size_t replay_size = x->replay_esn ?
1691 xfrm_replay_state_esn_len(x->replay_esn) :
1692 sizeof(struct xfrm_replay_state);
1693
Thomas Graf7deb2262007-08-22 13:57:39 -07001694 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001695 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001696 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001697 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001698 + nla_total_size(4) /* XFRM_AE_RTHR */
1699 + nla_total_size(4); /* XFRM_AE_ETHR */
1700}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001701
David S. Miller214e0052011-02-24 00:02:38 -05001702static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001703{
1704 struct xfrm_aevent_id *id;
1705 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001706
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001707 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1708 if (nlh == NULL)
1709 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001710
Thomas Graf7b67c852007-08-22 13:53:52 -07001711 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001712 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001713 id->sa_id.spi = x->id.spi;
1714 id->sa_id.family = x->props.family;
1715 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001716 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1717 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001718 id->flags = c->data.aevent;
1719
Steffen Klassertd8647b72011-03-08 00:10:27 +00001720 if (x->replay_esn)
1721 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1722 xfrm_replay_state_esn_len(x->replay_esn),
1723 x->replay_esn);
1724 else
1725 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1726
Thomas Grafc0144be2007-08-22 13:55:43 -07001727 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001728
Thomas Grafc0144be2007-08-22 13:55:43 -07001729 if (id->flags & XFRM_AE_RTHR)
1730 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001731
Thomas Grafc0144be2007-08-22 13:55:43 -07001732 if (id->flags & XFRM_AE_ETHR)
1733 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1734 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001735
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001736 if (xfrm_mark_put(skb, &x->mark))
1737 goto nla_put_failure;
1738
Thomas Graf98250692007-08-22 12:47:26 -07001739 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001740
Thomas Grafc0144be2007-08-22 13:55:43 -07001741nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001742 nlmsg_cancel(skb, nlh);
1743 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001744}
1745
Christoph Hellwig22e70052007-01-02 15:22:30 -08001746static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001747 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001748{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001749 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001750 struct xfrm_state *x;
1751 struct sk_buff *r_skb;
1752 int err;
1753 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001754 u32 mark;
1755 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001756 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001757 struct xfrm_usersa_id *id = &p->sa_id;
1758
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001759 mark = xfrm_mark_get(attrs, &m);
1760
1761 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001762 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001763 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001764
1765 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1766 if (r_skb == NULL) {
1767 xfrm_state_put(x);
1768 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001769 }
1770
1771 /*
1772 * XXX: is this lock really needed - none of the other
1773 * gets lock (the concern is things getting updated
1774 * while we are still reading) - jhs
1775 */
1776 spin_lock_bh(&x->lock);
1777 c.data.aevent = p->flags;
1778 c.seq = nlh->nlmsg_seq;
1779 c.pid = nlh->nlmsg_pid;
1780
1781 if (build_aevent(r_skb, x, &c) < 0)
1782 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001783 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001784 spin_unlock_bh(&x->lock);
1785 xfrm_state_put(x);
1786 return err;
1787}
1788
Christoph Hellwig22e70052007-01-02 15:22:30 -08001789static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001790 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001791{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001792 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001793 struct xfrm_state *x;
1794 struct km_event c;
1795 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001796 u32 mark = 0;
1797 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001798 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001799 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001800 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001801 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001802
Steffen Klassertd8647b72011-03-08 00:10:27 +00001803 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001804 return err;
1805
1806 /* pedantic mode - thou shalt sayeth replaceth */
1807 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1808 return err;
1809
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001810 mark = xfrm_mark_get(attrs, &m);
1811
1812 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 -08001813 if (x == NULL)
1814 return -ESRCH;
1815
1816 if (x->km.state != XFRM_STATE_VALID)
1817 goto out;
1818
Steffen Klasserte2b19122011-03-28 19:47:30 +00001819 err = xfrm_replay_verify_len(x->replay_esn, rp);
1820 if (err)
1821 goto out;
1822
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001823 spin_lock_bh(&x->lock);
Mathias Krause743b9112012-09-19 11:33:43 +00001824 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001825 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001826
1827 c.event = nlh->nlmsg_type;
1828 c.seq = nlh->nlmsg_seq;
1829 c.pid = nlh->nlmsg_pid;
1830 c.data.aevent = XFRM_AE_CU;
1831 km_state_notify(x, &c);
1832 err = 0;
1833out:
1834 xfrm_state_put(x);
1835 return err;
1836}
1837
Christoph Hellwig22e70052007-01-02 15:22:30 -08001838static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001839 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001841 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001842 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001843 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001844 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001845 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001846
Thomas Graf35a7aa02007-08-22 14:00:40 -07001847 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001848 if (err)
1849 return err;
1850
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001851 audit_info.loginuid = audit_get_loginuid(current);
1852 audit_info.sessionid = audit_get_sessionid(current);
1853 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001854 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001855 if (err) {
1856 if (err == -ESRCH) /* empty table */
1857 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001858 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001859 }
1860
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001861 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001862 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001863 c.seq = nlh->nlmsg_seq;
1864 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001865 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001866 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return 0;
1868}
1869
Christoph Hellwig22e70052007-01-02 15:22:30 -08001870static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001871 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001872{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001873 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001874 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001875 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001876 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001877 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001878 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001879 struct xfrm_mark m;
1880 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001881
Thomas Graf35a7aa02007-08-22 14:00:40 -07001882 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001883 if (err)
1884 return err;
1885
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001886 err = verify_policy_dir(p->dir);
1887 if (err)
1888 return err;
1889
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001890 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001891 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001892 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001893 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001894 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001895
Thomas Graf35a7aa02007-08-22 14:00:40 -07001896 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001897 if (err)
1898 return err;
1899
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001900 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001901 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001902 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001903
Paul Moore03e1ad72008-04-12 19:07:52 -07001904 err = security_xfrm_policy_alloc(&ctx, uctx);
1905 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001906 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001907 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001908 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001909 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001910 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001911 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001912 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001913 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001914
Timo Teräsea2dea92010-03-31 00:17:05 +00001915 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001916 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001917
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001918 err = 0;
1919 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001920 uid_t loginuid = audit_get_loginuid(current);
1921 u32 sessionid = audit_get_sessionid(current);
1922 u32 sid;
1923
1924 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001925 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001926 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001927
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001928 } else {
1929 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001930 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001931 }
1932 km_policy_expired(xp, p->dir, up->hard, current->pid);
1933
1934out:
1935 xfrm_pol_put(xp);
1936 return err;
1937}
1938
Christoph Hellwig22e70052007-01-02 15:22:30 -08001939static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001940 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001941{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001942 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001943 struct xfrm_state *x;
1944 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001945 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001946 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001947 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001948 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001949
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001950 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 -08001951
David S. Miller3a765aa2007-02-26 14:52:21 -08001952 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001953 if (x == NULL)
1954 return err;
1955
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001956 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001957 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001958 if (x->km.state != XFRM_STATE_VALID)
1959 goto out;
1960 km_state_expired(x, ue->hard, current->pid);
1961
Joy Latten161a09e2006-11-27 13:11:54 -06001962 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001963 uid_t loginuid = audit_get_loginuid(current);
1964 u32 sessionid = audit_get_sessionid(current);
1965 u32 sid;
1966
1967 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001968 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001969 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001970 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001971 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001972out:
1973 spin_unlock_bh(&x->lock);
1974 xfrm_state_put(x);
1975 return err;
1976}
1977
Christoph Hellwig22e70052007-01-02 15:22:30 -08001978static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001979 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001980{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001981 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001982 struct xfrm_policy *xp;
1983 struct xfrm_user_tmpl *ut;
1984 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001985 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001986 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001987
Thomas Graf7b67c852007-08-22 13:53:52 -07001988 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001989 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001990 int err = -ENOMEM;
1991
1992 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001993 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001994
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001995 xfrm_mark_get(attrs, &mark);
1996
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001997 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001998 if (err)
1999 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002000
2001 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002002 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002003 if (!xp)
2004 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002005
2006 memcpy(&x->id, &ua->id, sizeof(ua->id));
2007 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2008 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002009 xp->mark.m = x->mark.m = mark.m;
2010 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002011 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002012 /* extract the templates and for each call km_key */
2013 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2014 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2015 memcpy(&x->id, &t->id, sizeof(x->id));
2016 x->props.mode = t->mode;
2017 x->props.reqid = t->reqid;
2018 x->props.family = ut->family;
2019 t->aalgos = ua->aalgos;
2020 t->ealgos = ua->ealgos;
2021 t->calgos = ua->calgos;
2022 err = km_query(x, t, xp);
2023
2024 }
2025
2026 kfree(x);
2027 kfree(xp);
2028
2029 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002030
2031bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002032 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002033free_state:
2034 kfree(x);
2035nomem:
2036 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002037}
2038
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002039#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002040static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002041 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002042 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002043{
Thomas Graf5424f322007-08-22 14:01:33 -07002044 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002045 struct xfrm_user_migrate *um;
2046 int i, num_migrate;
2047
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002048 if (k != NULL) {
2049 struct xfrm_user_kmaddress *uk;
2050
2051 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2052 memcpy(&k->local, &uk->local, sizeof(k->local));
2053 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2054 k->family = uk->family;
2055 k->reserved = uk->reserved;
2056 }
2057
Thomas Graf5424f322007-08-22 14:01:33 -07002058 um = nla_data(rt);
2059 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002060
2061 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2062 return -EINVAL;
2063
2064 for (i = 0; i < num_migrate; i++, um++, ma++) {
2065 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2066 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2067 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2068 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2069
2070 ma->proto = um->proto;
2071 ma->mode = um->mode;
2072 ma->reqid = um->reqid;
2073
2074 ma->old_family = um->old_family;
2075 ma->new_family = um->new_family;
2076 }
2077
2078 *num = i;
2079 return 0;
2080}
2081
2082static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002083 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002084{
Thomas Graf7b67c852007-08-22 13:53:52 -07002085 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002086 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002087 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002088 u8 type;
2089 int err;
2090 int n = 0;
2091
Thomas Graf35a7aa02007-08-22 14:00:40 -07002092 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002093 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002094
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002095 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2096
Thomas Graf5424f322007-08-22 14:01:33 -07002097 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002098 if (err)
2099 return err;
2100
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002101 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002102 if (err)
2103 return err;
2104
2105 if (!n)
2106 return 0;
2107
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002108 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002109
2110 return 0;
2111}
2112#else
2113static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002114 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002115{
2116 return -ENOPROTOOPT;
2117}
2118#endif
2119
2120#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002121static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002122{
2123 struct xfrm_user_migrate um;
2124
2125 memset(&um, 0, sizeof(um));
2126 um.proto = m->proto;
2127 um.mode = m->mode;
2128 um.reqid = m->reqid;
2129 um.old_family = m->old_family;
2130 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2131 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2132 um.new_family = m->new_family;
2133 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2134 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2135
Thomas Grafc0144be2007-08-22 13:55:43 -07002136 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002137}
2138
David S. Miller183cad12011-02-24 00:28:01 -05002139static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002140{
2141 struct xfrm_user_kmaddress uk;
2142
2143 memset(&uk, 0, sizeof(uk));
2144 uk.family = k->family;
2145 uk.reserved = k->reserved;
2146 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002147 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002148
2149 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2150}
2151
2152static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002153{
2154 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002155 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2156 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2157 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002158}
2159
David S. Miller183cad12011-02-24 00:28:01 -05002160static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2161 int num_migrate, const struct xfrm_kmaddress *k,
2162 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002163{
David S. Miller183cad12011-02-24 00:28:01 -05002164 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002165 struct xfrm_userpolicy_id *pol_id;
2166 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002167 int i;
2168
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002169 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2170 if (nlh == NULL)
2171 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002172
Thomas Graf7b67c852007-08-22 13:53:52 -07002173 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002174 /* copy data from selector, dir, and type to the pol_id */
2175 memset(pol_id, 0, sizeof(*pol_id));
2176 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2177 pol_id->dir = dir;
2178
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002179 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2180 goto nlmsg_failure;
2181
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002182 if (copy_to_user_policy_type(type, skb) < 0)
2183 goto nlmsg_failure;
2184
2185 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2186 if (copy_to_user_migrate(mp, skb) < 0)
2187 goto nlmsg_failure;
2188 }
2189
Thomas Graf98250692007-08-22 12:47:26 -07002190 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002191nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002192 nlmsg_cancel(skb, nlh);
2193 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002194}
2195
David S. Miller183cad12011-02-24 00:28:01 -05002196static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2197 const struct xfrm_migrate *m, int num_migrate,
2198 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002199{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002200 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002201 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002202
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002203 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002204 if (skb == NULL)
2205 return -ENOMEM;
2206
2207 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002208 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002209 BUG();
2210
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002211 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002212}
2213#else
David S. Miller183cad12011-02-24 00:28:01 -05002214static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2215 const struct xfrm_migrate *m, int num_migrate,
2216 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002217{
2218 return -ENOPROTOOPT;
2219}
2220#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002221
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002222#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002223
2224static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002225 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002226 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2227 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2228 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2229 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2230 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2231 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002232 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002233 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002234 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002235 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002236 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002237 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002238 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002239 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2240 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002241 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002242 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002243 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2244 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245};
2246
Thomas Graf492b5582005-05-03 14:26:40 -07002247#undef XMSGSIZE
2248
Thomas Grafcf5cb792007-08-22 13:59:04 -07002249static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002250 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2251 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2252 [XFRMA_LASTUSED] = { .type = NLA_U64},
2253 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002254 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002255 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2256 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2257 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2258 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2259 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2260 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2261 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2262 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2263 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2264 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2265 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2266 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2267 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2268 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002269 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002270 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002271 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002272 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002273};
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002276 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002278 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002279} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2280 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2281 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2282 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002283 .dump = xfrm_dump_sa,
2284 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002285 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2286 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2287 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002288 .dump = xfrm_dump_policy,
2289 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002290 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002291 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002292 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002293 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2294 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002295 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002296 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2297 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002298 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2299 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002300 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002301 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002302 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303};
2304
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002305static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002307 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002308 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002310 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002314 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 type -= XFRM_MSG_BASE;
2317 link = &xfrm_dispatch[type];
2318
2319 /* All operations require privileges, even GET */
Eric Parisfd778462012-01-03 12:25:16 -05002320 if (!capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002321 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Thomas Graf492b5582005-05-03 14:26:40 -07002323 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2324 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002325 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002327 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002329 {
2330 struct netlink_dump_control c = {
2331 .dump = link->dump,
2332 .done = link->done,
2333 };
2334 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337
Thomas Graf35a7aa02007-08-22 14:00:40 -07002338 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002339 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002340 if (err < 0)
2341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Thomas Graf5424f322007-08-22 14:01:33 -07002346 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002349static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002351 mutex_lock(&xfrm_cfg_mutex);
2352 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2353 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
Thomas Graf7deb2262007-08-22 13:57:39 -07002356static inline size_t xfrm_expire_msgsize(void)
2357{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002358 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2359 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002360}
2361
David S. Miller214e0052011-02-24 00:02:38 -05002362static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 struct xfrm_user_expire *ue;
2365 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002367 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2368 if (nlh == NULL)
2369 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Thomas Graf7b67c852007-08-22 13:53:52 -07002371 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002373 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002375 if (xfrm_mark_put(skb, &x->mark))
2376 goto nla_put_failure;
2377
Thomas Graf98250692007-08-22 12:47:26 -07002378 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002379
2380nla_put_failure:
2381 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
David S. Miller214e0052011-02-24 00:02:38 -05002384static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002386 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 struct sk_buff *skb;
2388
Thomas Graf7deb2262007-08-22 13:57:39 -07002389 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (skb == NULL)
2391 return -ENOMEM;
2392
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002393 if (build_expire(skb, x, c) < 0) {
2394 kfree_skb(skb);
2395 return -EMSGSIZE;
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002398 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
David S. Miller214e0052011-02-24 00:02:38 -05002401static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002402{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002403 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002404 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002405
Steffen Klassertd8647b72011-03-08 00:10:27 +00002406 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002407 if (skb == NULL)
2408 return -ENOMEM;
2409
2410 if (build_aevent(skb, x, c) < 0)
2411 BUG();
2412
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002413 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002414}
2415
David S. Miller214e0052011-02-24 00:02:38 -05002416static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002418 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002419 struct xfrm_usersa_flush *p;
2420 struct nlmsghdr *nlh;
2421 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002422 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002423
Thomas Graf7deb2262007-08-22 13:57:39 -07002424 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002425 if (skb == NULL)
2426 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002427
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002428 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2429 if (nlh == NULL) {
2430 kfree_skb(skb);
2431 return -EMSGSIZE;
2432 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002433
Thomas Graf7b67c852007-08-22 13:53:52 -07002434 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002435 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002436
Thomas Graf98250692007-08-22 12:47:26 -07002437 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002438
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002439 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002440}
2441
Thomas Graf7deb2262007-08-22 13:57:39 -07002442static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002443{
Thomas Graf7deb2262007-08-22 13:57:39 -07002444 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002445 if (x->aead)
2446 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002447 if (x->aalg) {
2448 l += nla_total_size(sizeof(struct xfrm_algo) +
2449 (x->aalg->alg_key_len + 7) / 8);
2450 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2451 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002452 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002453 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002454 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002455 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002457 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002458 if (x->tfcpad)
2459 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002460 if (x->replay_esn)
2461 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002462 if (x->security)
2463 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2464 x->security->ctx_len);
2465 if (x->coaddr)
2466 l += nla_total_size(sizeof(*x->coaddr));
2467
Herbert Xud26f3982007-11-13 21:47:08 -08002468 /* Must count x->lastused as it may become non-zero behind our back. */
2469 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002470
2471 return l;
2472}
2473
David S. Miller214e0052011-02-24 00:02:38 -05002474static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002475{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002476 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002477 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002478 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002479 struct nlmsghdr *nlh;
2480 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002481 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002482 int headlen;
2483
2484 headlen = sizeof(*p);
2485 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002486 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002487 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002488 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002489 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002490 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002491
Thomas Graf7deb2262007-08-22 13:57:39 -07002492 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002493 if (skb == NULL)
2494 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002495
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002496 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2497 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002498 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002499
Thomas Graf7b67c852007-08-22 13:53:52 -07002500 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002501 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002502 struct nlattr *attr;
2503
Thomas Graf7b67c852007-08-22 13:53:52 -07002504 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002505 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2506 id->spi = x->id.spi;
2507 id->family = x->props.family;
2508 id->proto = x->id.proto;
2509
Thomas Grafc0144be2007-08-22 13:55:43 -07002510 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2511 if (attr == NULL)
2512 goto nla_put_failure;
2513
2514 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002515 }
2516
Herbert Xu68325d32007-10-09 13:30:57 -07002517 if (copy_to_user_state_extra(x, p, skb))
2518 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002519
Thomas Graf98250692007-08-22 12:47:26 -07002520 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002521
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002522 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002523
Thomas Grafc0144be2007-08-22 13:55:43 -07002524nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002525 /* Somebody screwed up with xfrm_sa_len! */
2526 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002527 kfree_skb(skb);
2528 return -1;
2529}
2530
David S. Miller214e0052011-02-24 00:02:38 -05002531static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002532{
2533
2534 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002535 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002536 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002537 case XFRM_MSG_NEWAE:
2538 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002539 case XFRM_MSG_DELSA:
2540 case XFRM_MSG_UPDSA:
2541 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002542 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002543 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002544 return xfrm_notify_sa_flush(c);
2545 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002546 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2547 c->event);
2548 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002549 }
2550
2551 return 0;
2552
2553}
2554
Thomas Graf7deb2262007-08-22 13:57:39 -07002555static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2556 struct xfrm_policy *xp)
2557{
2558 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2559 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002560 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002561 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2562 + userpolicy_type_attrsize();
2563}
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2566 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2567 int dir)
2568{
2569 struct xfrm_user_acquire *ua;
2570 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 __u32 seq = xfrm_get_acqseq();
2572
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002573 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2574 if (nlh == NULL)
2575 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Thomas Graf7b67c852007-08-22 13:53:52 -07002577 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 memcpy(&ua->id, &x->id, sizeof(ua->id));
2579 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2580 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2581 copy_to_user_policy(xp, &ua->policy, dir);
2582 ua->aalgos = xt->aalgos;
2583 ua->ealgos = xt->ealgos;
2584 ua->calgos = xt->calgos;
2585 ua->seq = x->km.seq = seq;
2586
2587 if (copy_to_user_tmpl(xp, skb) < 0)
2588 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002589 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002590 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002591 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002592 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002593 if (xfrm_mark_put(skb, &xp->mark))
2594 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Thomas Graf98250692007-08-22 12:47:26 -07002596 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002598nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002600 nlmsg_cancel(skb, nlh);
2601 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
2604static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2605 struct xfrm_policy *xp, int dir)
2606{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002607 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Thomas Graf7deb2262007-08-22 13:57:39 -07002610 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (skb == NULL)
2612 return -ENOMEM;
2613
2614 if (build_acquire(skb, x, xt, xp, dir) < 0)
2615 BUG();
2616
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002617 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
2620/* User gives us xfrm_user_policy_info followed by an array of 0
2621 * or more templates.
2622 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002623static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 u8 *data, int len, int *dir)
2625{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002626 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2628 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2629 struct xfrm_policy *xp;
2630 int nr;
2631
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002632 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 case AF_INET:
2634 if (opt != IP_XFRM_POLICY) {
2635 *dir = -EOPNOTSUPP;
2636 return NULL;
2637 }
2638 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002639#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 case AF_INET6:
2641 if (opt != IPV6_XFRM_POLICY) {
2642 *dir = -EOPNOTSUPP;
2643 return NULL;
2644 }
2645 break;
2646#endif
2647 default:
2648 *dir = -EINVAL;
2649 return NULL;
2650 }
2651
2652 *dir = -EINVAL;
2653
2654 if (len < sizeof(*p) ||
2655 verify_newpolicy_info(p))
2656 return NULL;
2657
2658 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002659 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return NULL;
2661
Herbert Xua4f1bac2005-07-26 15:43:17 -07002662 if (p->dir > XFRM_POLICY_OUT)
2663 return NULL;
2664
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002665 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (xp == NULL) {
2667 *dir = -ENOBUFS;
2668 return NULL;
2669 }
2670
2671 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002672 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 copy_templates(xp, ut, nr);
2674
2675 *dir = p->dir;
2676
2677 return xp;
2678}
2679
Thomas Graf7deb2262007-08-22 13:57:39 -07002680static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2681{
2682 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2683 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2684 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002685 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002686 + userpolicy_type_attrsize();
2687}
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002690 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
2692 struct xfrm_user_polexpire *upe;
2693 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002694 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002696 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2697 if (nlh == NULL)
2698 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Thomas Graf7b67c852007-08-22 13:53:52 -07002700 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 copy_to_user_policy(xp, &upe->pol, dir);
2702 if (copy_to_user_tmpl(xp, skb) < 0)
2703 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002704 if (copy_to_user_sec_ctx(xp, skb))
2705 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002706 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002707 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002708 if (xfrm_mark_put(skb, &xp->mark))
2709 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 upe->hard = !!hard;
2711
Thomas Graf98250692007-08-22 12:47:26 -07002712 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002714nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002716 nlmsg_cancel(skb, nlh);
2717 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
David S. Miller214e0052011-02-24 00:02:38 -05002720static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002722 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Thomas Graf7deb2262007-08-22 13:57:39 -07002725 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (skb == NULL)
2727 return -ENOMEM;
2728
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002729 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 BUG();
2731
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002732 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
David S. Miller214e0052011-02-24 00:02:38 -05002735static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002736{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002737 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002738 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002739 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002740 struct nlmsghdr *nlh;
2741 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002742 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002743 int headlen;
2744
2745 headlen = sizeof(*p);
2746 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002747 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002748 headlen = sizeof(*id);
2749 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002750 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002751 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002752 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002753
Thomas Graf7deb2262007-08-22 13:57:39 -07002754 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002755 if (skb == NULL)
2756 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002757
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002758 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2759 if (nlh == NULL)
2760 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002761
Thomas Graf7b67c852007-08-22 13:53:52 -07002762 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002763 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002764 struct nlattr *attr;
2765
Thomas Graf7b67c852007-08-22 13:53:52 -07002766 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002767 memset(id, 0, sizeof(*id));
2768 id->dir = dir;
2769 if (c->data.byid)
2770 id->index = xp->index;
2771 else
2772 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2773
Thomas Grafc0144be2007-08-22 13:55:43 -07002774 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2775 if (attr == NULL)
2776 goto nlmsg_failure;
2777
2778 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002779 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002780
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002781 copy_to_user_policy(xp, p, dir);
2782 if (copy_to_user_tmpl(xp, skb) < 0)
2783 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002784 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002785 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002786
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002787 if (xfrm_mark_put(skb, &xp->mark))
2788 goto nla_put_failure;
2789
Thomas Graf98250692007-08-22 12:47:26 -07002790 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002791
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002792 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002793
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002794nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002795nlmsg_failure:
2796 kfree_skb(skb);
2797 return -1;
2798}
2799
David S. Miller214e0052011-02-24 00:02:38 -05002800static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002801{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002802 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002803 struct nlmsghdr *nlh;
2804 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002805
Thomas Graf7deb2262007-08-22 13:57:39 -07002806 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002807 if (skb == NULL)
2808 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002809
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002810 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2811 if (nlh == NULL)
2812 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002813 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2814 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002815
Thomas Graf98250692007-08-22 12:47:26 -07002816 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002817
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002818 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002819
2820nlmsg_failure:
2821 kfree_skb(skb);
2822 return -1;
2823}
2824
David S. Miller214e0052011-02-24 00:02:38 -05002825static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002826{
2827
2828 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002829 case XFRM_MSG_NEWPOLICY:
2830 case XFRM_MSG_UPDPOLICY:
2831 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002832 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002833 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002834 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002835 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002836 return xfrm_exp_policy_notify(xp, dir, c);
2837 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002838 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2839 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002840 }
2841
2842 return 0;
2843
2844}
2845
Thomas Graf7deb2262007-08-22 13:57:39 -07002846static inline size_t xfrm_report_msgsize(void)
2847{
2848 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2849}
2850
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002851static int build_report(struct sk_buff *skb, u8 proto,
2852 struct xfrm_selector *sel, xfrm_address_t *addr)
2853{
2854 struct xfrm_user_report *ur;
2855 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002856
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002857 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2858 if (nlh == NULL)
2859 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002860
Thomas Graf7b67c852007-08-22 13:53:52 -07002861 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002862 ur->proto = proto;
2863 memcpy(&ur->sel, sel, sizeof(ur->sel));
2864
2865 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002866 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002867
Thomas Graf98250692007-08-22 12:47:26 -07002868 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002869
Thomas Grafc0144be2007-08-22 13:55:43 -07002870nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002871 nlmsg_cancel(skb, nlh);
2872 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002873}
2874
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002875static int xfrm_send_report(struct net *net, u8 proto,
2876 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002877{
2878 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002879
Thomas Graf7deb2262007-08-22 13:57:39 -07002880 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002881 if (skb == NULL)
2882 return -ENOMEM;
2883
2884 if (build_report(skb, proto, sel, addr) < 0)
2885 BUG();
2886
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002887 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002888}
2889
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002890static inline size_t xfrm_mapping_msgsize(void)
2891{
2892 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2893}
2894
2895static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2896 xfrm_address_t *new_saddr, __be16 new_sport)
2897{
2898 struct xfrm_user_mapping *um;
2899 struct nlmsghdr *nlh;
2900
2901 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2902 if (nlh == NULL)
2903 return -EMSGSIZE;
2904
2905 um = nlmsg_data(nlh);
2906
2907 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2908 um->id.spi = x->id.spi;
2909 um->id.family = x->props.family;
2910 um->id.proto = x->id.proto;
2911 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2912 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2913 um->new_sport = new_sport;
2914 um->old_sport = x->encap->encap_sport;
2915 um->reqid = x->props.reqid;
2916
2917 return nlmsg_end(skb, nlh);
2918}
2919
2920static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2921 __be16 sport)
2922{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002923 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002924 struct sk_buff *skb;
2925
2926 if (x->id.proto != IPPROTO_ESP)
2927 return -EINVAL;
2928
2929 if (!x->encap)
2930 return -EINVAL;
2931
2932 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2933 if (skb == NULL)
2934 return -ENOMEM;
2935
2936 if (build_mapping(skb, x, ipaddr, sport) < 0)
2937 BUG();
2938
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002939 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942static struct xfrm_mgr netlink_mgr = {
2943 .id = "netlink",
2944 .notify = xfrm_send_state_notify,
2945 .acquire = xfrm_send_acquire,
2946 .compile_policy = xfrm_compile_policy,
2947 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002948 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002949 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002950 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951};
2952
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002953static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
Patrick McHardybe336902006-03-20 22:40:54 -08002955 struct sock *nlsk;
2956
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002957 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002958 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002959 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002961 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00002962 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return 0;
2964}
2965
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002966static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002967{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002968 struct net *net;
2969 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002970 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002971 synchronize_net();
2972 list_for_each_entry(net, net_exit_list, exit_list)
2973 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002974}
2975
2976static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002977 .init = xfrm_user_net_init,
2978 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002979};
2980
2981static int __init xfrm_user_init(void)
2982{
2983 int rv;
2984
2985 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2986
2987 rv = register_pernet_subsys(&xfrm_user_net_ops);
2988 if (rv < 0)
2989 return rv;
2990 rv = xfrm_register_km(&netlink_mgr);
2991 if (rv < 0)
2992 unregister_pernet_subsys(&xfrm_user_net_ops);
2993 return rv;
2994}
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996static void __exit xfrm_user_exit(void)
2997{
2998 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002999 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000}
3001
3002module_init(xfrm_user_init);
3003module_exit(xfrm_user_exit);
3004MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003005MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003006