blob: 2cade027629913a3bd630b885a0c8c0cf4984ca5 [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
Sowmini Varadhane33d4f12015-10-21 11:48:25 -040034#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Graf5424f322007-08-22 14:01:33 -070036static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Thomas Graf5424f322007-08-22 14:01:33 -070038 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct xfrm_algo *algp;
40
41 if (!rt)
42 return 0;
43
Thomas Graf5424f322007-08-22 14:01:33 -070044 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080045 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070046 return -EINVAL;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 switch (type) {
49 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53
54 default:
55 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
59 return 0;
60}
61
Martin Willi4447bb32009-11-25 00:29:52 +000062static int verify_auth_trunc(struct nlattr **attrs)
63{
64 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
65 struct xfrm_algo_auth *algp;
66
67 if (!rt)
68 return 0;
69
70 algp = nla_data(rt);
71 if (nla_len(rt) < xfrm_alg_auth_len(algp))
72 return -EINVAL;
73
74 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 return 0;
76}
77
Herbert Xu1a6509d2008-01-28 19:37:29 -080078static int verify_aead(struct nlattr **attrs)
79{
80 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
81 struct xfrm_algo_aead *algp;
82
83 if (!rt)
84 return 0;
85
86 algp = nla_data(rt);
87 if (nla_len(rt) < aead_len(algp))
88 return -EINVAL;
89
90 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
91 return 0;
92}
93
Thomas Graf5424f322007-08-22 14:01:33 -070094static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070095 xfrm_address_t **addrp)
96{
Thomas Graf5424f322007-08-22 14:01:33 -070097 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070098
Thomas Grafcf5cb792007-08-22 13:59:04 -070099 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700100 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700101}
Trent Jaegerdf718372005-12-13 23:12:27 -0800102
Thomas Graf5424f322007-08-22 14:01:33 -0700103static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800104{
Thomas Graf5424f322007-08-22 14:01:33 -0700105 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800106 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800107
108 if (!rt)
109 return 0;
110
Thomas Graf5424f322007-08-22 14:01:33 -0700111 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700112 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800113 return -EINVAL;
114
115 return 0;
116}
117
Steffen Klassertd8647b72011-03-08 00:10:27 +0000118static inline int verify_replay(struct xfrm_usersa_info *p,
119 struct nlattr **attrs)
120{
121 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000122 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000123
Mathias Krauseecd79182012-09-20 10:01:49 +0000124 if (p->flags & XFRM_STATE_ESN) {
125 if (!rt)
126 return -EINVAL;
127
128 rs = nla_data(rt);
129
130 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
131 return -EINVAL;
132
133 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
134 nla_len(rt) != sizeof(*rs))
135 return -EINVAL;
136 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000137
Steffen Klassertd8647b72011-03-08 00:10:27 +0000138 if (!rt)
139 return 0;
140
Fan Du01714102014-01-18 09:54:28 +0800141 /* As only ESP and AH support ESN feature. */
142 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000143 return -EINVAL;
144
Steffen Klassertd8647b72011-03-08 00:10:27 +0000145 if (p->replay_window != 0)
146 return -EINVAL;
147
148 return 0;
149}
Trent Jaegerdf718372005-12-13 23:12:27 -0800150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700152 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int err;
155
156 err = -EINVAL;
157 switch (p->family) {
158 case AF_INET:
159 break;
160
161 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000162#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
164#else
165 err = -EAFNOSUPPORT;
166 goto out;
167#endif
168
169 default:
170 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 err = -EINVAL;
174 switch (p->id.proto) {
175 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000176 if ((!attrs[XFRMA_ALG_AUTH] &&
177 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800178 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700179 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000180 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200181 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out;
183 break;
184
185 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800186 if (attrs[XFRMA_ALG_COMP])
187 goto out;
188 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000189 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800190 !attrs[XFRMA_ALG_CRYPT] &&
191 !attrs[XFRMA_ALG_AEAD])
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])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000198 if (attrs[XFRMA_TFCPAD] &&
199 p->mode != XFRM_MODE_TUNNEL)
200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202
203 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700204 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800205 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700206 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000207 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000208 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200209 attrs[XFRMA_TFCPAD] ||
210 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto out;
212 break;
213
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000214#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700215 case IPPROTO_DSTOPTS:
216 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700217 if (attrs[XFRMA_ALG_COMP] ||
218 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000219 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800220 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700221 attrs[XFRMA_ALG_CRYPT] ||
222 attrs[XFRMA_ENCAP] ||
223 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000224 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700225 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700226 goto out;
227 break;
228#endif
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 default:
231 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Herbert Xu1a6509d2008-01-28 19:37:29 -0800234 if ((err = verify_aead(attrs)))
235 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000236 if ((err = verify_auth_trunc(attrs)))
237 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700238 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800245 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000246 if ((err = verify_replay(p, attrs)))
247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 err = -EINVAL;
250 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700251 case XFRM_MODE_TRANSPORT:
252 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700253 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700254 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256
257 default:
258 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 err = 0;
262
263out:
264 return err;
265}
266
267static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800268 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700269 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct xfrm_algo *p, *ualg;
272 struct xfrm_algo_desc *algo;
273
274 if (!rta)
275 return 0;
276
Thomas Graf5424f322007-08-22 14:01:33 -0700277 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 algo = get_byname(ualg->alg_name, 1);
280 if (!algo)
281 return -ENOSYS;
282 *props = algo->desc.sadb_alg_id;
283
Eric Dumazet0f99be02008-01-08 23:39:06 -0800284 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (!p)
286 return -ENOMEM;
287
Herbert Xu04ff1262006-08-13 08:50:00 +1000288 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *algpp = p;
290 return 0;
291}
292
Herbert Xu69b01372015-05-27 16:03:45 +0800293static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
294{
295 struct xfrm_algo *p, *ualg;
296 struct xfrm_algo_desc *algo;
297
298 if (!rta)
299 return 0;
300
301 ualg = nla_data(rta);
302
303 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
304 if (!algo)
305 return -ENOSYS;
306 x->props.ealgo = algo->desc.sadb_alg_id;
307
308 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
309 if (!p)
310 return -ENOMEM;
311
312 strcpy(p->alg_name, algo->name);
313 x->ealg = p;
314 x->geniv = algo->uinfo.encr.geniv;
315 return 0;
316}
317
Martin Willi4447bb32009-11-25 00:29:52 +0000318static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
319 struct nlattr *rta)
320{
321 struct xfrm_algo *ualg;
322 struct xfrm_algo_auth *p;
323 struct xfrm_algo_desc *algo;
324
325 if (!rta)
326 return 0;
327
328 ualg = nla_data(rta);
329
330 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
331 if (!algo)
332 return -ENOSYS;
333 *props = algo->desc.sadb_alg_id;
334
335 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
336 if (!p)
337 return -ENOMEM;
338
339 strcpy(p->alg_name, algo->name);
340 p->alg_key_len = ualg->alg_key_len;
341 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
342 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
343
344 *algpp = p;
345 return 0;
346}
347
348static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
349 struct nlattr *rta)
350{
351 struct xfrm_algo_auth *p, *ualg;
352 struct xfrm_algo_desc *algo;
353
354 if (!rta)
355 return 0;
356
357 ualg = nla_data(rta);
358
359 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
360 if (!algo)
361 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800362 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000363 return -EINVAL;
364 *props = algo->desc.sadb_alg_id;
365
366 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
367 if (!p)
368 return -ENOMEM;
369
370 strcpy(p->alg_name, algo->name);
371 if (!p->alg_trunc_len)
372 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
373
374 *algpp = p;
375 return 0;
376}
377
Herbert Xu69b01372015-05-27 16:03:45 +0800378static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800379{
380 struct xfrm_algo_aead *p, *ualg;
381 struct xfrm_algo_desc *algo;
382
383 if (!rta)
384 return 0;
385
386 ualg = nla_data(rta);
387
388 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
389 if (!algo)
390 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800391 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800392
393 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
394 if (!p)
395 return -ENOMEM;
396
397 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800398 x->aead = p;
399 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800400 return 0;
401}
402
Steffen Klasserte2b19122011-03-28 19:47:30 +0000403static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
404 struct nlattr *rp)
405{
406 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000407 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000408
409 if (!replay_esn || !rp)
410 return 0;
411
412 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000413 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000414
Andy Whitcroft79191ea2017-03-23 07:45:44 +0000415 /* Check the overall length and the internal bitmap length to avoid
416 * potential overflow. */
417 if (nla_len(rp) < ulen ||
418 xfrm_replay_state_esn_len(replay_esn) != ulen ||
419 replay_esn->bmp_len != up->bmp_len)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000420 return -EINVAL;
421
Andy Whitcroft64a54652017-03-22 07:29:31 +0000422 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
423 return -EINVAL;
424
Steffen Klasserte2b19122011-03-28 19:47:30 +0000425 return 0;
426}
427
Steffen Klassertd8647b72011-03-08 00:10:27 +0000428static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
429 struct xfrm_replay_state_esn **preplay_esn,
430 struct nlattr *rta)
431{
432 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000433 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000434
435 if (!rta)
436 return 0;
437
438 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000439 klen = xfrm_replay_state_esn_len(up);
440 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000441
Mathias Krauseecd79182012-09-20 10:01:49 +0000442 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000443 if (!p)
444 return -ENOMEM;
445
Mathias Krauseecd79182012-09-20 10:01:49 +0000446 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000447 if (!pp) {
448 kfree(p);
449 return -ENOMEM;
450 }
451
Mathias Krauseecd79182012-09-20 10:01:49 +0000452 memcpy(p, up, ulen);
453 memcpy(pp, up, ulen);
454
Steffen Klassertd8647b72011-03-08 00:10:27 +0000455 *replay_esn = p;
456 *preplay_esn = pp;
457
458 return 0;
459}
460
Joy Latten661697f2007-04-13 16:14:35 -0700461static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800462{
Trent Jaegerdf718372005-12-13 23:12:27 -0800463 int len = 0;
464
465 if (xfrm_ctx) {
466 len += sizeof(struct xfrm_user_sec_ctx);
467 len += xfrm_ctx->ctx_len;
468 }
469 return len;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
473{
474 memcpy(&x->id, &p->id, sizeof(x->id));
475 memcpy(&x->sel, &p->sel, sizeof(x->sel));
476 memcpy(&x->lft, &p->lft, sizeof(x->lft));
477 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800478 x->props.replay_window = min_t(unsigned int, p->replay_window,
479 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 x->props.reqid = p->reqid;
481 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700482 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700484
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700485 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700486 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800489/*
490 * someday when pfkey also has support, we could have the code
491 * somehow made shareable and move it to xfrm_state.c - JHS
492 *
493*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000494static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
495 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800496{
Thomas Graf5424f322007-08-22 14:01:33 -0700497 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000498 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700499 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
500 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
501 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800502
Steffen Klassertd8647b72011-03-08 00:10:27 +0000503 if (re) {
504 struct xfrm_replay_state_esn *replay_esn;
505 replay_esn = nla_data(re);
506 memcpy(x->replay_esn, replay_esn,
507 xfrm_replay_state_esn_len(replay_esn));
508 memcpy(x->preplay_esn, replay_esn,
509 xfrm_replay_state_esn_len(replay_esn));
510 }
511
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800512 if (rp) {
513 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700514 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800515 memcpy(&x->replay, replay, sizeof(*replay));
516 memcpy(&x->preplay, replay, sizeof(*replay));
517 }
518
519 if (lt) {
520 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700521 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800522 x->curlft.bytes = ltime->bytes;
523 x->curlft.packets = ltime->packets;
524 x->curlft.add_time = ltime->add_time;
525 x->curlft.use_time = ltime->use_time;
526 }
527
Thomas Grafcf5cb792007-08-22 13:59:04 -0700528 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700529 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800530
Thomas Grafcf5cb792007-08-22 13:59:04 -0700531 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700532 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800533}
534
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800535static struct xfrm_state *xfrm_state_construct(struct net *net,
536 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700537 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int *errp)
539{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800540 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int err = -ENOMEM;
542
543 if (!x)
544 goto error_no_put;
545
546 copy_from_user_state(x, p);
547
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100548 if (attrs[XFRMA_SA_EXTRA_FLAGS])
549 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
550
Herbert Xu69b01372015-05-27 16:03:45 +0800551 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800552 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000553 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
554 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000556 if (!x->props.aalgo) {
557 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
558 attrs[XFRMA_ALG_AUTH])))
559 goto error;
560 }
Herbert Xu69b01372015-05-27 16:03:45 +0800561 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto error;
563 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
564 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700565 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700567
568 if (attrs[XFRMA_ENCAP]) {
569 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
570 sizeof(*x->encap), GFP_KERNEL);
571 if (x->encap == NULL)
572 goto error;
573 }
574
Martin Willi35d28562010-12-08 04:37:49 +0000575 if (attrs[XFRMA_TFCPAD])
576 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
577
Thomas Graffd211502007-09-06 03:28:08 -0700578 if (attrs[XFRMA_COADDR]) {
579 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
580 sizeof(*x->coaddr), GFP_KERNEL);
581 if (x->coaddr == NULL)
582 goto error;
583 }
584
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000585 xfrm_mark_get(attrs, &x->mark);
586
Lorenzo Colittibf25c792017-08-11 02:11:33 +0900587 if (attrs[XFRMA_OUTPUT_MARK])
588 x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
589
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700590 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (err)
592 goto error;
593
Mathias Krause2f30ea52016-09-08 18:09:57 +0200594 if (attrs[XFRMA_SEC_CTX]) {
595 err = security_xfrm_state_alloc(x,
596 nla_data(attrs[XFRMA_SEC_CTX]));
597 if (err)
598 goto error;
599 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800600
Steffen Klassertd8647b72011-03-08 00:10:27 +0000601 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
602 attrs[XFRMA_REPLAY_ESN_VAL])))
603 goto error;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800606 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800607 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800608 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800609
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000610 if ((err = xfrm_init_replay(x)))
611 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800612
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000613 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000614 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 return x;
617
618error:
619 x->km.state = XFRM_STATE_DEAD;
620 xfrm_state_put(x);
621error_no_put:
622 *errp = err;
623 return NULL;
624}
625
Christoph Hellwig22e70052007-01-02 15:22:30 -0800626static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700627 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800629 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700630 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct xfrm_state *x;
632 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700633 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Thomas Graf35a7aa02007-08-22 14:00:40 -0700635 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (err)
637 return err;
638
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800639 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!x)
641 return err;
642
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700643 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
645 err = xfrm_state_add(x);
646 else
647 err = xfrm_state_update(x);
648
Tetsuo Handa2e710292014-04-22 21:48:30 +0900649 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (err < 0) {
652 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800653 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700654 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700657 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000658 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700659 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700660
661 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700662out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700663 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return err;
665}
666
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800667static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
668 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700669 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700670 int *errp)
671{
672 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000673 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700674 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000675 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700676
677 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
678 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000679 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700680 } else {
681 xfrm_address_t *saddr = NULL;
682
Thomas Graf35a7aa02007-08-22 14:00:40 -0700683 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700684 if (!saddr) {
685 err = -EINVAL;
686 goto out;
687 }
688
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800689 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000690 x = xfrm_state_lookup_byaddr(net, mark,
691 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800692 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700693 }
694
695 out:
696 if (!x && errp)
697 *errp = err;
698 return x;
699}
700
Christoph Hellwig22e70052007-01-02 15:22:30 -0800701static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700702 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800704 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700706 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700707 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700708 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800710 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
David S. Miller6f68dc32006-06-08 23:58:52 -0700714 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700715 goto out;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700718 err = -EPERM;
719 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700722 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600723
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700724 if (err < 0)
725 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700726
727 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000728 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700729 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700730 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700732out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900733 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700734 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700735 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
739{
Mathias Krausef778a632012-09-19 11:33:39 +0000740 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 memcpy(&p->id, &x->id, sizeof(p->id));
742 memcpy(&p->sel, &x->sel, sizeof(p->sel));
743 memcpy(&p->lft, &x->lft, sizeof(p->lft));
744 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400745 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
746 put_unaligned(x->stats.replay, &p->stats.replay);
747 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700748 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 p->mode = x->props.mode;
750 p->replay_window = x->props.replay_window;
751 p->reqid = x->props.reqid;
752 p->family = x->props.family;
753 p->flags = x->props.flags;
754 p->seq = x->km.seq;
755}
756
757struct xfrm_dump_info {
758 struct sk_buff *in_skb;
759 struct sk_buff *out_skb;
760 u32 nlmsg_seq;
761 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762};
763
Thomas Grafc0144be2007-08-22 13:55:43 -0700764static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
765{
Thomas Grafc0144be2007-08-22 13:55:43 -0700766 struct xfrm_user_sec_ctx *uctx;
767 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700768 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700769
770 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
771 if (attr == NULL)
772 return -EMSGSIZE;
773
774 uctx = nla_data(attr);
775 uctx->exttype = XFRMA_SEC_CTX;
776 uctx->len = ctx_size;
777 uctx->ctx_doi = s->ctx_doi;
778 uctx->ctx_alg = s->ctx_alg;
779 uctx->ctx_len = s->ctx_len;
780 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
781
782 return 0;
783}
784
Martin Willi4447bb32009-11-25 00:29:52 +0000785static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
786{
787 struct xfrm_algo *algo;
788 struct nlattr *nla;
789
790 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
791 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
792 if (!nla)
793 return -EMSGSIZE;
794
795 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000796 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000797 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
798 algo->alg_key_len = auth->alg_key_len;
799
800 return 0;
801}
802
Herbert Xu68325d32007-10-09 13:30:57 -0700803/* Don't change this without updating xfrm_sa_len! */
804static int copy_to_user_state_extra(struct xfrm_state *x,
805 struct xfrm_usersa_info *p,
806 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700808 int ret = 0;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 copy_to_user_state(x, p);
811
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100812 if (x->props.extra_flags) {
813 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
814 x->props.extra_flags);
815 if (ret)
816 goto out;
817 }
818
David S. Miller1d1e34d2012-06-27 21:57:03 -0700819 if (x->coaddr) {
820 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
821 if (ret)
822 goto out;
823 }
824 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200825 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
826 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700827 if (ret)
828 goto out;
829 }
830 if (x->aead) {
831 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
832 if (ret)
833 goto out;
834 }
835 if (x->aalg) {
836 ret = copy_to_user_auth(x->aalg, skb);
837 if (!ret)
838 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
839 xfrm_alg_auth_len(x->aalg), x->aalg);
840 if (ret)
841 goto out;
842 }
843 if (x->ealg) {
844 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
845 if (ret)
846 goto out;
847 }
848 if (x->calg) {
849 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
850 if (ret)
851 goto out;
852 }
853 if (x->encap) {
854 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
855 if (ret)
856 goto out;
857 }
858 if (x->tfcpad) {
859 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
860 if (ret)
861 goto out;
862 }
863 ret = xfrm_mark_put(skb, &x->mark);
864 if (ret)
865 goto out;
dingzhif293a5e2014-10-30 09:39:36 +0100866 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700867 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
868 xfrm_replay_state_esn_len(x->replay_esn),
869 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100870 else
871 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
872 &x->replay);
873 if (ret)
874 goto out;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700875 if (x->security)
876 ret = copy_sec_ctx(x->security, skb);
Lorenzo Colittibf25c792017-08-11 02:11:33 +0900877 if (x->props.output_mark) {
878 ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
879 if (ret)
880 goto out;
881 }
David S. Miller1d1e34d2012-06-27 21:57:03 -0700882out:
883 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700884}
885
886static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
887{
888 struct xfrm_dump_info *sp = ptr;
889 struct sk_buff *in_skb = sp->in_skb;
890 struct sk_buff *skb = sp->out_skb;
891 struct xfrm_usersa_info *p;
892 struct nlmsghdr *nlh;
893 int err;
894
Eric W. Biederman15e47302012-09-07 20:12:54 +0000895 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700896 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
897 if (nlh == NULL)
898 return -EMSGSIZE;
899
900 p = nlmsg_data(nlh);
901
902 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700903 if (err) {
904 nlmsg_cancel(skb, nlh);
905 return err;
906 }
Thomas Graf98250692007-08-22 12:47:26 -0700907 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Timo Teras4c563f72008-02-28 21:31:08 -0800911static int xfrm_dump_sa_done(struct netlink_callback *cb)
912{
913 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800914 struct sock *sk = cb->skb->sk;
915 struct net *net = sock_net(sk);
916
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200917 if (cb->args[0])
918 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800919 return 0;
920}
921
Nicolas Dichteld3623092014-02-14 15:30:36 +0100922static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
924{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800925 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800926 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct xfrm_dump_info info;
928
Timo Teras4c563f72008-02-28 21:31:08 -0800929 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
930 sizeof(cb->args) - sizeof(cb->args[0]));
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 info.in_skb = cb->skb;
933 info.out_skb = skb;
934 info.nlmsg_seq = cb->nlh->nlmsg_seq;
935 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800936
937 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100938 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100939 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100940 u8 proto = 0;
941 int err;
942
Nicolas Dichteld3623092014-02-14 15:30:36 +0100943 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
944 xfrma_policy);
945 if (err < 0)
946 return err;
947
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100948 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +0200949 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
950 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +0100951 if (filter == NULL)
952 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100953 }
954
955 if (attrs[XFRMA_PROTO])
956 proto = nla_get_u8(attrs[XFRMA_PROTO]);
957
958 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200959 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -0800960 }
961
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800962 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 return skb->len;
965}
966
967static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
968 struct xfrm_state *x, u32 seq)
969{
970 struct xfrm_dump_info info;
971 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000972 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Thomas Graf7deb2262007-08-22 13:57:39 -0700974 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!skb)
976 return ERR_PTR(-ENOMEM);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 info.in_skb = in_skb;
979 info.out_skb = skb;
980 info.nlmsg_seq = seq;
981 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Mathias Krause864745d2012-09-13 11:41:26 +0000983 err = dump_one_state(x, 0, &info);
984 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000986 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
989 return skb;
990}
991
Michal Kubecek21ee5432014-06-03 10:26:06 +0200992/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
993 * Must be called with RCU read lock.
994 */
995static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
996 u32 pid, unsigned int group)
997{
998 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
999
1000 if (nlsk)
1001 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1002 else
1003 return -1;
1004}
1005
Thomas Graf7deb2262007-08-22 13:57:39 -07001006static inline size_t xfrm_spdinfo_msgsize(void)
1007{
1008 return NLMSG_ALIGN(4)
1009 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001010 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1011 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1012 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -07001013}
1014
Alexey Dobriyane0710412010-01-23 13:37:10 +00001015static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001016 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001017{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001018 struct xfrmk_spdinfo si;
1019 struct xfrmu_spdinfo spc;
1020 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001021 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001022 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001023 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001024 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001025 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001026
Eric W. Biederman15e47302012-09-07 20:12:54 +00001027 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001028 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001029 return -EMSGSIZE;
1030
1031 f = nlmsg_data(nlh);
1032 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001033 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001034 spc.incnt = si.incnt;
1035 spc.outcnt = si.outcnt;
1036 spc.fwdcnt = si.fwdcnt;
1037 spc.inscnt = si.inscnt;
1038 spc.outscnt = si.outscnt;
1039 spc.fwdscnt = si.fwdscnt;
1040 sph.spdhcnt = si.spdhcnt;
1041 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001042
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001043 do {
1044 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1045
1046 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1047 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1048 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1049 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1050 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1051
David S. Miller1d1e34d2012-06-27 21:57:03 -07001052 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1053 if (!err)
1054 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001055 if (!err)
1056 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1057 if (!err)
1058 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001059 if (err) {
1060 nlmsg_cancel(skb, nlh);
1061 return err;
1062 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001063
Johannes Berg053c0952015-01-16 22:09:00 +01001064 nlmsg_end(skb, nlh);
1065 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001066}
1067
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001068static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1069 struct nlattr **attrs)
1070{
1071 struct net *net = sock_net(skb->sk);
1072 struct xfrmu_spdhthresh *thresh4 = NULL;
1073 struct xfrmu_spdhthresh *thresh6 = NULL;
1074
1075 /* selector prefixlen thresholds to hash policies */
1076 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1077 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1078
1079 if (nla_len(rta) < sizeof(*thresh4))
1080 return -EINVAL;
1081 thresh4 = nla_data(rta);
1082 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1083 return -EINVAL;
1084 }
1085 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1086 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1087
1088 if (nla_len(rta) < sizeof(*thresh6))
1089 return -EINVAL;
1090 thresh6 = nla_data(rta);
1091 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1092 return -EINVAL;
1093 }
1094
1095 if (thresh4 || thresh6) {
1096 write_seqlock(&net->xfrm.policy_hthresh.lock);
1097 if (thresh4) {
1098 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1099 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1100 }
1101 if (thresh6) {
1102 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1103 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1104 }
1105 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1106
1107 xfrm_policy_hash_rebuild(net);
1108 }
1109
1110 return 0;
1111}
1112
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001113static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001114 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001115{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001116 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001117 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001118 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001119 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001120 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001121
Thomas Graf7deb2262007-08-22 13:57:39 -07001122 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001123 if (r_skb == NULL)
1124 return -ENOMEM;
1125
Eric W. Biederman15e47302012-09-07 20:12:54 +00001126 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001127 BUG();
1128
Eric W. Biederman15e47302012-09-07 20:12:54 +00001129 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001130}
1131
Thomas Graf7deb2262007-08-22 13:57:39 -07001132static inline size_t xfrm_sadinfo_msgsize(void)
1133{
1134 return NLMSG_ALIGN(4)
1135 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1136 + nla_total_size(4); /* XFRMA_SAD_CNT */
1137}
1138
Alexey Dobriyane0710412010-01-23 13:37:10 +00001139static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001140 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001141{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001142 struct xfrmk_sadinfo si;
1143 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001144 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001145 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001146 u32 *f;
1147
Eric W. Biederman15e47302012-09-07 20:12:54 +00001148 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001149 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001150 return -EMSGSIZE;
1151
1152 f = nlmsg_data(nlh);
1153 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001154 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001155
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001156 sh.sadhmcnt = si.sadhmcnt;
1157 sh.sadhcnt = si.sadhcnt;
1158
David S. Miller1d1e34d2012-06-27 21:57:03 -07001159 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1160 if (!err)
1161 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1162 if (err) {
1163 nlmsg_cancel(skb, nlh);
1164 return err;
1165 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001166
Johannes Berg053c0952015-01-16 22:09:00 +01001167 nlmsg_end(skb, nlh);
1168 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001169}
1170
1171static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001172 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001173{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001174 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001175 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001176 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001177 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001178 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001179
Thomas Graf7deb2262007-08-22 13:57:39 -07001180 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001181 if (r_skb == NULL)
1182 return -ENOMEM;
1183
Eric W. Biederman15e47302012-09-07 20:12:54 +00001184 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001185 BUG();
1186
Eric W. Biederman15e47302012-09-07 20:12:54 +00001187 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001188}
1189
Christoph Hellwig22e70052007-01-02 15:22:30 -08001190static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001191 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001193 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001194 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 struct xfrm_state *x;
1196 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001197 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001199 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (x == NULL)
1201 goto out_noput;
1202
1203 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1204 if (IS_ERR(resp_skb)) {
1205 err = PTR_ERR(resp_skb);
1206 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001207 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 xfrm_state_put(x);
1210out_noput:
1211 return err;
1212}
1213
Christoph Hellwig22e70052007-01-02 15:22:30 -08001214static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001215 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001217 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 struct xfrm_state *x;
1219 struct xfrm_userspi_info *p;
1220 struct sk_buff *resp_skb;
1221 xfrm_address_t *daddr;
1222 int family;
1223 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001224 u32 mark;
1225 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Thomas Graf7b67c852007-08-22 13:53:52 -07001227 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001228 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (err)
1230 goto out_noput;
1231
1232 family = p->info.family;
1233 daddr = &p->info.id.daddr;
1234
1235 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001236
1237 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001239 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001240 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 xfrm_state_put(x);
1242 x = NULL;
1243 }
1244 }
1245
1246 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001247 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 p->info.id.proto, daddr,
1249 &p->info.saddr, 1,
1250 family);
1251 err = -ENOENT;
1252 if (x == NULL)
1253 goto out_noput;
1254
Herbert Xu658b2192007-10-09 13:29:52 -07001255 err = xfrm_alloc_spi(x, p->min, p->max);
1256 if (err)
1257 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Herbert Xu658b2192007-10-09 13:29:52 -07001259 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (IS_ERR(resp_skb)) {
1261 err = PTR_ERR(resp_skb);
1262 goto out;
1263 }
1264
Eric W. Biederman15e47302012-09-07 20:12:54 +00001265 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267out:
1268 xfrm_state_put(x);
1269out_noput:
1270 return err;
1271}
1272
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001273static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 switch (dir) {
1276 case XFRM_POLICY_IN:
1277 case XFRM_POLICY_OUT:
1278 case XFRM_POLICY_FWD:
1279 break;
1280
1281 default:
1282 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 return 0;
1286}
1287
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001288static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001289{
1290 switch (type) {
1291 case XFRM_POLICY_TYPE_MAIN:
1292#ifdef CONFIG_XFRM_SUB_POLICY
1293 case XFRM_POLICY_TYPE_SUB:
1294#endif
1295 break;
1296
1297 default:
1298 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001299 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001300
1301 return 0;
1302}
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1305{
Fan Due682adf2013-11-07 17:47:48 +08001306 int ret;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 switch (p->share) {
1309 case XFRM_SHARE_ANY:
1310 case XFRM_SHARE_SESSION:
1311 case XFRM_SHARE_USER:
1312 case XFRM_SHARE_UNIQUE:
1313 break;
1314
1315 default:
1316 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 switch (p->action) {
1320 case XFRM_POLICY_ALLOW:
1321 case XFRM_POLICY_BLOCK:
1322 break;
1323
1324 default:
1325 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 switch (p->sel.family) {
1329 case AF_INET:
1330 break;
1331
1332 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001333#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
1335#else
1336 return -EAFNOSUPPORT;
1337#endif
1338
1339 default:
1340 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Fan Due682adf2013-11-07 17:47:48 +08001343 ret = verify_policy_dir(p->dir);
1344 if (ret)
1345 return ret;
1346 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1347 return -EINVAL;
1348
1349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Thomas Graf5424f322007-08-22 14:01:33 -07001352static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001353{
Thomas Graf5424f322007-08-22 14:01:33 -07001354 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001355 struct xfrm_user_sec_ctx *uctx;
1356
1357 if (!rt)
1358 return 0;
1359
Thomas Graf5424f322007-08-22 14:01:33 -07001360 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001361 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1365 int nr)
1366{
1367 int i;
1368
1369 xp->xfrm_nr = nr;
1370 for (i = 0; i < nr; i++, ut++) {
1371 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1372
1373 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1374 memcpy(&t->saddr, &ut->saddr,
1375 sizeof(xfrm_address_t));
1376 t->reqid = ut->reqid;
1377 t->mode = ut->mode;
1378 t->share = ut->share;
1379 t->optional = ut->optional;
1380 t->aalgos = ut->aalgos;
1381 t->ealgos = ut->ealgos;
1382 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001383 /* If all masks are ~0, then we allow all algorithms. */
1384 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001385 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387}
1388
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001389static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1390{
1391 int i;
1392
1393 if (nr > XFRM_MAX_DEPTH)
1394 return -EINVAL;
1395
1396 for (i = 0; i < nr; i++) {
1397 /* We never validated the ut->family value, so many
1398 * applications simply leave it at zero. The check was
1399 * never made and ut->family was ignored because all
1400 * templates could be assumed to have the same family as
1401 * the policy itself. Now that we will have ipv4-in-ipv6
1402 * and ipv6-in-ipv4 tunnels, this is no longer true.
1403 */
1404 if (!ut[i].family)
1405 ut[i].family = family;
1406
1407 switch (ut[i].family) {
1408 case AF_INET:
1409 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001410#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001411 case AF_INET6:
1412 break;
1413#endif
1414 default:
1415 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001416 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001417 }
1418
1419 return 0;
1420}
1421
Thomas Graf5424f322007-08-22 14:01:33 -07001422static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Thomas Graf5424f322007-08-22 14:01:33 -07001424 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 if (!rt) {
1427 pol->xfrm_nr = 0;
1428 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001429 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1430 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001431 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001433 err = validate_tmpl(nr, utmpl, pol->family);
1434 if (err)
1435 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Thomas Graf5424f322007-08-22 14:01:33 -07001437 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 return 0;
1440}
1441
Thomas Graf5424f322007-08-22 14:01:33 -07001442static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001443{
Thomas Graf5424f322007-08-22 14:01:33 -07001444 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001445 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001446 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001447 int err;
1448
1449 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001450 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001451 type = upt->type;
1452 }
1453
1454 err = verify_policy_type(type);
1455 if (err)
1456 return err;
1457
1458 *tp = type;
1459 return 0;
1460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1463{
1464 xp->priority = p->priority;
1465 xp->index = p->index;
1466 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1467 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1468 xp->action = p->action;
1469 xp->flags = p->flags;
1470 xp->family = p->sel.family;
1471 /* XXX xp->share = p->share; */
1472}
1473
1474static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1475{
Mathias Krause7b789832012-09-19 11:33:40 +00001476 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1478 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1479 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1480 p->priority = xp->priority;
1481 p->index = xp->index;
1482 p->sel.family = xp->family;
1483 p->dir = dir;
1484 p->action = xp->action;
1485 p->flags = xp->flags;
1486 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1487}
1488
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001489static 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 -07001490{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001491 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int err;
1493
1494 if (!xp) {
1495 *errp = -ENOMEM;
1496 return NULL;
1497 }
1498
1499 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001500
Thomas Graf35a7aa02007-08-22 14:00:40 -07001501 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001502 if (err)
1503 goto error;
1504
Thomas Graf35a7aa02007-08-22 14:00:40 -07001505 if (!(err = copy_from_user_tmpl(xp, attrs)))
1506 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001507 if (err)
1508 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001510 xfrm_mark_get(attrs, &xp->mark);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001513 error:
1514 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001515 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001516 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001517 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Christoph Hellwig22e70052007-01-02 15:22:30 -08001520static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001521 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001523 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001524 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001526 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int err;
1528 int excl;
1529
1530 err = verify_newpolicy_info(p);
1531 if (err)
1532 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001533 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001534 if (err)
1535 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001537 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (!xp)
1539 return err;
1540
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001541 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001542 * Aha! this is anti-netlink really i.e more pfkey derived
1543 * in netlink excl is a flag and you wouldnt need
1544 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1546 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001547 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001550 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 kfree(xp);
1552 return err;
1553 }
1554
Herbert Xuf60f6b82005-06-18 22:44:37 -07001555 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001556 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001557 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001558 km_policy_notify(xp, p->dir, &c);
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 xfrm_pol_put(xp);
1561
1562 return 0;
1563}
1564
1565static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1566{
1567 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1568 int i;
1569
1570 if (xp->xfrm_nr == 0)
1571 return 0;
1572
1573 for (i = 0; i < xp->xfrm_nr; i++) {
1574 struct xfrm_user_tmpl *up = &vec[i];
1575 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1576
Mathias Krause1f868402012-09-19 11:33:41 +00001577 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001579 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1581 up->reqid = kp->reqid;
1582 up->mode = kp->mode;
1583 up->share = kp->share;
1584 up->optional = kp->optional;
1585 up->aalgos = kp->aalgos;
1586 up->ealgos = kp->ealgos;
1587 up->calgos = kp->calgos;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Thomas Grafc0144be2007-08-22 13:55:43 -07001590 return nla_put(skb, XFRMA_TMPL,
1591 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001592}
1593
Serge Hallyn0d681622006-07-24 23:30:44 -07001594static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1595{
1596 if (x->security) {
1597 return copy_sec_ctx(x->security, skb);
1598 }
1599 return 0;
1600}
1601
1602static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1603{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001604 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001605 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001606 return 0;
1607}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001608static inline size_t userpolicy_type_attrsize(void)
1609{
1610#ifdef CONFIG_XFRM_SUB_POLICY
1611 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1612#else
1613 return 0;
1614#endif
1615}
Serge Hallyn0d681622006-07-24 23:30:44 -07001616
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001617#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001618static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001619{
Thomas Grafc0144be2007-08-22 13:55:43 -07001620 struct xfrm_userpolicy_type upt = {
1621 .type = type,
1622 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001623
Thomas Grafc0144be2007-08-22 13:55:43 -07001624 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001625}
1626
1627#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001628static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001629{
1630 return 0;
1631}
1632#endif
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1635{
1636 struct xfrm_dump_info *sp = ptr;
1637 struct xfrm_userpolicy_info *p;
1638 struct sk_buff *in_skb = sp->in_skb;
1639 struct sk_buff *skb = sp->out_skb;
1640 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001641 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Eric W. Biederman15e47302012-09-07 20:12:54 +00001643 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001644 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1645 if (nlh == NULL)
1646 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Thomas Graf7b67c852007-08-22 13:53:52 -07001648 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001650 err = copy_to_user_tmpl(xp, skb);
1651 if (!err)
1652 err = copy_to_user_sec_ctx(xp, skb);
1653 if (!err)
1654 err = copy_to_user_policy_type(xp->type, skb);
1655 if (!err)
1656 err = xfrm_mark_put(skb, &xp->mark);
1657 if (err) {
1658 nlmsg_cancel(skb, nlh);
1659 return err;
1660 }
Thomas Graf98250692007-08-22 12:47:26 -07001661 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Timo Teras4c563f72008-02-28 21:31:08 -08001665static int xfrm_dump_policy_done(struct netlink_callback *cb)
1666{
Herbert Xu543aabb2017-10-19 20:51:10 +08001667 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Fan Du283bc9f2013-11-07 17:47:50 +08001668 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001669
Fan Du283bc9f2013-11-07 17:47:50 +08001670 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001671 return 0;
1672}
1673
Herbert Xu543aabb2017-10-19 20:51:10 +08001674static int xfrm_dump_policy_start(struct netlink_callback *cb)
1675{
1676 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1677
1678 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1679
1680 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1681 return 0;
1682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1685{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001686 struct net *net = sock_net(skb->sk);
Herbert Xu543aabb2017-10-19 20:51:10 +08001687 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 struct xfrm_dump_info info;
1689
1690 info.in_skb = cb->skb;
1691 info.out_skb = skb;
1692 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1693 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001694
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001695 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 return skb->len;
1698}
1699
1700static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1701 struct xfrm_policy *xp,
1702 int dir, u32 seq)
1703{
1704 struct xfrm_dump_info info;
1705 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001706 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Suren Baghdasaryan34803e72017-08-15 15:12:24 -07001708 err = verify_policy_dir(dir);
1709 if (err)
1710 return ERR_PTR(err);
1711
Thomas Graf7deb2262007-08-22 13:57:39 -07001712 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (!skb)
1714 return ERR_PTR(-ENOMEM);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 info.in_skb = in_skb;
1717 info.out_skb = skb;
1718 info.nlmsg_seq = seq;
1719 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Mathias Krausec2546372012-09-14 09:58:32 +00001721 err = dump_one_policy(xp, dir, 0, &info);
1722 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001724 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
1727 return skb;
1728}
1729
Christoph Hellwig22e70052007-01-02 15:22:30 -08001730static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001731 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001733 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct xfrm_policy *xp;
1735 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001736 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001738 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001740 struct xfrm_mark m;
1741 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Thomas Graf7b67c852007-08-22 13:53:52 -07001743 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1745
Thomas Graf35a7aa02007-08-22 14:00:40 -07001746 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001747 if (err)
1748 return err;
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 err = verify_policy_dir(p->dir);
1751 if (err)
1752 return err;
1753
1754 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001755 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001756 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001757 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001758 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001759
Thomas Graf35a7aa02007-08-22 14:00:40 -07001760 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001761 if (err)
1762 return err;
1763
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001764 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001765 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001766 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001767
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001768 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001769 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001770 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001771 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001772 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001773 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001774 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (xp == NULL)
1777 return -ENOENT;
1778
1779 if (!delete) {
1780 struct sk_buff *resp_skb;
1781
1782 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1783 if (IS_ERR(resp_skb)) {
1784 err = PTR_ERR(resp_skb);
1785 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001786 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001787 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001789 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001790 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001791
1792 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001793 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001794
Herbert Xue7443892005-06-18 22:44:18 -07001795 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001796 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001797 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001798 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001799 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001802out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001803 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001804 if (delete && err == 0)
1805 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return err;
1807}
1808
Christoph Hellwig22e70052007-01-02 15:22:30 -08001809static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001810 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001812 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001813 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001814 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001815 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Tetsuo Handa2e710292014-04-22 21:48:30 +09001817 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001818 if (err) {
1819 if (err == -ESRCH) /* empty table */
1820 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001821 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001822 }
Herbert Xubf088672005-06-18 22:44:00 -07001823 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001824 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001825 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001826 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001827 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001828 km_state_notify(NULL, &c);
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return 0;
1831}
1832
Steffen Klassertd8647b72011-03-08 00:10:27 +00001833static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001834{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001835 size_t replay_size = x->replay_esn ?
1836 xfrm_replay_state_esn_len(x->replay_esn) :
1837 sizeof(struct xfrm_replay_state);
1838
Thomas Graf7deb2262007-08-22 13:57:39 -07001839 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001840 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001841 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001842 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001843 + nla_total_size(4) /* XFRM_AE_RTHR */
1844 + nla_total_size(4); /* XFRM_AE_ETHR */
1845}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001846
David S. Miller214e0052011-02-24 00:02:38 -05001847static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001848{
1849 struct xfrm_aevent_id *id;
1850 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001851 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001852
Eric W. Biederman15e47302012-09-07 20:12:54 +00001853 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001854 if (nlh == NULL)
1855 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001856
Thomas Graf7b67c852007-08-22 13:53:52 -07001857 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001858 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001859 id->sa_id.spi = x->id.spi;
1860 id->sa_id.family = x->props.family;
1861 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001862 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001863 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001864 id->flags = c->data.aevent;
1865
David S. Millerd0fde792012-03-29 04:02:26 -04001866 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001867 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1868 xfrm_replay_state_esn_len(x->replay_esn),
1869 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001870 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001871 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1872 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001873 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001874 if (err)
1875 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001876 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1877 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001878 if (err)
1879 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001880
David S. Miller1d1e34d2012-06-27 21:57:03 -07001881 if (id->flags & XFRM_AE_RTHR) {
1882 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1883 if (err)
1884 goto out_cancel;
1885 }
1886 if (id->flags & XFRM_AE_ETHR) {
1887 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1888 x->replay_maxage * 10 / HZ);
1889 if (err)
1890 goto out_cancel;
1891 }
1892 err = xfrm_mark_put(skb, &x->mark);
1893 if (err)
1894 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001895
Johannes Berg053c0952015-01-16 22:09:00 +01001896 nlmsg_end(skb, nlh);
1897 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001898
David S. Miller1d1e34d2012-06-27 21:57:03 -07001899out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001900 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001901 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001902}
1903
Christoph Hellwig22e70052007-01-02 15:22:30 -08001904static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001905 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001906{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001907 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001908 struct xfrm_state *x;
1909 struct sk_buff *r_skb;
1910 int err;
1911 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001912 u32 mark;
1913 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001914 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001915 struct xfrm_usersa_id *id = &p->sa_id;
1916
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001917 mark = xfrm_mark_get(attrs, &m);
1918
1919 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001920 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001921 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001922
1923 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1924 if (r_skb == NULL) {
1925 xfrm_state_put(x);
1926 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001927 }
1928
1929 /*
1930 * XXX: is this lock really needed - none of the other
1931 * gets lock (the concern is things getting updated
1932 * while we are still reading) - jhs
1933 */
1934 spin_lock_bh(&x->lock);
1935 c.data.aevent = p->flags;
1936 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001937 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001938
1939 if (build_aevent(r_skb, x, &c) < 0)
1940 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001941 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001942 spin_unlock_bh(&x->lock);
1943 xfrm_state_put(x);
1944 return err;
1945}
1946
Christoph Hellwig22e70052007-01-02 15:22:30 -08001947static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001948 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001949{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001950 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001951 struct xfrm_state *x;
1952 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001953 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001954 u32 mark = 0;
1955 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001956 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001957 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001958 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001959 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02001960 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1961 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001962
Michael Rossberg4e077232015-09-29 11:25:08 +02001963 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001964 return err;
1965
1966 /* pedantic mode - thou shalt sayeth replaceth */
1967 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1968 return err;
1969
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001970 mark = xfrm_mark_get(attrs, &m);
1971
1972 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 -08001973 if (x == NULL)
1974 return -ESRCH;
1975
1976 if (x->km.state != XFRM_STATE_VALID)
1977 goto out;
1978
Steffen Klassert4479ff72013-09-09 09:39:01 +02001979 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001980 if (err)
1981 goto out;
1982
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001983 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001984 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001985 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001986
1987 c.event = nlh->nlmsg_type;
1988 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001989 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001990 c.data.aevent = XFRM_AE_CU;
1991 km_state_notify(x, &c);
1992 err = 0;
1993out:
1994 xfrm_state_put(x);
1995 return err;
1996}
1997
Christoph Hellwig22e70052007-01-02 15:22:30 -08001998static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001999 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002001 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002002 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002003 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002004 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002005
Thomas Graf35a7aa02007-08-22 14:00:40 -07002006 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002007 if (err)
2008 return err;
2009
Tetsuo Handa2e710292014-04-22 21:48:30 +09002010 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002011 if (err) {
2012 if (err == -ESRCH) /* empty table */
2013 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002014 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002015 }
2016
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002017 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002018 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002019 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002020 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002021 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002022 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return 0;
2024}
2025
Christoph Hellwig22e70052007-01-02 15:22:30 -08002026static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002027 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002028{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002029 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002030 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002031 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002032 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002033 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002034 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002035 struct xfrm_mark m;
2036 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002037
Thomas Graf35a7aa02007-08-22 14:00:40 -07002038 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002039 if (err)
2040 return err;
2041
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002042 err = verify_policy_dir(p->dir);
2043 if (err)
2044 return err;
2045
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002046 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002047 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002048 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002049 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002050 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002051
Thomas Graf35a7aa02007-08-22 14:00:40 -07002052 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002053 if (err)
2054 return err;
2055
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002056 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002057 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002058 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002059
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002060 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002061 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002062 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002063 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002064 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002065 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002066 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002067 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002068 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002069 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002070
Timo Teräsea2dea92010-03-31 00:17:05 +00002071 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002072 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002073
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002074 err = 0;
2075 if (up->hard) {
2076 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002077 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002078 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002079 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002080
2081out:
2082 xfrm_pol_put(xp);
2083 return err;
2084}
2085
Christoph Hellwig22e70052007-01-02 15:22:30 -08002086static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002087 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002088{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002089 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002090 struct xfrm_state *x;
2091 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002092 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002093 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002094 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002095 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002096
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002097 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 -08002098
David S. Miller3a765aa2007-02-26 14:52:21 -08002099 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002100 if (x == NULL)
2101 return err;
2102
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002103 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002104 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002105 if (x->km.state != XFRM_STATE_VALID)
2106 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002107 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002108
Joy Latten161a09e2006-11-27 13:11:54 -06002109 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002110 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002111 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002112 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002113 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002114out:
2115 spin_unlock_bh(&x->lock);
2116 xfrm_state_put(x);
2117 return err;
2118}
2119
Christoph Hellwig22e70052007-01-02 15:22:30 -08002120static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002121 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002122{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002123 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002124 struct xfrm_policy *xp;
2125 struct xfrm_user_tmpl *ut;
2126 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002127 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002128 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002129
Thomas Graf7b67c852007-08-22 13:53:52 -07002130 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002131 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002132 int err = -ENOMEM;
2133
2134 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002135 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002136
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002137 xfrm_mark_get(attrs, &mark);
2138
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002139 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002140 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002141 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002142
2143 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002144 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002145 if (!xp)
2146 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002147
2148 memcpy(&x->id, &ua->id, sizeof(ua->id));
2149 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2150 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002151 xp->mark.m = x->mark.m = mark.m;
2152 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002153 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002154 /* extract the templates and for each call km_key */
2155 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2156 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2157 memcpy(&x->id, &t->id, sizeof(x->id));
2158 x->props.mode = t->mode;
2159 x->props.reqid = t->reqid;
2160 x->props.family = ut->family;
2161 t->aalgos = ua->aalgos;
2162 t->ealgos = ua->ealgos;
2163 t->calgos = ua->calgos;
2164 err = km_query(x, t, xp);
2165
2166 }
2167
2168 kfree(x);
2169 kfree(xp);
2170
2171 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002172
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002173free_state:
2174 kfree(x);
2175nomem:
2176 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002177}
2178
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002179#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002180static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002181 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002182 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002183{
Thomas Graf5424f322007-08-22 14:01:33 -07002184 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002185 struct xfrm_user_migrate *um;
2186 int i, num_migrate;
2187
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002188 if (k != NULL) {
2189 struct xfrm_user_kmaddress *uk;
2190
2191 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2192 memcpy(&k->local, &uk->local, sizeof(k->local));
2193 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2194 k->family = uk->family;
2195 k->reserved = uk->reserved;
2196 }
2197
Thomas Graf5424f322007-08-22 14:01:33 -07002198 um = nla_data(rt);
2199 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002200
2201 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2202 return -EINVAL;
2203
2204 for (i = 0; i < num_migrate; i++, um++, ma++) {
2205 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2206 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2207 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2208 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2209
2210 ma->proto = um->proto;
2211 ma->mode = um->mode;
2212 ma->reqid = um->reqid;
2213
2214 ma->old_family = um->old_family;
2215 ma->new_family = um->new_family;
2216 }
2217
2218 *num = i;
2219 return 0;
2220}
2221
2222static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002223 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002224{
Thomas Graf7b67c852007-08-22 13:53:52 -07002225 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002226 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002227 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002228 u8 type;
2229 int err;
2230 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002231 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232
Suren Baghdasaryan34803e72017-08-15 15:12:24 -07002233 err = verify_policy_dir(pi->dir);
2234 if (err)
2235 return err;
2236
Thomas Graf35a7aa02007-08-22 14:00:40 -07002237 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002238 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002239
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002240 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2241
Thomas Graf5424f322007-08-22 14:01:33 -07002242 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002243 if (err)
2244 return err;
2245
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002246 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002247 if (err)
2248 return err;
2249
2250 if (!n)
2251 return 0;
2252
Fan Du8d549c42013-11-07 17:47:49 +08002253 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002254
2255 return 0;
2256}
2257#else
2258static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002259 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002260{
2261 return -ENOPROTOOPT;
2262}
2263#endif
2264
2265#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002266static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002267{
2268 struct xfrm_user_migrate um;
2269
2270 memset(&um, 0, sizeof(um));
2271 um.proto = m->proto;
2272 um.mode = m->mode;
2273 um.reqid = m->reqid;
2274 um.old_family = m->old_family;
2275 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2276 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2277 um.new_family = m->new_family;
2278 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2279 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2280
Thomas Grafc0144be2007-08-22 13:55:43 -07002281 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002282}
2283
David S. Miller183cad12011-02-24 00:28:01 -05002284static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002285{
2286 struct xfrm_user_kmaddress uk;
2287
2288 memset(&uk, 0, sizeof(uk));
2289 uk.family = k->family;
2290 uk.reserved = k->reserved;
2291 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002292 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002293
2294 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2295}
2296
2297static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002298{
2299 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002300 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2301 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2302 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002303}
2304
David S. Miller183cad12011-02-24 00:28:01 -05002305static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2306 int num_migrate, const struct xfrm_kmaddress *k,
2307 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002308{
David S. Miller183cad12011-02-24 00:28:01 -05002309 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002310 struct xfrm_userpolicy_id *pol_id;
2311 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002312 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002313
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002314 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2315 if (nlh == NULL)
2316 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002317
Thomas Graf7b67c852007-08-22 13:53:52 -07002318 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002319 /* copy data from selector, dir, and type to the pol_id */
2320 memset(pol_id, 0, sizeof(*pol_id));
2321 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2322 pol_id->dir = dir;
2323
David S. Miller1d1e34d2012-06-27 21:57:03 -07002324 if (k != NULL) {
2325 err = copy_to_user_kmaddress(k, skb);
2326 if (err)
2327 goto out_cancel;
2328 }
2329 err = copy_to_user_policy_type(type, skb);
2330 if (err)
2331 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002332 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002333 err = copy_to_user_migrate(mp, skb);
2334 if (err)
2335 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002336 }
2337
Johannes Berg053c0952015-01-16 22:09:00 +01002338 nlmsg_end(skb, nlh);
2339 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002340
2341out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002342 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002343 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002344}
2345
David S. Miller183cad12011-02-24 00:28:01 -05002346static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2347 const struct xfrm_migrate *m, int num_migrate,
2348 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002349{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002350 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002351 struct sk_buff *skb;
Suren Baghdasaryan34803e72017-08-15 15:12:24 -07002352 int err;
2353
2354 err = verify_policy_dir(dir);
2355 if (err)
2356 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002357
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002358 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002359 if (skb == NULL)
2360 return -ENOMEM;
2361
2362 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002363 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002364 BUG();
2365
Michal Kubecek21ee5432014-06-03 10:26:06 +02002366 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002367}
2368#else
David S. Miller183cad12011-02-24 00:28:01 -05002369static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2370 const struct xfrm_migrate *m, int num_migrate,
2371 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002372{
2373 return -ENOPROTOOPT;
2374}
2375#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002376
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002377#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002378
2379static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002380 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002381 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2382 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2383 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2384 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2385 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2386 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002387 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002388 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002389 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002390 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002391 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002392 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002393 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002394 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2395 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002396 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002397 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002398 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002399 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002400 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401};
2402
Thomas Graf492b5582005-05-03 14:26:40 -07002403#undef XMSGSIZE
2404
Thomas Grafcf5cb792007-08-22 13:59:04 -07002405static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002406 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2407 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2408 [XFRMA_LASTUSED] = { .type = NLA_U64},
2409 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002410 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002411 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2412 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2413 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2414 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2415 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2416 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2417 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2418 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2419 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2420 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2421 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2422 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2423 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2424 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002425 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002426 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002427 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002428 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002429 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002430 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002431 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Lorenzo Colittibf25c792017-08-11 02:11:33 +09002432 [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002433};
2434
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002435static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2436 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2437 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2438};
2439
Mathias Krause05600a72013-02-24 14:10:27 +01002440static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002441 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Herbert Xu543aabb2017-10-19 20:51:10 +08002442 int (*start)(struct netlink_callback *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002444 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002445 const struct nla_policy *nla_pol;
2446 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002447} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2448 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2449 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2450 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002451 .dump = xfrm_dump_sa,
2452 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002453 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2454 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2455 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Herbert Xu543aabb2017-10-19 20:51:10 +08002456 .start = xfrm_dump_policy_start,
Timo Teras4c563f72008-02-28 21:31:08 -08002457 .dump = xfrm_dump_policy,
2458 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002459 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002460 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002461 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002462 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2463 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002464 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002465 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2466 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002467 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2468 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002469 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002470 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002471 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2472 .nla_pol = xfrma_spd_policy,
2473 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002474 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475};
2476
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002477static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002479 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002480 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002481 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002482 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Fan Du74005992015-01-27 17:00:29 +08002484#ifdef CONFIG_COMPAT
Andy Lutomirski2bf8c472016-03-22 14:25:10 -07002485 if (in_compat_syscall())
Yi Zhao83e2d052016-11-29 18:09:01 +08002486 return -EOPNOTSUPP;
Fan Du74005992015-01-27 17:00:29 +08002487#endif
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 type -= XFRM_MSG_BASE;
2494 link = &xfrm_dispatch[type];
2495
2496 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002497 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002498 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Thomas Graf492b5582005-05-03 14:26:40 -07002500 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2501 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002502 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002504 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002506 {
2507 struct netlink_dump_control c = {
Herbert Xu543aabb2017-10-19 20:51:10 +08002508 .start = link->start,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002509 .dump = link->dump,
2510 .done = link->done,
2511 };
2512 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
2515
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002516 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2517 link->nla_max ? : XFRMA_MAX,
2518 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002519 if (err < 0)
2520 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Thomas Graf5424f322007-08-22 14:01:33 -07002525 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002528static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Fan Du283bc9f2013-11-07 17:47:50 +08002530 struct net *net = sock_net(skb->sk);
2531
2532 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002533 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002534 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Thomas Graf7deb2262007-08-22 13:57:39 -07002537static inline size_t xfrm_expire_msgsize(void)
2538{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002539 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2540 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002541}
2542
David S. Miller214e0052011-02-24 00:02:38 -05002543static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
2545 struct xfrm_user_expire *ue;
2546 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002547 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Eric W. Biederman15e47302012-09-07 20:12:54 +00002549 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002550 if (nlh == NULL)
2551 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Thomas Graf7b67c852007-08-22 13:53:52 -07002553 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002555 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
David S. Miller1d1e34d2012-06-27 21:57:03 -07002557 err = xfrm_mark_put(skb, &x->mark);
2558 if (err)
2559 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002560
Johannes Berg053c0952015-01-16 22:09:00 +01002561 nlmsg_end(skb, nlh);
2562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
David S. Miller214e0052011-02-24 00:02:38 -05002565static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002567 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 struct sk_buff *skb;
2569
Thomas Graf7deb2262007-08-22 13:57:39 -07002570 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (skb == NULL)
2572 return -ENOMEM;
2573
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002574 if (build_expire(skb, x, c) < 0) {
2575 kfree_skb(skb);
2576 return -EMSGSIZE;
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Michal Kubecek21ee5432014-06-03 10:26:06 +02002579 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
David S. Miller214e0052011-02-24 00:02:38 -05002582static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002583{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002584 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002585 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002586
Steffen Klassertd8647b72011-03-08 00:10:27 +00002587 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002588 if (skb == NULL)
2589 return -ENOMEM;
2590
2591 if (build_aevent(skb, x, c) < 0)
2592 BUG();
2593
Michal Kubecek21ee5432014-06-03 10:26:06 +02002594 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002595}
2596
David S. Miller214e0052011-02-24 00:02:38 -05002597static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002598{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002599 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002600 struct xfrm_usersa_flush *p;
2601 struct nlmsghdr *nlh;
2602 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002603 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002604
Thomas Graf7deb2262007-08-22 13:57:39 -07002605 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002606 if (skb == NULL)
2607 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002608
Eric W. Biederman15e47302012-09-07 20:12:54 +00002609 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002610 if (nlh == NULL) {
2611 kfree_skb(skb);
2612 return -EMSGSIZE;
2613 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002614
Thomas Graf7b67c852007-08-22 13:53:52 -07002615 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002616 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002617
Thomas Graf98250692007-08-22 12:47:26 -07002618 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002619
Michal Kubecek21ee5432014-06-03 10:26:06 +02002620 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002621}
2622
Thomas Graf7deb2262007-08-22 13:57:39 -07002623static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002624{
Thomas Graf7deb2262007-08-22 13:57:39 -07002625 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002626 if (x->aead)
2627 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002628 if (x->aalg) {
2629 l += nla_total_size(sizeof(struct xfrm_algo) +
2630 (x->aalg->alg_key_len + 7) / 8);
2631 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2632 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002633 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002634 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002635 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002636 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002637 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002638 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002639 if (x->tfcpad)
2640 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002641 if (x->replay_esn)
2642 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002643 else
2644 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002645 if (x->security)
2646 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2647 x->security->ctx_len);
2648 if (x->coaddr)
2649 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002650 if (x->props.extra_flags)
2651 l += nla_total_size(sizeof(x->props.extra_flags));
Lorenzo Colittibf25c792017-08-11 02:11:33 +09002652 if (x->props.output_mark)
2653 l += nla_total_size(sizeof(x->props.output_mark));
Herbert Xu68325d32007-10-09 13:30:57 -07002654
Herbert Xud26f3982007-11-13 21:47:08 -08002655 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002656 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002657
2658 return l;
2659}
2660
David S. Miller214e0052011-02-24 00:02:38 -05002661static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002662{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002663 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002664 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002665 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002666 struct nlmsghdr *nlh;
2667 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002668 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002669 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002670
2671 headlen = sizeof(*p);
2672 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002673 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002674 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002675 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002676 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002677 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002678
Thomas Graf7deb2262007-08-22 13:57:39 -07002679 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002680 if (skb == NULL)
2681 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002682
Eric W. Biederman15e47302012-09-07 20:12:54 +00002683 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002684 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002685 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002686 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002687
Thomas Graf7b67c852007-08-22 13:53:52 -07002688 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002689 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002690 struct nlattr *attr;
2691
Thomas Graf7b67c852007-08-22 13:53:52 -07002692 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002693 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2694 id->spi = x->id.spi;
2695 id->family = x->props.family;
2696 id->proto = x->id.proto;
2697
Thomas Grafc0144be2007-08-22 13:55:43 -07002698 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002699 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002700 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002701 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002702
2703 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002704 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002705 err = copy_to_user_state_extra(x, p, skb);
2706 if (err)
2707 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002708
Thomas Graf98250692007-08-22 12:47:26 -07002709 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002710
Michal Kubecek21ee5432014-06-03 10:26:06 +02002711 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002712
David S. Miller1d1e34d2012-06-27 21:57:03 -07002713out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002714 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002715 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002716}
2717
David S. Miller214e0052011-02-24 00:02:38 -05002718static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002719{
2720
2721 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002722 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002723 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002724 case XFRM_MSG_NEWAE:
2725 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002726 case XFRM_MSG_DELSA:
2727 case XFRM_MSG_UPDSA:
2728 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002729 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002730 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002731 return xfrm_notify_sa_flush(c);
2732 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002733 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2734 c->event);
2735 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002736 }
2737
2738 return 0;
2739
2740}
2741
Thomas Graf7deb2262007-08-22 13:57:39 -07002742static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2743 struct xfrm_policy *xp)
2744{
2745 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2746 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002747 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002748 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2749 + userpolicy_type_attrsize();
2750}
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002753 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002755 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct xfrm_user_acquire *ua;
2757 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002758 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002760 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2761 if (nlh == NULL)
2762 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Thomas Graf7b67c852007-08-22 13:53:52 -07002764 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 memcpy(&ua->id, &x->id, sizeof(ua->id));
2766 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2767 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002768 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 ua->aalgos = xt->aalgos;
2770 ua->ealgos = xt->ealgos;
2771 ua->calgos = xt->calgos;
2772 ua->seq = x->km.seq = seq;
2773
David S. Miller1d1e34d2012-06-27 21:57:03 -07002774 err = copy_to_user_tmpl(xp, skb);
2775 if (!err)
2776 err = copy_to_user_state_sec_ctx(x, skb);
2777 if (!err)
2778 err = copy_to_user_policy_type(xp->type, skb);
2779 if (!err)
2780 err = xfrm_mark_put(skb, &xp->mark);
2781 if (err) {
2782 nlmsg_cancel(skb, nlh);
2783 return err;
2784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Johannes Berg053c0952015-01-16 22:09:00 +01002786 nlmsg_end(skb, nlh);
2787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
2790static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002791 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002793 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Thomas Graf7deb2262007-08-22 13:57:39 -07002796 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (skb == NULL)
2798 return -ENOMEM;
2799
Fan Du65e07362012-08-15 10:13:47 +08002800 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 BUG();
2802
Michal Kubecek21ee5432014-06-03 10:26:06 +02002803 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
2806/* User gives us xfrm_user_policy_info followed by an array of 0
2807 * or more templates.
2808 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002809static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 u8 *data, int len, int *dir)
2811{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002812 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2814 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2815 struct xfrm_policy *xp;
2816 int nr;
2817
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002818 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 case AF_INET:
2820 if (opt != IP_XFRM_POLICY) {
2821 *dir = -EOPNOTSUPP;
2822 return NULL;
2823 }
2824 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002825#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 case AF_INET6:
2827 if (opt != IPV6_XFRM_POLICY) {
2828 *dir = -EOPNOTSUPP;
2829 return NULL;
2830 }
2831 break;
2832#endif
2833 default:
2834 *dir = -EINVAL;
2835 return NULL;
2836 }
2837
2838 *dir = -EINVAL;
2839
2840 if (len < sizeof(*p) ||
2841 verify_newpolicy_info(p))
2842 return NULL;
2843
2844 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002845 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return NULL;
2847
Herbert Xua4f1bac2005-07-26 15:43:17 -07002848 if (p->dir > XFRM_POLICY_OUT)
2849 return NULL;
2850
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002851 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (xp == NULL) {
2853 *dir = -ENOBUFS;
2854 return NULL;
2855 }
2856
2857 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002858 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 copy_templates(xp, ut, nr);
2860
2861 *dir = p->dir;
2862
2863 return xp;
2864}
2865
Thomas Graf7deb2262007-08-22 13:57:39 -07002866static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2867{
2868 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2869 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2870 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002871 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002872 + userpolicy_type_attrsize();
2873}
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002876 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002879 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002880 struct nlmsghdr *nlh;
2881 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Eric W. Biederman15e47302012-09-07 20:12:54 +00002883 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002884 if (nlh == NULL)
2885 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Thomas Graf7b67c852007-08-22 13:53:52 -07002887 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002889 err = copy_to_user_tmpl(xp, skb);
2890 if (!err)
2891 err = copy_to_user_sec_ctx(xp, skb);
2892 if (!err)
2893 err = copy_to_user_policy_type(xp->type, skb);
2894 if (!err)
2895 err = xfrm_mark_put(skb, &xp->mark);
2896 if (err) {
2897 nlmsg_cancel(skb, nlh);
2898 return err;
2899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 upe->hard = !!hard;
2901
Johannes Berg053c0952015-01-16 22:09:00 +01002902 nlmsg_end(skb, nlh);
2903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
David S. Miller214e0052011-02-24 00:02:38 -05002906static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002908 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Thomas Graf7deb2262007-08-22 13:57:39 -07002911 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (skb == NULL)
2913 return -ENOMEM;
2914
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002915 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 BUG();
2917
Michal Kubecek21ee5432014-06-03 10:26:06 +02002918 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
2920
David S. Miller214e0052011-02-24 00:02:38 -05002921static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002922{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002923 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002924 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002925 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002926 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002927 struct nlmsghdr *nlh;
2928 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002929 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002930
2931 headlen = sizeof(*p);
2932 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002933 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002934 headlen = sizeof(*id);
2935 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002936 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002937 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002938 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002939
Thomas Graf7deb2262007-08-22 13:57:39 -07002940 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002941 if (skb == NULL)
2942 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002943
Eric W. Biederman15e47302012-09-07 20:12:54 +00002944 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002945 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002946 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002947 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002948
Thomas Graf7b67c852007-08-22 13:53:52 -07002949 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002950 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002951 struct nlattr *attr;
2952
Thomas Graf7b67c852007-08-22 13:53:52 -07002953 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002954 memset(id, 0, sizeof(*id));
2955 id->dir = dir;
2956 if (c->data.byid)
2957 id->index = xp->index;
2958 else
2959 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2960
Thomas Grafc0144be2007-08-22 13:55:43 -07002961 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002962 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002963 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002964 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002965
2966 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002967 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002968
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002969 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002970 err = copy_to_user_tmpl(xp, skb);
2971 if (!err)
2972 err = copy_to_user_policy_type(xp->type, skb);
2973 if (!err)
2974 err = xfrm_mark_put(skb, &xp->mark);
2975 if (err)
2976 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002977
Thomas Graf98250692007-08-22 12:47:26 -07002978 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002979
Michal Kubecek21ee5432014-06-03 10:26:06 +02002980 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002981
David S. Miller1d1e34d2012-06-27 21:57:03 -07002982out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002983 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002984 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002985}
2986
David S. Miller214e0052011-02-24 00:02:38 -05002987static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002988{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002989 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002990 struct nlmsghdr *nlh;
2991 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002992 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002993
Thomas Graf7deb2262007-08-22 13:57:39 -07002994 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002995 if (skb == NULL)
2996 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002997
Eric W. Biederman15e47302012-09-07 20:12:54 +00002998 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002999 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003000 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003001 goto out_free_skb;
3002 err = copy_to_user_policy_type(c->data.type, skb);
3003 if (err)
3004 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003005
Thomas Graf98250692007-08-22 12:47:26 -07003006 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003007
Michal Kubecek21ee5432014-06-03 10:26:06 +02003008 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003009
David S. Miller1d1e34d2012-06-27 21:57:03 -07003010out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003011 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003012 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003013}
3014
David S. Miller214e0052011-02-24 00:02:38 -05003015static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003016{
Suren Baghdasaryan34803e72017-08-15 15:12:24 -07003017 int err;
3018
3019 err = verify_policy_dir(dir);
3020 if (err)
3021 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003022
3023 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003024 case XFRM_MSG_NEWPOLICY:
3025 case XFRM_MSG_UPDPOLICY:
3026 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003027 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003028 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003029 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003030 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003031 return xfrm_exp_policy_notify(xp, dir, c);
3032 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003033 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3034 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003035 }
3036
3037 return 0;
3038
3039}
3040
Thomas Graf7deb2262007-08-22 13:57:39 -07003041static inline size_t xfrm_report_msgsize(void)
3042{
3043 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3044}
3045
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003046static int build_report(struct sk_buff *skb, u8 proto,
3047 struct xfrm_selector *sel, xfrm_address_t *addr)
3048{
3049 struct xfrm_user_report *ur;
3050 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003051
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003052 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3053 if (nlh == NULL)
3054 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003055
Thomas Graf7b67c852007-08-22 13:53:52 -07003056 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003057 ur->proto = proto;
3058 memcpy(&ur->sel, sel, sizeof(ur->sel));
3059
David S. Miller1d1e34d2012-06-27 21:57:03 -07003060 if (addr) {
3061 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3062 if (err) {
3063 nlmsg_cancel(skb, nlh);
3064 return err;
3065 }
3066 }
Johannes Berg053c0952015-01-16 22:09:00 +01003067 nlmsg_end(skb, nlh);
3068 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003069}
3070
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003071static int xfrm_send_report(struct net *net, u8 proto,
3072 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003073{
3074 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003075
Thomas Graf7deb2262007-08-22 13:57:39 -07003076 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003077 if (skb == NULL)
3078 return -ENOMEM;
3079
3080 if (build_report(skb, proto, sel, addr) < 0)
3081 BUG();
3082
Michal Kubecek21ee5432014-06-03 10:26:06 +02003083 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003084}
3085
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003086static inline size_t xfrm_mapping_msgsize(void)
3087{
3088 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3089}
3090
3091static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3092 xfrm_address_t *new_saddr, __be16 new_sport)
3093{
3094 struct xfrm_user_mapping *um;
3095 struct nlmsghdr *nlh;
3096
3097 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3098 if (nlh == NULL)
3099 return -EMSGSIZE;
3100
3101 um = nlmsg_data(nlh);
3102
3103 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3104 um->id.spi = x->id.spi;
3105 um->id.family = x->props.family;
3106 um->id.proto = x->id.proto;
3107 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3108 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3109 um->new_sport = new_sport;
3110 um->old_sport = x->encap->encap_sport;
3111 um->reqid = x->props.reqid;
3112
Johannes Berg053c0952015-01-16 22:09:00 +01003113 nlmsg_end(skb, nlh);
3114 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003115}
3116
3117static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3118 __be16 sport)
3119{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003120 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003121 struct sk_buff *skb;
3122
3123 if (x->id.proto != IPPROTO_ESP)
3124 return -EINVAL;
3125
3126 if (!x->encap)
3127 return -EINVAL;
3128
3129 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3130 if (skb == NULL)
3131 return -ENOMEM;
3132
3133 if (build_mapping(skb, x, ipaddr, sport) < 0)
3134 BUG();
3135
Michal Kubecek21ee5432014-06-03 10:26:06 +02003136 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003137}
3138
Horia Geanta0f245582014-02-12 16:20:06 +02003139static bool xfrm_is_alive(const struct km_event *c)
3140{
3141 return (bool)xfrm_acquire_is_on(c->net);
3142}
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144static struct xfrm_mgr netlink_mgr = {
3145 .id = "netlink",
3146 .notify = xfrm_send_state_notify,
3147 .acquire = xfrm_send_acquire,
3148 .compile_policy = xfrm_compile_policy,
3149 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003150 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003151 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003152 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003153 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154};
3155
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003156static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Patrick McHardybe336902006-03-20 22:40:54 -08003158 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003159 struct netlink_kernel_cfg cfg = {
3160 .groups = XFRMNLGRP_MAX,
3161 .input = xfrm_netlink_rcv,
3162 };
Patrick McHardybe336902006-03-20 22:40:54 -08003163
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003164 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003165 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003167 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003168 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return 0;
3170}
3171
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003172static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003173{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003174 struct net *net;
3175 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003176 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003177 synchronize_net();
3178 list_for_each_entry(net, net_exit_list, exit_list)
3179 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003180}
3181
3182static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003183 .init = xfrm_user_net_init,
3184 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003185};
3186
3187static int __init xfrm_user_init(void)
3188{
3189 int rv;
3190
3191 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3192
3193 rv = register_pernet_subsys(&xfrm_user_net_ops);
3194 if (rv < 0)
3195 return rv;
3196 rv = xfrm_register_km(&netlink_mgr);
3197 if (rv < 0)
3198 unregister_pernet_subsys(&xfrm_user_net_ops);
3199 return rv;
3200}
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202static void __exit xfrm_user_exit(void)
3203{
3204 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003205 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
3207
3208module_init(xfrm_user_init);
3209module_exit(xfrm_user_exit);
3210MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003211MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003212