blob: f3e9d500fa5ac4f539c8c8450444d460600c7fdd [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
1455 switch (ut[i].id.proto) {
1456 case IPPROTO_AH:
1457 case IPPROTO_ESP:
1458 case IPPROTO_COMP:
1459#if IS_ENABLED(CONFIG_IPV6)
1460 case IPPROTO_ROUTING:
1461 case IPPROTO_DSTOPTS:
1462#endif
1463 case IPSEC_PROTO_ANY:
1464 break;
1465 default:
1466 return -EINVAL;
1467 }
1468
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001469 }
1470
1471 return 0;
1472}
1473
Thomas Graf5424f322007-08-22 14:01:33 -07001474static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Thomas Graf5424f322007-08-22 14:01:33 -07001476 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 if (!rt) {
1479 pol->xfrm_nr = 0;
1480 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001481 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1482 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001483 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001485 err = validate_tmpl(nr, utmpl, pol->family);
1486 if (err)
1487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Thomas Graf5424f322007-08-22 14:01:33 -07001489 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491 return 0;
1492}
1493
Thomas Graf5424f322007-08-22 14:01:33 -07001494static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001495{
Thomas Graf5424f322007-08-22 14:01:33 -07001496 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001497 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001498 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001499 int err;
1500
1501 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001502 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001503 type = upt->type;
1504 }
1505
1506 err = verify_policy_type(type);
1507 if (err)
1508 return err;
1509
1510 *tp = type;
1511 return 0;
1512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1515{
1516 xp->priority = p->priority;
1517 xp->index = p->index;
1518 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1519 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1520 xp->action = p->action;
1521 xp->flags = p->flags;
1522 xp->family = p->sel.family;
1523 /* XXX xp->share = p->share; */
1524}
1525
1526static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1527{
Mathias Krause7b789832012-09-19 11:33:40 +00001528 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1530 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1531 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1532 p->priority = xp->priority;
1533 p->index = xp->index;
1534 p->sel.family = xp->family;
1535 p->dir = dir;
1536 p->action = xp->action;
1537 p->flags = xp->flags;
1538 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1539}
1540
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001541static 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 -07001542{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001543 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int err;
1545
1546 if (!xp) {
1547 *errp = -ENOMEM;
1548 return NULL;
1549 }
1550
1551 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001552
Thomas Graf35a7aa02007-08-22 14:00:40 -07001553 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001554 if (err)
1555 goto error;
1556
Thomas Graf35a7aa02007-08-22 14:00:40 -07001557 if (!(err = copy_from_user_tmpl(xp, attrs)))
1558 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001559 if (err)
1560 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001562 xfrm_mark_get(attrs, &xp->mark);
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001565 error:
1566 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001567 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001568 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001569 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Christoph Hellwig22e70052007-01-02 15:22:30 -08001572static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001573 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001575 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001576 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001578 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int err;
1580 int excl;
1581
1582 err = verify_newpolicy_info(p);
1583 if (err)
1584 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001585 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001586 if (err)
1587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001589 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (!xp)
1591 return err;
1592
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001593 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001594 * Aha! this is anti-netlink really i.e more pfkey derived
1595 * in netlink excl is a flag and you wouldnt need
1596 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1598 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001599 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001602 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 kfree(xp);
1604 return err;
1605 }
1606
Herbert Xuf60f6b82005-06-18 22:44:37 -07001607 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001608 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001609 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001610 km_policy_notify(xp, p->dir, &c);
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 xfrm_pol_put(xp);
1613
1614 return 0;
1615}
1616
1617static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1618{
1619 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1620 int i;
1621
1622 if (xp->xfrm_nr == 0)
1623 return 0;
1624
1625 for (i = 0; i < xp->xfrm_nr; i++) {
1626 struct xfrm_user_tmpl *up = &vec[i];
1627 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1628
Mathias Krause1f868402012-09-19 11:33:41 +00001629 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001631 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1633 up->reqid = kp->reqid;
1634 up->mode = kp->mode;
1635 up->share = kp->share;
1636 up->optional = kp->optional;
1637 up->aalgos = kp->aalgos;
1638 up->ealgos = kp->ealgos;
1639 up->calgos = kp->calgos;
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Thomas Grafc0144be2007-08-22 13:55:43 -07001642 return nla_put(skb, XFRMA_TMPL,
1643 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001644}
1645
Serge Hallyn0d681622006-07-24 23:30:44 -07001646static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1647{
1648 if (x->security) {
1649 return copy_sec_ctx(x->security, skb);
1650 }
1651 return 0;
1652}
1653
1654static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1655{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001656 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001657 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001658 return 0;
1659}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001660static inline size_t userpolicy_type_attrsize(void)
1661{
1662#ifdef CONFIG_XFRM_SUB_POLICY
1663 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1664#else
1665 return 0;
1666#endif
1667}
Serge Hallyn0d681622006-07-24 23:30:44 -07001668
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001669#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001670static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001671{
Eric Dumazet2038a9e2018-06-18 21:35:07 -07001672 struct xfrm_userpolicy_type upt;
1673
1674 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1675 memset(&upt, 0, sizeof(upt));
1676 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001677
Thomas Grafc0144be2007-08-22 13:55:43 -07001678 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001679}
1680
1681#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001682static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001683{
1684 return 0;
1685}
1686#endif
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1689{
1690 struct xfrm_dump_info *sp = ptr;
1691 struct xfrm_userpolicy_info *p;
1692 struct sk_buff *in_skb = sp->in_skb;
1693 struct sk_buff *skb = sp->out_skb;
1694 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001695 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Eric W. Biederman15e47302012-09-07 20:12:54 +00001697 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001698 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1699 if (nlh == NULL)
1700 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Thomas Graf7b67c852007-08-22 13:53:52 -07001702 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001704 err = copy_to_user_tmpl(xp, skb);
1705 if (!err)
1706 err = copy_to_user_sec_ctx(xp, skb);
1707 if (!err)
1708 err = copy_to_user_policy_type(xp->type, skb);
1709 if (!err)
1710 err = xfrm_mark_put(skb, &xp->mark);
1711 if (err) {
1712 nlmsg_cancel(skb, nlh);
1713 return err;
1714 }
Thomas Graf98250692007-08-22 12:47:26 -07001715 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Timo Teras4c563f72008-02-28 21:31:08 -08001719static int xfrm_dump_policy_done(struct netlink_callback *cb)
1720{
Herbert Xu543aabb2017-10-19 20:51:10 +08001721 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Fan Du283bc9f2013-11-07 17:47:50 +08001722 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001723
Fan Du283bc9f2013-11-07 17:47:50 +08001724 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001725 return 0;
1726}
1727
Herbert Xu543aabb2017-10-19 20:51:10 +08001728static int xfrm_dump_policy_start(struct netlink_callback *cb)
1729{
1730 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1731
1732 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1733
1734 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1735 return 0;
1736}
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1739{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001740 struct net *net = sock_net(skb->sk);
Herbert Xu543aabb2017-10-19 20:51:10 +08001741 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 struct xfrm_dump_info info;
1743
1744 info.in_skb = cb->skb;
1745 info.out_skb = skb;
1746 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1747 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001748
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001749 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 return skb->len;
1752}
1753
1754static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1755 struct xfrm_policy *xp,
1756 int dir, u32 seq)
1757{
1758 struct xfrm_dump_info info;
1759 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001760 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Thomas Graf7deb2262007-08-22 13:57:39 -07001762 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (!skb)
1764 return ERR_PTR(-ENOMEM);
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 info.in_skb = in_skb;
1767 info.out_skb = skb;
1768 info.nlmsg_seq = seq;
1769 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Mathias Krausec2546372012-09-14 09:58:32 +00001771 err = dump_one_policy(xp, dir, 0, &info);
1772 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001774 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776
1777 return skb;
1778}
1779
Christoph Hellwig22e70052007-01-02 15:22:30 -08001780static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001781 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001783 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct xfrm_policy *xp;
1785 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001786 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001788 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001790 struct xfrm_mark m;
1791 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Thomas Graf7b67c852007-08-22 13:53:52 -07001793 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1795
Thomas Graf35a7aa02007-08-22 14:00:40 -07001796 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001797 if (err)
1798 return err;
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 err = verify_policy_dir(p->dir);
1801 if (err)
1802 return err;
1803
1804 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001805 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001806 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001807 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001808 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001809
Thomas Graf35a7aa02007-08-22 14:00:40 -07001810 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001811 if (err)
1812 return err;
1813
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001814 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001815 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001816 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001817
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001818 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001819 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001820 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001821 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001822 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001823 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001824 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (xp == NULL)
1827 return -ENOENT;
1828
1829 if (!delete) {
1830 struct sk_buff *resp_skb;
1831
1832 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1833 if (IS_ERR(resp_skb)) {
1834 err = PTR_ERR(resp_skb);
1835 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001836 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001837 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001839 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001840 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001841
1842 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001843 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001844
Herbert Xue7443892005-06-18 22:44:18 -07001845 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001846 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001847 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001848 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001849 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001852out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001853 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001854 if (delete && err == 0)
1855 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return err;
1857}
1858
Christoph Hellwig22e70052007-01-02 15:22:30 -08001859static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001860 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001862 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001863 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001864 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001865 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Tetsuo Handa2e710292014-04-22 21:48:30 +09001867 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001868 if (err) {
1869 if (err == -ESRCH) /* empty table */
1870 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001871 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001872 }
Herbert Xubf088672005-06-18 22:44:00 -07001873 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001874 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001875 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001876 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001877 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001878 km_state_notify(NULL, &c);
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return 0;
1881}
1882
Steffen Klassertd8647b72011-03-08 00:10:27 +00001883static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001884{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001885 size_t replay_size = x->replay_esn ?
1886 xfrm_replay_state_esn_len(x->replay_esn) :
1887 sizeof(struct xfrm_replay_state);
1888
Thomas Graf7deb2262007-08-22 13:57:39 -07001889 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001890 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001891 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001892 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001893 + nla_total_size(4) /* XFRM_AE_RTHR */
1894 + nla_total_size(4); /* XFRM_AE_ETHR */
1895}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001896
David S. Miller214e0052011-02-24 00:02:38 -05001897static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001898{
1899 struct xfrm_aevent_id *id;
1900 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001901 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001902
Eric W. Biederman15e47302012-09-07 20:12:54 +00001903 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001904 if (nlh == NULL)
1905 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001906
Thomas Graf7b67c852007-08-22 13:53:52 -07001907 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001908 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001909 id->sa_id.spi = x->id.spi;
1910 id->sa_id.family = x->props.family;
1911 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001912 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001913 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001914 id->flags = c->data.aevent;
1915
David S. Millerd0fde792012-03-29 04:02:26 -04001916 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001917 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1918 xfrm_replay_state_esn_len(x->replay_esn),
1919 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001920 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001921 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1922 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001923 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001924 if (err)
1925 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001926 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1927 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001928 if (err)
1929 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001930
David S. Miller1d1e34d2012-06-27 21:57:03 -07001931 if (id->flags & XFRM_AE_RTHR) {
1932 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1933 if (err)
1934 goto out_cancel;
1935 }
1936 if (id->flags & XFRM_AE_ETHR) {
1937 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1938 x->replay_maxage * 10 / HZ);
1939 if (err)
1940 goto out_cancel;
1941 }
1942 err = xfrm_mark_put(skb, &x->mark);
1943 if (err)
1944 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001945
Johannes Berg053c0952015-01-16 22:09:00 +01001946 nlmsg_end(skb, nlh);
1947 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001948
David S. Miller1d1e34d2012-06-27 21:57:03 -07001949out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001950 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001951 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001952}
1953
Christoph Hellwig22e70052007-01-02 15:22:30 -08001954static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001955 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001956{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001957 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001958 struct xfrm_state *x;
1959 struct sk_buff *r_skb;
1960 int err;
1961 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001962 u32 mark;
1963 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001964 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001965 struct xfrm_usersa_id *id = &p->sa_id;
1966
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001967 mark = xfrm_mark_get(attrs, &m);
1968
1969 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001970 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001971 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001972
1973 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1974 if (r_skb == NULL) {
1975 xfrm_state_put(x);
1976 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001977 }
1978
1979 /*
1980 * XXX: is this lock really needed - none of the other
1981 * gets lock (the concern is things getting updated
1982 * while we are still reading) - jhs
1983 */
1984 spin_lock_bh(&x->lock);
1985 c.data.aevent = p->flags;
1986 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001987 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001988
1989 if (build_aevent(r_skb, x, &c) < 0)
1990 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001991 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001992 spin_unlock_bh(&x->lock);
1993 xfrm_state_put(x);
1994 return err;
1995}
1996
Christoph Hellwig22e70052007-01-02 15:22:30 -08001997static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001998 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001999{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002000 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002001 struct xfrm_state *x;
2002 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08002003 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002004 u32 mark = 0;
2005 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07002006 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07002007 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00002008 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07002009 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02002010 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2011 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002012
Michael Rossberg4e077232015-09-29 11:25:08 +02002013 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002014 return err;
2015
2016 /* pedantic mode - thou shalt sayeth replaceth */
2017 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2018 return err;
2019
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002020 mark = xfrm_mark_get(attrs, &m);
2021
2022 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 -08002023 if (x == NULL)
2024 return -ESRCH;
2025
2026 if (x->km.state != XFRM_STATE_VALID)
2027 goto out;
2028
Steffen Klassert4479ff72013-09-09 09:39:01 +02002029 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00002030 if (err)
2031 goto out;
2032
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002033 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00002034 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002035 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002036
2037 c.event = nlh->nlmsg_type;
2038 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002039 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002040 c.data.aevent = XFRM_AE_CU;
2041 km_state_notify(x, &c);
2042 err = 0;
2043out:
2044 xfrm_state_put(x);
2045 return err;
2046}
2047
Christoph Hellwig22e70052007-01-02 15:22:30 -08002048static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002049 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002051 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002052 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002053 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002054 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002055
Thomas Graf35a7aa02007-08-22 14:00:40 -07002056 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002057 if (err)
2058 return err;
2059
Tetsuo Handa2e710292014-04-22 21:48:30 +09002060 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002061 if (err) {
2062 if (err == -ESRCH) /* empty table */
2063 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002064 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002065 }
2066
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002067 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002068 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002069 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002070 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002071 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002072 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return 0;
2074}
2075
Christoph Hellwig22e70052007-01-02 15:22:30 -08002076static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002077 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002078{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002079 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002080 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002081 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002082 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002083 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002084 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002085 struct xfrm_mark m;
2086 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002087
Thomas Graf35a7aa02007-08-22 14:00:40 -07002088 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002089 if (err)
2090 return err;
2091
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002092 err = verify_policy_dir(p->dir);
2093 if (err)
2094 return err;
2095
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002096 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002097 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002098 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002099 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002100 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002101
Thomas Graf35a7aa02007-08-22 14:00:40 -07002102 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002103 if (err)
2104 return err;
2105
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002106 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002107 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002108 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002109
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002110 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002111 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002112 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002113 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002114 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002115 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002116 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002117 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002118 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002119 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002120
Timo Teräsea2dea92010-03-31 00:17:05 +00002121 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002122 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002123
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002124 err = 0;
2125 if (up->hard) {
2126 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002127 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002128 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002129 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002130
2131out:
2132 xfrm_pol_put(xp);
2133 return err;
2134}
2135
Christoph Hellwig22e70052007-01-02 15:22:30 -08002136static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002137 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002138{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002139 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002140 struct xfrm_state *x;
2141 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002142 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002143 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002144 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002145 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002146
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002147 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 -08002148
David S. Miller3a765aa2007-02-26 14:52:21 -08002149 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002150 if (x == NULL)
2151 return err;
2152
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002153 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002154 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002155 if (x->km.state != XFRM_STATE_VALID)
2156 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002157 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002158
Joy Latten161a09e2006-11-27 13:11:54 -06002159 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002160 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002161 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002162 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002163 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002164out:
2165 spin_unlock_bh(&x->lock);
2166 xfrm_state_put(x);
2167 return err;
2168}
2169
Christoph Hellwig22e70052007-01-02 15:22:30 -08002170static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002171 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002172{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002173 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002174 struct xfrm_policy *xp;
2175 struct xfrm_user_tmpl *ut;
2176 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002177 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002178 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002179
Thomas Graf7b67c852007-08-22 13:53:52 -07002180 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002181 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002182 int err = -ENOMEM;
2183
2184 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002185 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002186
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002187 xfrm_mark_get(attrs, &mark);
2188
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002189 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002190 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002191 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002192
2193 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002194 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002195 if (!xp)
2196 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002197
2198 memcpy(&x->id, &ua->id, sizeof(ua->id));
2199 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2200 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002201 xp->mark.m = x->mark.m = mark.m;
2202 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002203 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002204 /* extract the templates and for each call km_key */
2205 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2206 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2207 memcpy(&x->id, &t->id, sizeof(x->id));
2208 x->props.mode = t->mode;
2209 x->props.reqid = t->reqid;
2210 x->props.family = ut->family;
2211 t->aalgos = ua->aalgos;
2212 t->ealgos = ua->ealgos;
2213 t->calgos = ua->calgos;
2214 err = km_query(x, t, xp);
2215
2216 }
2217
2218 kfree(x);
2219 kfree(xp);
2220
2221 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002222
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002223free_state:
2224 kfree(x);
2225nomem:
2226 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002227}
2228
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002229#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002230static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002231 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002232 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002233{
Thomas Graf5424f322007-08-22 14:01:33 -07002234 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002235 struct xfrm_user_migrate *um;
2236 int i, num_migrate;
2237
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002238 if (k != NULL) {
2239 struct xfrm_user_kmaddress *uk;
2240
2241 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2242 memcpy(&k->local, &uk->local, sizeof(k->local));
2243 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2244 k->family = uk->family;
2245 k->reserved = uk->reserved;
2246 }
2247
Thomas Graf5424f322007-08-22 14:01:33 -07002248 um = nla_data(rt);
2249 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002250
2251 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2252 return -EINVAL;
2253
2254 for (i = 0; i < num_migrate; i++, um++, ma++) {
2255 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2256 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2257 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2258 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2259
2260 ma->proto = um->proto;
2261 ma->mode = um->mode;
2262 ma->reqid = um->reqid;
2263
2264 ma->old_family = um->old_family;
2265 ma->new_family = um->new_family;
2266 }
2267
2268 *num = i;
2269 return 0;
2270}
2271
2272static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002273 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002274{
Thomas Graf7b67c852007-08-22 13:53:52 -07002275 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002276 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002277 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002278 u8 type;
2279 int err;
2280 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002281 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002282
Thomas Graf35a7aa02007-08-22 14:00:40 -07002283 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002284 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002285
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002286 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2287
Thomas Graf5424f322007-08-22 14:01:33 -07002288 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002289 if (err)
2290 return err;
2291
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002292 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002293 if (err)
2294 return err;
2295
2296 if (!n)
2297 return 0;
2298
Fan Du8d549c42013-11-07 17:47:49 +08002299 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002300
2301 return 0;
2302}
2303#else
2304static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002305 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002306{
2307 return -ENOPROTOOPT;
2308}
2309#endif
2310
2311#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002312static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002313{
2314 struct xfrm_user_migrate um;
2315
2316 memset(&um, 0, sizeof(um));
2317 um.proto = m->proto;
2318 um.mode = m->mode;
2319 um.reqid = m->reqid;
2320 um.old_family = m->old_family;
2321 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2322 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2323 um.new_family = m->new_family;
2324 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2325 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2326
Thomas Grafc0144be2007-08-22 13:55:43 -07002327 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002328}
2329
David S. Miller183cad12011-02-24 00:28:01 -05002330static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002331{
2332 struct xfrm_user_kmaddress uk;
2333
2334 memset(&uk, 0, sizeof(uk));
2335 uk.family = k->family;
2336 uk.reserved = k->reserved;
2337 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002338 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002339
2340 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2341}
2342
2343static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002344{
2345 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002346 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2347 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2348 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002349}
2350
David S. Miller183cad12011-02-24 00:28:01 -05002351static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2352 int num_migrate, const struct xfrm_kmaddress *k,
2353 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002354{
David S. Miller183cad12011-02-24 00:28:01 -05002355 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002356 struct xfrm_userpolicy_id *pol_id;
2357 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002358 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002359
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002360 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2361 if (nlh == NULL)
2362 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002363
Thomas Graf7b67c852007-08-22 13:53:52 -07002364 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002365 /* copy data from selector, dir, and type to the pol_id */
2366 memset(pol_id, 0, sizeof(*pol_id));
2367 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2368 pol_id->dir = dir;
2369
David S. Miller1d1e34d2012-06-27 21:57:03 -07002370 if (k != NULL) {
2371 err = copy_to_user_kmaddress(k, skb);
2372 if (err)
2373 goto out_cancel;
2374 }
2375 err = copy_to_user_policy_type(type, skb);
2376 if (err)
2377 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002378 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002379 err = copy_to_user_migrate(mp, skb);
2380 if (err)
2381 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002382 }
2383
Johannes Berg053c0952015-01-16 22:09:00 +01002384 nlmsg_end(skb, nlh);
2385 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002386
2387out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002388 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002389 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002390}
2391
David S. Miller183cad12011-02-24 00:28:01 -05002392static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2393 const struct xfrm_migrate *m, int num_migrate,
2394 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002395{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002396 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002397 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002398
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002399 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002400 if (skb == NULL)
2401 return -ENOMEM;
2402
2403 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002404 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002405 BUG();
2406
Michal Kubecek21ee5432014-06-03 10:26:06 +02002407 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002408}
2409#else
David S. Miller183cad12011-02-24 00:28:01 -05002410static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2411 const struct xfrm_migrate *m, int num_migrate,
2412 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002413{
2414 return -ENOPROTOOPT;
2415}
2416#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002417
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002418#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002419
2420static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002421 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002422 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2423 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2424 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2425 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2426 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2427 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002428 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002429 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002430 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002431 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002432 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002433 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002434 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002435 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2436 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002437 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002438 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002439 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002440 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002441 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442};
2443
Thomas Graf492b5582005-05-03 14:26:40 -07002444#undef XMSGSIZE
2445
Thomas Grafcf5cb792007-08-22 13:59:04 -07002446static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002447 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2448 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2449 [XFRMA_LASTUSED] = { .type = NLA_U64},
2450 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002451 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002452 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2453 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2454 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2455 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2456 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2457 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2458 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2459 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2460 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2461 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2462 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2463 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2464 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2465 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002466 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002467 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002468 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002469 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002470 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002471 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002472 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002473};
2474
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002475static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2476 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2477 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2478};
2479
Mathias Krause05600a72013-02-24 14:10:27 +01002480static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002481 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Herbert Xu543aabb2017-10-19 20:51:10 +08002482 int (*start)(struct netlink_callback *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002484 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002485 const struct nla_policy *nla_pol;
2486 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002487} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2488 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2489 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2490 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002491 .dump = xfrm_dump_sa,
2492 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002493 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2494 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2495 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Herbert Xu543aabb2017-10-19 20:51:10 +08002496 .start = xfrm_dump_policy_start,
Timo Teras4c563f72008-02-28 21:31:08 -08002497 .dump = xfrm_dump_policy,
2498 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002499 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002500 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002501 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002502 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2503 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002504 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002505 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2506 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002507 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2508 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002509 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002510 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002511 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2512 .nla_pol = xfrma_spd_policy,
2513 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002514 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515};
2516
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002517static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002519 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002520 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002521 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002522 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Fan Du74005992015-01-27 17:00:29 +08002524#ifdef CONFIG_COMPAT
Andy Lutomirski2bf8c472016-03-22 14:25:10 -07002525 if (in_compat_syscall())
Yi Zhao83e2d052016-11-29 18:09:01 +08002526 return -EOPNOTSUPP;
Fan Du74005992015-01-27 17:00:29 +08002527#endif
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002531 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 type -= XFRM_MSG_BASE;
2534 link = &xfrm_dispatch[type];
2535
2536 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002537 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002538 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Thomas Graf492b5582005-05-03 14:26:40 -07002540 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2541 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002542 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002544 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002546 {
2547 struct netlink_dump_control c = {
Herbert Xu543aabb2017-10-19 20:51:10 +08002548 .start = link->start,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002549 .dump = link->dump,
2550 .done = link->done,
2551 };
2552 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002556 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2557 link->nla_max ? : XFRMA_MAX,
2558 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002559 if (err < 0)
2560 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002563 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Thomas Graf5424f322007-08-22 14:01:33 -07002565 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002568static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
Fan Du283bc9f2013-11-07 17:47:50 +08002570 struct net *net = sock_net(skb->sk);
2571
2572 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002573 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002574 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Thomas Graf7deb2262007-08-22 13:57:39 -07002577static inline size_t xfrm_expire_msgsize(void)
2578{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002579 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2580 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002581}
2582
David S. Miller214e0052011-02-24 00:02:38 -05002583static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
2585 struct xfrm_user_expire *ue;
2586 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002587 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Eric W. Biederman15e47302012-09-07 20:12:54 +00002589 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002590 if (nlh == NULL)
2591 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Thomas Graf7b67c852007-08-22 13:53:52 -07002593 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002595 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
David S. Miller1d1e34d2012-06-27 21:57:03 -07002597 err = xfrm_mark_put(skb, &x->mark);
2598 if (err)
2599 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002600
Johannes Berg053c0952015-01-16 22:09:00 +01002601 nlmsg_end(skb, nlh);
2602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
David S. Miller214e0052011-02-24 00:02:38 -05002605static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002607 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct sk_buff *skb;
2609
Thomas Graf7deb2262007-08-22 13:57:39 -07002610 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (skb == NULL)
2612 return -ENOMEM;
2613
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002614 if (build_expire(skb, x, c) < 0) {
2615 kfree_skb(skb);
2616 return -EMSGSIZE;
2617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Michal Kubecek21ee5432014-06-03 10:26:06 +02002619 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
David S. Miller214e0052011-02-24 00:02:38 -05002622static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002623{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002624 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002625 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002626
Steffen Klassertd8647b72011-03-08 00:10:27 +00002627 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002628 if (skb == NULL)
2629 return -ENOMEM;
2630
2631 if (build_aevent(skb, x, c) < 0)
2632 BUG();
2633
Michal Kubecek21ee5432014-06-03 10:26:06 +02002634 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002635}
2636
David S. Miller214e0052011-02-24 00:02:38 -05002637static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002638{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002639 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002640 struct xfrm_usersa_flush *p;
2641 struct nlmsghdr *nlh;
2642 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002643 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002644
Thomas Graf7deb2262007-08-22 13:57:39 -07002645 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002646 if (skb == NULL)
2647 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002648
Eric W. Biederman15e47302012-09-07 20:12:54 +00002649 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002650 if (nlh == NULL) {
2651 kfree_skb(skb);
2652 return -EMSGSIZE;
2653 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002654
Thomas Graf7b67c852007-08-22 13:53:52 -07002655 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002656 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002657
Thomas Graf98250692007-08-22 12:47:26 -07002658 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002659
Michal Kubecek21ee5432014-06-03 10:26:06 +02002660 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002661}
2662
Thomas Graf7deb2262007-08-22 13:57:39 -07002663static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002664{
Thomas Graf7deb2262007-08-22 13:57:39 -07002665 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002666 if (x->aead)
2667 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002668 if (x->aalg) {
2669 l += nla_total_size(sizeof(struct xfrm_algo) +
2670 (x->aalg->alg_key_len + 7) / 8);
2671 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2672 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002673 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002674 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002675 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002676 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002677 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002678 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002679 if (x->tfcpad)
2680 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002681 if (x->replay_esn)
2682 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002683 else
2684 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002685 if (x->security)
2686 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2687 x->security->ctx_len);
2688 if (x->coaddr)
2689 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002690 if (x->props.extra_flags)
2691 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002692
Herbert Xud26f3982007-11-13 21:47:08 -08002693 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002694 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002695
2696 return l;
2697}
2698
David S. Miller214e0052011-02-24 00:02:38 -05002699static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002700{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002701 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002702 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002703 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002704 struct nlmsghdr *nlh;
2705 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002706 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002707 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002708
2709 headlen = sizeof(*p);
2710 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002711 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002712 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002713 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002714 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002715 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002716
Thomas Graf7deb2262007-08-22 13:57:39 -07002717 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002718 if (skb == NULL)
2719 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002720
Eric W. Biederman15e47302012-09-07 20:12:54 +00002721 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002722 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002723 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002724 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002725
Thomas Graf7b67c852007-08-22 13:53:52 -07002726 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002727 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002728 struct nlattr *attr;
2729
Thomas Graf7b67c852007-08-22 13:53:52 -07002730 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002731 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2732 id->spi = x->id.spi;
2733 id->family = x->props.family;
2734 id->proto = x->id.proto;
2735
Thomas Grafc0144be2007-08-22 13:55:43 -07002736 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002737 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002738 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002739 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002740
2741 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002742 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002743 err = copy_to_user_state_extra(x, p, skb);
2744 if (err)
2745 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002746
Thomas Graf98250692007-08-22 12:47:26 -07002747 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002748
Michal Kubecek21ee5432014-06-03 10:26:06 +02002749 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002750
David S. Miller1d1e34d2012-06-27 21:57:03 -07002751out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002752 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002753 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002754}
2755
David S. Miller214e0052011-02-24 00:02:38 -05002756static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002757{
2758
2759 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002760 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002761 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002762 case XFRM_MSG_NEWAE:
2763 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002764 case XFRM_MSG_DELSA:
2765 case XFRM_MSG_UPDSA:
2766 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002767 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002768 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002769 return xfrm_notify_sa_flush(c);
2770 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002771 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2772 c->event);
2773 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002774 }
2775
2776 return 0;
2777
2778}
2779
Thomas Graf7deb2262007-08-22 13:57:39 -07002780static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2781 struct xfrm_policy *xp)
2782{
2783 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2784 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002785 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002786 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2787 + userpolicy_type_attrsize();
2788}
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002791 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002793 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct xfrm_user_acquire *ua;
2795 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002796 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002798 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2799 if (nlh == NULL)
2800 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Thomas Graf7b67c852007-08-22 13:53:52 -07002802 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 memcpy(&ua->id, &x->id, sizeof(ua->id));
2804 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2805 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002806 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 ua->aalgos = xt->aalgos;
2808 ua->ealgos = xt->ealgos;
2809 ua->calgos = xt->calgos;
2810 ua->seq = x->km.seq = seq;
2811
David S. Miller1d1e34d2012-06-27 21:57:03 -07002812 err = copy_to_user_tmpl(xp, skb);
2813 if (!err)
2814 err = copy_to_user_state_sec_ctx(x, skb);
2815 if (!err)
2816 err = copy_to_user_policy_type(xp->type, skb);
2817 if (!err)
2818 err = xfrm_mark_put(skb, &xp->mark);
2819 if (err) {
2820 nlmsg_cancel(skb, nlh);
2821 return err;
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Johannes Berg053c0952015-01-16 22:09:00 +01002824 nlmsg_end(skb, nlh);
2825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002829 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002831 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Thomas Graf7deb2262007-08-22 13:57:39 -07002834 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (skb == NULL)
2836 return -ENOMEM;
2837
Fan Du65e07362012-08-15 10:13:47 +08002838 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 BUG();
2840
Michal Kubecek21ee5432014-06-03 10:26:06 +02002841 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
2844/* User gives us xfrm_user_policy_info followed by an array of 0
2845 * or more templates.
2846 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002847static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 u8 *data, int len, int *dir)
2849{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002850 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2852 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2853 struct xfrm_policy *xp;
2854 int nr;
2855
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002856 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 case AF_INET:
2858 if (opt != IP_XFRM_POLICY) {
2859 *dir = -EOPNOTSUPP;
2860 return NULL;
2861 }
2862 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002863#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 case AF_INET6:
2865 if (opt != IPV6_XFRM_POLICY) {
2866 *dir = -EOPNOTSUPP;
2867 return NULL;
2868 }
2869 break;
2870#endif
2871 default:
2872 *dir = -EINVAL;
2873 return NULL;
2874 }
2875
2876 *dir = -EINVAL;
2877
2878 if (len < sizeof(*p) ||
2879 verify_newpolicy_info(p))
2880 return NULL;
2881
2882 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002883 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return NULL;
2885
Herbert Xua4f1bac2005-07-26 15:43:17 -07002886 if (p->dir > XFRM_POLICY_OUT)
2887 return NULL;
2888
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002889 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (xp == NULL) {
2891 *dir = -ENOBUFS;
2892 return NULL;
2893 }
2894
2895 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002896 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 copy_templates(xp, ut, nr);
2898
2899 *dir = p->dir;
2900
2901 return xp;
2902}
2903
Thomas Graf7deb2262007-08-22 13:57:39 -07002904static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2905{
2906 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2907 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2908 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002909 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002910 + userpolicy_type_attrsize();
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002914 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002917 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002918 struct nlmsghdr *nlh;
2919 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Eric W. Biederman15e47302012-09-07 20:12:54 +00002921 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002922 if (nlh == NULL)
2923 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Thomas Graf7b67c852007-08-22 13:53:52 -07002925 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002927 err = copy_to_user_tmpl(xp, skb);
2928 if (!err)
2929 err = copy_to_user_sec_ctx(xp, skb);
2930 if (!err)
2931 err = copy_to_user_policy_type(xp->type, skb);
2932 if (!err)
2933 err = xfrm_mark_put(skb, &xp->mark);
2934 if (err) {
2935 nlmsg_cancel(skb, nlh);
2936 return err;
2937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 upe->hard = !!hard;
2939
Johannes Berg053c0952015-01-16 22:09:00 +01002940 nlmsg_end(skb, nlh);
2941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
David S. Miller214e0052011-02-24 00:02:38 -05002944static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002946 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Thomas Graf7deb2262007-08-22 13:57:39 -07002949 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (skb == NULL)
2951 return -ENOMEM;
2952
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002953 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 BUG();
2955
Michal Kubecek21ee5432014-06-03 10:26:06 +02002956 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
David S. Miller214e0052011-02-24 00:02:38 -05002959static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002960{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002961 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002962 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002963 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002964 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002965 struct nlmsghdr *nlh;
2966 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002967 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002968
2969 headlen = sizeof(*p);
2970 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002971 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002972 headlen = sizeof(*id);
2973 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002974 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002975 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002976 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002977
Thomas Graf7deb2262007-08-22 13:57:39 -07002978 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002979 if (skb == NULL)
2980 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002981
Eric W. Biederman15e47302012-09-07 20:12:54 +00002982 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002983 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002984 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002985 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002986
Thomas Graf7b67c852007-08-22 13:53:52 -07002987 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002988 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002989 struct nlattr *attr;
2990
Thomas Graf7b67c852007-08-22 13:53:52 -07002991 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002992 memset(id, 0, sizeof(*id));
2993 id->dir = dir;
2994 if (c->data.byid)
2995 id->index = xp->index;
2996 else
2997 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2998
Thomas Grafc0144be2007-08-22 13:55:43 -07002999 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07003000 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07003001 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003002 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07003003
3004 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07003005 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003006
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003007 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003008 err = copy_to_user_tmpl(xp, skb);
3009 if (!err)
3010 err = copy_to_user_policy_type(xp->type, skb);
3011 if (!err)
3012 err = xfrm_mark_put(skb, &xp->mark);
3013 if (err)
3014 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003015
Thomas Graf98250692007-08-22 12:47:26 -07003016 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003017
Michal Kubecek21ee5432014-06-03 10:26:06 +02003018 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003019
David S. Miller1d1e34d2012-06-27 21:57:03 -07003020out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003021 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003022 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003023}
3024
David S. Miller214e0052011-02-24 00:02:38 -05003025static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003026{
Alexey Dobriyan70678022008-11-25 17:50:36 -08003027 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003028 struct nlmsghdr *nlh;
3029 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003030 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003031
Thomas Graf7deb2262007-08-22 13:57:39 -07003032 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003033 if (skb == NULL)
3034 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003035
Eric W. Biederman15e47302012-09-07 20:12:54 +00003036 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003037 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003038 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003039 goto out_free_skb;
3040 err = copy_to_user_policy_type(c->data.type, skb);
3041 if (err)
3042 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003043
Thomas Graf98250692007-08-22 12:47:26 -07003044 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003045
Michal Kubecek21ee5432014-06-03 10:26:06 +02003046 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003047
David S. Miller1d1e34d2012-06-27 21:57:03 -07003048out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003049 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003050 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003051}
3052
David S. Miller214e0052011-02-24 00:02:38 -05003053static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003054{
3055
3056 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003057 case XFRM_MSG_NEWPOLICY:
3058 case XFRM_MSG_UPDPOLICY:
3059 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003060 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003061 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003062 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003063 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003064 return xfrm_exp_policy_notify(xp, dir, c);
3065 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003066 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3067 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003068 }
3069
3070 return 0;
3071
3072}
3073
Thomas Graf7deb2262007-08-22 13:57:39 -07003074static inline size_t xfrm_report_msgsize(void)
3075{
3076 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3077}
3078
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003079static int build_report(struct sk_buff *skb, u8 proto,
3080 struct xfrm_selector *sel, xfrm_address_t *addr)
3081{
3082 struct xfrm_user_report *ur;
3083 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003084
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003085 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3086 if (nlh == NULL)
3087 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003088
Thomas Graf7b67c852007-08-22 13:53:52 -07003089 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003090 ur->proto = proto;
3091 memcpy(&ur->sel, sel, sizeof(ur->sel));
3092
David S. Miller1d1e34d2012-06-27 21:57:03 -07003093 if (addr) {
3094 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3095 if (err) {
3096 nlmsg_cancel(skb, nlh);
3097 return err;
3098 }
3099 }
Johannes Berg053c0952015-01-16 22:09:00 +01003100 nlmsg_end(skb, nlh);
3101 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003102}
3103
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003104static int xfrm_send_report(struct net *net, u8 proto,
3105 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003106{
3107 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003108
Thomas Graf7deb2262007-08-22 13:57:39 -07003109 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003110 if (skb == NULL)
3111 return -ENOMEM;
3112
3113 if (build_report(skb, proto, sel, addr) < 0)
3114 BUG();
3115
Michal Kubecek21ee5432014-06-03 10:26:06 +02003116 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003117}
3118
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003119static inline size_t xfrm_mapping_msgsize(void)
3120{
3121 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3122}
3123
3124static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3125 xfrm_address_t *new_saddr, __be16 new_sport)
3126{
3127 struct xfrm_user_mapping *um;
3128 struct nlmsghdr *nlh;
3129
3130 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3131 if (nlh == NULL)
3132 return -EMSGSIZE;
3133
3134 um = nlmsg_data(nlh);
3135
3136 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3137 um->id.spi = x->id.spi;
3138 um->id.family = x->props.family;
3139 um->id.proto = x->id.proto;
3140 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3141 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3142 um->new_sport = new_sport;
3143 um->old_sport = x->encap->encap_sport;
3144 um->reqid = x->props.reqid;
3145
Johannes Berg053c0952015-01-16 22:09:00 +01003146 nlmsg_end(skb, nlh);
3147 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003148}
3149
3150static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3151 __be16 sport)
3152{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003153 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003154 struct sk_buff *skb;
3155
3156 if (x->id.proto != IPPROTO_ESP)
3157 return -EINVAL;
3158
3159 if (!x->encap)
3160 return -EINVAL;
3161
3162 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3163 if (skb == NULL)
3164 return -ENOMEM;
3165
3166 if (build_mapping(skb, x, ipaddr, sport) < 0)
3167 BUG();
3168
Michal Kubecek21ee5432014-06-03 10:26:06 +02003169 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003170}
3171
Horia Geanta0f245582014-02-12 16:20:06 +02003172static bool xfrm_is_alive(const struct km_event *c)
3173{
3174 return (bool)xfrm_acquire_is_on(c->net);
3175}
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177static struct xfrm_mgr netlink_mgr = {
3178 .id = "netlink",
3179 .notify = xfrm_send_state_notify,
3180 .acquire = xfrm_send_acquire,
3181 .compile_policy = xfrm_compile_policy,
3182 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003183 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003184 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003185 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003186 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187};
3188
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003189static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Patrick McHardybe336902006-03-20 22:40:54 -08003191 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003192 struct netlink_kernel_cfg cfg = {
3193 .groups = XFRMNLGRP_MAX,
3194 .input = xfrm_netlink_rcv,
3195 };
Patrick McHardybe336902006-03-20 22:40:54 -08003196
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003197 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003198 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003200 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003201 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 return 0;
3203}
3204
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003205static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003206{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003207 struct net *net;
3208 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003209 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003210 synchronize_net();
3211 list_for_each_entry(net, net_exit_list, exit_list)
3212 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003213}
3214
3215static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003216 .init = xfrm_user_net_init,
3217 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003218};
3219
3220static int __init xfrm_user_init(void)
3221{
3222 int rv;
3223
3224 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3225
3226 rv = register_pernet_subsys(&xfrm_user_net_ops);
3227 if (rv < 0)
3228 return rv;
3229 rv = xfrm_register_km(&netlink_mgr);
3230 if (rv < 0)
3231 unregister_pernet_subsys(&xfrm_user_net_ops);
3232 return rv;
3233}
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235static void __exit xfrm_user_exit(void)
3236{
3237 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003238 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
3241module_init(xfrm_user_init);
3242module_exit(xfrm_user_exit);
3243MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003244MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003245