blob: 23c31dbda1aa2ab0a9cd7caf3cb19d3d97d7c63a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Sowmini Varadhane33d4f12015-10-21 11:48:25 -040034#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Graf5424f322007-08-22 14:01:33 -070036static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Thomas Graf5424f322007-08-22 14:01:33 -070038 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct xfrm_algo *algp;
40
41 if (!rt)
42 return 0;
43
Thomas Graf5424f322007-08-22 14:01:33 -070044 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080045 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070046 return -EINVAL;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 switch (type) {
49 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53
54 default:
55 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
59 return 0;
60}
61
Martin Willi4447bb32009-11-25 00:29:52 +000062static int verify_auth_trunc(struct nlattr **attrs)
63{
64 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
65 struct xfrm_algo_auth *algp;
66
67 if (!rt)
68 return 0;
69
70 algp = nla_data(rt);
71 if (nla_len(rt) < xfrm_alg_auth_len(algp))
72 return -EINVAL;
73
74 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 return 0;
76}
77
Herbert Xu1a6509d2008-01-28 19:37:29 -080078static int verify_aead(struct nlattr **attrs)
79{
80 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
81 struct xfrm_algo_aead *algp;
82
83 if (!rt)
84 return 0;
85
86 algp = nla_data(rt);
87 if (nla_len(rt) < aead_len(algp))
88 return -EINVAL;
89
90 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
91 return 0;
92}
93
Thomas Graf5424f322007-08-22 14:01:33 -070094static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070095 xfrm_address_t **addrp)
96{
Thomas Graf5424f322007-08-22 14:01:33 -070097 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070098
Thomas Grafcf5cb792007-08-22 13:59:04 -070099 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700100 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700101}
Trent Jaegerdf718372005-12-13 23:12:27 -0800102
Thomas Graf5424f322007-08-22 14:01:33 -0700103static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800104{
Thomas Graf5424f322007-08-22 14:01:33 -0700105 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800106 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800107
108 if (!rt)
109 return 0;
110
Thomas Graf5424f322007-08-22 14:01:33 -0700111 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700112 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800113 return -EINVAL;
114
115 return 0;
116}
117
Steffen Klassertd8647b72011-03-08 00:10:27 +0000118static inline int verify_replay(struct xfrm_usersa_info *p,
119 struct nlattr **attrs)
120{
121 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000122 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000123
124 if (!rt)
Florian Westphal0355a9f2018-02-12 14:42:01 +0100125 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
126
127 rs = nla_data(rt);
128
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
131
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000135
Fan Du01714102014-01-18 09:54:28 +0800136 /* As only ESP and AH support ESN feature. */
137 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000138 return -EINVAL;
139
Steffen Klassertd8647b72011-03-08 00:10:27 +0000140 if (p->replay_window != 0)
141 return -EINVAL;
142
143 return 0;
144}
Trent Jaegerdf718372005-12-13 23:12:27 -0800145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700147 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 int err;
150
151 err = -EINVAL;
152 switch (p->family) {
153 case AF_INET:
154 break;
155
156 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000157#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159#else
160 err = -EAFNOSUPPORT;
161 goto out;
162#endif
163
164 default:
165 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 err = -EINVAL;
169 switch (p->id.proto) {
170 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000171 if ((!attrs[XFRMA_ALG_AUTH] &&
172 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800173 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700174 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000175 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200176 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 goto out;
178 break;
179
180 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800181 if (attrs[XFRMA_ALG_COMP])
182 goto out;
183 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000184 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800185 !attrs[XFRMA_ALG_CRYPT] &&
186 !attrs[XFRMA_ALG_AEAD])
187 goto out;
188 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000189 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800190 attrs[XFRMA_ALG_CRYPT]) &&
191 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000193 if (attrs[XFRMA_TFCPAD] &&
194 p->mode != XFRM_MODE_TUNNEL)
195 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197
198 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700199 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800200 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700201 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000202 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000203 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200204 attrs[XFRMA_TFCPAD] ||
205 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto out;
207 break;
208
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000209#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700210 case IPPROTO_DSTOPTS:
211 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700212 if (attrs[XFRMA_ALG_COMP] ||
213 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000214 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800215 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700216 attrs[XFRMA_ALG_CRYPT] ||
217 attrs[XFRMA_ENCAP] ||
218 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000219 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700220 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700221 goto out;
222 break;
223#endif
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 default:
226 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Herbert Xu1a6509d2008-01-28 19:37:29 -0800229 if ((err = verify_aead(attrs)))
230 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000231 if ((err = verify_auth_trunc(attrs)))
232 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700233 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700235 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700237 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700239 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800240 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000241 if ((err = verify_replay(p, attrs)))
242 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 err = -EINVAL;
245 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700246 case XFRM_MODE_TRANSPORT:
247 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700248 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700249 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251
252 default:
253 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 err = 0;
257
258out:
259 return err;
260}
261
262static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800263 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700264 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct xfrm_algo *p, *ualg;
267 struct xfrm_algo_desc *algo;
268
269 if (!rta)
270 return 0;
271
Thomas Graf5424f322007-08-22 14:01:33 -0700272 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 algo = get_byname(ualg->alg_name, 1);
275 if (!algo)
276 return -ENOSYS;
277 *props = algo->desc.sadb_alg_id;
278
Eric Dumazet0f99be02008-01-08 23:39:06 -0800279 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!p)
281 return -ENOMEM;
282
Herbert Xu04ff1262006-08-13 08:50:00 +1000283 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *algpp = p;
285 return 0;
286}
287
Herbert Xu69b01372015-05-27 16:03:45 +0800288static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
289{
290 struct xfrm_algo *p, *ualg;
291 struct xfrm_algo_desc *algo;
292
293 if (!rta)
294 return 0;
295
296 ualg = nla_data(rta);
297
298 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
299 if (!algo)
300 return -ENOSYS;
301 x->props.ealgo = algo->desc.sadb_alg_id;
302
303 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
304 if (!p)
305 return -ENOMEM;
306
307 strcpy(p->alg_name, algo->name);
308 x->ealg = p;
309 x->geniv = algo->uinfo.encr.geniv;
310 return 0;
311}
312
Martin Willi4447bb32009-11-25 00:29:52 +0000313static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
314 struct nlattr *rta)
315{
316 struct xfrm_algo *ualg;
317 struct xfrm_algo_auth *p;
318 struct xfrm_algo_desc *algo;
319
320 if (!rta)
321 return 0;
322
323 ualg = nla_data(rta);
324
325 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
326 if (!algo)
327 return -ENOSYS;
328 *props = algo->desc.sadb_alg_id;
329
330 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
331 if (!p)
332 return -ENOMEM;
333
334 strcpy(p->alg_name, algo->name);
335 p->alg_key_len = ualg->alg_key_len;
336 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
337 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
338
339 *algpp = p;
340 return 0;
341}
342
343static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
344 struct nlattr *rta)
345{
346 struct xfrm_algo_auth *p, *ualg;
347 struct xfrm_algo_desc *algo;
348
349 if (!rta)
350 return 0;
351
352 ualg = nla_data(rta);
353
354 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
355 if (!algo)
356 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800357 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000358 return -EINVAL;
359 *props = algo->desc.sadb_alg_id;
360
361 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
362 if (!p)
363 return -ENOMEM;
364
365 strcpy(p->alg_name, algo->name);
366 if (!p->alg_trunc_len)
367 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
368
369 *algpp = p;
370 return 0;
371}
372
Herbert Xu69b01372015-05-27 16:03:45 +0800373static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800374{
375 struct xfrm_algo_aead *p, *ualg;
376 struct xfrm_algo_desc *algo;
377
378 if (!rta)
379 return 0;
380
381 ualg = nla_data(rta);
382
383 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
384 if (!algo)
385 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800386 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800387
388 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
389 if (!p)
390 return -ENOMEM;
391
392 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800393 x->aead = p;
394 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800395 return 0;
396}
397
Steffen Klasserte2b19122011-03-28 19:47:30 +0000398static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
399 struct nlattr *rp)
400{
401 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000402 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000403
404 if (!replay_esn || !rp)
405 return 0;
406
407 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000408 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000409
Andy Whitcroft79191ea2017-03-23 07:45:44 +0000410 /* Check the overall length and the internal bitmap length to avoid
411 * potential overflow. */
412 if (nla_len(rp) < ulen ||
413 xfrm_replay_state_esn_len(replay_esn) != ulen ||
414 replay_esn->bmp_len != up->bmp_len)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000415 return -EINVAL;
416
Andy Whitcroft64a54652017-03-22 07:29:31 +0000417 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
418 return -EINVAL;
419
Steffen Klasserte2b19122011-03-28 19:47:30 +0000420 return 0;
421}
422
Steffen Klassertd8647b72011-03-08 00:10:27 +0000423static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
424 struct xfrm_replay_state_esn **preplay_esn,
425 struct nlattr *rta)
426{
427 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000428 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000429
430 if (!rta)
431 return 0;
432
433 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000434 klen = xfrm_replay_state_esn_len(up);
435 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000436
Mathias Krauseecd79182012-09-20 10:01:49 +0000437 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000438 if (!p)
439 return -ENOMEM;
440
Mathias Krauseecd79182012-09-20 10:01:49 +0000441 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000442 if (!pp) {
443 kfree(p);
444 return -ENOMEM;
445 }
446
Mathias Krauseecd79182012-09-20 10:01:49 +0000447 memcpy(p, up, ulen);
448 memcpy(pp, up, ulen);
449
Steffen Klassertd8647b72011-03-08 00:10:27 +0000450 *replay_esn = p;
451 *preplay_esn = pp;
452
453 return 0;
454}
455
Joy Latten661697f2007-04-13 16:14:35 -0700456static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800457{
Trent Jaegerdf718372005-12-13 23:12:27 -0800458 int len = 0;
459
460 if (xfrm_ctx) {
461 len += sizeof(struct xfrm_user_sec_ctx);
462 len += xfrm_ctx->ctx_len;
463 }
464 return len;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
468{
469 memcpy(&x->id, &p->id, sizeof(x->id));
470 memcpy(&x->sel, &p->sel, sizeof(x->sel));
471 memcpy(&x->lft, &p->lft, sizeof(x->lft));
472 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800473 x->props.replay_window = min_t(unsigned int, p->replay_window,
474 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 x->props.reqid = p->reqid;
476 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700477 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700479
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700480 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700481 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800484/*
485 * someday when pfkey also has support, we could have the code
486 * somehow made shareable and move it to xfrm_state.c - JHS
487 *
488*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000489static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
490 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800491{
Thomas Graf5424f322007-08-22 14:01:33 -0700492 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000493 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700494 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
495 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
496 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800497
Steffen Klassertd8647b72011-03-08 00:10:27 +0000498 if (re) {
499 struct xfrm_replay_state_esn *replay_esn;
500 replay_esn = nla_data(re);
501 memcpy(x->replay_esn, replay_esn,
502 xfrm_replay_state_esn_len(replay_esn));
503 memcpy(x->preplay_esn, replay_esn,
504 xfrm_replay_state_esn_len(replay_esn));
505 }
506
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800507 if (rp) {
508 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700509 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800510 memcpy(&x->replay, replay, sizeof(*replay));
511 memcpy(&x->preplay, replay, sizeof(*replay));
512 }
513
514 if (lt) {
515 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700516 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800517 x->curlft.bytes = ltime->bytes;
518 x->curlft.packets = ltime->packets;
519 x->curlft.add_time = ltime->add_time;
520 x->curlft.use_time = ltime->use_time;
521 }
522
Thomas Grafcf5cb792007-08-22 13:59:04 -0700523 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700524 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800525
Thomas Grafcf5cb792007-08-22 13:59:04 -0700526 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700527 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800528}
529
Steffen Klassert78f10c52018-06-12 12:44:26 +0200530static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
531{
532 if (attrs[XFRMA_SET_MARK]) {
533 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
534 if (attrs[XFRMA_SET_MARK_MASK])
535 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
536 else
537 m->m = 0xffffffff;
538 } else {
539 m->v = m->m = 0;
540 }
541}
542
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800543static struct xfrm_state *xfrm_state_construct(struct net *net,
544 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700545 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int *errp)
547{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800548 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int err = -ENOMEM;
550
551 if (!x)
552 goto error_no_put;
553
554 copy_from_user_state(x, p);
555
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100556 if (attrs[XFRMA_SA_EXTRA_FLAGS])
557 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
558
Herbert Xu69b01372015-05-27 16:03:45 +0800559 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800560 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000561 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
562 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000564 if (!x->props.aalgo) {
565 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
566 attrs[XFRMA_ALG_AUTH])))
567 goto error;
568 }
Herbert Xu69b01372015-05-27 16:03:45 +0800569 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto error;
571 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
572 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700573 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700575
576 if (attrs[XFRMA_ENCAP]) {
577 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
578 sizeof(*x->encap), GFP_KERNEL);
579 if (x->encap == NULL)
580 goto error;
581 }
582
Martin Willi35d28562010-12-08 04:37:49 +0000583 if (attrs[XFRMA_TFCPAD])
584 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
585
Thomas Graffd211502007-09-06 03:28:08 -0700586 if (attrs[XFRMA_COADDR]) {
587 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
588 sizeof(*x->coaddr), GFP_KERNEL);
589 if (x->coaddr == NULL)
590 goto error;
591 }
592
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000593 xfrm_mark_get(attrs, &x->mark);
594
Steffen Klassert78f10c52018-06-12 12:44:26 +0200595 xfrm_smark_init(attrs, &x->props.smark);
Lorenzo Colitti34e23de2017-08-11 02:11:33 +0900596
Steffen Klasserta80f5602018-06-12 14:07:07 +0200597 if (attrs[XFRMA_IF_ID])
598 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
599
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700600 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (err)
602 goto error;
603
Mathias Krause2f30ea52016-09-08 18:09:57 +0200604 if (attrs[XFRMA_SEC_CTX]) {
605 err = security_xfrm_state_alloc(x,
606 nla_data(attrs[XFRMA_SEC_CTX]));
607 if (err)
608 goto error;
609 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800610
Steffen Klassertd8647b72011-03-08 00:10:27 +0000611 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
612 attrs[XFRMA_REPLAY_ESN_VAL])))
613 goto error;
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800616 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800617 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800618 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800619
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000620 if ((err = xfrm_init_replay(x)))
621 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800622
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000623 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000624 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return x;
627
628error:
629 x->km.state = XFRM_STATE_DEAD;
630 xfrm_state_put(x);
631error_no_put:
632 *errp = err;
633 return NULL;
634}
635
Christoph Hellwig22e70052007-01-02 15:22:30 -0800636static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700637 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800639 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700640 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct xfrm_state *x;
642 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700643 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Thomas Graf35a7aa02007-08-22 14:00:40 -0700645 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (err)
647 return err;
648
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800649 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!x)
651 return err;
652
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700653 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
655 err = xfrm_state_add(x);
656 else
657 err = xfrm_state_update(x);
658
Tetsuo Handa2e710292014-04-22 21:48:30 +0900659 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (err < 0) {
662 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800663 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700664 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700667 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000668 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700669 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700670
671 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700672out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700673 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return err;
675}
676
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800677static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
678 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700679 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700680 int *errp)
681{
682 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000683 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700684 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000685 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700686
687 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
688 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000689 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700690 } else {
691 xfrm_address_t *saddr = NULL;
692
Thomas Graf35a7aa02007-08-22 14:00:40 -0700693 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700694 if (!saddr) {
695 err = -EINVAL;
696 goto out;
697 }
698
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800699 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000700 x = xfrm_state_lookup_byaddr(net, mark,
701 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800702 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700703 }
704
705 out:
706 if (!x && errp)
707 *errp = err;
708 return x;
709}
710
Christoph Hellwig22e70052007-01-02 15:22:30 -0800711static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700712 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800714 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700716 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700717 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700718 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800720 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700722 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
David S. Miller6f68dc32006-06-08 23:58:52 -0700724 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700725 goto out;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700728 err = -EPERM;
729 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700732 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600733
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700734 if (err < 0)
735 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700736
737 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000738 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700739 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700740 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700742out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900743 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700744 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700745 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
749{
Mathias Krausef778a632012-09-19 11:33:39 +0000750 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 memcpy(&p->id, &x->id, sizeof(p->id));
752 memcpy(&p->sel, &x->sel, sizeof(p->sel));
753 memcpy(&p->lft, &x->lft, sizeof(p->lft));
754 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400755 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
756 put_unaligned(x->stats.replay, &p->stats.replay);
757 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700758 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 p->mode = x->props.mode;
760 p->replay_window = x->props.replay_window;
761 p->reqid = x->props.reqid;
762 p->family = x->props.family;
763 p->flags = x->props.flags;
764 p->seq = x->km.seq;
765}
766
767struct xfrm_dump_info {
768 struct sk_buff *in_skb;
769 struct sk_buff *out_skb;
770 u32 nlmsg_seq;
771 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772};
773
Thomas Grafc0144be2007-08-22 13:55:43 -0700774static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
775{
Thomas Grafc0144be2007-08-22 13:55:43 -0700776 struct xfrm_user_sec_ctx *uctx;
777 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700778 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700779
780 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
781 if (attr == NULL)
782 return -EMSGSIZE;
783
784 uctx = nla_data(attr);
785 uctx->exttype = XFRMA_SEC_CTX;
786 uctx->len = ctx_size;
787 uctx->ctx_doi = s->ctx_doi;
788 uctx->ctx_alg = s->ctx_alg;
789 uctx->ctx_len = s->ctx_len;
790 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
791
792 return 0;
793}
794
Martin Willi4447bb32009-11-25 00:29:52 +0000795static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
796{
797 struct xfrm_algo *algo;
798 struct nlattr *nla;
799
800 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
801 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
802 if (!nla)
803 return -EMSGSIZE;
804
805 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000806 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000807 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
808 algo->alg_key_len = auth->alg_key_len;
809
810 return 0;
811}
812
Steffen Klassert78f10c52018-06-12 12:44:26 +0200813static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
814{
815 int ret = 0;
816
817 if (m->v | m->m) {
818 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
819 if (!ret)
820 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
821 }
822 return ret;
823}
824
Herbert Xu68325d32007-10-09 13:30:57 -0700825/* Don't change this without updating xfrm_sa_len! */
826static int copy_to_user_state_extra(struct xfrm_state *x,
827 struct xfrm_usersa_info *p,
828 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700830 int ret = 0;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 copy_to_user_state(x, p);
833
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100834 if (x->props.extra_flags) {
835 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
836 x->props.extra_flags);
837 if (ret)
838 goto out;
839 }
840
David S. Miller1d1e34d2012-06-27 21:57:03 -0700841 if (x->coaddr) {
842 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
843 if (ret)
844 goto out;
845 }
846 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200847 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
848 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700849 if (ret)
850 goto out;
851 }
852 if (x->aead) {
853 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
854 if (ret)
855 goto out;
856 }
857 if (x->aalg) {
858 ret = copy_to_user_auth(x->aalg, skb);
859 if (!ret)
860 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
861 xfrm_alg_auth_len(x->aalg), x->aalg);
862 if (ret)
863 goto out;
864 }
865 if (x->ealg) {
866 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
867 if (ret)
868 goto out;
869 }
870 if (x->calg) {
871 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
872 if (ret)
873 goto out;
874 }
875 if (x->encap) {
876 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
877 if (ret)
878 goto out;
879 }
880 if (x->tfcpad) {
881 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
882 if (ret)
883 goto out;
884 }
885 ret = xfrm_mark_put(skb, &x->mark);
886 if (ret)
887 goto out;
Steffen Klassert78f10c52018-06-12 12:44:26 +0200888
889 ret = xfrm_smark_put(skb, &x->props.smark);
890 if (ret)
891 goto out;
892
dingzhif293a5e2014-10-30 09:39:36 +0100893 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700894 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
895 xfrm_replay_state_esn_len(x->replay_esn),
896 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100897 else
898 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
899 &x->replay);
900 if (ret)
901 goto out;
Steffen Klasserta80f5602018-06-12 14:07:07 +0200902
903 if (x->if_id) {
904 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
905 if (ret)
906 goto out;
907 }
908
Steffen Klassert2a5cc532017-08-31 10:37:00 +0200909 if (x->security)
910 ret = copy_sec_ctx(x->security, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700911out:
912 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700913}
914
915static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
916{
917 struct xfrm_dump_info *sp = ptr;
918 struct sk_buff *in_skb = sp->in_skb;
919 struct sk_buff *skb = sp->out_skb;
920 struct xfrm_usersa_info *p;
921 struct nlmsghdr *nlh;
922 int err;
923
Eric W. Biederman15e47302012-09-07 20:12:54 +0000924 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700925 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
926 if (nlh == NULL)
927 return -EMSGSIZE;
928
929 p = nlmsg_data(nlh);
930
931 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700932 if (err) {
933 nlmsg_cancel(skb, nlh);
934 return err;
935 }
Thomas Graf98250692007-08-22 12:47:26 -0700936 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Timo Teras4c563f72008-02-28 21:31:08 -0800940static int xfrm_dump_sa_done(struct netlink_callback *cb)
941{
942 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800943 struct sock *sk = cb->skb->sk;
944 struct net *net = sock_net(sk);
945
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200946 if (cb->args[0])
947 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800948 return 0;
949}
950
Nicolas Dichteld3623092014-02-14 15:30:36 +0100951static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
953{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800954 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800955 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct xfrm_dump_info info;
957
Timo Teras4c563f72008-02-28 21:31:08 -0800958 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
959 sizeof(cb->args) - sizeof(cb->args[0]));
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 info.in_skb = cb->skb;
962 info.out_skb = skb;
963 info.nlmsg_seq = cb->nlh->nlmsg_seq;
964 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800965
966 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100967 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100968 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100969 u8 proto = 0;
970 int err;
971
Nicolas Dichteld3623092014-02-14 15:30:36 +0100972 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
973 xfrma_policy);
974 if (err < 0)
975 return err;
976
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100977 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +0200978 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
979 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +0100980 if (filter == NULL)
981 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100982 }
983
984 if (attrs[XFRMA_PROTO])
985 proto = nla_get_u8(attrs[XFRMA_PROTO]);
986
987 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200988 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -0800989 }
990
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800991 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 return skb->len;
994}
995
996static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
997 struct xfrm_state *x, u32 seq)
998{
999 struct xfrm_dump_info info;
1000 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +00001001 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Thomas Graf7deb2262007-08-22 13:57:39 -07001003 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (!skb)
1005 return ERR_PTR(-ENOMEM);
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 info.in_skb = in_skb;
1008 info.out_skb = skb;
1009 info.nlmsg_seq = seq;
1010 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Mathias Krause864745d2012-09-13 11:41:26 +00001012 err = dump_one_state(x, 0, &info);
1013 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +00001015 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
1018 return skb;
1019}
1020
Michal Kubecek21ee5432014-06-03 10:26:06 +02001021/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1022 * Must be called with RCU read lock.
1023 */
1024static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1025 u32 pid, unsigned int group)
1026{
1027 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1028
Florian Westphal301a6da2018-06-25 14:00:07 +02001029 if (!nlsk) {
1030 kfree_skb(skb);
1031 return -EPIPE;
1032 }
1033
1034 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
Michal Kubecek21ee5432014-06-03 10:26:06 +02001035}
1036
Thomas Graf7deb2262007-08-22 13:57:39 -07001037static inline size_t xfrm_spdinfo_msgsize(void)
1038{
1039 return NLMSG_ALIGN(4)
1040 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001041 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1042 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1043 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -07001044}
1045
Alexey Dobriyane0710412010-01-23 13:37:10 +00001046static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001047 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001048{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001049 struct xfrmk_spdinfo si;
1050 struct xfrmu_spdinfo spc;
1051 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001052 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001053 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001054 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001055 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001056 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001057
Eric W. Biederman15e47302012-09-07 20:12:54 +00001058 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001059 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001060 return -EMSGSIZE;
1061
1062 f = nlmsg_data(nlh);
1063 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001064 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001065 spc.incnt = si.incnt;
1066 spc.outcnt = si.outcnt;
1067 spc.fwdcnt = si.fwdcnt;
1068 spc.inscnt = si.inscnt;
1069 spc.outscnt = si.outscnt;
1070 spc.fwdscnt = si.fwdscnt;
1071 sph.spdhcnt = si.spdhcnt;
1072 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001073
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001074 do {
1075 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1076
1077 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1078 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1079 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1080 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1081 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1082
David S. Miller1d1e34d2012-06-27 21:57:03 -07001083 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1084 if (!err)
1085 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001086 if (!err)
1087 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1088 if (!err)
1089 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001090 if (err) {
1091 nlmsg_cancel(skb, nlh);
1092 return err;
1093 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001094
Johannes Berg053c0952015-01-16 22:09:00 +01001095 nlmsg_end(skb, nlh);
1096 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001097}
1098
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001099static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1100 struct nlattr **attrs)
1101{
1102 struct net *net = sock_net(skb->sk);
1103 struct xfrmu_spdhthresh *thresh4 = NULL;
1104 struct xfrmu_spdhthresh *thresh6 = NULL;
1105
1106 /* selector prefixlen thresholds to hash policies */
1107 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1108 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1109
1110 if (nla_len(rta) < sizeof(*thresh4))
1111 return -EINVAL;
1112 thresh4 = nla_data(rta);
1113 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1114 return -EINVAL;
1115 }
1116 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1117 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1118
1119 if (nla_len(rta) < sizeof(*thresh6))
1120 return -EINVAL;
1121 thresh6 = nla_data(rta);
1122 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1123 return -EINVAL;
1124 }
1125
1126 if (thresh4 || thresh6) {
1127 write_seqlock(&net->xfrm.policy_hthresh.lock);
1128 if (thresh4) {
1129 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1130 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1131 }
1132 if (thresh6) {
1133 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1134 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1135 }
1136 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1137
1138 xfrm_policy_hash_rebuild(net);
1139 }
1140
1141 return 0;
1142}
1143
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001144static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001145 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001146{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001147 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001148 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001149 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001150 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001151 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001152
Thomas Graf7deb2262007-08-22 13:57:39 -07001153 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001154 if (r_skb == NULL)
1155 return -ENOMEM;
1156
Eric W. Biederman15e47302012-09-07 20:12:54 +00001157 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001158 BUG();
1159
Eric W. Biederman15e47302012-09-07 20:12:54 +00001160 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001161}
1162
Thomas Graf7deb2262007-08-22 13:57:39 -07001163static inline size_t xfrm_sadinfo_msgsize(void)
1164{
1165 return NLMSG_ALIGN(4)
1166 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1167 + nla_total_size(4); /* XFRMA_SAD_CNT */
1168}
1169
Alexey Dobriyane0710412010-01-23 13:37:10 +00001170static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001171 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001172{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001173 struct xfrmk_sadinfo si;
1174 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001175 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001176 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001177 u32 *f;
1178
Eric W. Biederman15e47302012-09-07 20:12:54 +00001179 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001180 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001181 return -EMSGSIZE;
1182
1183 f = nlmsg_data(nlh);
1184 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001185 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001186
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001187 sh.sadhmcnt = si.sadhmcnt;
1188 sh.sadhcnt = si.sadhcnt;
1189
David S. Miller1d1e34d2012-06-27 21:57:03 -07001190 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1191 if (!err)
1192 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1193 if (err) {
1194 nlmsg_cancel(skb, nlh);
1195 return err;
1196 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001197
Johannes Berg053c0952015-01-16 22:09:00 +01001198 nlmsg_end(skb, nlh);
1199 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001200}
1201
1202static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001203 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001204{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001205 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001206 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001207 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001208 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001209 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001210
Thomas Graf7deb2262007-08-22 13:57:39 -07001211 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001212 if (r_skb == NULL)
1213 return -ENOMEM;
1214
Eric W. Biederman15e47302012-09-07 20:12:54 +00001215 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001216 BUG();
1217
Eric W. Biederman15e47302012-09-07 20:12:54 +00001218 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001219}
1220
Christoph Hellwig22e70052007-01-02 15:22:30 -08001221static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001222 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001224 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001225 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 struct xfrm_state *x;
1227 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001228 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001230 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (x == NULL)
1232 goto out_noput;
1233
1234 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1235 if (IS_ERR(resp_skb)) {
1236 err = PTR_ERR(resp_skb);
1237 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001238 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 xfrm_state_put(x);
1241out_noput:
1242 return err;
1243}
1244
Christoph Hellwig22e70052007-01-02 15:22:30 -08001245static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001246 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001248 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct xfrm_state *x;
1250 struct xfrm_userspi_info *p;
1251 struct sk_buff *resp_skb;
1252 xfrm_address_t *daddr;
1253 int family;
1254 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001255 u32 mark;
1256 struct xfrm_mark m;
Steffen Klasserta80f5602018-06-12 14:07:07 +02001257 u32 if_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Thomas Graf7b67c852007-08-22 13:53:52 -07001259 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001260 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (err)
1262 goto out_noput;
1263
1264 family = p->info.family;
1265 daddr = &p->info.id.daddr;
1266
1267 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001268
1269 mark = xfrm_mark_get(attrs, &m);
Steffen Klasserta80f5602018-06-12 14:07:07 +02001270
1271 if (attrs[XFRMA_IF_ID])
1272 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001275 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001276 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 xfrm_state_put(x);
1278 x = NULL;
1279 }
1280 }
1281
1282 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001283 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Steffen Klasserta80f5602018-06-12 14:07:07 +02001284 if_id, p->info.id.proto, daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 &p->info.saddr, 1,
1286 family);
1287 err = -ENOENT;
1288 if (x == NULL)
1289 goto out_noput;
1290
Herbert Xu658b2192007-10-09 13:29:52 -07001291 err = xfrm_alloc_spi(x, p->min, p->max);
1292 if (err)
1293 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Herbert Xu658b2192007-10-09 13:29:52 -07001295 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (IS_ERR(resp_skb)) {
1297 err = PTR_ERR(resp_skb);
1298 goto out;
1299 }
1300
Eric W. Biederman15e47302012-09-07 20:12:54 +00001301 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303out:
1304 xfrm_state_put(x);
1305out_noput:
1306 return err;
1307}
1308
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001309static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 switch (dir) {
1312 case XFRM_POLICY_IN:
1313 case XFRM_POLICY_OUT:
1314 case XFRM_POLICY_FWD:
1315 break;
1316
1317 default:
1318 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 return 0;
1322}
1323
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001324static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001325{
1326 switch (type) {
1327 case XFRM_POLICY_TYPE_MAIN:
1328#ifdef CONFIG_XFRM_SUB_POLICY
1329 case XFRM_POLICY_TYPE_SUB:
1330#endif
1331 break;
1332
1333 default:
1334 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001335 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001336
1337 return 0;
1338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1341{
Fan Due682adf2013-11-07 17:47:48 +08001342 int ret;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 switch (p->share) {
1345 case XFRM_SHARE_ANY:
1346 case XFRM_SHARE_SESSION:
1347 case XFRM_SHARE_USER:
1348 case XFRM_SHARE_UNIQUE:
1349 break;
1350
1351 default:
1352 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 switch (p->action) {
1356 case XFRM_POLICY_ALLOW:
1357 case XFRM_POLICY_BLOCK:
1358 break;
1359
1360 default:
1361 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 switch (p->sel.family) {
1365 case AF_INET:
1366 break;
1367
1368 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001369#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 break;
1371#else
1372 return -EAFNOSUPPORT;
1373#endif
1374
1375 default:
1376 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Fan Due682adf2013-11-07 17:47:48 +08001379 ret = verify_policy_dir(p->dir);
1380 if (ret)
1381 return ret;
1382 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1383 return -EINVAL;
1384
1385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Thomas Graf5424f322007-08-22 14:01:33 -07001388static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001389{
Thomas Graf5424f322007-08-22 14:01:33 -07001390 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001391 struct xfrm_user_sec_ctx *uctx;
1392
1393 if (!rt)
1394 return 0;
1395
Thomas Graf5424f322007-08-22 14:01:33 -07001396 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001397 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1401 int nr)
1402{
1403 int i;
1404
1405 xp->xfrm_nr = nr;
1406 for (i = 0; i < nr; i++, ut++) {
1407 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1408
1409 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1410 memcpy(&t->saddr, &ut->saddr,
1411 sizeof(xfrm_address_t));
1412 t->reqid = ut->reqid;
1413 t->mode = ut->mode;
1414 t->share = ut->share;
1415 t->optional = ut->optional;
1416 t->aalgos = ut->aalgos;
1417 t->ealgos = ut->ealgos;
1418 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001419 /* If all masks are ~0, then we allow all algorithms. */
1420 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001421 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423}
1424
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001425static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1426{
Steffen Klassert9a54c512017-12-08 08:07:25 +01001427 u16 prev_family;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001428 int i;
1429
1430 if (nr > XFRM_MAX_DEPTH)
1431 return -EINVAL;
1432
Steffen Klassert9a54c512017-12-08 08:07:25 +01001433 prev_family = family;
1434
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001435 for (i = 0; i < nr; i++) {
1436 /* We never validated the ut->family value, so many
1437 * applications simply leave it at zero. The check was
1438 * never made and ut->family was ignored because all
1439 * templates could be assumed to have the same family as
1440 * the policy itself. Now that we will have ipv4-in-ipv6
1441 * and ipv6-in-ipv4 tunnels, this is no longer true.
1442 */
1443 if (!ut[i].family)
1444 ut[i].family = family;
1445
Steffen Klassert9a54c512017-12-08 08:07:25 +01001446 if ((ut[i].mode == XFRM_MODE_TRANSPORT) &&
1447 (ut[i].family != prev_family))
1448 return -EINVAL;
1449
1450 prev_family = ut[i].family;
1451
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001452 switch (ut[i].family) {
1453 case AF_INET:
1454 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001455#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001456 case AF_INET6:
1457 break;
1458#endif
1459 default:
1460 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001461 }
Cong Wang85552882017-11-27 11:15:16 -08001462
1463 switch (ut[i].id.proto) {
1464 case IPPROTO_AH:
1465 case IPPROTO_ESP:
1466 case IPPROTO_COMP:
1467#if IS_ENABLED(CONFIG_IPV6)
1468 case IPPROTO_ROUTING:
1469 case IPPROTO_DSTOPTS:
1470#endif
1471 case IPSEC_PROTO_ANY:
1472 break;
1473 default:
1474 return -EINVAL;
1475 }
1476
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001477 }
1478
1479 return 0;
1480}
1481
Thomas Graf5424f322007-08-22 14:01:33 -07001482static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Thomas Graf5424f322007-08-22 14:01:33 -07001484 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (!rt) {
1487 pol->xfrm_nr = 0;
1488 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001489 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1490 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001491 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001493 err = validate_tmpl(nr, utmpl, pol->family);
1494 if (err)
1495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Thomas Graf5424f322007-08-22 14:01:33 -07001497 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499 return 0;
1500}
1501
Thomas Graf5424f322007-08-22 14:01:33 -07001502static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001503{
Thomas Graf5424f322007-08-22 14:01:33 -07001504 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001505 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001506 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001507 int err;
1508
1509 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001510 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001511 type = upt->type;
1512 }
1513
1514 err = verify_policy_type(type);
1515 if (err)
1516 return err;
1517
1518 *tp = type;
1519 return 0;
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1523{
1524 xp->priority = p->priority;
1525 xp->index = p->index;
1526 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1527 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1528 xp->action = p->action;
1529 xp->flags = p->flags;
1530 xp->family = p->sel.family;
1531 /* XXX xp->share = p->share; */
1532}
1533
1534static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1535{
Mathias Krause7b789832012-09-19 11:33:40 +00001536 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1538 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1539 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1540 p->priority = xp->priority;
1541 p->index = xp->index;
1542 p->sel.family = xp->family;
1543 p->dir = dir;
1544 p->action = xp->action;
1545 p->flags = xp->flags;
1546 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1547}
1548
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001549static 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 -07001550{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001551 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int err;
1553
1554 if (!xp) {
1555 *errp = -ENOMEM;
1556 return NULL;
1557 }
1558
1559 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001560
Thomas Graf35a7aa02007-08-22 14:00:40 -07001561 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001562 if (err)
1563 goto error;
1564
Thomas Graf35a7aa02007-08-22 14:00:40 -07001565 if (!(err = copy_from_user_tmpl(xp, attrs)))
1566 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001567 if (err)
1568 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001570 xfrm_mark_get(attrs, &xp->mark);
1571
Steffen Klasserta80f5602018-06-12 14:07:07 +02001572 if (attrs[XFRMA_IF_ID])
1573 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001576 error:
1577 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001578 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001579 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001580 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Christoph Hellwig22e70052007-01-02 15:22:30 -08001583static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001584 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001586 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001587 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001589 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int err;
1591 int excl;
1592
1593 err = verify_newpolicy_info(p);
1594 if (err)
1595 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001596 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001597 if (err)
1598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001600 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!xp)
1602 return err;
1603
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001604 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001605 * Aha! this is anti-netlink really i.e more pfkey derived
1606 * in netlink excl is a flag and you wouldnt need
1607 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1609 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001610 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001613 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 kfree(xp);
1615 return err;
1616 }
1617
Herbert Xuf60f6b82005-06-18 22:44:37 -07001618 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001619 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001620 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001621 km_policy_notify(xp, p->dir, &c);
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 xfrm_pol_put(xp);
1624
1625 return 0;
1626}
1627
1628static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1629{
1630 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1631 int i;
1632
1633 if (xp->xfrm_nr == 0)
1634 return 0;
1635
1636 for (i = 0; i < xp->xfrm_nr; i++) {
1637 struct xfrm_user_tmpl *up = &vec[i];
1638 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1639
Mathias Krause1f868402012-09-19 11:33:41 +00001640 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001642 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1644 up->reqid = kp->reqid;
1645 up->mode = kp->mode;
1646 up->share = kp->share;
1647 up->optional = kp->optional;
1648 up->aalgos = kp->aalgos;
1649 up->ealgos = kp->ealgos;
1650 up->calgos = kp->calgos;
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Thomas Grafc0144be2007-08-22 13:55:43 -07001653 return nla_put(skb, XFRMA_TMPL,
1654 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001655}
1656
Serge Hallyn0d681622006-07-24 23:30:44 -07001657static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1658{
1659 if (x->security) {
1660 return copy_sec_ctx(x->security, skb);
1661 }
1662 return 0;
1663}
1664
1665static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1666{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001667 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001668 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001669 return 0;
1670}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001671static inline size_t userpolicy_type_attrsize(void)
1672{
1673#ifdef CONFIG_XFRM_SUB_POLICY
1674 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1675#else
1676 return 0;
1677#endif
1678}
Serge Hallyn0d681622006-07-24 23:30:44 -07001679
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001680#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001681static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001682{
Eric Dumazet2038a9e2018-06-18 21:35:07 -07001683 struct xfrm_userpolicy_type upt;
1684
1685 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1686 memset(&upt, 0, sizeof(upt));
1687 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001688
Thomas Grafc0144be2007-08-22 13:55:43 -07001689 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001690}
1691
1692#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001693static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001694{
1695 return 0;
1696}
1697#endif
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1700{
1701 struct xfrm_dump_info *sp = ptr;
1702 struct xfrm_userpolicy_info *p;
1703 struct sk_buff *in_skb = sp->in_skb;
1704 struct sk_buff *skb = sp->out_skb;
1705 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001706 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Eric W. Biederman15e47302012-09-07 20:12:54 +00001708 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001709 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1710 if (nlh == NULL)
1711 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Thomas Graf7b67c852007-08-22 13:53:52 -07001713 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001715 err = copy_to_user_tmpl(xp, skb);
1716 if (!err)
1717 err = copy_to_user_sec_ctx(xp, skb);
1718 if (!err)
1719 err = copy_to_user_policy_type(xp->type, skb);
1720 if (!err)
1721 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klasserta80f5602018-06-12 14:07:07 +02001722 if (!err)
1723 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001724 if (err) {
1725 nlmsg_cancel(skb, nlh);
1726 return err;
1727 }
Thomas Graf98250692007-08-22 12:47:26 -07001728 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Timo Teras4c563f72008-02-28 21:31:08 -08001732static int xfrm_dump_policy_done(struct netlink_callback *cb)
1733{
Herbert Xu543aabb2017-10-19 20:51:10 +08001734 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Fan Du283bc9f2013-11-07 17:47:50 +08001735 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001736
Fan Du283bc9f2013-11-07 17:47:50 +08001737 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001738 return 0;
1739}
1740
Herbert Xu543aabb2017-10-19 20:51:10 +08001741static int xfrm_dump_policy_start(struct netlink_callback *cb)
1742{
1743 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1744
1745 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1746
1747 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1748 return 0;
1749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1752{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001753 struct net *net = sock_net(skb->sk);
Herbert Xu543aabb2017-10-19 20:51:10 +08001754 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 struct xfrm_dump_info info;
1756
1757 info.in_skb = cb->skb;
1758 info.out_skb = skb;
1759 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1760 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001761
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001762 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 return skb->len;
1765}
1766
1767static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1768 struct xfrm_policy *xp,
1769 int dir, u32 seq)
1770{
1771 struct xfrm_dump_info info;
1772 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001773 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Thomas Graf7deb2262007-08-22 13:57:39 -07001775 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (!skb)
1777 return ERR_PTR(-ENOMEM);
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 info.in_skb = in_skb;
1780 info.out_skb = skb;
1781 info.nlmsg_seq = seq;
1782 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Mathias Krausec2546372012-09-14 09:58:32 +00001784 err = dump_one_policy(xp, dir, 0, &info);
1785 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001787 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789
1790 return skb;
1791}
1792
Christoph Hellwig22e70052007-01-02 15:22:30 -08001793static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001794 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001796 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 struct xfrm_policy *xp;
1798 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001799 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001801 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001803 struct xfrm_mark m;
1804 u32 mark = xfrm_mark_get(attrs, &m);
Steffen Klasserta80f5602018-06-12 14:07:07 +02001805 u32 if_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Thomas Graf7b67c852007-08-22 13:53:52 -07001807 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1809
Thomas Graf35a7aa02007-08-22 14:00:40 -07001810 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001811 if (err)
1812 return err;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 err = verify_policy_dir(p->dir);
1815 if (err)
1816 return err;
1817
Steffen Klasserta80f5602018-06-12 14:07:07 +02001818 if (attrs[XFRMA_IF_ID])
1819 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (p->index)
Steffen Klasserta80f5602018-06-12 14:07:07 +02001822 xp = xfrm_policy_byid(net, mark, if_id, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001823 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001824 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001825 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001826
Thomas Graf35a7aa02007-08-22 14:00:40 -07001827 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001828 if (err)
1829 return err;
1830
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001831 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001832 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001833 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001834
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001835 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001836 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001837 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001838 }
Steffen Klasserta80f5602018-06-12 14:07:07 +02001839 xp = xfrm_policy_bysel_ctx(net, mark, if_id, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001840 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001841 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (xp == NULL)
1844 return -ENOENT;
1845
1846 if (!delete) {
1847 struct sk_buff *resp_skb;
1848
1849 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1850 if (IS_ERR(resp_skb)) {
1851 err = PTR_ERR(resp_skb);
1852 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001853 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001854 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001856 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001857 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001858
1859 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001860 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001861
Herbert Xue7443892005-06-18 22:44:18 -07001862 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001863 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001864 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001865 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001866 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001869out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001870 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001871 if (delete && err == 0)
1872 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return err;
1874}
1875
Christoph Hellwig22e70052007-01-02 15:22:30 -08001876static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001877 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001879 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001880 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001881 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001882 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Tetsuo Handa2e710292014-04-22 21:48:30 +09001884 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001885 if (err) {
1886 if (err == -ESRCH) /* empty table */
1887 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001888 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001889 }
Herbert Xubf088672005-06-18 22:44:00 -07001890 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001891 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001892 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001893 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001894 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001895 km_state_notify(NULL, &c);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return 0;
1898}
1899
Steffen Klassertd8647b72011-03-08 00:10:27 +00001900static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001901{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001902 size_t replay_size = x->replay_esn ?
1903 xfrm_replay_state_esn_len(x->replay_esn) :
1904 sizeof(struct xfrm_replay_state);
1905
Thomas Graf7deb2262007-08-22 13:57:39 -07001906 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001907 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001908 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001909 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001910 + nla_total_size(4) /* XFRM_AE_RTHR */
1911 + nla_total_size(4); /* XFRM_AE_ETHR */
1912}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001913
David S. Miller214e0052011-02-24 00:02:38 -05001914static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001915{
1916 struct xfrm_aevent_id *id;
1917 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001918 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001919
Eric W. Biederman15e47302012-09-07 20:12:54 +00001920 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001921 if (nlh == NULL)
1922 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001923
Thomas Graf7b67c852007-08-22 13:53:52 -07001924 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001925 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001926 id->sa_id.spi = x->id.spi;
1927 id->sa_id.family = x->props.family;
1928 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001929 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001930 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001931 id->flags = c->data.aevent;
1932
David S. Millerd0fde792012-03-29 04:02:26 -04001933 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001934 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1935 xfrm_replay_state_esn_len(x->replay_esn),
1936 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001937 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001938 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1939 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001940 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001941 if (err)
1942 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001943 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1944 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001945 if (err)
1946 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001947
David S. Miller1d1e34d2012-06-27 21:57:03 -07001948 if (id->flags & XFRM_AE_RTHR) {
1949 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1950 if (err)
1951 goto out_cancel;
1952 }
1953 if (id->flags & XFRM_AE_ETHR) {
1954 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1955 x->replay_maxage * 10 / HZ);
1956 if (err)
1957 goto out_cancel;
1958 }
1959 err = xfrm_mark_put(skb, &x->mark);
1960 if (err)
1961 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001962
Steffen Klasserta80f5602018-06-12 14:07:07 +02001963 err = xfrm_if_id_put(skb, x->if_id);
1964 if (err)
1965 goto out_cancel;
1966
Johannes Berg053c0952015-01-16 22:09:00 +01001967 nlmsg_end(skb, nlh);
1968 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001969
David S. Miller1d1e34d2012-06-27 21:57:03 -07001970out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001971 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001972 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001973}
1974
Christoph Hellwig22e70052007-01-02 15:22:30 -08001975static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001976 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001977{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001978 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001979 struct xfrm_state *x;
1980 struct sk_buff *r_skb;
1981 int err;
1982 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001983 u32 mark;
1984 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001985 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001986 struct xfrm_usersa_id *id = &p->sa_id;
1987
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001988 mark = xfrm_mark_get(attrs, &m);
1989
1990 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001991 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001992 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001993
1994 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1995 if (r_skb == NULL) {
1996 xfrm_state_put(x);
1997 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001998 }
1999
2000 /*
2001 * XXX: is this lock really needed - none of the other
2002 * gets lock (the concern is things getting updated
2003 * while we are still reading) - jhs
2004 */
2005 spin_lock_bh(&x->lock);
2006 c.data.aevent = p->flags;
2007 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002008 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002009
2010 if (build_aevent(r_skb, x, &c) < 0)
2011 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00002012 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002013 spin_unlock_bh(&x->lock);
2014 xfrm_state_put(x);
2015 return err;
2016}
2017
Christoph Hellwig22e70052007-01-02 15:22:30 -08002018static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002019 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002020{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002021 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002022 struct xfrm_state *x;
2023 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08002024 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002025 u32 mark = 0;
2026 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07002027 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07002028 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00002029 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07002030 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02002031 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2032 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002033
Michael Rossberg4e077232015-09-29 11:25:08 +02002034 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002035 return err;
2036
2037 /* pedantic mode - thou shalt sayeth replaceth */
2038 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2039 return err;
2040
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002041 mark = xfrm_mark_get(attrs, &m);
2042
2043 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 -08002044 if (x == NULL)
2045 return -ESRCH;
2046
2047 if (x->km.state != XFRM_STATE_VALID)
2048 goto out;
2049
Steffen Klassert4479ff72013-09-09 09:39:01 +02002050 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00002051 if (err)
2052 goto out;
2053
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002054 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00002055 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002056 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002057
2058 c.event = nlh->nlmsg_type;
2059 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002060 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002061 c.data.aevent = XFRM_AE_CU;
2062 km_state_notify(x, &c);
2063 err = 0;
2064out:
2065 xfrm_state_put(x);
2066 return err;
2067}
2068
Christoph Hellwig22e70052007-01-02 15:22:30 -08002069static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002070 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002072 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002073 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002074 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002075 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002076
Thomas Graf35a7aa02007-08-22 14:00:40 -07002077 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002078 if (err)
2079 return err;
2080
Tetsuo Handa2e710292014-04-22 21:48:30 +09002081 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002082 if (err) {
2083 if (err == -ESRCH) /* empty table */
2084 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002085 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002086 }
2087
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002088 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002089 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002090 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002091 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002092 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002093 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 0;
2095}
2096
Christoph Hellwig22e70052007-01-02 15:22:30 -08002097static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002098 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002099{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002100 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002101 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002102 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002103 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002104 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002105 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002106 struct xfrm_mark m;
2107 u32 mark = xfrm_mark_get(attrs, &m);
Steffen Klasserta80f5602018-06-12 14:07:07 +02002108 u32 if_id = 0;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002109
Thomas Graf35a7aa02007-08-22 14:00:40 -07002110 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002111 if (err)
2112 return err;
2113
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002114 err = verify_policy_dir(p->dir);
2115 if (err)
2116 return err;
2117
Steffen Klasserta80f5602018-06-12 14:07:07 +02002118 if (attrs[XFRMA_IF_ID])
2119 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2120
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002121 if (p->index)
Steffen Klasserta80f5602018-06-12 14:07:07 +02002122 xp = xfrm_policy_byid(net, mark, if_id, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002123 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002124 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002125 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002126
Thomas Graf35a7aa02007-08-22 14:00:40 -07002127 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002128 if (err)
2129 return err;
2130
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002131 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002132 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002133 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002134
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002135 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002136 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002137 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002138 }
Steffen Klasserta80f5602018-06-12 14:07:07 +02002139 xp = xfrm_policy_bysel_ctx(net, mark, if_id, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002140 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002141 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002142 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002143 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002144 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002145
Timo Teräsea2dea92010-03-31 00:17:05 +00002146 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002147 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002148
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002149 err = 0;
2150 if (up->hard) {
2151 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002152 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002153 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002154 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002155
2156out:
2157 xfrm_pol_put(xp);
2158 return err;
2159}
2160
Christoph Hellwig22e70052007-01-02 15:22:30 -08002161static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002162 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002163{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002164 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002165 struct xfrm_state *x;
2166 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002167 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002168 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002169 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002170 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002171
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002172 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002173
David S. Miller3a765aa2007-02-26 14:52:21 -08002174 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002175 if (x == NULL)
2176 return err;
2177
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002178 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002179 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002180 if (x->km.state != XFRM_STATE_VALID)
2181 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002182 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002183
Joy Latten161a09e2006-11-27 13:11:54 -06002184 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002185 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002186 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002187 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002188 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002189out:
2190 spin_unlock_bh(&x->lock);
2191 xfrm_state_put(x);
2192 return err;
2193}
2194
Christoph Hellwig22e70052007-01-02 15:22:30 -08002195static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002196 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002197{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002198 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002199 struct xfrm_policy *xp;
2200 struct xfrm_user_tmpl *ut;
2201 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002202 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002203 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002204
Thomas Graf7b67c852007-08-22 13:53:52 -07002205 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002206 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002207 int err = -ENOMEM;
2208
2209 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002210 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002211
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002212 xfrm_mark_get(attrs, &mark);
2213
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002214 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002215 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002216 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002217
2218 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002219 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002220 if (!xp)
2221 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002222
2223 memcpy(&x->id, &ua->id, sizeof(ua->id));
2224 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2225 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002226 xp->mark.m = x->mark.m = mark.m;
2227 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002228 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002229 /* extract the templates and for each call km_key */
2230 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2231 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2232 memcpy(&x->id, &t->id, sizeof(x->id));
2233 x->props.mode = t->mode;
2234 x->props.reqid = t->reqid;
2235 x->props.family = ut->family;
2236 t->aalgos = ua->aalgos;
2237 t->ealgos = ua->ealgos;
2238 t->calgos = ua->calgos;
2239 err = km_query(x, t, xp);
2240
2241 }
2242
2243 kfree(x);
2244 kfree(xp);
2245
2246 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002247
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002248free_state:
2249 kfree(x);
2250nomem:
2251 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002252}
2253
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002254#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002255static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002256 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002257 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002258{
Thomas Graf5424f322007-08-22 14:01:33 -07002259 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002260 struct xfrm_user_migrate *um;
2261 int i, num_migrate;
2262
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002263 if (k != NULL) {
2264 struct xfrm_user_kmaddress *uk;
2265
2266 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2267 memcpy(&k->local, &uk->local, sizeof(k->local));
2268 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2269 k->family = uk->family;
2270 k->reserved = uk->reserved;
2271 }
2272
Thomas Graf5424f322007-08-22 14:01:33 -07002273 um = nla_data(rt);
2274 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002275
2276 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2277 return -EINVAL;
2278
2279 for (i = 0; i < num_migrate; i++, um++, ma++) {
2280 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2281 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2282 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2283 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2284
2285 ma->proto = um->proto;
2286 ma->mode = um->mode;
2287 ma->reqid = um->reqid;
2288
2289 ma->old_family = um->old_family;
2290 ma->new_family = um->new_family;
2291 }
2292
2293 *num = i;
2294 return 0;
2295}
2296
2297static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002298 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002299{
Thomas Graf7b67c852007-08-22 13:53:52 -07002300 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002301 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002302 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002303 u8 type;
2304 int err;
2305 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002306 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002307
Thomas Graf35a7aa02007-08-22 14:00:40 -07002308 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002309 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002310
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002311 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2312
Thomas Graf5424f322007-08-22 14:01:33 -07002313 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002314 if (err)
2315 return err;
2316
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002317 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002318 if (err)
2319 return err;
2320
2321 if (!n)
2322 return 0;
2323
Fan Du8d549c42013-11-07 17:47:49 +08002324 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002325
2326 return 0;
2327}
2328#else
2329static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002330 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002331{
2332 return -ENOPROTOOPT;
2333}
2334#endif
2335
2336#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002337static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002338{
2339 struct xfrm_user_migrate um;
2340
2341 memset(&um, 0, sizeof(um));
2342 um.proto = m->proto;
2343 um.mode = m->mode;
2344 um.reqid = m->reqid;
2345 um.old_family = m->old_family;
2346 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2347 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2348 um.new_family = m->new_family;
2349 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2350 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2351
Thomas Grafc0144be2007-08-22 13:55:43 -07002352 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002353}
2354
David S. Miller183cad12011-02-24 00:28:01 -05002355static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002356{
2357 struct xfrm_user_kmaddress uk;
2358
2359 memset(&uk, 0, sizeof(uk));
2360 uk.family = k->family;
2361 uk.reserved = k->reserved;
2362 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002363 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002364
2365 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2366}
2367
2368static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002369{
2370 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002371 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2372 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2373 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002374}
2375
David S. Miller183cad12011-02-24 00:28:01 -05002376static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2377 int num_migrate, const struct xfrm_kmaddress *k,
2378 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002379{
David S. Miller183cad12011-02-24 00:28:01 -05002380 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002381 struct xfrm_userpolicy_id *pol_id;
2382 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002383 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002384
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002385 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2386 if (nlh == NULL)
2387 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002388
Thomas Graf7b67c852007-08-22 13:53:52 -07002389 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002390 /* copy data from selector, dir, and type to the pol_id */
2391 memset(pol_id, 0, sizeof(*pol_id));
2392 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2393 pol_id->dir = dir;
2394
David S. Miller1d1e34d2012-06-27 21:57:03 -07002395 if (k != NULL) {
2396 err = copy_to_user_kmaddress(k, skb);
2397 if (err)
2398 goto out_cancel;
2399 }
2400 err = copy_to_user_policy_type(type, skb);
2401 if (err)
2402 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002403 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002404 err = copy_to_user_migrate(mp, skb);
2405 if (err)
2406 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002407 }
2408
Johannes Berg053c0952015-01-16 22:09:00 +01002409 nlmsg_end(skb, nlh);
2410 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002411
2412out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002413 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002414 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002415}
2416
David S. Miller183cad12011-02-24 00:28:01 -05002417static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2418 const struct xfrm_migrate *m, int num_migrate,
2419 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002420{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002421 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002422 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002423
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002424 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002425 if (skb == NULL)
2426 return -ENOMEM;
2427
2428 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002429 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002430 BUG();
2431
Michal Kubecek21ee5432014-06-03 10:26:06 +02002432 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002433}
2434#else
David S. Miller183cad12011-02-24 00:28:01 -05002435static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2436 const struct xfrm_migrate *m, int num_migrate,
2437 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002438{
2439 return -ENOPROTOOPT;
2440}
2441#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002442
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002443#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002444
2445static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002446 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002447 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2448 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2449 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2450 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2451 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2452 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002453 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002454 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002455 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002456 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002457 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002458 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002459 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002460 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2461 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002462 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002463 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002464 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002465 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002466 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467};
2468
Thomas Graf492b5582005-05-03 14:26:40 -07002469#undef XMSGSIZE
2470
Thomas Grafcf5cb792007-08-22 13:59:04 -07002471static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002472 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2473 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2474 [XFRMA_LASTUSED] = { .type = NLA_U64},
2475 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002476 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002477 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2478 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2479 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2480 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2481 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2482 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2483 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2484 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2485 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2486 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2487 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2488 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2489 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2490 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002491 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002492 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002493 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002494 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002495 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002496 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002497 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Steffen Klassert78f10c52018-06-12 12:44:26 +02002498 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2499 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
Steffen Klasserta80f5602018-06-12 14:07:07 +02002500 [XFRMA_IF_ID] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002501};
2502
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002503static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2504 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2505 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2506};
2507
Mathias Krause05600a72013-02-24 14:10:27 +01002508static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002509 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Herbert Xu543aabb2017-10-19 20:51:10 +08002510 int (*start)(struct netlink_callback *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002512 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002513 const struct nla_policy *nla_pol;
2514 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002515} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2516 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2517 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2518 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002519 .dump = xfrm_dump_sa,
2520 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002521 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2522 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2523 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Herbert Xu543aabb2017-10-19 20:51:10 +08002524 .start = xfrm_dump_policy_start,
Timo Teras4c563f72008-02-28 21:31:08 -08002525 .dump = xfrm_dump_policy,
2526 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002527 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002528 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002529 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002530 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2531 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002532 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002533 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2534 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002535 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2536 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002537 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002538 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002539 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2540 .nla_pol = xfrma_spd_policy,
2541 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002542 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543};
2544
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002545static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002547 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002548 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002549 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002550 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Fan Du74005992015-01-27 17:00:29 +08002552#ifdef CONFIG_COMPAT
Andy Lutomirski2bf8c472016-03-22 14:25:10 -07002553 if (in_compat_syscall())
Yi Zhao83e2d052016-11-29 18:09:01 +08002554 return -EOPNOTSUPP;
Fan Du74005992015-01-27 17:00:29 +08002555#endif
2556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002559 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 type -= XFRM_MSG_BASE;
2562 link = &xfrm_dispatch[type];
2563
2564 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002565 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002566 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Thomas Graf492b5582005-05-03 14:26:40 -07002568 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2569 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002570 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002574 {
2575 struct netlink_dump_control c = {
Herbert Xu543aabb2017-10-19 20:51:10 +08002576 .start = link->start,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002577 .dump = link->dump,
2578 .done = link->done,
2579 };
2580 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002584 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2585 link->nla_max ? : XFRMA_MAX,
2586 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002587 if (err < 0)
2588 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Thomas Graf5424f322007-08-22 14:01:33 -07002593 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002596static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Fan Du283bc9f2013-11-07 17:47:50 +08002598 struct net *net = sock_net(skb->sk);
2599
2600 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002601 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002602 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
Thomas Graf7deb2262007-08-22 13:57:39 -07002605static inline size_t xfrm_expire_msgsize(void)
2606{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002607 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2608 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002609}
2610
David S. Miller214e0052011-02-24 00:02:38 -05002611static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 struct xfrm_user_expire *ue;
2614 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002615 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Eric W. Biederman15e47302012-09-07 20:12:54 +00002617 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002618 if (nlh == NULL)
2619 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Thomas Graf7b67c852007-08-22 13:53:52 -07002621 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002623 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
David S. Miller1d1e34d2012-06-27 21:57:03 -07002625 err = xfrm_mark_put(skb, &x->mark);
2626 if (err)
2627 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002628
Steffen Klasserta80f5602018-06-12 14:07:07 +02002629 err = xfrm_if_id_put(skb, x->if_id);
2630 if (err)
2631 return err;
2632
Johannes Berg053c0952015-01-16 22:09:00 +01002633 nlmsg_end(skb, nlh);
2634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
David S. Miller214e0052011-02-24 00:02:38 -05002637static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002639 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 struct sk_buff *skb;
2641
Thomas Graf7deb2262007-08-22 13:57:39 -07002642 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (skb == NULL)
2644 return -ENOMEM;
2645
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002646 if (build_expire(skb, x, c) < 0) {
2647 kfree_skb(skb);
2648 return -EMSGSIZE;
2649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Michal Kubecek21ee5432014-06-03 10:26:06 +02002651 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
David S. Miller214e0052011-02-24 00:02:38 -05002654static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002655{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002656 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002657 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002658
Steffen Klassertd8647b72011-03-08 00:10:27 +00002659 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002660 if (skb == NULL)
2661 return -ENOMEM;
2662
2663 if (build_aevent(skb, x, c) < 0)
2664 BUG();
2665
Michal Kubecek21ee5432014-06-03 10:26:06 +02002666 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002667}
2668
David S. Miller214e0052011-02-24 00:02:38 -05002669static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002670{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002671 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002672 struct xfrm_usersa_flush *p;
2673 struct nlmsghdr *nlh;
2674 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002675 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676
Thomas Graf7deb2262007-08-22 13:57:39 -07002677 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002678 if (skb == NULL)
2679 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002680
Eric W. Biederman15e47302012-09-07 20:12:54 +00002681 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002682 if (nlh == NULL) {
2683 kfree_skb(skb);
2684 return -EMSGSIZE;
2685 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002686
Thomas Graf7b67c852007-08-22 13:53:52 -07002687 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002688 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002689
Thomas Graf98250692007-08-22 12:47:26 -07002690 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002691
Michal Kubecek21ee5432014-06-03 10:26:06 +02002692 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002693}
2694
Thomas Graf7deb2262007-08-22 13:57:39 -07002695static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002696{
Thomas Graf7deb2262007-08-22 13:57:39 -07002697 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002698 if (x->aead)
2699 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002700 if (x->aalg) {
2701 l += nla_total_size(sizeof(struct xfrm_algo) +
2702 (x->aalg->alg_key_len + 7) / 8);
2703 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2704 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002705 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002706 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002707 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002708 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002709 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002710 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002711 if (x->tfcpad)
2712 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002713 if (x->replay_esn)
2714 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002715 else
2716 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002717 if (x->security)
2718 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2719 x->security->ctx_len);
2720 if (x->coaddr)
2721 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002722 if (x->props.extra_flags)
2723 l += nla_total_size(sizeof(x->props.extra_flags));
Steffen Klassert78f10c52018-06-12 12:44:26 +02002724 if (x->props.smark.v | x->props.smark.m) {
2725 l += nla_total_size(sizeof(x->props.smark.v));
2726 l += nla_total_size(sizeof(x->props.smark.m));
2727 }
Steffen Klasserta80f5602018-06-12 14:07:07 +02002728 if (x->if_id)
2729 l += nla_total_size(sizeof(x->if_id));
Herbert Xu68325d32007-10-09 13:30:57 -07002730
Herbert Xud26f3982007-11-13 21:47:08 -08002731 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002732 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002733
2734 return l;
2735}
2736
David S. Miller214e0052011-02-24 00:02:38 -05002737static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002738{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002739 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002740 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002741 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002742 struct nlmsghdr *nlh;
2743 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002744 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002745 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002746
2747 headlen = sizeof(*p);
2748 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002749 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002750 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002751 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002752 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002753 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002754
Thomas Graf7deb2262007-08-22 13:57:39 -07002755 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002756 if (skb == NULL)
2757 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002758
Eric W. Biederman15e47302012-09-07 20:12:54 +00002759 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002760 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002761 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002762 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002763
Thomas Graf7b67c852007-08-22 13:53:52 -07002764 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002765 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002766 struct nlattr *attr;
2767
Thomas Graf7b67c852007-08-22 13:53:52 -07002768 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002769 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2770 id->spi = x->id.spi;
2771 id->family = x->props.family;
2772 id->proto = x->id.proto;
2773
Thomas Grafc0144be2007-08-22 13:55:43 -07002774 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002775 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002776 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002777 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002778
2779 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002780 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002781 err = copy_to_user_state_extra(x, p, skb);
2782 if (err)
2783 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002784
Thomas Graf98250692007-08-22 12:47:26 -07002785 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002786
Michal Kubecek21ee5432014-06-03 10:26:06 +02002787 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002788
David S. Miller1d1e34d2012-06-27 21:57:03 -07002789out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002790 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002791 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002792}
2793
David S. Miller214e0052011-02-24 00:02:38 -05002794static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002795{
2796
2797 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002798 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002799 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002800 case XFRM_MSG_NEWAE:
2801 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002802 case XFRM_MSG_DELSA:
2803 case XFRM_MSG_UPDSA:
2804 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002805 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002806 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002807 return xfrm_notify_sa_flush(c);
2808 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002809 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2810 c->event);
2811 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002812 }
2813
2814 return 0;
2815
2816}
2817
Thomas Graf7deb2262007-08-22 13:57:39 -07002818static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2819 struct xfrm_policy *xp)
2820{
2821 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2822 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002823 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002824 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2825 + userpolicy_type_attrsize();
2826}
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002829 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002831 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct xfrm_user_acquire *ua;
2833 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002834 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002836 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2837 if (nlh == NULL)
2838 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Thomas Graf7b67c852007-08-22 13:53:52 -07002840 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 memcpy(&ua->id, &x->id, sizeof(ua->id));
2842 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2843 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002844 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 ua->aalgos = xt->aalgos;
2846 ua->ealgos = xt->ealgos;
2847 ua->calgos = xt->calgos;
2848 ua->seq = x->km.seq = seq;
2849
David S. Miller1d1e34d2012-06-27 21:57:03 -07002850 err = copy_to_user_tmpl(xp, skb);
2851 if (!err)
2852 err = copy_to_user_state_sec_ctx(x, skb);
2853 if (!err)
2854 err = copy_to_user_policy_type(xp->type, skb);
2855 if (!err)
2856 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klasserta80f5602018-06-12 14:07:07 +02002857 if (!err)
2858 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002859 if (err) {
2860 nlmsg_cancel(skb, nlh);
2861 return err;
2862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Johannes Berg053c0952015-01-16 22:09:00 +01002864 nlmsg_end(skb, nlh);
2865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
2867
2868static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002869 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002871 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Thomas Graf7deb2262007-08-22 13:57:39 -07002874 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (skb == NULL)
2876 return -ENOMEM;
2877
Fan Du65e07362012-08-15 10:13:47 +08002878 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 BUG();
2880
Michal Kubecek21ee5432014-06-03 10:26:06 +02002881 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
2884/* User gives us xfrm_user_policy_info followed by an array of 0
2885 * or more templates.
2886 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002887static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 u8 *data, int len, int *dir)
2889{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002890 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2892 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2893 struct xfrm_policy *xp;
2894 int nr;
2895
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002896 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 case AF_INET:
2898 if (opt != IP_XFRM_POLICY) {
2899 *dir = -EOPNOTSUPP;
2900 return NULL;
2901 }
2902 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002903#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 case AF_INET6:
2905 if (opt != IPV6_XFRM_POLICY) {
2906 *dir = -EOPNOTSUPP;
2907 return NULL;
2908 }
2909 break;
2910#endif
2911 default:
2912 *dir = -EINVAL;
2913 return NULL;
2914 }
2915
2916 *dir = -EINVAL;
2917
2918 if (len < sizeof(*p) ||
2919 verify_newpolicy_info(p))
2920 return NULL;
2921
2922 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002923 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return NULL;
2925
Herbert Xua4f1bac2005-07-26 15:43:17 -07002926 if (p->dir > XFRM_POLICY_OUT)
2927 return NULL;
2928
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002929 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (xp == NULL) {
2931 *dir = -ENOBUFS;
2932 return NULL;
2933 }
2934
2935 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002936 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 copy_templates(xp, ut, nr);
2938
2939 *dir = p->dir;
2940
2941 return xp;
2942}
2943
Thomas Graf7deb2262007-08-22 13:57:39 -07002944static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2945{
2946 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2947 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2948 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002949 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002950 + userpolicy_type_attrsize();
2951}
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002954 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
2956 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002957 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002958 struct nlmsghdr *nlh;
2959 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Eric W. Biederman15e47302012-09-07 20:12:54 +00002961 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002962 if (nlh == NULL)
2963 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Thomas Graf7b67c852007-08-22 13:53:52 -07002965 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002967 err = copy_to_user_tmpl(xp, skb);
2968 if (!err)
2969 err = copy_to_user_sec_ctx(xp, skb);
2970 if (!err)
2971 err = copy_to_user_policy_type(xp->type, skb);
2972 if (!err)
2973 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klasserta80f5602018-06-12 14:07:07 +02002974 if (!err)
2975 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002976 if (err) {
2977 nlmsg_cancel(skb, nlh);
2978 return err;
2979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 upe->hard = !!hard;
2981
Johannes Berg053c0952015-01-16 22:09:00 +01002982 nlmsg_end(skb, nlh);
2983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
David S. Miller214e0052011-02-24 00:02:38 -05002986static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002988 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Thomas Graf7deb2262007-08-22 13:57:39 -07002991 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 if (skb == NULL)
2993 return -ENOMEM;
2994
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002995 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 BUG();
2997
Michal Kubecek21ee5432014-06-03 10:26:06 +02002998 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
David S. Miller214e0052011-02-24 00:02:38 -05003001static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003002{
David S. Miller1d1e34d2012-06-27 21:57:03 -07003003 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003004 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003005 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07003006 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003007 struct nlmsghdr *nlh;
3008 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003009 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07003010
3011 headlen = sizeof(*p);
3012 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07003013 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07003014 headlen = sizeof(*id);
3015 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07003016 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003017 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07003018 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003019
Thomas Graf7deb2262007-08-22 13:57:39 -07003020 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003021 if (skb == NULL)
3022 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003023
Eric W. Biederman15e47302012-09-07 20:12:54 +00003024 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003025 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003026 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003027 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003028
Thomas Graf7b67c852007-08-22 13:53:52 -07003029 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07003030 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07003031 struct nlattr *attr;
3032
Thomas Graf7b67c852007-08-22 13:53:52 -07003033 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07003034 memset(id, 0, sizeof(*id));
3035 id->dir = dir;
3036 if (c->data.byid)
3037 id->index = xp->index;
3038 else
3039 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3040
Thomas Grafc0144be2007-08-22 13:55:43 -07003041 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07003042 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07003043 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003044 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07003045
3046 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07003047 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003048
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003049 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003050 err = copy_to_user_tmpl(xp, skb);
3051 if (!err)
3052 err = copy_to_user_policy_type(xp->type, skb);
3053 if (!err)
3054 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klasserta80f5602018-06-12 14:07:07 +02003055 if (!err)
3056 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003057 if (err)
3058 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003059
Thomas Graf98250692007-08-22 12:47:26 -07003060 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003061
Michal Kubecek21ee5432014-06-03 10:26:06 +02003062 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003063
David S. Miller1d1e34d2012-06-27 21:57:03 -07003064out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003065 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003066 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003067}
3068
David S. Miller214e0052011-02-24 00:02:38 -05003069static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003070{
Alexey Dobriyan70678022008-11-25 17:50:36 -08003071 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003072 struct nlmsghdr *nlh;
3073 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003074 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003075
Thomas Graf7deb2262007-08-22 13:57:39 -07003076 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003077 if (skb == NULL)
3078 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003079
Eric W. Biederman15e47302012-09-07 20:12:54 +00003080 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003081 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003082 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003083 goto out_free_skb;
3084 err = copy_to_user_policy_type(c->data.type, skb);
3085 if (err)
3086 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003087
Thomas Graf98250692007-08-22 12:47:26 -07003088 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003089
Michal Kubecek21ee5432014-06-03 10:26:06 +02003090 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003091
David S. Miller1d1e34d2012-06-27 21:57:03 -07003092out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003093 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003094 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003095}
3096
David S. Miller214e0052011-02-24 00:02:38 -05003097static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003098{
3099
3100 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003101 case XFRM_MSG_NEWPOLICY:
3102 case XFRM_MSG_UPDPOLICY:
3103 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003104 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003105 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003106 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003107 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003108 return xfrm_exp_policy_notify(xp, dir, c);
3109 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003110 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3111 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003112 }
3113
3114 return 0;
3115
3116}
3117
Thomas Graf7deb2262007-08-22 13:57:39 -07003118static inline size_t xfrm_report_msgsize(void)
3119{
3120 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3121}
3122
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003123static int build_report(struct sk_buff *skb, u8 proto,
3124 struct xfrm_selector *sel, xfrm_address_t *addr)
3125{
3126 struct xfrm_user_report *ur;
3127 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003128
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003129 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3130 if (nlh == NULL)
3131 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003132
Thomas Graf7b67c852007-08-22 13:53:52 -07003133 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003134 ur->proto = proto;
3135 memcpy(&ur->sel, sel, sizeof(ur->sel));
3136
David S. Miller1d1e34d2012-06-27 21:57:03 -07003137 if (addr) {
3138 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3139 if (err) {
3140 nlmsg_cancel(skb, nlh);
3141 return err;
3142 }
3143 }
Johannes Berg053c0952015-01-16 22:09:00 +01003144 nlmsg_end(skb, nlh);
3145 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003146}
3147
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003148static int xfrm_send_report(struct net *net, u8 proto,
3149 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003150{
3151 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003152
Thomas Graf7deb2262007-08-22 13:57:39 -07003153 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003154 if (skb == NULL)
3155 return -ENOMEM;
3156
3157 if (build_report(skb, proto, sel, addr) < 0)
3158 BUG();
3159
Michal Kubecek21ee5432014-06-03 10:26:06 +02003160 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003161}
3162
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003163static inline size_t xfrm_mapping_msgsize(void)
3164{
3165 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3166}
3167
3168static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3169 xfrm_address_t *new_saddr, __be16 new_sport)
3170{
3171 struct xfrm_user_mapping *um;
3172 struct nlmsghdr *nlh;
3173
3174 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3175 if (nlh == NULL)
3176 return -EMSGSIZE;
3177
3178 um = nlmsg_data(nlh);
3179
3180 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3181 um->id.spi = x->id.spi;
3182 um->id.family = x->props.family;
3183 um->id.proto = x->id.proto;
3184 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3185 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3186 um->new_sport = new_sport;
3187 um->old_sport = x->encap->encap_sport;
3188 um->reqid = x->props.reqid;
3189
Johannes Berg053c0952015-01-16 22:09:00 +01003190 nlmsg_end(skb, nlh);
3191 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003192}
3193
3194static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3195 __be16 sport)
3196{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003197 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003198 struct sk_buff *skb;
3199
3200 if (x->id.proto != IPPROTO_ESP)
3201 return -EINVAL;
3202
3203 if (!x->encap)
3204 return -EINVAL;
3205
3206 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3207 if (skb == NULL)
3208 return -ENOMEM;
3209
3210 if (build_mapping(skb, x, ipaddr, sport) < 0)
3211 BUG();
3212
Michal Kubecek21ee5432014-06-03 10:26:06 +02003213 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003214}
3215
Horia Geanta0f245582014-02-12 16:20:06 +02003216static bool xfrm_is_alive(const struct km_event *c)
3217{
3218 return (bool)xfrm_acquire_is_on(c->net);
3219}
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221static struct xfrm_mgr netlink_mgr = {
3222 .id = "netlink",
3223 .notify = xfrm_send_state_notify,
3224 .acquire = xfrm_send_acquire,
3225 .compile_policy = xfrm_compile_policy,
3226 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003227 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003228 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003229 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003230 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231};
3232
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003233static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Patrick McHardybe336902006-03-20 22:40:54 -08003235 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003236 struct netlink_kernel_cfg cfg = {
3237 .groups = XFRMNLGRP_MAX,
3238 .input = xfrm_netlink_rcv,
3239 };
Patrick McHardybe336902006-03-20 22:40:54 -08003240
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003241 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003242 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003244 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003245 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return 0;
3247}
3248
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003249static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003250{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003251 struct net *net;
3252 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003253 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003254 synchronize_net();
3255 list_for_each_entry(net, net_exit_list, exit_list)
3256 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003257}
3258
3259static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003260 .init = xfrm_user_net_init,
3261 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003262};
3263
3264static int __init xfrm_user_init(void)
3265{
3266 int rv;
3267
3268 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3269
3270 rv = register_pernet_subsys(&xfrm_user_net_ops);
3271 if (rv < 0)
3272 return rv;
3273 rv = xfrm_register_km(&netlink_mgr);
3274 if (rv < 0)
3275 unregister_pernet_subsys(&xfrm_user_net_ops);
3276 return rv;
3277}
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279static void __exit xfrm_user_exit(void)
3280{
3281 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003282 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
3285module_init(xfrm_user_init);
3286module_exit(xfrm_user_exit);
3287MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003288MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003289