blob: df4b7fc721f664a1f2fdbf2daae5b809d1e54528 [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
124 if (!rt)
Florian Westphal0355a9f2018-02-12 14:42:01 +0100125 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
126
127 rs = nla_data(rt);
128
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
131
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000135
Fan Du01714102014-01-18 09:54:28 +0800136 /* As only ESP and AH support ESN feature. */
137 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000138 return -EINVAL;
139
Steffen Klassertd8647b72011-03-08 00:10:27 +0000140 if (p->replay_window != 0)
141 return -EINVAL;
142
143 return 0;
144}
Trent Jaegerdf718372005-12-13 23:12:27 -0800145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700147 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 int err;
150
151 err = -EINVAL;
152 switch (p->family) {
153 case AF_INET:
Anirudh Gupta92a63c22019-05-21 20:59:47 +0530154 break;
155
156 case AF_INET6:
157#if IS_ENABLED(CONFIG_IPV6)
158 break;
159#else
160 err = -EAFNOSUPPORT;
161 goto out;
162#endif
163
164 default:
165 goto out;
166 }
167
168 switch (p->sel.family) {
169 case AF_INET:
Steffen Klassert89cb86e2018-08-01 13:45:11 +0200170 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
171 goto out;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174
175 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000176#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert89cb86e2018-08-01 13:45:11 +0200177 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
178 goto out;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181#else
182 err = -EAFNOSUPPORT;
183 goto out;
184#endif
185
186 default:
187 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 err = -EINVAL;
191 switch (p->id.proto) {
192 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000193 if ((!attrs[XFRMA_ALG_AUTH] &&
194 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800195 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700196 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000197 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200198 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto out;
200 break;
201
202 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800203 if (attrs[XFRMA_ALG_COMP])
204 goto out;
205 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000206 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800207 !attrs[XFRMA_ALG_CRYPT] &&
208 !attrs[XFRMA_ALG_AEAD])
209 goto out;
210 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000211 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800212 attrs[XFRMA_ALG_CRYPT]) &&
213 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000215 if (attrs[XFRMA_TFCPAD] &&
216 p->mode != XFRM_MODE_TUNNEL)
217 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219
220 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700221 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800222 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700223 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000224 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000225 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200226 attrs[XFRMA_TFCPAD] ||
227 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto out;
229 break;
230
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000231#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700232 case IPPROTO_DSTOPTS:
233 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700234 if (attrs[XFRMA_ALG_COMP] ||
235 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000236 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800237 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700238 attrs[XFRMA_ALG_CRYPT] ||
239 attrs[XFRMA_ENCAP] ||
240 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000241 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700243 goto out;
244 break;
245#endif
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 default:
248 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Herbert Xu1a6509d2008-01-28 19:37:29 -0800251 if ((err = verify_aead(attrs)))
252 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000253 if ((err = verify_auth_trunc(attrs)))
254 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700255 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700257 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700259 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700261 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800262 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000263 if ((err = verify_replay(p, attrs)))
264 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 err = -EINVAL;
267 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700268 case XFRM_MODE_TRANSPORT:
269 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700270 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700271 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
273
274 default:
275 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 err = 0;
279
280out:
281 return err;
282}
283
284static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800285 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700286 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct xfrm_algo *p, *ualg;
289 struct xfrm_algo_desc *algo;
290
291 if (!rta)
292 return 0;
293
Thomas Graf5424f322007-08-22 14:01:33 -0700294 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 algo = get_byname(ualg->alg_name, 1);
297 if (!algo)
298 return -ENOSYS;
299 *props = algo->desc.sadb_alg_id;
300
Eric Dumazet0f99be02008-01-08 23:39:06 -0800301 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!p)
303 return -ENOMEM;
304
Herbert Xu04ff1262006-08-13 08:50:00 +1000305 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *algpp = p;
307 return 0;
308}
309
Herbert Xu69b01372015-05-27 16:03:45 +0800310static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
311{
312 struct xfrm_algo *p, *ualg;
313 struct xfrm_algo_desc *algo;
314
315 if (!rta)
316 return 0;
317
318 ualg = nla_data(rta);
319
320 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
321 if (!algo)
322 return -ENOSYS;
323 x->props.ealgo = algo->desc.sadb_alg_id;
324
325 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
326 if (!p)
327 return -ENOMEM;
328
329 strcpy(p->alg_name, algo->name);
330 x->ealg = p;
331 x->geniv = algo->uinfo.encr.geniv;
332 return 0;
333}
334
Martin Willi4447bb32009-11-25 00:29:52 +0000335static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
336 struct nlattr *rta)
337{
338 struct xfrm_algo *ualg;
339 struct xfrm_algo_auth *p;
340 struct xfrm_algo_desc *algo;
341
342 if (!rta)
343 return 0;
344
345 ualg = nla_data(rta);
346
347 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
348 if (!algo)
349 return -ENOSYS;
350 *props = algo->desc.sadb_alg_id;
351
352 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
353 if (!p)
354 return -ENOMEM;
355
356 strcpy(p->alg_name, algo->name);
357 p->alg_key_len = ualg->alg_key_len;
358 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
359 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
360
361 *algpp = p;
362 return 0;
363}
364
365static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
366 struct nlattr *rta)
367{
368 struct xfrm_algo_auth *p, *ualg;
369 struct xfrm_algo_desc *algo;
370
371 if (!rta)
372 return 0;
373
374 ualg = nla_data(rta);
375
376 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
377 if (!algo)
378 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800379 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000380 return -EINVAL;
381 *props = algo->desc.sadb_alg_id;
382
383 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
384 if (!p)
385 return -ENOMEM;
386
387 strcpy(p->alg_name, algo->name);
388 if (!p->alg_trunc_len)
389 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
390
391 *algpp = p;
392 return 0;
393}
394
Herbert Xu69b01372015-05-27 16:03:45 +0800395static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800396{
397 struct xfrm_algo_aead *p, *ualg;
398 struct xfrm_algo_desc *algo;
399
400 if (!rta)
401 return 0;
402
403 ualg = nla_data(rta);
404
405 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
406 if (!algo)
407 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800408 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800409
410 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
411 if (!p)
412 return -ENOMEM;
413
414 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800415 x->aead = p;
416 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800417 return 0;
418}
419
Steffen Klasserte2b19122011-03-28 19:47:30 +0000420static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
421 struct nlattr *rp)
422{
423 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000424 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000425
426 if (!replay_esn || !rp)
427 return 0;
428
429 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000430 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000431
Andy Whitcroft79191ea2017-03-23 07:45:44 +0000432 /* Check the overall length and the internal bitmap length to avoid
433 * potential overflow. */
434 if (nla_len(rp) < ulen ||
435 xfrm_replay_state_esn_len(replay_esn) != ulen ||
436 replay_esn->bmp_len != up->bmp_len)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000437 return -EINVAL;
438
Andy Whitcroft64a54652017-03-22 07:29:31 +0000439 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
440 return -EINVAL;
441
Steffen Klasserte2b19122011-03-28 19:47:30 +0000442 return 0;
443}
444
Steffen Klassertd8647b72011-03-08 00:10:27 +0000445static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
446 struct xfrm_replay_state_esn **preplay_esn,
447 struct nlattr *rta)
448{
449 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000450 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000451
452 if (!rta)
453 return 0;
454
455 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000456 klen = xfrm_replay_state_esn_len(up);
457 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000458
Mathias Krauseecd79182012-09-20 10:01:49 +0000459 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000460 if (!p)
461 return -ENOMEM;
462
Mathias Krauseecd79182012-09-20 10:01:49 +0000463 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000464 if (!pp) {
465 kfree(p);
466 return -ENOMEM;
467 }
468
Mathias Krauseecd79182012-09-20 10:01:49 +0000469 memcpy(p, up, ulen);
470 memcpy(pp, up, ulen);
471
Steffen Klassertd8647b72011-03-08 00:10:27 +0000472 *replay_esn = p;
473 *preplay_esn = pp;
474
475 return 0;
476}
477
Joy Latten661697f2007-04-13 16:14:35 -0700478static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800479{
Trent Jaegerdf718372005-12-13 23:12:27 -0800480 int len = 0;
481
482 if (xfrm_ctx) {
483 len += sizeof(struct xfrm_user_sec_ctx);
484 len += xfrm_ctx->ctx_len;
485 }
486 return len;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
490{
491 memcpy(&x->id, &p->id, sizeof(x->id));
492 memcpy(&x->sel, &p->sel, sizeof(x->sel));
493 memcpy(&x->lft, &p->lft, sizeof(x->lft));
494 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800495 x->props.replay_window = min_t(unsigned int, p->replay_window,
496 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 x->props.reqid = p->reqid;
498 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700499 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700501
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700502 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700503 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800506/*
507 * someday when pfkey also has support, we could have the code
508 * somehow made shareable and move it to xfrm_state.c - JHS
509 *
510*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000511static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
512 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800513{
Thomas Graf5424f322007-08-22 14:01:33 -0700514 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000515 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700516 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
517 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
518 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800519
Steffen Klassertd8647b72011-03-08 00:10:27 +0000520 if (re) {
521 struct xfrm_replay_state_esn *replay_esn;
522 replay_esn = nla_data(re);
523 memcpy(x->replay_esn, replay_esn,
524 xfrm_replay_state_esn_len(replay_esn));
525 memcpy(x->preplay_esn, replay_esn,
526 xfrm_replay_state_esn_len(replay_esn));
527 }
528
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800529 if (rp) {
530 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700531 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800532 memcpy(&x->replay, replay, sizeof(*replay));
533 memcpy(&x->preplay, replay, sizeof(*replay));
534 }
535
536 if (lt) {
537 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700538 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800539 x->curlft.bytes = ltime->bytes;
540 x->curlft.packets = ltime->packets;
541 x->curlft.add_time = ltime->add_time;
542 x->curlft.use_time = ltime->use_time;
543 }
544
Thomas Grafcf5cb792007-08-22 13:59:04 -0700545 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700546 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800547
Thomas Grafcf5cb792007-08-22 13:59:04 -0700548 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700549 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800550}
551
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800552static struct xfrm_state *xfrm_state_construct(struct net *net,
553 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700554 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int *errp)
556{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800557 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int err = -ENOMEM;
559
560 if (!x)
561 goto error_no_put;
562
563 copy_from_user_state(x, p);
564
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100565 if (attrs[XFRMA_SA_EXTRA_FLAGS])
566 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
567
Herbert Xu69b01372015-05-27 16:03:45 +0800568 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800569 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000570 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
571 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000573 if (!x->props.aalgo) {
574 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
575 attrs[XFRMA_ALG_AUTH])))
576 goto error;
577 }
Herbert Xu69b01372015-05-27 16:03:45 +0800578 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto error;
580 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
581 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700582 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700584
585 if (attrs[XFRMA_ENCAP]) {
586 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
587 sizeof(*x->encap), GFP_KERNEL);
588 if (x->encap == NULL)
589 goto error;
590 }
591
Martin Willi35d28562010-12-08 04:37:49 +0000592 if (attrs[XFRMA_TFCPAD])
593 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
594
Thomas Graffd211502007-09-06 03:28:08 -0700595 if (attrs[XFRMA_COADDR]) {
596 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
597 sizeof(*x->coaddr), GFP_KERNEL);
598 if (x->coaddr == NULL)
599 goto error;
600 }
601
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000602 xfrm_mark_get(attrs, &x->mark);
603
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700604 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (err)
606 goto error;
607
Mathias Krause2f30ea52016-09-08 18:09:57 +0200608 if (attrs[XFRMA_SEC_CTX]) {
609 err = security_xfrm_state_alloc(x,
610 nla_data(attrs[XFRMA_SEC_CTX]));
611 if (err)
612 goto error;
613 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800614
Steffen Klassertd8647b72011-03-08 00:10:27 +0000615 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
616 attrs[XFRMA_REPLAY_ESN_VAL])))
617 goto error;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800620 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800621 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800622 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800623
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000624 if ((err = xfrm_init_replay(x)))
625 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800626
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000627 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000628 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 return x;
631
632error:
633 x->km.state = XFRM_STATE_DEAD;
634 xfrm_state_put(x);
635error_no_put:
636 *errp = err;
637 return NULL;
638}
639
Christoph Hellwig22e70052007-01-02 15:22:30 -0800640static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700641 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800643 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700644 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct xfrm_state *x;
646 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700647 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Thomas Graf35a7aa02007-08-22 14:00:40 -0700649 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (err)
651 return err;
652
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800653 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!x)
655 return err;
656
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700657 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
659 err = xfrm_state_add(x);
660 else
661 err = xfrm_state_update(x);
662
Tetsuo Handa2e710292014-04-22 21:48:30 +0900663 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (err < 0) {
666 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800667 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700671 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000672 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700673 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700674
675 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700676out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700677 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return err;
679}
680
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800681static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
682 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700683 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700684 int *errp)
685{
686 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000687 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700688 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000689 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700690
691 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
692 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000693 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700694 } else {
695 xfrm_address_t *saddr = NULL;
696
Thomas Graf35a7aa02007-08-22 14:00:40 -0700697 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700698 if (!saddr) {
699 err = -EINVAL;
700 goto out;
701 }
702
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800703 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000704 x = xfrm_state_lookup_byaddr(net, mark,
705 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800706 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700707 }
708
709 out:
710 if (!x && errp)
711 *errp = err;
712 return x;
713}
714
Christoph Hellwig22e70052007-01-02 15:22:30 -0800715static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700716 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800718 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700720 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700721 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700722 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800724 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700726 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
David S. Miller6f68dc32006-06-08 23:58:52 -0700728 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700729 goto out;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700732 err = -EPERM;
733 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700736 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600737
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700738 if (err < 0)
739 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700740
741 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000742 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700743 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700744 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700746out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900747 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700748 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700749 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
753{
Mathias Krausef778a632012-09-19 11:33:39 +0000754 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 memcpy(&p->id, &x->id, sizeof(p->id));
756 memcpy(&p->sel, &x->sel, sizeof(p->sel));
757 memcpy(&p->lft, &x->lft, sizeof(p->lft));
758 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400759 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
760 put_unaligned(x->stats.replay, &p->stats.replay);
761 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700762 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 p->mode = x->props.mode;
764 p->replay_window = x->props.replay_window;
765 p->reqid = x->props.reqid;
766 p->family = x->props.family;
767 p->flags = x->props.flags;
768 p->seq = x->km.seq;
769}
770
771struct xfrm_dump_info {
772 struct sk_buff *in_skb;
773 struct sk_buff *out_skb;
774 u32 nlmsg_seq;
775 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776};
777
Thomas Grafc0144be2007-08-22 13:55:43 -0700778static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
779{
Thomas Grafc0144be2007-08-22 13:55:43 -0700780 struct xfrm_user_sec_ctx *uctx;
781 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700782 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700783
784 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
785 if (attr == NULL)
786 return -EMSGSIZE;
787
788 uctx = nla_data(attr);
789 uctx->exttype = XFRMA_SEC_CTX;
790 uctx->len = ctx_size;
791 uctx->ctx_doi = s->ctx_doi;
792 uctx->ctx_alg = s->ctx_alg;
793 uctx->ctx_len = s->ctx_len;
794 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
795
796 return 0;
797}
798
Martin Willi4447bb32009-11-25 00:29:52 +0000799static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
800{
801 struct xfrm_algo *algo;
802 struct nlattr *nla;
803
804 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
805 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
806 if (!nla)
807 return -EMSGSIZE;
808
809 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000810 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000811 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
812 algo->alg_key_len = auth->alg_key_len;
813
814 return 0;
815}
816
Herbert Xu68325d32007-10-09 13:30:57 -0700817/* Don't change this without updating xfrm_sa_len! */
818static int copy_to_user_state_extra(struct xfrm_state *x,
819 struct xfrm_usersa_info *p,
820 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700822 int ret = 0;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 copy_to_user_state(x, p);
825
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100826 if (x->props.extra_flags) {
827 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
828 x->props.extra_flags);
829 if (ret)
830 goto out;
831 }
832
David S. Miller1d1e34d2012-06-27 21:57:03 -0700833 if (x->coaddr) {
834 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
835 if (ret)
836 goto out;
837 }
838 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200839 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
840 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700841 if (ret)
842 goto out;
843 }
844 if (x->aead) {
845 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
846 if (ret)
847 goto out;
848 }
849 if (x->aalg) {
850 ret = copy_to_user_auth(x->aalg, skb);
851 if (!ret)
852 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
853 xfrm_alg_auth_len(x->aalg), x->aalg);
854 if (ret)
855 goto out;
856 }
857 if (x->ealg) {
858 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
859 if (ret)
860 goto out;
861 }
862 if (x->calg) {
863 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
864 if (ret)
865 goto out;
866 }
867 if (x->encap) {
868 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
869 if (ret)
870 goto out;
871 }
872 if (x->tfcpad) {
873 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
874 if (ret)
875 goto out;
876 }
877 ret = xfrm_mark_put(skb, &x->mark);
878 if (ret)
879 goto out;
dingzhif293a5e2014-10-30 09:39:36 +0100880 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700881 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
882 xfrm_replay_state_esn_len(x->replay_esn),
883 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100884 else
885 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
886 &x->replay);
887 if (ret)
888 goto out;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700889 if (x->security)
890 ret = copy_sec_ctx(x->security, skb);
891out:
892 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700893}
894
895static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
896{
897 struct xfrm_dump_info *sp = ptr;
898 struct sk_buff *in_skb = sp->in_skb;
899 struct sk_buff *skb = sp->out_skb;
900 struct xfrm_usersa_info *p;
901 struct nlmsghdr *nlh;
902 int err;
903
Eric W. Biederman15e47302012-09-07 20:12:54 +0000904 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700905 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
906 if (nlh == NULL)
907 return -EMSGSIZE;
908
909 p = nlmsg_data(nlh);
910
911 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700912 if (err) {
913 nlmsg_cancel(skb, nlh);
914 return err;
915 }
Thomas Graf98250692007-08-22 12:47:26 -0700916 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Timo Teras4c563f72008-02-28 21:31:08 -0800920static int xfrm_dump_sa_done(struct netlink_callback *cb)
921{
922 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800923 struct sock *sk = cb->skb->sk;
924 struct net *net = sock_net(sk);
925
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200926 if (cb->args[0])
927 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800928 return 0;
929}
930
Nicolas Dichteld3623092014-02-14 15:30:36 +0100931static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
933{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800934 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800935 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct xfrm_dump_info info;
937
Timo Teras4c563f72008-02-28 21:31:08 -0800938 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
939 sizeof(cb->args) - sizeof(cb->args[0]));
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 info.in_skb = cb->skb;
942 info.out_skb = skb;
943 info.nlmsg_seq = cb->nlh->nlmsg_seq;
944 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800945
946 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100947 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100948 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100949 u8 proto = 0;
950 int err;
951
Nicolas Dichteld3623092014-02-14 15:30:36 +0100952 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
953 xfrma_policy);
954 if (err < 0)
955 return err;
956
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100957 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +0200958 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
959 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +0100960 if (filter == NULL)
961 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100962 }
963
964 if (attrs[XFRMA_PROTO])
965 proto = nla_get_u8(attrs[XFRMA_PROTO]);
966
967 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200968 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -0800969 }
970
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800971 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 return skb->len;
974}
975
976static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
977 struct xfrm_state *x, u32 seq)
978{
979 struct xfrm_dump_info info;
980 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000981 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Thomas Graf7deb2262007-08-22 13:57:39 -0700983 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (!skb)
985 return ERR_PTR(-ENOMEM);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 info.in_skb = in_skb;
988 info.out_skb = skb;
989 info.nlmsg_seq = seq;
990 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Mathias Krause864745d2012-09-13 11:41:26 +0000992 err = dump_one_state(x, 0, &info);
993 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000995 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
998 return skb;
999}
1000
Michal Kubecek21ee5432014-06-03 10:26:06 +02001001/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1002 * Must be called with RCU read lock.
1003 */
1004static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1005 u32 pid, unsigned int group)
1006{
1007 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1008
Florian Westphal301a6da2018-06-25 14:00:07 +02001009 if (!nlsk) {
1010 kfree_skb(skb);
1011 return -EPIPE;
1012 }
1013
1014 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
Michal Kubecek21ee5432014-06-03 10:26:06 +02001015}
1016
Thomas Graf7deb2262007-08-22 13:57:39 -07001017static inline size_t xfrm_spdinfo_msgsize(void)
1018{
1019 return NLMSG_ALIGN(4)
1020 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001021 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1022 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1023 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -07001024}
1025
Alexey Dobriyane0710412010-01-23 13:37:10 +00001026static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001027 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001028{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001029 struct xfrmk_spdinfo si;
1030 struct xfrmu_spdinfo spc;
1031 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001032 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001033 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001034 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001035 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001036 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001037
Eric W. Biederman15e47302012-09-07 20:12:54 +00001038 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001039 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001040 return -EMSGSIZE;
1041
1042 f = nlmsg_data(nlh);
1043 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001044 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001045 spc.incnt = si.incnt;
1046 spc.outcnt = si.outcnt;
1047 spc.fwdcnt = si.fwdcnt;
1048 spc.inscnt = si.inscnt;
1049 spc.outscnt = si.outscnt;
1050 spc.fwdscnt = si.fwdscnt;
1051 sph.spdhcnt = si.spdhcnt;
1052 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001053
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001054 do {
1055 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1056
1057 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1058 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1059 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1060 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1061 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1062
David S. Miller1d1e34d2012-06-27 21:57:03 -07001063 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1064 if (!err)
1065 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001066 if (!err)
1067 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1068 if (!err)
1069 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001070 if (err) {
1071 nlmsg_cancel(skb, nlh);
1072 return err;
1073 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001074
Johannes Berg053c0952015-01-16 22:09:00 +01001075 nlmsg_end(skb, nlh);
1076 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001077}
1078
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001079static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1080 struct nlattr **attrs)
1081{
1082 struct net *net = sock_net(skb->sk);
1083 struct xfrmu_spdhthresh *thresh4 = NULL;
1084 struct xfrmu_spdhthresh *thresh6 = NULL;
1085
1086 /* selector prefixlen thresholds to hash policies */
1087 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1088 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1089
1090 if (nla_len(rta) < sizeof(*thresh4))
1091 return -EINVAL;
1092 thresh4 = nla_data(rta);
1093 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1094 return -EINVAL;
1095 }
1096 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1097 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1098
1099 if (nla_len(rta) < sizeof(*thresh6))
1100 return -EINVAL;
1101 thresh6 = nla_data(rta);
1102 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1103 return -EINVAL;
1104 }
1105
1106 if (thresh4 || thresh6) {
1107 write_seqlock(&net->xfrm.policy_hthresh.lock);
1108 if (thresh4) {
1109 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1110 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1111 }
1112 if (thresh6) {
1113 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1114 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1115 }
1116 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1117
1118 xfrm_policy_hash_rebuild(net);
1119 }
1120
1121 return 0;
1122}
1123
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001124static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001125 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001126{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001127 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001128 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001129 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001130 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001131 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001132
Thomas Graf7deb2262007-08-22 13:57:39 -07001133 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001134 if (r_skb == NULL)
1135 return -ENOMEM;
1136
Eric W. Biederman15e47302012-09-07 20:12:54 +00001137 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001138 BUG();
1139
Eric W. Biederman15e47302012-09-07 20:12:54 +00001140 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001141}
1142
Thomas Graf7deb2262007-08-22 13:57:39 -07001143static inline size_t xfrm_sadinfo_msgsize(void)
1144{
1145 return NLMSG_ALIGN(4)
1146 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1147 + nla_total_size(4); /* XFRMA_SAD_CNT */
1148}
1149
Alexey Dobriyane0710412010-01-23 13:37:10 +00001150static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001151 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001152{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001153 struct xfrmk_sadinfo si;
1154 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001155 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001156 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001157 u32 *f;
1158
Eric W. Biederman15e47302012-09-07 20:12:54 +00001159 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001160 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001161 return -EMSGSIZE;
1162
1163 f = nlmsg_data(nlh);
1164 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001165 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001166
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001167 sh.sadhmcnt = si.sadhmcnt;
1168 sh.sadhcnt = si.sadhcnt;
1169
David S. Miller1d1e34d2012-06-27 21:57:03 -07001170 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1171 if (!err)
1172 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1173 if (err) {
1174 nlmsg_cancel(skb, nlh);
1175 return err;
1176 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001177
Johannes Berg053c0952015-01-16 22:09:00 +01001178 nlmsg_end(skb, nlh);
1179 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001180}
1181
1182static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001183 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001184{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001185 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001186 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001187 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001188 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001189 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001190
Thomas Graf7deb2262007-08-22 13:57:39 -07001191 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001192 if (r_skb == NULL)
1193 return -ENOMEM;
1194
Eric W. Biederman15e47302012-09-07 20:12:54 +00001195 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001196 BUG();
1197
Eric W. Biederman15e47302012-09-07 20:12:54 +00001198 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001199}
1200
Christoph Hellwig22e70052007-01-02 15:22:30 -08001201static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001202 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001204 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001205 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 struct xfrm_state *x;
1207 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001208 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001210 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (x == NULL)
1212 goto out_noput;
1213
1214 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1215 if (IS_ERR(resp_skb)) {
1216 err = PTR_ERR(resp_skb);
1217 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001218 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220 xfrm_state_put(x);
1221out_noput:
1222 return err;
1223}
1224
Christoph Hellwig22e70052007-01-02 15:22:30 -08001225static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001226 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001228 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct xfrm_state *x;
1230 struct xfrm_userspi_info *p;
1231 struct sk_buff *resp_skb;
1232 xfrm_address_t *daddr;
1233 int family;
1234 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001235 u32 mark;
1236 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Thomas Graf7b67c852007-08-22 13:53:52 -07001238 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001239 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (err)
1241 goto out_noput;
1242
1243 family = p->info.family;
1244 daddr = &p->info.id.daddr;
1245
1246 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001247
1248 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001250 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001251 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 xfrm_state_put(x);
1253 x = NULL;
1254 }
1255 }
1256
1257 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001258 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 p->info.id.proto, daddr,
1260 &p->info.saddr, 1,
1261 family);
1262 err = -ENOENT;
1263 if (x == NULL)
1264 goto out_noput;
1265
Herbert Xu658b2192007-10-09 13:29:52 -07001266 err = xfrm_alloc_spi(x, p->min, p->max);
1267 if (err)
1268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Herbert Xu658b2192007-10-09 13:29:52 -07001270 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (IS_ERR(resp_skb)) {
1272 err = PTR_ERR(resp_skb);
1273 goto out;
1274 }
1275
Eric W. Biederman15e47302012-09-07 20:12:54 +00001276 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278out:
1279 xfrm_state_put(x);
1280out_noput:
1281 return err;
1282}
1283
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001284static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 switch (dir) {
1287 case XFRM_POLICY_IN:
1288 case XFRM_POLICY_OUT:
1289 case XFRM_POLICY_FWD:
1290 break;
1291
1292 default:
1293 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 return 0;
1297}
1298
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001299static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001300{
1301 switch (type) {
1302 case XFRM_POLICY_TYPE_MAIN:
1303#ifdef CONFIG_XFRM_SUB_POLICY
1304 case XFRM_POLICY_TYPE_SUB:
1305#endif
1306 break;
1307
1308 default:
1309 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001310 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001311
1312 return 0;
1313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1316{
Fan Due682adf2013-11-07 17:47:48 +08001317 int ret;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 switch (p->share) {
1320 case XFRM_SHARE_ANY:
1321 case XFRM_SHARE_SESSION:
1322 case XFRM_SHARE_USER:
1323 case XFRM_SHARE_UNIQUE:
1324 break;
1325
1326 default:
1327 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 switch (p->action) {
1331 case XFRM_POLICY_ALLOW:
1332 case XFRM_POLICY_BLOCK:
1333 break;
1334
1335 default:
1336 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 switch (p->sel.family) {
1340 case AF_INET:
Steffen Klassert89cb86e2018-08-01 13:45:11 +02001341 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1342 return -EINVAL;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
1345
1346 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001347#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert89cb86e2018-08-01 13:45:11 +02001348 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1349 return -EINVAL;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 break;
1352#else
1353 return -EAFNOSUPPORT;
1354#endif
1355
1356 default:
1357 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Fan Due682adf2013-11-07 17:47:48 +08001360 ret = verify_policy_dir(p->dir);
1361 if (ret)
1362 return ret;
YueHaibing7c967212019-02-28 15:18:59 +08001363 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
Fan Due682adf2013-11-07 17:47:48 +08001364 return -EINVAL;
1365
1366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Thomas Graf5424f322007-08-22 14:01:33 -07001369static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001370{
Thomas Graf5424f322007-08-22 14:01:33 -07001371 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001372 struct xfrm_user_sec_ctx *uctx;
1373
1374 if (!rt)
1375 return 0;
1376
Thomas Graf5424f322007-08-22 14:01:33 -07001377 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001378 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001379}
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1382 int nr)
1383{
1384 int i;
1385
1386 xp->xfrm_nr = nr;
1387 for (i = 0; i < nr; i++, ut++) {
1388 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1389
1390 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1391 memcpy(&t->saddr, &ut->saddr,
1392 sizeof(xfrm_address_t));
1393 t->reqid = ut->reqid;
1394 t->mode = ut->mode;
1395 t->share = ut->share;
1396 t->optional = ut->optional;
1397 t->aalgos = ut->aalgos;
1398 t->ealgos = ut->ealgos;
1399 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001400 /* If all masks are ~0, then we allow all algorithms. */
1401 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001402 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404}
1405
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001406static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1407{
Steffen Klassert9a54c512017-12-08 08:07:25 +01001408 u16 prev_family;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001409 int i;
1410
1411 if (nr > XFRM_MAX_DEPTH)
1412 return -EINVAL;
1413
Steffen Klassert9a54c512017-12-08 08:07:25 +01001414 prev_family = family;
1415
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001416 for (i = 0; i < nr; i++) {
1417 /* We never validated the ut->family value, so many
1418 * applications simply leave it at zero. The check was
1419 * never made and ut->family was ignored because all
1420 * templates could be assumed to have the same family as
1421 * the policy itself. Now that we will have ipv4-in-ipv6
1422 * and ipv6-in-ipv4 tunnels, this is no longer true.
1423 */
1424 if (!ut[i].family)
1425 ut[i].family = family;
1426
Florian Westphala19fd852019-01-09 14:37:34 +01001427 switch (ut[i].mode) {
1428 case XFRM_MODE_TUNNEL:
1429 case XFRM_MODE_BEET:
1430 break;
1431 default:
1432 if (ut[i].family != prev_family)
1433 return -EINVAL;
1434 break;
1435 }
Sean Tranchettide500b92018-09-19 13:54:56 -06001436 if (ut[i].mode >= XFRM_MODE_MAX)
1437 return -EINVAL;
1438
Steffen Klassert9a54c512017-12-08 08:07:25 +01001439 prev_family = ut[i].family;
1440
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001441 switch (ut[i].family) {
1442 case AF_INET:
1443 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001444#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001445 case AF_INET6:
1446 break;
1447#endif
1448 default:
1449 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001450 }
Cong Wang85552882017-11-27 11:15:16 -08001451
1452 switch (ut[i].id.proto) {
1453 case IPPROTO_AH:
1454 case IPPROTO_ESP:
1455 case IPPROTO_COMP:
1456#if IS_ENABLED(CONFIG_IPV6)
1457 case IPPROTO_ROUTING:
1458 case IPPROTO_DSTOPTS:
1459#endif
1460 case IPSEC_PROTO_ANY:
1461 break;
1462 default:
1463 return -EINVAL;
1464 }
1465
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001466 }
1467
1468 return 0;
1469}
1470
Thomas Graf5424f322007-08-22 14:01:33 -07001471static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Thomas Graf5424f322007-08-22 14:01:33 -07001473 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (!rt) {
1476 pol->xfrm_nr = 0;
1477 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001478 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1479 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001480 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001482 err = validate_tmpl(nr, utmpl, pol->family);
1483 if (err)
1484 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Thomas Graf5424f322007-08-22 14:01:33 -07001486 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488 return 0;
1489}
1490
Thomas Graf5424f322007-08-22 14:01:33 -07001491static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001492{
Thomas Graf5424f322007-08-22 14:01:33 -07001493 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001494 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001495 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001496 int err;
1497
1498 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001499 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001500 type = upt->type;
1501 }
1502
1503 err = verify_policy_type(type);
1504 if (err)
1505 return err;
1506
1507 *tp = type;
1508 return 0;
1509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1512{
1513 xp->priority = p->priority;
1514 xp->index = p->index;
1515 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1516 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1517 xp->action = p->action;
1518 xp->flags = p->flags;
1519 xp->family = p->sel.family;
1520 /* XXX xp->share = p->share; */
1521}
1522
1523static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1524{
Mathias Krause7b789832012-09-19 11:33:40 +00001525 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1527 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1528 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1529 p->priority = xp->priority;
1530 p->index = xp->index;
1531 p->sel.family = xp->family;
1532 p->dir = dir;
1533 p->action = xp->action;
1534 p->flags = xp->flags;
1535 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1536}
1537
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001538static 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 -07001539{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001540 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 int err;
1542
1543 if (!xp) {
1544 *errp = -ENOMEM;
1545 return NULL;
1546 }
1547
1548 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001549
Thomas Graf35a7aa02007-08-22 14:00:40 -07001550 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001551 if (err)
1552 goto error;
1553
Thomas Graf35a7aa02007-08-22 14:00:40 -07001554 if (!(err = copy_from_user_tmpl(xp, attrs)))
1555 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001556 if (err)
1557 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001559 xfrm_mark_get(attrs, &xp->mark);
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001562 error:
1563 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001564 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001565 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001566 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Christoph Hellwig22e70052007-01-02 15:22:30 -08001569static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001570 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001572 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001573 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001575 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 int err;
1577 int excl;
1578
1579 err = verify_newpolicy_info(p);
1580 if (err)
1581 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001582 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001583 if (err)
1584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001586 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (!xp)
1588 return err;
1589
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001590 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001591 * Aha! this is anti-netlink really i.e more pfkey derived
1592 * in netlink excl is a flag and you wouldnt need
1593 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1595 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001596 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001599 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 kfree(xp);
1601 return err;
1602 }
1603
Herbert Xuf60f6b82005-06-18 22:44:37 -07001604 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001605 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001606 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001607 km_policy_notify(xp, p->dir, &c);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 xfrm_pol_put(xp);
1610
1611 return 0;
1612}
1613
1614static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1615{
1616 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1617 int i;
1618
1619 if (xp->xfrm_nr == 0)
1620 return 0;
1621
1622 for (i = 0; i < xp->xfrm_nr; i++) {
1623 struct xfrm_user_tmpl *up = &vec[i];
1624 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1625
Mathias Krause1f868402012-09-19 11:33:41 +00001626 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001628 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1630 up->reqid = kp->reqid;
1631 up->mode = kp->mode;
1632 up->share = kp->share;
1633 up->optional = kp->optional;
1634 up->aalgos = kp->aalgos;
1635 up->ealgos = kp->ealgos;
1636 up->calgos = kp->calgos;
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Thomas Grafc0144be2007-08-22 13:55:43 -07001639 return nla_put(skb, XFRMA_TMPL,
1640 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001641}
1642
Serge Hallyn0d681622006-07-24 23:30:44 -07001643static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1644{
1645 if (x->security) {
1646 return copy_sec_ctx(x->security, skb);
1647 }
1648 return 0;
1649}
1650
1651static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1652{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001653 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001654 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001655 return 0;
1656}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001657static inline size_t userpolicy_type_attrsize(void)
1658{
1659#ifdef CONFIG_XFRM_SUB_POLICY
1660 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1661#else
1662 return 0;
1663#endif
1664}
Serge Hallyn0d681622006-07-24 23:30:44 -07001665
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001666#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001667static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001668{
Eric Dumazet2038a9e2018-06-18 21:35:07 -07001669 struct xfrm_userpolicy_type upt;
1670
1671 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1672 memset(&upt, 0, sizeof(upt));
1673 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001674
Thomas Grafc0144be2007-08-22 13:55:43 -07001675 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001676}
1677
1678#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001679static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001680{
1681 return 0;
1682}
1683#endif
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1686{
1687 struct xfrm_dump_info *sp = ptr;
1688 struct xfrm_userpolicy_info *p;
1689 struct sk_buff *in_skb = sp->in_skb;
1690 struct sk_buff *skb = sp->out_skb;
1691 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001692 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Eric W. Biederman15e47302012-09-07 20:12:54 +00001694 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001695 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1696 if (nlh == NULL)
1697 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Thomas Graf7b67c852007-08-22 13:53:52 -07001699 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001701 err = copy_to_user_tmpl(xp, skb);
1702 if (!err)
1703 err = copy_to_user_sec_ctx(xp, skb);
1704 if (!err)
1705 err = copy_to_user_policy_type(xp->type, skb);
1706 if (!err)
1707 err = xfrm_mark_put(skb, &xp->mark);
1708 if (err) {
1709 nlmsg_cancel(skb, nlh);
1710 return err;
1711 }
Thomas Graf98250692007-08-22 12:47:26 -07001712 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Timo Teras4c563f72008-02-28 21:31:08 -08001716static int xfrm_dump_policy_done(struct netlink_callback *cb)
1717{
Herbert Xu543aabb2017-10-19 20:51:10 +08001718 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Fan Du283bc9f2013-11-07 17:47:50 +08001719 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001720
Fan Du283bc9f2013-11-07 17:47:50 +08001721 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001722 return 0;
1723}
1724
Herbert Xu543aabb2017-10-19 20:51:10 +08001725static int xfrm_dump_policy_start(struct netlink_callback *cb)
1726{
1727 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1728
1729 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1730
1731 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1732 return 0;
1733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1736{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001737 struct net *net = sock_net(skb->sk);
Herbert Xu543aabb2017-10-19 20:51:10 +08001738 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct xfrm_dump_info info;
1740
1741 info.in_skb = cb->skb;
1742 info.out_skb = skb;
1743 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1744 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001745
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001746 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 return skb->len;
1749}
1750
1751static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1752 struct xfrm_policy *xp,
1753 int dir, u32 seq)
1754{
1755 struct xfrm_dump_info info;
1756 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001757 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Thomas Graf7deb2262007-08-22 13:57:39 -07001759 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (!skb)
1761 return ERR_PTR(-ENOMEM);
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 info.in_skb = in_skb;
1764 info.out_skb = skb;
1765 info.nlmsg_seq = seq;
1766 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Mathias Krausec2546372012-09-14 09:58:32 +00001768 err = dump_one_policy(xp, dir, 0, &info);
1769 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001771 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
1774 return skb;
1775}
1776
Christoph Hellwig22e70052007-01-02 15:22:30 -08001777static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001778 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001780 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 struct xfrm_policy *xp;
1782 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001783 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001785 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001787 struct xfrm_mark m;
1788 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Thomas Graf7b67c852007-08-22 13:53:52 -07001790 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1792
Thomas Graf35a7aa02007-08-22 14:00:40 -07001793 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001794 if (err)
1795 return err;
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 err = verify_policy_dir(p->dir);
1798 if (err)
1799 return err;
1800
1801 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001802 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001803 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001804 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001805 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001806
Thomas Graf35a7aa02007-08-22 14:00:40 -07001807 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001808 if (err)
1809 return err;
1810
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001811 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001812 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001813 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001814
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001815 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001816 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001817 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001818 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001819 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001820 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001821 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (xp == NULL)
1824 return -ENOENT;
1825
1826 if (!delete) {
1827 struct sk_buff *resp_skb;
1828
1829 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1830 if (IS_ERR(resp_skb)) {
1831 err = PTR_ERR(resp_skb);
1832 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001833 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001834 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001836 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001837 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001838
1839 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001840 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001841
Herbert Xue7443892005-06-18 22:44:18 -07001842 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001843 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001844 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001845 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001846 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001849out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001850 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001851 if (delete && err == 0)
1852 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return err;
1854}
1855
Christoph Hellwig22e70052007-01-02 15:22:30 -08001856static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001857 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001859 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001860 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001861 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001862 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Tetsuo Handa2e710292014-04-22 21:48:30 +09001864 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001865 if (err) {
1866 if (err == -ESRCH) /* empty table */
1867 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001868 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001869 }
Herbert Xubf088672005-06-18 22:44:00 -07001870 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001871 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001872 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001873 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001874 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001875 km_state_notify(NULL, &c);
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return 0;
1878}
1879
Steffen Klassertd8647b72011-03-08 00:10:27 +00001880static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001881{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001882 size_t replay_size = x->replay_esn ?
1883 xfrm_replay_state_esn_len(x->replay_esn) :
1884 sizeof(struct xfrm_replay_state);
1885
Thomas Graf7deb2262007-08-22 13:57:39 -07001886 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001887 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001888 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001889 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001890 + nla_total_size(4) /* XFRM_AE_RTHR */
1891 + nla_total_size(4); /* XFRM_AE_ETHR */
1892}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001893
David S. Miller214e0052011-02-24 00:02:38 -05001894static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001895{
1896 struct xfrm_aevent_id *id;
1897 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001898 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001899
Eric W. Biederman15e47302012-09-07 20:12:54 +00001900 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001901 if (nlh == NULL)
1902 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001903
Thomas Graf7b67c852007-08-22 13:53:52 -07001904 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001905 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001906 id->sa_id.spi = x->id.spi;
1907 id->sa_id.family = x->props.family;
1908 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001909 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001910 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001911 id->flags = c->data.aevent;
1912
David S. Millerd0fde792012-03-29 04:02:26 -04001913 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001914 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1915 xfrm_replay_state_esn_len(x->replay_esn),
1916 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001917 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001918 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1919 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001920 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001921 if (err)
1922 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001923 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1924 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001925 if (err)
1926 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001927
David S. Miller1d1e34d2012-06-27 21:57:03 -07001928 if (id->flags & XFRM_AE_RTHR) {
1929 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1930 if (err)
1931 goto out_cancel;
1932 }
1933 if (id->flags & XFRM_AE_ETHR) {
1934 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1935 x->replay_maxage * 10 / HZ);
1936 if (err)
1937 goto out_cancel;
1938 }
1939 err = xfrm_mark_put(skb, &x->mark);
1940 if (err)
1941 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001942
Johannes Berg053c0952015-01-16 22:09:00 +01001943 nlmsg_end(skb, nlh);
1944 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001945
David S. Miller1d1e34d2012-06-27 21:57:03 -07001946out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001947 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001948 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001949}
1950
Christoph Hellwig22e70052007-01-02 15:22:30 -08001951static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001952 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001953{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001954 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001955 struct xfrm_state *x;
1956 struct sk_buff *r_skb;
1957 int err;
1958 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001959 u32 mark;
1960 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001961 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001962 struct xfrm_usersa_id *id = &p->sa_id;
1963
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001964 mark = xfrm_mark_get(attrs, &m);
1965
1966 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001967 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001968 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001969
1970 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1971 if (r_skb == NULL) {
1972 xfrm_state_put(x);
1973 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001974 }
1975
1976 /*
1977 * XXX: is this lock really needed - none of the other
1978 * gets lock (the concern is things getting updated
1979 * while we are still reading) - jhs
1980 */
1981 spin_lock_bh(&x->lock);
1982 c.data.aevent = p->flags;
1983 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001984 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001985
1986 if (build_aevent(r_skb, x, &c) < 0)
1987 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001988 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001989 spin_unlock_bh(&x->lock);
1990 xfrm_state_put(x);
1991 return err;
1992}
1993
Christoph Hellwig22e70052007-01-02 15:22:30 -08001994static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001995 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001996{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001997 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001998 struct xfrm_state *x;
1999 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08002000 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002001 u32 mark = 0;
2002 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07002003 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07002004 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00002005 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07002006 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02002007 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2008 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002009
Michael Rossberg4e077232015-09-29 11:25:08 +02002010 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002011 return err;
2012
2013 /* pedantic mode - thou shalt sayeth replaceth */
2014 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2015 return err;
2016
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002017 mark = xfrm_mark_get(attrs, &m);
2018
2019 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 -08002020 if (x == NULL)
2021 return -ESRCH;
2022
2023 if (x->km.state != XFRM_STATE_VALID)
2024 goto out;
2025
Steffen Klassert4479ff72013-09-09 09:39:01 +02002026 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00002027 if (err)
2028 goto out;
2029
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002030 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00002031 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002032 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002033
2034 c.event = nlh->nlmsg_type;
2035 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002036 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002037 c.data.aevent = XFRM_AE_CU;
2038 km_state_notify(x, &c);
2039 err = 0;
2040out:
2041 xfrm_state_put(x);
2042 return err;
2043}
2044
Christoph Hellwig22e70052007-01-02 15:22:30 -08002045static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002046 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002048 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002049 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002050 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002051 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002052
Thomas Graf35a7aa02007-08-22 14:00:40 -07002053 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002054 if (err)
2055 return err;
2056
Tetsuo Handa2e710292014-04-22 21:48:30 +09002057 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002058 if (err) {
2059 if (err == -ESRCH) /* empty table */
2060 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002061 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002062 }
2063
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002064 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002065 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002066 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002067 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002068 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002069 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return 0;
2071}
2072
Christoph Hellwig22e70052007-01-02 15:22:30 -08002073static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002074 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002075{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002076 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002077 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002078 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002079 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002080 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002081 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002082 struct xfrm_mark m;
2083 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002084
Thomas Graf35a7aa02007-08-22 14:00:40 -07002085 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002086 if (err)
2087 return err;
2088
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002089 err = verify_policy_dir(p->dir);
2090 if (err)
2091 return err;
2092
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002093 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002094 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002095 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002096 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002097 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002098
Thomas Graf35a7aa02007-08-22 14:00:40 -07002099 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002100 if (err)
2101 return err;
2102
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002103 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002104 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002105 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002106
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002107 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002108 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002109 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002110 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002111 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002112 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002113 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002114 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002115 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002116 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002117
Timo Teräsea2dea92010-03-31 00:17:05 +00002118 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002119 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002120
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002121 err = 0;
2122 if (up->hard) {
2123 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002124 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002125 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002126 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002127
2128out:
2129 xfrm_pol_put(xp);
2130 return err;
2131}
2132
Christoph Hellwig22e70052007-01-02 15:22:30 -08002133static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002134 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002135{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002136 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002137 struct xfrm_state *x;
2138 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002139 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002140 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002141 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002142 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002143
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002144 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 -08002145
David S. Miller3a765aa2007-02-26 14:52:21 -08002146 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002147 if (x == NULL)
2148 return err;
2149
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002150 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002151 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002152 if (x->km.state != XFRM_STATE_VALID)
2153 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002154 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002155
Joy Latten161a09e2006-11-27 13:11:54 -06002156 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002157 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002158 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002159 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002160 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002161out:
2162 spin_unlock_bh(&x->lock);
2163 xfrm_state_put(x);
2164 return err;
2165}
2166
Christoph Hellwig22e70052007-01-02 15:22:30 -08002167static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002168 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002169{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002170 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002171 struct xfrm_policy *xp;
2172 struct xfrm_user_tmpl *ut;
2173 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002174 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002175 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002176
Thomas Graf7b67c852007-08-22 13:53:52 -07002177 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002178 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002179 int err = -ENOMEM;
2180
2181 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002182 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002183
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002184 xfrm_mark_get(attrs, &mark);
2185
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002186 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002187 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002188 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002189
2190 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002191 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002192 if (!xp)
2193 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002194
2195 memcpy(&x->id, &ua->id, sizeof(ua->id));
2196 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2197 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002198 xp->mark.m = x->mark.m = mark.m;
2199 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002200 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002201 /* extract the templates and for each call km_key */
2202 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2203 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2204 memcpy(&x->id, &t->id, sizeof(x->id));
2205 x->props.mode = t->mode;
2206 x->props.reqid = t->reqid;
2207 x->props.family = ut->family;
2208 t->aalgos = ua->aalgos;
2209 t->ealgos = ua->ealgos;
2210 t->calgos = ua->calgos;
2211 err = km_query(x, t, xp);
2212
2213 }
2214
2215 kfree(x);
2216 kfree(xp);
2217
2218 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002219
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002220free_state:
2221 kfree(x);
2222nomem:
2223 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002224}
2225
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002226#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002227static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002228 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002229 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002230{
Thomas Graf5424f322007-08-22 14:01:33 -07002231 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232 struct xfrm_user_migrate *um;
2233 int i, num_migrate;
2234
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002235 if (k != NULL) {
2236 struct xfrm_user_kmaddress *uk;
2237
2238 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2239 memcpy(&k->local, &uk->local, sizeof(k->local));
2240 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2241 k->family = uk->family;
2242 k->reserved = uk->reserved;
2243 }
2244
Thomas Graf5424f322007-08-22 14:01:33 -07002245 um = nla_data(rt);
2246 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002247
2248 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2249 return -EINVAL;
2250
2251 for (i = 0; i < num_migrate; i++, um++, ma++) {
2252 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2253 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2254 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2255 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2256
2257 ma->proto = um->proto;
2258 ma->mode = um->mode;
2259 ma->reqid = um->reqid;
2260
2261 ma->old_family = um->old_family;
2262 ma->new_family = um->new_family;
2263 }
2264
2265 *num = i;
2266 return 0;
2267}
2268
2269static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002270 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002271{
Thomas Graf7b67c852007-08-22 13:53:52 -07002272 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002273 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002274 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002275 u8 type;
2276 int err;
2277 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002278 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002279
Thomas Graf35a7aa02007-08-22 14:00:40 -07002280 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002281 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002282
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002283 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2284
Thomas Graf5424f322007-08-22 14:01:33 -07002285 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002286 if (err)
2287 return err;
2288
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002289 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002290 if (err)
2291 return err;
2292
2293 if (!n)
2294 return 0;
2295
Fan Du8d549c42013-11-07 17:47:49 +08002296 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002297
2298 return 0;
2299}
2300#else
2301static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002302 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002303{
2304 return -ENOPROTOOPT;
2305}
2306#endif
2307
2308#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002309static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002310{
2311 struct xfrm_user_migrate um;
2312
2313 memset(&um, 0, sizeof(um));
2314 um.proto = m->proto;
2315 um.mode = m->mode;
2316 um.reqid = m->reqid;
2317 um.old_family = m->old_family;
2318 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2319 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2320 um.new_family = m->new_family;
2321 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2322 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2323
Thomas Grafc0144be2007-08-22 13:55:43 -07002324 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002325}
2326
David S. Miller183cad12011-02-24 00:28:01 -05002327static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002328{
2329 struct xfrm_user_kmaddress uk;
2330
2331 memset(&uk, 0, sizeof(uk));
2332 uk.family = k->family;
2333 uk.reserved = k->reserved;
2334 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002335 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002336
2337 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2338}
2339
2340static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002341{
2342 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002343 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2344 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2345 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002346}
2347
David S. Miller183cad12011-02-24 00:28:01 -05002348static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2349 int num_migrate, const struct xfrm_kmaddress *k,
2350 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002351{
David S. Miller183cad12011-02-24 00:28:01 -05002352 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002353 struct xfrm_userpolicy_id *pol_id;
2354 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002355 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002356
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002357 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2358 if (nlh == NULL)
2359 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002360
Thomas Graf7b67c852007-08-22 13:53:52 -07002361 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002362 /* copy data from selector, dir, and type to the pol_id */
2363 memset(pol_id, 0, sizeof(*pol_id));
2364 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2365 pol_id->dir = dir;
2366
David S. Miller1d1e34d2012-06-27 21:57:03 -07002367 if (k != NULL) {
2368 err = copy_to_user_kmaddress(k, skb);
2369 if (err)
2370 goto out_cancel;
2371 }
2372 err = copy_to_user_policy_type(type, skb);
2373 if (err)
2374 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002375 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002376 err = copy_to_user_migrate(mp, skb);
2377 if (err)
2378 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002379 }
2380
Johannes Berg053c0952015-01-16 22:09:00 +01002381 nlmsg_end(skb, nlh);
2382 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002383
2384out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002385 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002386 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002387}
2388
David S. Miller183cad12011-02-24 00:28:01 -05002389static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2390 const struct xfrm_migrate *m, int num_migrate,
2391 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002392{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002393 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002394 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002395
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002396 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002397 if (skb == NULL)
2398 return -ENOMEM;
2399
2400 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002401 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002402 BUG();
2403
Michal Kubecek21ee5432014-06-03 10:26:06 +02002404 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002405}
2406#else
David S. Miller183cad12011-02-24 00:28:01 -05002407static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2408 const struct xfrm_migrate *m, int num_migrate,
2409 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002410{
2411 return -ENOPROTOOPT;
2412}
2413#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002414
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002415#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002416
2417static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002418 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002419 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2420 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2421 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2422 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2423 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2424 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002425 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002426 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002427 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002428 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002429 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002430 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002431 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002432 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2433 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002434 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002435 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002436 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002437 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002438 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439};
2440
Thomas Graf492b5582005-05-03 14:26:40 -07002441#undef XMSGSIZE
2442
Thomas Grafcf5cb792007-08-22 13:59:04 -07002443static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002444 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2445 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2446 [XFRMA_LASTUSED] = { .type = NLA_U64},
2447 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002448 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002449 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2450 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2451 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2452 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2453 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2454 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2455 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2456 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2457 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2458 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2459 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2460 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2461 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2462 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002463 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002464 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002465 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002466 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002467 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002468 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002469 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002470};
2471
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002472static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2473 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2474 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2475};
2476
Mathias Krause05600a72013-02-24 14:10:27 +01002477static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002478 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Herbert Xu543aabb2017-10-19 20:51:10 +08002479 int (*start)(struct netlink_callback *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002481 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002482 const struct nla_policy *nla_pol;
2483 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002484} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2485 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2486 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2487 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002488 .dump = xfrm_dump_sa,
2489 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002490 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2491 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2492 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Herbert Xu543aabb2017-10-19 20:51:10 +08002493 .start = xfrm_dump_policy_start,
Timo Teras4c563f72008-02-28 21:31:08 -08002494 .dump = xfrm_dump_policy,
2495 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002496 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002497 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002498 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002499 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2500 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002501 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002502 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2503 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002504 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2505 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002506 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002507 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002508 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2509 .nla_pol = xfrma_spd_policy,
2510 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002511 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512};
2513
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002514static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002516 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002517 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002518 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002519 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Fan Du74005992015-01-27 17:00:29 +08002521#ifdef CONFIG_COMPAT
Andy Lutomirski2bf8c472016-03-22 14:25:10 -07002522 if (in_compat_syscall())
Yi Zhao83e2d052016-11-29 18:09:01 +08002523 return -EOPNOTSUPP;
Fan Du74005992015-01-27 17:00:29 +08002524#endif
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002528 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 type -= XFRM_MSG_BASE;
2531 link = &xfrm_dispatch[type];
2532
2533 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002534 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002535 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Thomas Graf492b5582005-05-03 14:26:40 -07002537 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2538 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002539 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002541 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002543 {
2544 struct netlink_dump_control c = {
Herbert Xu543aabb2017-10-19 20:51:10 +08002545 .start = link->start,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002546 .dump = link->dump,
2547 .done = link->done,
2548 };
2549 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002553 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2554 link->nla_max ? : XFRMA_MAX,
2555 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002556 if (err < 0)
2557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Thomas Graf5424f322007-08-22 14:01:33 -07002562 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002565static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Fan Du283bc9f2013-11-07 17:47:50 +08002567 struct net *net = sock_net(skb->sk);
2568
2569 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002570 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002571 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
Thomas Graf7deb2262007-08-22 13:57:39 -07002574static inline size_t xfrm_expire_msgsize(void)
2575{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002576 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2577 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002578}
2579
David S. Miller214e0052011-02-24 00:02:38 -05002580static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 struct xfrm_user_expire *ue;
2583 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002584 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Eric W. Biederman15e47302012-09-07 20:12:54 +00002586 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002587 if (nlh == NULL)
2588 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Thomas Graf7b67c852007-08-22 13:53:52 -07002590 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002592 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
David S. Miller1d1e34d2012-06-27 21:57:03 -07002594 err = xfrm_mark_put(skb, &x->mark);
2595 if (err)
2596 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002597
Johannes Berg053c0952015-01-16 22:09:00 +01002598 nlmsg_end(skb, nlh);
2599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
David S. Miller214e0052011-02-24 00:02:38 -05002602static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002604 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct sk_buff *skb;
2606
Thomas Graf7deb2262007-08-22 13:57:39 -07002607 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (skb == NULL)
2609 return -ENOMEM;
2610
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002611 if (build_expire(skb, x, c) < 0) {
2612 kfree_skb(skb);
2613 return -EMSGSIZE;
2614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Michal Kubecek21ee5432014-06-03 10:26:06 +02002616 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
David S. Miller214e0052011-02-24 00:02:38 -05002619static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002620{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002621 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002622 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002623
Steffen Klassertd8647b72011-03-08 00:10:27 +00002624 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002625 if (skb == NULL)
2626 return -ENOMEM;
2627
2628 if (build_aevent(skb, x, c) < 0)
2629 BUG();
2630
Michal Kubecek21ee5432014-06-03 10:26:06 +02002631 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002632}
2633
David S. Miller214e0052011-02-24 00:02:38 -05002634static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002635{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002636 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002637 struct xfrm_usersa_flush *p;
2638 struct nlmsghdr *nlh;
2639 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002640 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002641
Thomas Graf7deb2262007-08-22 13:57:39 -07002642 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002643 if (skb == NULL)
2644 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002645
Eric W. Biederman15e47302012-09-07 20:12:54 +00002646 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002647 if (nlh == NULL) {
2648 kfree_skb(skb);
2649 return -EMSGSIZE;
2650 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002651
Thomas Graf7b67c852007-08-22 13:53:52 -07002652 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002653 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002654
Thomas Graf98250692007-08-22 12:47:26 -07002655 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002656
Michal Kubecek21ee5432014-06-03 10:26:06 +02002657 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002658}
2659
Thomas Graf7deb2262007-08-22 13:57:39 -07002660static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002661{
Thomas Graf7deb2262007-08-22 13:57:39 -07002662 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002663 if (x->aead)
2664 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002665 if (x->aalg) {
2666 l += nla_total_size(sizeof(struct xfrm_algo) +
2667 (x->aalg->alg_key_len + 7) / 8);
2668 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2669 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002670 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002671 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002672 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002673 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002674 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002675 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002676 if (x->tfcpad)
2677 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002678 if (x->replay_esn)
2679 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002680 else
2681 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002682 if (x->security)
2683 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2684 x->security->ctx_len);
2685 if (x->coaddr)
2686 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002687 if (x->props.extra_flags)
2688 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002689
Herbert Xud26f3982007-11-13 21:47:08 -08002690 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002691 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002692
2693 return l;
2694}
2695
David S. Miller214e0052011-02-24 00:02:38 -05002696static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002697{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002698 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002699 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002700 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002701 struct nlmsghdr *nlh;
2702 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002703 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002704 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002705
2706 headlen = sizeof(*p);
2707 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002708 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002709 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002710 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002711 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002712 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002713
Thomas Graf7deb2262007-08-22 13:57:39 -07002714 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002715 if (skb == NULL)
2716 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002717
Eric W. Biederman15e47302012-09-07 20:12:54 +00002718 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002719 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002720 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002721 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002722
Thomas Graf7b67c852007-08-22 13:53:52 -07002723 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002724 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002725 struct nlattr *attr;
2726
Thomas Graf7b67c852007-08-22 13:53:52 -07002727 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002728 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2729 id->spi = x->id.spi;
2730 id->family = x->props.family;
2731 id->proto = x->id.proto;
2732
Thomas Grafc0144be2007-08-22 13:55:43 -07002733 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002734 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002735 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002736 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002737
2738 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002739 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002740 err = copy_to_user_state_extra(x, p, skb);
2741 if (err)
2742 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002743
Thomas Graf98250692007-08-22 12:47:26 -07002744 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002745
Michal Kubecek21ee5432014-06-03 10:26:06 +02002746 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002747
David S. Miller1d1e34d2012-06-27 21:57:03 -07002748out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002749 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002750 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002751}
2752
David S. Miller214e0052011-02-24 00:02:38 -05002753static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002754{
2755
2756 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002757 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002758 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002759 case XFRM_MSG_NEWAE:
2760 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002761 case XFRM_MSG_DELSA:
2762 case XFRM_MSG_UPDSA:
2763 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002764 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002765 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002766 return xfrm_notify_sa_flush(c);
2767 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002768 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2769 c->event);
2770 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002771 }
2772
2773 return 0;
2774
2775}
2776
Thomas Graf7deb2262007-08-22 13:57:39 -07002777static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2778 struct xfrm_policy *xp)
2779{
2780 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2781 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002782 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002783 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2784 + userpolicy_type_attrsize();
2785}
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002788 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002790 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 struct xfrm_user_acquire *ua;
2792 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002793 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002795 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2796 if (nlh == NULL)
2797 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Thomas Graf7b67c852007-08-22 13:53:52 -07002799 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 memcpy(&ua->id, &x->id, sizeof(ua->id));
2801 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2802 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002803 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 ua->aalgos = xt->aalgos;
2805 ua->ealgos = xt->ealgos;
2806 ua->calgos = xt->calgos;
2807 ua->seq = x->km.seq = seq;
2808
David S. Miller1d1e34d2012-06-27 21:57:03 -07002809 err = copy_to_user_tmpl(xp, skb);
2810 if (!err)
2811 err = copy_to_user_state_sec_ctx(x, skb);
2812 if (!err)
2813 err = copy_to_user_policy_type(xp->type, skb);
2814 if (!err)
2815 err = xfrm_mark_put(skb, &xp->mark);
2816 if (err) {
2817 nlmsg_cancel(skb, nlh);
2818 return err;
2819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Johannes Berg053c0952015-01-16 22:09:00 +01002821 nlmsg_end(skb, nlh);
2822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
2824
2825static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002826 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002828 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Thomas Graf7deb2262007-08-22 13:57:39 -07002831 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (skb == NULL)
2833 return -ENOMEM;
2834
Fan Du65e07362012-08-15 10:13:47 +08002835 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 BUG();
2837
Michal Kubecek21ee5432014-06-03 10:26:06 +02002838 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}
2840
2841/* User gives us xfrm_user_policy_info followed by an array of 0
2842 * or more templates.
2843 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002844static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 u8 *data, int len, int *dir)
2846{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002847 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2849 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2850 struct xfrm_policy *xp;
2851 int nr;
2852
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002853 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 case AF_INET:
2855 if (opt != IP_XFRM_POLICY) {
2856 *dir = -EOPNOTSUPP;
2857 return NULL;
2858 }
2859 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002860#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 case AF_INET6:
2862 if (opt != IPV6_XFRM_POLICY) {
2863 *dir = -EOPNOTSUPP;
2864 return NULL;
2865 }
2866 break;
2867#endif
2868 default:
2869 *dir = -EINVAL;
2870 return NULL;
2871 }
2872
2873 *dir = -EINVAL;
2874
2875 if (len < sizeof(*p) ||
2876 verify_newpolicy_info(p))
2877 return NULL;
2878
2879 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002880 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return NULL;
2882
Herbert Xua4f1bac2005-07-26 15:43:17 -07002883 if (p->dir > XFRM_POLICY_OUT)
2884 return NULL;
2885
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002886 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (xp == NULL) {
2888 *dir = -ENOBUFS;
2889 return NULL;
2890 }
2891
2892 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002893 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 copy_templates(xp, ut, nr);
2895
2896 *dir = p->dir;
2897
2898 return xp;
2899}
2900
Thomas Graf7deb2262007-08-22 13:57:39 -07002901static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2902{
2903 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2904 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2905 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002906 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002907 + userpolicy_type_attrsize();
2908}
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002911 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002914 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002915 struct nlmsghdr *nlh;
2916 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Eric W. Biederman15e47302012-09-07 20:12:54 +00002918 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002919 if (nlh == NULL)
2920 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Thomas Graf7b67c852007-08-22 13:53:52 -07002922 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002924 err = copy_to_user_tmpl(xp, skb);
2925 if (!err)
2926 err = copy_to_user_sec_ctx(xp, skb);
2927 if (!err)
2928 err = copy_to_user_policy_type(xp->type, skb);
2929 if (!err)
2930 err = xfrm_mark_put(skb, &xp->mark);
2931 if (err) {
2932 nlmsg_cancel(skb, nlh);
2933 return err;
2934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 upe->hard = !!hard;
2936
Johannes Berg053c0952015-01-16 22:09:00 +01002937 nlmsg_end(skb, nlh);
2938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
David S. Miller214e0052011-02-24 00:02:38 -05002941static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002943 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Thomas Graf7deb2262007-08-22 13:57:39 -07002946 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (skb == NULL)
2948 return -ENOMEM;
2949
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002950 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 BUG();
2952
Michal Kubecek21ee5432014-06-03 10:26:06 +02002953 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
2955
David S. Miller214e0052011-02-24 00:02:38 -05002956static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002957{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002958 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002959 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002960 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002961 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002962 struct nlmsghdr *nlh;
2963 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002964 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002965
2966 headlen = sizeof(*p);
2967 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002968 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002969 headlen = sizeof(*id);
2970 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002971 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002972 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002973 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002974
Thomas Graf7deb2262007-08-22 13:57:39 -07002975 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002976 if (skb == NULL)
2977 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002978
Eric W. Biederman15e47302012-09-07 20:12:54 +00002979 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002980 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002981 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002982 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002983
Thomas Graf7b67c852007-08-22 13:53:52 -07002984 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002985 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002986 struct nlattr *attr;
2987
Thomas Graf7b67c852007-08-22 13:53:52 -07002988 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002989 memset(id, 0, sizeof(*id));
2990 id->dir = dir;
2991 if (c->data.byid)
2992 id->index = xp->index;
2993 else
2994 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2995
Thomas Grafc0144be2007-08-22 13:55:43 -07002996 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002997 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002998 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002999 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07003000
3001 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07003002 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003003
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003004 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003005 err = copy_to_user_tmpl(xp, skb);
3006 if (!err)
3007 err = copy_to_user_policy_type(xp->type, skb);
3008 if (!err)
3009 err = xfrm_mark_put(skb, &xp->mark);
3010 if (err)
3011 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003012
Thomas Graf98250692007-08-22 12:47:26 -07003013 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003014
Michal Kubecek21ee5432014-06-03 10:26:06 +02003015 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003016
David S. Miller1d1e34d2012-06-27 21:57:03 -07003017out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003018 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003019 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003020}
3021
David S. Miller214e0052011-02-24 00:02:38 -05003022static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003023{
Alexey Dobriyan70678022008-11-25 17:50:36 -08003024 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003025 struct nlmsghdr *nlh;
3026 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003027 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003028
Thomas Graf7deb2262007-08-22 13:57:39 -07003029 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003030 if (skb == NULL)
3031 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003032
Eric W. Biederman15e47302012-09-07 20:12:54 +00003033 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003034 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003035 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003036 goto out_free_skb;
3037 err = copy_to_user_policy_type(c->data.type, skb);
3038 if (err)
3039 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003040
Thomas Graf98250692007-08-22 12:47:26 -07003041 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003042
Michal Kubecek21ee5432014-06-03 10:26:06 +02003043 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003044
David S. Miller1d1e34d2012-06-27 21:57:03 -07003045out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003046 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003047 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003048}
3049
David S. Miller214e0052011-02-24 00:02:38 -05003050static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003051{
3052
3053 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003054 case XFRM_MSG_NEWPOLICY:
3055 case XFRM_MSG_UPDPOLICY:
3056 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003057 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003058 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003059 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003060 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003061 return xfrm_exp_policy_notify(xp, dir, c);
3062 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003063 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3064 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003065 }
3066
3067 return 0;
3068
3069}
3070
Thomas Graf7deb2262007-08-22 13:57:39 -07003071static inline size_t xfrm_report_msgsize(void)
3072{
3073 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3074}
3075
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003076static int build_report(struct sk_buff *skb, u8 proto,
3077 struct xfrm_selector *sel, xfrm_address_t *addr)
3078{
3079 struct xfrm_user_report *ur;
3080 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003081
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003082 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3083 if (nlh == NULL)
3084 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003085
Thomas Graf7b67c852007-08-22 13:53:52 -07003086 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003087 ur->proto = proto;
3088 memcpy(&ur->sel, sel, sizeof(ur->sel));
3089
David S. Miller1d1e34d2012-06-27 21:57:03 -07003090 if (addr) {
3091 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3092 if (err) {
3093 nlmsg_cancel(skb, nlh);
3094 return err;
3095 }
3096 }
Johannes Berg053c0952015-01-16 22:09:00 +01003097 nlmsg_end(skb, nlh);
3098 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003099}
3100
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003101static int xfrm_send_report(struct net *net, u8 proto,
3102 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003103{
3104 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003105
Thomas Graf7deb2262007-08-22 13:57:39 -07003106 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003107 if (skb == NULL)
3108 return -ENOMEM;
3109
3110 if (build_report(skb, proto, sel, addr) < 0)
3111 BUG();
3112
Michal Kubecek21ee5432014-06-03 10:26:06 +02003113 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003114}
3115
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003116static inline size_t xfrm_mapping_msgsize(void)
3117{
3118 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3119}
3120
3121static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3122 xfrm_address_t *new_saddr, __be16 new_sport)
3123{
3124 struct xfrm_user_mapping *um;
3125 struct nlmsghdr *nlh;
3126
3127 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3128 if (nlh == NULL)
3129 return -EMSGSIZE;
3130
3131 um = nlmsg_data(nlh);
3132
3133 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3134 um->id.spi = x->id.spi;
3135 um->id.family = x->props.family;
3136 um->id.proto = x->id.proto;
3137 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3138 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3139 um->new_sport = new_sport;
3140 um->old_sport = x->encap->encap_sport;
3141 um->reqid = x->props.reqid;
3142
Johannes Berg053c0952015-01-16 22:09:00 +01003143 nlmsg_end(skb, nlh);
3144 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003145}
3146
3147static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3148 __be16 sport)
3149{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003150 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003151 struct sk_buff *skb;
3152
3153 if (x->id.proto != IPPROTO_ESP)
3154 return -EINVAL;
3155
3156 if (!x->encap)
3157 return -EINVAL;
3158
3159 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3160 if (skb == NULL)
3161 return -ENOMEM;
3162
3163 if (build_mapping(skb, x, ipaddr, sport) < 0)
3164 BUG();
3165
Michal Kubecek21ee5432014-06-03 10:26:06 +02003166 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003167}
3168
Horia Geanta0f245582014-02-12 16:20:06 +02003169static bool xfrm_is_alive(const struct km_event *c)
3170{
3171 return (bool)xfrm_acquire_is_on(c->net);
3172}
3173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174static struct xfrm_mgr netlink_mgr = {
3175 .id = "netlink",
3176 .notify = xfrm_send_state_notify,
3177 .acquire = xfrm_send_acquire,
3178 .compile_policy = xfrm_compile_policy,
3179 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003180 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003181 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003182 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003183 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184};
3185
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003186static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Patrick McHardybe336902006-03-20 22:40:54 -08003188 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003189 struct netlink_kernel_cfg cfg = {
3190 .groups = XFRMNLGRP_MAX,
3191 .input = xfrm_netlink_rcv,
3192 };
Patrick McHardybe336902006-03-20 22:40:54 -08003193
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003194 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003195 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003197 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003198 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return 0;
3200}
3201
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003202static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003203{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003204 struct net *net;
3205 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003206 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003207 synchronize_net();
3208 list_for_each_entry(net, net_exit_list, exit_list)
3209 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003210}
3211
3212static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003213 .init = xfrm_user_net_init,
3214 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003215};
3216
3217static int __init xfrm_user_init(void)
3218{
3219 int rv;
3220
3221 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3222
3223 rv = register_pernet_subsys(&xfrm_user_net_ops);
3224 if (rv < 0)
3225 return rv;
3226 rv = xfrm_register_km(&netlink_mgr);
3227 if (rv < 0)
3228 unregister_pernet_subsys(&xfrm_user_net_ops);
3229 return rv;
3230}
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232static void __exit xfrm_user_exit(void)
3233{
3234 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003235 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
3238module_init(xfrm_user_init);
3239module_exit(xfrm_user_exit);
3240MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003241MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003242