blob: 98420bf914cedf72f8a0ee707304a7e3e26e358c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Sowmini Varadhane33d4f12015-10-21 11:48:25 -040034#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Graf5424f322007-08-22 14:01:33 -070036static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Thomas Graf5424f322007-08-22 14:01:33 -070038 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct xfrm_algo *algp;
40
41 if (!rt)
42 return 0;
43
Thomas Graf5424f322007-08-22 14:01:33 -070044 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080045 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070046 return -EINVAL;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 switch (type) {
49 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53
54 default:
55 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
59 return 0;
60}
61
Martin Willi4447bb32009-11-25 00:29:52 +000062static int verify_auth_trunc(struct nlattr **attrs)
63{
64 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
65 struct xfrm_algo_auth *algp;
66
67 if (!rt)
68 return 0;
69
70 algp = nla_data(rt);
71 if (nla_len(rt) < xfrm_alg_auth_len(algp))
72 return -EINVAL;
73
74 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 return 0;
76}
77
Herbert Xu1a6509d2008-01-28 19:37:29 -080078static int verify_aead(struct nlattr **attrs)
79{
80 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
81 struct xfrm_algo_aead *algp;
82
83 if (!rt)
84 return 0;
85
86 algp = nla_data(rt);
87 if (nla_len(rt) < aead_len(algp))
88 return -EINVAL;
89
90 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
91 return 0;
92}
93
Thomas Graf5424f322007-08-22 14:01:33 -070094static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070095 xfrm_address_t **addrp)
96{
Thomas Graf5424f322007-08-22 14:01:33 -070097 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070098
Thomas Grafcf5cb792007-08-22 13:59:04 -070099 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700100 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700101}
Trent Jaegerdf718372005-12-13 23:12:27 -0800102
Thomas Graf5424f322007-08-22 14:01:33 -0700103static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800104{
Thomas Graf5424f322007-08-22 14:01:33 -0700105 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800106 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800107
108 if (!rt)
109 return 0;
110
Thomas Graf5424f322007-08-22 14:01:33 -0700111 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700112 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800113 return -EINVAL;
114
115 return 0;
116}
117
Steffen Klassertd8647b72011-03-08 00:10:27 +0000118static inline int verify_replay(struct xfrm_usersa_info *p,
119 struct nlattr **attrs)
120{
121 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000122 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000123
Mathias Krauseecd79182012-09-20 10:01:49 +0000124 if (p->flags & XFRM_STATE_ESN) {
125 if (!rt)
126 return -EINVAL;
127
128 rs = nla_data(rt);
129
130 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
131 return -EINVAL;
132
133 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
134 nla_len(rt) != sizeof(*rs))
135 return -EINVAL;
136 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000137
Steffen Klassertd8647b72011-03-08 00:10:27 +0000138 if (!rt)
139 return 0;
140
Fan Du01714102014-01-18 09:54:28 +0800141 /* As only ESP and AH support ESN feature. */
142 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000143 return -EINVAL;
144
Steffen Klassertd8647b72011-03-08 00:10:27 +0000145 if (p->replay_window != 0)
146 return -EINVAL;
147
148 return 0;
149}
Trent Jaegerdf718372005-12-13 23:12:27 -0800150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700152 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int err;
155
156 err = -EINVAL;
157 switch (p->family) {
158 case AF_INET:
159 break;
160
161 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000162#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
164#else
165 err = -EAFNOSUPPORT;
166 goto out;
167#endif
168
169 default:
170 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 err = -EINVAL;
174 switch (p->id.proto) {
175 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000176 if ((!attrs[XFRMA_ALG_AUTH] &&
177 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800178 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700179 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000180 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200181 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out;
183 break;
184
185 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800186 if (attrs[XFRMA_ALG_COMP])
187 goto out;
188 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000189 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800190 !attrs[XFRMA_ALG_CRYPT] &&
191 !attrs[XFRMA_ALG_AEAD])
192 goto out;
193 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000194 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800195 attrs[XFRMA_ALG_CRYPT]) &&
196 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000198 if (attrs[XFRMA_TFCPAD] &&
199 p->mode != XFRM_MODE_TUNNEL)
200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202
203 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700204 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800205 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700206 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000207 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000208 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200209 attrs[XFRMA_TFCPAD] ||
210 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto out;
212 break;
213
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000214#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700215 case IPPROTO_DSTOPTS:
216 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700217 if (attrs[XFRMA_ALG_COMP] ||
218 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000219 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800220 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700221 attrs[XFRMA_ALG_CRYPT] ||
222 attrs[XFRMA_ENCAP] ||
223 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000224 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700225 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700226 goto out;
227 break;
228#endif
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 default:
231 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Herbert Xu1a6509d2008-01-28 19:37:29 -0800234 if ((err = verify_aead(attrs)))
235 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000236 if ((err = verify_auth_trunc(attrs)))
237 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700238 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800245 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000246 if ((err = verify_replay(p, attrs)))
247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 err = -EINVAL;
250 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700251 case XFRM_MODE_TRANSPORT:
252 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700253 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700254 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256
257 default:
258 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 err = 0;
262
263out:
264 return err;
265}
266
267static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800268 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700269 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct xfrm_algo *p, *ualg;
272 struct xfrm_algo_desc *algo;
273
274 if (!rta)
275 return 0;
276
Thomas Graf5424f322007-08-22 14:01:33 -0700277 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 algo = get_byname(ualg->alg_name, 1);
280 if (!algo)
281 return -ENOSYS;
282 *props = algo->desc.sadb_alg_id;
283
Eric Dumazet0f99be02008-01-08 23:39:06 -0800284 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (!p)
286 return -ENOMEM;
287
Herbert Xu04ff1262006-08-13 08:50:00 +1000288 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *algpp = p;
290 return 0;
291}
292
Herbert Xu69b01372015-05-27 16:03:45 +0800293static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
294{
295 struct xfrm_algo *p, *ualg;
296 struct xfrm_algo_desc *algo;
297
298 if (!rta)
299 return 0;
300
301 ualg = nla_data(rta);
302
303 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
304 if (!algo)
305 return -ENOSYS;
306 x->props.ealgo = algo->desc.sadb_alg_id;
307
308 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
309 if (!p)
310 return -ENOMEM;
311
312 strcpy(p->alg_name, algo->name);
313 x->ealg = p;
314 x->geniv = algo->uinfo.encr.geniv;
315 return 0;
316}
317
Martin Willi4447bb32009-11-25 00:29:52 +0000318static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
319 struct nlattr *rta)
320{
321 struct xfrm_algo *ualg;
322 struct xfrm_algo_auth *p;
323 struct xfrm_algo_desc *algo;
324
325 if (!rta)
326 return 0;
327
328 ualg = nla_data(rta);
329
330 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
331 if (!algo)
332 return -ENOSYS;
333 *props = algo->desc.sadb_alg_id;
334
335 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
336 if (!p)
337 return -ENOMEM;
338
339 strcpy(p->alg_name, algo->name);
340 p->alg_key_len = ualg->alg_key_len;
341 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
342 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
343
344 *algpp = p;
345 return 0;
346}
347
348static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
349 struct nlattr *rta)
350{
351 struct xfrm_algo_auth *p, *ualg;
352 struct xfrm_algo_desc *algo;
353
354 if (!rta)
355 return 0;
356
357 ualg = nla_data(rta);
358
359 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
360 if (!algo)
361 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800362 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000363 return -EINVAL;
364 *props = algo->desc.sadb_alg_id;
365
366 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
367 if (!p)
368 return -ENOMEM;
369
370 strcpy(p->alg_name, algo->name);
371 if (!p->alg_trunc_len)
372 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
373
374 *algpp = p;
375 return 0;
376}
377
Herbert Xu69b01372015-05-27 16:03:45 +0800378static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800379{
380 struct xfrm_algo_aead *p, *ualg;
381 struct xfrm_algo_desc *algo;
382
383 if (!rta)
384 return 0;
385
386 ualg = nla_data(rta);
387
388 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
389 if (!algo)
390 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800391 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800392
393 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
394 if (!p)
395 return -ENOMEM;
396
397 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800398 x->aead = p;
399 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800400 return 0;
401}
402
Steffen Klasserte2b19122011-03-28 19:47:30 +0000403static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
404 struct nlattr *rp)
405{
406 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000407 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000408
409 if (!replay_esn || !rp)
410 return 0;
411
412 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000413 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000414
Mathias Krauseecd79182012-09-20 10:01:49 +0000415 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000416 return -EINVAL;
417
Andy Whitcroft64a54652017-03-22 07:29:31 +0000418 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
419 return -EINVAL;
420
Steffen Klasserte2b19122011-03-28 19:47:30 +0000421 return 0;
422}
423
Steffen Klassertd8647b72011-03-08 00:10:27 +0000424static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
425 struct xfrm_replay_state_esn **preplay_esn,
426 struct nlattr *rta)
427{
428 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000429 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000430
431 if (!rta)
432 return 0;
433
434 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000435 klen = xfrm_replay_state_esn_len(up);
436 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000437
Mathias Krauseecd79182012-09-20 10:01:49 +0000438 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000439 if (!p)
440 return -ENOMEM;
441
Mathias Krauseecd79182012-09-20 10:01:49 +0000442 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000443 if (!pp) {
444 kfree(p);
445 return -ENOMEM;
446 }
447
Mathias Krauseecd79182012-09-20 10:01:49 +0000448 memcpy(p, up, ulen);
449 memcpy(pp, up, ulen);
450
Steffen Klassertd8647b72011-03-08 00:10:27 +0000451 *replay_esn = p;
452 *preplay_esn = pp;
453
454 return 0;
455}
456
Joy Latten661697f2007-04-13 16:14:35 -0700457static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800458{
Trent Jaegerdf718372005-12-13 23:12:27 -0800459 int len = 0;
460
461 if (xfrm_ctx) {
462 len += sizeof(struct xfrm_user_sec_ctx);
463 len += xfrm_ctx->ctx_len;
464 }
465 return len;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
469{
470 memcpy(&x->id, &p->id, sizeof(x->id));
471 memcpy(&x->sel, &p->sel, sizeof(x->sel));
472 memcpy(&x->lft, &p->lft, sizeof(x->lft));
473 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800474 x->props.replay_window = min_t(unsigned int, p->replay_window,
475 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 x->props.reqid = p->reqid;
477 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700478 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700480
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700481 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700482 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800485/*
486 * someday when pfkey also has support, we could have the code
487 * somehow made shareable and move it to xfrm_state.c - JHS
488 *
489*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000490static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
491 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800492{
Thomas Graf5424f322007-08-22 14:01:33 -0700493 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000494 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700495 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
496 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
497 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800498
Steffen Klassertd8647b72011-03-08 00:10:27 +0000499 if (re) {
500 struct xfrm_replay_state_esn *replay_esn;
501 replay_esn = nla_data(re);
502 memcpy(x->replay_esn, replay_esn,
503 xfrm_replay_state_esn_len(replay_esn));
504 memcpy(x->preplay_esn, replay_esn,
505 xfrm_replay_state_esn_len(replay_esn));
506 }
507
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800508 if (rp) {
509 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700510 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800511 memcpy(&x->replay, replay, sizeof(*replay));
512 memcpy(&x->preplay, replay, sizeof(*replay));
513 }
514
515 if (lt) {
516 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700517 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800518 x->curlft.bytes = ltime->bytes;
519 x->curlft.packets = ltime->packets;
520 x->curlft.add_time = ltime->add_time;
521 x->curlft.use_time = ltime->use_time;
522 }
523
Thomas Grafcf5cb792007-08-22 13:59:04 -0700524 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700525 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800526
Thomas Grafcf5cb792007-08-22 13:59:04 -0700527 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700528 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800529}
530
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800531static struct xfrm_state *xfrm_state_construct(struct net *net,
532 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700533 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int *errp)
535{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800536 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int err = -ENOMEM;
538
539 if (!x)
540 goto error_no_put;
541
542 copy_from_user_state(x, p);
543
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100544 if (attrs[XFRMA_SA_EXTRA_FLAGS])
545 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
546
Herbert Xu69b01372015-05-27 16:03:45 +0800547 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800548 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000549 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
550 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000552 if (!x->props.aalgo) {
553 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
554 attrs[XFRMA_ALG_AUTH])))
555 goto error;
556 }
Herbert Xu69b01372015-05-27 16:03:45 +0800557 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto error;
559 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
560 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700561 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700563
564 if (attrs[XFRMA_ENCAP]) {
565 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
566 sizeof(*x->encap), GFP_KERNEL);
567 if (x->encap == NULL)
568 goto error;
569 }
570
Martin Willi35d28562010-12-08 04:37:49 +0000571 if (attrs[XFRMA_TFCPAD])
572 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
573
Thomas Graffd211502007-09-06 03:28:08 -0700574 if (attrs[XFRMA_COADDR]) {
575 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
576 sizeof(*x->coaddr), GFP_KERNEL);
577 if (x->coaddr == NULL)
578 goto error;
579 }
580
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000581 xfrm_mark_get(attrs, &x->mark);
582
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700583 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (err)
585 goto error;
586
Mathias Krause2f30ea52016-09-08 18:09:57 +0200587 if (attrs[XFRMA_SEC_CTX]) {
588 err = security_xfrm_state_alloc(x,
589 nla_data(attrs[XFRMA_SEC_CTX]));
590 if (err)
591 goto error;
592 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800593
Steffen Klassertd8647b72011-03-08 00:10:27 +0000594 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
595 attrs[XFRMA_REPLAY_ESN_VAL])))
596 goto error;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800599 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800600 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800601 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800602
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000603 if ((err = xfrm_init_replay(x)))
604 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800605
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000606 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000607 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 return x;
610
611error:
612 x->km.state = XFRM_STATE_DEAD;
613 xfrm_state_put(x);
614error_no_put:
615 *errp = err;
616 return NULL;
617}
618
Christoph Hellwig22e70052007-01-02 15:22:30 -0800619static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700620 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800622 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700623 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct xfrm_state *x;
625 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700626 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Thomas Graf35a7aa02007-08-22 14:00:40 -0700628 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (err)
630 return err;
631
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800632 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!x)
634 return err;
635
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700636 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
638 err = xfrm_state_add(x);
639 else
640 err = xfrm_state_update(x);
641
Tetsuo Handa2e710292014-04-22 21:48:30 +0900642 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (err < 0) {
645 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800646 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700650 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000651 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700652 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700653
654 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700655out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700656 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return err;
658}
659
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800660static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
661 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700662 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700663 int *errp)
664{
665 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000666 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700667 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000668 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700669
670 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
671 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000672 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700673 } else {
674 xfrm_address_t *saddr = NULL;
675
Thomas Graf35a7aa02007-08-22 14:00:40 -0700676 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700677 if (!saddr) {
678 err = -EINVAL;
679 goto out;
680 }
681
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800682 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000683 x = xfrm_state_lookup_byaddr(net, mark,
684 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800685 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700686 }
687
688 out:
689 if (!x && errp)
690 *errp = err;
691 return x;
692}
693
Christoph Hellwig22e70052007-01-02 15:22:30 -0800694static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700695 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800697 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700699 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700700 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700701 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800703 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700705 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
David S. Miller6f68dc32006-06-08 23:58:52 -0700707 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700708 goto out;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700711 err = -EPERM;
712 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700715 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600716
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700717 if (err < 0)
718 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700719
720 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000721 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700722 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700723 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700725out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900726 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700727 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
732{
Mathias Krausef778a632012-09-19 11:33:39 +0000733 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 memcpy(&p->id, &x->id, sizeof(p->id));
735 memcpy(&p->sel, &x->sel, sizeof(p->sel));
736 memcpy(&p->lft, &x->lft, sizeof(p->lft));
737 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400738 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
739 put_unaligned(x->stats.replay, &p->stats.replay);
740 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700741 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 p->mode = x->props.mode;
743 p->replay_window = x->props.replay_window;
744 p->reqid = x->props.reqid;
745 p->family = x->props.family;
746 p->flags = x->props.flags;
747 p->seq = x->km.seq;
748}
749
750struct xfrm_dump_info {
751 struct sk_buff *in_skb;
752 struct sk_buff *out_skb;
753 u32 nlmsg_seq;
754 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755};
756
Thomas Grafc0144be2007-08-22 13:55:43 -0700757static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
758{
Thomas Grafc0144be2007-08-22 13:55:43 -0700759 struct xfrm_user_sec_ctx *uctx;
760 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700761 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700762
763 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
764 if (attr == NULL)
765 return -EMSGSIZE;
766
767 uctx = nla_data(attr);
768 uctx->exttype = XFRMA_SEC_CTX;
769 uctx->len = ctx_size;
770 uctx->ctx_doi = s->ctx_doi;
771 uctx->ctx_alg = s->ctx_alg;
772 uctx->ctx_len = s->ctx_len;
773 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
774
775 return 0;
776}
777
Martin Willi4447bb32009-11-25 00:29:52 +0000778static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
779{
780 struct xfrm_algo *algo;
781 struct nlattr *nla;
782
783 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
784 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
785 if (!nla)
786 return -EMSGSIZE;
787
788 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000789 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000790 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
791 algo->alg_key_len = auth->alg_key_len;
792
793 return 0;
794}
795
Herbert Xu68325d32007-10-09 13:30:57 -0700796/* Don't change this without updating xfrm_sa_len! */
797static int copy_to_user_state_extra(struct xfrm_state *x,
798 struct xfrm_usersa_info *p,
799 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700801 int ret = 0;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 copy_to_user_state(x, p);
804
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100805 if (x->props.extra_flags) {
806 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
807 x->props.extra_flags);
808 if (ret)
809 goto out;
810 }
811
David S. Miller1d1e34d2012-06-27 21:57:03 -0700812 if (x->coaddr) {
813 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
814 if (ret)
815 goto out;
816 }
817 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200818 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
819 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700820 if (ret)
821 goto out;
822 }
823 if (x->aead) {
824 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
825 if (ret)
826 goto out;
827 }
828 if (x->aalg) {
829 ret = copy_to_user_auth(x->aalg, skb);
830 if (!ret)
831 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
832 xfrm_alg_auth_len(x->aalg), x->aalg);
833 if (ret)
834 goto out;
835 }
836 if (x->ealg) {
837 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
838 if (ret)
839 goto out;
840 }
841 if (x->calg) {
842 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
843 if (ret)
844 goto out;
845 }
846 if (x->encap) {
847 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
848 if (ret)
849 goto out;
850 }
851 if (x->tfcpad) {
852 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
853 if (ret)
854 goto out;
855 }
856 ret = xfrm_mark_put(skb, &x->mark);
857 if (ret)
858 goto out;
dingzhif293a5e2014-10-30 09:39:36 +0100859 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700860 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
861 xfrm_replay_state_esn_len(x->replay_esn),
862 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100863 else
864 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
865 &x->replay);
866 if (ret)
867 goto out;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700868 if (x->security)
869 ret = copy_sec_ctx(x->security, skb);
870out:
871 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700872}
873
874static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
875{
876 struct xfrm_dump_info *sp = ptr;
877 struct sk_buff *in_skb = sp->in_skb;
878 struct sk_buff *skb = sp->out_skb;
879 struct xfrm_usersa_info *p;
880 struct nlmsghdr *nlh;
881 int err;
882
Eric W. Biederman15e47302012-09-07 20:12:54 +0000883 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700884 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
885 if (nlh == NULL)
886 return -EMSGSIZE;
887
888 p = nlmsg_data(nlh);
889
890 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700891 if (err) {
892 nlmsg_cancel(skb, nlh);
893 return err;
894 }
Thomas Graf98250692007-08-22 12:47:26 -0700895 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Timo Teras4c563f72008-02-28 21:31:08 -0800899static int xfrm_dump_sa_done(struct netlink_callback *cb)
900{
901 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800902 struct sock *sk = cb->skb->sk;
903 struct net *net = sock_net(sk);
904
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200905 if (cb->args[0])
906 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800907 return 0;
908}
909
Nicolas Dichteld3623092014-02-14 15:30:36 +0100910static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
912{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800913 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800914 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct xfrm_dump_info info;
916
Timo Teras4c563f72008-02-28 21:31:08 -0800917 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
918 sizeof(cb->args) - sizeof(cb->args[0]));
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 info.in_skb = cb->skb;
921 info.out_skb = skb;
922 info.nlmsg_seq = cb->nlh->nlmsg_seq;
923 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800924
925 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100926 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100927 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100928 u8 proto = 0;
929 int err;
930
Nicolas Dichteld3623092014-02-14 15:30:36 +0100931 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
932 xfrma_policy);
933 if (err < 0)
934 return err;
935
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100936 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +0200937 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
938 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +0100939 if (filter == NULL)
940 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100941 }
942
943 if (attrs[XFRMA_PROTO])
944 proto = nla_get_u8(attrs[XFRMA_PROTO]);
945
946 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200947 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -0800948 }
949
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800950 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 return skb->len;
953}
954
955static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
956 struct xfrm_state *x, u32 seq)
957{
958 struct xfrm_dump_info info;
959 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000960 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Thomas Graf7deb2262007-08-22 13:57:39 -0700962 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!skb)
964 return ERR_PTR(-ENOMEM);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 info.in_skb = in_skb;
967 info.out_skb = skb;
968 info.nlmsg_seq = seq;
969 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Mathias Krause864745d2012-09-13 11:41:26 +0000971 err = dump_one_state(x, 0, &info);
972 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000974 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977 return skb;
978}
979
Michal Kubecek21ee5432014-06-03 10:26:06 +0200980/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
981 * Must be called with RCU read lock.
982 */
983static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
984 u32 pid, unsigned int group)
985{
986 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
987
988 if (nlsk)
989 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
990 else
991 return -1;
992}
993
Thomas Graf7deb2262007-08-22 13:57:39 -0700994static inline size_t xfrm_spdinfo_msgsize(void)
995{
996 return NLMSG_ALIGN(4)
997 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200998 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
999 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1000 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -07001001}
1002
Alexey Dobriyane0710412010-01-23 13:37:10 +00001003static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001004 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001005{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001006 struct xfrmk_spdinfo si;
1007 struct xfrmu_spdinfo spc;
1008 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001009 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001010 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001011 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001012 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001013 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001014
Eric W. Biederman15e47302012-09-07 20:12:54 +00001015 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001016 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001017 return -EMSGSIZE;
1018
1019 f = nlmsg_data(nlh);
1020 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001021 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001022 spc.incnt = si.incnt;
1023 spc.outcnt = si.outcnt;
1024 spc.fwdcnt = si.fwdcnt;
1025 spc.inscnt = si.inscnt;
1026 spc.outscnt = si.outscnt;
1027 spc.fwdscnt = si.fwdscnt;
1028 sph.spdhcnt = si.spdhcnt;
1029 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001030
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001031 do {
1032 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1033
1034 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1035 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1036 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1037 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1038 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1039
David S. Miller1d1e34d2012-06-27 21:57:03 -07001040 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1041 if (!err)
1042 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001043 if (!err)
1044 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1045 if (!err)
1046 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001047 if (err) {
1048 nlmsg_cancel(skb, nlh);
1049 return err;
1050 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001051
Johannes Berg053c0952015-01-16 22:09:00 +01001052 nlmsg_end(skb, nlh);
1053 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001054}
1055
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001056static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1057 struct nlattr **attrs)
1058{
1059 struct net *net = sock_net(skb->sk);
1060 struct xfrmu_spdhthresh *thresh4 = NULL;
1061 struct xfrmu_spdhthresh *thresh6 = NULL;
1062
1063 /* selector prefixlen thresholds to hash policies */
1064 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1065 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1066
1067 if (nla_len(rta) < sizeof(*thresh4))
1068 return -EINVAL;
1069 thresh4 = nla_data(rta);
1070 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1071 return -EINVAL;
1072 }
1073 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1074 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1075
1076 if (nla_len(rta) < sizeof(*thresh6))
1077 return -EINVAL;
1078 thresh6 = nla_data(rta);
1079 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1080 return -EINVAL;
1081 }
1082
1083 if (thresh4 || thresh6) {
1084 write_seqlock(&net->xfrm.policy_hthresh.lock);
1085 if (thresh4) {
1086 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1087 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1088 }
1089 if (thresh6) {
1090 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1091 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1092 }
1093 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1094
1095 xfrm_policy_hash_rebuild(net);
1096 }
1097
1098 return 0;
1099}
1100
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001101static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001102 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001103{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001104 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001105 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001106 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001107 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001108 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001109
Thomas Graf7deb2262007-08-22 13:57:39 -07001110 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001111 if (r_skb == NULL)
1112 return -ENOMEM;
1113
Eric W. Biederman15e47302012-09-07 20:12:54 +00001114 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001115 BUG();
1116
Eric W. Biederman15e47302012-09-07 20:12:54 +00001117 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001118}
1119
Thomas Graf7deb2262007-08-22 13:57:39 -07001120static inline size_t xfrm_sadinfo_msgsize(void)
1121{
1122 return NLMSG_ALIGN(4)
1123 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1124 + nla_total_size(4); /* XFRMA_SAD_CNT */
1125}
1126
Alexey Dobriyane0710412010-01-23 13:37:10 +00001127static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001128 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001129{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001130 struct xfrmk_sadinfo si;
1131 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001132 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001133 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001134 u32 *f;
1135
Eric W. Biederman15e47302012-09-07 20:12:54 +00001136 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001137 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001138 return -EMSGSIZE;
1139
1140 f = nlmsg_data(nlh);
1141 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001142 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001143
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001144 sh.sadhmcnt = si.sadhmcnt;
1145 sh.sadhcnt = si.sadhcnt;
1146
David S. Miller1d1e34d2012-06-27 21:57:03 -07001147 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1148 if (!err)
1149 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1150 if (err) {
1151 nlmsg_cancel(skb, nlh);
1152 return err;
1153 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001154
Johannes Berg053c0952015-01-16 22:09:00 +01001155 nlmsg_end(skb, nlh);
1156 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001157}
1158
1159static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001160 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001161{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001162 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001163 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001164 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001165 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001166 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001167
Thomas Graf7deb2262007-08-22 13:57:39 -07001168 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001169 if (r_skb == NULL)
1170 return -ENOMEM;
1171
Eric W. Biederman15e47302012-09-07 20:12:54 +00001172 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001173 BUG();
1174
Eric W. Biederman15e47302012-09-07 20:12:54 +00001175 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001176}
1177
Christoph Hellwig22e70052007-01-02 15:22:30 -08001178static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001179 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001181 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001182 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct xfrm_state *x;
1184 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001185 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001187 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (x == NULL)
1189 goto out_noput;
1190
1191 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1192 if (IS_ERR(resp_skb)) {
1193 err = PTR_ERR(resp_skb);
1194 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001195 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 xfrm_state_put(x);
1198out_noput:
1199 return err;
1200}
1201
Christoph Hellwig22e70052007-01-02 15:22:30 -08001202static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001203 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001205 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 struct xfrm_state *x;
1207 struct xfrm_userspi_info *p;
1208 struct sk_buff *resp_skb;
1209 xfrm_address_t *daddr;
1210 int family;
1211 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001212 u32 mark;
1213 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Thomas Graf7b67c852007-08-22 13:53:52 -07001215 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001216 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (err)
1218 goto out_noput;
1219
1220 family = p->info.family;
1221 daddr = &p->info.id.daddr;
1222
1223 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001224
1225 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001227 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001228 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 xfrm_state_put(x);
1230 x = NULL;
1231 }
1232 }
1233
1234 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001235 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 p->info.id.proto, daddr,
1237 &p->info.saddr, 1,
1238 family);
1239 err = -ENOENT;
1240 if (x == NULL)
1241 goto out_noput;
1242
Herbert Xu658b2192007-10-09 13:29:52 -07001243 err = xfrm_alloc_spi(x, p->min, p->max);
1244 if (err)
1245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Herbert Xu658b2192007-10-09 13:29:52 -07001247 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (IS_ERR(resp_skb)) {
1249 err = PTR_ERR(resp_skb);
1250 goto out;
1251 }
1252
Eric W. Biederman15e47302012-09-07 20:12:54 +00001253 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255out:
1256 xfrm_state_put(x);
1257out_noput:
1258 return err;
1259}
1260
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001261static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 switch (dir) {
1264 case XFRM_POLICY_IN:
1265 case XFRM_POLICY_OUT:
1266 case XFRM_POLICY_FWD:
1267 break;
1268
1269 default:
1270 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 return 0;
1274}
1275
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001276static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001277{
1278 switch (type) {
1279 case XFRM_POLICY_TYPE_MAIN:
1280#ifdef CONFIG_XFRM_SUB_POLICY
1281 case XFRM_POLICY_TYPE_SUB:
1282#endif
1283 break;
1284
1285 default:
1286 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001287 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001288
1289 return 0;
1290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1293{
Fan Due682adf2013-11-07 17:47:48 +08001294 int ret;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 switch (p->share) {
1297 case XFRM_SHARE_ANY:
1298 case XFRM_SHARE_SESSION:
1299 case XFRM_SHARE_USER:
1300 case XFRM_SHARE_UNIQUE:
1301 break;
1302
1303 default:
1304 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 switch (p->action) {
1308 case XFRM_POLICY_ALLOW:
1309 case XFRM_POLICY_BLOCK:
1310 break;
1311
1312 default:
1313 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 switch (p->sel.family) {
1317 case AF_INET:
1318 break;
1319
1320 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001321#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 break;
1323#else
1324 return -EAFNOSUPPORT;
1325#endif
1326
1327 default:
1328 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Fan Due682adf2013-11-07 17:47:48 +08001331 ret = verify_policy_dir(p->dir);
1332 if (ret)
1333 return ret;
1334 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1335 return -EINVAL;
1336
1337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Thomas Graf5424f322007-08-22 14:01:33 -07001340static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001341{
Thomas Graf5424f322007-08-22 14:01:33 -07001342 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001343 struct xfrm_user_sec_ctx *uctx;
1344
1345 if (!rt)
1346 return 0;
1347
Thomas Graf5424f322007-08-22 14:01:33 -07001348 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001349 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001350}
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1353 int nr)
1354{
1355 int i;
1356
1357 xp->xfrm_nr = nr;
1358 for (i = 0; i < nr; i++, ut++) {
1359 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1360
1361 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1362 memcpy(&t->saddr, &ut->saddr,
1363 sizeof(xfrm_address_t));
1364 t->reqid = ut->reqid;
1365 t->mode = ut->mode;
1366 t->share = ut->share;
1367 t->optional = ut->optional;
1368 t->aalgos = ut->aalgos;
1369 t->ealgos = ut->ealgos;
1370 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001371 /* If all masks are ~0, then we allow all algorithms. */
1372 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001373 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375}
1376
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001377static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1378{
1379 int i;
1380
1381 if (nr > XFRM_MAX_DEPTH)
1382 return -EINVAL;
1383
1384 for (i = 0; i < nr; i++) {
1385 /* We never validated the ut->family value, so many
1386 * applications simply leave it at zero. The check was
1387 * never made and ut->family was ignored because all
1388 * templates could be assumed to have the same family as
1389 * the policy itself. Now that we will have ipv4-in-ipv6
1390 * and ipv6-in-ipv4 tunnels, this is no longer true.
1391 */
1392 if (!ut[i].family)
1393 ut[i].family = family;
1394
1395 switch (ut[i].family) {
1396 case AF_INET:
1397 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001398#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001399 case AF_INET6:
1400 break;
1401#endif
1402 default:
1403 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001404 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001405 }
1406
1407 return 0;
1408}
1409
Thomas Graf5424f322007-08-22 14:01:33 -07001410static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Thomas Graf5424f322007-08-22 14:01:33 -07001412 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (!rt) {
1415 pol->xfrm_nr = 0;
1416 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001417 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1418 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001419 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001421 err = validate_tmpl(nr, utmpl, pol->family);
1422 if (err)
1423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Thomas Graf5424f322007-08-22 14:01:33 -07001425 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427 return 0;
1428}
1429
Thomas Graf5424f322007-08-22 14:01:33 -07001430static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001431{
Thomas Graf5424f322007-08-22 14:01:33 -07001432 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001433 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001434 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001435 int err;
1436
1437 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001438 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001439 type = upt->type;
1440 }
1441
1442 err = verify_policy_type(type);
1443 if (err)
1444 return err;
1445
1446 *tp = type;
1447 return 0;
1448}
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1451{
1452 xp->priority = p->priority;
1453 xp->index = p->index;
1454 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1455 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1456 xp->action = p->action;
1457 xp->flags = p->flags;
1458 xp->family = p->sel.family;
1459 /* XXX xp->share = p->share; */
1460}
1461
1462static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1463{
Mathias Krause7b789832012-09-19 11:33:40 +00001464 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1466 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1467 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1468 p->priority = xp->priority;
1469 p->index = xp->index;
1470 p->sel.family = xp->family;
1471 p->dir = dir;
1472 p->action = xp->action;
1473 p->flags = xp->flags;
1474 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1475}
1476
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001477static 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 -07001478{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001479 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 int err;
1481
1482 if (!xp) {
1483 *errp = -ENOMEM;
1484 return NULL;
1485 }
1486
1487 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001488
Thomas Graf35a7aa02007-08-22 14:00:40 -07001489 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001490 if (err)
1491 goto error;
1492
Thomas Graf35a7aa02007-08-22 14:00:40 -07001493 if (!(err = copy_from_user_tmpl(xp, attrs)))
1494 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001495 if (err)
1496 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001498 xfrm_mark_get(attrs, &xp->mark);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001501 error:
1502 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001503 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001504 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001505 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
Christoph Hellwig22e70052007-01-02 15:22:30 -08001508static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001509 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001511 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001512 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001514 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int err;
1516 int excl;
1517
1518 err = verify_newpolicy_info(p);
1519 if (err)
1520 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001521 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001522 if (err)
1523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001525 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (!xp)
1527 return err;
1528
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001529 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001530 * Aha! this is anti-netlink really i.e more pfkey derived
1531 * in netlink excl is a flag and you wouldnt need
1532 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1534 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001535 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001538 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 kfree(xp);
1540 return err;
1541 }
1542
Herbert Xuf60f6b82005-06-18 22:44:37 -07001543 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001544 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001545 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001546 km_policy_notify(xp, p->dir, &c);
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 xfrm_pol_put(xp);
1549
1550 return 0;
1551}
1552
1553static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1554{
1555 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1556 int i;
1557
1558 if (xp->xfrm_nr == 0)
1559 return 0;
1560
1561 for (i = 0; i < xp->xfrm_nr; i++) {
1562 struct xfrm_user_tmpl *up = &vec[i];
1563 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1564
Mathias Krause1f868402012-09-19 11:33:41 +00001565 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001567 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1569 up->reqid = kp->reqid;
1570 up->mode = kp->mode;
1571 up->share = kp->share;
1572 up->optional = kp->optional;
1573 up->aalgos = kp->aalgos;
1574 up->ealgos = kp->ealgos;
1575 up->calgos = kp->calgos;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Thomas Grafc0144be2007-08-22 13:55:43 -07001578 return nla_put(skb, XFRMA_TMPL,
1579 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001580}
1581
Serge Hallyn0d681622006-07-24 23:30:44 -07001582static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1583{
1584 if (x->security) {
1585 return copy_sec_ctx(x->security, skb);
1586 }
1587 return 0;
1588}
1589
1590static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1591{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001592 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001593 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001594 return 0;
1595}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001596static inline size_t userpolicy_type_attrsize(void)
1597{
1598#ifdef CONFIG_XFRM_SUB_POLICY
1599 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1600#else
1601 return 0;
1602#endif
1603}
Serge Hallyn0d681622006-07-24 23:30:44 -07001604
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001605#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001606static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001607{
Thomas Grafc0144be2007-08-22 13:55:43 -07001608 struct xfrm_userpolicy_type upt = {
1609 .type = type,
1610 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001611
Thomas Grafc0144be2007-08-22 13:55:43 -07001612 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001613}
1614
1615#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001616static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001617{
1618 return 0;
1619}
1620#endif
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1623{
1624 struct xfrm_dump_info *sp = ptr;
1625 struct xfrm_userpolicy_info *p;
1626 struct sk_buff *in_skb = sp->in_skb;
1627 struct sk_buff *skb = sp->out_skb;
1628 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001629 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Eric W. Biederman15e47302012-09-07 20:12:54 +00001631 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001632 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1633 if (nlh == NULL)
1634 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Thomas Graf7b67c852007-08-22 13:53:52 -07001636 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001638 err = copy_to_user_tmpl(xp, skb);
1639 if (!err)
1640 err = copy_to_user_sec_ctx(xp, skb);
1641 if (!err)
1642 err = copy_to_user_policy_type(xp->type, skb);
1643 if (!err)
1644 err = xfrm_mark_put(skb, &xp->mark);
1645 if (err) {
1646 nlmsg_cancel(skb, nlh);
1647 return err;
1648 }
Thomas Graf98250692007-08-22 12:47:26 -07001649 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
Timo Teras4c563f72008-02-28 21:31:08 -08001653static int xfrm_dump_policy_done(struct netlink_callback *cb)
1654{
1655 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001656 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001657
Fan Du283bc9f2013-11-07 17:47:50 +08001658 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001659 return 0;
1660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1663{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001664 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001665 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 struct xfrm_dump_info info;
1667
Timo Teras4c563f72008-02-28 21:31:08 -08001668 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1669 sizeof(cb->args) - sizeof(cb->args[0]));
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 info.in_skb = cb->skb;
1672 info.out_skb = skb;
1673 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1674 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001675
1676 if (!cb->args[0]) {
1677 cb->args[0] = 1;
1678 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1679 }
1680
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001681 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 return skb->len;
1684}
1685
1686static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1687 struct xfrm_policy *xp,
1688 int dir, u32 seq)
1689{
1690 struct xfrm_dump_info info;
1691 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001692 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Thomas Graf7deb2262007-08-22 13:57:39 -07001694 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (!skb)
1696 return ERR_PTR(-ENOMEM);
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 info.in_skb = in_skb;
1699 info.out_skb = skb;
1700 info.nlmsg_seq = seq;
1701 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Mathias Krausec2546372012-09-14 09:58:32 +00001703 err = dump_one_policy(xp, dir, 0, &info);
1704 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001706 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708
1709 return skb;
1710}
1711
Christoph Hellwig22e70052007-01-02 15:22:30 -08001712static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001713 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001715 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct xfrm_policy *xp;
1717 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001718 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001720 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001722 struct xfrm_mark m;
1723 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Thomas Graf7b67c852007-08-22 13:53:52 -07001725 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1727
Thomas Graf35a7aa02007-08-22 14:00:40 -07001728 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001729 if (err)
1730 return err;
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 err = verify_policy_dir(p->dir);
1733 if (err)
1734 return err;
1735
1736 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001737 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001738 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001739 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001740 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001741
Thomas Graf35a7aa02007-08-22 14:00:40 -07001742 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001743 if (err)
1744 return err;
1745
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001746 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001747 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001748 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001749
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001750 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001751 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001752 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001753 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001754 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001755 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001756 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (xp == NULL)
1759 return -ENOENT;
1760
1761 if (!delete) {
1762 struct sk_buff *resp_skb;
1763
1764 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1765 if (IS_ERR(resp_skb)) {
1766 err = PTR_ERR(resp_skb);
1767 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001768 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001769 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001771 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001772 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001773
1774 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001775 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001776
Herbert Xue7443892005-06-18 22:44:18 -07001777 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001778 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001779 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001780 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001781 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001784out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001785 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001786 if (delete && err == 0)
1787 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return err;
1789}
1790
Christoph Hellwig22e70052007-01-02 15:22:30 -08001791static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001792 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001794 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001795 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001796 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001797 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Tetsuo Handa2e710292014-04-22 21:48:30 +09001799 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001800 if (err) {
1801 if (err == -ESRCH) /* empty table */
1802 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001803 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001804 }
Herbert Xubf088672005-06-18 22:44:00 -07001805 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001806 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001807 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001808 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001809 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001810 km_state_notify(NULL, &c);
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
1813}
1814
Steffen Klassertd8647b72011-03-08 00:10:27 +00001815static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001816{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001817 size_t replay_size = x->replay_esn ?
1818 xfrm_replay_state_esn_len(x->replay_esn) :
1819 sizeof(struct xfrm_replay_state);
1820
Thomas Graf7deb2262007-08-22 13:57:39 -07001821 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001822 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001823 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001824 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001825 + nla_total_size(4) /* XFRM_AE_RTHR */
1826 + nla_total_size(4); /* XFRM_AE_ETHR */
1827}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001828
David S. Miller214e0052011-02-24 00:02:38 -05001829static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001830{
1831 struct xfrm_aevent_id *id;
1832 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001833 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001834
Eric W. Biederman15e47302012-09-07 20:12:54 +00001835 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001836 if (nlh == NULL)
1837 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001838
Thomas Graf7b67c852007-08-22 13:53:52 -07001839 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001840 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001841 id->sa_id.spi = x->id.spi;
1842 id->sa_id.family = x->props.family;
1843 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001844 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001845 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001846 id->flags = c->data.aevent;
1847
David S. Millerd0fde792012-03-29 04:02:26 -04001848 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001849 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1850 xfrm_replay_state_esn_len(x->replay_esn),
1851 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001852 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001853 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1854 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001855 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001856 if (err)
1857 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001858 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1859 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001860 if (err)
1861 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001862
David S. Miller1d1e34d2012-06-27 21:57:03 -07001863 if (id->flags & XFRM_AE_RTHR) {
1864 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1865 if (err)
1866 goto out_cancel;
1867 }
1868 if (id->flags & XFRM_AE_ETHR) {
1869 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1870 x->replay_maxage * 10 / HZ);
1871 if (err)
1872 goto out_cancel;
1873 }
1874 err = xfrm_mark_put(skb, &x->mark);
1875 if (err)
1876 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001877
Johannes Berg053c0952015-01-16 22:09:00 +01001878 nlmsg_end(skb, nlh);
1879 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001880
David S. Miller1d1e34d2012-06-27 21:57:03 -07001881out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001882 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001883 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001884}
1885
Christoph Hellwig22e70052007-01-02 15:22:30 -08001886static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001887 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001888{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001889 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001890 struct xfrm_state *x;
1891 struct sk_buff *r_skb;
1892 int err;
1893 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001894 u32 mark;
1895 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001896 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001897 struct xfrm_usersa_id *id = &p->sa_id;
1898
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001899 mark = xfrm_mark_get(attrs, &m);
1900
1901 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001902 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001903 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001904
1905 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1906 if (r_skb == NULL) {
1907 xfrm_state_put(x);
1908 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001909 }
1910
1911 /*
1912 * XXX: is this lock really needed - none of the other
1913 * gets lock (the concern is things getting updated
1914 * while we are still reading) - jhs
1915 */
1916 spin_lock_bh(&x->lock);
1917 c.data.aevent = p->flags;
1918 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001919 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001920
1921 if (build_aevent(r_skb, x, &c) < 0)
1922 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001923 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001924 spin_unlock_bh(&x->lock);
1925 xfrm_state_put(x);
1926 return err;
1927}
1928
Christoph Hellwig22e70052007-01-02 15:22:30 -08001929static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001930 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001931{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001932 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001933 struct xfrm_state *x;
1934 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001935 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001936 u32 mark = 0;
1937 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001938 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001939 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001940 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001941 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02001942 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1943 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001944
Michael Rossberg4e077232015-09-29 11:25:08 +02001945 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001946 return err;
1947
1948 /* pedantic mode - thou shalt sayeth replaceth */
1949 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1950 return err;
1951
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001952 mark = xfrm_mark_get(attrs, &m);
1953
1954 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 -08001955 if (x == NULL)
1956 return -ESRCH;
1957
1958 if (x->km.state != XFRM_STATE_VALID)
1959 goto out;
1960
Steffen Klassert4479ff72013-09-09 09:39:01 +02001961 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001962 if (err)
1963 goto out;
1964
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001965 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001966 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001967 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001968
1969 c.event = nlh->nlmsg_type;
1970 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001971 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001972 c.data.aevent = XFRM_AE_CU;
1973 km_state_notify(x, &c);
1974 err = 0;
1975out:
1976 xfrm_state_put(x);
1977 return err;
1978}
1979
Christoph Hellwig22e70052007-01-02 15:22:30 -08001980static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001981 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001983 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001984 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001985 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001986 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001987
Thomas Graf35a7aa02007-08-22 14:00:40 -07001988 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001989 if (err)
1990 return err;
1991
Tetsuo Handa2e710292014-04-22 21:48:30 +09001992 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001993 if (err) {
1994 if (err == -ESRCH) /* empty table */
1995 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001996 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001997 }
1998
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001999 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002000 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002001 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002002 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002003 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002004 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return 0;
2006}
2007
Christoph Hellwig22e70052007-01-02 15:22:30 -08002008static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002009 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002010{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002011 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002012 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002013 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002014 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002015 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002016 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002017 struct xfrm_mark m;
2018 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002019
Thomas Graf35a7aa02007-08-22 14:00:40 -07002020 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002021 if (err)
2022 return err;
2023
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002024 err = verify_policy_dir(p->dir);
2025 if (err)
2026 return err;
2027
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002028 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002029 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002030 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002031 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002032 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002033
Thomas Graf35a7aa02007-08-22 14:00:40 -07002034 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002035 if (err)
2036 return err;
2037
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002038 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002039 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002040 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002041
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002042 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002043 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002044 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002045 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002046 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002047 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002048 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002049 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002050 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002051 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002052
Timo Teräsea2dea92010-03-31 00:17:05 +00002053 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002054 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002055
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002056 err = 0;
2057 if (up->hard) {
2058 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002059 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002060 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002061 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002062
2063out:
2064 xfrm_pol_put(xp);
2065 return err;
2066}
2067
Christoph Hellwig22e70052007-01-02 15:22:30 -08002068static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002069 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002070{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002071 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002072 struct xfrm_state *x;
2073 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002074 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002075 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002076 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002077 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002078
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002079 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 -08002080
David S. Miller3a765aa2007-02-26 14:52:21 -08002081 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002082 if (x == NULL)
2083 return err;
2084
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002085 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002086 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002087 if (x->km.state != XFRM_STATE_VALID)
2088 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002089 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002090
Joy Latten161a09e2006-11-27 13:11:54 -06002091 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002092 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002093 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002094 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002095 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002096out:
2097 spin_unlock_bh(&x->lock);
2098 xfrm_state_put(x);
2099 return err;
2100}
2101
Christoph Hellwig22e70052007-01-02 15:22:30 -08002102static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002103 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002104{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002105 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002106 struct xfrm_policy *xp;
2107 struct xfrm_user_tmpl *ut;
2108 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002109 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002110 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002111
Thomas Graf7b67c852007-08-22 13:53:52 -07002112 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002113 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002114 int err = -ENOMEM;
2115
2116 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002117 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002118
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002119 xfrm_mark_get(attrs, &mark);
2120
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002121 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002122 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002123 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002124
2125 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002126 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002127 if (!xp)
2128 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002129
2130 memcpy(&x->id, &ua->id, sizeof(ua->id));
2131 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2132 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002133 xp->mark.m = x->mark.m = mark.m;
2134 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002135 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002136 /* extract the templates and for each call km_key */
2137 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2138 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2139 memcpy(&x->id, &t->id, sizeof(x->id));
2140 x->props.mode = t->mode;
2141 x->props.reqid = t->reqid;
2142 x->props.family = ut->family;
2143 t->aalgos = ua->aalgos;
2144 t->ealgos = ua->ealgos;
2145 t->calgos = ua->calgos;
2146 err = km_query(x, t, xp);
2147
2148 }
2149
2150 kfree(x);
2151 kfree(xp);
2152
2153 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002154
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002155free_state:
2156 kfree(x);
2157nomem:
2158 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002159}
2160
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002161#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002162static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002163 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002164 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002165{
Thomas Graf5424f322007-08-22 14:01:33 -07002166 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002167 struct xfrm_user_migrate *um;
2168 int i, num_migrate;
2169
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002170 if (k != NULL) {
2171 struct xfrm_user_kmaddress *uk;
2172
2173 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2174 memcpy(&k->local, &uk->local, sizeof(k->local));
2175 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2176 k->family = uk->family;
2177 k->reserved = uk->reserved;
2178 }
2179
Thomas Graf5424f322007-08-22 14:01:33 -07002180 um = nla_data(rt);
2181 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002182
2183 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2184 return -EINVAL;
2185
2186 for (i = 0; i < num_migrate; i++, um++, ma++) {
2187 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2188 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2189 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2190 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2191
2192 ma->proto = um->proto;
2193 ma->mode = um->mode;
2194 ma->reqid = um->reqid;
2195
2196 ma->old_family = um->old_family;
2197 ma->new_family = um->new_family;
2198 }
2199
2200 *num = i;
2201 return 0;
2202}
2203
2204static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002205 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002206{
Thomas Graf7b67c852007-08-22 13:53:52 -07002207 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002208 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002209 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002210 u8 type;
2211 int err;
2212 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002213 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002214
Thomas Graf35a7aa02007-08-22 14:00:40 -07002215 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002216 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002217
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002218 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2219
Thomas Graf5424f322007-08-22 14:01:33 -07002220 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002221 if (err)
2222 return err;
2223
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002224 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002225 if (err)
2226 return err;
2227
2228 if (!n)
2229 return 0;
2230
Fan Du8d549c42013-11-07 17:47:49 +08002231 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232
2233 return 0;
2234}
2235#else
2236static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002237 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002238{
2239 return -ENOPROTOOPT;
2240}
2241#endif
2242
2243#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002244static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002245{
2246 struct xfrm_user_migrate um;
2247
2248 memset(&um, 0, sizeof(um));
2249 um.proto = m->proto;
2250 um.mode = m->mode;
2251 um.reqid = m->reqid;
2252 um.old_family = m->old_family;
2253 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2254 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2255 um.new_family = m->new_family;
2256 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2257 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2258
Thomas Grafc0144be2007-08-22 13:55:43 -07002259 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002260}
2261
David S. Miller183cad12011-02-24 00:28:01 -05002262static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002263{
2264 struct xfrm_user_kmaddress uk;
2265
2266 memset(&uk, 0, sizeof(uk));
2267 uk.family = k->family;
2268 uk.reserved = k->reserved;
2269 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002270 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002271
2272 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2273}
2274
2275static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002276{
2277 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002278 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2279 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2280 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002281}
2282
David S. Miller183cad12011-02-24 00:28:01 -05002283static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2284 int num_migrate, const struct xfrm_kmaddress *k,
2285 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002286{
David S. Miller183cad12011-02-24 00:28:01 -05002287 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002288 struct xfrm_userpolicy_id *pol_id;
2289 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002290 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002291
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002292 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2293 if (nlh == NULL)
2294 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002295
Thomas Graf7b67c852007-08-22 13:53:52 -07002296 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002297 /* copy data from selector, dir, and type to the pol_id */
2298 memset(pol_id, 0, sizeof(*pol_id));
2299 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2300 pol_id->dir = dir;
2301
David S. Miller1d1e34d2012-06-27 21:57:03 -07002302 if (k != NULL) {
2303 err = copy_to_user_kmaddress(k, skb);
2304 if (err)
2305 goto out_cancel;
2306 }
2307 err = copy_to_user_policy_type(type, skb);
2308 if (err)
2309 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002310 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002311 err = copy_to_user_migrate(mp, skb);
2312 if (err)
2313 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002314 }
2315
Johannes Berg053c0952015-01-16 22:09:00 +01002316 nlmsg_end(skb, nlh);
2317 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002318
2319out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002320 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002321 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002322}
2323
David S. Miller183cad12011-02-24 00:28:01 -05002324static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2325 const struct xfrm_migrate *m, int num_migrate,
2326 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002327{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002328 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002329 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002330
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002331 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002332 if (skb == NULL)
2333 return -ENOMEM;
2334
2335 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002336 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002337 BUG();
2338
Michal Kubecek21ee5432014-06-03 10:26:06 +02002339 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002340}
2341#else
David S. Miller183cad12011-02-24 00:28:01 -05002342static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2343 const struct xfrm_migrate *m, int num_migrate,
2344 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002345{
2346 return -ENOPROTOOPT;
2347}
2348#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002349
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002350#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002351
2352static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002353 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002354 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2355 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2356 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2357 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2358 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2359 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002360 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002361 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002362 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002363 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002364 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002365 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002366 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002367 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2368 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002369 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002370 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002371 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002372 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002373 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374};
2375
Thomas Graf492b5582005-05-03 14:26:40 -07002376#undef XMSGSIZE
2377
Thomas Grafcf5cb792007-08-22 13:59:04 -07002378static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002379 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2380 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2381 [XFRMA_LASTUSED] = { .type = NLA_U64},
2382 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002383 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002384 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2385 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2386 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2387 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2388 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2389 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2390 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2391 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2392 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2393 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2394 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2395 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2396 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2397 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002398 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002399 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002400 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002401 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002402 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002403 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002404 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002405};
2406
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002407static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2408 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2409 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2410};
2411
Mathias Krause05600a72013-02-24 14:10:27 +01002412static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002413 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002415 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002416 const struct nla_policy *nla_pol;
2417 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002418} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2419 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2420 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2421 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002422 .dump = xfrm_dump_sa,
2423 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002424 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2425 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2426 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002427 .dump = xfrm_dump_policy,
2428 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002429 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002430 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002431 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002432 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2433 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002434 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002435 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2436 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002437 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2438 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002439 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002440 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002441 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2442 .nla_pol = xfrma_spd_policy,
2443 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002444 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445};
2446
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002447static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002449 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002450 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002451 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002452 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Fan Du74005992015-01-27 17:00:29 +08002454#ifdef CONFIG_COMPAT
Andy Lutomirski2bf8c472016-03-22 14:25:10 -07002455 if (in_compat_syscall())
Yi Zhao83e2d052016-11-29 18:09:01 +08002456 return -EOPNOTSUPP;
Fan Du74005992015-01-27 17:00:29 +08002457#endif
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 type -= XFRM_MSG_BASE;
2464 link = &xfrm_dispatch[type];
2465
2466 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002467 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002468 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Thomas Graf492b5582005-05-03 14:26:40 -07002470 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2471 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002472 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002474 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002476 {
2477 struct netlink_dump_control c = {
2478 .dump = link->dump,
2479 .done = link->done,
2480 };
2481 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002485 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2486 link->nla_max ? : XFRMA_MAX,
2487 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002488 if (err < 0)
2489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002492 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Thomas Graf5424f322007-08-22 14:01:33 -07002494 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002497static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Fan Du283bc9f2013-11-07 17:47:50 +08002499 struct net *net = sock_net(skb->sk);
2500
2501 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002502 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002503 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Thomas Graf7deb2262007-08-22 13:57:39 -07002506static inline size_t xfrm_expire_msgsize(void)
2507{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002508 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2509 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002510}
2511
David S. Miller214e0052011-02-24 00:02:38 -05002512static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 struct xfrm_user_expire *ue;
2515 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002516 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Eric W. Biederman15e47302012-09-07 20:12:54 +00002518 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002519 if (nlh == NULL)
2520 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Thomas Graf7b67c852007-08-22 13:53:52 -07002522 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002524 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
David S. Miller1d1e34d2012-06-27 21:57:03 -07002526 err = xfrm_mark_put(skb, &x->mark);
2527 if (err)
2528 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002529
Johannes Berg053c0952015-01-16 22:09:00 +01002530 nlmsg_end(skb, nlh);
2531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
David S. Miller214e0052011-02-24 00:02:38 -05002534static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002536 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 struct sk_buff *skb;
2538
Thomas Graf7deb2262007-08-22 13:57:39 -07002539 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (skb == NULL)
2541 return -ENOMEM;
2542
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002543 if (build_expire(skb, x, c) < 0) {
2544 kfree_skb(skb);
2545 return -EMSGSIZE;
2546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Michal Kubecek21ee5432014-06-03 10:26:06 +02002548 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
David S. Miller214e0052011-02-24 00:02:38 -05002551static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002552{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002553 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002554 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002555
Steffen Klassertd8647b72011-03-08 00:10:27 +00002556 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002557 if (skb == NULL)
2558 return -ENOMEM;
2559
2560 if (build_aevent(skb, x, c) < 0)
2561 BUG();
2562
Michal Kubecek21ee5432014-06-03 10:26:06 +02002563 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002564}
2565
David S. Miller214e0052011-02-24 00:02:38 -05002566static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002567{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002568 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002569 struct xfrm_usersa_flush *p;
2570 struct nlmsghdr *nlh;
2571 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002572 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002573
Thomas Graf7deb2262007-08-22 13:57:39 -07002574 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002575 if (skb == NULL)
2576 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002577
Eric W. Biederman15e47302012-09-07 20:12:54 +00002578 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002579 if (nlh == NULL) {
2580 kfree_skb(skb);
2581 return -EMSGSIZE;
2582 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002583
Thomas Graf7b67c852007-08-22 13:53:52 -07002584 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002585 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002586
Thomas Graf98250692007-08-22 12:47:26 -07002587 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002588
Michal Kubecek21ee5432014-06-03 10:26:06 +02002589 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002590}
2591
Thomas Graf7deb2262007-08-22 13:57:39 -07002592static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002593{
Thomas Graf7deb2262007-08-22 13:57:39 -07002594 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002595 if (x->aead)
2596 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002597 if (x->aalg) {
2598 l += nla_total_size(sizeof(struct xfrm_algo) +
2599 (x->aalg->alg_key_len + 7) / 8);
2600 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2601 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002602 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002603 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002604 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002605 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002606 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002607 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002608 if (x->tfcpad)
2609 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002610 if (x->replay_esn)
2611 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002612 else
2613 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002614 if (x->security)
2615 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2616 x->security->ctx_len);
2617 if (x->coaddr)
2618 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002619 if (x->props.extra_flags)
2620 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002621
Herbert Xud26f3982007-11-13 21:47:08 -08002622 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002623 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002624
2625 return l;
2626}
2627
David S. Miller214e0052011-02-24 00:02:38 -05002628static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002629{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002630 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002631 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002632 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002633 struct nlmsghdr *nlh;
2634 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002635 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002636 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002637
2638 headlen = sizeof(*p);
2639 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002640 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002641 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002642 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002643 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002644 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002645
Thomas Graf7deb2262007-08-22 13:57:39 -07002646 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002647 if (skb == NULL)
2648 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002649
Eric W. Biederman15e47302012-09-07 20:12:54 +00002650 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002651 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002652 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002653 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002654
Thomas Graf7b67c852007-08-22 13:53:52 -07002655 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002656 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002657 struct nlattr *attr;
2658
Thomas Graf7b67c852007-08-22 13:53:52 -07002659 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002660 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2661 id->spi = x->id.spi;
2662 id->family = x->props.family;
2663 id->proto = x->id.proto;
2664
Thomas Grafc0144be2007-08-22 13:55:43 -07002665 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002666 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002667 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002668 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002669
2670 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002671 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002672 err = copy_to_user_state_extra(x, p, skb);
2673 if (err)
2674 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002675
Thomas Graf98250692007-08-22 12:47:26 -07002676 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002677
Michal Kubecek21ee5432014-06-03 10:26:06 +02002678 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002679
David S. Miller1d1e34d2012-06-27 21:57:03 -07002680out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002681 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002682 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002683}
2684
David S. Miller214e0052011-02-24 00:02:38 -05002685static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002686{
2687
2688 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002689 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002690 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002691 case XFRM_MSG_NEWAE:
2692 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002693 case XFRM_MSG_DELSA:
2694 case XFRM_MSG_UPDSA:
2695 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002696 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002697 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002698 return xfrm_notify_sa_flush(c);
2699 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002700 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2701 c->event);
2702 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002703 }
2704
2705 return 0;
2706
2707}
2708
Thomas Graf7deb2262007-08-22 13:57:39 -07002709static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2710 struct xfrm_policy *xp)
2711{
2712 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2713 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002714 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002715 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2716 + userpolicy_type_attrsize();
2717}
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002720 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002722 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 struct xfrm_user_acquire *ua;
2724 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002725 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002727 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2728 if (nlh == NULL)
2729 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Thomas Graf7b67c852007-08-22 13:53:52 -07002731 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 memcpy(&ua->id, &x->id, sizeof(ua->id));
2733 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2734 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002735 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 ua->aalgos = xt->aalgos;
2737 ua->ealgos = xt->ealgos;
2738 ua->calgos = xt->calgos;
2739 ua->seq = x->km.seq = seq;
2740
David S. Miller1d1e34d2012-06-27 21:57:03 -07002741 err = copy_to_user_tmpl(xp, skb);
2742 if (!err)
2743 err = copy_to_user_state_sec_ctx(x, skb);
2744 if (!err)
2745 err = copy_to_user_policy_type(xp->type, skb);
2746 if (!err)
2747 err = xfrm_mark_put(skb, &xp->mark);
2748 if (err) {
2749 nlmsg_cancel(skb, nlh);
2750 return err;
2751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Johannes Berg053c0952015-01-16 22:09:00 +01002753 nlmsg_end(skb, nlh);
2754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
2757static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002758 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002760 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Thomas Graf7deb2262007-08-22 13:57:39 -07002763 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (skb == NULL)
2765 return -ENOMEM;
2766
Fan Du65e07362012-08-15 10:13:47 +08002767 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 BUG();
2769
Michal Kubecek21ee5432014-06-03 10:26:06 +02002770 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
2773/* User gives us xfrm_user_policy_info followed by an array of 0
2774 * or more templates.
2775 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002776static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 u8 *data, int len, int *dir)
2778{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002779 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2781 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2782 struct xfrm_policy *xp;
2783 int nr;
2784
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002785 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 case AF_INET:
2787 if (opt != IP_XFRM_POLICY) {
2788 *dir = -EOPNOTSUPP;
2789 return NULL;
2790 }
2791 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002792#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 case AF_INET6:
2794 if (opt != IPV6_XFRM_POLICY) {
2795 *dir = -EOPNOTSUPP;
2796 return NULL;
2797 }
2798 break;
2799#endif
2800 default:
2801 *dir = -EINVAL;
2802 return NULL;
2803 }
2804
2805 *dir = -EINVAL;
2806
2807 if (len < sizeof(*p) ||
2808 verify_newpolicy_info(p))
2809 return NULL;
2810
2811 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002812 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return NULL;
2814
Herbert Xua4f1bac2005-07-26 15:43:17 -07002815 if (p->dir > XFRM_POLICY_OUT)
2816 return NULL;
2817
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002818 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (xp == NULL) {
2820 *dir = -ENOBUFS;
2821 return NULL;
2822 }
2823
2824 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002825 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 copy_templates(xp, ut, nr);
2827
2828 *dir = p->dir;
2829
2830 return xp;
2831}
2832
Thomas Graf7deb2262007-08-22 13:57:39 -07002833static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2834{
2835 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2836 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2837 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002838 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002839 + userpolicy_type_attrsize();
2840}
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002843 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002846 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002847 struct nlmsghdr *nlh;
2848 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Eric W. Biederman15e47302012-09-07 20:12:54 +00002850 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002851 if (nlh == NULL)
2852 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Thomas Graf7b67c852007-08-22 13:53:52 -07002854 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002856 err = copy_to_user_tmpl(xp, skb);
2857 if (!err)
2858 err = copy_to_user_sec_ctx(xp, skb);
2859 if (!err)
2860 err = copy_to_user_policy_type(xp->type, skb);
2861 if (!err)
2862 err = xfrm_mark_put(skb, &xp->mark);
2863 if (err) {
2864 nlmsg_cancel(skb, nlh);
2865 return err;
2866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 upe->hard = !!hard;
2868
Johannes Berg053c0952015-01-16 22:09:00 +01002869 nlmsg_end(skb, nlh);
2870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
David S. Miller214e0052011-02-24 00:02:38 -05002873static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002875 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Thomas Graf7deb2262007-08-22 13:57:39 -07002878 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (skb == NULL)
2880 return -ENOMEM;
2881
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002882 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 BUG();
2884
Michal Kubecek21ee5432014-06-03 10:26:06 +02002885 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
David S. Miller214e0052011-02-24 00:02:38 -05002888static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002889{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002890 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002891 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002892 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002893 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002894 struct nlmsghdr *nlh;
2895 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002896 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002897
2898 headlen = sizeof(*p);
2899 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002900 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002901 headlen = sizeof(*id);
2902 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002903 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002904 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002905 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002906
Thomas Graf7deb2262007-08-22 13:57:39 -07002907 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002908 if (skb == NULL)
2909 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002910
Eric W. Biederman15e47302012-09-07 20:12:54 +00002911 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002912 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002913 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002914 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002915
Thomas Graf7b67c852007-08-22 13:53:52 -07002916 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002917 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002918 struct nlattr *attr;
2919
Thomas Graf7b67c852007-08-22 13:53:52 -07002920 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002921 memset(id, 0, sizeof(*id));
2922 id->dir = dir;
2923 if (c->data.byid)
2924 id->index = xp->index;
2925 else
2926 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2927
Thomas Grafc0144be2007-08-22 13:55:43 -07002928 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002929 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002930 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002931 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002932
2933 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002934 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002935
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002936 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002937 err = copy_to_user_tmpl(xp, skb);
2938 if (!err)
2939 err = copy_to_user_policy_type(xp->type, skb);
2940 if (!err)
2941 err = xfrm_mark_put(skb, &xp->mark);
2942 if (err)
2943 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002944
Thomas Graf98250692007-08-22 12:47:26 -07002945 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002946
Michal Kubecek21ee5432014-06-03 10:26:06 +02002947 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002948
David S. Miller1d1e34d2012-06-27 21:57:03 -07002949out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002950 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002951 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002952}
2953
David S. Miller214e0052011-02-24 00:02:38 -05002954static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002955{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002956 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002957 struct nlmsghdr *nlh;
2958 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002959 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002960
Thomas Graf7deb2262007-08-22 13:57:39 -07002961 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002962 if (skb == NULL)
2963 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002964
Eric W. Biederman15e47302012-09-07 20:12:54 +00002965 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002966 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002967 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002968 goto out_free_skb;
2969 err = copy_to_user_policy_type(c->data.type, skb);
2970 if (err)
2971 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002972
Thomas Graf98250692007-08-22 12:47:26 -07002973 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002974
Michal Kubecek21ee5432014-06-03 10:26:06 +02002975 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002976
David S. Miller1d1e34d2012-06-27 21:57:03 -07002977out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002978 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002979 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002980}
2981
David S. Miller214e0052011-02-24 00:02:38 -05002982static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002983{
2984
2985 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002986 case XFRM_MSG_NEWPOLICY:
2987 case XFRM_MSG_UPDPOLICY:
2988 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002989 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002990 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002991 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002992 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002993 return xfrm_exp_policy_notify(xp, dir, c);
2994 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002995 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2996 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002997 }
2998
2999 return 0;
3000
3001}
3002
Thomas Graf7deb2262007-08-22 13:57:39 -07003003static inline size_t xfrm_report_msgsize(void)
3004{
3005 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3006}
3007
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003008static int build_report(struct sk_buff *skb, u8 proto,
3009 struct xfrm_selector *sel, xfrm_address_t *addr)
3010{
3011 struct xfrm_user_report *ur;
3012 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003013
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003014 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3015 if (nlh == NULL)
3016 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003017
Thomas Graf7b67c852007-08-22 13:53:52 -07003018 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003019 ur->proto = proto;
3020 memcpy(&ur->sel, sel, sizeof(ur->sel));
3021
David S. Miller1d1e34d2012-06-27 21:57:03 -07003022 if (addr) {
3023 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3024 if (err) {
3025 nlmsg_cancel(skb, nlh);
3026 return err;
3027 }
3028 }
Johannes Berg053c0952015-01-16 22:09:00 +01003029 nlmsg_end(skb, nlh);
3030 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003031}
3032
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003033static int xfrm_send_report(struct net *net, u8 proto,
3034 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003035{
3036 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003037
Thomas Graf7deb2262007-08-22 13:57:39 -07003038 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003039 if (skb == NULL)
3040 return -ENOMEM;
3041
3042 if (build_report(skb, proto, sel, addr) < 0)
3043 BUG();
3044
Michal Kubecek21ee5432014-06-03 10:26:06 +02003045 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003046}
3047
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003048static inline size_t xfrm_mapping_msgsize(void)
3049{
3050 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3051}
3052
3053static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3054 xfrm_address_t *new_saddr, __be16 new_sport)
3055{
3056 struct xfrm_user_mapping *um;
3057 struct nlmsghdr *nlh;
3058
3059 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3060 if (nlh == NULL)
3061 return -EMSGSIZE;
3062
3063 um = nlmsg_data(nlh);
3064
3065 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3066 um->id.spi = x->id.spi;
3067 um->id.family = x->props.family;
3068 um->id.proto = x->id.proto;
3069 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3070 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3071 um->new_sport = new_sport;
3072 um->old_sport = x->encap->encap_sport;
3073 um->reqid = x->props.reqid;
3074
Johannes Berg053c0952015-01-16 22:09:00 +01003075 nlmsg_end(skb, nlh);
3076 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003077}
3078
3079static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3080 __be16 sport)
3081{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003082 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003083 struct sk_buff *skb;
3084
3085 if (x->id.proto != IPPROTO_ESP)
3086 return -EINVAL;
3087
3088 if (!x->encap)
3089 return -EINVAL;
3090
3091 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3092 if (skb == NULL)
3093 return -ENOMEM;
3094
3095 if (build_mapping(skb, x, ipaddr, sport) < 0)
3096 BUG();
3097
Michal Kubecek21ee5432014-06-03 10:26:06 +02003098 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003099}
3100
Horia Geanta0f245582014-02-12 16:20:06 +02003101static bool xfrm_is_alive(const struct km_event *c)
3102{
3103 return (bool)xfrm_acquire_is_on(c->net);
3104}
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106static struct xfrm_mgr netlink_mgr = {
3107 .id = "netlink",
3108 .notify = xfrm_send_state_notify,
3109 .acquire = xfrm_send_acquire,
3110 .compile_policy = xfrm_compile_policy,
3111 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003112 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003113 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003114 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003115 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116};
3117
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003118static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Patrick McHardybe336902006-03-20 22:40:54 -08003120 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003121 struct netlink_kernel_cfg cfg = {
3122 .groups = XFRMNLGRP_MAX,
3123 .input = xfrm_netlink_rcv,
3124 };
Patrick McHardybe336902006-03-20 22:40:54 -08003125
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003126 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003127 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003129 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003130 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return 0;
3132}
3133
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003134static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003135{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003136 struct net *net;
3137 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003138 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003139 synchronize_net();
3140 list_for_each_entry(net, net_exit_list, exit_list)
3141 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003142}
3143
3144static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003145 .init = xfrm_user_net_init,
3146 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003147};
3148
3149static int __init xfrm_user_init(void)
3150{
3151 int rv;
3152
3153 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3154
3155 rv = register_pernet_subsys(&xfrm_user_net_ops);
3156 if (rv < 0)
3157 return rv;
3158 rv = xfrm_register_km(&netlink_mgr);
3159 if (rv < 0)
3160 unregister_pernet_subsys(&xfrm_user_net_ops);
3161 return rv;
3162}
3163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164static void __exit xfrm_user_exit(void)
3165{
3166 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003167 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
3170module_init(xfrm_user_init);
3171module_exit(xfrm_user_exit);
3172MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003173MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003174