blob: 8cd6c8129004d238083306958502f9c41ffa5ceb [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* xfrm_user.c: User interface to configure xfrm engine.
3 *
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 *
6 * Changes:
7 * Mitsuru KANDA @USAGI
8 * Kazunori MIYAZAWA @USAGI
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Jakub Kicinskib6459412021-12-28 16:49:13 -080014#include <linux/compat.h>
Herbert Xu9409f382006-08-06 19:49:12 +100015#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/types.h>
19#include <linux/slab.h>
20#include <linux/socket.h>
21#include <linux/string.h>
22#include <linux/net.h>
23#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/pfkeyv2.h>
25#include <linux/ipsec.h>
26#include <linux/init.h>
27#include <linux/security.h>
28#include <net/sock.h>
29#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010030#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000031#include <net/ah.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080032#include <linux/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000033#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070034#include <linux/in6.h>
35#endif
Sowmini Varadhane33d4f12015-10-21 11:48:25 -040036#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Graf5424f322007-08-22 14:01:33 -070038static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Thomas Graf5424f322007-08-22 14:01:33 -070040 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct xfrm_algo *algp;
42
43 if (!rt)
44 return 0;
45
Thomas Graf5424f322007-08-22 14:01:33 -070046 algp = nla_data(rt);
Alexey Dobriyan06cd22f2017-09-21 23:46:30 +030047 if (nla_len(rt) < (int)xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070048 return -EINVAL;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 switch (type) {
51 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 break;
55
56 default:
57 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Herbert Xu633439f2017-04-06 16:16:10 +080060 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return 0;
62}
63
Martin Willi4447bb32009-11-25 00:29:52 +000064static int verify_auth_trunc(struct nlattr **attrs)
65{
66 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
67 struct xfrm_algo_auth *algp;
68
69 if (!rt)
70 return 0;
71
72 algp = nla_data(rt);
Alexey Dobriyan1bd963a2017-09-21 23:47:09 +030073 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
Martin Willi4447bb32009-11-25 00:29:52 +000074 return -EINVAL;
75
Herbert Xu633439f2017-04-06 16:16:10 +080076 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
Martin Willi4447bb32009-11-25 00:29:52 +000077 return 0;
78}
79
Herbert Xu1a6509d2008-01-28 19:37:29 -080080static int verify_aead(struct nlattr **attrs)
81{
82 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
83 struct xfrm_algo_aead *algp;
84
85 if (!rt)
86 return 0;
87
88 algp = nla_data(rt);
Alexey Dobriyan373b8ee2017-09-21 23:45:43 +030089 if (nla_len(rt) < (int)aead_len(algp))
Herbert Xu1a6509d2008-01-28 19:37:29 -080090 return -EINVAL;
91
Herbert Xu633439f2017-04-06 16:16:10 +080092 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
Herbert Xu1a6509d2008-01-28 19:37:29 -080093 return 0;
94}
95
Thomas Graf5424f322007-08-22 14:01:33 -070096static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070097 xfrm_address_t **addrp)
98{
Thomas Graf5424f322007-08-22 14:01:33 -070099 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700100
Thomas Grafcf5cb792007-08-22 13:59:04 -0700101 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700102 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700103}
Trent Jaegerdf718372005-12-13 23:12:27 -0800104
Thomas Graf5424f322007-08-22 14:01:33 -0700105static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800106{
Thomas Graf5424f322007-08-22 14:01:33 -0700107 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800108 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800109
110 if (!rt)
111 return 0;
112
Thomas Graf5424f322007-08-22 14:01:33 -0700113 uctx = nla_data(rt);
Xin Long171d4492020-02-09 21:15:29 +0800114 if (uctx->len > nla_len(rt) ||
115 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800116 return -EINVAL;
117
118 return 0;
119}
120
Steffen Klassertd8647b72011-03-08 00:10:27 +0000121static inline int verify_replay(struct xfrm_usersa_info *p,
122 struct nlattr **attrs)
123{
124 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000125 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000126
127 if (!rt)
Florian Westphald97ca5d2018-02-12 14:42:01 +0100128 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
129
130 rs = nla_data(rt);
131
132 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
133 return -EINVAL;
134
135 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
136 nla_len(rt) != sizeof(*rs))
137 return -EINVAL;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000138
Fan Du01714102014-01-18 09:54:28 +0800139 /* As only ESP and AH support ESN feature. */
140 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000141 return -EINVAL;
142
Steffen Klassertd8647b72011-03-08 00:10:27 +0000143 if (p->replay_window != 0)
144 return -EINVAL;
145
146 return 0;
147}
Trent Jaegerdf718372005-12-13 23:12:27 -0800148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700150 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int err;
153
154 err = -EINVAL;
155 switch (p->family) {
156 case AF_INET:
Anirudh Guptab38ff402019-05-21 20:59:47 +0530157 break;
158
159 case AF_INET6:
160#if IS_ENABLED(CONFIG_IPV6)
161 break;
162#else
163 err = -EAFNOSUPPORT;
164 goto out;
165#endif
166
167 default:
168 goto out;
169 }
170
171 switch (p->sel.family) {
Nicolas Dichtelb8d6d002019-06-14 11:13:55 +0200172 case AF_UNSPEC:
173 break;
174
Anirudh Guptab38ff402019-05-21 20:59:47 +0530175 case AF_INET:
Steffen Klassert07bf7902018-08-01 13:45:11 +0200176 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
177 goto out;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
180
181 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000182#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert07bf7902018-08-01 13:45:11 +0200183 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
184 goto out;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187#else
188 err = -EAFNOSUPPORT;
189 goto out;
190#endif
191
192 default:
193 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 err = -EINVAL;
197 switch (p->id.proto) {
198 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000199 if ((!attrs[XFRMA_ALG_AUTH] &&
200 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800201 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700202 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000203 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200204 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto out;
206 break;
207
208 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800209 if (attrs[XFRMA_ALG_COMP])
210 goto out;
211 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000212 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800213 !attrs[XFRMA_ALG_CRYPT] &&
214 !attrs[XFRMA_ALG_AEAD])
215 goto out;
216 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000217 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800218 attrs[XFRMA_ALG_CRYPT]) &&
219 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000221 if (attrs[XFRMA_TFCPAD] &&
222 p->mode != XFRM_MODE_TUNNEL)
223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225
226 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700227 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800228 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700229 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000230 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000231 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200232 attrs[XFRMA_TFCPAD] ||
233 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto out;
235 break;
236
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000237#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700238 case IPPROTO_DSTOPTS:
239 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if (attrs[XFRMA_ALG_COMP] ||
241 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000242 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800243 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 attrs[XFRMA_ALG_CRYPT] ||
245 attrs[XFRMA_ENCAP] ||
246 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000247 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700248 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700249 goto out;
250 break;
251#endif
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 default:
254 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Herbert Xu1a6509d2008-01-28 19:37:29 -0800257 if ((err = verify_aead(attrs)))
258 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000259 if ((err = verify_auth_trunc(attrs)))
260 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700261 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700263 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700265 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700267 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800268 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000269 if ((err = verify_replay(p, attrs)))
270 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 err = -EINVAL;
273 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700274 case XFRM_MODE_TRANSPORT:
275 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700276 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a69452c2006-10-03 23:47:05 -0700277 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
279
280 default:
281 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 err = 0;
285
Antony Antony4e484b32021-12-22 14:11:18 +0100286 if (attrs[XFRMA_MTIMER_THRESH])
287 if (!attrs[XFRMA_ENCAP])
288 err = -EINVAL;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290out:
291 return err;
292}
293
294static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800295 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700296 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct xfrm_algo *p, *ualg;
299 struct xfrm_algo_desc *algo;
300
301 if (!rta)
302 return 0;
303
Thomas Graf5424f322007-08-22 14:01:33 -0700304 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 algo = get_byname(ualg->alg_name, 1);
307 if (!algo)
308 return -ENOSYS;
309 *props = algo->desc.sadb_alg_id;
310
Eric Dumazet0f99be02008-01-08 23:39:06 -0800311 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!p)
313 return -ENOMEM;
314
Herbert Xu04ff1262006-08-13 08:50:00 +1000315 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 *algpp = p;
317 return 0;
318}
319
Herbert Xu69b01372015-05-27 16:03:45 +0800320static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
321{
322 struct xfrm_algo *p, *ualg;
323 struct xfrm_algo_desc *algo;
324
325 if (!rta)
326 return 0;
327
328 ualg = nla_data(rta);
329
330 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
331 if (!algo)
332 return -ENOSYS;
333 x->props.ealgo = algo->desc.sadb_alg_id;
334
335 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
336 if (!p)
337 return -ENOMEM;
338
339 strcpy(p->alg_name, algo->name);
340 x->ealg = p;
341 x->geniv = algo->uinfo.encr.geniv;
342 return 0;
343}
344
Martin Willi4447bb32009-11-25 00:29:52 +0000345static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
346 struct nlattr *rta)
347{
348 struct xfrm_algo *ualg;
349 struct xfrm_algo_auth *p;
350 struct xfrm_algo_desc *algo;
351
352 if (!rta)
353 return 0;
354
355 ualg = nla_data(rta);
356
357 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
358 if (!algo)
359 return -ENOSYS;
360 *props = algo->desc.sadb_alg_id;
361
362 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
363 if (!p)
364 return -ENOMEM;
365
366 strcpy(p->alg_name, algo->name);
367 p->alg_key_len = ualg->alg_key_len;
368 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
369 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
370
371 *algpp = p;
372 return 0;
373}
374
375static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
376 struct nlattr *rta)
377{
378 struct xfrm_algo_auth *p, *ualg;
379 struct xfrm_algo_desc *algo;
380
381 if (!rta)
382 return 0;
383
384 ualg = nla_data(rta);
385
386 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
387 if (!algo)
388 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800389 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000390 return -EINVAL;
391 *props = algo->desc.sadb_alg_id;
392
393 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
394 if (!p)
395 return -ENOMEM;
396
397 strcpy(p->alg_name, algo->name);
398 if (!p->alg_trunc_len)
399 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
400
401 *algpp = p;
402 return 0;
403}
404
Herbert Xu69b01372015-05-27 16:03:45 +0800405static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800406{
407 struct xfrm_algo_aead *p, *ualg;
408 struct xfrm_algo_desc *algo;
409
410 if (!rta)
411 return 0;
412
413 ualg = nla_data(rta);
414
415 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
416 if (!algo)
417 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800418 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800419
420 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
421 if (!p)
422 return -ENOMEM;
423
424 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800425 x->aead = p;
426 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800427 return 0;
428}
429
Steffen Klasserte2b19122011-03-28 19:47:30 +0000430static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
431 struct nlattr *rp)
432{
433 struct xfrm_replay_state_esn *up;
Alexey Dobriyan5e708e42017-09-21 23:47:50 +0300434 unsigned int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000435
436 if (!replay_esn || !rp)
437 return 0;
438
439 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000440 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000441
Andy Whitcroftf843ee62017-03-23 07:45:44 +0000442 /* Check the overall length and the internal bitmap length to avoid
443 * potential overflow. */
Alexey Dobriyan5e708e42017-09-21 23:47:50 +0300444 if (nla_len(rp) < (int)ulen ||
Andy Whitcroftf843ee62017-03-23 07:45:44 +0000445 xfrm_replay_state_esn_len(replay_esn) != ulen ||
446 replay_esn->bmp_len != up->bmp_len)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000447 return -EINVAL;
448
Andy Whitcroft677e8062017-03-22 07:29:31 +0000449 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
450 return -EINVAL;
451
Steffen Klasserte2b19122011-03-28 19:47:30 +0000452 return 0;
453}
454
Steffen Klassertd8647b72011-03-08 00:10:27 +0000455static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
456 struct xfrm_replay_state_esn **preplay_esn,
457 struct nlattr *rta)
458{
459 struct xfrm_replay_state_esn *p, *pp, *up;
Alexey Dobriyan5e708e42017-09-21 23:47:50 +0300460 unsigned int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000461
462 if (!rta)
463 return 0;
464
465 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000466 klen = xfrm_replay_state_esn_len(up);
Alexey Dobriyan5e708e42017-09-21 23:47:50 +0300467 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000468
Mathias Krauseecd79182012-09-20 10:01:49 +0000469 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000470 if (!p)
471 return -ENOMEM;
472
Mathias Krauseecd79182012-09-20 10:01:49 +0000473 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000474 if (!pp) {
475 kfree(p);
476 return -ENOMEM;
477 }
478
Mathias Krauseecd79182012-09-20 10:01:49 +0000479 memcpy(p, up, ulen);
480 memcpy(pp, up, ulen);
481
Steffen Klassertd8647b72011-03-08 00:10:27 +0000482 *replay_esn = p;
483 *preplay_esn = pp;
484
485 return 0;
486}
487
Alexey Dobriyana1b831f2017-09-21 23:48:54 +0300488static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800489{
Alexey Dobriyana1b831f2017-09-21 23:48:54 +0300490 unsigned int len = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -0800491
492 if (xfrm_ctx) {
493 len += sizeof(struct xfrm_user_sec_ctx);
494 len += xfrm_ctx->ctx_len;
495 }
496 return len;
497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
500{
501 memcpy(&x->id, &p->id, sizeof(x->id));
502 memcpy(&x->sel, &p->sel, sizeof(x->sel));
503 memcpy(&x->lft, &p->lft, sizeof(x->lft));
504 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800505 x->props.replay_window = min_t(unsigned int, p->replay_window,
506 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 x->props.reqid = p->reqid;
508 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700509 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700511
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700512 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700513 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800516/*
517 * someday when pfkey also has support, we could have the code
518 * somehow made shareable and move it to xfrm_state.c - JHS
519 *
520*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000521static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
522 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800523{
Thomas Graf5424f322007-08-22 14:01:33 -0700524 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000525 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700526 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
527 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
528 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Antony Antony4e484b32021-12-22 14:11:18 +0100529 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800530
Steffen Klassertd8647b72011-03-08 00:10:27 +0000531 if (re) {
532 struct xfrm_replay_state_esn *replay_esn;
533 replay_esn = nla_data(re);
534 memcpy(x->replay_esn, replay_esn,
535 xfrm_replay_state_esn_len(replay_esn));
536 memcpy(x->preplay_esn, replay_esn,
537 xfrm_replay_state_esn_len(replay_esn));
538 }
539
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800540 if (rp) {
541 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700542 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800543 memcpy(&x->replay, replay, sizeof(*replay));
544 memcpy(&x->preplay, replay, sizeof(*replay));
545 }
546
547 if (lt) {
548 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700549 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800550 x->curlft.bytes = ltime->bytes;
551 x->curlft.packets = ltime->packets;
552 x->curlft.add_time = ltime->add_time;
553 x->curlft.use_time = ltime->use_time;
554 }
555
Thomas Grafcf5cb792007-08-22 13:59:04 -0700556 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700557 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800558
Thomas Grafcf5cb792007-08-22 13:59:04 -0700559 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700560 x->replay_maxdiff = nla_get_u32(rt);
Antony Antony4e484b32021-12-22 14:11:18 +0100561
562 if (mt)
563 x->mapping_maxage = nla_get_u32(mt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800564}
565
Steffen Klassert9b42c1f2018-06-12 12:44:26 +0200566static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
567{
568 if (attrs[XFRMA_SET_MARK]) {
569 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
570 if (attrs[XFRMA_SET_MARK_MASK])
571 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
572 else
573 m->m = 0xffffffff;
574 } else {
575 m->v = m->m = 0;
576 }
577}
578
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800579static struct xfrm_state *xfrm_state_construct(struct net *net,
580 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700581 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int *errp)
583{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800584 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int err = -ENOMEM;
586
587 if (!x)
588 goto error_no_put;
589
590 copy_from_user_state(x, p);
591
Steffen Klassert6fd06962021-06-07 15:21:49 +0200592 if (attrs[XFRMA_ENCAP]) {
593 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
594 sizeof(*x->encap), GFP_KERNEL);
595 if (x->encap == NULL)
596 goto error;
597 }
598
599 if (attrs[XFRMA_COADDR]) {
600 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
601 sizeof(*x->coaddr), GFP_KERNEL);
602 if (x->coaddr == NULL)
603 goto error;
604 }
605
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100606 if (attrs[XFRMA_SA_EXTRA_FLAGS])
607 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
608
Herbert Xu69b01372015-05-27 16:03:45 +0800609 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800610 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000611 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
612 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000614 if (!x->props.aalgo) {
615 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
616 attrs[XFRMA_ALG_AUTH])))
617 goto error;
618 }
Herbert Xu69b01372015-05-27 16:03:45 +0800619 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 goto error;
621 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
622 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700623 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700625
Martin Willi35d28562010-12-08 04:37:49 +0000626 if (attrs[XFRMA_TFCPAD])
627 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
628
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000629 xfrm_mark_get(attrs, &x->mark);
630
Steffen Klassert9b42c1f2018-06-12 12:44:26 +0200631 xfrm_smark_init(attrs, &x->props.smark);
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900632
Antony Antony68ac0f32021-12-12 11:35:00 +0100633 if (attrs[XFRMA_IF_ID]) {
Steffen Klassert7e652642018-06-12 14:07:07 +0200634 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
Antony Antony68ac0f32021-12-12 11:35:00 +0100635 if (!x->if_id) {
636 err = -EINVAL;
637 goto error;
638 }
639 }
Thomas Graffd211502007-09-06 03:28:08 -0700640
Ilan Tayariffdb5212017-08-01 12:49:08 +0300641 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (err)
643 goto error;
644
Mathias Krause2f30ea52016-09-08 18:09:57 +0200645 if (attrs[XFRMA_SEC_CTX]) {
646 err = security_xfrm_state_alloc(x,
647 nla_data(attrs[XFRMA_SEC_CTX]));
648 if (err)
649 goto error;
650 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800651
Steffen Klassertd8647b72011-03-08 00:10:27 +0000652 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
653 attrs[XFRMA_REPLAY_ESN_VAL])))
654 goto error;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800657 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800658 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800659 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800660
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000661 if ((err = xfrm_init_replay(x)))
662 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800663
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000664 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000665 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Yossi Kupermancc015722018-01-17 15:52:41 +0200667 /* configure the hardware if offload is requested */
668 if (attrs[XFRMA_OFFLOAD_DEV]) {
669 err = xfrm_dev_state_add(net, x,
670 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
671 if (err)
672 goto error;
673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return x;
676
677error:
678 x->km.state = XFRM_STATE_DEAD;
679 xfrm_state_put(x);
680error_no_put:
681 *errp = err;
682 return NULL;
683}
684
Christoph Hellwig22e70052007-01-02 15:22:30 -0800685static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700686 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800688 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700689 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct xfrm_state *x;
691 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700692 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Thomas Graf35a7aa02007-08-22 14:00:40 -0700694 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (err)
696 return err;
697
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800698 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!x)
700 return err;
701
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700702 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
704 err = xfrm_state_add(x);
705 else
706 err = xfrm_state_update(x);
707
Tetsuo Handa2e710292014-04-22 21:48:30 +0900708 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (err < 0) {
711 x->km.state = XFRM_STATE_DEAD;
Steffen Klassertc5d4d7d2017-09-04 10:28:02 +0200712 xfrm_dev_state_delete(x);
Herbert Xu21380b82006-02-22 14:47:13 -0800713 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700714 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
Yossi Kupermancc015722018-01-17 15:52:41 +0200717 if (x->km.state == XFRM_STATE_VOID)
718 x->km.state = XFRM_STATE_VALID;
719
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700720 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000721 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700722 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700723
724 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700725out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700726 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return err;
728}
729
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800730static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
731 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700732 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700733 int *errp)
734{
735 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000736 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700737 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000738 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700739
740 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
741 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000742 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700743 } else {
744 xfrm_address_t *saddr = NULL;
745
Thomas Graf35a7aa02007-08-22 14:00:40 -0700746 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700747 if (!saddr) {
748 err = -EINVAL;
749 goto out;
750 }
751
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800752 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000753 x = xfrm_state_lookup_byaddr(net, mark,
754 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800755 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700756 }
757
758 out:
759 if (!x && errp)
760 *errp = err;
761 return x;
762}
763
Christoph Hellwig22e70052007-01-02 15:22:30 -0800764static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700765 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800767 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700769 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700770 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700771 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800773 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
David S. Miller6f68dc32006-06-08 23:58:52 -0700777 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700778 goto out;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700781 err = -EPERM;
782 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700785 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600786
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700787 if (err < 0)
788 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700789
790 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000791 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700792 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700793 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700795out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900796 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700797 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700798 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
802{
Mathias Krausef778a632012-09-19 11:33:39 +0000803 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 memcpy(&p->id, &x->id, sizeof(p->id));
805 memcpy(&p->sel, &x->sel, sizeof(p->sel));
806 memcpy(&p->lft, &x->lft, sizeof(p->lft));
807 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400808 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
809 put_unaligned(x->stats.replay, &p->stats.replay);
810 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700811 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 p->mode = x->props.mode;
813 p->replay_window = x->props.replay_window;
814 p->reqid = x->props.reqid;
815 p->family = x->props.family;
816 p->flags = x->props.flags;
817 p->seq = x->km.seq;
818}
819
820struct xfrm_dump_info {
821 struct sk_buff *in_skb;
822 struct sk_buff *out_skb;
823 u32 nlmsg_seq;
824 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825};
826
Thomas Grafc0144be2007-08-22 13:55:43 -0700827static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
828{
Thomas Grafc0144be2007-08-22 13:55:43 -0700829 struct xfrm_user_sec_ctx *uctx;
830 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700831 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700832
833 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
834 if (attr == NULL)
835 return -EMSGSIZE;
836
837 uctx = nla_data(attr);
838 uctx->exttype = XFRMA_SEC_CTX;
839 uctx->len = ctx_size;
840 uctx->ctx_doi = s->ctx_doi;
841 uctx->ctx_alg = s->ctx_alg;
842 uctx->ctx_len = s->ctx_len;
843 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
844
845 return 0;
846}
847
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200848static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
849{
850 struct xfrm_user_offload *xuo;
851 struct nlattr *attr;
852
853 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
854 if (attr == NULL)
855 return -EMSGSIZE;
856
857 xuo = nla_data(attr);
Mathias Krause5fe0d4b2017-08-26 17:08:57 +0200858 memset(xuo, 0, sizeof(*xuo));
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200859 xuo->ifindex = xso->dev->ifindex;
860 xuo->flags = xso->flags;
861
862 return 0;
863}
864
Antony Antonyc7a58992020-11-17 17:47:23 +0100865static bool xfrm_redact(void)
866{
867 return IS_ENABLED(CONFIG_SECURITY) &&
868 security_locked_down(LOCKDOWN_XFRM_SECRET);
869}
870
Martin Willi4447bb32009-11-25 00:29:52 +0000871static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
872{
873 struct xfrm_algo *algo;
Antony Antonyc7a58992020-11-17 17:47:23 +0100874 struct xfrm_algo_auth *ap;
Martin Willi4447bb32009-11-25 00:29:52 +0000875 struct nlattr *nla;
Antony Antonyc7a58992020-11-17 17:47:23 +0100876 bool redact_secret = xfrm_redact();
Martin Willi4447bb32009-11-25 00:29:52 +0000877
878 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
879 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
880 if (!nla)
881 return -EMSGSIZE;
Martin Willi4447bb32009-11-25 00:29:52 +0000882 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000883 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Antony Antonyc7a58992020-11-17 17:47:23 +0100884
885 if (redact_secret && auth->alg_key_len)
886 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
887 else
888 memcpy(algo->alg_key, auth->alg_key,
889 (auth->alg_key_len + 7) / 8);
Martin Willi4447bb32009-11-25 00:29:52 +0000890 algo->alg_key_len = auth->alg_key_len;
891
Antony Antonyc7a58992020-11-17 17:47:23 +0100892 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
893 if (!nla)
894 return -EMSGSIZE;
895 ap = nla_data(nla);
896 memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
897 if (redact_secret && auth->alg_key_len)
898 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
899 else
900 memcpy(ap->alg_key, auth->alg_key,
901 (auth->alg_key_len + 7) / 8);
902 return 0;
903}
904
905static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
906{
907 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
908 struct xfrm_algo_aead *ap;
909 bool redact_secret = xfrm_redact();
910
911 if (!nla)
912 return -EMSGSIZE;
913
914 ap = nla_data(nla);
915 memcpy(ap, aead, sizeof(*aead));
916
917 if (redact_secret && aead->alg_key_len)
918 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
919 else
920 memcpy(ap->alg_key, aead->alg_key,
921 (aead->alg_key_len + 7) / 8);
922 return 0;
923}
924
925static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
926{
927 struct xfrm_algo *ap;
928 bool redact_secret = xfrm_redact();
929 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
930 xfrm_alg_len(ealg));
931 if (!nla)
932 return -EMSGSIZE;
933
934 ap = nla_data(nla);
935 memcpy(ap, ealg, sizeof(*ealg));
936
937 if (redact_secret && ealg->alg_key_len)
938 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
939 else
940 memcpy(ap->alg_key, ealg->alg_key,
941 (ealg->alg_key_len + 7) / 8);
942
Martin Willi4447bb32009-11-25 00:29:52 +0000943 return 0;
944}
945
Steffen Klassert9b42c1f2018-06-12 12:44:26 +0200946static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
947{
948 int ret = 0;
949
950 if (m->v | m->m) {
951 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
952 if (!ret)
953 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
954 }
955 return ret;
956}
957
Herbert Xu68325d32007-10-09 13:30:57 -0700958/* Don't change this without updating xfrm_sa_len! */
959static int copy_to_user_state_extra(struct xfrm_state *x,
960 struct xfrm_usersa_info *p,
961 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700963 int ret = 0;
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 copy_to_user_state(x, p);
966
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100967 if (x->props.extra_flags) {
968 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
969 x->props.extra_flags);
970 if (ret)
971 goto out;
972 }
973
David S. Miller1d1e34d2012-06-27 21:57:03 -0700974 if (x->coaddr) {
975 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
976 if (ret)
977 goto out;
978 }
979 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200980 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
981 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700982 if (ret)
983 goto out;
984 }
985 if (x->aead) {
Antony Antonyc7a58992020-11-17 17:47:23 +0100986 ret = copy_to_user_aead(x->aead, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700987 if (ret)
988 goto out;
989 }
990 if (x->aalg) {
991 ret = copy_to_user_auth(x->aalg, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700992 if (ret)
993 goto out;
994 }
995 if (x->ealg) {
Antony Antonyc7a58992020-11-17 17:47:23 +0100996 ret = copy_to_user_ealg(x->ealg, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700997 if (ret)
998 goto out;
999 }
1000 if (x->calg) {
1001 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1002 if (ret)
1003 goto out;
1004 }
1005 if (x->encap) {
1006 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1007 if (ret)
1008 goto out;
1009 }
1010 if (x->tfcpad) {
1011 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1012 if (ret)
1013 goto out;
1014 }
1015 ret = xfrm_mark_put(skb, &x->mark);
1016 if (ret)
1017 goto out;
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02001018
1019 ret = xfrm_smark_put(skb, &x->props.smark);
1020 if (ret)
1021 goto out;
1022
dingzhif293a5e2014-10-30 09:39:36 +01001023 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -07001024 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1025 xfrm_replay_state_esn_len(x->replay_esn),
1026 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +01001027 else
1028 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1029 &x->replay);
1030 if (ret)
1031 goto out;
Steffen Klassertd77e38e2017-04-14 10:06:10 +02001032 if(x->xso.dev)
1033 ret = copy_user_offload(&x->xso, skb);
1034 if (ret)
1035 goto out;
Steffen Klassert7e652642018-06-12 14:07:07 +02001036 if (x->if_id) {
1037 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09001038 if (ret)
1039 goto out;
1040 }
Antony Antony4e484b32021-12-22 14:11:18 +01001041 if (x->security) {
Steffen Klassert85981122017-08-31 10:37:00 +02001042 ret = copy_sec_ctx(x->security, skb);
Antony Antony4e484b32021-12-22 14:11:18 +01001043 if (ret)
1044 goto out;
1045 }
1046 if (x->mapping_maxage)
1047 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001048out:
1049 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -07001050}
1051
1052static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1053{
1054 struct xfrm_dump_info *sp = ptr;
1055 struct sk_buff *in_skb = sp->in_skb;
1056 struct sk_buff *skb = sp->out_skb;
Dmitry Safonov5f3eea62020-09-21 15:36:53 +01001057 struct xfrm_translator *xtr;
Herbert Xu68325d32007-10-09 13:30:57 -07001058 struct xfrm_usersa_info *p;
1059 struct nlmsghdr *nlh;
1060 int err;
1061
Eric W. Biederman15e47302012-09-07 20:12:54 +00001062 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -07001063 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1064 if (nlh == NULL)
1065 return -EMSGSIZE;
1066
1067 p = nlmsg_data(nlh);
1068
1069 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001070 if (err) {
1071 nlmsg_cancel(skb, nlh);
1072 return err;
1073 }
Thomas Graf98250692007-08-22 12:47:26 -07001074 nlmsg_end(skb, nlh);
Dmitry Safonov5f3eea62020-09-21 15:36:53 +01001075
1076 xtr = xfrm_get_translator();
1077 if (xtr) {
1078 err = xtr->alloc_compat(skb, nlh);
1079
1080 xfrm_put_translator(xtr);
1081 if (err) {
1082 nlmsg_cancel(skb, nlh);
1083 return err;
1084 }
1085 }
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Timo Teras4c563f72008-02-28 21:31:08 -08001090static int xfrm_dump_sa_done(struct netlink_callback *cb)
1091{
1092 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001093 struct sock *sk = cb->skb->sk;
1094 struct net *net = sock_net(sk);
1095
Vegard Nossum1ba5bf92016-07-05 10:18:08 +02001096 if (cb->args[0])
1097 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001098 return 0;
1099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1102{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001103 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001104 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct xfrm_dump_info info;
1106
Timo Teras4c563f72008-02-28 21:31:08 -08001107 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1108 sizeof(cb->args) - sizeof(cb->args[0]));
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 info.in_skb = cb->skb;
1111 info.out_skb = skb;
1112 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1113 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001114
1115 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +01001116 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001117 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001118 u8 proto = 0;
1119 int err;
1120
Johannes Berg8cb08172019-04-26 14:07:28 +02001121 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1122 xfrma_policy, cb->extack);
Nicolas Dichteld3623092014-02-14 15:30:36 +01001123 if (err < 0)
1124 return err;
1125
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001126 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +02001127 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1128 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +01001129 if (filter == NULL)
1130 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001131 }
1132
1133 if (attrs[XFRMA_PROTO])
1134 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1135
1136 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +02001137 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -08001138 }
1139
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001140 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 return skb->len;
1143}
1144
1145static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1146 struct xfrm_state *x, u32 seq)
1147{
1148 struct xfrm_dump_info info;
1149 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +00001150 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Thomas Graf7deb2262007-08-22 13:57:39 -07001152 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!skb)
1154 return ERR_PTR(-ENOMEM);
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 info.in_skb = in_skb;
1157 info.out_skb = skb;
1158 info.nlmsg_seq = seq;
1159 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Mathias Krause864745d2012-09-13 11:41:26 +00001161 err = dump_one_state(x, 0, &info);
1162 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +00001164 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
1167 return skb;
1168}
1169
Michal Kubecek21ee5432014-06-03 10:26:06 +02001170/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1171 * Must be called with RCU read lock.
1172 */
1173static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1174 u32 pid, unsigned int group)
1175{
1176 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
Dmitry Safonov5461fc02020-09-21 15:36:52 +01001177 struct xfrm_translator *xtr;
Michal Kubecek21ee5432014-06-03 10:26:06 +02001178
Florian Westphal86126b72018-06-25 14:00:07 +02001179 if (!nlsk) {
1180 kfree_skb(skb);
1181 return -EPIPE;
1182 }
1183
Dmitry Safonov5461fc02020-09-21 15:36:52 +01001184 xtr = xfrm_get_translator();
1185 if (xtr) {
1186 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1187
1188 xfrm_put_translator(xtr);
1189 if (err) {
1190 kfree_skb(skb);
1191 return err;
1192 }
1193 }
1194
Florian Westphal86126b72018-06-25 14:00:07 +02001195 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
Michal Kubecek21ee5432014-06-03 10:26:06 +02001196}
1197
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03001198static inline unsigned int xfrm_spdinfo_msgsize(void)
Thomas Graf7deb2262007-08-22 13:57:39 -07001199{
1200 return NLMSG_ALIGN(4)
1201 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001202 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1203 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1204 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -07001205}
1206
Alexey Dobriyane0710412010-01-23 13:37:10 +00001207static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001208 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001209{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001210 struct xfrmk_spdinfo si;
1211 struct xfrmu_spdinfo spc;
1212 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001213 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001214 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001215 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001216 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001217 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001218
Eric W. Biederman15e47302012-09-07 20:12:54 +00001219 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001220 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001221 return -EMSGSIZE;
1222
1223 f = nlmsg_data(nlh);
1224 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001225 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001226 spc.incnt = si.incnt;
1227 spc.outcnt = si.outcnt;
1228 spc.fwdcnt = si.fwdcnt;
1229 spc.inscnt = si.inscnt;
1230 spc.outscnt = si.outscnt;
1231 spc.fwdscnt = si.fwdscnt;
1232 sph.spdhcnt = si.spdhcnt;
1233 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001234
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001235 do {
1236 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1237
1238 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1239 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1240 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1241 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1242 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1243
David S. Miller1d1e34d2012-06-27 21:57:03 -07001244 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1245 if (!err)
1246 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001247 if (!err)
1248 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1249 if (!err)
1250 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001251 if (err) {
1252 nlmsg_cancel(skb, nlh);
1253 return err;
1254 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001255
Johannes Berg053c0952015-01-16 22:09:00 +01001256 nlmsg_end(skb, nlh);
1257 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001258}
1259
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001260static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1261 struct nlattr **attrs)
1262{
1263 struct net *net = sock_net(skb->sk);
1264 struct xfrmu_spdhthresh *thresh4 = NULL;
1265 struct xfrmu_spdhthresh *thresh6 = NULL;
1266
1267 /* selector prefixlen thresholds to hash policies */
1268 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1269 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1270
1271 if (nla_len(rta) < sizeof(*thresh4))
1272 return -EINVAL;
1273 thresh4 = nla_data(rta);
1274 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1275 return -EINVAL;
1276 }
1277 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1278 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1279
1280 if (nla_len(rta) < sizeof(*thresh6))
1281 return -EINVAL;
1282 thresh6 = nla_data(rta);
1283 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1284 return -EINVAL;
1285 }
1286
1287 if (thresh4 || thresh6) {
1288 write_seqlock(&net->xfrm.policy_hthresh.lock);
1289 if (thresh4) {
1290 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1291 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1292 }
1293 if (thresh6) {
1294 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1295 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1296 }
1297 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1298
1299 xfrm_policy_hash_rebuild(net);
1300 }
1301
1302 return 0;
1303}
1304
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001305static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001306 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001307{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001308 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001309 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001310 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001311 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001312 u32 seq = nlh->nlmsg_seq;
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05001313 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001314
Thomas Graf7deb2262007-08-22 13:57:39 -07001315 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001316 if (r_skb == NULL)
1317 return -ENOMEM;
1318
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05001319 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1320 BUG_ON(err < 0);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001321
Eric W. Biederman15e47302012-09-07 20:12:54 +00001322 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001323}
1324
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03001325static inline unsigned int xfrm_sadinfo_msgsize(void)
Thomas Graf7deb2262007-08-22 13:57:39 -07001326{
1327 return NLMSG_ALIGN(4)
1328 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1329 + nla_total_size(4); /* XFRMA_SAD_CNT */
1330}
1331
Alexey Dobriyane0710412010-01-23 13:37:10 +00001332static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001333 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001334{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001335 struct xfrmk_sadinfo si;
1336 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001337 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001338 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001339 u32 *f;
1340
Eric W. Biederman15e47302012-09-07 20:12:54 +00001341 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001342 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001343 return -EMSGSIZE;
1344
1345 f = nlmsg_data(nlh);
1346 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001347 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001348
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001349 sh.sadhmcnt = si.sadhmcnt;
1350 sh.sadhcnt = si.sadhcnt;
1351
David S. Miller1d1e34d2012-06-27 21:57:03 -07001352 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1353 if (!err)
1354 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1355 if (err) {
1356 nlmsg_cancel(skb, nlh);
1357 return err;
1358 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001359
Johannes Berg053c0952015-01-16 22:09:00 +01001360 nlmsg_end(skb, nlh);
1361 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001362}
1363
1364static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001365 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001366{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001367 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001368 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001369 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001370 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001371 u32 seq = nlh->nlmsg_seq;
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05001372 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001373
Thomas Graf7deb2262007-08-22 13:57:39 -07001374 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001375 if (r_skb == NULL)
1376 return -ENOMEM;
1377
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05001378 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1379 BUG_ON(err < 0);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001380
Eric W. Biederman15e47302012-09-07 20:12:54 +00001381 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001382}
1383
Christoph Hellwig22e70052007-01-02 15:22:30 -08001384static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001385 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001387 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001388 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct xfrm_state *x;
1390 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001391 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001393 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (x == NULL)
1395 goto out_noput;
1396
1397 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1398 if (IS_ERR(resp_skb)) {
1399 err = PTR_ERR(resp_skb);
1400 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001401 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 xfrm_state_put(x);
1404out_noput:
1405 return err;
1406}
1407
Christoph Hellwig22e70052007-01-02 15:22:30 -08001408static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001409 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001411 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct xfrm_state *x;
1413 struct xfrm_userspi_info *p;
Dmitry Safonov5f3eea62020-09-21 15:36:53 +01001414 struct xfrm_translator *xtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct sk_buff *resp_skb;
1416 xfrm_address_t *daddr;
1417 int family;
1418 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001419 u32 mark;
1420 struct xfrm_mark m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001421 u32 if_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Thomas Graf7b67c852007-08-22 13:53:52 -07001423 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001424 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (err)
1426 goto out_noput;
1427
1428 family = p->info.family;
1429 daddr = &p->info.id.daddr;
1430
1431 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001432
1433 mark = xfrm_mark_get(attrs, &m);
Steffen Klassert7e652642018-06-12 14:07:07 +02001434
Antony Antony68ac0f32021-12-12 11:35:00 +01001435 if (attrs[XFRMA_IF_ID]) {
Steffen Klassert7e652642018-06-12 14:07:07 +02001436 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
Antony Antony68ac0f32021-12-12 11:35:00 +01001437 if (!if_id) {
1438 err = -EINVAL;
1439 goto out_noput;
1440 }
1441 }
Steffen Klassert7e652642018-06-12 14:07:07 +02001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001444 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001445 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 xfrm_state_put(x);
1447 x = NULL;
1448 }
1449 }
1450
1451 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001452 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001453 if_id, p->info.id.proto, daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 &p->info.saddr, 1,
1455 family);
1456 err = -ENOENT;
1457 if (x == NULL)
1458 goto out_noput;
1459
Herbert Xu658b2192007-10-09 13:29:52 -07001460 err = xfrm_alloc_spi(x, p->min, p->max);
1461 if (err)
1462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Herbert Xu658b2192007-10-09 13:29:52 -07001464 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (IS_ERR(resp_skb)) {
1466 err = PTR_ERR(resp_skb);
1467 goto out;
1468 }
1469
Dmitry Safonov5f3eea62020-09-21 15:36:53 +01001470 xtr = xfrm_get_translator();
1471 if (xtr) {
1472 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1473
1474 xfrm_put_translator(xtr);
1475 if (err) {
1476 kfree_skb(resp_skb);
1477 goto out;
1478 }
1479 }
1480
Eric W. Biederman15e47302012-09-07 20:12:54 +00001481 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483out:
1484 xfrm_state_put(x);
1485out_noput:
1486 return err;
1487}
1488
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001489static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 switch (dir) {
1492 case XFRM_POLICY_IN:
1493 case XFRM_POLICY_OUT:
1494 case XFRM_POLICY_FWD:
1495 break;
1496
1497 default:
1498 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 return 0;
1502}
1503
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001504static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001505{
1506 switch (type) {
1507 case XFRM_POLICY_TYPE_MAIN:
1508#ifdef CONFIG_XFRM_SUB_POLICY
1509 case XFRM_POLICY_TYPE_SUB:
1510#endif
1511 break;
1512
1513 default:
1514 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001515 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001516
1517 return 0;
1518}
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1521{
Fan Due682adf02013-11-07 17:47:48 +08001522 int ret;
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 switch (p->share) {
1525 case XFRM_SHARE_ANY:
1526 case XFRM_SHARE_SESSION:
1527 case XFRM_SHARE_USER:
1528 case XFRM_SHARE_UNIQUE:
1529 break;
1530
1531 default:
1532 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 switch (p->action) {
1536 case XFRM_POLICY_ALLOW:
1537 case XFRM_POLICY_BLOCK:
1538 break;
1539
1540 default:
1541 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 switch (p->sel.family) {
1545 case AF_INET:
Steffen Klassert07bf7902018-08-01 13:45:11 +02001546 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1547 return -EINVAL;
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 break;
1550
1551 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001552#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert07bf7902018-08-01 13:45:11 +02001553 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1554 return -EINVAL;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 break;
1557#else
1558 return -EAFNOSUPPORT;
1559#endif
1560
1561 default:
1562 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Fan Due682adf02013-11-07 17:47:48 +08001565 ret = verify_policy_dir(p->dir);
1566 if (ret)
1567 return ret;
YueHaibingb805d78d2019-02-28 15:18:59 +08001568 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
Fan Due682adf02013-11-07 17:47:48 +08001569 return -EINVAL;
1570
1571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Thomas Graf5424f322007-08-22 14:01:33 -07001574static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001575{
Thomas Graf5424f322007-08-22 14:01:33 -07001576 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001577 struct xfrm_user_sec_ctx *uctx;
1578
1579 if (!rt)
1580 return 0;
1581
Thomas Graf5424f322007-08-22 14:01:33 -07001582 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001583 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1587 int nr)
1588{
1589 int i;
1590
1591 xp->xfrm_nr = nr;
1592 for (i = 0; i < nr; i++, ut++) {
1593 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1594
1595 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1596 memcpy(&t->saddr, &ut->saddr,
1597 sizeof(xfrm_address_t));
1598 t->reqid = ut->reqid;
1599 t->mode = ut->mode;
1600 t->share = ut->share;
1601 t->optional = ut->optional;
1602 t->aalgos = ut->aalgos;
1603 t->ealgos = ut->ealgos;
1604 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001605 /* If all masks are ~0, then we allow all algorithms. */
1606 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001607 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609}
1610
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001611static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1612{
Steffen Klassert732706a2017-12-08 08:07:25 +01001613 u16 prev_family;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001614 int i;
1615
1616 if (nr > XFRM_MAX_DEPTH)
1617 return -EINVAL;
1618
Steffen Klassert732706a2017-12-08 08:07:25 +01001619 prev_family = family;
1620
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001621 for (i = 0; i < nr; i++) {
1622 /* We never validated the ut->family value, so many
1623 * applications simply leave it at zero. The check was
1624 * never made and ut->family was ignored because all
1625 * templates could be assumed to have the same family as
1626 * the policy itself. Now that we will have ipv4-in-ipv6
1627 * and ipv6-in-ipv4 tunnels, this is no longer true.
1628 */
1629 if (!ut[i].family)
1630 ut[i].family = family;
1631
Florian Westphal35e6103862019-01-09 14:37:34 +01001632 switch (ut[i].mode) {
1633 case XFRM_MODE_TUNNEL:
1634 case XFRM_MODE_BEET:
1635 break;
1636 default:
1637 if (ut[i].family != prev_family)
1638 return -EINVAL;
1639 break;
1640 }
Sean Tranchetti32bf94f2018-09-19 13:54:56 -06001641 if (ut[i].mode >= XFRM_MODE_MAX)
1642 return -EINVAL;
1643
Steffen Klassert732706a2017-12-08 08:07:25 +01001644 prev_family = ut[i].family;
1645
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001646 switch (ut[i].family) {
1647 case AF_INET:
1648 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001649#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001650 case AF_INET6:
1651 break;
1652#endif
1653 default:
1654 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001655 }
Cong Wang6a53b752017-11-27 11:15:16 -08001656
Cong Wangdbb24832019-03-22 16:26:19 -07001657 if (!xfrm_id_proto_valid(ut[i].id.proto))
Cong Wang6a53b752017-11-27 11:15:16 -08001658 return -EINVAL;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001659 }
1660
1661 return 0;
1662}
1663
Thomas Graf5424f322007-08-22 14:01:33 -07001664static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Thomas Graf5424f322007-08-22 14:01:33 -07001666 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (!rt) {
1669 pol->xfrm_nr = 0;
1670 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001671 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1672 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001673 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001675 err = validate_tmpl(nr, utmpl, pol->family);
1676 if (err)
1677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Thomas Graf5424f322007-08-22 14:01:33 -07001679 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681 return 0;
1682}
1683
Thomas Graf5424f322007-08-22 14:01:33 -07001684static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001685{
Thomas Graf5424f322007-08-22 14:01:33 -07001686 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001687 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001688 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001689 int err;
1690
1691 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001692 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001693 type = upt->type;
1694 }
1695
1696 err = verify_policy_type(type);
1697 if (err)
1698 return err;
1699
1700 *tp = type;
1701 return 0;
1702}
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1705{
1706 xp->priority = p->priority;
1707 xp->index = p->index;
1708 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1709 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1710 xp->action = p->action;
1711 xp->flags = p->flags;
1712 xp->family = p->sel.family;
1713 /* XXX xp->share = p->share; */
1714}
1715
1716static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1717{
Mathias Krause7b789832012-09-19 11:33:40 +00001718 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1720 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1721 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1722 p->priority = xp->priority;
1723 p->index = xp->index;
1724 p->sel.family = xp->family;
1725 p->dir = dir;
1726 p->action = xp->action;
1727 p->flags = xp->flags;
1728 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1729}
1730
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001731static 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 -07001732{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001733 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int err;
1735
1736 if (!xp) {
1737 *errp = -ENOMEM;
1738 return NULL;
1739 }
1740
1741 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001742
Thomas Graf35a7aa02007-08-22 14:00:40 -07001743 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001744 if (err)
1745 goto error;
1746
Thomas Graf35a7aa02007-08-22 14:00:40 -07001747 if (!(err = copy_from_user_tmpl(xp, attrs)))
1748 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001749 if (err)
1750 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001752 xfrm_mark_get(attrs, &xp->mark);
1753
Antony Antony68ac0f32021-12-12 11:35:00 +01001754 if (attrs[XFRMA_IF_ID]) {
Steffen Klassert7e652642018-06-12 14:07:07 +02001755 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
Antony Antony68ac0f32021-12-12 11:35:00 +01001756 if (!xp->if_id) {
1757 err = -EINVAL;
1758 goto error;
1759 }
1760 }
Steffen Klassert7e652642018-06-12 14:07:07 +02001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001763 error:
1764 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001765 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001766 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001767 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Christoph Hellwig22e70052007-01-02 15:22:30 -08001770static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001771 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001773 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001774 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001776 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int err;
1778 int excl;
1779
1780 err = verify_newpolicy_info(p);
1781 if (err)
1782 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001783 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001784 if (err)
1785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001787 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (!xp)
1789 return err;
1790
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001791 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001792 * Aha! this is anti-netlink really i.e more pfkey derived
Bhaskar Chowdhurya7fd0e62021-03-27 04:42:54 +05301793 * in netlink excl is a flag and you wouldn't need
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001794 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1796 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001797 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001800 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 kfree(xp);
1802 return err;
1803 }
1804
Herbert Xuf60f6b82005-06-18 22:44:37 -07001805 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001806 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001807 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001808 km_policy_notify(xp, p->dir, &c);
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 xfrm_pol_put(xp);
1811
1812 return 0;
1813}
1814
1815static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1816{
1817 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1818 int i;
1819
1820 if (xp->xfrm_nr == 0)
1821 return 0;
1822
1823 for (i = 0; i < xp->xfrm_nr; i++) {
1824 struct xfrm_user_tmpl *up = &vec[i];
1825 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1826
Mathias Krause1f868402012-09-19 11:33:41 +00001827 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001829 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1831 up->reqid = kp->reqid;
1832 up->mode = kp->mode;
1833 up->share = kp->share;
1834 up->optional = kp->optional;
1835 up->aalgos = kp->aalgos;
1836 up->ealgos = kp->ealgos;
1837 up->calgos = kp->calgos;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Thomas Grafc0144be2007-08-22 13:55:43 -07001840 return nla_put(skb, XFRMA_TMPL,
1841 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001842}
1843
Serge Hallyn0d681622006-07-24 23:30:44 -07001844static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1845{
1846 if (x->security) {
1847 return copy_sec_ctx(x->security, skb);
1848 }
1849 return 0;
1850}
1851
1852static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1853{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001854 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001855 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001856 return 0;
1857}
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03001858static inline unsigned int userpolicy_type_attrsize(void)
Thomas Grafcfbfd452007-08-22 13:57:04 -07001859{
1860#ifdef CONFIG_XFRM_SUB_POLICY
1861 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1862#else
1863 return 0;
1864#endif
1865}
Serge Hallyn0d681622006-07-24 23:30:44 -07001866
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001867#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001868static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001869{
Eric Dumazet45c180b2018-06-18 21:35:07 -07001870 struct xfrm_userpolicy_type upt;
1871
1872 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1873 memset(&upt, 0, sizeof(upt));
1874 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001875
Thomas Grafc0144be2007-08-22 13:55:43 -07001876 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001877}
1878
1879#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001880static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001881{
1882 return 0;
1883}
1884#endif
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1887{
1888 struct xfrm_dump_info *sp = ptr;
1889 struct xfrm_userpolicy_info *p;
1890 struct sk_buff *in_skb = sp->in_skb;
1891 struct sk_buff *skb = sp->out_skb;
Dmitry Safonov5f3eea62020-09-21 15:36:53 +01001892 struct xfrm_translator *xtr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001894 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Eric W. Biederman15e47302012-09-07 20:12:54 +00001896 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001897 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1898 if (nlh == NULL)
1899 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Thomas Graf7b67c852007-08-22 13:53:52 -07001901 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001903 err = copy_to_user_tmpl(xp, skb);
1904 if (!err)
1905 err = copy_to_user_sec_ctx(xp, skb);
1906 if (!err)
1907 err = copy_to_user_policy_type(xp->type, skb);
1908 if (!err)
1909 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klassert7e652642018-06-12 14:07:07 +02001910 if (!err)
1911 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001912 if (err) {
1913 nlmsg_cancel(skb, nlh);
1914 return err;
1915 }
Thomas Graf98250692007-08-22 12:47:26 -07001916 nlmsg_end(skb, nlh);
Dmitry Safonov5f3eea62020-09-21 15:36:53 +01001917
1918 xtr = xfrm_get_translator();
1919 if (xtr) {
1920 err = xtr->alloc_compat(skb, nlh);
1921
1922 xfrm_put_translator(xtr);
1923 if (err) {
1924 nlmsg_cancel(skb, nlh);
1925 return err;
1926 }
1927 }
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Timo Teras4c563f72008-02-28 21:31:08 -08001932static int xfrm_dump_policy_done(struct netlink_callback *cb)
1933{
Herbert Xu1137b5e2017-10-19 20:51:10 +08001934 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Fan Du283bc9f2013-11-07 17:47:50 +08001935 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001936
Fan Du283bc9f2013-11-07 17:47:50 +08001937 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001938 return 0;
1939}
1940
Herbert Xu1137b5e2017-10-19 20:51:10 +08001941static int xfrm_dump_policy_start(struct netlink_callback *cb)
1942{
1943 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1944
1945 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1946
1947 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1948 return 0;
1949}
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1952{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001953 struct net *net = sock_net(skb->sk);
Herbert Xu1137b5e2017-10-19 20:51:10 +08001954 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct xfrm_dump_info info;
1956
1957 info.in_skb = cb->skb;
1958 info.out_skb = skb;
1959 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1960 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001961
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001962 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 return skb->len;
1965}
1966
1967static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1968 struct xfrm_policy *xp,
1969 int dir, u32 seq)
1970{
1971 struct xfrm_dump_info info;
1972 struct sk_buff *skb;
Mathias Krausec25463722012-09-14 09:58:32 +00001973 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Thomas Graf7deb2262007-08-22 13:57:39 -07001975 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (!skb)
1977 return ERR_PTR(-ENOMEM);
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 info.in_skb = in_skb;
1980 info.out_skb = skb;
1981 info.nlmsg_seq = seq;
1982 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Mathias Krausec25463722012-09-14 09:58:32 +00001984 err = dump_one_policy(xp, dir, 0, &info);
1985 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 kfree_skb(skb);
Mathias Krausec25463722012-09-14 09:58:32 +00001987 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
1990 return skb;
1991}
1992
Nicolas Dichtel88d0adb2021-09-14 16:46:34 +02001993static int xfrm_notify_userpolicy(struct net *net)
1994{
1995 struct xfrm_userpolicy_default *up;
1996 int len = NLMSG_ALIGN(sizeof(*up));
1997 struct nlmsghdr *nlh;
1998 struct sk_buff *skb;
Nicolas Dichtel93ec1322021-09-22 10:50:06 +02001999 int err;
Nicolas Dichtel88d0adb2021-09-14 16:46:34 +02002000
2001 skb = nlmsg_new(len, GFP_ATOMIC);
2002 if (skb == NULL)
2003 return -ENOMEM;
2004
2005 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
2006 if (nlh == NULL) {
2007 kfree_skb(skb);
2008 return -EMSGSIZE;
2009 }
2010
2011 up = nlmsg_data(nlh);
2012 up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
2013 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2014 up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2015 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2016 up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2017 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2018
2019 nlmsg_end(skb, nlh);
2020
Nicolas Dichtel93ec1322021-09-22 10:50:06 +02002021 rcu_read_lock();
2022 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2023 rcu_read_unlock();
2024
2025 return err;
Nicolas Dichtel88d0adb2021-09-14 16:46:34 +02002026}
2027
Steffen Klassert2d151d32021-07-18 09:11:06 +02002028static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2029 struct nlattr **attrs)
2030{
2031 struct net *net = sock_net(skb->sk);
2032 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
Steffen Klassert2d151d32021-07-18 09:11:06 +02002033
Nicolas Dichtelf8d858e2021-09-14 16:46:33 +02002034 if (up->in == XFRM_USERPOLICY_BLOCK)
2035 net->xfrm.policy_default |= XFRM_POL_DEFAULT_IN;
2036 else if (up->in == XFRM_USERPOLICY_ACCEPT)
2037 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_IN;
Pavel Skripkin5d8dbb72021-07-28 19:38:18 +03002038
Nicolas Dichtelf8d858e2021-09-14 16:46:33 +02002039 if (up->fwd == XFRM_USERPOLICY_BLOCK)
2040 net->xfrm.policy_default |= XFRM_POL_DEFAULT_FWD;
2041 else if (up->fwd == XFRM_USERPOLICY_ACCEPT)
2042 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_FWD;
Pavel Skripkin5d8dbb72021-07-28 19:38:18 +03002043
Nicolas Dichtelf8d858e2021-09-14 16:46:33 +02002044 if (up->out == XFRM_USERPOLICY_BLOCK)
2045 net->xfrm.policy_default |= XFRM_POL_DEFAULT_OUT;
2046 else if (up->out == XFRM_USERPOLICY_ACCEPT)
2047 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_OUT;
Steffen Klassert2d151d32021-07-18 09:11:06 +02002048
2049 rt_genid_bump_all(net);
2050
Nicolas Dichtel88d0adb2021-09-14 16:46:34 +02002051 xfrm_notify_userpolicy(net);
Steffen Klassert2d151d32021-07-18 09:11:06 +02002052 return 0;
2053}
2054
2055static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2056 struct nlattr **attrs)
2057{
2058 struct sk_buff *r_skb;
2059 struct nlmsghdr *r_nlh;
2060 struct net *net = sock_net(skb->sk);
Nicolas Dichtelf8d858e2021-09-14 16:46:33 +02002061 struct xfrm_userpolicy_default *r_up;
Steffen Klassert2d151d32021-07-18 09:11:06 +02002062 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2063 u32 portid = NETLINK_CB(skb).portid;
2064 u32 seq = nlh->nlmsg_seq;
2065
Steffen Klassert2d151d32021-07-18 09:11:06 +02002066 r_skb = nlmsg_new(len, GFP_ATOMIC);
2067 if (!r_skb)
2068 return -ENOMEM;
2069
2070 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2071 if (!r_nlh) {
2072 kfree_skb(r_skb);
2073 return -EMSGSIZE;
2074 }
2075
2076 r_up = nlmsg_data(r_nlh);
2077
Nicolas Dichtelf8d858e2021-09-14 16:46:33 +02002078 r_up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
2079 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2080 r_up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2081 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2082 r_up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2083 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
Steffen Klassert2d151d32021-07-18 09:11:06 +02002084 nlmsg_end(r_skb, r_nlh);
2085
2086 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2087}
2088
Christoph Hellwig22e70052007-01-02 15:22:30 -08002089static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002090 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002092 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 struct xfrm_policy *xp;
2094 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002095 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002097 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002099 struct xfrm_mark m;
Steffen Klassert7e652642018-06-12 14:07:07 +02002100 u32 if_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Thomas Graf7b67c852007-08-22 13:53:52 -07002102 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2104
Thomas Graf35a7aa02007-08-22 14:00:40 -07002105 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002106 if (err)
2107 return err;
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 err = verify_policy_dir(p->dir);
2110 if (err)
2111 return err;
2112
Steffen Klassert7e652642018-06-12 14:07:07 +02002113 if (attrs[XFRMA_IF_ID])
2114 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2115
Xin Long4f47e8ab2020-06-22 16:40:29 +08002116 xfrm_mark_get(attrs, &m);
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (p->index)
Xin Long4f47e8ab2020-06-22 16:40:29 +08002119 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2120 p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08002121 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002122 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002123 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08002124
Thomas Graf35a7aa02007-08-22 14:00:40 -07002125 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08002126 if (err)
2127 return err;
2128
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002129 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08002130 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002131 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08002132
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002133 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002134 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08002135 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002136 }
Xin Long4f47e8ab2020-06-22 16:40:29 +08002137 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2138 &p->sel, ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002139 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (xp == NULL)
2142 return -ENOENT;
2143
2144 if (!delete) {
2145 struct sk_buff *resp_skb;
2146
2147 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2148 if (IS_ERR(resp_skb)) {
2149 err = PTR_ERR(resp_skb);
2150 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002151 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002152 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002154 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09002155 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002156
2157 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002158 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002159
Herbert Xue7443892005-06-18 22:44:18 -07002160 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002161 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002162 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002163 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002164 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002167out:
Eric Parisef41aaa2007-03-07 15:37:58 -08002168 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return err;
2170}
2171
Christoph Hellwig22e70052007-01-02 15:22:30 -08002172static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002173 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002175 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002176 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07002177 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04002178 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Cong Wangf75a2802019-01-31 13:05:49 -08002180 err = xfrm_state_flush(net, p->proto, true, false);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00002181 if (err) {
2182 if (err == -ESRCH) /* empty table */
2183 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002184 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00002185 }
Herbert Xubf08867f92005-06-18 22:44:00 -07002186 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002187 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002188 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002189 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002190 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002191 km_state_notify(NULL, &c);
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return 0;
2194}
2195
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03002196static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07002197{
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03002198 unsigned int replay_size = x->replay_esn ?
Steffen Klassertd8647b72011-03-08 00:10:27 +00002199 xfrm_replay_state_esn_len(x->replay_esn) :
2200 sizeof(struct xfrm_replay_state);
2201
Thomas Graf7deb2262007-08-22 13:57:39 -07002202 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00002203 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002204 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002205 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002206 + nla_total_size(4) /* XFRM_AE_RTHR */
2207 + nla_total_size(4); /* XFRM_AE_ETHR */
2208}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002209
David S. Miller214e0052011-02-24 00:02:38 -05002210static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002211{
2212 struct xfrm_aevent_id *id;
2213 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002214 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002215
Eric W. Biederman15e47302012-09-07 20:12:54 +00002216 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002217 if (nlh == NULL)
2218 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002219
Thomas Graf7b67c852007-08-22 13:53:52 -07002220 id = nlmsg_data(nlh);
Mathias Krause931e79d2017-08-26 17:09:00 +02002221 memset(&id->sa_id, 0, sizeof(id->sa_id));
Weilong Chen9b7a7872013-12-24 09:43:46 +08002222 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002223 id->sa_id.spi = x->id.spi;
2224 id->sa_id.family = x->props.family;
2225 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08002226 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08002227 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002228 id->flags = c->data.aevent;
2229
David S. Millerd0fde792012-03-29 04:02:26 -04002230 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002231 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2232 xfrm_replay_state_esn_len(x->replay_esn),
2233 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04002234 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002235 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2236 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04002237 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002238 if (err)
2239 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002240 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2241 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002242 if (err)
2243 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00002244
David S. Miller1d1e34d2012-06-27 21:57:03 -07002245 if (id->flags & XFRM_AE_RTHR) {
2246 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2247 if (err)
2248 goto out_cancel;
2249 }
2250 if (id->flags & XFRM_AE_ETHR) {
2251 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2252 x->replay_maxage * 10 / HZ);
2253 if (err)
2254 goto out_cancel;
2255 }
2256 err = xfrm_mark_put(skb, &x->mark);
2257 if (err)
2258 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002259
Steffen Klassert7e652642018-06-12 14:07:07 +02002260 err = xfrm_if_id_put(skb, x->if_id);
2261 if (err)
2262 goto out_cancel;
2263
Johannes Berg053c0952015-01-16 22:09:00 +01002264 nlmsg_end(skb, nlh);
2265 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002266
David S. Miller1d1e34d2012-06-27 21:57:03 -07002267out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002268 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002269 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002270}
2271
Christoph Hellwig22e70052007-01-02 15:22:30 -08002272static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002273 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002274{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002275 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002276 struct xfrm_state *x;
2277 struct sk_buff *r_skb;
2278 int err;
2279 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002280 u32 mark;
2281 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07002282 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002283 struct xfrm_usersa_id *id = &p->sa_id;
2284
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002285 mark = xfrm_mark_get(attrs, &m);
2286
2287 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00002288 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002289 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00002290
2291 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2292 if (r_skb == NULL) {
2293 xfrm_state_put(x);
2294 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002295 }
2296
2297 /*
2298 * XXX: is this lock really needed - none of the other
2299 * gets lock (the concern is things getting updated
2300 * while we are still reading) - jhs
2301 */
2302 spin_lock_bh(&x->lock);
2303 c.data.aevent = p->flags;
2304 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002305 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002306
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05002307 err = build_aevent(r_skb, x, &c);
2308 BUG_ON(err < 0);
2309
Eric W. Biederman15e47302012-09-07 20:12:54 +00002310 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002311 spin_unlock_bh(&x->lock);
2312 xfrm_state_put(x);
2313 return err;
2314}
2315
Christoph Hellwig22e70052007-01-02 15:22:30 -08002316static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002317 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002318{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002319 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002320 struct xfrm_state *x;
2321 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08002322 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002323 u32 mark = 0;
2324 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07002325 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07002326 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00002327 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07002328 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02002329 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2330 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002331
Michael Rossberg4e077232015-09-29 11:25:08 +02002332 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002333 return err;
2334
2335 /* pedantic mode - thou shalt sayeth replaceth */
2336 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2337 return err;
2338
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002339 mark = xfrm_mark_get(attrs, &m);
2340
2341 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 -08002342 if (x == NULL)
2343 return -ESRCH;
2344
2345 if (x->km.state != XFRM_STATE_VALID)
2346 goto out;
2347
Steffen Klassert4479ff72013-09-09 09:39:01 +02002348 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00002349 if (err)
2350 goto out;
2351
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002352 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00002353 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002354 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002355
2356 c.event = nlh->nlmsg_type;
2357 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002358 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002359 c.data.aevent = XFRM_AE_CU;
2360 km_state_notify(x, &c);
2361 err = 0;
2362out:
2363 xfrm_state_put(x);
2364 return err;
2365}
2366
Christoph Hellwig22e70052007-01-02 15:22:30 -08002367static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002368 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002370 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002371 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002372 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002373 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002374
Thomas Graf35a7aa02007-08-22 14:00:40 -07002375 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002376 if (err)
2377 return err;
2378
Tetsuo Handa2e710292014-04-22 21:48:30 +09002379 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002380 if (err) {
2381 if (err == -ESRCH) /* empty table */
2382 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002383 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002384 }
2385
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002386 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002387 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002388 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002389 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002390 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return 0;
2393}
2394
Christoph Hellwig22e70052007-01-02 15:22:30 -08002395static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002396 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002397{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002398 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002399 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002400 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002401 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002402 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002403 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002404 struct xfrm_mark m;
Steffen Klassert7e652642018-06-12 14:07:07 +02002405 u32 if_id = 0;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002406
Thomas Graf35a7aa02007-08-22 14:00:40 -07002407 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002408 if (err)
2409 return err;
2410
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002411 err = verify_policy_dir(p->dir);
2412 if (err)
2413 return err;
2414
Steffen Klassert7e652642018-06-12 14:07:07 +02002415 if (attrs[XFRMA_IF_ID])
2416 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2417
Xin Long4f47e8ab2020-06-22 16:40:29 +08002418 xfrm_mark_get(attrs, &m);
2419
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002420 if (p->index)
Xin Long4f47e8ab2020-06-22 16:40:29 +08002421 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2422 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002423 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002424 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002425 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002426
Thomas Graf35a7aa02007-08-22 14:00:40 -07002427 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002428 if (err)
2429 return err;
2430
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002431 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002432 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002433 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002434
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002435 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002436 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002437 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002438 }
Xin Long4f47e8ab2020-06-22 16:40:29 +08002439 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002440 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002441 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002442 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002443 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002444 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002445
Timo Teräsea2dea9d2010-03-31 00:17:05 +00002446 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002447 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002448
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002449 err = 0;
2450 if (up->hard) {
2451 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002452 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002453 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002454 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002455
2456out:
2457 xfrm_pol_put(xp);
2458 return err;
2459}
2460
Christoph Hellwig22e70052007-01-02 15:22:30 -08002461static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002462 struct nlattr **attrs)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002463{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002464 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002465 struct xfrm_state *x;
2466 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002467 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002468 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002469 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002470 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002471
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002472 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002473
David S. Miller3a765aa2007-02-26 14:52:21 -08002474 err = -ENOENT;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002475 if (x == NULL)
2476 return err;
2477
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002478 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002479 err = -EINVAL;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002480 if (x->km.state != XFRM_STATE_VALID)
2481 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002482 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002483
Joy Latten161a09e2006-11-27 13:11:54 -06002484 if (ue->hard) {
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002485 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002486 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002487 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002488 err = 0;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002489out:
2490 spin_unlock_bh(&x->lock);
2491 xfrm_state_put(x);
2492 return err;
2493}
2494
Christoph Hellwig22e70052007-01-02 15:22:30 -08002495static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002496 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002497{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002498 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002499 struct xfrm_policy *xp;
2500 struct xfrm_user_tmpl *ut;
2501 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002502 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002503 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002504
Thomas Graf7b67c852007-08-22 13:53:52 -07002505 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002506 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002507 int err = -ENOMEM;
2508
2509 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002510 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002511
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002512 xfrm_mark_get(attrs, &mark);
2513
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002514 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002515 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002516 goto free_state;
Xin Longa1a7e3a2020-02-09 21:16:38 +08002517 err = verify_sec_ctx_len(attrs);
2518 if (err)
2519 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002520
2521 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002522 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002523 if (!xp)
2524 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002525
2526 memcpy(&x->id, &ua->id, sizeof(ua->id));
2527 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2528 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002529 xp->mark.m = x->mark.m = mark.m;
2530 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002531 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002532 /* extract the templates and for each call km_key */
2533 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2534 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2535 memcpy(&x->id, &t->id, sizeof(x->id));
2536 x->props.mode = t->mode;
2537 x->props.reqid = t->reqid;
2538 x->props.family = ut->family;
2539 t->aalgos = ua->aalgos;
2540 t->ealgos = ua->ealgos;
2541 t->calgos = ua->calgos;
2542 err = km_query(x, t, xp);
2543
2544 }
2545
Mathias Krause4a135e52018-11-21 21:09:23 +01002546 xfrm_state_free(x);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002547 kfree(xp);
2548
2549 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002550
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002551free_state:
Mathias Krause4a135e52018-11-21 21:09:23 +01002552 xfrm_state_free(x);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002553nomem:
2554 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002555}
2556
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002557#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002558static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002559 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002560 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002561{
Thomas Graf5424f322007-08-22 14:01:33 -07002562 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002563 struct xfrm_user_migrate *um;
2564 int i, num_migrate;
2565
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002566 if (k != NULL) {
2567 struct xfrm_user_kmaddress *uk;
2568
2569 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2570 memcpy(&k->local, &uk->local, sizeof(k->local));
2571 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2572 k->family = uk->family;
2573 k->reserved = uk->reserved;
2574 }
2575
Thomas Graf5424f322007-08-22 14:01:33 -07002576 um = nla_data(rt);
2577 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002578
2579 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2580 return -EINVAL;
2581
2582 for (i = 0; i < num_migrate; i++, um++, ma++) {
2583 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2584 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2585 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2586 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2587
2588 ma->proto = um->proto;
2589 ma->mode = um->mode;
2590 ma->reqid = um->reqid;
2591
2592 ma->old_family = um->old_family;
2593 ma->new_family = um->new_family;
2594 }
2595
2596 *num = i;
2597 return 0;
2598}
2599
2600static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002601 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002602{
Thomas Graf7b67c852007-08-22 13:53:52 -07002603 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002604 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002605 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002606 u8 type;
2607 int err;
2608 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002609 struct net *net = sock_net(skb->sk);
Antony Antony4ab47d42017-06-06 12:12:13 +02002610 struct xfrm_encap_tmpl *encap = NULL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002611
Thomas Graf35a7aa02007-08-22 14:00:40 -07002612 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002613 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002614
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002615 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2616
Thomas Graf5424f322007-08-22 14:01:33 -07002617 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002618 if (err)
2619 return err;
2620
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002621 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002622 if (err)
2623 return err;
2624
2625 if (!n)
2626 return 0;
2627
Antony Antony4ab47d42017-06-06 12:12:13 +02002628 if (attrs[XFRMA_ENCAP]) {
2629 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2630 sizeof(*encap), GFP_KERNEL);
2631 if (!encap)
Zheng Yongjun4ac7a6e2021-02-04 15:42:54 +08002632 return -ENOMEM;
Antony Antony4ab47d42017-06-06 12:12:13 +02002633 }
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002634
Antony Antony4ab47d42017-06-06 12:12:13 +02002635 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2636
2637 kfree(encap);
2638
2639 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002640}
2641#else
2642static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002643 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002644{
2645 return -ENOPROTOOPT;
2646}
2647#endif
2648
2649#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002650static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002651{
2652 struct xfrm_user_migrate um;
2653
2654 memset(&um, 0, sizeof(um));
2655 um.proto = m->proto;
2656 um.mode = m->mode;
2657 um.reqid = m->reqid;
2658 um.old_family = m->old_family;
2659 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2660 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2661 um.new_family = m->new_family;
2662 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2663 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2664
Thomas Grafc0144be2007-08-22 13:55:43 -07002665 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002666}
2667
David S. Miller183cad12011-02-24 00:28:01 -05002668static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002669{
2670 struct xfrm_user_kmaddress uk;
2671
2672 memset(&uk, 0, sizeof(uk));
2673 uk.family = k->family;
2674 uk.reserved = k->reserved;
2675 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002676 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002677
2678 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2679}
2680
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03002681static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2682 int with_encp)
Thomas Graf7deb2262007-08-22 13:57:39 -07002683{
2684 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002685 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
Antony Antony8bafd732017-06-06 12:12:14 +02002686 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002687 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2688 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002689}
2690
David S. Miller183cad12011-02-24 00:28:01 -05002691static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2692 int num_migrate, const struct xfrm_kmaddress *k,
Antony Antony8bafd732017-06-06 12:12:14 +02002693 const struct xfrm_selector *sel,
2694 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002695{
David S. Miller183cad12011-02-24 00:28:01 -05002696 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002697 struct xfrm_userpolicy_id *pol_id;
2698 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002699 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002700
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002701 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2702 if (nlh == NULL)
2703 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002704
Thomas Graf7b67c852007-08-22 13:53:52 -07002705 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002706 /* copy data from selector, dir, and type to the pol_id */
2707 memset(pol_id, 0, sizeof(*pol_id));
2708 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2709 pol_id->dir = dir;
2710
David S. Miller1d1e34d2012-06-27 21:57:03 -07002711 if (k != NULL) {
2712 err = copy_to_user_kmaddress(k, skb);
2713 if (err)
2714 goto out_cancel;
2715 }
Antony Antony8bafd732017-06-06 12:12:14 +02002716 if (encap) {
2717 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2718 if (err)
2719 goto out_cancel;
2720 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002721 err = copy_to_user_policy_type(type, skb);
2722 if (err)
2723 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002724 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002725 err = copy_to_user_migrate(mp, skb);
2726 if (err)
2727 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002728 }
2729
Johannes Berg053c0952015-01-16 22:09:00 +01002730 nlmsg_end(skb, nlh);
2731 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002732
2733out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002734 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002735 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002736}
2737
David S. Miller183cad12011-02-24 00:28:01 -05002738static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2739 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002740 const struct xfrm_kmaddress *k,
2741 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002742{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002743 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002744 struct sk_buff *skb;
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05002745 int err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002746
Antony Antony8bafd732017-06-06 12:12:14 +02002747 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2748 GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002749 if (skb == NULL)
2750 return -ENOMEM;
2751
2752 /* build migrate */
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05002753 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2754 BUG_ON(err < 0);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002755
Michal Kubecek21ee5432014-06-03 10:26:06 +02002756 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002757}
2758#else
David S. Miller183cad12011-02-24 00:28:01 -05002759static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2760 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002761 const struct xfrm_kmaddress *k,
2762 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002763{
2764 return -ENOPROTOOPT;
2765}
2766#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002767
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002768#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002769
Dmitry Safonov5461fc02020-09-21 15:36:52 +01002770const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002771 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002772 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2773 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2774 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2775 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2776 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2777 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002778 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002779 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002780 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002781 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002782 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002783 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002784 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002785 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2786 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002787 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002788 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002789 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002790 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002791 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Steffen Klassert2d151d32021-07-18 09:11:06 +02002792 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2793 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794};
Dmitry Safonov5461fc02020-09-21 15:36:52 +01002795EXPORT_SYMBOL_GPL(xfrm_msg_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Thomas Graf492b5582005-05-03 14:26:40 -07002797#undef XMSGSIZE
2798
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002799const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002800 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2801 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2802 [XFRMA_LASTUSED] = { .type = NLA_U64},
2803 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002804 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002805 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2806 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2807 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2808 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2809 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2810 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2811 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2812 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2813 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2814 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2815 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2816 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2817 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2818 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002819 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002820 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002821 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002822 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002823 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002824 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002825 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Steffen Klassertd77e38e2017-04-14 10:06:10 +02002826 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002827 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2828 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
Steffen Klassert7e652642018-06-12 14:07:07 +02002829 [XFRMA_IF_ID] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002830};
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002831EXPORT_SYMBOL_GPL(xfrma_policy);
Thomas Grafcf5cb792007-08-22 13:59:04 -07002832
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002833static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2834 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2835 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2836};
2837
Mathias Krause05600a72013-02-24 14:10:27 +01002838static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002839 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Herbert Xu1137b5e2017-10-19 20:51:10 +08002840 int (*start)(struct netlink_callback *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002842 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002843 const struct nla_policy *nla_pol;
2844 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002845} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2846 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2847 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2848 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002849 .dump = xfrm_dump_sa,
2850 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002851 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2852 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2853 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Herbert Xu1137b5e2017-10-19 20:51:10 +08002854 .start = xfrm_dump_policy_start,
Timo Teras4c563f72008-02-28 21:31:08 -08002855 .dump = xfrm_dump_policy,
2856 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002857 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002858 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002859 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002860 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2861 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002862 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002863 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2864 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002865 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2866 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002867 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002868 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002869 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2870 .nla_pol = xfrma_spd_policy,
2871 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002872 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Steffen Klassert2d151d32021-07-18 09:11:06 +02002873 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
2874 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875};
2876
Johannes Berg2d4bc932017-04-12 14:34:04 +02002877static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2878 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002880 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002881 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002882 const struct xfrm_link *link;
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002883 struct nlmsghdr *nlh64 = NULL;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002884 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 type -= XFRM_MSG_BASE;
2891 link = &xfrm_dispatch[type];
2892
2893 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002894 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002895 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002897 if (in_compat_syscall()) {
2898 struct xfrm_translator *xtr = xfrm_get_translator();
2899
2900 if (!xtr)
2901 return -EOPNOTSUPP;
2902
2903 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2904 link->nla_pol, extack);
2905 xfrm_put_translator(xtr);
2906 if (IS_ERR(nlh64))
2907 return PTR_ERR(nlh64);
2908 if (nlh64)
2909 nlh = nlh64;
2910 }
2911
Thomas Graf492b5582005-05-03 14:26:40 -07002912 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2913 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002914 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002915 struct netlink_dump_control c = {
2916 .start = link->start,
2917 .dump = link->dump,
2918 .done = link->done,
2919 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002921 if (link->dump == NULL) {
2922 err = -EINVAL;
2923 goto err;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002924 }
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002925
2926 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2927 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
2929
Johannes Berg8cb08172019-04-26 14:07:28 +02002930 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2931 link->nla_max ? : XFRMA_MAX,
2932 link->nla_pol ? : xfrma_policy, extack);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002933 if (err < 0)
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002934 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002936 if (link->doit == NULL) {
2937 err = -EINVAL;
2938 goto err;
2939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002941 err = link->doit(skb, nlh, attrs);
2942
Pavel Skripkin7c1a80e2021-06-25 13:23:54 +03002943 /* We need to free skb allocated in xfrm_alloc_compat() before
2944 * returning from this function, because consume_skb() won't take
2945 * care of frag_list since netlink destructor sets
2946 * sbk->head to NULL. (see netlink_skb_destructor())
2947 */
2948 if (skb_has_frag_list(skb)) {
2949 kfree_skb(skb_shinfo(skb)->frag_list);
2950 skb_shinfo(skb)->frag_list = NULL;
2951 }
2952
Dmitry Safonov5106f4a2020-09-21 15:36:55 +01002953err:
2954 kvfree(nlh64);
2955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002958static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Fan Du283bc9f2013-11-07 17:47:50 +08002960 struct net *net = sock_net(skb->sk);
2961
2962 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002963 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002964 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03002967static inline unsigned int xfrm_expire_msgsize(void)
Thomas Graf7deb2262007-08-22 13:57:39 -07002968{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002969 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2970 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002971}
2972
David S. Miller214e0052011-02-24 00:02:38 -05002973static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
2975 struct xfrm_user_expire *ue;
2976 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002977 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Eric W. Biederman15e47302012-09-07 20:12:54 +00002979 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002980 if (nlh == NULL)
2981 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Thomas Graf7b67c852007-08-22 13:53:52 -07002983 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002985 ue->hard = (c->data.hard != 0) ? 1 : 0;
Mathias Krausee3e5fc12017-08-26 17:08:59 +02002986 /* clear the padding bytes */
Kees Cookcaf283d2021-06-17 08:34:19 -07002987 memset_after(ue, 0, hard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
David S. Miller1d1e34d2012-06-27 21:57:03 -07002989 err = xfrm_mark_put(skb, &x->mark);
2990 if (err)
2991 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002992
Steffen Klassert7e652642018-06-12 14:07:07 +02002993 err = xfrm_if_id_put(skb, x->if_id);
2994 if (err)
2995 return err;
2996
Johannes Berg053c0952015-01-16 22:09:00 +01002997 nlmsg_end(skb, nlh);
2998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
David S. Miller214e0052011-02-24 00:02:38 -05003001static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003003 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct sk_buff *skb;
3005
Thomas Graf7deb2262007-08-22 13:57:39 -07003006 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if (skb == NULL)
3008 return -ENOMEM;
3009
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00003010 if (build_expire(skb, x, c) < 0) {
3011 kfree_skb(skb);
3012 return -EMSGSIZE;
3013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Michal Kubecek21ee5432014-06-03 10:26:06 +02003015 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
David S. Miller214e0052011-02-24 00:02:38 -05003018static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003019{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003020 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003021 struct sk_buff *skb;
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003022 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003023
Steffen Klassertd8647b72011-03-08 00:10:27 +00003024 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003025 if (skb == NULL)
3026 return -ENOMEM;
3027
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003028 err = build_aevent(skb, x, c);
3029 BUG_ON(err < 0);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003030
Michal Kubecek21ee5432014-06-03 10:26:06 +02003031 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003032}
3033
David S. Miller214e0052011-02-24 00:02:38 -05003034static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003035{
Alexey Dobriyan70678022008-11-25 17:50:36 -08003036 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003037 struct xfrm_usersa_flush *p;
3038 struct nlmsghdr *nlh;
3039 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07003040 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003041
Thomas Graf7deb2262007-08-22 13:57:39 -07003042 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003043 if (skb == NULL)
3044 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003045
Eric W. Biederman15e47302012-09-07 20:12:54 +00003046 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003047 if (nlh == NULL) {
3048 kfree_skb(skb);
3049 return -EMSGSIZE;
3050 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003051
Thomas Graf7b67c852007-08-22 13:53:52 -07003052 p = nlmsg_data(nlh);
Herbert Xubf08867f92005-06-18 22:44:00 -07003053 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003054
Thomas Graf98250692007-08-22 12:47:26 -07003055 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003056
Michal Kubecek21ee5432014-06-03 10:26:06 +02003057 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003058}
3059
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003060static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003061{
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003062 unsigned int l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08003063 if (x->aead)
3064 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00003065 if (x->aalg) {
3066 l += nla_total_size(sizeof(struct xfrm_algo) +
3067 (x->aalg->alg_key_len + 7) / 8);
3068 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3069 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003070 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08003071 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003072 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07003073 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003074 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07003075 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00003076 if (x->tfcpad)
3077 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00003078 if (x->replay_esn)
3079 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01003080 else
3081 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07003082 if (x->security)
3083 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3084 x->security->ctx_len);
3085 if (x->coaddr)
3086 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01003087 if (x->props.extra_flags)
3088 l += nla_total_size(sizeof(x->props.extra_flags));
Steffen Klassertd77e38e2017-04-14 10:06:10 +02003089 if (x->xso.dev)
Eric Dumazet7770a392021-12-08 12:20:19 -08003090 l += nla_total_size(sizeof(struct xfrm_user_offload));
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02003091 if (x->props.smark.v | x->props.smark.m) {
3092 l += nla_total_size(sizeof(x->props.smark.v));
3093 l += nla_total_size(sizeof(x->props.smark.m));
3094 }
Steffen Klassert7e652642018-06-12 14:07:07 +02003095 if (x->if_id)
3096 l += nla_total_size(sizeof(x->if_id));
Herbert Xu68325d32007-10-09 13:30:57 -07003097
Herbert Xud26f3982007-11-13 21:47:08 -08003098 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02003099 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003100
Antony Antony4e484b32021-12-22 14:11:18 +01003101 if (x->mapping_maxage)
3102 l += nla_total_size(sizeof(x->mapping_maxage));
3103
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003104 return l;
3105}
3106
David S. Miller214e0052011-02-24 00:02:38 -05003107static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003108{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003109 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003110 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07003111 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003112 struct nlmsghdr *nlh;
3113 struct sk_buff *skb;
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003114 unsigned int len = xfrm_sa_len(x);
3115 unsigned int headlen;
3116 int err;
Herbert Xu0603eac2005-06-18 22:54:36 -07003117
3118 headlen = sizeof(*p);
3119 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07003120 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07003121 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00003122 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07003123 }
Thomas Graf7deb2262007-08-22 13:57:39 -07003124 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003125
Thomas Graf7deb2262007-08-22 13:57:39 -07003126 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003127 if (skb == NULL)
3128 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003129
Eric W. Biederman15e47302012-09-07 20:12:54 +00003130 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003131 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003132 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003133 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003134
Thomas Graf7b67c852007-08-22 13:53:52 -07003135 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07003136 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07003137 struct nlattr *attr;
3138
Thomas Graf7b67c852007-08-22 13:53:52 -07003139 id = nlmsg_data(nlh);
Mathias Krause50329c82017-08-26 17:08:58 +02003140 memset(id, 0, sizeof(*id));
Herbert Xu0603eac2005-06-18 22:54:36 -07003141 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3142 id->spi = x->id.spi;
3143 id->family = x->props.family;
3144 id->proto = x->id.proto;
3145
Thomas Grafc0144be2007-08-22 13:55:43 -07003146 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07003147 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07003148 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003149 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07003150
3151 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07003152 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07003153 err = copy_to_user_state_extra(x, p, skb);
3154 if (err)
3155 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003156
Thomas Graf98250692007-08-22 12:47:26 -07003157 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003158
Michal Kubecek21ee5432014-06-03 10:26:06 +02003159 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003160
David S. Miller1d1e34d2012-06-27 21:57:03 -07003161out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003162 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003163 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003164}
3165
David S. Miller214e0052011-02-24 00:02:38 -05003166static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003167{
3168
3169 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003170 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003171 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003172 case XFRM_MSG_NEWAE:
3173 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003174 case XFRM_MSG_DELSA:
3175 case XFRM_MSG_UPDSA:
3176 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003177 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003178 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003179 return xfrm_notify_sa_flush(c);
3180 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003181 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3182 c->event);
3183 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003184 }
3185
3186 return 0;
3187
3188}
3189
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003190static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3191 struct xfrm_policy *xp)
Thomas Graf7deb2262007-08-22 13:57:39 -07003192{
3193 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3194 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00003195 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07003196 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3197 + userpolicy_type_attrsize();
3198}
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08003201 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
David S. Miller1d1e34d2012-06-27 21:57:03 -07003203 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 struct xfrm_user_acquire *ua;
3205 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003206 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003208 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3209 if (nlh == NULL)
3210 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Thomas Graf7b67c852007-08-22 13:53:52 -07003212 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 memcpy(&ua->id, &x->id, sizeof(ua->id));
3214 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3215 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08003216 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 ua->aalgos = xt->aalgos;
3218 ua->ealgos = xt->ealgos;
3219 ua->calgos = xt->calgos;
3220 ua->seq = x->km.seq = seq;
3221
David S. Miller1d1e34d2012-06-27 21:57:03 -07003222 err = copy_to_user_tmpl(xp, skb);
3223 if (!err)
3224 err = copy_to_user_state_sec_ctx(x, skb);
3225 if (!err)
3226 err = copy_to_user_policy_type(xp->type, skb);
3227 if (!err)
3228 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klassert7e652642018-06-12 14:07:07 +02003229 if (!err)
3230 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003231 if (err) {
3232 nlmsg_cancel(skb, nlh);
3233 return err;
3234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Johannes Berg053c0952015-01-16 22:09:00 +01003236 nlmsg_end(skb, nlh);
3237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
3240static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08003241 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003243 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 struct sk_buff *skb;
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003245 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Thomas Graf7deb2262007-08-22 13:57:39 -07003247 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (skb == NULL)
3249 return -ENOMEM;
3250
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003251 err = build_acquire(skb, x, xt, xp);
3252 BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Michal Kubecek21ee5432014-06-03 10:26:06 +02003254 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
3257/* User gives us xfrm_user_policy_info followed by an array of 0
3258 * or more templates.
3259 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003260static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 u8 *data, int len, int *dir)
3262{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003263 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3265 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3266 struct xfrm_policy *xp;
3267 int nr;
3268
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003269 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 case AF_INET:
3271 if (opt != IP_XFRM_POLICY) {
3272 *dir = -EOPNOTSUPP;
3273 return NULL;
3274 }
3275 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003276#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 case AF_INET6:
3278 if (opt != IPV6_XFRM_POLICY) {
3279 *dir = -EOPNOTSUPP;
3280 return NULL;
3281 }
3282 break;
3283#endif
3284 default:
3285 *dir = -EINVAL;
3286 return NULL;
3287 }
3288
3289 *dir = -EINVAL;
3290
3291 if (len < sizeof(*p) ||
3292 verify_newpolicy_info(p))
3293 return NULL;
3294
3295 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08003296 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return NULL;
3298
Herbert Xua4f1bac2005-07-26 15:43:17 -07003299 if (p->dir > XFRM_POLICY_OUT)
3300 return NULL;
3301
Herbert Xu2f09a4d2010-08-14 22:38:09 -07003302 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 if (xp == NULL) {
3304 *dir = -ENOBUFS;
3305 return NULL;
3306 }
3307
3308 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003309 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 copy_templates(xp, ut, nr);
3311
3312 *dir = p->dir;
3313
3314 return xp;
3315}
3316
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003317static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
Thomas Graf7deb2262007-08-22 13:57:39 -07003318{
3319 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3320 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3321 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003322 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07003323 + userpolicy_type_attrsize();
3324}
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05003327 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
3329 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003330 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003331 struct nlmsghdr *nlh;
3332 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Eric W. Biederman15e47302012-09-07 20:12:54 +00003334 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003335 if (nlh == NULL)
3336 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Thomas Graf7b67c852007-08-22 13:53:52 -07003338 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003340 err = copy_to_user_tmpl(xp, skb);
3341 if (!err)
3342 err = copy_to_user_sec_ctx(xp, skb);
3343 if (!err)
3344 err = copy_to_user_policy_type(xp->type, skb);
3345 if (!err)
3346 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klassert7e652642018-06-12 14:07:07 +02003347 if (!err)
3348 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003349 if (err) {
3350 nlmsg_cancel(skb, nlh);
3351 return err;
3352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 upe->hard = !!hard;
3354
Johannes Berg053c0952015-01-16 22:09:00 +01003355 nlmsg_end(skb, nlh);
3356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
David S. Miller214e0052011-02-24 00:02:38 -05003359static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003361 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 struct sk_buff *skb;
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003363 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Thomas Graf7deb2262007-08-22 13:57:39 -07003365 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (skb == NULL)
3367 return -ENOMEM;
3368
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003369 err = build_polexpire(skb, xp, dir, c);
3370 BUG_ON(err < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Michal Kubecek21ee5432014-06-03 10:26:06 +02003372 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
David S. Miller214e0052011-02-24 00:02:38 -05003375static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003376{
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003377 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003378 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003379 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07003380 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003381 struct nlmsghdr *nlh;
3382 struct sk_buff *skb;
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003383 unsigned int headlen;
3384 int err;
Herbert Xu0603eac2005-06-18 22:54:36 -07003385
3386 headlen = sizeof(*p);
3387 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07003388 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07003389 headlen = sizeof(*id);
3390 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07003391 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003392 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07003393 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003394
Thomas Graf7deb2262007-08-22 13:57:39 -07003395 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003396 if (skb == NULL)
3397 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003398
Eric W. Biederman15e47302012-09-07 20:12:54 +00003399 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003400 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003401 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003402 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003403
Thomas Graf7b67c852007-08-22 13:53:52 -07003404 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07003405 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07003406 struct nlattr *attr;
3407
Thomas Graf7b67c852007-08-22 13:53:52 -07003408 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07003409 memset(id, 0, sizeof(*id));
3410 id->dir = dir;
3411 if (c->data.byid)
3412 id->index = xp->index;
3413 else
3414 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3415
Thomas Grafc0144be2007-08-22 13:55:43 -07003416 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07003417 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07003418 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003419 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07003420
3421 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07003422 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003423
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003424 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003425 err = copy_to_user_tmpl(xp, skb);
3426 if (!err)
3427 err = copy_to_user_policy_type(xp->type, skb);
3428 if (!err)
3429 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klassert7e652642018-06-12 14:07:07 +02003430 if (!err)
3431 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003432 if (err)
3433 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003434
Thomas Graf98250692007-08-22 12:47:26 -07003435 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003436
Michal Kubecek21ee5432014-06-03 10:26:06 +02003437 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003438
David S. Miller1d1e34d2012-06-27 21:57:03 -07003439out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003440 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003441 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003442}
3443
David S. Miller214e0052011-02-24 00:02:38 -05003444static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003445{
Alexey Dobriyan70678022008-11-25 17:50:36 -08003446 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003447 struct nlmsghdr *nlh;
3448 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003449 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003450
Thomas Graf7deb2262007-08-22 13:57:39 -07003451 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003452 if (skb == NULL)
3453 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003454
Eric W. Biederman15e47302012-09-07 20:12:54 +00003455 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003456 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003457 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003458 goto out_free_skb;
3459 err = copy_to_user_policy_type(c->data.type, skb);
3460 if (err)
3461 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003462
Thomas Graf98250692007-08-22 12:47:26 -07003463 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003464
Michal Kubecek21ee5432014-06-03 10:26:06 +02003465 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003466
David S. Miller1d1e34d2012-06-27 21:57:03 -07003467out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003468 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003469 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003470}
3471
David S. Miller214e0052011-02-24 00:02:38 -05003472static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003473{
3474
3475 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003476 case XFRM_MSG_NEWPOLICY:
3477 case XFRM_MSG_UPDPOLICY:
3478 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003479 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003480 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003481 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003482 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003483 return xfrm_exp_policy_notify(xp, dir, c);
3484 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003485 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3486 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003487 }
3488
3489 return 0;
3490
3491}
3492
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003493static inline unsigned int xfrm_report_msgsize(void)
Thomas Graf7deb2262007-08-22 13:57:39 -07003494{
3495 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3496}
3497
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003498static int build_report(struct sk_buff *skb, u8 proto,
3499 struct xfrm_selector *sel, xfrm_address_t *addr)
3500{
3501 struct xfrm_user_report *ur;
3502 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003503
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003504 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3505 if (nlh == NULL)
3506 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003507
Thomas Graf7b67c852007-08-22 13:53:52 -07003508 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003509 ur->proto = proto;
3510 memcpy(&ur->sel, sel, sizeof(ur->sel));
3511
David S. Miller1d1e34d2012-06-27 21:57:03 -07003512 if (addr) {
3513 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3514 if (err) {
3515 nlmsg_cancel(skb, nlh);
3516 return err;
3517 }
3518 }
Johannes Berg053c0952015-01-16 22:09:00 +01003519 nlmsg_end(skb, nlh);
3520 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003521}
3522
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003523static int xfrm_send_report(struct net *net, u8 proto,
3524 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003525{
3526 struct sk_buff *skb;
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003527 int err;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003528
Thomas Graf7deb2262007-08-22 13:57:39 -07003529 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003530 if (skb == NULL)
3531 return -ENOMEM;
3532
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003533 err = build_report(skb, proto, sel, addr);
3534 BUG_ON(err < 0);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003535
Michal Kubecek21ee5432014-06-03 10:26:06 +02003536 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003537}
3538
Alexey Dobriyana1b831f2017-09-21 23:48:54 +03003539static inline unsigned int xfrm_mapping_msgsize(void)
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003540{
3541 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3542}
3543
3544static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3545 xfrm_address_t *new_saddr, __be16 new_sport)
3546{
3547 struct xfrm_user_mapping *um;
3548 struct nlmsghdr *nlh;
3549
3550 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3551 if (nlh == NULL)
3552 return -EMSGSIZE;
3553
3554 um = nlmsg_data(nlh);
3555
3556 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3557 um->id.spi = x->id.spi;
3558 um->id.family = x->props.family;
3559 um->id.proto = x->id.proto;
3560 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3561 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3562 um->new_sport = new_sport;
3563 um->old_sport = x->encap->encap_sport;
3564 um->reqid = x->props.reqid;
3565
Johannes Berg053c0952015-01-16 22:09:00 +01003566 nlmsg_end(skb, nlh);
3567 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003568}
3569
3570static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3571 __be16 sport)
3572{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003573 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003574 struct sk_buff *skb;
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003575 int err;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003576
3577 if (x->id.proto != IPPROTO_ESP)
3578 return -EINVAL;
3579
3580 if (!x->encap)
3581 return -EINVAL;
3582
3583 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3584 if (skb == NULL)
3585 return -ENOMEM;
3586
Gustavo A. R. Silva2fc5f832017-10-26 06:31:35 -05003587 err = build_mapping(skb, x, ipaddr, sport);
3588 BUG_ON(err < 0);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003589
Michal Kubecek21ee5432014-06-03 10:26:06 +02003590 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003591}
3592
Horia Geanta0f245582014-02-12 16:20:06 +02003593static bool xfrm_is_alive(const struct km_event *c)
3594{
3595 return (bool)xfrm_acquire_is_on(c->net);
3596}
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598static struct xfrm_mgr netlink_mgr = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 .notify = xfrm_send_state_notify,
3600 .acquire = xfrm_send_acquire,
3601 .compile_policy = xfrm_compile_policy,
3602 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003603 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003604 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003605 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003606 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607};
3608
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003609static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
Patrick McHardybe336902006-03-20 22:40:54 -08003611 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003612 struct netlink_kernel_cfg cfg = {
3613 .groups = XFRMNLGRP_MAX,
3614 .input = xfrm_netlink_rcv,
3615 };
Patrick McHardybe336902006-03-20 22:40:54 -08003616
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003617 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003618 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003620 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003621 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return 0;
3623}
3624
Florian Westphal6218fe12021-04-14 18:12:53 +02003625static void __net_exit xfrm_user_net_pre_exit(struct net *net)
3626{
3627 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3628}
3629
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003630static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003631{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003632 struct net *net;
Florian Westphal6218fe12021-04-14 18:12:53 +02003633
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003634 list_for_each_entry(net, net_exit_list, exit_list)
3635 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003636}
3637
3638static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003639 .init = xfrm_user_net_init,
Florian Westphal6218fe12021-04-14 18:12:53 +02003640 .pre_exit = xfrm_user_net_pre_exit,
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003641 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003642};
3643
3644static int __init xfrm_user_init(void)
3645{
3646 int rv;
3647
3648 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3649
3650 rv = register_pernet_subsys(&xfrm_user_net_ops);
3651 if (rv < 0)
3652 return rv;
3653 rv = xfrm_register_km(&netlink_mgr);
3654 if (rv < 0)
3655 unregister_pernet_subsys(&xfrm_user_net_ops);
3656 return rv;
3657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659static void __exit xfrm_user_exit(void)
3660{
3661 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003662 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
3665module_init(xfrm_user_init);
3666module_exit(xfrm_user_exit);
3667MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003668MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);