blob: 1ae3ec7c18b0de977b1b781c8fee72d8357543bc [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 Krauseecd79182012-09-20 10:01:49 +0000126 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000127
Mathias Krauseecd79182012-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] ||
Fan Duea9884b2013-12-16 18:47:48 +0800184 attrs[XFRMA_TFCPAD] ||
185 (ntohl(p->id.spi) >= 0x10000))
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto out;
188 break;
189
190 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800191 if (attrs[XFRMA_ALG_COMP])
192 goto out;
193 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000194 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800195 !attrs[XFRMA_ALG_CRYPT] &&
196 !attrs[XFRMA_ALG_AEAD])
197 goto out;
198 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000199 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800200 attrs[XFRMA_ALG_CRYPT]) &&
201 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000203 if (attrs[XFRMA_TFCPAD] &&
204 p->mode != XFRM_MODE_TUNNEL)
205 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207
208 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700209 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800210 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700211 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000212 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000213 attrs[XFRMA_ALG_CRYPT] ||
214 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 goto out;
216 break;
217
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000218#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700219 case IPPROTO_DSTOPTS:
220 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700221 if (attrs[XFRMA_ALG_COMP] ||
222 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000223 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800224 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700225 attrs[XFRMA_ALG_CRYPT] ||
226 attrs[XFRMA_ENCAP] ||
227 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000228 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700229 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700230 goto out;
231 break;
232#endif
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 default:
235 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Herbert Xu1a6509d2008-01-28 19:37:29 -0800238 if ((err = verify_aead(attrs)))
239 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000240 if ((err = verify_auth_trunc(attrs)))
241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
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_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700246 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700248 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800249 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000250 if ((err = verify_replay(p, attrs)))
251 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 err = -EINVAL;
254 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700255 case XFRM_MODE_TRANSPORT:
256 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700257 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700258 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
260
261 default:
262 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 err = 0;
266
267out:
268 return err;
269}
270
271static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800272 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700273 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct xfrm_algo *p, *ualg;
276 struct xfrm_algo_desc *algo;
277
278 if (!rta)
279 return 0;
280
Thomas Graf5424f322007-08-22 14:01:33 -0700281 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 algo = get_byname(ualg->alg_name, 1);
284 if (!algo)
285 return -ENOSYS;
286 *props = algo->desc.sadb_alg_id;
287
Eric Dumazet0f99be02008-01-08 23:39:06 -0800288 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!p)
290 return -ENOMEM;
291
Herbert Xu04ff1262006-08-13 08:50:00 +1000292 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *algpp = p;
294 return 0;
295}
296
Martin Willi4447bb32009-11-25 00:29:52 +0000297static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
298 struct nlattr *rta)
299{
300 struct xfrm_algo *ualg;
301 struct xfrm_algo_auth *p;
302 struct xfrm_algo_desc *algo;
303
304 if (!rta)
305 return 0;
306
307 ualg = nla_data(rta);
308
309 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
310 if (!algo)
311 return -ENOSYS;
312 *props = algo->desc.sadb_alg_id;
313
314 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
315 if (!p)
316 return -ENOMEM;
317
318 strcpy(p->alg_name, algo->name);
319 p->alg_key_len = ualg->alg_key_len;
320 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
321 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
322
323 *algpp = p;
324 return 0;
325}
326
327static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
328 struct nlattr *rta)
329{
330 struct xfrm_algo_auth *p, *ualg;
331 struct xfrm_algo_desc *algo;
332
333 if (!rta)
334 return 0;
335
336 ualg = nla_data(rta);
337
338 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
339 if (!algo)
340 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000341 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
342 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000343 return -EINVAL;
344 *props = algo->desc.sadb_alg_id;
345
346 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
347 if (!p)
348 return -ENOMEM;
349
350 strcpy(p->alg_name, algo->name);
351 if (!p->alg_trunc_len)
352 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
353
354 *algpp = p;
355 return 0;
356}
357
Herbert Xu1a6509d2008-01-28 19:37:29 -0800358static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
359 struct nlattr *rta)
360{
361 struct xfrm_algo_aead *p, *ualg;
362 struct xfrm_algo_desc *algo;
363
364 if (!rta)
365 return 0;
366
367 ualg = nla_data(rta);
368
369 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
370 if (!algo)
371 return -ENOSYS;
372 *props = algo->desc.sadb_alg_id;
373
374 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
375 if (!p)
376 return -ENOMEM;
377
378 strcpy(p->alg_name, algo->name);
379 *algpp = p;
380 return 0;
381}
382
Steffen Klasserte2b19122011-03-28 19:47:30 +0000383static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
384 struct nlattr *rp)
385{
386 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000387 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000388
389 if (!replay_esn || !rp)
390 return 0;
391
392 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000393 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000394
Mathias Krauseecd79182012-09-20 10:01:49 +0000395 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000396 return -EINVAL;
397
398 return 0;
399}
400
Steffen Klassertd8647b72011-03-08 00:10:27 +0000401static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
402 struct xfrm_replay_state_esn **preplay_esn,
403 struct nlattr *rta)
404{
405 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000406 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000407
408 if (!rta)
409 return 0;
410
411 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000412 klen = xfrm_replay_state_esn_len(up);
413 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000414
Mathias Krauseecd79182012-09-20 10:01:49 +0000415 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000416 if (!p)
417 return -ENOMEM;
418
Mathias Krauseecd79182012-09-20 10:01:49 +0000419 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000420 if (!pp) {
421 kfree(p);
422 return -ENOMEM;
423 }
424
Mathias Krauseecd79182012-09-20 10:01:49 +0000425 memcpy(p, up, ulen);
426 memcpy(pp, up, ulen);
427
Steffen Klassertd8647b72011-03-08 00:10:27 +0000428 *replay_esn = p;
429 *preplay_esn = pp;
430
431 return 0;
432}
433
Joy Latten661697f2007-04-13 16:14:35 -0700434static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800435{
Trent Jaegerdf718372005-12-13 23:12:27 -0800436 int len = 0;
437
438 if (xfrm_ctx) {
439 len += sizeof(struct xfrm_user_sec_ctx);
440 len += xfrm_ctx->ctx_len;
441 }
442 return len;
443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
446{
447 memcpy(&x->id, &p->id, sizeof(x->id));
448 memcpy(&x->sel, &p->sel, sizeof(x->sel));
449 memcpy(&x->lft, &p->lft, sizeof(x->lft));
450 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800451 x->props.replay_window = min_t(unsigned int, p->replay_window,
452 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 x->props.reqid = p->reqid;
454 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700455 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700457
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700458 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700459 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800462/*
463 * someday when pfkey also has support, we could have the code
464 * somehow made shareable and move it to xfrm_state.c - JHS
465 *
466*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000467static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
468 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800469{
Thomas Graf5424f322007-08-22 14:01:33 -0700470 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000471 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700472 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
473 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
474 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800475
Steffen Klassertd8647b72011-03-08 00:10:27 +0000476 if (re) {
477 struct xfrm_replay_state_esn *replay_esn;
478 replay_esn = nla_data(re);
479 memcpy(x->replay_esn, replay_esn,
480 xfrm_replay_state_esn_len(replay_esn));
481 memcpy(x->preplay_esn, replay_esn,
482 xfrm_replay_state_esn_len(replay_esn));
483 }
484
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800485 if (rp) {
486 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700487 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800488 memcpy(&x->replay, replay, sizeof(*replay));
489 memcpy(&x->preplay, replay, sizeof(*replay));
490 }
491
492 if (lt) {
493 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700494 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800495 x->curlft.bytes = ltime->bytes;
496 x->curlft.packets = ltime->packets;
497 x->curlft.add_time = ltime->add_time;
498 x->curlft.use_time = ltime->use_time;
499 }
500
Thomas Grafcf5cb792007-08-22 13:59:04 -0700501 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700502 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800503
Thomas Grafcf5cb792007-08-22 13:59:04 -0700504 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700505 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800506}
507
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800508static struct xfrm_state *xfrm_state_construct(struct net *net,
509 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700510 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int *errp)
512{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800513 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int err = -ENOMEM;
515
516 if (!x)
517 goto error_no_put;
518
519 copy_from_user_state(x, p);
520
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100521 if (attrs[XFRMA_SA_EXTRA_FLAGS])
522 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
523
Herbert Xu1a6509d2008-01-28 19:37:29 -0800524 if ((err = attach_aead(&x->aead, &x->props.ealgo,
525 attrs[XFRMA_ALG_AEAD])))
526 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000527 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
528 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000530 if (!x->props.aalgo) {
531 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
532 attrs[XFRMA_ALG_AUTH])))
533 goto error;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
536 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700537 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto error;
539 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
540 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700541 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700543
544 if (attrs[XFRMA_ENCAP]) {
545 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
546 sizeof(*x->encap), GFP_KERNEL);
547 if (x->encap == NULL)
548 goto error;
549 }
550
Martin Willi35d28562010-12-08 04:37:49 +0000551 if (attrs[XFRMA_TFCPAD])
552 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
553
Thomas Graffd211502007-09-06 03:28:08 -0700554 if (attrs[XFRMA_COADDR]) {
555 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
556 sizeof(*x->coaddr), GFP_KERNEL);
557 if (x->coaddr == NULL)
558 goto error;
559 }
560
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000561 xfrm_mark_get(attrs, &x->mark);
562
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700563 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (err)
565 goto error;
566
Thomas Graffd211502007-09-06 03:28:08 -0700567 if (attrs[XFRMA_SEC_CTX] &&
568 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800569 goto error;
570
Steffen Klassertd8647b72011-03-08 00:10:27 +0000571 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
572 attrs[XFRMA_REPLAY_ESN_VAL])))
573 goto error;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800576 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800577 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800578 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800579
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000580 if ((err = xfrm_init_replay(x)))
581 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800582
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000583 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000584 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 return x;
587
588error:
589 x->km.state = XFRM_STATE_DEAD;
590 xfrm_state_put(x);
591error_no_put:
592 *errp = err;
593 return NULL;
594}
595
Christoph Hellwig22e70052007-01-02 15:22:30 -0800596static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700597 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800599 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700600 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct xfrm_state *x;
602 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700603 struct km_event c;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700604 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500605 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800606 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Thomas Graf35a7aa02007-08-22 14:00:40 -0700608 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (err)
610 return err;
611
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800612 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!x)
614 return err;
615
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700616 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
618 err = xfrm_state_add(x);
619 else
620 err = xfrm_state_update(x);
621
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800622 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400623 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (err < 0) {
626 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800627 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700631 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000632 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700633 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700634
635 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700636out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700637 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return err;
639}
640
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800641static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
642 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700643 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700644 int *errp)
645{
646 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000647 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700648 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000649 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700650
651 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
652 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000653 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700654 } else {
655 xfrm_address_t *saddr = NULL;
656
Thomas Graf35a7aa02007-08-22 14:00:40 -0700657 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700658 if (!saddr) {
659 err = -EINVAL;
660 goto out;
661 }
662
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800663 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000664 x = xfrm_state_lookup_byaddr(net, mark,
665 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800666 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700667 }
668
669 out:
670 if (!x && errp)
671 *errp = err;
672 return x;
673}
674
Christoph Hellwig22e70052007-01-02 15:22:30 -0800675static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700676 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800678 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700680 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700681 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700682 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700683 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500684 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800685 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800687 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700689 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
David S. Miller6f68dc32006-06-08 23:58:52 -0700691 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700692 goto out;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700695 err = -EPERM;
696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700699 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600700
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700701 if (err < 0)
702 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700703
704 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000705 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700706 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700707 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700709out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800710 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400711 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700712 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700713 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
717{
Mathias Krausef778a632012-09-19 11:33:39 +0000718 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 memcpy(&p->id, &x->id, sizeof(p->id));
720 memcpy(&p->sel, &x->sel, sizeof(p->sel));
721 memcpy(&p->lft, &x->lft, sizeof(p->lft));
722 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
723 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700724 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 p->mode = x->props.mode;
726 p->replay_window = x->props.replay_window;
727 p->reqid = x->props.reqid;
728 p->family = x->props.family;
729 p->flags = x->props.flags;
730 p->seq = x->km.seq;
731}
732
733struct xfrm_dump_info {
734 struct sk_buff *in_skb;
735 struct sk_buff *out_skb;
736 u32 nlmsg_seq;
737 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738};
739
Thomas Grafc0144be2007-08-22 13:55:43 -0700740static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
741{
Thomas Grafc0144be2007-08-22 13:55:43 -0700742 struct xfrm_user_sec_ctx *uctx;
743 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700744 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700745
746 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
747 if (attr == NULL)
748 return -EMSGSIZE;
749
750 uctx = nla_data(attr);
751 uctx->exttype = XFRMA_SEC_CTX;
752 uctx->len = ctx_size;
753 uctx->ctx_doi = s->ctx_doi;
754 uctx->ctx_alg = s->ctx_alg;
755 uctx->ctx_len = s->ctx_len;
756 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
757
758 return 0;
759}
760
Martin Willi4447bb32009-11-25 00:29:52 +0000761static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
762{
763 struct xfrm_algo *algo;
764 struct nlattr *nla;
765
766 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
767 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
768 if (!nla)
769 return -EMSGSIZE;
770
771 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000772 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000773 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
774 algo->alg_key_len = auth->alg_key_len;
775
776 return 0;
777}
778
Herbert Xu68325d32007-10-09 13:30:57 -0700779/* Don't change this without updating xfrm_sa_len! */
780static int copy_to_user_state_extra(struct xfrm_state *x,
781 struct xfrm_usersa_info *p,
782 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700784 int ret = 0;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 copy_to_user_state(x, p);
787
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100788 if (x->props.extra_flags) {
789 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
790 x->props.extra_flags);
791 if (ret)
792 goto out;
793 }
794
David S. Miller1d1e34d2012-06-27 21:57:03 -0700795 if (x->coaddr) {
796 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
797 if (ret)
798 goto out;
799 }
800 if (x->lastused) {
801 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
802 if (ret)
803 goto out;
804 }
805 if (x->aead) {
806 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
807 if (ret)
808 goto out;
809 }
810 if (x->aalg) {
811 ret = copy_to_user_auth(x->aalg, skb);
812 if (!ret)
813 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
814 xfrm_alg_auth_len(x->aalg), x->aalg);
815 if (ret)
816 goto out;
817 }
818 if (x->ealg) {
819 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
820 if (ret)
821 goto out;
822 }
823 if (x->calg) {
824 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
825 if (ret)
826 goto out;
827 }
828 if (x->encap) {
829 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
830 if (ret)
831 goto out;
832 }
833 if (x->tfcpad) {
834 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
835 if (ret)
836 goto out;
837 }
838 ret = xfrm_mark_put(skb, &x->mark);
839 if (ret)
840 goto out;
841 if (x->replay_esn) {
842 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
843 xfrm_replay_state_esn_len(x->replay_esn),
844 x->replay_esn);
845 if (ret)
846 goto out;
847 }
848 if (x->security)
849 ret = copy_sec_ctx(x->security, skb);
850out:
851 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700852}
853
854static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
855{
856 struct xfrm_dump_info *sp = ptr;
857 struct sk_buff *in_skb = sp->in_skb;
858 struct sk_buff *skb = sp->out_skb;
859 struct xfrm_usersa_info *p;
860 struct nlmsghdr *nlh;
861 int err;
862
Eric W. Biederman15e47302012-09-07 20:12:54 +0000863 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700864 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
865 if (nlh == NULL)
866 return -EMSGSIZE;
867
868 p = nlmsg_data(nlh);
869
870 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700871 if (err) {
872 nlmsg_cancel(skb, nlh);
873 return err;
874 }
Thomas Graf98250692007-08-22 12:47:26 -0700875 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Timo Teras4c563f72008-02-28 21:31:08 -0800879static int xfrm_dump_sa_done(struct netlink_callback *cb)
880{
881 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800882 struct sock *sk = cb->skb->sk;
883 struct net *net = sock_net(sk);
884
885 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800886 return 0;
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
890{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800891 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800892 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct xfrm_dump_info info;
894
Timo Teras4c563f72008-02-28 21:31:08 -0800895 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
896 sizeof(cb->args) - sizeof(cb->args[0]));
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 info.in_skb = cb->skb;
899 info.out_skb = skb;
900 info.nlmsg_seq = cb->nlh->nlmsg_seq;
901 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800902
903 if (!cb->args[0]) {
904 cb->args[0] = 1;
905 xfrm_state_walk_init(walk, 0);
906 }
907
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800908 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 return skb->len;
911}
912
913static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
914 struct xfrm_state *x, u32 seq)
915{
916 struct xfrm_dump_info info;
917 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000918 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Thomas Graf7deb2262007-08-22 13:57:39 -0700920 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!skb)
922 return ERR_PTR(-ENOMEM);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 info.in_skb = in_skb;
925 info.out_skb = skb;
926 info.nlmsg_seq = seq;
927 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Mathias Krause864745d2012-09-13 11:41:26 +0000929 err = dump_one_state(x, 0, &info);
930 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000932 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
935 return skb;
936}
937
Thomas Graf7deb2262007-08-22 13:57:39 -0700938static inline size_t xfrm_spdinfo_msgsize(void)
939{
940 return NLMSG_ALIGN(4)
941 + nla_total_size(sizeof(struct xfrmu_spdinfo))
942 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
943}
944
Alexey Dobriyane0710412010-01-23 13:37:10 +0000945static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000946 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700947{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700948 struct xfrmk_spdinfo si;
949 struct xfrmu_spdinfo spc;
950 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700951 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700952 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700953 u32 *f;
954
Eric W. Biederman15e47302012-09-07 20:12:54 +0000955 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300956 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700957 return -EMSGSIZE;
958
959 f = nlmsg_data(nlh);
960 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000961 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700962 spc.incnt = si.incnt;
963 spc.outcnt = si.outcnt;
964 spc.fwdcnt = si.fwdcnt;
965 spc.inscnt = si.inscnt;
966 spc.outscnt = si.outscnt;
967 spc.fwdscnt = si.fwdscnt;
968 sph.spdhcnt = si.spdhcnt;
969 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700970
David S. Miller1d1e34d2012-06-27 21:57:03 -0700971 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
972 if (!err)
973 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
974 if (err) {
975 nlmsg_cancel(skb, nlh);
976 return err;
977 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700978
979 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700980}
981
982static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700983 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700984{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800985 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700986 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700987 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000988 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700989 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700990
Thomas Graf7deb2262007-08-22 13:57:39 -0700991 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700992 if (r_skb == NULL)
993 return -ENOMEM;
994
Eric W. Biederman15e47302012-09-07 20:12:54 +0000995 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700996 BUG();
997
Eric W. Biederman15e47302012-09-07 20:12:54 +0000998 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700999}
1000
Thomas Graf7deb2262007-08-22 13:57:39 -07001001static inline size_t xfrm_sadinfo_msgsize(void)
1002{
1003 return NLMSG_ALIGN(4)
1004 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1005 + nla_total_size(4); /* XFRMA_SAD_CNT */
1006}
1007
Alexey Dobriyane0710412010-01-23 13:37:10 +00001008static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001009 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001010{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001011 struct xfrmk_sadinfo si;
1012 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001013 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001014 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001015 u32 *f;
1016
Eric W. Biederman15e47302012-09-07 20:12:54 +00001017 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001018 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001019 return -EMSGSIZE;
1020
1021 f = nlmsg_data(nlh);
1022 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001023 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001024
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001025 sh.sadhmcnt = si.sadhmcnt;
1026 sh.sadhcnt = si.sadhcnt;
1027
David S. Miller1d1e34d2012-06-27 21:57:03 -07001028 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1029 if (!err)
1030 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1031 if (err) {
1032 nlmsg_cancel(skb, nlh);
1033 return err;
1034 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001035
1036 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001037}
1038
1039static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001040 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001041{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001042 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001043 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001044 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001045 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001046 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001047
Thomas Graf7deb2262007-08-22 13:57:39 -07001048 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001049 if (r_skb == NULL)
1050 return -ENOMEM;
1051
Eric W. Biederman15e47302012-09-07 20:12:54 +00001052 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001053 BUG();
1054
Eric W. Biederman15e47302012-09-07 20:12:54 +00001055 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001056}
1057
Christoph Hellwig22e70052007-01-02 15:22:30 -08001058static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001059 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001061 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001062 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct xfrm_state *x;
1064 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001065 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001067 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (x == NULL)
1069 goto out_noput;
1070
1071 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1072 if (IS_ERR(resp_skb)) {
1073 err = PTR_ERR(resp_skb);
1074 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001075 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 xfrm_state_put(x);
1078out_noput:
1079 return err;
1080}
1081
Christoph Hellwig22e70052007-01-02 15:22:30 -08001082static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001083 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001085 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 struct xfrm_state *x;
1087 struct xfrm_userspi_info *p;
1088 struct sk_buff *resp_skb;
1089 xfrm_address_t *daddr;
1090 int family;
1091 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001092 u32 mark;
1093 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Thomas Graf7b67c852007-08-22 13:53:52 -07001095 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001096 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (err)
1098 goto out_noput;
1099
1100 family = p->info.family;
1101 daddr = &p->info.id.daddr;
1102
1103 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001104
1105 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001107 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001108 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 xfrm_state_put(x);
1110 x = NULL;
1111 }
1112 }
1113
1114 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001115 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 p->info.id.proto, daddr,
1117 &p->info.saddr, 1,
1118 family);
1119 err = -ENOENT;
1120 if (x == NULL)
1121 goto out_noput;
1122
Herbert Xu658b2192007-10-09 13:29:52 -07001123 err = xfrm_alloc_spi(x, p->min, p->max);
1124 if (err)
1125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Herbert Xu658b2192007-10-09 13:29:52 -07001127 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (IS_ERR(resp_skb)) {
1129 err = PTR_ERR(resp_skb);
1130 goto out;
1131 }
1132
Eric W. Biederman15e47302012-09-07 20:12:54 +00001133 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135out:
1136 xfrm_state_put(x);
1137out_noput:
1138 return err;
1139}
1140
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001141static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 switch (dir) {
1144 case XFRM_POLICY_IN:
1145 case XFRM_POLICY_OUT:
1146 case XFRM_POLICY_FWD:
1147 break;
1148
1149 default:
1150 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 return 0;
1154}
1155
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001156static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001157{
1158 switch (type) {
1159 case XFRM_POLICY_TYPE_MAIN:
1160#ifdef CONFIG_XFRM_SUB_POLICY
1161 case XFRM_POLICY_TYPE_SUB:
1162#endif
1163 break;
1164
1165 default:
1166 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001167 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001168
1169 return 0;
1170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1173{
Fan Due682adf2013-11-07 17:47:48 +08001174 int ret;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 switch (p->share) {
1177 case XFRM_SHARE_ANY:
1178 case XFRM_SHARE_SESSION:
1179 case XFRM_SHARE_USER:
1180 case XFRM_SHARE_UNIQUE:
1181 break;
1182
1183 default:
1184 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 switch (p->action) {
1188 case XFRM_POLICY_ALLOW:
1189 case XFRM_POLICY_BLOCK:
1190 break;
1191
1192 default:
1193 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 switch (p->sel.family) {
1197 case AF_INET:
1198 break;
1199
1200 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001201#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 break;
1203#else
1204 return -EAFNOSUPPORT;
1205#endif
1206
1207 default:
1208 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Fan Due682adf2013-11-07 17:47:48 +08001211 ret = verify_policy_dir(p->dir);
1212 if (ret)
1213 return ret;
1214 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1215 return -EINVAL;
1216
1217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Thomas Graf5424f322007-08-22 14:01:33 -07001220static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001221{
Thomas Graf5424f322007-08-22 14:01:33 -07001222 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001223 struct xfrm_user_sec_ctx *uctx;
1224
1225 if (!rt)
1226 return 0;
1227
Thomas Graf5424f322007-08-22 14:01:33 -07001228 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001229 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1233 int nr)
1234{
1235 int i;
1236
1237 xp->xfrm_nr = nr;
1238 for (i = 0; i < nr; i++, ut++) {
1239 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1240
1241 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1242 memcpy(&t->saddr, &ut->saddr,
1243 sizeof(xfrm_address_t));
1244 t->reqid = ut->reqid;
1245 t->mode = ut->mode;
1246 t->share = ut->share;
1247 t->optional = ut->optional;
1248 t->aalgos = ut->aalgos;
1249 t->ealgos = ut->ealgos;
1250 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001251 /* If all masks are ~0, then we allow all algorithms. */
1252 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001253 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255}
1256
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001257static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1258{
1259 int i;
1260
1261 if (nr > XFRM_MAX_DEPTH)
1262 return -EINVAL;
1263
1264 for (i = 0; i < nr; i++) {
1265 /* We never validated the ut->family value, so many
1266 * applications simply leave it at zero. The check was
1267 * never made and ut->family was ignored because all
1268 * templates could be assumed to have the same family as
1269 * the policy itself. Now that we will have ipv4-in-ipv6
1270 * and ipv6-in-ipv4 tunnels, this is no longer true.
1271 */
1272 if (!ut[i].family)
1273 ut[i].family = family;
1274
1275 switch (ut[i].family) {
1276 case AF_INET:
1277 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001278#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001279 case AF_INET6:
1280 break;
1281#endif
1282 default:
1283 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001284 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001285 }
1286
1287 return 0;
1288}
1289
Thomas Graf5424f322007-08-22 14:01:33 -07001290static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Thomas Graf5424f322007-08-22 14:01:33 -07001292 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (!rt) {
1295 pol->xfrm_nr = 0;
1296 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001297 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1298 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001299 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001301 err = validate_tmpl(nr, utmpl, pol->family);
1302 if (err)
1303 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Thomas Graf5424f322007-08-22 14:01:33 -07001305 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 return 0;
1308}
1309
Thomas Graf5424f322007-08-22 14:01:33 -07001310static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001311{
Thomas Graf5424f322007-08-22 14:01:33 -07001312 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001313 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001314 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001315 int err;
1316
1317 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001318 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001319 type = upt->type;
1320 }
1321
1322 err = verify_policy_type(type);
1323 if (err)
1324 return err;
1325
1326 *tp = type;
1327 return 0;
1328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1331{
1332 xp->priority = p->priority;
1333 xp->index = p->index;
1334 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1335 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1336 xp->action = p->action;
1337 xp->flags = p->flags;
1338 xp->family = p->sel.family;
1339 /* XXX xp->share = p->share; */
1340}
1341
1342static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1343{
Mathias Krause7b789832012-09-19 11:33:40 +00001344 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1346 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1347 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1348 p->priority = xp->priority;
1349 p->index = xp->index;
1350 p->sel.family = xp->family;
1351 p->dir = dir;
1352 p->action = xp->action;
1353 p->flags = xp->flags;
1354 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1355}
1356
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001357static 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 -07001358{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001359 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 int err;
1361
1362 if (!xp) {
1363 *errp = -ENOMEM;
1364 return NULL;
1365 }
1366
1367 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001368
Thomas Graf35a7aa02007-08-22 14:00:40 -07001369 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001370 if (err)
1371 goto error;
1372
Thomas Graf35a7aa02007-08-22 14:00:40 -07001373 if (!(err = copy_from_user_tmpl(xp, attrs)))
1374 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001375 if (err)
1376 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001378 xfrm_mark_get(attrs, &xp->mark);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001381 error:
1382 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001383 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001384 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001385 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Christoph Hellwig22e70052007-01-02 15:22:30 -08001388static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001389 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001391 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001392 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001394 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 int err;
1396 int excl;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001397 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001398 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001399 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 err = verify_newpolicy_info(p);
1402 if (err)
1403 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001404 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001405 if (err)
1406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001408 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (!xp)
1410 return err;
1411
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001412 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001413 * Aha! this is anti-netlink really i.e more pfkey derived
1414 * in netlink excl is a flag and you wouldnt need
1415 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1417 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001418 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001419 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001422 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 kfree(xp);
1424 return err;
1425 }
1426
Herbert Xuf60f6b82005-06-18 22:44:37 -07001427 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001428 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001429 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001430 km_policy_notify(xp, p->dir, &c);
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 xfrm_pol_put(xp);
1433
1434 return 0;
1435}
1436
1437static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1438{
1439 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1440 int i;
1441
1442 if (xp->xfrm_nr == 0)
1443 return 0;
1444
1445 for (i = 0; i < xp->xfrm_nr; i++) {
1446 struct xfrm_user_tmpl *up = &vec[i];
1447 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1448
Mathias Krause1f868402012-09-19 11:33:41 +00001449 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001451 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1453 up->reqid = kp->reqid;
1454 up->mode = kp->mode;
1455 up->share = kp->share;
1456 up->optional = kp->optional;
1457 up->aalgos = kp->aalgos;
1458 up->ealgos = kp->ealgos;
1459 up->calgos = kp->calgos;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Thomas Grafc0144be2007-08-22 13:55:43 -07001462 return nla_put(skb, XFRMA_TMPL,
1463 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001464}
1465
Serge Hallyn0d681622006-07-24 23:30:44 -07001466static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1467{
1468 if (x->security) {
1469 return copy_sec_ctx(x->security, skb);
1470 }
1471 return 0;
1472}
1473
1474static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1475{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001476 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001477 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001478 return 0;
1479}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001480static inline size_t userpolicy_type_attrsize(void)
1481{
1482#ifdef CONFIG_XFRM_SUB_POLICY
1483 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1484#else
1485 return 0;
1486#endif
1487}
Serge Hallyn0d681622006-07-24 23:30:44 -07001488
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001489#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001490static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001491{
Thomas Grafc0144be2007-08-22 13:55:43 -07001492 struct xfrm_userpolicy_type upt = {
1493 .type = type,
1494 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001495
Thomas Grafc0144be2007-08-22 13:55:43 -07001496 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001497}
1498
1499#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001500static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001501{
1502 return 0;
1503}
1504#endif
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1507{
1508 struct xfrm_dump_info *sp = ptr;
1509 struct xfrm_userpolicy_info *p;
1510 struct sk_buff *in_skb = sp->in_skb;
1511 struct sk_buff *skb = sp->out_skb;
1512 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001513 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Eric W. Biederman15e47302012-09-07 20:12:54 +00001515 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001516 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1517 if (nlh == NULL)
1518 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Thomas Graf7b67c852007-08-22 13:53:52 -07001520 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001522 err = copy_to_user_tmpl(xp, skb);
1523 if (!err)
1524 err = copy_to_user_sec_ctx(xp, skb);
1525 if (!err)
1526 err = copy_to_user_policy_type(xp->type, skb);
1527 if (!err)
1528 err = xfrm_mark_put(skb, &xp->mark);
1529 if (err) {
1530 nlmsg_cancel(skb, nlh);
1531 return err;
1532 }
Thomas Graf98250692007-08-22 12:47:26 -07001533 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Timo Teras4c563f72008-02-28 21:31:08 -08001537static int xfrm_dump_policy_done(struct netlink_callback *cb)
1538{
1539 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001540 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001541
Fan Du283bc9f2013-11-07 17:47:50 +08001542 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001543 return 0;
1544}
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1547{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001548 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001549 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 struct xfrm_dump_info info;
1551
Timo Teras4c563f72008-02-28 21:31:08 -08001552 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1553 sizeof(cb->args) - sizeof(cb->args[0]));
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 info.in_skb = cb->skb;
1556 info.out_skb = skb;
1557 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1558 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001559
1560 if (!cb->args[0]) {
1561 cb->args[0] = 1;
1562 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1563 }
1564
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001565 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 return skb->len;
1568}
1569
1570static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1571 struct xfrm_policy *xp,
1572 int dir, u32 seq)
1573{
1574 struct xfrm_dump_info info;
1575 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001576 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Thomas Graf7deb2262007-08-22 13:57:39 -07001578 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (!skb)
1580 return ERR_PTR(-ENOMEM);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 info.in_skb = in_skb;
1583 info.out_skb = skb;
1584 info.nlmsg_seq = seq;
1585 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Mathias Krausec2546372012-09-14 09:58:32 +00001587 err = dump_one_policy(xp, dir, 0, &info);
1588 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001590 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592
1593 return skb;
1594}
1595
Christoph Hellwig22e70052007-01-02 15:22:30 -08001596static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001597 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001599 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 struct xfrm_policy *xp;
1601 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001602 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001604 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001606 struct xfrm_mark m;
1607 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Thomas Graf7b67c852007-08-22 13:53:52 -07001609 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1611
Thomas Graf35a7aa02007-08-22 14:00:40 -07001612 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001613 if (err)
1614 return err;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 err = verify_policy_dir(p->dir);
1617 if (err)
1618 return err;
1619
1620 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001621 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001622 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001623 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001624 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001625
Thomas Graf35a7aa02007-08-22 14:00:40 -07001626 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001627 if (err)
1628 return err;
1629
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001630 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001631 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001632 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001633
Paul Moore03e1ad72008-04-12 19:07:52 -07001634 err = security_xfrm_policy_alloc(&ctx, uctx);
1635 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001636 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001637 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001638 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001639 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001640 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (xp == NULL)
1643 return -ENOENT;
1644
1645 if (!delete) {
1646 struct sk_buff *resp_skb;
1647
1648 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1649 if (IS_ERR(resp_skb)) {
1650 err = PTR_ERR(resp_skb);
1651 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001652 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001653 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001655 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001656 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001657 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001658 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001659
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001660 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001661 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1662 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001663
1664 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001665 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001666
Herbert Xue7443892005-06-18 22:44:18 -07001667 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001668 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001669 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001670 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001671 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001674out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001675 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001676 if (delete && err == 0)
1677 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return err;
1679}
1680
Christoph Hellwig22e70052007-01-02 15:22:30 -08001681static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001682 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001684 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001685 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001686 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001687 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001688 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001690 audit_info.loginuid = audit_get_loginuid(current);
1691 audit_info.sessionid = audit_get_sessionid(current);
1692 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001693 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001694 if (err) {
1695 if (err == -ESRCH) /* empty table */
1696 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001697 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001698 }
Herbert Xubf088672005-06-18 22:44:00 -07001699 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001700 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001701 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001702 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001703 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001704 km_state_notify(NULL, &c);
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return 0;
1707}
1708
Steffen Klassertd8647b72011-03-08 00:10:27 +00001709static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001710{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001711 size_t replay_size = x->replay_esn ?
1712 xfrm_replay_state_esn_len(x->replay_esn) :
1713 sizeof(struct xfrm_replay_state);
1714
Thomas Graf7deb2262007-08-22 13:57:39 -07001715 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001716 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001717 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001718 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001719 + nla_total_size(4) /* XFRM_AE_RTHR */
1720 + nla_total_size(4); /* XFRM_AE_ETHR */
1721}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001722
David S. Miller214e0052011-02-24 00:02:38 -05001723static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001724{
1725 struct xfrm_aevent_id *id;
1726 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001727 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001728
Eric W. Biederman15e47302012-09-07 20:12:54 +00001729 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001730 if (nlh == NULL)
1731 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001732
Thomas Graf7b67c852007-08-22 13:53:52 -07001733 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001734 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001735 id->sa_id.spi = x->id.spi;
1736 id->sa_id.family = x->props.family;
1737 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001738 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001739 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001740 id->flags = c->data.aevent;
1741
David S. Millerd0fde792012-03-29 04:02:26 -04001742 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001743 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1744 xfrm_replay_state_esn_len(x->replay_esn),
1745 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001746 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001747 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1748 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001749 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001750 if (err)
1751 goto out_cancel;
1752 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1753 if (err)
1754 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001755
David S. Miller1d1e34d2012-06-27 21:57:03 -07001756 if (id->flags & XFRM_AE_RTHR) {
1757 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1758 if (err)
1759 goto out_cancel;
1760 }
1761 if (id->flags & XFRM_AE_ETHR) {
1762 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1763 x->replay_maxage * 10 / HZ);
1764 if (err)
1765 goto out_cancel;
1766 }
1767 err = xfrm_mark_put(skb, &x->mark);
1768 if (err)
1769 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001770
Thomas Graf98250692007-08-22 12:47:26 -07001771 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001772
David S. Miller1d1e34d2012-06-27 21:57:03 -07001773out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001774 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001775 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001776}
1777
Christoph Hellwig22e70052007-01-02 15:22:30 -08001778static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001779 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001780{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001781 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001782 struct xfrm_state *x;
1783 struct sk_buff *r_skb;
1784 int err;
1785 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001786 u32 mark;
1787 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001788 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001789 struct xfrm_usersa_id *id = &p->sa_id;
1790
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001791 mark = xfrm_mark_get(attrs, &m);
1792
1793 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001794 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001795 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001796
1797 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1798 if (r_skb == NULL) {
1799 xfrm_state_put(x);
1800 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001801 }
1802
1803 /*
1804 * XXX: is this lock really needed - none of the other
1805 * gets lock (the concern is things getting updated
1806 * while we are still reading) - jhs
1807 */
1808 spin_lock_bh(&x->lock);
1809 c.data.aevent = p->flags;
1810 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001811 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001812
1813 if (build_aevent(r_skb, x, &c) < 0)
1814 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001815 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001816 spin_unlock_bh(&x->lock);
1817 xfrm_state_put(x);
1818 return err;
1819}
1820
Christoph Hellwig22e70052007-01-02 15:22:30 -08001821static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001822 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001823{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001824 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001825 struct xfrm_state *x;
1826 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001827 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001828 u32 mark = 0;
1829 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001830 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001831 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001832 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001833 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001834
Steffen Klassertd8647b72011-03-08 00:10:27 +00001835 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001836 return err;
1837
1838 /* pedantic mode - thou shalt sayeth replaceth */
1839 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1840 return err;
1841
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001842 mark = xfrm_mark_get(attrs, &m);
1843
1844 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 -08001845 if (x == NULL)
1846 return -ESRCH;
1847
1848 if (x->km.state != XFRM_STATE_VALID)
1849 goto out;
1850
Steffen Klassert4479ff72013-09-09 09:39:01 +02001851 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001852 if (err)
1853 goto out;
1854
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001855 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001856 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001857 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001858
1859 c.event = nlh->nlmsg_type;
1860 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001861 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001862 c.data.aevent = XFRM_AE_CU;
1863 km_state_notify(x, &c);
1864 err = 0;
1865out:
1866 xfrm_state_put(x);
1867 return err;
1868}
1869
Christoph Hellwig22e70052007-01-02 15:22:30 -08001870static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001871 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001873 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001874 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001875 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001876 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001877 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001878
Thomas Graf35a7aa02007-08-22 14:00:40 -07001879 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001880 if (err)
1881 return err;
1882
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001883 audit_info.loginuid = audit_get_loginuid(current);
1884 audit_info.sessionid = audit_get_sessionid(current);
1885 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001886 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001887 if (err) {
1888 if (err == -ESRCH) /* empty table */
1889 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001890 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001891 }
1892
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001893 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001894 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001895 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001896 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001897 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001898 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return 0;
1900}
1901
Christoph Hellwig22e70052007-01-02 15:22:30 -08001902static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001903 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001904{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001905 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001906 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001907 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001908 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001909 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001910 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001911 struct xfrm_mark m;
1912 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001913
Thomas Graf35a7aa02007-08-22 14:00:40 -07001914 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001915 if (err)
1916 return err;
1917
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001918 err = verify_policy_dir(p->dir);
1919 if (err)
1920 return err;
1921
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001922 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001923 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001924 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001925 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001926 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001927
Thomas Graf35a7aa02007-08-22 14:00:40 -07001928 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001929 if (err)
1930 return err;
1931
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001932 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001933 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001934 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001935
Paul Moore03e1ad72008-04-12 19:07:52 -07001936 err = security_xfrm_policy_alloc(&ctx, uctx);
1937 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001938 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001939 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001940 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001941 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001942 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001943 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001944 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001945 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001946
Timo Teräsea2dea92010-03-31 00:17:05 +00001947 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001948 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001949
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001950 err = 0;
1951 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001952 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001953 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001954 u32 sid;
1955
1956 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001957 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001958 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001959
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001960 } else {
1961 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001962 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001963 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001964 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001965
1966out:
1967 xfrm_pol_put(xp);
1968 return err;
1969}
1970
Christoph Hellwig22e70052007-01-02 15:22:30 -08001971static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001972 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001973{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001974 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001975 struct xfrm_state *x;
1976 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001977 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001978 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001979 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001980 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001981
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001982 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 -08001983
David S. Miller3a765aa2007-02-26 14:52:21 -08001984 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001985 if (x == NULL)
1986 return err;
1987
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001988 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001989 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001990 if (x->km.state != XFRM_STATE_VALID)
1991 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001992 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001993
Joy Latten161a09e2006-11-27 13:11:54 -06001994 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001995 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001996 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001997 u32 sid;
1998
1999 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002000 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04002001 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06002002 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002003 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002004out:
2005 spin_unlock_bh(&x->lock);
2006 xfrm_state_put(x);
2007 return err;
2008}
2009
Christoph Hellwig22e70052007-01-02 15:22:30 -08002010static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002011 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002012{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002013 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002014 struct xfrm_policy *xp;
2015 struct xfrm_user_tmpl *ut;
2016 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002017 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002018 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002019
Thomas Graf7b67c852007-08-22 13:53:52 -07002020 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002021 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002022 int err = -ENOMEM;
2023
2024 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002025 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002026
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002027 xfrm_mark_get(attrs, &mark);
2028
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002029 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002030 if (err)
2031 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002032
2033 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002034 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002035 if (!xp)
2036 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002037
2038 memcpy(&x->id, &ua->id, sizeof(ua->id));
2039 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2040 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002041 xp->mark.m = x->mark.m = mark.m;
2042 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002043 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002044 /* extract the templates and for each call km_key */
2045 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2046 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2047 memcpy(&x->id, &t->id, sizeof(x->id));
2048 x->props.mode = t->mode;
2049 x->props.reqid = t->reqid;
2050 x->props.family = ut->family;
2051 t->aalgos = ua->aalgos;
2052 t->ealgos = ua->ealgos;
2053 t->calgos = ua->calgos;
2054 err = km_query(x, t, xp);
2055
2056 }
2057
2058 kfree(x);
2059 kfree(xp);
2060
2061 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002062
2063bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002064 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002065free_state:
2066 kfree(x);
2067nomem:
2068 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002069}
2070
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002071#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002073 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002074 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002075{
Thomas Graf5424f322007-08-22 14:01:33 -07002076 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002077 struct xfrm_user_migrate *um;
2078 int i, num_migrate;
2079
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002080 if (k != NULL) {
2081 struct xfrm_user_kmaddress *uk;
2082
2083 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2084 memcpy(&k->local, &uk->local, sizeof(k->local));
2085 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2086 k->family = uk->family;
2087 k->reserved = uk->reserved;
2088 }
2089
Thomas Graf5424f322007-08-22 14:01:33 -07002090 um = nla_data(rt);
2091 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002092
2093 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2094 return -EINVAL;
2095
2096 for (i = 0; i < num_migrate; i++, um++, ma++) {
2097 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2098 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2099 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2100 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2101
2102 ma->proto = um->proto;
2103 ma->mode = um->mode;
2104 ma->reqid = um->reqid;
2105
2106 ma->old_family = um->old_family;
2107 ma->new_family = um->new_family;
2108 }
2109
2110 *num = i;
2111 return 0;
2112}
2113
2114static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002115 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002116{
Thomas Graf7b67c852007-08-22 13:53:52 -07002117 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002118 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002119 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002120 u8 type;
2121 int err;
2122 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002123 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002124
Thomas Graf35a7aa02007-08-22 14:00:40 -07002125 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002126 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002127
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002128 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2129
Thomas Graf5424f322007-08-22 14:01:33 -07002130 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002131 if (err)
2132 return err;
2133
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002134 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002135 if (err)
2136 return err;
2137
2138 if (!n)
2139 return 0;
2140
Fan Du8d549c42013-11-07 17:47:49 +08002141 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002142
2143 return 0;
2144}
2145#else
2146static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002147 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002148{
2149 return -ENOPROTOOPT;
2150}
2151#endif
2152
2153#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002154static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002155{
2156 struct xfrm_user_migrate um;
2157
2158 memset(&um, 0, sizeof(um));
2159 um.proto = m->proto;
2160 um.mode = m->mode;
2161 um.reqid = m->reqid;
2162 um.old_family = m->old_family;
2163 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2164 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2165 um.new_family = m->new_family;
2166 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2167 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2168
Thomas Grafc0144be2007-08-22 13:55:43 -07002169 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002170}
2171
David S. Miller183cad12011-02-24 00:28:01 -05002172static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002173{
2174 struct xfrm_user_kmaddress uk;
2175
2176 memset(&uk, 0, sizeof(uk));
2177 uk.family = k->family;
2178 uk.reserved = k->reserved;
2179 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002180 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002181
2182 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2183}
2184
2185static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002186{
2187 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002188 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2189 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2190 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002191}
2192
David S. Miller183cad12011-02-24 00:28:01 -05002193static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2194 int num_migrate, const struct xfrm_kmaddress *k,
2195 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002196{
David S. Miller183cad12011-02-24 00:28:01 -05002197 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002198 struct xfrm_userpolicy_id *pol_id;
2199 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002200 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002201
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002202 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2203 if (nlh == NULL)
2204 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002205
Thomas Graf7b67c852007-08-22 13:53:52 -07002206 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002207 /* copy data from selector, dir, and type to the pol_id */
2208 memset(pol_id, 0, sizeof(*pol_id));
2209 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2210 pol_id->dir = dir;
2211
David S. Miller1d1e34d2012-06-27 21:57:03 -07002212 if (k != NULL) {
2213 err = copy_to_user_kmaddress(k, skb);
2214 if (err)
2215 goto out_cancel;
2216 }
2217 err = copy_to_user_policy_type(type, skb);
2218 if (err)
2219 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002220 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002221 err = copy_to_user_migrate(mp, skb);
2222 if (err)
2223 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002224 }
2225
Thomas Graf98250692007-08-22 12:47:26 -07002226 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002227
2228out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002229 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002230 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002231}
2232
David S. Miller183cad12011-02-24 00:28:01 -05002233static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2234 const struct xfrm_migrate *m, int num_migrate,
2235 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002236{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002237 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002238 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002239
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002240 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002241 if (skb == NULL)
2242 return -ENOMEM;
2243
2244 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002245 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002246 BUG();
2247
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002248 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002249}
2250#else
David S. Miller183cad12011-02-24 00:28:01 -05002251static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2252 const struct xfrm_migrate *m, int num_migrate,
2253 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002254{
2255 return -ENOPROTOOPT;
2256}
2257#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002258
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002259#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002260
2261static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002262 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002263 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2264 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2265 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2266 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2267 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2268 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002269 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002270 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002271 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002272 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002273 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002274 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002275 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002276 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2277 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002278 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002279 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002280 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2281 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282};
2283
Thomas Graf492b5582005-05-03 14:26:40 -07002284#undef XMSGSIZE
2285
Thomas Grafcf5cb792007-08-22 13:59:04 -07002286static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002287 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2288 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2289 [XFRMA_LASTUSED] = { .type = NLA_U64},
2290 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002291 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002292 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2293 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2294 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2295 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2296 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2297 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2298 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2299 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2300 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2301 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2302 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2303 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2304 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2305 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002306 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002307 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002308 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002309 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002310 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002311};
2312
Mathias Krause05600a72013-02-24 14:10:27 +01002313static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002314 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002316 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002317} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2318 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2319 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2320 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002321 .dump = xfrm_dump_sa,
2322 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002323 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2324 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2325 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002326 .dump = xfrm_dump_policy,
2327 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002328 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002329 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002330 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002331 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2332 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002333 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002334 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2335 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002336 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2337 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002338 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002339 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002340 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341};
2342
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002343static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002345 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002346 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002347 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002348 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002352 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 type -= XFRM_MSG_BASE;
2355 link = &xfrm_dispatch[type];
2356
2357 /* All operations require privileges, even GET */
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002358 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002359 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Thomas Graf492b5582005-05-03 14:26:40 -07002361 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2362 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002363 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002367 {
2368 struct netlink_dump_control c = {
2369 .dump = link->dump,
2370 .done = link->done,
2371 };
2372 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
Thomas Graf35a7aa02007-08-22 14:00:40 -07002376 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002377 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002378 if (err < 0)
2379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002382 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Thomas Graf5424f322007-08-22 14:01:33 -07002384 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002387static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Fan Du283bc9f2013-11-07 17:47:50 +08002389 struct net *net = sock_net(skb->sk);
2390
2391 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002392 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002393 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
Thomas Graf7deb2262007-08-22 13:57:39 -07002396static inline size_t xfrm_expire_msgsize(void)
2397{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002398 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2399 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002400}
2401
David S. Miller214e0052011-02-24 00:02:38 -05002402static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 struct xfrm_user_expire *ue;
2405 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002406 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Eric W. Biederman15e47302012-09-07 20:12:54 +00002408 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002409 if (nlh == NULL)
2410 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Thomas Graf7b67c852007-08-22 13:53:52 -07002412 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002414 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
David S. Miller1d1e34d2012-06-27 21:57:03 -07002416 err = xfrm_mark_put(skb, &x->mark);
2417 if (err)
2418 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002419
Thomas Graf98250692007-08-22 12:47:26 -07002420 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
David S. Miller214e0052011-02-24 00:02:38 -05002423static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002425 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct sk_buff *skb;
2427
Thomas Graf7deb2262007-08-22 13:57:39 -07002428 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (skb == NULL)
2430 return -ENOMEM;
2431
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002432 if (build_expire(skb, x, c) < 0) {
2433 kfree_skb(skb);
2434 return -EMSGSIZE;
2435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002437 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
David S. Miller214e0052011-02-24 00:02:38 -05002440static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002441{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002442 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002443 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002444
Steffen Klassertd8647b72011-03-08 00:10:27 +00002445 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002446 if (skb == NULL)
2447 return -ENOMEM;
2448
2449 if (build_aevent(skb, x, c) < 0)
2450 BUG();
2451
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002452 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002453}
2454
David S. Miller214e0052011-02-24 00:02:38 -05002455static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002457 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002458 struct xfrm_usersa_flush *p;
2459 struct nlmsghdr *nlh;
2460 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002461 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002462
Thomas Graf7deb2262007-08-22 13:57:39 -07002463 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002464 if (skb == NULL)
2465 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002466
Eric W. Biederman15e47302012-09-07 20:12:54 +00002467 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002468 if (nlh == NULL) {
2469 kfree_skb(skb);
2470 return -EMSGSIZE;
2471 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002472
Thomas Graf7b67c852007-08-22 13:53:52 -07002473 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002474 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002475
Thomas Graf98250692007-08-22 12:47:26 -07002476 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002477
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002478 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002479}
2480
Thomas Graf7deb2262007-08-22 13:57:39 -07002481static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002482{
Thomas Graf7deb2262007-08-22 13:57:39 -07002483 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002484 if (x->aead)
2485 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002486 if (x->aalg) {
2487 l += nla_total_size(sizeof(struct xfrm_algo) +
2488 (x->aalg->alg_key_len + 7) / 8);
2489 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2490 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002491 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002492 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002493 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002494 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002495 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002496 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002497 if (x->tfcpad)
2498 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002499 if (x->replay_esn)
2500 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002501 if (x->security)
2502 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2503 x->security->ctx_len);
2504 if (x->coaddr)
2505 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002506 if (x->props.extra_flags)
2507 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002508
Herbert Xud26f3982007-11-13 21:47:08 -08002509 /* Must count x->lastused as it may become non-zero behind our back. */
2510 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002511
2512 return l;
2513}
2514
David S. Miller214e0052011-02-24 00:02:38 -05002515static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002516{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002517 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002518 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002519 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002520 struct nlmsghdr *nlh;
2521 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002522 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002523 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002524
2525 headlen = sizeof(*p);
2526 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002527 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002528 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002529 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002530 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002531 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002532
Thomas Graf7deb2262007-08-22 13:57:39 -07002533 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002534 if (skb == NULL)
2535 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002536
Eric W. Biederman15e47302012-09-07 20:12:54 +00002537 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002538 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002539 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002540 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002541
Thomas Graf7b67c852007-08-22 13:53:52 -07002542 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002543 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002544 struct nlattr *attr;
2545
Thomas Graf7b67c852007-08-22 13:53:52 -07002546 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002547 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2548 id->spi = x->id.spi;
2549 id->family = x->props.family;
2550 id->proto = x->id.proto;
2551
Thomas Grafc0144be2007-08-22 13:55:43 -07002552 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002553 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002554 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002555 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002556
2557 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002558 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002559 err = copy_to_user_state_extra(x, p, skb);
2560 if (err)
2561 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002562
Thomas Graf98250692007-08-22 12:47:26 -07002563 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002564
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002565 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002566
David S. Miller1d1e34d2012-06-27 21:57:03 -07002567out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002568 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002569 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002570}
2571
David S. Miller214e0052011-02-24 00:02:38 -05002572static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002573{
2574
2575 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002576 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002577 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002578 case XFRM_MSG_NEWAE:
2579 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002580 case XFRM_MSG_DELSA:
2581 case XFRM_MSG_UPDSA:
2582 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002583 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002584 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002585 return xfrm_notify_sa_flush(c);
2586 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002587 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2588 c->event);
2589 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002590 }
2591
2592 return 0;
2593
2594}
2595
Thomas Graf7deb2262007-08-22 13:57:39 -07002596static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2597 struct xfrm_policy *xp)
2598{
2599 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2600 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002601 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002602 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2603 + userpolicy_type_attrsize();
2604}
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002607 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002609 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 struct xfrm_user_acquire *ua;
2611 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002612 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002614 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2615 if (nlh == NULL)
2616 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Thomas Graf7b67c852007-08-22 13:53:52 -07002618 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 memcpy(&ua->id, &x->id, sizeof(ua->id));
2620 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2621 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002622 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 ua->aalgos = xt->aalgos;
2624 ua->ealgos = xt->ealgos;
2625 ua->calgos = xt->calgos;
2626 ua->seq = x->km.seq = seq;
2627
David S. Miller1d1e34d2012-06-27 21:57:03 -07002628 err = copy_to_user_tmpl(xp, skb);
2629 if (!err)
2630 err = copy_to_user_state_sec_ctx(x, skb);
2631 if (!err)
2632 err = copy_to_user_policy_type(xp->type, skb);
2633 if (!err)
2634 err = xfrm_mark_put(skb, &xp->mark);
2635 if (err) {
2636 nlmsg_cancel(skb, nlh);
2637 return err;
2638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Thomas Graf98250692007-08-22 12:47:26 -07002640 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
2643static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002644 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002646 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Thomas Graf7deb2262007-08-22 13:57:39 -07002649 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (skb == NULL)
2651 return -ENOMEM;
2652
Fan Du65e07362012-08-15 10:13:47 +08002653 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 BUG();
2655
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002656 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
2659/* User gives us xfrm_user_policy_info followed by an array of 0
2660 * or more templates.
2661 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002662static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 u8 *data, int len, int *dir)
2664{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002665 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2667 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2668 struct xfrm_policy *xp;
2669 int nr;
2670
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002671 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 case AF_INET:
2673 if (opt != IP_XFRM_POLICY) {
2674 *dir = -EOPNOTSUPP;
2675 return NULL;
2676 }
2677 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002678#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 case AF_INET6:
2680 if (opt != IPV6_XFRM_POLICY) {
2681 *dir = -EOPNOTSUPP;
2682 return NULL;
2683 }
2684 break;
2685#endif
2686 default:
2687 *dir = -EINVAL;
2688 return NULL;
2689 }
2690
2691 *dir = -EINVAL;
2692
2693 if (len < sizeof(*p) ||
2694 verify_newpolicy_info(p))
2695 return NULL;
2696
2697 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002698 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return NULL;
2700
Herbert Xua4f1bac2005-07-26 15:43:17 -07002701 if (p->dir > XFRM_POLICY_OUT)
2702 return NULL;
2703
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002704 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (xp == NULL) {
2706 *dir = -ENOBUFS;
2707 return NULL;
2708 }
2709
2710 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002711 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 copy_templates(xp, ut, nr);
2713
2714 *dir = p->dir;
2715
2716 return xp;
2717}
2718
Thomas Graf7deb2262007-08-22 13:57:39 -07002719static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2720{
2721 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2722 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2723 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002724 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002725 + userpolicy_type_attrsize();
2726}
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002729 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002732 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002733 struct nlmsghdr *nlh;
2734 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Eric W. Biederman15e47302012-09-07 20:12:54 +00002736 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002737 if (nlh == NULL)
2738 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Thomas Graf7b67c852007-08-22 13:53:52 -07002740 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002742 err = copy_to_user_tmpl(xp, skb);
2743 if (!err)
2744 err = copy_to_user_sec_ctx(xp, skb);
2745 if (!err)
2746 err = copy_to_user_policy_type(xp->type, skb);
2747 if (!err)
2748 err = xfrm_mark_put(skb, &xp->mark);
2749 if (err) {
2750 nlmsg_cancel(skb, nlh);
2751 return err;
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 upe->hard = !!hard;
2754
Thomas Graf98250692007-08-22 12:47:26 -07002755 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
David S. Miller214e0052011-02-24 00:02:38 -05002758static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002760 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Thomas Graf7deb2262007-08-22 13:57:39 -07002763 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (skb == NULL)
2765 return -ENOMEM;
2766
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002767 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 BUG();
2769
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002770 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
David S. Miller214e0052011-02-24 00:02:38 -05002773static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002774{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002775 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002776 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002777 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002778 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002779 struct nlmsghdr *nlh;
2780 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002781 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002782
2783 headlen = sizeof(*p);
2784 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002785 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002786 headlen = sizeof(*id);
2787 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002788 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002789 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002790 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002791
Thomas Graf7deb2262007-08-22 13:57:39 -07002792 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002793 if (skb == NULL)
2794 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002795
Eric W. Biederman15e47302012-09-07 20:12:54 +00002796 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002797 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002798 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002799 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002800
Thomas Graf7b67c852007-08-22 13:53:52 -07002801 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002802 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002803 struct nlattr *attr;
2804
Thomas Graf7b67c852007-08-22 13:53:52 -07002805 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002806 memset(id, 0, sizeof(*id));
2807 id->dir = dir;
2808 if (c->data.byid)
2809 id->index = xp->index;
2810 else
2811 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2812
Thomas Grafc0144be2007-08-22 13:55:43 -07002813 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002814 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002815 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002816 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002817
2818 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002819 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002820
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002821 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002822 err = copy_to_user_tmpl(xp, skb);
2823 if (!err)
2824 err = copy_to_user_policy_type(xp->type, skb);
2825 if (!err)
2826 err = xfrm_mark_put(skb, &xp->mark);
2827 if (err)
2828 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002829
Thomas Graf98250692007-08-22 12:47:26 -07002830 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002831
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002832 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002833
David S. Miller1d1e34d2012-06-27 21:57:03 -07002834out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002835 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002836 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002837}
2838
David S. Miller214e0052011-02-24 00:02:38 -05002839static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002840{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002841 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002842 struct nlmsghdr *nlh;
2843 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002844 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002845
Thomas Graf7deb2262007-08-22 13:57:39 -07002846 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002847 if (skb == NULL)
2848 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002849
Eric W. Biederman15e47302012-09-07 20:12:54 +00002850 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002851 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002852 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002853 goto out_free_skb;
2854 err = copy_to_user_policy_type(c->data.type, skb);
2855 if (err)
2856 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002857
Thomas Graf98250692007-08-22 12:47:26 -07002858 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002859
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002860 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002861
David S. Miller1d1e34d2012-06-27 21:57:03 -07002862out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002863 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002864 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002865}
2866
David S. Miller214e0052011-02-24 00:02:38 -05002867static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002868{
2869
2870 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002871 case XFRM_MSG_NEWPOLICY:
2872 case XFRM_MSG_UPDPOLICY:
2873 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002874 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002875 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002876 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002877 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002878 return xfrm_exp_policy_notify(xp, dir, c);
2879 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002880 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2881 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002882 }
2883
2884 return 0;
2885
2886}
2887
Thomas Graf7deb2262007-08-22 13:57:39 -07002888static inline size_t xfrm_report_msgsize(void)
2889{
2890 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2891}
2892
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002893static int build_report(struct sk_buff *skb, u8 proto,
2894 struct xfrm_selector *sel, xfrm_address_t *addr)
2895{
2896 struct xfrm_user_report *ur;
2897 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002898
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002899 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2900 if (nlh == NULL)
2901 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002902
Thomas Graf7b67c852007-08-22 13:53:52 -07002903 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002904 ur->proto = proto;
2905 memcpy(&ur->sel, sel, sizeof(ur->sel));
2906
David S. Miller1d1e34d2012-06-27 21:57:03 -07002907 if (addr) {
2908 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2909 if (err) {
2910 nlmsg_cancel(skb, nlh);
2911 return err;
2912 }
2913 }
Thomas Graf98250692007-08-22 12:47:26 -07002914 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002915}
2916
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002917static int xfrm_send_report(struct net *net, u8 proto,
2918 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002919{
2920 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002921
Thomas Graf7deb2262007-08-22 13:57:39 -07002922 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002923 if (skb == NULL)
2924 return -ENOMEM;
2925
2926 if (build_report(skb, proto, sel, addr) < 0)
2927 BUG();
2928
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002929 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002930}
2931
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002932static inline size_t xfrm_mapping_msgsize(void)
2933{
2934 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2935}
2936
2937static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2938 xfrm_address_t *new_saddr, __be16 new_sport)
2939{
2940 struct xfrm_user_mapping *um;
2941 struct nlmsghdr *nlh;
2942
2943 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2944 if (nlh == NULL)
2945 return -EMSGSIZE;
2946
2947 um = nlmsg_data(nlh);
2948
2949 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2950 um->id.spi = x->id.spi;
2951 um->id.family = x->props.family;
2952 um->id.proto = x->id.proto;
2953 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2954 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2955 um->new_sport = new_sport;
2956 um->old_sport = x->encap->encap_sport;
2957 um->reqid = x->props.reqid;
2958
2959 return nlmsg_end(skb, nlh);
2960}
2961
2962static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2963 __be16 sport)
2964{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002965 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002966 struct sk_buff *skb;
2967
2968 if (x->id.proto != IPPROTO_ESP)
2969 return -EINVAL;
2970
2971 if (!x->encap)
2972 return -EINVAL;
2973
2974 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2975 if (skb == NULL)
2976 return -ENOMEM;
2977
2978 if (build_mapping(skb, x, ipaddr, sport) < 0)
2979 BUG();
2980
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002981 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002982}
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984static struct xfrm_mgr netlink_mgr = {
2985 .id = "netlink",
2986 .notify = xfrm_send_state_notify,
2987 .acquire = xfrm_send_acquire,
2988 .compile_policy = xfrm_compile_policy,
2989 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002990 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002991 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002992 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993};
2994
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002995static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Patrick McHardybe336902006-03-20 22:40:54 -08002997 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002998 struct netlink_kernel_cfg cfg = {
2999 .groups = XFRMNLGRP_MAX,
3000 .input = xfrm_netlink_rcv,
3001 };
Patrick McHardybe336902006-03-20 22:40:54 -08003002
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003003 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003004 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003006 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003007 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return 0;
3009}
3010
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003011static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003012{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003013 struct net *net;
3014 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003015 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003016 synchronize_net();
3017 list_for_each_entry(net, net_exit_list, exit_list)
3018 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003019}
3020
3021static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003022 .init = xfrm_user_net_init,
3023 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003024};
3025
3026static int __init xfrm_user_init(void)
3027{
3028 int rv;
3029
3030 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3031
3032 rv = register_pernet_subsys(&xfrm_user_net_ops);
3033 if (rv < 0)
3034 return rv;
3035 rv = xfrm_register_km(&netlink_mgr);
3036 if (rv < 0)
3037 unregister_pernet_subsys(&xfrm_user_net_ops);
3038 return rv;
3039}
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041static void __exit xfrm_user_exit(void)
3042{
3043 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003044 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
3047module_init(xfrm_user_init);
3048module_exit(xfrm_user_exit);
3049MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003050MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003051