blob: 671a1d0333f0d2c828c757944016cbe48836ec72 [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
418 return 0;
419}
420
Steffen Klassertd8647b72011-03-08 00:10:27 +0000421static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
422 struct xfrm_replay_state_esn **preplay_esn,
423 struct nlattr *rta)
424{
425 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000426 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000427
428 if (!rta)
429 return 0;
430
431 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000432 klen = xfrm_replay_state_esn_len(up);
433 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000434
Mathias Krauseecd79182012-09-20 10:01:49 +0000435 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000436 if (!p)
437 return -ENOMEM;
438
Mathias Krauseecd79182012-09-20 10:01:49 +0000439 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000440 if (!pp) {
441 kfree(p);
442 return -ENOMEM;
443 }
444
Mathias Krauseecd79182012-09-20 10:01:49 +0000445 memcpy(p, up, ulen);
446 memcpy(pp, up, ulen);
447
Steffen Klassertd8647b72011-03-08 00:10:27 +0000448 *replay_esn = p;
449 *preplay_esn = pp;
450
451 return 0;
452}
453
Joy Latten661697f2007-04-13 16:14:35 -0700454static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800455{
Trent Jaegerdf718372005-12-13 23:12:27 -0800456 int len = 0;
457
458 if (xfrm_ctx) {
459 len += sizeof(struct xfrm_user_sec_ctx);
460 len += xfrm_ctx->ctx_len;
461 }
462 return len;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
466{
467 memcpy(&x->id, &p->id, sizeof(x->id));
468 memcpy(&x->sel, &p->sel, sizeof(x->sel));
469 memcpy(&x->lft, &p->lft, sizeof(x->lft));
470 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800471 x->props.replay_window = min_t(unsigned int, p->replay_window,
472 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 x->props.reqid = p->reqid;
474 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700475 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700477
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700478 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700479 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800482/*
483 * someday when pfkey also has support, we could have the code
484 * somehow made shareable and move it to xfrm_state.c - JHS
485 *
486*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000487static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
488 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800489{
Thomas Graf5424f322007-08-22 14:01:33 -0700490 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000491 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700492 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
493 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
494 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800495
Steffen Klassertd8647b72011-03-08 00:10:27 +0000496 if (re) {
497 struct xfrm_replay_state_esn *replay_esn;
498 replay_esn = nla_data(re);
499 memcpy(x->replay_esn, replay_esn,
500 xfrm_replay_state_esn_len(replay_esn));
501 memcpy(x->preplay_esn, replay_esn,
502 xfrm_replay_state_esn_len(replay_esn));
503 }
504
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800505 if (rp) {
506 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700507 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800508 memcpy(&x->replay, replay, sizeof(*replay));
509 memcpy(&x->preplay, replay, sizeof(*replay));
510 }
511
512 if (lt) {
513 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700514 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800515 x->curlft.bytes = ltime->bytes;
516 x->curlft.packets = ltime->packets;
517 x->curlft.add_time = ltime->add_time;
518 x->curlft.use_time = ltime->use_time;
519 }
520
Thomas Grafcf5cb792007-08-22 13:59:04 -0700521 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700522 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800523
Thomas Grafcf5cb792007-08-22 13:59:04 -0700524 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700525 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800526}
527
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800528static struct xfrm_state *xfrm_state_construct(struct net *net,
529 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700530 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int *errp)
532{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800533 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int err = -ENOMEM;
535
536 if (!x)
537 goto error_no_put;
538
539 copy_from_user_state(x, p);
540
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100541 if (attrs[XFRMA_SA_EXTRA_FLAGS])
542 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
543
Herbert Xu69b01372015-05-27 16:03:45 +0800544 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800545 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000546 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
547 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000549 if (!x->props.aalgo) {
550 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
551 attrs[XFRMA_ALG_AUTH])))
552 goto error;
553 }
Herbert Xu69b01372015-05-27 16:03:45 +0800554 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto error;
556 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
557 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700558 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700560
561 if (attrs[XFRMA_ENCAP]) {
562 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
563 sizeof(*x->encap), GFP_KERNEL);
564 if (x->encap == NULL)
565 goto error;
566 }
567
Martin Willi35d28562010-12-08 04:37:49 +0000568 if (attrs[XFRMA_TFCPAD])
569 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
570
Thomas Graffd211502007-09-06 03:28:08 -0700571 if (attrs[XFRMA_COADDR]) {
572 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
573 sizeof(*x->coaddr), GFP_KERNEL);
574 if (x->coaddr == NULL)
575 goto error;
576 }
577
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000578 xfrm_mark_get(attrs, &x->mark);
579
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700580 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (err)
582 goto error;
583
Mathias Krause2f30ea52016-09-08 18:09:57 +0200584 if (attrs[XFRMA_SEC_CTX]) {
585 err = security_xfrm_state_alloc(x,
586 nla_data(attrs[XFRMA_SEC_CTX]));
587 if (err)
588 goto error;
589 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800590
Steffen Klassertd8647b72011-03-08 00:10:27 +0000591 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
592 attrs[XFRMA_REPLAY_ESN_VAL])))
593 goto error;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800596 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800597 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800598 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800599
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000600 if ((err = xfrm_init_replay(x)))
601 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800602
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000603 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000604 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 return x;
607
608error:
609 x->km.state = XFRM_STATE_DEAD;
610 xfrm_state_put(x);
611error_no_put:
612 *errp = err;
613 return NULL;
614}
615
Christoph Hellwig22e70052007-01-02 15:22:30 -0800616static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700617 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800619 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700620 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct xfrm_state *x;
622 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700623 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Thomas Graf35a7aa02007-08-22 14:00:40 -0700625 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (err)
627 return err;
628
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800629 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!x)
631 return err;
632
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700633 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
635 err = xfrm_state_add(x);
636 else
637 err = xfrm_state_update(x);
638
Tetsuo Handa2e710292014-04-22 21:48:30 +0900639 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (err < 0) {
642 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800643 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700644 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700647 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000648 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700649 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700650
651 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700652out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700653 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return err;
655}
656
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800657static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
658 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700659 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700660 int *errp)
661{
662 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000663 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700664 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000665 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700666
667 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
668 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000669 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700670 } else {
671 xfrm_address_t *saddr = NULL;
672
Thomas Graf35a7aa02007-08-22 14:00:40 -0700673 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700674 if (!saddr) {
675 err = -EINVAL;
676 goto out;
677 }
678
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800679 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000680 x = xfrm_state_lookup_byaddr(net, mark,
681 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800682 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700683 }
684
685 out:
686 if (!x && errp)
687 *errp = err;
688 return x;
689}
690
Christoph Hellwig22e70052007-01-02 15:22:30 -0800691static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700692 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800694 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700696 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700697 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700698 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800700 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David S. Miller6f68dc32006-06-08 23:58:52 -0700704 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700705 goto out;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700708 err = -EPERM;
709 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700712 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600713
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700714 if (err < 0)
715 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700716
717 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000718 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700719 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700720 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700722out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900723 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700724 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700725 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
729{
Mathias Krausef778a632012-09-19 11:33:39 +0000730 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 memcpy(&p->id, &x->id, sizeof(p->id));
732 memcpy(&p->sel, &x->sel, sizeof(p->sel));
733 memcpy(&p->lft, &x->lft, sizeof(p->lft));
734 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400735 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
736 put_unaligned(x->stats.replay, &p->stats.replay);
737 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700738 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 p->mode = x->props.mode;
740 p->replay_window = x->props.replay_window;
741 p->reqid = x->props.reqid;
742 p->family = x->props.family;
743 p->flags = x->props.flags;
744 p->seq = x->km.seq;
745}
746
747struct xfrm_dump_info {
748 struct sk_buff *in_skb;
749 struct sk_buff *out_skb;
750 u32 nlmsg_seq;
751 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752};
753
Thomas Grafc0144be2007-08-22 13:55:43 -0700754static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
755{
Thomas Grafc0144be2007-08-22 13:55:43 -0700756 struct xfrm_user_sec_ctx *uctx;
757 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700758 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700759
760 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
761 if (attr == NULL)
762 return -EMSGSIZE;
763
764 uctx = nla_data(attr);
765 uctx->exttype = XFRMA_SEC_CTX;
766 uctx->len = ctx_size;
767 uctx->ctx_doi = s->ctx_doi;
768 uctx->ctx_alg = s->ctx_alg;
769 uctx->ctx_len = s->ctx_len;
770 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
771
772 return 0;
773}
774
Martin Willi4447bb32009-11-25 00:29:52 +0000775static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
776{
777 struct xfrm_algo *algo;
778 struct nlattr *nla;
779
780 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
781 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
782 if (!nla)
783 return -EMSGSIZE;
784
785 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000786 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000787 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
788 algo->alg_key_len = auth->alg_key_len;
789
790 return 0;
791}
792
Herbert Xu68325d32007-10-09 13:30:57 -0700793/* Don't change this without updating xfrm_sa_len! */
794static int copy_to_user_state_extra(struct xfrm_state *x,
795 struct xfrm_usersa_info *p,
796 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700798 int ret = 0;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 copy_to_user_state(x, p);
801
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100802 if (x->props.extra_flags) {
803 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
804 x->props.extra_flags);
805 if (ret)
806 goto out;
807 }
808
David S. Miller1d1e34d2012-06-27 21:57:03 -0700809 if (x->coaddr) {
810 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
811 if (ret)
812 goto out;
813 }
814 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200815 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
816 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700817 if (ret)
818 goto out;
819 }
820 if (x->aead) {
821 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
822 if (ret)
823 goto out;
824 }
825 if (x->aalg) {
826 ret = copy_to_user_auth(x->aalg, skb);
827 if (!ret)
828 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
829 xfrm_alg_auth_len(x->aalg), x->aalg);
830 if (ret)
831 goto out;
832 }
833 if (x->ealg) {
834 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
835 if (ret)
836 goto out;
837 }
838 if (x->calg) {
839 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
840 if (ret)
841 goto out;
842 }
843 if (x->encap) {
844 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
845 if (ret)
846 goto out;
847 }
848 if (x->tfcpad) {
849 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
850 if (ret)
851 goto out;
852 }
853 ret = xfrm_mark_put(skb, &x->mark);
854 if (ret)
855 goto out;
dingzhif293a5e2014-10-30 09:39:36 +0100856 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700857 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
858 xfrm_replay_state_esn_len(x->replay_esn),
859 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100860 else
861 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
862 &x->replay);
863 if (ret)
864 goto out;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700865 if (x->security)
866 ret = copy_sec_ctx(x->security, skb);
867out:
868 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700869}
870
871static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
872{
873 struct xfrm_dump_info *sp = ptr;
874 struct sk_buff *in_skb = sp->in_skb;
875 struct sk_buff *skb = sp->out_skb;
876 struct xfrm_usersa_info *p;
877 struct nlmsghdr *nlh;
878 int err;
879
Eric W. Biederman15e47302012-09-07 20:12:54 +0000880 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700881 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
882 if (nlh == NULL)
883 return -EMSGSIZE;
884
885 p = nlmsg_data(nlh);
886
887 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700888 if (err) {
889 nlmsg_cancel(skb, nlh);
890 return err;
891 }
Thomas Graf98250692007-08-22 12:47:26 -0700892 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Timo Teras4c563f72008-02-28 21:31:08 -0800896static int xfrm_dump_sa_done(struct netlink_callback *cb)
897{
898 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800899 struct sock *sk = cb->skb->sk;
900 struct net *net = sock_net(sk);
901
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200902 if (cb->args[0])
903 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800904 return 0;
905}
906
Nicolas Dichteld3623092014-02-14 15:30:36 +0100907static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
909{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800910 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800911 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct xfrm_dump_info info;
913
Timo Teras4c563f72008-02-28 21:31:08 -0800914 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
915 sizeof(cb->args) - sizeof(cb->args[0]));
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 info.in_skb = cb->skb;
918 info.out_skb = skb;
919 info.nlmsg_seq = cb->nlh->nlmsg_seq;
920 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800921
922 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100923 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100924 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100925 u8 proto = 0;
926 int err;
927
Nicolas Dichteld3623092014-02-14 15:30:36 +0100928 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
929 xfrma_policy);
930 if (err < 0)
931 return err;
932
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100933 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +0200934 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
935 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +0100936 if (filter == NULL)
937 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100938 }
939
940 if (attrs[XFRMA_PROTO])
941 proto = nla_get_u8(attrs[XFRMA_PROTO]);
942
943 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200944 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -0800945 }
946
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800947 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 return skb->len;
950}
951
952static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
953 struct xfrm_state *x, u32 seq)
954{
955 struct xfrm_dump_info info;
956 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000957 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Thomas Graf7deb2262007-08-22 13:57:39 -0700959 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (!skb)
961 return ERR_PTR(-ENOMEM);
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 info.in_skb = in_skb;
964 info.out_skb = skb;
965 info.nlmsg_seq = seq;
966 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Mathias Krause864745d2012-09-13 11:41:26 +0000968 err = dump_one_state(x, 0, &info);
969 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000971 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
974 return skb;
975}
976
Michal Kubecek21ee5432014-06-03 10:26:06 +0200977/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
978 * Must be called with RCU read lock.
979 */
980static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
981 u32 pid, unsigned int group)
982{
983 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
984
985 if (nlsk)
986 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
987 else
988 return -1;
989}
990
Thomas Graf7deb2262007-08-22 13:57:39 -0700991static inline size_t xfrm_spdinfo_msgsize(void)
992{
993 return NLMSG_ALIGN(4)
994 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200995 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
996 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
997 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -0700998}
999
Alexey Dobriyane0710412010-01-23 13:37:10 +00001000static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001001 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001002{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001003 struct xfrmk_spdinfo si;
1004 struct xfrmu_spdinfo spc;
1005 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001006 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001007 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001008 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001009 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001010 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001011
Eric W. Biederman15e47302012-09-07 20:12:54 +00001012 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001013 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001014 return -EMSGSIZE;
1015
1016 f = nlmsg_data(nlh);
1017 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001018 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001019 spc.incnt = si.incnt;
1020 spc.outcnt = si.outcnt;
1021 spc.fwdcnt = si.fwdcnt;
1022 spc.inscnt = si.inscnt;
1023 spc.outscnt = si.outscnt;
1024 spc.fwdscnt = si.fwdscnt;
1025 sph.spdhcnt = si.spdhcnt;
1026 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001027
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001028 do {
1029 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1030
1031 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1032 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1033 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1034 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1035 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1036
David S. Miller1d1e34d2012-06-27 21:57:03 -07001037 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1038 if (!err)
1039 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001040 if (!err)
1041 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1042 if (!err)
1043 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001044 if (err) {
1045 nlmsg_cancel(skb, nlh);
1046 return err;
1047 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001048
Johannes Berg053c0952015-01-16 22:09:00 +01001049 nlmsg_end(skb, nlh);
1050 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001051}
1052
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001053static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1054 struct nlattr **attrs)
1055{
1056 struct net *net = sock_net(skb->sk);
1057 struct xfrmu_spdhthresh *thresh4 = NULL;
1058 struct xfrmu_spdhthresh *thresh6 = NULL;
1059
1060 /* selector prefixlen thresholds to hash policies */
1061 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1062 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1063
1064 if (nla_len(rta) < sizeof(*thresh4))
1065 return -EINVAL;
1066 thresh4 = nla_data(rta);
1067 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1068 return -EINVAL;
1069 }
1070 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1071 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1072
1073 if (nla_len(rta) < sizeof(*thresh6))
1074 return -EINVAL;
1075 thresh6 = nla_data(rta);
1076 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1077 return -EINVAL;
1078 }
1079
1080 if (thresh4 || thresh6) {
1081 write_seqlock(&net->xfrm.policy_hthresh.lock);
1082 if (thresh4) {
1083 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1084 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1085 }
1086 if (thresh6) {
1087 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1088 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1089 }
1090 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1091
1092 xfrm_policy_hash_rebuild(net);
1093 }
1094
1095 return 0;
1096}
1097
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001098static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001099 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001100{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001101 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001102 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001103 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001104 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001105 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001106
Thomas Graf7deb2262007-08-22 13:57:39 -07001107 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001108 if (r_skb == NULL)
1109 return -ENOMEM;
1110
Eric W. Biederman15e47302012-09-07 20:12:54 +00001111 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001112 BUG();
1113
Eric W. Biederman15e47302012-09-07 20:12:54 +00001114 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001115}
1116
Thomas Graf7deb2262007-08-22 13:57:39 -07001117static inline size_t xfrm_sadinfo_msgsize(void)
1118{
1119 return NLMSG_ALIGN(4)
1120 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1121 + nla_total_size(4); /* XFRMA_SAD_CNT */
1122}
1123
Alexey Dobriyane0710412010-01-23 13:37:10 +00001124static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001125 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001126{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001127 struct xfrmk_sadinfo si;
1128 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001129 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001130 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001131 u32 *f;
1132
Eric W. Biederman15e47302012-09-07 20:12:54 +00001133 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001134 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001135 return -EMSGSIZE;
1136
1137 f = nlmsg_data(nlh);
1138 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001139 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001140
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001141 sh.sadhmcnt = si.sadhmcnt;
1142 sh.sadhcnt = si.sadhcnt;
1143
David S. Miller1d1e34d2012-06-27 21:57:03 -07001144 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1145 if (!err)
1146 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1147 if (err) {
1148 nlmsg_cancel(skb, nlh);
1149 return err;
1150 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001151
Johannes Berg053c0952015-01-16 22:09:00 +01001152 nlmsg_end(skb, nlh);
1153 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001154}
1155
1156static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001157 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001158{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001159 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001160 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001161 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001162 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001163 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001164
Thomas Graf7deb2262007-08-22 13:57:39 -07001165 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001166 if (r_skb == NULL)
1167 return -ENOMEM;
1168
Eric W. Biederman15e47302012-09-07 20:12:54 +00001169 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001170 BUG();
1171
Eric W. Biederman15e47302012-09-07 20:12:54 +00001172 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001173}
1174
Christoph Hellwig22e70052007-01-02 15:22:30 -08001175static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001176 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001178 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001179 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct xfrm_state *x;
1181 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001182 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001184 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (x == NULL)
1186 goto out_noput;
1187
1188 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1189 if (IS_ERR(resp_skb)) {
1190 err = PTR_ERR(resp_skb);
1191 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001192 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194 xfrm_state_put(x);
1195out_noput:
1196 return err;
1197}
1198
Christoph Hellwig22e70052007-01-02 15:22:30 -08001199static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001200 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001202 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct xfrm_state *x;
1204 struct xfrm_userspi_info *p;
1205 struct sk_buff *resp_skb;
1206 xfrm_address_t *daddr;
1207 int family;
1208 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001209 u32 mark;
1210 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Thomas Graf7b67c852007-08-22 13:53:52 -07001212 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001213 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (err)
1215 goto out_noput;
1216
1217 family = p->info.family;
1218 daddr = &p->info.id.daddr;
1219
1220 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001221
1222 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001224 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001225 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 xfrm_state_put(x);
1227 x = NULL;
1228 }
1229 }
1230
1231 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001232 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 p->info.id.proto, daddr,
1234 &p->info.saddr, 1,
1235 family);
1236 err = -ENOENT;
1237 if (x == NULL)
1238 goto out_noput;
1239
Herbert Xu658b2192007-10-09 13:29:52 -07001240 err = xfrm_alloc_spi(x, p->min, p->max);
1241 if (err)
1242 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Herbert Xu658b2192007-10-09 13:29:52 -07001244 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (IS_ERR(resp_skb)) {
1246 err = PTR_ERR(resp_skb);
1247 goto out;
1248 }
1249
Eric W. Biederman15e47302012-09-07 20:12:54 +00001250 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252out:
1253 xfrm_state_put(x);
1254out_noput:
1255 return err;
1256}
1257
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001258static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 switch (dir) {
1261 case XFRM_POLICY_IN:
1262 case XFRM_POLICY_OUT:
1263 case XFRM_POLICY_FWD:
1264 break;
1265
1266 default:
1267 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 return 0;
1271}
1272
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001273static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001274{
1275 switch (type) {
1276 case XFRM_POLICY_TYPE_MAIN:
1277#ifdef CONFIG_XFRM_SUB_POLICY
1278 case XFRM_POLICY_TYPE_SUB:
1279#endif
1280 break;
1281
1282 default:
1283 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001284 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001285
1286 return 0;
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1290{
Fan Due682adf2013-11-07 17:47:48 +08001291 int ret;
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 switch (p->share) {
1294 case XFRM_SHARE_ANY:
1295 case XFRM_SHARE_SESSION:
1296 case XFRM_SHARE_USER:
1297 case XFRM_SHARE_UNIQUE:
1298 break;
1299
1300 default:
1301 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 switch (p->action) {
1305 case XFRM_POLICY_ALLOW:
1306 case XFRM_POLICY_BLOCK:
1307 break;
1308
1309 default:
1310 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 switch (p->sel.family) {
1314 case AF_INET:
1315 break;
1316
1317 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001318#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 break;
1320#else
1321 return -EAFNOSUPPORT;
1322#endif
1323
1324 default:
1325 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Fan Due682adf2013-11-07 17:47:48 +08001328 ret = verify_policy_dir(p->dir);
1329 if (ret)
1330 return ret;
1331 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1332 return -EINVAL;
1333
1334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Thomas Graf5424f322007-08-22 14:01:33 -07001337static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001338{
Thomas Graf5424f322007-08-22 14:01:33 -07001339 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001340 struct xfrm_user_sec_ctx *uctx;
1341
1342 if (!rt)
1343 return 0;
1344
Thomas Graf5424f322007-08-22 14:01:33 -07001345 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001346 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1350 int nr)
1351{
1352 int i;
1353
1354 xp->xfrm_nr = nr;
1355 for (i = 0; i < nr; i++, ut++) {
1356 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1357
1358 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1359 memcpy(&t->saddr, &ut->saddr,
1360 sizeof(xfrm_address_t));
1361 t->reqid = ut->reqid;
1362 t->mode = ut->mode;
1363 t->share = ut->share;
1364 t->optional = ut->optional;
1365 t->aalgos = ut->aalgos;
1366 t->ealgos = ut->ealgos;
1367 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001368 /* If all masks are ~0, then we allow all algorithms. */
1369 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001370 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372}
1373
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001374static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1375{
1376 int i;
1377
1378 if (nr > XFRM_MAX_DEPTH)
1379 return -EINVAL;
1380
1381 for (i = 0; i < nr; i++) {
1382 /* We never validated the ut->family value, so many
1383 * applications simply leave it at zero. The check was
1384 * never made and ut->family was ignored because all
1385 * templates could be assumed to have the same family as
1386 * the policy itself. Now that we will have ipv4-in-ipv6
1387 * and ipv6-in-ipv4 tunnels, this is no longer true.
1388 */
1389 if (!ut[i].family)
1390 ut[i].family = family;
1391
1392 switch (ut[i].family) {
1393 case AF_INET:
1394 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001395#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001396 case AF_INET6:
1397 break;
1398#endif
1399 default:
1400 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001401 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001402 }
1403
1404 return 0;
1405}
1406
Thomas Graf5424f322007-08-22 14:01:33 -07001407static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Thomas Graf5424f322007-08-22 14:01:33 -07001409 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (!rt) {
1412 pol->xfrm_nr = 0;
1413 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001414 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1415 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001416 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001418 err = validate_tmpl(nr, utmpl, pol->family);
1419 if (err)
1420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Thomas Graf5424f322007-08-22 14:01:33 -07001422 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 return 0;
1425}
1426
Thomas Graf5424f322007-08-22 14:01:33 -07001427static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001428{
Thomas Graf5424f322007-08-22 14:01:33 -07001429 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001430 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001431 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001432 int err;
1433
1434 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001435 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001436 type = upt->type;
1437 }
1438
1439 err = verify_policy_type(type);
1440 if (err)
1441 return err;
1442
1443 *tp = type;
1444 return 0;
1445}
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1448{
1449 xp->priority = p->priority;
1450 xp->index = p->index;
1451 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1452 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1453 xp->action = p->action;
1454 xp->flags = p->flags;
1455 xp->family = p->sel.family;
1456 /* XXX xp->share = p->share; */
1457}
1458
1459static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1460{
Mathias Krause7b789832012-09-19 11:33:40 +00001461 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1463 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1464 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1465 p->priority = xp->priority;
1466 p->index = xp->index;
1467 p->sel.family = xp->family;
1468 p->dir = dir;
1469 p->action = xp->action;
1470 p->flags = xp->flags;
1471 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1472}
1473
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001474static 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 -07001475{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001476 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 int err;
1478
1479 if (!xp) {
1480 *errp = -ENOMEM;
1481 return NULL;
1482 }
1483
1484 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001485
Thomas Graf35a7aa02007-08-22 14:00:40 -07001486 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001487 if (err)
1488 goto error;
1489
Thomas Graf35a7aa02007-08-22 14:00:40 -07001490 if (!(err = copy_from_user_tmpl(xp, attrs)))
1491 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001492 if (err)
1493 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001495 xfrm_mark_get(attrs, &xp->mark);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001498 error:
1499 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001500 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001501 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001502 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Christoph Hellwig22e70052007-01-02 15:22:30 -08001505static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001506 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001508 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001509 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001511 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 int err;
1513 int excl;
1514
1515 err = verify_newpolicy_info(p);
1516 if (err)
1517 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001518 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001519 if (err)
1520 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001522 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (!xp)
1524 return err;
1525
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001526 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001527 * Aha! this is anti-netlink really i.e more pfkey derived
1528 * in netlink excl is a flag and you wouldnt need
1529 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1531 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001532 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001535 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 kfree(xp);
1537 return err;
1538 }
1539
Herbert Xuf60f6b82005-06-18 22:44:37 -07001540 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001541 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001542 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001543 km_policy_notify(xp, p->dir, &c);
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 xfrm_pol_put(xp);
1546
1547 return 0;
1548}
1549
1550static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1551{
1552 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1553 int i;
1554
1555 if (xp->xfrm_nr == 0)
1556 return 0;
1557
1558 for (i = 0; i < xp->xfrm_nr; i++) {
1559 struct xfrm_user_tmpl *up = &vec[i];
1560 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1561
Mathias Krause1f868402012-09-19 11:33:41 +00001562 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001564 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1566 up->reqid = kp->reqid;
1567 up->mode = kp->mode;
1568 up->share = kp->share;
1569 up->optional = kp->optional;
1570 up->aalgos = kp->aalgos;
1571 up->ealgos = kp->ealgos;
1572 up->calgos = kp->calgos;
1573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Thomas Grafc0144be2007-08-22 13:55:43 -07001575 return nla_put(skb, XFRMA_TMPL,
1576 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001577}
1578
Serge Hallyn0d681622006-07-24 23:30:44 -07001579static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1580{
1581 if (x->security) {
1582 return copy_sec_ctx(x->security, skb);
1583 }
1584 return 0;
1585}
1586
1587static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1588{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001589 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001590 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001591 return 0;
1592}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001593static inline size_t userpolicy_type_attrsize(void)
1594{
1595#ifdef CONFIG_XFRM_SUB_POLICY
1596 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1597#else
1598 return 0;
1599#endif
1600}
Serge Hallyn0d681622006-07-24 23:30:44 -07001601
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001602#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001603static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001604{
Thomas Grafc0144be2007-08-22 13:55:43 -07001605 struct xfrm_userpolicy_type upt = {
1606 .type = type,
1607 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001608
Thomas Grafc0144be2007-08-22 13:55:43 -07001609 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001610}
1611
1612#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001613static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001614{
1615 return 0;
1616}
1617#endif
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1620{
1621 struct xfrm_dump_info *sp = ptr;
1622 struct xfrm_userpolicy_info *p;
1623 struct sk_buff *in_skb = sp->in_skb;
1624 struct sk_buff *skb = sp->out_skb;
1625 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001626 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Eric W. Biederman15e47302012-09-07 20:12:54 +00001628 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001629 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1630 if (nlh == NULL)
1631 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Thomas Graf7b67c852007-08-22 13:53:52 -07001633 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001635 err = copy_to_user_tmpl(xp, skb);
1636 if (!err)
1637 err = copy_to_user_sec_ctx(xp, skb);
1638 if (!err)
1639 err = copy_to_user_policy_type(xp->type, skb);
1640 if (!err)
1641 err = xfrm_mark_put(skb, &xp->mark);
1642 if (err) {
1643 nlmsg_cancel(skb, nlh);
1644 return err;
1645 }
Thomas Graf98250692007-08-22 12:47:26 -07001646 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
Timo Teras4c563f72008-02-28 21:31:08 -08001650static int xfrm_dump_policy_done(struct netlink_callback *cb)
1651{
1652 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001653 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001654
Fan Du283bc9f2013-11-07 17:47:50 +08001655 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001656 return 0;
1657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1660{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001661 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001662 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 struct xfrm_dump_info info;
1664
Timo Teras4c563f72008-02-28 21:31:08 -08001665 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1666 sizeof(cb->args) - sizeof(cb->args[0]));
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 info.in_skb = cb->skb;
1669 info.out_skb = skb;
1670 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1671 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001672
1673 if (!cb->args[0]) {
1674 cb->args[0] = 1;
1675 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1676 }
1677
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001678 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 return skb->len;
1681}
1682
1683static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1684 struct xfrm_policy *xp,
1685 int dir, u32 seq)
1686{
1687 struct xfrm_dump_info info;
1688 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001689 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Thomas Graf7deb2262007-08-22 13:57:39 -07001691 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (!skb)
1693 return ERR_PTR(-ENOMEM);
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 info.in_skb = in_skb;
1696 info.out_skb = skb;
1697 info.nlmsg_seq = seq;
1698 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Mathias Krausec2546372012-09-14 09:58:32 +00001700 err = dump_one_policy(xp, dir, 0, &info);
1701 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001703 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705
1706 return skb;
1707}
1708
Christoph Hellwig22e70052007-01-02 15:22:30 -08001709static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001710 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001712 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct xfrm_policy *xp;
1714 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001715 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001717 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001719 struct xfrm_mark m;
1720 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Thomas Graf7b67c852007-08-22 13:53:52 -07001722 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1724
Thomas Graf35a7aa02007-08-22 14:00:40 -07001725 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001726 if (err)
1727 return err;
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 err = verify_policy_dir(p->dir);
1730 if (err)
1731 return err;
1732
1733 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001734 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001735 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001736 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001737 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001738
Thomas Graf35a7aa02007-08-22 14:00:40 -07001739 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001740 if (err)
1741 return err;
1742
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001743 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001744 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001745 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001746
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001747 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001748 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001749 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001750 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001751 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001752 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001753 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (xp == NULL)
1756 return -ENOENT;
1757
1758 if (!delete) {
1759 struct sk_buff *resp_skb;
1760
1761 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1762 if (IS_ERR(resp_skb)) {
1763 err = PTR_ERR(resp_skb);
1764 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001765 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001766 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001768 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001769 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001770
1771 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001772 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001773
Herbert Xue7443892005-06-18 22:44:18 -07001774 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001775 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001776 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001777 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001778 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001781out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001782 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001783 if (delete && err == 0)
1784 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return err;
1786}
1787
Christoph Hellwig22e70052007-01-02 15:22:30 -08001788static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001789 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001791 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001792 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001793 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001794 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Tetsuo Handa2e710292014-04-22 21:48:30 +09001796 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001797 if (err) {
1798 if (err == -ESRCH) /* empty table */
1799 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001800 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001801 }
Herbert Xubf088672005-06-18 22:44:00 -07001802 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001803 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001804 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001805 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001806 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001807 km_state_notify(NULL, &c);
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return 0;
1810}
1811
Steffen Klassertd8647b72011-03-08 00:10:27 +00001812static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001813{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001814 size_t replay_size = x->replay_esn ?
1815 xfrm_replay_state_esn_len(x->replay_esn) :
1816 sizeof(struct xfrm_replay_state);
1817
Thomas Graf7deb2262007-08-22 13:57:39 -07001818 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001819 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001820 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001821 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001822 + nla_total_size(4) /* XFRM_AE_RTHR */
1823 + nla_total_size(4); /* XFRM_AE_ETHR */
1824}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001825
David S. Miller214e0052011-02-24 00:02:38 -05001826static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001827{
1828 struct xfrm_aevent_id *id;
1829 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001830 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001831
Eric W. Biederman15e47302012-09-07 20:12:54 +00001832 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001833 if (nlh == NULL)
1834 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001835
Thomas Graf7b67c852007-08-22 13:53:52 -07001836 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001837 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001838 id->sa_id.spi = x->id.spi;
1839 id->sa_id.family = x->props.family;
1840 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001841 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001842 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001843 id->flags = c->data.aevent;
1844
David S. Millerd0fde792012-03-29 04:02:26 -04001845 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001846 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1847 xfrm_replay_state_esn_len(x->replay_esn),
1848 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001849 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001850 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1851 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001852 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001853 if (err)
1854 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001855 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1856 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001857 if (err)
1858 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001859
David S. Miller1d1e34d2012-06-27 21:57:03 -07001860 if (id->flags & XFRM_AE_RTHR) {
1861 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1862 if (err)
1863 goto out_cancel;
1864 }
1865 if (id->flags & XFRM_AE_ETHR) {
1866 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1867 x->replay_maxage * 10 / HZ);
1868 if (err)
1869 goto out_cancel;
1870 }
1871 err = xfrm_mark_put(skb, &x->mark);
1872 if (err)
1873 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001874
Johannes Berg053c0952015-01-16 22:09:00 +01001875 nlmsg_end(skb, nlh);
1876 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001877
David S. Miller1d1e34d2012-06-27 21:57:03 -07001878out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001879 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001880 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001881}
1882
Christoph Hellwig22e70052007-01-02 15:22:30 -08001883static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001884 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001885{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001886 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001887 struct xfrm_state *x;
1888 struct sk_buff *r_skb;
1889 int err;
1890 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001891 u32 mark;
1892 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001893 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001894 struct xfrm_usersa_id *id = &p->sa_id;
1895
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001896 mark = xfrm_mark_get(attrs, &m);
1897
1898 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001899 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001900 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001901
1902 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1903 if (r_skb == NULL) {
1904 xfrm_state_put(x);
1905 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001906 }
1907
1908 /*
1909 * XXX: is this lock really needed - none of the other
1910 * gets lock (the concern is things getting updated
1911 * while we are still reading) - jhs
1912 */
1913 spin_lock_bh(&x->lock);
1914 c.data.aevent = p->flags;
1915 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001916 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001917
1918 if (build_aevent(r_skb, x, &c) < 0)
1919 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001920 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001921 spin_unlock_bh(&x->lock);
1922 xfrm_state_put(x);
1923 return err;
1924}
1925
Christoph Hellwig22e70052007-01-02 15:22:30 -08001926static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001927 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001928{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001929 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001930 struct xfrm_state *x;
1931 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001932 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001933 u32 mark = 0;
1934 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001935 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001936 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001937 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001938 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02001939 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1940 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001941
Michael Rossberg4e077232015-09-29 11:25:08 +02001942 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001943 return err;
1944
1945 /* pedantic mode - thou shalt sayeth replaceth */
1946 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1947 return err;
1948
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001949 mark = xfrm_mark_get(attrs, &m);
1950
1951 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 -08001952 if (x == NULL)
1953 return -ESRCH;
1954
1955 if (x->km.state != XFRM_STATE_VALID)
1956 goto out;
1957
Steffen Klassert4479ff72013-09-09 09:39:01 +02001958 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001959 if (err)
1960 goto out;
1961
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001962 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001963 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001964 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001965
1966 c.event = nlh->nlmsg_type;
1967 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001968 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001969 c.data.aevent = XFRM_AE_CU;
1970 km_state_notify(x, &c);
1971 err = 0;
1972out:
1973 xfrm_state_put(x);
1974 return err;
1975}
1976
Christoph Hellwig22e70052007-01-02 15:22:30 -08001977static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001978 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001980 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001981 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001982 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001983 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001984
Thomas Graf35a7aa02007-08-22 14:00:40 -07001985 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001986 if (err)
1987 return err;
1988
Tetsuo Handa2e710292014-04-22 21:48:30 +09001989 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001990 if (err) {
1991 if (err == -ESRCH) /* empty table */
1992 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001993 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001994 }
1995
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001996 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001997 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001998 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001999 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002000 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002001 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return 0;
2003}
2004
Christoph Hellwig22e70052007-01-02 15:22:30 -08002005static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002006 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002007{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002008 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002009 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002010 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002011 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002012 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002013 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002014 struct xfrm_mark m;
2015 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002016
Thomas Graf35a7aa02007-08-22 14:00:40 -07002017 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002018 if (err)
2019 return err;
2020
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002021 err = verify_policy_dir(p->dir);
2022 if (err)
2023 return err;
2024
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002025 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002026 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002027 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002028 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002029 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002030
Thomas Graf35a7aa02007-08-22 14:00:40 -07002031 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002032 if (err)
2033 return err;
2034
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002035 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002036 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002037 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002038
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002039 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002040 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002041 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002042 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002043 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002044 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002045 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002046 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002047 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002048 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002049
Timo Teräsea2dea92010-03-31 00:17:05 +00002050 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002051 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002052
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002053 err = 0;
2054 if (up->hard) {
2055 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002056 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002057 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002058 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002059
2060out:
2061 xfrm_pol_put(xp);
2062 return err;
2063}
2064
Christoph Hellwig22e70052007-01-02 15:22:30 -08002065static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002066 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002067{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002068 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002069 struct xfrm_state *x;
2070 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002071 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002072 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002073 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002074 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002075
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002076 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 -08002077
David S. Miller3a765aa2007-02-26 14:52:21 -08002078 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002079 if (x == NULL)
2080 return err;
2081
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002082 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002083 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002084 if (x->km.state != XFRM_STATE_VALID)
2085 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002086 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002087
Joy Latten161a09e2006-11-27 13:11:54 -06002088 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002089 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002090 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002091 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002092 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002093out:
2094 spin_unlock_bh(&x->lock);
2095 xfrm_state_put(x);
2096 return err;
2097}
2098
Christoph Hellwig22e70052007-01-02 15:22:30 -08002099static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002100 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002101{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002102 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002103 struct xfrm_policy *xp;
2104 struct xfrm_user_tmpl *ut;
2105 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002106 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002107 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002108
Thomas Graf7b67c852007-08-22 13:53:52 -07002109 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002110 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002111 int err = -ENOMEM;
2112
2113 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002114 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002115
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002116 xfrm_mark_get(attrs, &mark);
2117
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002118 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002119 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002120 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002121
2122 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002123 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002124 if (!xp)
2125 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002126
2127 memcpy(&x->id, &ua->id, sizeof(ua->id));
2128 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2129 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002130 xp->mark.m = x->mark.m = mark.m;
2131 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002132 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002133 /* extract the templates and for each call km_key */
2134 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2135 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2136 memcpy(&x->id, &t->id, sizeof(x->id));
2137 x->props.mode = t->mode;
2138 x->props.reqid = t->reqid;
2139 x->props.family = ut->family;
2140 t->aalgos = ua->aalgos;
2141 t->ealgos = ua->ealgos;
2142 t->calgos = ua->calgos;
2143 err = km_query(x, t, xp);
2144
2145 }
2146
2147 kfree(x);
2148 kfree(xp);
2149
2150 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002151
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002152free_state:
2153 kfree(x);
2154nomem:
2155 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002156}
2157
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002158#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002159static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002160 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002161 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002162{
Thomas Graf5424f322007-08-22 14:01:33 -07002163 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002164 struct xfrm_user_migrate *um;
2165 int i, num_migrate;
2166
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002167 if (k != NULL) {
2168 struct xfrm_user_kmaddress *uk;
2169
2170 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2171 memcpy(&k->local, &uk->local, sizeof(k->local));
2172 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2173 k->family = uk->family;
2174 k->reserved = uk->reserved;
2175 }
2176
Thomas Graf5424f322007-08-22 14:01:33 -07002177 um = nla_data(rt);
2178 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002179
2180 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2181 return -EINVAL;
2182
2183 for (i = 0; i < num_migrate; i++, um++, ma++) {
2184 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2185 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2186 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2187 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2188
2189 ma->proto = um->proto;
2190 ma->mode = um->mode;
2191 ma->reqid = um->reqid;
2192
2193 ma->old_family = um->old_family;
2194 ma->new_family = um->new_family;
2195 }
2196
2197 *num = i;
2198 return 0;
2199}
2200
2201static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002202 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002203{
Thomas Graf7b67c852007-08-22 13:53:52 -07002204 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002205 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002206 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002207 u8 type;
2208 int err;
2209 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002210 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002211
Thomas Graf35a7aa02007-08-22 14:00:40 -07002212 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002213 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002214
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002215 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2216
Thomas Graf5424f322007-08-22 14:01:33 -07002217 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002218 if (err)
2219 return err;
2220
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002221 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002222 if (err)
2223 return err;
2224
2225 if (!n)
2226 return 0;
2227
Fan Du8d549c42013-11-07 17:47:49 +08002228 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002229
2230 return 0;
2231}
2232#else
2233static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002234 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002235{
2236 return -ENOPROTOOPT;
2237}
2238#endif
2239
2240#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002241static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002242{
2243 struct xfrm_user_migrate um;
2244
2245 memset(&um, 0, sizeof(um));
2246 um.proto = m->proto;
2247 um.mode = m->mode;
2248 um.reqid = m->reqid;
2249 um.old_family = m->old_family;
2250 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2251 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2252 um.new_family = m->new_family;
2253 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2254 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2255
Thomas Grafc0144be2007-08-22 13:55:43 -07002256 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002257}
2258
David S. Miller183cad12011-02-24 00:28:01 -05002259static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002260{
2261 struct xfrm_user_kmaddress uk;
2262
2263 memset(&uk, 0, sizeof(uk));
2264 uk.family = k->family;
2265 uk.reserved = k->reserved;
2266 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002267 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002268
2269 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2270}
2271
2272static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002273{
2274 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002275 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2276 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2277 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002278}
2279
David S. Miller183cad12011-02-24 00:28:01 -05002280static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2281 int num_migrate, const struct xfrm_kmaddress *k,
2282 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002283{
David S. Miller183cad12011-02-24 00:28:01 -05002284 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002285 struct xfrm_userpolicy_id *pol_id;
2286 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002287 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002288
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002289 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2290 if (nlh == NULL)
2291 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002292
Thomas Graf7b67c852007-08-22 13:53:52 -07002293 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002294 /* copy data from selector, dir, and type to the pol_id */
2295 memset(pol_id, 0, sizeof(*pol_id));
2296 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2297 pol_id->dir = dir;
2298
David S. Miller1d1e34d2012-06-27 21:57:03 -07002299 if (k != NULL) {
2300 err = copy_to_user_kmaddress(k, skb);
2301 if (err)
2302 goto out_cancel;
2303 }
2304 err = copy_to_user_policy_type(type, skb);
2305 if (err)
2306 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002307 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002308 err = copy_to_user_migrate(mp, skb);
2309 if (err)
2310 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002311 }
2312
Johannes Berg053c0952015-01-16 22:09:00 +01002313 nlmsg_end(skb, nlh);
2314 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002315
2316out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002317 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002318 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002319}
2320
David S. Miller183cad12011-02-24 00:28:01 -05002321static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2322 const struct xfrm_migrate *m, int num_migrate,
2323 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002324{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002325 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002326 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002327
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002328 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002329 if (skb == NULL)
2330 return -ENOMEM;
2331
2332 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002333 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002334 BUG();
2335
Michal Kubecek21ee5432014-06-03 10:26:06 +02002336 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002337}
2338#else
David S. Miller183cad12011-02-24 00:28:01 -05002339static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2340 const struct xfrm_migrate *m, int num_migrate,
2341 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002342{
2343 return -ENOPROTOOPT;
2344}
2345#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002346
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002347#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002348
2349static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002350 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002351 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2352 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2353 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2354 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2355 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2356 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002357 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002358 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002359 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002360 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002361 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002362 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002363 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002364 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2365 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002366 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002367 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002368 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002369 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002370 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371};
2372
Thomas Graf492b5582005-05-03 14:26:40 -07002373#undef XMSGSIZE
2374
Thomas Grafcf5cb792007-08-22 13:59:04 -07002375static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002376 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2377 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2378 [XFRMA_LASTUSED] = { .type = NLA_U64},
2379 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002380 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002381 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2382 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2383 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2384 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2385 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2386 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2387 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2388 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2389 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2390 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2391 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2392 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2393 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2394 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002395 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002396 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002397 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002398 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002399 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002400 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002401 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002402};
2403
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002404static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2405 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2406 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2407};
2408
Mathias Krause05600a72013-02-24 14:10:27 +01002409static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002410 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002412 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002413 const struct nla_policy *nla_pol;
2414 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002415} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2416 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2417 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2418 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002419 .dump = xfrm_dump_sa,
2420 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002421 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2422 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2423 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002424 .dump = xfrm_dump_policy,
2425 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002426 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002427 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002428 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002429 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2430 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002431 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002432 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2433 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002434 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2435 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002436 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002437 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002438 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2439 .nla_pol = xfrma_spd_policy,
2440 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002441 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442};
2443
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002444static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002446 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002447 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002448 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002449 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Fan Du74005992015-01-27 17:00:29 +08002451#ifdef CONFIG_COMPAT
Andy Lutomirski2bf8c472016-03-22 14:25:10 -07002452 if (in_compat_syscall())
Yi Zhao83e2d052016-11-29 18:09:01 +08002453 return -EOPNOTSUPP;
Fan Du74005992015-01-27 17:00:29 +08002454#endif
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 type -= XFRM_MSG_BASE;
2461 link = &xfrm_dispatch[type];
2462
2463 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002464 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002465 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Thomas Graf492b5582005-05-03 14:26:40 -07002467 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2468 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002469 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002471 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002473 {
2474 struct netlink_dump_control c = {
2475 .dump = link->dump,
2476 .done = link->done,
2477 };
2478 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002482 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2483 link->nla_max ? : XFRMA_MAX,
2484 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002485 if (err < 0)
2486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Thomas Graf5424f322007-08-22 14:01:33 -07002491 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002494static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Fan Du283bc9f2013-11-07 17:47:50 +08002496 struct net *net = sock_net(skb->sk);
2497
2498 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002499 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002500 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Thomas Graf7deb2262007-08-22 13:57:39 -07002503static inline size_t xfrm_expire_msgsize(void)
2504{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002505 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2506 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002507}
2508
David S. Miller214e0052011-02-24 00:02:38 -05002509static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 struct xfrm_user_expire *ue;
2512 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002513 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Eric W. Biederman15e47302012-09-07 20:12:54 +00002515 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002516 if (nlh == NULL)
2517 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Thomas Graf7b67c852007-08-22 13:53:52 -07002519 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002521 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
David S. Miller1d1e34d2012-06-27 21:57:03 -07002523 err = xfrm_mark_put(skb, &x->mark);
2524 if (err)
2525 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002526
Johannes Berg053c0952015-01-16 22:09:00 +01002527 nlmsg_end(skb, nlh);
2528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
David S. Miller214e0052011-02-24 00:02:38 -05002531static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002533 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 struct sk_buff *skb;
2535
Thomas Graf7deb2262007-08-22 13:57:39 -07002536 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (skb == NULL)
2538 return -ENOMEM;
2539
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002540 if (build_expire(skb, x, c) < 0) {
2541 kfree_skb(skb);
2542 return -EMSGSIZE;
2543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Michal Kubecek21ee5432014-06-03 10:26:06 +02002545 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
David S. Miller214e0052011-02-24 00:02:38 -05002548static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002549{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002550 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002551 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002552
Steffen Klassertd8647b72011-03-08 00:10:27 +00002553 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002554 if (skb == NULL)
2555 return -ENOMEM;
2556
2557 if (build_aevent(skb, x, c) < 0)
2558 BUG();
2559
Michal Kubecek21ee5432014-06-03 10:26:06 +02002560 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002561}
2562
David S. Miller214e0052011-02-24 00:02:38 -05002563static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002564{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002565 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002566 struct xfrm_usersa_flush *p;
2567 struct nlmsghdr *nlh;
2568 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002569 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002570
Thomas Graf7deb2262007-08-22 13:57:39 -07002571 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002572 if (skb == NULL)
2573 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002574
Eric W. Biederman15e47302012-09-07 20:12:54 +00002575 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002576 if (nlh == NULL) {
2577 kfree_skb(skb);
2578 return -EMSGSIZE;
2579 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002580
Thomas Graf7b67c852007-08-22 13:53:52 -07002581 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002582 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002583
Thomas Graf98250692007-08-22 12:47:26 -07002584 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002585
Michal Kubecek21ee5432014-06-03 10:26:06 +02002586 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002587}
2588
Thomas Graf7deb2262007-08-22 13:57:39 -07002589static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002590{
Thomas Graf7deb2262007-08-22 13:57:39 -07002591 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002592 if (x->aead)
2593 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002594 if (x->aalg) {
2595 l += nla_total_size(sizeof(struct xfrm_algo) +
2596 (x->aalg->alg_key_len + 7) / 8);
2597 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2598 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002599 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002600 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002601 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002602 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002603 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002604 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002605 if (x->tfcpad)
2606 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002607 if (x->replay_esn)
2608 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002609 else
2610 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002611 if (x->security)
2612 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2613 x->security->ctx_len);
2614 if (x->coaddr)
2615 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002616 if (x->props.extra_flags)
2617 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002618
Herbert Xud26f3982007-11-13 21:47:08 -08002619 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002620 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002621
2622 return l;
2623}
2624
David S. Miller214e0052011-02-24 00:02:38 -05002625static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002626{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002627 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002628 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002629 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002630 struct nlmsghdr *nlh;
2631 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002632 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002633 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002634
2635 headlen = sizeof(*p);
2636 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002637 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002638 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002639 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002640 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002641 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002642
Thomas Graf7deb2262007-08-22 13:57:39 -07002643 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002644 if (skb == NULL)
2645 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002646
Eric W. Biederman15e47302012-09-07 20:12:54 +00002647 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002648 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002649 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002650 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002651
Thomas Graf7b67c852007-08-22 13:53:52 -07002652 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002653 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002654 struct nlattr *attr;
2655
Thomas Graf7b67c852007-08-22 13:53:52 -07002656 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002657 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2658 id->spi = x->id.spi;
2659 id->family = x->props.family;
2660 id->proto = x->id.proto;
2661
Thomas Grafc0144be2007-08-22 13:55:43 -07002662 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002663 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002664 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002665 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002666
2667 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002668 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002669 err = copy_to_user_state_extra(x, p, skb);
2670 if (err)
2671 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002672
Thomas Graf98250692007-08-22 12:47:26 -07002673 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002674
Michal Kubecek21ee5432014-06-03 10:26:06 +02002675 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676
David S. Miller1d1e34d2012-06-27 21:57:03 -07002677out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002678 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002679 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002680}
2681
David S. Miller214e0052011-02-24 00:02:38 -05002682static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002683{
2684
2685 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002686 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002687 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002688 case XFRM_MSG_NEWAE:
2689 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002690 case XFRM_MSG_DELSA:
2691 case XFRM_MSG_UPDSA:
2692 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002693 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002694 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002695 return xfrm_notify_sa_flush(c);
2696 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002697 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2698 c->event);
2699 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002700 }
2701
2702 return 0;
2703
2704}
2705
Thomas Graf7deb2262007-08-22 13:57:39 -07002706static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2707 struct xfrm_policy *xp)
2708{
2709 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2710 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002711 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002712 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2713 + userpolicy_type_attrsize();
2714}
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002717 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002719 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct xfrm_user_acquire *ua;
2721 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002722 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002724 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2725 if (nlh == NULL)
2726 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Thomas Graf7b67c852007-08-22 13:53:52 -07002728 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 memcpy(&ua->id, &x->id, sizeof(ua->id));
2730 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2731 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002732 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 ua->aalgos = xt->aalgos;
2734 ua->ealgos = xt->ealgos;
2735 ua->calgos = xt->calgos;
2736 ua->seq = x->km.seq = seq;
2737
David S. Miller1d1e34d2012-06-27 21:57:03 -07002738 err = copy_to_user_tmpl(xp, skb);
2739 if (!err)
2740 err = copy_to_user_state_sec_ctx(x, skb);
2741 if (!err)
2742 err = copy_to_user_policy_type(xp->type, skb);
2743 if (!err)
2744 err = xfrm_mark_put(skb, &xp->mark);
2745 if (err) {
2746 nlmsg_cancel(skb, nlh);
2747 return err;
2748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Johannes Berg053c0952015-01-16 22:09:00 +01002750 nlmsg_end(skb, nlh);
2751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
2753
2754static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002755 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002757 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Thomas Graf7deb2262007-08-22 13:57:39 -07002760 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (skb == NULL)
2762 return -ENOMEM;
2763
Fan Du65e07362012-08-15 10:13:47 +08002764 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 BUG();
2766
Michal Kubecek21ee5432014-06-03 10:26:06 +02002767 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770/* User gives us xfrm_user_policy_info followed by an array of 0
2771 * or more templates.
2772 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002773static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 u8 *data, int len, int *dir)
2775{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002776 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2778 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2779 struct xfrm_policy *xp;
2780 int nr;
2781
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002782 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 case AF_INET:
2784 if (opt != IP_XFRM_POLICY) {
2785 *dir = -EOPNOTSUPP;
2786 return NULL;
2787 }
2788 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002789#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 case AF_INET6:
2791 if (opt != IPV6_XFRM_POLICY) {
2792 *dir = -EOPNOTSUPP;
2793 return NULL;
2794 }
2795 break;
2796#endif
2797 default:
2798 *dir = -EINVAL;
2799 return NULL;
2800 }
2801
2802 *dir = -EINVAL;
2803
2804 if (len < sizeof(*p) ||
2805 verify_newpolicy_info(p))
2806 return NULL;
2807
2808 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002809 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return NULL;
2811
Herbert Xua4f1bac2005-07-26 15:43:17 -07002812 if (p->dir > XFRM_POLICY_OUT)
2813 return NULL;
2814
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002815 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (xp == NULL) {
2817 *dir = -ENOBUFS;
2818 return NULL;
2819 }
2820
2821 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002822 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 copy_templates(xp, ut, nr);
2824
2825 *dir = p->dir;
2826
2827 return xp;
2828}
2829
Thomas Graf7deb2262007-08-22 13:57:39 -07002830static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2831{
2832 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2833 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2834 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002835 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002836 + userpolicy_type_attrsize();
2837}
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002840 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
2842 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002843 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002844 struct nlmsghdr *nlh;
2845 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Eric W. Biederman15e47302012-09-07 20:12:54 +00002847 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002848 if (nlh == NULL)
2849 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Thomas Graf7b67c852007-08-22 13:53:52 -07002851 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002853 err = copy_to_user_tmpl(xp, skb);
2854 if (!err)
2855 err = copy_to_user_sec_ctx(xp, skb);
2856 if (!err)
2857 err = copy_to_user_policy_type(xp->type, skb);
2858 if (!err)
2859 err = xfrm_mark_put(skb, &xp->mark);
2860 if (err) {
2861 nlmsg_cancel(skb, nlh);
2862 return err;
2863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 upe->hard = !!hard;
2865
Johannes Berg053c0952015-01-16 22:09:00 +01002866 nlmsg_end(skb, nlh);
2867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
David S. Miller214e0052011-02-24 00:02:38 -05002870static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002872 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Thomas Graf7deb2262007-08-22 13:57:39 -07002875 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (skb == NULL)
2877 return -ENOMEM;
2878
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002879 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 BUG();
2881
Michal Kubecek21ee5432014-06-03 10:26:06 +02002882 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
David S. Miller214e0052011-02-24 00:02:38 -05002885static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002886{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002887 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002888 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002889 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002890 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002891 struct nlmsghdr *nlh;
2892 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002893 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002894
2895 headlen = sizeof(*p);
2896 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002897 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002898 headlen = sizeof(*id);
2899 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002900 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002901 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002902 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002903
Thomas Graf7deb2262007-08-22 13:57:39 -07002904 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002905 if (skb == NULL)
2906 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002907
Eric W. Biederman15e47302012-09-07 20:12:54 +00002908 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002909 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002910 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002911 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002912
Thomas Graf7b67c852007-08-22 13:53:52 -07002913 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002914 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002915 struct nlattr *attr;
2916
Thomas Graf7b67c852007-08-22 13:53:52 -07002917 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002918 memset(id, 0, sizeof(*id));
2919 id->dir = dir;
2920 if (c->data.byid)
2921 id->index = xp->index;
2922 else
2923 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2924
Thomas Grafc0144be2007-08-22 13:55:43 -07002925 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002926 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002927 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002928 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002929
2930 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002931 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002932
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002933 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002934 err = copy_to_user_tmpl(xp, skb);
2935 if (!err)
2936 err = copy_to_user_policy_type(xp->type, skb);
2937 if (!err)
2938 err = xfrm_mark_put(skb, &xp->mark);
2939 if (err)
2940 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002941
Thomas Graf98250692007-08-22 12:47:26 -07002942 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002943
Michal Kubecek21ee5432014-06-03 10:26:06 +02002944 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002945
David S. Miller1d1e34d2012-06-27 21:57:03 -07002946out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002947 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002948 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002949}
2950
David S. Miller214e0052011-02-24 00:02:38 -05002951static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002952{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002953 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002954 struct nlmsghdr *nlh;
2955 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002956 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002957
Thomas Graf7deb2262007-08-22 13:57:39 -07002958 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002959 if (skb == NULL)
2960 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002961
Eric W. Biederman15e47302012-09-07 20:12:54 +00002962 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002963 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002964 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002965 goto out_free_skb;
2966 err = copy_to_user_policy_type(c->data.type, skb);
2967 if (err)
2968 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002969
Thomas Graf98250692007-08-22 12:47:26 -07002970 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002971
Michal Kubecek21ee5432014-06-03 10:26:06 +02002972 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002973
David S. Miller1d1e34d2012-06-27 21:57:03 -07002974out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002975 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002976 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002977}
2978
David S. Miller214e0052011-02-24 00:02:38 -05002979static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002980{
2981
2982 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002983 case XFRM_MSG_NEWPOLICY:
2984 case XFRM_MSG_UPDPOLICY:
2985 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002986 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002987 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002988 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002989 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002990 return xfrm_exp_policy_notify(xp, dir, c);
2991 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002992 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2993 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002994 }
2995
2996 return 0;
2997
2998}
2999
Thomas Graf7deb2262007-08-22 13:57:39 -07003000static inline size_t xfrm_report_msgsize(void)
3001{
3002 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3003}
3004
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003005static int build_report(struct sk_buff *skb, u8 proto,
3006 struct xfrm_selector *sel, xfrm_address_t *addr)
3007{
3008 struct xfrm_user_report *ur;
3009 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003010
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003011 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3012 if (nlh == NULL)
3013 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003014
Thomas Graf7b67c852007-08-22 13:53:52 -07003015 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003016 ur->proto = proto;
3017 memcpy(&ur->sel, sel, sizeof(ur->sel));
3018
David S. Miller1d1e34d2012-06-27 21:57:03 -07003019 if (addr) {
3020 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3021 if (err) {
3022 nlmsg_cancel(skb, nlh);
3023 return err;
3024 }
3025 }
Johannes Berg053c0952015-01-16 22:09:00 +01003026 nlmsg_end(skb, nlh);
3027 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003028}
3029
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003030static int xfrm_send_report(struct net *net, u8 proto,
3031 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003032{
3033 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003034
Thomas Graf7deb2262007-08-22 13:57:39 -07003035 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003036 if (skb == NULL)
3037 return -ENOMEM;
3038
3039 if (build_report(skb, proto, sel, addr) < 0)
3040 BUG();
3041
Michal Kubecek21ee5432014-06-03 10:26:06 +02003042 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003043}
3044
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003045static inline size_t xfrm_mapping_msgsize(void)
3046{
3047 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3048}
3049
3050static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3051 xfrm_address_t *new_saddr, __be16 new_sport)
3052{
3053 struct xfrm_user_mapping *um;
3054 struct nlmsghdr *nlh;
3055
3056 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3057 if (nlh == NULL)
3058 return -EMSGSIZE;
3059
3060 um = nlmsg_data(nlh);
3061
3062 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3063 um->id.spi = x->id.spi;
3064 um->id.family = x->props.family;
3065 um->id.proto = x->id.proto;
3066 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3067 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3068 um->new_sport = new_sport;
3069 um->old_sport = x->encap->encap_sport;
3070 um->reqid = x->props.reqid;
3071
Johannes Berg053c0952015-01-16 22:09:00 +01003072 nlmsg_end(skb, nlh);
3073 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003074}
3075
3076static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3077 __be16 sport)
3078{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003079 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003080 struct sk_buff *skb;
3081
3082 if (x->id.proto != IPPROTO_ESP)
3083 return -EINVAL;
3084
3085 if (!x->encap)
3086 return -EINVAL;
3087
3088 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3089 if (skb == NULL)
3090 return -ENOMEM;
3091
3092 if (build_mapping(skb, x, ipaddr, sport) < 0)
3093 BUG();
3094
Michal Kubecek21ee5432014-06-03 10:26:06 +02003095 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003096}
3097
Horia Geanta0f245582014-02-12 16:20:06 +02003098static bool xfrm_is_alive(const struct km_event *c)
3099{
3100 return (bool)xfrm_acquire_is_on(c->net);
3101}
3102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103static struct xfrm_mgr netlink_mgr = {
3104 .id = "netlink",
3105 .notify = xfrm_send_state_notify,
3106 .acquire = xfrm_send_acquire,
3107 .compile_policy = xfrm_compile_policy,
3108 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003109 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003110 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003111 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003112 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113};
3114
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003115static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Patrick McHardybe336902006-03-20 22:40:54 -08003117 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003118 struct netlink_kernel_cfg cfg = {
3119 .groups = XFRMNLGRP_MAX,
3120 .input = xfrm_netlink_rcv,
3121 };
Patrick McHardybe336902006-03-20 22:40:54 -08003122
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003123 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003124 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003126 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003127 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return 0;
3129}
3130
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003131static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003132{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003133 struct net *net;
3134 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003135 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003136 synchronize_net();
3137 list_for_each_entry(net, net_exit_list, exit_list)
3138 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003139}
3140
3141static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003142 .init = xfrm_user_net_init,
3143 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003144};
3145
3146static int __init xfrm_user_init(void)
3147{
3148 int rv;
3149
3150 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3151
3152 rv = register_pernet_subsys(&xfrm_user_net_ops);
3153 if (rv < 0)
3154 return rv;
3155 rv = xfrm_register_km(&netlink_mgr);
3156 if (rv < 0)
3157 unregister_pernet_subsys(&xfrm_user_net_ops);
3158 return rv;
3159}
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161static void __exit xfrm_user_exit(void)
3162{
3163 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003164 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
3167module_init(xfrm_user_init);
3168module_exit(xfrm_user_exit);
3169MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003170MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003171