blob: ff641b2e1577bdd7c919e7ecab1ecbbe972f1c10 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Sowmini Varadhane33d4f12015-10-21 11:48:25 -040034#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Graf5424f322007-08-22 14:01:33 -070036static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Thomas Graf5424f322007-08-22 14:01:33 -070038 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct xfrm_algo *algp;
40
41 if (!rt)
42 return 0;
43
Thomas Graf5424f322007-08-22 14:01:33 -070044 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080045 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070046 return -EINVAL;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 switch (type) {
49 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53
54 default:
55 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
59 return 0;
60}
61
Martin Willi4447bb32009-11-25 00:29:52 +000062static int verify_auth_trunc(struct nlattr **attrs)
63{
64 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
65 struct xfrm_algo_auth *algp;
66
67 if (!rt)
68 return 0;
69
70 algp = nla_data(rt);
71 if (nla_len(rt) < xfrm_alg_auth_len(algp))
72 return -EINVAL;
73
74 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 return 0;
76}
77
Herbert Xu1a6509d2008-01-28 19:37:29 -080078static int verify_aead(struct nlattr **attrs)
79{
80 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
81 struct xfrm_algo_aead *algp;
82
83 if (!rt)
84 return 0;
85
86 algp = nla_data(rt);
87 if (nla_len(rt) < aead_len(algp))
88 return -EINVAL;
89
90 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
91 return 0;
92}
93
Thomas Graf5424f322007-08-22 14:01:33 -070094static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070095 xfrm_address_t **addrp)
96{
Thomas Graf5424f322007-08-22 14:01:33 -070097 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070098
Thomas Grafcf5cb792007-08-22 13:59:04 -070099 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700100 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700101}
Trent Jaegerdf718372005-12-13 23:12:27 -0800102
Thomas Graf5424f322007-08-22 14:01:33 -0700103static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800104{
Thomas Graf5424f322007-08-22 14:01:33 -0700105 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800106 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800107
108 if (!rt)
109 return 0;
110
Thomas Graf5424f322007-08-22 14:01:33 -0700111 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700112 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800113 return -EINVAL;
114
115 return 0;
116}
117
Steffen Klassertd8647b72011-03-08 00:10:27 +0000118static inline int verify_replay(struct xfrm_usersa_info *p,
119 struct nlattr **attrs)
120{
121 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000122 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000123
124 if (!rt)
Florian Westphal0355a9f2018-02-12 14:42:01 +0100125 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
126
127 rs = nla_data(rt);
128
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
131
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000135
Fan Du01714102014-01-18 09:54:28 +0800136 /* As only ESP and AH support ESN feature. */
137 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000138 return -EINVAL;
139
Steffen Klassertd8647b72011-03-08 00:10:27 +0000140 if (p->replay_window != 0)
141 return -EINVAL;
142
143 return 0;
144}
Trent Jaegerdf718372005-12-13 23:12:27 -0800145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700147 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 int err;
150
151 err = -EINVAL;
152 switch (p->family) {
153 case AF_INET:
Anirudh Gupta92a63c22019-05-21 20:59:47 +0530154 break;
155
156 case AF_INET6:
157#if IS_ENABLED(CONFIG_IPV6)
158 break;
159#else
160 err = -EAFNOSUPPORT;
161 goto out;
162#endif
163
164 default:
165 goto out;
166 }
167
168 switch (p->sel.family) {
Nicolas Dichtel2d0dbd02019-06-14 11:13:55 +0200169 case AF_UNSPEC:
170 break;
171
Anirudh Gupta92a63c22019-05-21 20:59:47 +0530172 case AF_INET:
Steffen Klassert89cb86e2018-08-01 13:45:11 +0200173 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
174 goto out;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177
178 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000179#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert89cb86e2018-08-01 13:45:11 +0200180 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
181 goto out;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184#else
185 err = -EAFNOSUPPORT;
186 goto out;
187#endif
188
189 default:
190 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 err = -EINVAL;
194 switch (p->id.proto) {
195 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000196 if ((!attrs[XFRMA_ALG_AUTH] &&
197 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800198 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700199 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000200 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200201 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto out;
203 break;
204
205 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800206 if (attrs[XFRMA_ALG_COMP])
207 goto out;
208 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000209 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800210 !attrs[XFRMA_ALG_CRYPT] &&
211 !attrs[XFRMA_ALG_AEAD])
212 goto out;
213 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000214 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800215 attrs[XFRMA_ALG_CRYPT]) &&
216 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000218 if (attrs[XFRMA_TFCPAD] &&
219 p->mode != XFRM_MODE_TUNNEL)
220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
222
223 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700224 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800225 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700226 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000227 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000228 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200229 attrs[XFRMA_TFCPAD] ||
230 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto out;
232 break;
233
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000234#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700235 case IPPROTO_DSTOPTS:
236 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700237 if (attrs[XFRMA_ALG_COMP] ||
238 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000239 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800240 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700241 attrs[XFRMA_ALG_CRYPT] ||
242 attrs[XFRMA_ENCAP] ||
243 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000244 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700245 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700246 goto out;
247 break;
248#endif
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 default:
251 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Herbert Xu1a6509d2008-01-28 19:37:29 -0800254 if ((err = verify_aead(attrs)))
255 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000256 if ((err = verify_auth_trunc(attrs)))
257 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700258 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700260 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700262 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700264 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800265 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000266 if ((err = verify_replay(p, attrs)))
267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 err = -EINVAL;
270 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700271 case XFRM_MODE_TRANSPORT:
272 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700273 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700274 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276
277 default:
278 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 err = 0;
282
283out:
284 return err;
285}
286
287static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800288 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700289 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct xfrm_algo *p, *ualg;
292 struct xfrm_algo_desc *algo;
293
294 if (!rta)
295 return 0;
296
Thomas Graf5424f322007-08-22 14:01:33 -0700297 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 algo = get_byname(ualg->alg_name, 1);
300 if (!algo)
301 return -ENOSYS;
302 *props = algo->desc.sadb_alg_id;
303
Eric Dumazet0f99be02008-01-08 23:39:06 -0800304 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (!p)
306 return -ENOMEM;
307
Herbert Xu04ff1262006-08-13 08:50:00 +1000308 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *algpp = p;
310 return 0;
311}
312
Herbert Xu69b01372015-05-27 16:03:45 +0800313static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
314{
315 struct xfrm_algo *p, *ualg;
316 struct xfrm_algo_desc *algo;
317
318 if (!rta)
319 return 0;
320
321 ualg = nla_data(rta);
322
323 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
324 if (!algo)
325 return -ENOSYS;
326 x->props.ealgo = algo->desc.sadb_alg_id;
327
328 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
329 if (!p)
330 return -ENOMEM;
331
332 strcpy(p->alg_name, algo->name);
333 x->ealg = p;
334 x->geniv = algo->uinfo.encr.geniv;
335 return 0;
336}
337
Martin Willi4447bb32009-11-25 00:29:52 +0000338static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
339 struct nlattr *rta)
340{
341 struct xfrm_algo *ualg;
342 struct xfrm_algo_auth *p;
343 struct xfrm_algo_desc *algo;
344
345 if (!rta)
346 return 0;
347
348 ualg = nla_data(rta);
349
350 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
351 if (!algo)
352 return -ENOSYS;
353 *props = algo->desc.sadb_alg_id;
354
355 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
356 if (!p)
357 return -ENOMEM;
358
359 strcpy(p->alg_name, algo->name);
360 p->alg_key_len = ualg->alg_key_len;
361 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
362 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
363
364 *algpp = p;
365 return 0;
366}
367
368static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
369 struct nlattr *rta)
370{
371 struct xfrm_algo_auth *p, *ualg;
372 struct xfrm_algo_desc *algo;
373
374 if (!rta)
375 return 0;
376
377 ualg = nla_data(rta);
378
379 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
380 if (!algo)
381 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800382 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000383 return -EINVAL;
384 *props = algo->desc.sadb_alg_id;
385
386 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
387 if (!p)
388 return -ENOMEM;
389
390 strcpy(p->alg_name, algo->name);
391 if (!p->alg_trunc_len)
392 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
393
394 *algpp = p;
395 return 0;
396}
397
Herbert Xu69b01372015-05-27 16:03:45 +0800398static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800399{
400 struct xfrm_algo_aead *p, *ualg;
401 struct xfrm_algo_desc *algo;
402
403 if (!rta)
404 return 0;
405
406 ualg = nla_data(rta);
407
408 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
409 if (!algo)
410 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800411 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800412
413 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
414 if (!p)
415 return -ENOMEM;
416
417 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800418 x->aead = p;
419 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800420 return 0;
421}
422
Steffen Klasserte2b19122011-03-28 19:47:30 +0000423static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
424 struct nlattr *rp)
425{
426 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000427 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000428
429 if (!replay_esn || !rp)
430 return 0;
431
432 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000433 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000434
Andy Whitcroft79191ea2017-03-23 07:45:44 +0000435 /* Check the overall length and the internal bitmap length to avoid
436 * potential overflow. */
437 if (nla_len(rp) < ulen ||
438 xfrm_replay_state_esn_len(replay_esn) != ulen ||
439 replay_esn->bmp_len != up->bmp_len)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000440 return -EINVAL;
441
Andy Whitcroft64a54652017-03-22 07:29:31 +0000442 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
443 return -EINVAL;
444
Steffen Klasserte2b19122011-03-28 19:47:30 +0000445 return 0;
446}
447
Steffen Klassertd8647b72011-03-08 00:10:27 +0000448static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
449 struct xfrm_replay_state_esn **preplay_esn,
450 struct nlattr *rta)
451{
452 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000453 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000454
455 if (!rta)
456 return 0;
457
458 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000459 klen = xfrm_replay_state_esn_len(up);
460 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000461
Mathias Krauseecd79182012-09-20 10:01:49 +0000462 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000463 if (!p)
464 return -ENOMEM;
465
Mathias Krauseecd79182012-09-20 10:01:49 +0000466 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000467 if (!pp) {
468 kfree(p);
469 return -ENOMEM;
470 }
471
Mathias Krauseecd79182012-09-20 10:01:49 +0000472 memcpy(p, up, ulen);
473 memcpy(pp, up, ulen);
474
Steffen Klassertd8647b72011-03-08 00:10:27 +0000475 *replay_esn = p;
476 *preplay_esn = pp;
477
478 return 0;
479}
480
Joy Latten661697f2007-04-13 16:14:35 -0700481static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800482{
Trent Jaegerdf718372005-12-13 23:12:27 -0800483 int len = 0;
484
485 if (xfrm_ctx) {
486 len += sizeof(struct xfrm_user_sec_ctx);
487 len += xfrm_ctx->ctx_len;
488 }
489 return len;
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
493{
494 memcpy(&x->id, &p->id, sizeof(x->id));
495 memcpy(&x->sel, &p->sel, sizeof(x->sel));
496 memcpy(&x->lft, &p->lft, sizeof(x->lft));
497 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800498 x->props.replay_window = min_t(unsigned int, p->replay_window,
499 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 x->props.reqid = p->reqid;
501 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700502 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700504
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700505 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700506 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800509/*
510 * someday when pfkey also has support, we could have the code
511 * somehow made shareable and move it to xfrm_state.c - JHS
512 *
513*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000514static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
515 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800516{
Thomas Graf5424f322007-08-22 14:01:33 -0700517 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000518 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700519 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
520 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
521 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800522
Steffen Klassertd8647b72011-03-08 00:10:27 +0000523 if (re) {
524 struct xfrm_replay_state_esn *replay_esn;
525 replay_esn = nla_data(re);
526 memcpy(x->replay_esn, replay_esn,
527 xfrm_replay_state_esn_len(replay_esn));
528 memcpy(x->preplay_esn, replay_esn,
529 xfrm_replay_state_esn_len(replay_esn));
530 }
531
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800532 if (rp) {
533 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700534 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800535 memcpy(&x->replay, replay, sizeof(*replay));
536 memcpy(&x->preplay, replay, sizeof(*replay));
537 }
538
539 if (lt) {
540 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700541 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800542 x->curlft.bytes = ltime->bytes;
543 x->curlft.packets = ltime->packets;
544 x->curlft.add_time = ltime->add_time;
545 x->curlft.use_time = ltime->use_time;
546 }
547
Thomas Grafcf5cb792007-08-22 13:59:04 -0700548 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700549 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800550
Thomas Grafcf5cb792007-08-22 13:59:04 -0700551 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700552 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800553}
554
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800555static struct xfrm_state *xfrm_state_construct(struct net *net,
556 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700557 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int *errp)
559{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800560 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int err = -ENOMEM;
562
563 if (!x)
564 goto error_no_put;
565
566 copy_from_user_state(x, p);
567
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100568 if (attrs[XFRMA_SA_EXTRA_FLAGS])
569 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
570
Herbert Xu69b01372015-05-27 16:03:45 +0800571 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800572 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000573 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
574 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000576 if (!x->props.aalgo) {
577 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
578 attrs[XFRMA_ALG_AUTH])))
579 goto error;
580 }
Herbert Xu69b01372015-05-27 16:03:45 +0800581 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto error;
583 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
584 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700585 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700587
588 if (attrs[XFRMA_ENCAP]) {
589 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
590 sizeof(*x->encap), GFP_KERNEL);
591 if (x->encap == NULL)
592 goto error;
593 }
594
Martin Willi35d28562010-12-08 04:37:49 +0000595 if (attrs[XFRMA_TFCPAD])
596 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
597
Thomas Graffd211502007-09-06 03:28:08 -0700598 if (attrs[XFRMA_COADDR]) {
599 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
600 sizeof(*x->coaddr), GFP_KERNEL);
601 if (x->coaddr == NULL)
602 goto error;
603 }
604
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000605 xfrm_mark_get(attrs, &x->mark);
606
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700607 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (err)
609 goto error;
610
Mathias Krause2f30ea52016-09-08 18:09:57 +0200611 if (attrs[XFRMA_SEC_CTX]) {
612 err = security_xfrm_state_alloc(x,
613 nla_data(attrs[XFRMA_SEC_CTX]));
614 if (err)
615 goto error;
616 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800617
Steffen Klassertd8647b72011-03-08 00:10:27 +0000618 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
619 attrs[XFRMA_REPLAY_ESN_VAL])))
620 goto error;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800623 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800624 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800625 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800626
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000627 if ((err = xfrm_init_replay(x)))
628 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800629
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000630 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000631 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 return x;
634
635error:
636 x->km.state = XFRM_STATE_DEAD;
637 xfrm_state_put(x);
638error_no_put:
639 *errp = err;
640 return NULL;
641}
642
Christoph Hellwig22e70052007-01-02 15:22:30 -0800643static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700644 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800646 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700647 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct xfrm_state *x;
649 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700650 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Thomas Graf35a7aa02007-08-22 14:00:40 -0700652 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (err)
654 return err;
655
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800656 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!x)
658 return err;
659
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700660 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
662 err = xfrm_state_add(x);
663 else
664 err = xfrm_state_update(x);
665
Tetsuo Handa2e710292014-04-22 21:48:30 +0900666 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (err < 0) {
669 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800670 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700674 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000675 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700676 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700677
678 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700679out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700680 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return err;
682}
683
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800684static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
685 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700686 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700687 int *errp)
688{
689 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000690 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700691 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000692 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700693
694 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
695 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000696 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700697 } else {
698 xfrm_address_t *saddr = NULL;
699
Thomas Graf35a7aa02007-08-22 14:00:40 -0700700 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700701 if (!saddr) {
702 err = -EINVAL;
703 goto out;
704 }
705
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800706 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000707 x = xfrm_state_lookup_byaddr(net, mark,
708 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800709 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700710 }
711
712 out:
713 if (!x && errp)
714 *errp = err;
715 return x;
716}
717
Christoph Hellwig22e70052007-01-02 15:22:30 -0800718static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700719 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800721 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700723 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700724 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700725 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800727 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700729 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
David S. Miller6f68dc32006-06-08 23:58:52 -0700731 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700732 goto out;
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700735 err = -EPERM;
736 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700739 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600740
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700741 if (err < 0)
742 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700743
744 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000745 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700746 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700747 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700749out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900750 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700751 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
756{
Mathias Krausef778a632012-09-19 11:33:39 +0000757 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 memcpy(&p->id, &x->id, sizeof(p->id));
759 memcpy(&p->sel, &x->sel, sizeof(p->sel));
760 memcpy(&p->lft, &x->lft, sizeof(p->lft));
761 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400762 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
763 put_unaligned(x->stats.replay, &p->stats.replay);
764 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700765 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 p->mode = x->props.mode;
767 p->replay_window = x->props.replay_window;
768 p->reqid = x->props.reqid;
769 p->family = x->props.family;
770 p->flags = x->props.flags;
771 p->seq = x->km.seq;
772}
773
774struct xfrm_dump_info {
775 struct sk_buff *in_skb;
776 struct sk_buff *out_skb;
777 u32 nlmsg_seq;
778 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779};
780
Thomas Grafc0144be2007-08-22 13:55:43 -0700781static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
782{
Thomas Grafc0144be2007-08-22 13:55:43 -0700783 struct xfrm_user_sec_ctx *uctx;
784 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700785 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700786
787 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
788 if (attr == NULL)
789 return -EMSGSIZE;
790
791 uctx = nla_data(attr);
792 uctx->exttype = XFRMA_SEC_CTX;
793 uctx->len = ctx_size;
794 uctx->ctx_doi = s->ctx_doi;
795 uctx->ctx_alg = s->ctx_alg;
796 uctx->ctx_len = s->ctx_len;
797 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
798
799 return 0;
800}
801
Martin Willi4447bb32009-11-25 00:29:52 +0000802static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
803{
804 struct xfrm_algo *algo;
805 struct nlattr *nla;
806
807 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
808 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
809 if (!nla)
810 return -EMSGSIZE;
811
812 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000813 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000814 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
815 algo->alg_key_len = auth->alg_key_len;
816
817 return 0;
818}
819
Herbert Xu68325d32007-10-09 13:30:57 -0700820/* Don't change this without updating xfrm_sa_len! */
821static int copy_to_user_state_extra(struct xfrm_state *x,
822 struct xfrm_usersa_info *p,
823 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700825 int ret = 0;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 copy_to_user_state(x, p);
828
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100829 if (x->props.extra_flags) {
830 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
831 x->props.extra_flags);
832 if (ret)
833 goto out;
834 }
835
David S. Miller1d1e34d2012-06-27 21:57:03 -0700836 if (x->coaddr) {
837 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
838 if (ret)
839 goto out;
840 }
841 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200842 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
843 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700844 if (ret)
845 goto out;
846 }
847 if (x->aead) {
848 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
849 if (ret)
850 goto out;
851 }
852 if (x->aalg) {
853 ret = copy_to_user_auth(x->aalg, skb);
854 if (!ret)
855 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
856 xfrm_alg_auth_len(x->aalg), x->aalg);
857 if (ret)
858 goto out;
859 }
860 if (x->ealg) {
861 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
862 if (ret)
863 goto out;
864 }
865 if (x->calg) {
866 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
867 if (ret)
868 goto out;
869 }
870 if (x->encap) {
871 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
872 if (ret)
873 goto out;
874 }
875 if (x->tfcpad) {
876 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
877 if (ret)
878 goto out;
879 }
880 ret = xfrm_mark_put(skb, &x->mark);
881 if (ret)
882 goto out;
dingzhif293a5e2014-10-30 09:39:36 +0100883 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700884 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
885 xfrm_replay_state_esn_len(x->replay_esn),
886 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100887 else
888 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
889 &x->replay);
890 if (ret)
891 goto out;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700892 if (x->security)
893 ret = copy_sec_ctx(x->security, skb);
894out:
895 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700896}
897
898static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
899{
900 struct xfrm_dump_info *sp = ptr;
901 struct sk_buff *in_skb = sp->in_skb;
902 struct sk_buff *skb = sp->out_skb;
903 struct xfrm_usersa_info *p;
904 struct nlmsghdr *nlh;
905 int err;
906
Eric W. Biederman15e47302012-09-07 20:12:54 +0000907 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700908 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
909 if (nlh == NULL)
910 return -EMSGSIZE;
911
912 p = nlmsg_data(nlh);
913
914 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700915 if (err) {
916 nlmsg_cancel(skb, nlh);
917 return err;
918 }
Thomas Graf98250692007-08-22 12:47:26 -0700919 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Timo Teras4c563f72008-02-28 21:31:08 -0800923static int xfrm_dump_sa_done(struct netlink_callback *cb)
924{
925 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800926 struct sock *sk = cb->skb->sk;
927 struct net *net = sock_net(sk);
928
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200929 if (cb->args[0])
930 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800931 return 0;
932}
933
Nicolas Dichteld3623092014-02-14 15:30:36 +0100934static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
936{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800937 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800938 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct xfrm_dump_info info;
940
Timo Teras4c563f72008-02-28 21:31:08 -0800941 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
942 sizeof(cb->args) - sizeof(cb->args[0]));
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 info.in_skb = cb->skb;
945 info.out_skb = skb;
946 info.nlmsg_seq = cb->nlh->nlmsg_seq;
947 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800948
949 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100950 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100951 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100952 u8 proto = 0;
953 int err;
954
Nicolas Dichteld3623092014-02-14 15:30:36 +0100955 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
956 xfrma_policy);
957 if (err < 0)
958 return err;
959
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100960 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +0200961 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
962 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +0100963 if (filter == NULL)
964 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100965 }
966
967 if (attrs[XFRMA_PROTO])
968 proto = nla_get_u8(attrs[XFRMA_PROTO]);
969
970 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200971 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -0800972 }
973
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800974 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 return skb->len;
977}
978
979static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
980 struct xfrm_state *x, u32 seq)
981{
982 struct xfrm_dump_info info;
983 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000984 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Thomas Graf7deb2262007-08-22 13:57:39 -0700986 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (!skb)
988 return ERR_PTR(-ENOMEM);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 info.in_skb = in_skb;
991 info.out_skb = skb;
992 info.nlmsg_seq = seq;
993 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Mathias Krause864745d2012-09-13 11:41:26 +0000995 err = dump_one_state(x, 0, &info);
996 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000998 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
1001 return skb;
1002}
1003
Michal Kubecek21ee5432014-06-03 10:26:06 +02001004/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1005 * Must be called with RCU read lock.
1006 */
1007static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1008 u32 pid, unsigned int group)
1009{
1010 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1011
Florian Westphal301a6da2018-06-25 14:00:07 +02001012 if (!nlsk) {
1013 kfree_skb(skb);
1014 return -EPIPE;
1015 }
1016
1017 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
Michal Kubecek21ee5432014-06-03 10:26:06 +02001018}
1019
Thomas Graf7deb2262007-08-22 13:57:39 -07001020static inline size_t xfrm_spdinfo_msgsize(void)
1021{
1022 return NLMSG_ALIGN(4)
1023 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001024 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1025 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1026 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -07001027}
1028
Alexey Dobriyane0710412010-01-23 13:37:10 +00001029static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001030 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001031{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001032 struct xfrmk_spdinfo si;
1033 struct xfrmu_spdinfo spc;
1034 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001035 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001036 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001037 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001038 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001039 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001040
Eric W. Biederman15e47302012-09-07 20:12:54 +00001041 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001042 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001043 return -EMSGSIZE;
1044
1045 f = nlmsg_data(nlh);
1046 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001047 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001048 spc.incnt = si.incnt;
1049 spc.outcnt = si.outcnt;
1050 spc.fwdcnt = si.fwdcnt;
1051 spc.inscnt = si.inscnt;
1052 spc.outscnt = si.outscnt;
1053 spc.fwdscnt = si.fwdscnt;
1054 sph.spdhcnt = si.spdhcnt;
1055 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001056
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001057 do {
1058 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1059
1060 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1061 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1062 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1063 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1064 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1065
David S. Miller1d1e34d2012-06-27 21:57:03 -07001066 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1067 if (!err)
1068 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001069 if (!err)
1070 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1071 if (!err)
1072 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001073 if (err) {
1074 nlmsg_cancel(skb, nlh);
1075 return err;
1076 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001077
Johannes Berg053c0952015-01-16 22:09:00 +01001078 nlmsg_end(skb, nlh);
1079 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001080}
1081
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001082static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1083 struct nlattr **attrs)
1084{
1085 struct net *net = sock_net(skb->sk);
1086 struct xfrmu_spdhthresh *thresh4 = NULL;
1087 struct xfrmu_spdhthresh *thresh6 = NULL;
1088
1089 /* selector prefixlen thresholds to hash policies */
1090 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1091 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1092
1093 if (nla_len(rta) < sizeof(*thresh4))
1094 return -EINVAL;
1095 thresh4 = nla_data(rta);
1096 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1097 return -EINVAL;
1098 }
1099 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1100 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1101
1102 if (nla_len(rta) < sizeof(*thresh6))
1103 return -EINVAL;
1104 thresh6 = nla_data(rta);
1105 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1106 return -EINVAL;
1107 }
1108
1109 if (thresh4 || thresh6) {
1110 write_seqlock(&net->xfrm.policy_hthresh.lock);
1111 if (thresh4) {
1112 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1113 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1114 }
1115 if (thresh6) {
1116 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1117 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1118 }
1119 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1120
1121 xfrm_policy_hash_rebuild(net);
1122 }
1123
1124 return 0;
1125}
1126
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001127static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001128 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001129{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001130 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001131 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001132 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001133 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001134 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001135
Thomas Graf7deb2262007-08-22 13:57:39 -07001136 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001137 if (r_skb == NULL)
1138 return -ENOMEM;
1139
Eric W. Biederman15e47302012-09-07 20:12:54 +00001140 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001141 BUG();
1142
Eric W. Biederman15e47302012-09-07 20:12:54 +00001143 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001144}
1145
Thomas Graf7deb2262007-08-22 13:57:39 -07001146static inline size_t xfrm_sadinfo_msgsize(void)
1147{
1148 return NLMSG_ALIGN(4)
1149 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1150 + nla_total_size(4); /* XFRMA_SAD_CNT */
1151}
1152
Alexey Dobriyane0710412010-01-23 13:37:10 +00001153static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001154 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001155{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001156 struct xfrmk_sadinfo si;
1157 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001158 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001159 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001160 u32 *f;
1161
Eric W. Biederman15e47302012-09-07 20:12:54 +00001162 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001163 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001164 return -EMSGSIZE;
1165
1166 f = nlmsg_data(nlh);
1167 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001168 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001169
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001170 sh.sadhmcnt = si.sadhmcnt;
1171 sh.sadhcnt = si.sadhcnt;
1172
David S. Miller1d1e34d2012-06-27 21:57:03 -07001173 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1174 if (!err)
1175 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1176 if (err) {
1177 nlmsg_cancel(skb, nlh);
1178 return err;
1179 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001180
Johannes Berg053c0952015-01-16 22:09:00 +01001181 nlmsg_end(skb, nlh);
1182 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001183}
1184
1185static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001186 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001187{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001188 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001189 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001190 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001191 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001192 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001193
Thomas Graf7deb2262007-08-22 13:57:39 -07001194 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001195 if (r_skb == NULL)
1196 return -ENOMEM;
1197
Eric W. Biederman15e47302012-09-07 20:12:54 +00001198 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001199 BUG();
1200
Eric W. Biederman15e47302012-09-07 20:12:54 +00001201 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001202}
1203
Christoph Hellwig22e70052007-01-02 15:22:30 -08001204static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001205 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001207 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001208 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 struct xfrm_state *x;
1210 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001211 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001213 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (x == NULL)
1215 goto out_noput;
1216
1217 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1218 if (IS_ERR(resp_skb)) {
1219 err = PTR_ERR(resp_skb);
1220 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001221 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 xfrm_state_put(x);
1224out_noput:
1225 return err;
1226}
1227
Christoph Hellwig22e70052007-01-02 15:22:30 -08001228static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001229 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001231 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 struct xfrm_state *x;
1233 struct xfrm_userspi_info *p;
1234 struct sk_buff *resp_skb;
1235 xfrm_address_t *daddr;
1236 int family;
1237 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001238 u32 mark;
1239 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Thomas Graf7b67c852007-08-22 13:53:52 -07001241 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001242 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (err)
1244 goto out_noput;
1245
1246 family = p->info.family;
1247 daddr = &p->info.id.daddr;
1248
1249 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001250
1251 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001253 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001254 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 xfrm_state_put(x);
1256 x = NULL;
1257 }
1258 }
1259
1260 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001261 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 p->info.id.proto, daddr,
1263 &p->info.saddr, 1,
1264 family);
1265 err = -ENOENT;
1266 if (x == NULL)
1267 goto out_noput;
1268
Herbert Xu658b2192007-10-09 13:29:52 -07001269 err = xfrm_alloc_spi(x, p->min, p->max);
1270 if (err)
1271 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Herbert Xu658b2192007-10-09 13:29:52 -07001273 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (IS_ERR(resp_skb)) {
1275 err = PTR_ERR(resp_skb);
1276 goto out;
1277 }
1278
Eric W. Biederman15e47302012-09-07 20:12:54 +00001279 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281out:
1282 xfrm_state_put(x);
1283out_noput:
1284 return err;
1285}
1286
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001287static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 switch (dir) {
1290 case XFRM_POLICY_IN:
1291 case XFRM_POLICY_OUT:
1292 case XFRM_POLICY_FWD:
1293 break;
1294
1295 default:
1296 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 return 0;
1300}
1301
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001302static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001303{
1304 switch (type) {
1305 case XFRM_POLICY_TYPE_MAIN:
1306#ifdef CONFIG_XFRM_SUB_POLICY
1307 case XFRM_POLICY_TYPE_SUB:
1308#endif
1309 break;
1310
1311 default:
1312 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001313 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001314
1315 return 0;
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1319{
Fan Due682adf2013-11-07 17:47:48 +08001320 int ret;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 switch (p->share) {
1323 case XFRM_SHARE_ANY:
1324 case XFRM_SHARE_SESSION:
1325 case XFRM_SHARE_USER:
1326 case XFRM_SHARE_UNIQUE:
1327 break;
1328
1329 default:
1330 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 switch (p->action) {
1334 case XFRM_POLICY_ALLOW:
1335 case XFRM_POLICY_BLOCK:
1336 break;
1337
1338 default:
1339 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 switch (p->sel.family) {
1343 case AF_INET:
Steffen Klassert89cb86e2018-08-01 13:45:11 +02001344 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1345 return -EINVAL;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 break;
1348
1349 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001350#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert89cb86e2018-08-01 13:45:11 +02001351 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1352 return -EINVAL;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 break;
1355#else
1356 return -EAFNOSUPPORT;
1357#endif
1358
1359 default:
1360 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Fan Due682adf2013-11-07 17:47:48 +08001363 ret = verify_policy_dir(p->dir);
1364 if (ret)
1365 return ret;
YueHaibing7c967212019-02-28 15:18:59 +08001366 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
Fan Due682adf2013-11-07 17:47:48 +08001367 return -EINVAL;
1368
1369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Thomas Graf5424f322007-08-22 14:01:33 -07001372static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001373{
Thomas Graf5424f322007-08-22 14:01:33 -07001374 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001375 struct xfrm_user_sec_ctx *uctx;
1376
1377 if (!rt)
1378 return 0;
1379
Thomas Graf5424f322007-08-22 14:01:33 -07001380 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001381 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1385 int nr)
1386{
1387 int i;
1388
1389 xp->xfrm_nr = nr;
1390 for (i = 0; i < nr; i++, ut++) {
1391 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1392
1393 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1394 memcpy(&t->saddr, &ut->saddr,
1395 sizeof(xfrm_address_t));
1396 t->reqid = ut->reqid;
1397 t->mode = ut->mode;
1398 t->share = ut->share;
1399 t->optional = ut->optional;
1400 t->aalgos = ut->aalgos;
1401 t->ealgos = ut->ealgos;
1402 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001403 /* If all masks are ~0, then we allow all algorithms. */
1404 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001405 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407}
1408
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001409static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1410{
Steffen Klassert9a54c512017-12-08 08:07:25 +01001411 u16 prev_family;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001412 int i;
1413
1414 if (nr > XFRM_MAX_DEPTH)
1415 return -EINVAL;
1416
Steffen Klassert9a54c512017-12-08 08:07:25 +01001417 prev_family = family;
1418
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001419 for (i = 0; i < nr; i++) {
1420 /* We never validated the ut->family value, so many
1421 * applications simply leave it at zero. The check was
1422 * never made and ut->family was ignored because all
1423 * templates could be assumed to have the same family as
1424 * the policy itself. Now that we will have ipv4-in-ipv6
1425 * and ipv6-in-ipv4 tunnels, this is no longer true.
1426 */
1427 if (!ut[i].family)
1428 ut[i].family = family;
1429
Florian Westphala19fd852019-01-09 14:37:34 +01001430 switch (ut[i].mode) {
1431 case XFRM_MODE_TUNNEL:
1432 case XFRM_MODE_BEET:
1433 break;
1434 default:
1435 if (ut[i].family != prev_family)
1436 return -EINVAL;
1437 break;
1438 }
Sean Tranchettide500b92018-09-19 13:54:56 -06001439 if (ut[i].mode >= XFRM_MODE_MAX)
1440 return -EINVAL;
1441
Steffen Klassert9a54c512017-12-08 08:07:25 +01001442 prev_family = ut[i].family;
1443
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001444 switch (ut[i].family) {
1445 case AF_INET:
1446 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001447#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001448 case AF_INET6:
1449 break;
1450#endif
1451 default:
1452 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001453 }
Cong Wang85552882017-11-27 11:15:16 -08001454
Cong Wang88d08312019-03-22 16:26:19 -07001455 if (!xfrm_id_proto_valid(ut[i].id.proto))
Cong Wang85552882017-11-27 11:15:16 -08001456 return -EINVAL;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001457 }
1458
1459 return 0;
1460}
1461
Thomas Graf5424f322007-08-22 14:01:33 -07001462static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Thomas Graf5424f322007-08-22 14:01:33 -07001464 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 if (!rt) {
1467 pol->xfrm_nr = 0;
1468 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001469 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1470 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001471 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001473 err = validate_tmpl(nr, utmpl, pol->family);
1474 if (err)
1475 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Thomas Graf5424f322007-08-22 14:01:33 -07001477 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479 return 0;
1480}
1481
Thomas Graf5424f322007-08-22 14:01:33 -07001482static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001483{
Thomas Graf5424f322007-08-22 14:01:33 -07001484 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001485 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001486 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001487 int err;
1488
1489 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001490 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001491 type = upt->type;
1492 }
1493
1494 err = verify_policy_type(type);
1495 if (err)
1496 return err;
1497
1498 *tp = type;
1499 return 0;
1500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1503{
1504 xp->priority = p->priority;
1505 xp->index = p->index;
1506 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1507 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1508 xp->action = p->action;
1509 xp->flags = p->flags;
1510 xp->family = p->sel.family;
1511 /* XXX xp->share = p->share; */
1512}
1513
1514static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1515{
Mathias Krause7b789832012-09-19 11:33:40 +00001516 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1518 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1519 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1520 p->priority = xp->priority;
1521 p->index = xp->index;
1522 p->sel.family = xp->family;
1523 p->dir = dir;
1524 p->action = xp->action;
1525 p->flags = xp->flags;
1526 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1527}
1528
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001529static 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 -07001530{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001531 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int err;
1533
1534 if (!xp) {
1535 *errp = -ENOMEM;
1536 return NULL;
1537 }
1538
1539 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001540
Thomas Graf35a7aa02007-08-22 14:00:40 -07001541 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001542 if (err)
1543 goto error;
1544
Thomas Graf35a7aa02007-08-22 14:00:40 -07001545 if (!(err = copy_from_user_tmpl(xp, attrs)))
1546 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001547 if (err)
1548 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001550 xfrm_mark_get(attrs, &xp->mark);
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001553 error:
1554 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001555 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001556 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001557 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
Christoph Hellwig22e70052007-01-02 15:22:30 -08001560static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001561 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001563 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001564 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001566 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int err;
1568 int excl;
1569
1570 err = verify_newpolicy_info(p);
1571 if (err)
1572 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001573 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001574 if (err)
1575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001577 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (!xp)
1579 return err;
1580
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001581 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001582 * Aha! this is anti-netlink really i.e more pfkey derived
1583 * in netlink excl is a flag and you wouldnt need
1584 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1586 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001587 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001590 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 kfree(xp);
1592 return err;
1593 }
1594
Herbert Xuf60f6b82005-06-18 22:44:37 -07001595 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001596 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001597 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001598 km_policy_notify(xp, p->dir, &c);
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 xfrm_pol_put(xp);
1601
1602 return 0;
1603}
1604
1605static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1606{
1607 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1608 int i;
1609
1610 if (xp->xfrm_nr == 0)
1611 return 0;
1612
1613 for (i = 0; i < xp->xfrm_nr; i++) {
1614 struct xfrm_user_tmpl *up = &vec[i];
1615 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1616
Mathias Krause1f868402012-09-19 11:33:41 +00001617 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001619 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1621 up->reqid = kp->reqid;
1622 up->mode = kp->mode;
1623 up->share = kp->share;
1624 up->optional = kp->optional;
1625 up->aalgos = kp->aalgos;
1626 up->ealgos = kp->ealgos;
1627 up->calgos = kp->calgos;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Thomas Grafc0144be2007-08-22 13:55:43 -07001630 return nla_put(skb, XFRMA_TMPL,
1631 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001632}
1633
Serge Hallyn0d681622006-07-24 23:30:44 -07001634static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1635{
1636 if (x->security) {
1637 return copy_sec_ctx(x->security, skb);
1638 }
1639 return 0;
1640}
1641
1642static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1643{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001644 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001645 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001646 return 0;
1647}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001648static inline size_t userpolicy_type_attrsize(void)
1649{
1650#ifdef CONFIG_XFRM_SUB_POLICY
1651 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1652#else
1653 return 0;
1654#endif
1655}
Serge Hallyn0d681622006-07-24 23:30:44 -07001656
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001657#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001658static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001659{
Eric Dumazet2038a9e2018-06-18 21:35:07 -07001660 struct xfrm_userpolicy_type upt;
1661
1662 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1663 memset(&upt, 0, sizeof(upt));
1664 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001665
Thomas Grafc0144be2007-08-22 13:55:43 -07001666 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001667}
1668
1669#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001670static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001671{
1672 return 0;
1673}
1674#endif
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1677{
1678 struct xfrm_dump_info *sp = ptr;
1679 struct xfrm_userpolicy_info *p;
1680 struct sk_buff *in_skb = sp->in_skb;
1681 struct sk_buff *skb = sp->out_skb;
1682 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001683 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Eric W. Biederman15e47302012-09-07 20:12:54 +00001685 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001686 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1687 if (nlh == NULL)
1688 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Thomas Graf7b67c852007-08-22 13:53:52 -07001690 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001692 err = copy_to_user_tmpl(xp, skb);
1693 if (!err)
1694 err = copy_to_user_sec_ctx(xp, skb);
1695 if (!err)
1696 err = copy_to_user_policy_type(xp->type, skb);
1697 if (!err)
1698 err = xfrm_mark_put(skb, &xp->mark);
1699 if (err) {
1700 nlmsg_cancel(skb, nlh);
1701 return err;
1702 }
Thomas Graf98250692007-08-22 12:47:26 -07001703 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Timo Teras4c563f72008-02-28 21:31:08 -08001707static int xfrm_dump_policy_done(struct netlink_callback *cb)
1708{
Herbert Xu543aabb2017-10-19 20:51:10 +08001709 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Fan Du283bc9f2013-11-07 17:47:50 +08001710 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001711
Fan Du283bc9f2013-11-07 17:47:50 +08001712 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001713 return 0;
1714}
1715
Herbert Xu543aabb2017-10-19 20:51:10 +08001716static int xfrm_dump_policy_start(struct netlink_callback *cb)
1717{
1718 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1719
1720 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1721
1722 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1723 return 0;
1724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1727{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001728 struct net *net = sock_net(skb->sk);
Herbert Xu543aabb2017-10-19 20:51:10 +08001729 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct xfrm_dump_info info;
1731
1732 info.in_skb = cb->skb;
1733 info.out_skb = skb;
1734 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1735 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001736
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001737 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 return skb->len;
1740}
1741
1742static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1743 struct xfrm_policy *xp,
1744 int dir, u32 seq)
1745{
1746 struct xfrm_dump_info info;
1747 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001748 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Thomas Graf7deb2262007-08-22 13:57:39 -07001750 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (!skb)
1752 return ERR_PTR(-ENOMEM);
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 info.in_skb = in_skb;
1755 info.out_skb = skb;
1756 info.nlmsg_seq = seq;
1757 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Mathias Krausec2546372012-09-14 09:58:32 +00001759 err = dump_one_policy(xp, dir, 0, &info);
1760 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001762 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
1765 return skb;
1766}
1767
Christoph Hellwig22e70052007-01-02 15:22:30 -08001768static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001769 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001771 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct xfrm_policy *xp;
1773 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001774 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001776 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001778 struct xfrm_mark m;
1779 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Thomas Graf7b67c852007-08-22 13:53:52 -07001781 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1783
Thomas Graf35a7aa02007-08-22 14:00:40 -07001784 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001785 if (err)
1786 return err;
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 err = verify_policy_dir(p->dir);
1789 if (err)
1790 return err;
1791
1792 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001793 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001794 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001795 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001796 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001797
Thomas Graf35a7aa02007-08-22 14:00:40 -07001798 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001799 if (err)
1800 return err;
1801
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001802 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001803 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001804 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001805
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001806 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001807 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001808 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001809 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001810 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001811 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001812 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (xp == NULL)
1815 return -ENOENT;
1816
1817 if (!delete) {
1818 struct sk_buff *resp_skb;
1819
1820 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1821 if (IS_ERR(resp_skb)) {
1822 err = PTR_ERR(resp_skb);
1823 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001824 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001825 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001827 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001828 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001829
1830 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001831 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001832
Herbert Xue7443892005-06-18 22:44:18 -07001833 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001834 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001835 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001836 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001837 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001840out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001841 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001842 if (delete && err == 0)
1843 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return err;
1845}
1846
Christoph Hellwig22e70052007-01-02 15:22:30 -08001847static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001848 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001850 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001851 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001852 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001853 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Tetsuo Handa2e710292014-04-22 21:48:30 +09001855 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001856 if (err) {
1857 if (err == -ESRCH) /* empty table */
1858 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001859 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001860 }
Herbert Xubf088672005-06-18 22:44:00 -07001861 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001862 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001863 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001864 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001865 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001866 km_state_notify(NULL, &c);
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return 0;
1869}
1870
Steffen Klassertd8647b72011-03-08 00:10:27 +00001871static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001872{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001873 size_t replay_size = x->replay_esn ?
1874 xfrm_replay_state_esn_len(x->replay_esn) :
1875 sizeof(struct xfrm_replay_state);
1876
Thomas Graf7deb2262007-08-22 13:57:39 -07001877 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001878 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001879 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001880 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001881 + nla_total_size(4) /* XFRM_AE_RTHR */
1882 + nla_total_size(4); /* XFRM_AE_ETHR */
1883}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001884
David S. Miller214e0052011-02-24 00:02:38 -05001885static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001886{
1887 struct xfrm_aevent_id *id;
1888 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001889 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001890
Eric W. Biederman15e47302012-09-07 20:12:54 +00001891 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001892 if (nlh == NULL)
1893 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001894
Thomas Graf7b67c852007-08-22 13:53:52 -07001895 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001896 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001897 id->sa_id.spi = x->id.spi;
1898 id->sa_id.family = x->props.family;
1899 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001900 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001901 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001902 id->flags = c->data.aevent;
1903
David S. Millerd0fde792012-03-29 04:02:26 -04001904 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001905 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1906 xfrm_replay_state_esn_len(x->replay_esn),
1907 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001908 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001909 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1910 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001911 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001912 if (err)
1913 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001914 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1915 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001916 if (err)
1917 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001918
David S. Miller1d1e34d2012-06-27 21:57:03 -07001919 if (id->flags & XFRM_AE_RTHR) {
1920 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1921 if (err)
1922 goto out_cancel;
1923 }
1924 if (id->flags & XFRM_AE_ETHR) {
1925 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1926 x->replay_maxage * 10 / HZ);
1927 if (err)
1928 goto out_cancel;
1929 }
1930 err = xfrm_mark_put(skb, &x->mark);
1931 if (err)
1932 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001933
Johannes Berg053c0952015-01-16 22:09:00 +01001934 nlmsg_end(skb, nlh);
1935 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001936
David S. Miller1d1e34d2012-06-27 21:57:03 -07001937out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001938 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001939 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001940}
1941
Christoph Hellwig22e70052007-01-02 15:22:30 -08001942static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001943 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001944{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001945 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001946 struct xfrm_state *x;
1947 struct sk_buff *r_skb;
1948 int err;
1949 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001950 u32 mark;
1951 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001952 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001953 struct xfrm_usersa_id *id = &p->sa_id;
1954
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001955 mark = xfrm_mark_get(attrs, &m);
1956
1957 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001958 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001959 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001960
1961 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1962 if (r_skb == NULL) {
1963 xfrm_state_put(x);
1964 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001965 }
1966
1967 /*
1968 * XXX: is this lock really needed - none of the other
1969 * gets lock (the concern is things getting updated
1970 * while we are still reading) - jhs
1971 */
1972 spin_lock_bh(&x->lock);
1973 c.data.aevent = p->flags;
1974 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001975 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001976
1977 if (build_aevent(r_skb, x, &c) < 0)
1978 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001979 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001980 spin_unlock_bh(&x->lock);
1981 xfrm_state_put(x);
1982 return err;
1983}
1984
Christoph Hellwig22e70052007-01-02 15:22:30 -08001985static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001986 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001987{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001988 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001989 struct xfrm_state *x;
1990 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001991 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001992 u32 mark = 0;
1993 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001994 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001995 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001996 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001997 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02001998 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1999 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002000
Michael Rossberg4e077232015-09-29 11:25:08 +02002001 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002002 return err;
2003
2004 /* pedantic mode - thou shalt sayeth replaceth */
2005 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2006 return err;
2007
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002008 mark = xfrm_mark_get(attrs, &m);
2009
2010 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 -08002011 if (x == NULL)
2012 return -ESRCH;
2013
2014 if (x->km.state != XFRM_STATE_VALID)
2015 goto out;
2016
Steffen Klassert4479ff72013-09-09 09:39:01 +02002017 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00002018 if (err)
2019 goto out;
2020
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002021 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00002022 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002023 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002024
2025 c.event = nlh->nlmsg_type;
2026 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002027 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002028 c.data.aevent = XFRM_AE_CU;
2029 km_state_notify(x, &c);
2030 err = 0;
2031out:
2032 xfrm_state_put(x);
2033 return err;
2034}
2035
Christoph Hellwig22e70052007-01-02 15:22:30 -08002036static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002037 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002039 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002040 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002041 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002042 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002043
Thomas Graf35a7aa02007-08-22 14:00:40 -07002044 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002045 if (err)
2046 return err;
2047
Tetsuo Handa2e710292014-04-22 21:48:30 +09002048 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002049 if (err) {
2050 if (err == -ESRCH) /* empty table */
2051 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002052 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002053 }
2054
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002055 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002056 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002057 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002058 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002059 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return 0;
2062}
2063
Christoph Hellwig22e70052007-01-02 15:22:30 -08002064static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002065 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002066{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002067 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002068 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002069 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002070 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002071 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002072 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002073 struct xfrm_mark m;
2074 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002075
Thomas Graf35a7aa02007-08-22 14:00:40 -07002076 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002077 if (err)
2078 return err;
2079
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002080 err = verify_policy_dir(p->dir);
2081 if (err)
2082 return err;
2083
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002084 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002085 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002086 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002087 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002088 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002089
Thomas Graf35a7aa02007-08-22 14:00:40 -07002090 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002091 if (err)
2092 return err;
2093
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002094 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002095 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002096 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002097
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002098 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002099 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002100 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002101 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002102 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002103 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002104 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002105 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002106 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002107 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002108
Timo Teräsea2dea92010-03-31 00:17:05 +00002109 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002110 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002111
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002112 err = 0;
2113 if (up->hard) {
2114 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002115 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002116 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002117 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002118
2119out:
2120 xfrm_pol_put(xp);
2121 return err;
2122}
2123
Christoph Hellwig22e70052007-01-02 15:22:30 -08002124static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002125 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002126{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002127 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002128 struct xfrm_state *x;
2129 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002130 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002131 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002132 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002133 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002134
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002135 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 -08002136
David S. Miller3a765aa2007-02-26 14:52:21 -08002137 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002138 if (x == NULL)
2139 return err;
2140
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002141 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002142 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002143 if (x->km.state != XFRM_STATE_VALID)
2144 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002145 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002146
Joy Latten161a09e2006-11-27 13:11:54 -06002147 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002148 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002149 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002150 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002151 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002152out:
2153 spin_unlock_bh(&x->lock);
2154 xfrm_state_put(x);
2155 return err;
2156}
2157
Christoph Hellwig22e70052007-01-02 15:22:30 -08002158static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002159 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002160{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002161 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002162 struct xfrm_policy *xp;
2163 struct xfrm_user_tmpl *ut;
2164 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002165 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002166 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002167
Thomas Graf7b67c852007-08-22 13:53:52 -07002168 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002169 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002170 int err = -ENOMEM;
2171
2172 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002173 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002174
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002175 xfrm_mark_get(attrs, &mark);
2176
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002177 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002178 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002179 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002180
2181 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002182 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002183 if (!xp)
2184 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002185
2186 memcpy(&x->id, &ua->id, sizeof(ua->id));
2187 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2188 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002189 xp->mark.m = x->mark.m = mark.m;
2190 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002191 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002192 /* extract the templates and for each call km_key */
2193 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2194 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2195 memcpy(&x->id, &t->id, sizeof(x->id));
2196 x->props.mode = t->mode;
2197 x->props.reqid = t->reqid;
2198 x->props.family = ut->family;
2199 t->aalgos = ua->aalgos;
2200 t->ealgos = ua->ealgos;
2201 t->calgos = ua->calgos;
2202 err = km_query(x, t, xp);
2203
2204 }
2205
2206 kfree(x);
2207 kfree(xp);
2208
2209 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002210
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002211free_state:
2212 kfree(x);
2213nomem:
2214 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002215}
2216
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002217#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002218static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002219 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002220 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002221{
Thomas Graf5424f322007-08-22 14:01:33 -07002222 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002223 struct xfrm_user_migrate *um;
2224 int i, num_migrate;
2225
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002226 if (k != NULL) {
2227 struct xfrm_user_kmaddress *uk;
2228
2229 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2230 memcpy(&k->local, &uk->local, sizeof(k->local));
2231 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2232 k->family = uk->family;
2233 k->reserved = uk->reserved;
2234 }
2235
Thomas Graf5424f322007-08-22 14:01:33 -07002236 um = nla_data(rt);
2237 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002238
2239 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2240 return -EINVAL;
2241
2242 for (i = 0; i < num_migrate; i++, um++, ma++) {
2243 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2244 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2245 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2246 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2247
2248 ma->proto = um->proto;
2249 ma->mode = um->mode;
2250 ma->reqid = um->reqid;
2251
2252 ma->old_family = um->old_family;
2253 ma->new_family = um->new_family;
2254 }
2255
2256 *num = i;
2257 return 0;
2258}
2259
2260static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002261 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002262{
Thomas Graf7b67c852007-08-22 13:53:52 -07002263 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002264 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002265 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002266 u8 type;
2267 int err;
2268 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002269 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002270
Thomas Graf35a7aa02007-08-22 14:00:40 -07002271 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002272 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002273
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002274 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2275
Thomas Graf5424f322007-08-22 14:01:33 -07002276 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002277 if (err)
2278 return err;
2279
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002280 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002281 if (err)
2282 return err;
2283
2284 if (!n)
2285 return 0;
2286
Fan Du8d549c42013-11-07 17:47:49 +08002287 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002288
2289 return 0;
2290}
2291#else
2292static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002293 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002294{
2295 return -ENOPROTOOPT;
2296}
2297#endif
2298
2299#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002300static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002301{
2302 struct xfrm_user_migrate um;
2303
2304 memset(&um, 0, sizeof(um));
2305 um.proto = m->proto;
2306 um.mode = m->mode;
2307 um.reqid = m->reqid;
2308 um.old_family = m->old_family;
2309 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2310 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2311 um.new_family = m->new_family;
2312 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2313 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2314
Thomas Grafc0144be2007-08-22 13:55:43 -07002315 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002316}
2317
David S. Miller183cad12011-02-24 00:28:01 -05002318static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002319{
2320 struct xfrm_user_kmaddress uk;
2321
2322 memset(&uk, 0, sizeof(uk));
2323 uk.family = k->family;
2324 uk.reserved = k->reserved;
2325 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002326 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002327
2328 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2329}
2330
2331static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002332{
2333 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002334 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2335 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2336 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002337}
2338
David S. Miller183cad12011-02-24 00:28:01 -05002339static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2340 int num_migrate, const struct xfrm_kmaddress *k,
2341 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002342{
David S. Miller183cad12011-02-24 00:28:01 -05002343 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002344 struct xfrm_userpolicy_id *pol_id;
2345 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002346 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002347
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002348 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2349 if (nlh == NULL)
2350 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002351
Thomas Graf7b67c852007-08-22 13:53:52 -07002352 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002353 /* copy data from selector, dir, and type to the pol_id */
2354 memset(pol_id, 0, sizeof(*pol_id));
2355 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2356 pol_id->dir = dir;
2357
David S. Miller1d1e34d2012-06-27 21:57:03 -07002358 if (k != NULL) {
2359 err = copy_to_user_kmaddress(k, skb);
2360 if (err)
2361 goto out_cancel;
2362 }
2363 err = copy_to_user_policy_type(type, skb);
2364 if (err)
2365 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002366 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002367 err = copy_to_user_migrate(mp, skb);
2368 if (err)
2369 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002370 }
2371
Johannes Berg053c0952015-01-16 22:09:00 +01002372 nlmsg_end(skb, nlh);
2373 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002374
2375out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002376 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002377 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002378}
2379
David S. Miller183cad12011-02-24 00:28:01 -05002380static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2381 const struct xfrm_migrate *m, int num_migrate,
2382 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002383{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002384 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002385 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002386
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002387 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002388 if (skb == NULL)
2389 return -ENOMEM;
2390
2391 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002392 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002393 BUG();
2394
Michal Kubecek21ee5432014-06-03 10:26:06 +02002395 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002396}
2397#else
David S. Miller183cad12011-02-24 00:28:01 -05002398static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2399 const struct xfrm_migrate *m, int num_migrate,
2400 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002401{
2402 return -ENOPROTOOPT;
2403}
2404#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002405
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002406#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002407
2408static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002409 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002410 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2411 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2412 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2413 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2414 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2415 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002416 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002417 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002418 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002419 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002420 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002421 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002422 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002423 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2424 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002425 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002426 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002427 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002428 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002429 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430};
2431
Thomas Graf492b5582005-05-03 14:26:40 -07002432#undef XMSGSIZE
2433
Thomas Grafcf5cb792007-08-22 13:59:04 -07002434static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002435 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2436 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2437 [XFRMA_LASTUSED] = { .type = NLA_U64},
2438 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002439 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002440 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2441 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2442 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2443 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2444 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2445 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2446 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2447 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2448 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2449 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2450 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2451 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2452 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2453 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002454 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002455 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002456 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002457 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002458 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002459 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002460 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002461};
2462
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002463static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2464 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2465 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2466};
2467
Mathias Krause05600a72013-02-24 14:10:27 +01002468static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002469 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Herbert Xu543aabb2017-10-19 20:51:10 +08002470 int (*start)(struct netlink_callback *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002472 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002473 const struct nla_policy *nla_pol;
2474 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002475} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2476 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2477 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2478 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002479 .dump = xfrm_dump_sa,
2480 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002481 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2482 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2483 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Herbert Xu543aabb2017-10-19 20:51:10 +08002484 .start = xfrm_dump_policy_start,
Timo Teras4c563f72008-02-28 21:31:08 -08002485 .dump = xfrm_dump_policy,
2486 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002487 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002488 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002489 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002490 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2491 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002492 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002493 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2494 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002495 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2496 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002497 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002498 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002499 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2500 .nla_pol = xfrma_spd_policy,
2501 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002502 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503};
2504
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002505static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002507 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002508 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002509 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002510 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Fan Du74005992015-01-27 17:00:29 +08002512#ifdef CONFIG_COMPAT
Andy Lutomirski2bf8c472016-03-22 14:25:10 -07002513 if (in_compat_syscall())
Yi Zhao83e2d052016-11-29 18:09:01 +08002514 return -EOPNOTSUPP;
Fan Du74005992015-01-27 17:00:29 +08002515#endif
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002519 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 type -= XFRM_MSG_BASE;
2522 link = &xfrm_dispatch[type];
2523
2524 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002525 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002526 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Thomas Graf492b5582005-05-03 14:26:40 -07002528 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2529 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002530 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002532 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002534 {
2535 struct netlink_dump_control c = {
Herbert Xu543aabb2017-10-19 20:51:10 +08002536 .start = link->start,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002537 .dump = link->dump,
2538 .done = link->done,
2539 };
2540 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002544 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2545 link->nla_max ? : XFRMA_MAX,
2546 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002547 if (err < 0)
2548 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002551 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Thomas Graf5424f322007-08-22 14:01:33 -07002553 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002556static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Fan Du283bc9f2013-11-07 17:47:50 +08002558 struct net *net = sock_net(skb->sk);
2559
2560 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002561 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002562 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
Thomas Graf7deb2262007-08-22 13:57:39 -07002565static inline size_t xfrm_expire_msgsize(void)
2566{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002567 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2568 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002569}
2570
David S. Miller214e0052011-02-24 00:02:38 -05002571static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 struct xfrm_user_expire *ue;
2574 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002575 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Eric W. Biederman15e47302012-09-07 20:12:54 +00002577 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002578 if (nlh == NULL)
2579 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Thomas Graf7b67c852007-08-22 13:53:52 -07002581 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002583 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
David S. Miller1d1e34d2012-06-27 21:57:03 -07002585 err = xfrm_mark_put(skb, &x->mark);
2586 if (err)
2587 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002588
Johannes Berg053c0952015-01-16 22:09:00 +01002589 nlmsg_end(skb, nlh);
2590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
David S. Miller214e0052011-02-24 00:02:38 -05002593static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002595 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 struct sk_buff *skb;
2597
Thomas Graf7deb2262007-08-22 13:57:39 -07002598 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (skb == NULL)
2600 return -ENOMEM;
2601
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002602 if (build_expire(skb, x, c) < 0) {
2603 kfree_skb(skb);
2604 return -EMSGSIZE;
2605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Michal Kubecek21ee5432014-06-03 10:26:06 +02002607 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
David S. Miller214e0052011-02-24 00:02:38 -05002610static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002611{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002612 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002613 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002614
Steffen Klassertd8647b72011-03-08 00:10:27 +00002615 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002616 if (skb == NULL)
2617 return -ENOMEM;
2618
2619 if (build_aevent(skb, x, c) < 0)
2620 BUG();
2621
Michal Kubecek21ee5432014-06-03 10:26:06 +02002622 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002623}
2624
David S. Miller214e0052011-02-24 00:02:38 -05002625static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002626{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002627 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002628 struct xfrm_usersa_flush *p;
2629 struct nlmsghdr *nlh;
2630 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002631 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002632
Thomas Graf7deb2262007-08-22 13:57:39 -07002633 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002634 if (skb == NULL)
2635 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002636
Eric W. Biederman15e47302012-09-07 20:12:54 +00002637 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002638 if (nlh == NULL) {
2639 kfree_skb(skb);
2640 return -EMSGSIZE;
2641 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002642
Thomas Graf7b67c852007-08-22 13:53:52 -07002643 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002644 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002645
Thomas Graf98250692007-08-22 12:47:26 -07002646 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002647
Michal Kubecek21ee5432014-06-03 10:26:06 +02002648 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002649}
2650
Thomas Graf7deb2262007-08-22 13:57:39 -07002651static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002652{
Thomas Graf7deb2262007-08-22 13:57:39 -07002653 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002654 if (x->aead)
2655 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002656 if (x->aalg) {
2657 l += nla_total_size(sizeof(struct xfrm_algo) +
2658 (x->aalg->alg_key_len + 7) / 8);
2659 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2660 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002661 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002662 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002663 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002664 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002665 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002666 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002667 if (x->tfcpad)
2668 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002669 if (x->replay_esn)
2670 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002671 else
2672 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002673 if (x->security)
2674 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2675 x->security->ctx_len);
2676 if (x->coaddr)
2677 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002678 if (x->props.extra_flags)
2679 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002680
Herbert Xud26f3982007-11-13 21:47:08 -08002681 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002682 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002683
2684 return l;
2685}
2686
David S. Miller214e0052011-02-24 00:02:38 -05002687static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002688{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002689 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002690 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002691 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002692 struct nlmsghdr *nlh;
2693 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002694 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002695 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002696
2697 headlen = sizeof(*p);
2698 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002699 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002700 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002701 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002702 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002703 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002704
Thomas Graf7deb2262007-08-22 13:57:39 -07002705 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002706 if (skb == NULL)
2707 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002708
Eric W. Biederman15e47302012-09-07 20:12:54 +00002709 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002710 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002711 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002712 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002713
Thomas Graf7b67c852007-08-22 13:53:52 -07002714 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002715 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002716 struct nlattr *attr;
2717
Thomas Graf7b67c852007-08-22 13:53:52 -07002718 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002719 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2720 id->spi = x->id.spi;
2721 id->family = x->props.family;
2722 id->proto = x->id.proto;
2723
Thomas Grafc0144be2007-08-22 13:55:43 -07002724 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002725 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002726 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002727 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002728
2729 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002730 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002731 err = copy_to_user_state_extra(x, p, skb);
2732 if (err)
2733 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002734
Thomas Graf98250692007-08-22 12:47:26 -07002735 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002736
Michal Kubecek21ee5432014-06-03 10:26:06 +02002737 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002738
David S. Miller1d1e34d2012-06-27 21:57:03 -07002739out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002740 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002741 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002742}
2743
David S. Miller214e0052011-02-24 00:02:38 -05002744static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002745{
2746
2747 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002748 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002749 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002750 case XFRM_MSG_NEWAE:
2751 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002752 case XFRM_MSG_DELSA:
2753 case XFRM_MSG_UPDSA:
2754 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002755 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002756 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002757 return xfrm_notify_sa_flush(c);
2758 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002759 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2760 c->event);
2761 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002762 }
2763
2764 return 0;
2765
2766}
2767
Thomas Graf7deb2262007-08-22 13:57:39 -07002768static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2769 struct xfrm_policy *xp)
2770{
2771 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2772 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002773 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002774 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2775 + userpolicy_type_attrsize();
2776}
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002779 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002781 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 struct xfrm_user_acquire *ua;
2783 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002784 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002786 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2787 if (nlh == NULL)
2788 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Thomas Graf7b67c852007-08-22 13:53:52 -07002790 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 memcpy(&ua->id, &x->id, sizeof(ua->id));
2792 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2793 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002794 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 ua->aalgos = xt->aalgos;
2796 ua->ealgos = xt->ealgos;
2797 ua->calgos = xt->calgos;
2798 ua->seq = x->km.seq = seq;
2799
David S. Miller1d1e34d2012-06-27 21:57:03 -07002800 err = copy_to_user_tmpl(xp, skb);
2801 if (!err)
2802 err = copy_to_user_state_sec_ctx(x, skb);
2803 if (!err)
2804 err = copy_to_user_policy_type(xp->type, skb);
2805 if (!err)
2806 err = xfrm_mark_put(skb, &xp->mark);
2807 if (err) {
2808 nlmsg_cancel(skb, nlh);
2809 return err;
2810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Johannes Berg053c0952015-01-16 22:09:00 +01002812 nlmsg_end(skb, nlh);
2813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
2816static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002817 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002819 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Thomas Graf7deb2262007-08-22 13:57:39 -07002822 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (skb == NULL)
2824 return -ENOMEM;
2825
Fan Du65e07362012-08-15 10:13:47 +08002826 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 BUG();
2828
Michal Kubecek21ee5432014-06-03 10:26:06 +02002829 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
2831
2832/* User gives us xfrm_user_policy_info followed by an array of 0
2833 * or more templates.
2834 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002835static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 u8 *data, int len, int *dir)
2837{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002838 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2840 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2841 struct xfrm_policy *xp;
2842 int nr;
2843
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002844 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 case AF_INET:
2846 if (opt != IP_XFRM_POLICY) {
2847 *dir = -EOPNOTSUPP;
2848 return NULL;
2849 }
2850 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002851#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 case AF_INET6:
2853 if (opt != IPV6_XFRM_POLICY) {
2854 *dir = -EOPNOTSUPP;
2855 return NULL;
2856 }
2857 break;
2858#endif
2859 default:
2860 *dir = -EINVAL;
2861 return NULL;
2862 }
2863
2864 *dir = -EINVAL;
2865
2866 if (len < sizeof(*p) ||
2867 verify_newpolicy_info(p))
2868 return NULL;
2869
2870 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002871 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return NULL;
2873
Herbert Xua4f1bac2005-07-26 15:43:17 -07002874 if (p->dir > XFRM_POLICY_OUT)
2875 return NULL;
2876
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002877 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (xp == NULL) {
2879 *dir = -ENOBUFS;
2880 return NULL;
2881 }
2882
2883 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002884 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 copy_templates(xp, ut, nr);
2886
2887 *dir = p->dir;
2888
2889 return xp;
2890}
2891
Thomas Graf7deb2262007-08-22 13:57:39 -07002892static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2893{
2894 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2895 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2896 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002897 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002898 + userpolicy_type_attrsize();
2899}
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002902 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002905 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002906 struct nlmsghdr *nlh;
2907 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Eric W. Biederman15e47302012-09-07 20:12:54 +00002909 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002910 if (nlh == NULL)
2911 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Thomas Graf7b67c852007-08-22 13:53:52 -07002913 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002915 err = copy_to_user_tmpl(xp, skb);
2916 if (!err)
2917 err = copy_to_user_sec_ctx(xp, skb);
2918 if (!err)
2919 err = copy_to_user_policy_type(xp->type, skb);
2920 if (!err)
2921 err = xfrm_mark_put(skb, &xp->mark);
2922 if (err) {
2923 nlmsg_cancel(skb, nlh);
2924 return err;
2925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 upe->hard = !!hard;
2927
Johannes Berg053c0952015-01-16 22:09:00 +01002928 nlmsg_end(skb, nlh);
2929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
David S. Miller214e0052011-02-24 00:02:38 -05002932static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002934 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Thomas Graf7deb2262007-08-22 13:57:39 -07002937 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (skb == NULL)
2939 return -ENOMEM;
2940
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002941 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 BUG();
2943
Michal Kubecek21ee5432014-06-03 10:26:06 +02002944 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
David S. Miller214e0052011-02-24 00:02:38 -05002947static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002948{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002949 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002950 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002951 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002952 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002953 struct nlmsghdr *nlh;
2954 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002955 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002956
2957 headlen = sizeof(*p);
2958 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002959 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002960 headlen = sizeof(*id);
2961 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002962 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002963 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002964 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002965
Thomas Graf7deb2262007-08-22 13:57:39 -07002966 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002967 if (skb == NULL)
2968 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002969
Eric W. Biederman15e47302012-09-07 20:12:54 +00002970 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002971 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002972 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002973 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002974
Thomas Graf7b67c852007-08-22 13:53:52 -07002975 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002976 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002977 struct nlattr *attr;
2978
Thomas Graf7b67c852007-08-22 13:53:52 -07002979 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002980 memset(id, 0, sizeof(*id));
2981 id->dir = dir;
2982 if (c->data.byid)
2983 id->index = xp->index;
2984 else
2985 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2986
Thomas Grafc0144be2007-08-22 13:55:43 -07002987 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002988 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002989 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002990 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002991
2992 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002993 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002994
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002995 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002996 err = copy_to_user_tmpl(xp, skb);
2997 if (!err)
2998 err = copy_to_user_policy_type(xp->type, skb);
2999 if (!err)
3000 err = xfrm_mark_put(skb, &xp->mark);
3001 if (err)
3002 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003003
Thomas Graf98250692007-08-22 12:47:26 -07003004 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003005
Michal Kubecek21ee5432014-06-03 10:26:06 +02003006 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003007
David S. Miller1d1e34d2012-06-27 21:57:03 -07003008out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003009 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003010 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003011}
3012
David S. Miller214e0052011-02-24 00:02:38 -05003013static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003014{
Alexey Dobriyan70678022008-11-25 17:50:36 -08003015 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003016 struct nlmsghdr *nlh;
3017 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003018 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003019
Thomas Graf7deb2262007-08-22 13:57:39 -07003020 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003021 if (skb == NULL)
3022 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003023
Eric W. Biederman15e47302012-09-07 20:12:54 +00003024 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003025 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003026 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003027 goto out_free_skb;
3028 err = copy_to_user_policy_type(c->data.type, skb);
3029 if (err)
3030 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003031
Thomas Graf98250692007-08-22 12:47:26 -07003032 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003033
Michal Kubecek21ee5432014-06-03 10:26:06 +02003034 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003035
David S. Miller1d1e34d2012-06-27 21:57:03 -07003036out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003037 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003038 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003039}
3040
David S. Miller214e0052011-02-24 00:02:38 -05003041static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003042{
3043
3044 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003045 case XFRM_MSG_NEWPOLICY:
3046 case XFRM_MSG_UPDPOLICY:
3047 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003048 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003049 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003050 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003051 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003052 return xfrm_exp_policy_notify(xp, dir, c);
3053 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003054 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3055 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003056 }
3057
3058 return 0;
3059
3060}
3061
Thomas Graf7deb2262007-08-22 13:57:39 -07003062static inline size_t xfrm_report_msgsize(void)
3063{
3064 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3065}
3066
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003067static int build_report(struct sk_buff *skb, u8 proto,
3068 struct xfrm_selector *sel, xfrm_address_t *addr)
3069{
3070 struct xfrm_user_report *ur;
3071 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003072
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003073 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3074 if (nlh == NULL)
3075 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003076
Thomas Graf7b67c852007-08-22 13:53:52 -07003077 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003078 ur->proto = proto;
3079 memcpy(&ur->sel, sel, sizeof(ur->sel));
3080
David S. Miller1d1e34d2012-06-27 21:57:03 -07003081 if (addr) {
3082 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3083 if (err) {
3084 nlmsg_cancel(skb, nlh);
3085 return err;
3086 }
3087 }
Johannes Berg053c0952015-01-16 22:09:00 +01003088 nlmsg_end(skb, nlh);
3089 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003090}
3091
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003092static int xfrm_send_report(struct net *net, u8 proto,
3093 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003094{
3095 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003096
Thomas Graf7deb2262007-08-22 13:57:39 -07003097 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003098 if (skb == NULL)
3099 return -ENOMEM;
3100
3101 if (build_report(skb, proto, sel, addr) < 0)
3102 BUG();
3103
Michal Kubecek21ee5432014-06-03 10:26:06 +02003104 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003105}
3106
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003107static inline size_t xfrm_mapping_msgsize(void)
3108{
3109 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3110}
3111
3112static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3113 xfrm_address_t *new_saddr, __be16 new_sport)
3114{
3115 struct xfrm_user_mapping *um;
3116 struct nlmsghdr *nlh;
3117
3118 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3119 if (nlh == NULL)
3120 return -EMSGSIZE;
3121
3122 um = nlmsg_data(nlh);
3123
3124 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3125 um->id.spi = x->id.spi;
3126 um->id.family = x->props.family;
3127 um->id.proto = x->id.proto;
3128 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3129 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3130 um->new_sport = new_sport;
3131 um->old_sport = x->encap->encap_sport;
3132 um->reqid = x->props.reqid;
3133
Johannes Berg053c0952015-01-16 22:09:00 +01003134 nlmsg_end(skb, nlh);
3135 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003136}
3137
3138static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3139 __be16 sport)
3140{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003141 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003142 struct sk_buff *skb;
3143
3144 if (x->id.proto != IPPROTO_ESP)
3145 return -EINVAL;
3146
3147 if (!x->encap)
3148 return -EINVAL;
3149
3150 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3151 if (skb == NULL)
3152 return -ENOMEM;
3153
3154 if (build_mapping(skb, x, ipaddr, sport) < 0)
3155 BUG();
3156
Michal Kubecek21ee5432014-06-03 10:26:06 +02003157 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003158}
3159
Horia Geanta0f245582014-02-12 16:20:06 +02003160static bool xfrm_is_alive(const struct km_event *c)
3161{
3162 return (bool)xfrm_acquire_is_on(c->net);
3163}
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165static struct xfrm_mgr netlink_mgr = {
3166 .id = "netlink",
3167 .notify = xfrm_send_state_notify,
3168 .acquire = xfrm_send_acquire,
3169 .compile_policy = xfrm_compile_policy,
3170 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003171 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003172 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003173 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003174 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175};
3176
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003177static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Patrick McHardybe336902006-03-20 22:40:54 -08003179 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003180 struct netlink_kernel_cfg cfg = {
3181 .groups = XFRMNLGRP_MAX,
3182 .input = xfrm_netlink_rcv,
3183 };
Patrick McHardybe336902006-03-20 22:40:54 -08003184
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003185 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003186 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003188 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003189 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return 0;
3191}
3192
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003193static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003194{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003195 struct net *net;
3196 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003197 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003198 synchronize_net();
3199 list_for_each_entry(net, net_exit_list, exit_list)
3200 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003201}
3202
3203static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003204 .init = xfrm_user_net_init,
3205 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003206};
3207
3208static int __init xfrm_user_init(void)
3209{
3210 int rv;
3211
3212 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3213
3214 rv = register_pernet_subsys(&xfrm_user_net_ops);
3215 if (rv < 0)
3216 return rv;
3217 rv = xfrm_register_km(&netlink_mgr);
3218 if (rv < 0)
3219 unregister_pernet_subsys(&xfrm_user_net_ops);
3220 return rv;
3221}
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223static void __exit xfrm_user_exit(void)
3224{
3225 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003226 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
3229module_init(xfrm_user_init);
3230module_exit(xfrm_user_exit);
3231MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003232MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003233