blob: 5ea9c1a83d3e9d8d4e6839722b4d11d5dd0bb7ce [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);
Xin Longfa40d122020-02-09 21:15:29 +0800112 if (uctx->len > nla_len(rt) ||
113 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800114 return -EINVAL;
115
116 return 0;
117}
118
Steffen Klassertd8647b72011-03-08 00:10:27 +0000119static inline int verify_replay(struct xfrm_usersa_info *p,
120 struct nlattr **attrs)
121{
122 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000123 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000124
125 if (!rt)
Florian Westphal0355a9f2018-02-12 14:42:01 +0100126 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
127
128 rs = nla_data(rt);
129
130 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
131 return -EINVAL;
132
133 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
134 nla_len(rt) != sizeof(*rs))
135 return -EINVAL;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000136
Fan Du01714102014-01-18 09:54:28 +0800137 /* As only ESP and AH support ESN feature. */
138 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000139 return -EINVAL;
140
Steffen Klassertd8647b72011-03-08 00:10:27 +0000141 if (p->replay_window != 0)
142 return -EINVAL;
143
144 return 0;
145}
Trent Jaegerdf718372005-12-13 23:12:27 -0800146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700148 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int err;
151
152 err = -EINVAL;
153 switch (p->family) {
154 case AF_INET:
Anirudh Gupta92a63c22019-05-21 20:59:47 +0530155 break;
156
157 case AF_INET6:
158#if IS_ENABLED(CONFIG_IPV6)
159 break;
160#else
161 err = -EAFNOSUPPORT;
162 goto out;
163#endif
164
165 default:
166 goto out;
167 }
168
169 switch (p->sel.family) {
Nicolas Dichtel2d0dbd02019-06-14 11:13:55 +0200170 case AF_UNSPEC:
171 break;
172
Anirudh Gupta92a63c22019-05-21 20:59:47 +0530173 case AF_INET:
Steffen Klassert89cb86e2018-08-01 13:45:11 +0200174 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
175 goto out;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
178
179 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000180#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert89cb86e2018-08-01 13:45:11 +0200181 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
182 goto out;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185#else
186 err = -EAFNOSUPPORT;
187 goto out;
188#endif
189
190 default:
191 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 err = -EINVAL;
195 switch (p->id.proto) {
196 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000197 if ((!attrs[XFRMA_ALG_AUTH] &&
198 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800199 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700200 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000201 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200202 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto out;
204 break;
205
206 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800207 if (attrs[XFRMA_ALG_COMP])
208 goto out;
209 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000210 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800211 !attrs[XFRMA_ALG_CRYPT] &&
212 !attrs[XFRMA_ALG_AEAD])
213 goto out;
214 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000215 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800216 attrs[XFRMA_ALG_CRYPT]) &&
217 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000219 if (attrs[XFRMA_TFCPAD] &&
220 p->mode != XFRM_MODE_TUNNEL)
221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
223
224 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700225 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800226 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700227 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000228 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000229 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200230 attrs[XFRMA_TFCPAD] ||
231 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto out;
233 break;
234
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000235#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700236 case IPPROTO_DSTOPTS:
237 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700238 if (attrs[XFRMA_ALG_COMP] ||
239 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000240 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800241 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 attrs[XFRMA_ALG_CRYPT] ||
243 attrs[XFRMA_ENCAP] ||
244 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000245 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700246 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700247 goto out;
248 break;
249#endif
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 default:
252 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Herbert Xu1a6509d2008-01-28 19:37:29 -0800255 if ((err = verify_aead(attrs)))
256 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000257 if ((err = verify_auth_trunc(attrs)))
258 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700259 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700261 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700263 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700265 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800266 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000267 if ((err = verify_replay(p, attrs)))
268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 err = -EINVAL;
271 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700272 case XFRM_MODE_TRANSPORT:
273 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700274 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700275 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277
278 default:
279 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 err = 0;
283
284out:
285 return err;
286}
287
288static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800289 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700290 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct xfrm_algo *p, *ualg;
293 struct xfrm_algo_desc *algo;
294
295 if (!rta)
296 return 0;
297
Thomas Graf5424f322007-08-22 14:01:33 -0700298 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 algo = get_byname(ualg->alg_name, 1);
301 if (!algo)
302 return -ENOSYS;
303 *props = algo->desc.sadb_alg_id;
304
Eric Dumazet0f99be02008-01-08 23:39:06 -0800305 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!p)
307 return -ENOMEM;
308
Herbert Xu04ff1262006-08-13 08:50:00 +1000309 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *algpp = p;
311 return 0;
312}
313
Herbert Xu69b01372015-05-27 16:03:45 +0800314static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
315{
316 struct xfrm_algo *p, *ualg;
317 struct xfrm_algo_desc *algo;
318
319 if (!rta)
320 return 0;
321
322 ualg = nla_data(rta);
323
324 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
325 if (!algo)
326 return -ENOSYS;
327 x->props.ealgo = algo->desc.sadb_alg_id;
328
329 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
330 if (!p)
331 return -ENOMEM;
332
333 strcpy(p->alg_name, algo->name);
334 x->ealg = p;
335 x->geniv = algo->uinfo.encr.geniv;
336 return 0;
337}
338
Martin Willi4447bb32009-11-25 00:29:52 +0000339static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
340 struct nlattr *rta)
341{
342 struct xfrm_algo *ualg;
343 struct xfrm_algo_auth *p;
344 struct xfrm_algo_desc *algo;
345
346 if (!rta)
347 return 0;
348
349 ualg = nla_data(rta);
350
351 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
352 if (!algo)
353 return -ENOSYS;
354 *props = algo->desc.sadb_alg_id;
355
356 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
357 if (!p)
358 return -ENOMEM;
359
360 strcpy(p->alg_name, algo->name);
361 p->alg_key_len = ualg->alg_key_len;
362 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
363 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
364
365 *algpp = p;
366 return 0;
367}
368
369static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
370 struct nlattr *rta)
371{
372 struct xfrm_algo_auth *p, *ualg;
373 struct xfrm_algo_desc *algo;
374
375 if (!rta)
376 return 0;
377
378 ualg = nla_data(rta);
379
380 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
381 if (!algo)
382 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800383 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000384 return -EINVAL;
385 *props = algo->desc.sadb_alg_id;
386
387 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
388 if (!p)
389 return -ENOMEM;
390
391 strcpy(p->alg_name, algo->name);
392 if (!p->alg_trunc_len)
393 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
394
395 *algpp = p;
396 return 0;
397}
398
Herbert Xu69b01372015-05-27 16:03:45 +0800399static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800400{
401 struct xfrm_algo_aead *p, *ualg;
402 struct xfrm_algo_desc *algo;
403
404 if (!rta)
405 return 0;
406
407 ualg = nla_data(rta);
408
409 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
410 if (!algo)
411 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800412 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800413
414 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
415 if (!p)
416 return -ENOMEM;
417
418 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800419 x->aead = p;
420 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800421 return 0;
422}
423
Steffen Klasserte2b19122011-03-28 19:47:30 +0000424static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
425 struct nlattr *rp)
426{
427 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000428 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000429
430 if (!replay_esn || !rp)
431 return 0;
432
433 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000434 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000435
Andy Whitcroft79191ea2017-03-23 07:45:44 +0000436 /* Check the overall length and the internal bitmap length to avoid
437 * potential overflow. */
438 if (nla_len(rp) < ulen ||
439 xfrm_replay_state_esn_len(replay_esn) != ulen ||
440 replay_esn->bmp_len != up->bmp_len)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000441 return -EINVAL;
442
Andy Whitcroft64a54652017-03-22 07:29:31 +0000443 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
444 return -EINVAL;
445
Steffen Klasserte2b19122011-03-28 19:47:30 +0000446 return 0;
447}
448
Steffen Klassertd8647b72011-03-08 00:10:27 +0000449static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
450 struct xfrm_replay_state_esn **preplay_esn,
451 struct nlattr *rta)
452{
453 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000454 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000455
456 if (!rta)
457 return 0;
458
459 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000460 klen = xfrm_replay_state_esn_len(up);
461 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000462
Mathias Krauseecd79182012-09-20 10:01:49 +0000463 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000464 if (!p)
465 return -ENOMEM;
466
Mathias Krauseecd79182012-09-20 10:01:49 +0000467 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000468 if (!pp) {
469 kfree(p);
470 return -ENOMEM;
471 }
472
Mathias Krauseecd79182012-09-20 10:01:49 +0000473 memcpy(p, up, ulen);
474 memcpy(pp, up, ulen);
475
Steffen Klassertd8647b72011-03-08 00:10:27 +0000476 *replay_esn = p;
477 *preplay_esn = pp;
478
479 return 0;
480}
481
Joy Latten661697f2007-04-13 16:14:35 -0700482static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800483{
Trent Jaegerdf718372005-12-13 23:12:27 -0800484 int len = 0;
485
486 if (xfrm_ctx) {
487 len += sizeof(struct xfrm_user_sec_ctx);
488 len += xfrm_ctx->ctx_len;
489 }
490 return len;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
494{
495 memcpy(&x->id, &p->id, sizeof(x->id));
496 memcpy(&x->sel, &p->sel, sizeof(x->sel));
497 memcpy(&x->lft, &p->lft, sizeof(x->lft));
498 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800499 x->props.replay_window = min_t(unsigned int, p->replay_window,
500 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 x->props.reqid = p->reqid;
502 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700503 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700505
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700506 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700507 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800510/*
511 * someday when pfkey also has support, we could have the code
512 * somehow made shareable and move it to xfrm_state.c - JHS
513 *
514*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000515static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
516 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800517{
Thomas Graf5424f322007-08-22 14:01:33 -0700518 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000519 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700520 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
521 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
522 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800523
Steffen Klassertd8647b72011-03-08 00:10:27 +0000524 if (re) {
525 struct xfrm_replay_state_esn *replay_esn;
526 replay_esn = nla_data(re);
527 memcpy(x->replay_esn, replay_esn,
528 xfrm_replay_state_esn_len(replay_esn));
529 memcpy(x->preplay_esn, replay_esn,
530 xfrm_replay_state_esn_len(replay_esn));
531 }
532
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800533 if (rp) {
534 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700535 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800536 memcpy(&x->replay, replay, sizeof(*replay));
537 memcpy(&x->preplay, replay, sizeof(*replay));
538 }
539
540 if (lt) {
541 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700542 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800543 x->curlft.bytes = ltime->bytes;
544 x->curlft.packets = ltime->packets;
545 x->curlft.add_time = ltime->add_time;
546 x->curlft.use_time = ltime->use_time;
547 }
548
Thomas Grafcf5cb792007-08-22 13:59:04 -0700549 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700550 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800551
Thomas Grafcf5cb792007-08-22 13:59:04 -0700552 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700553 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800554}
555
Steffen Klassert78f10c52018-06-12 12:44:26 +0200556static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
557{
558 if (attrs[XFRMA_SET_MARK]) {
559 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
560 if (attrs[XFRMA_SET_MARK_MASK])
561 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
562 else
563 m->m = 0xffffffff;
564 } else {
565 m->v = m->m = 0;
566 }
567}
568
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800569static struct xfrm_state *xfrm_state_construct(struct net *net,
570 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700571 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int *errp)
573{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800574 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int err = -ENOMEM;
576
577 if (!x)
578 goto error_no_put;
579
580 copy_from_user_state(x, p);
581
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100582 if (attrs[XFRMA_SA_EXTRA_FLAGS])
583 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
584
Herbert Xu69b01372015-05-27 16:03:45 +0800585 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800586 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000587 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
588 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000590 if (!x->props.aalgo) {
591 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
592 attrs[XFRMA_ALG_AUTH])))
593 goto error;
594 }
Herbert Xu69b01372015-05-27 16:03:45 +0800595 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto error;
597 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
598 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700599 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700601
602 if (attrs[XFRMA_ENCAP]) {
603 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
604 sizeof(*x->encap), GFP_KERNEL);
605 if (x->encap == NULL)
606 goto error;
607 }
608
Martin Willi35d28562010-12-08 04:37:49 +0000609 if (attrs[XFRMA_TFCPAD])
610 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
611
Thomas Graffd211502007-09-06 03:28:08 -0700612 if (attrs[XFRMA_COADDR]) {
613 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
614 sizeof(*x->coaddr), GFP_KERNEL);
615 if (x->coaddr == NULL)
616 goto error;
617 }
618
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000619 xfrm_mark_get(attrs, &x->mark);
620
Steffen Klassert78f10c52018-06-12 12:44:26 +0200621 xfrm_smark_init(attrs, &x->props.smark);
Lorenzo Colitti34e23de2017-08-11 02:11:33 +0900622
Steffen Klasserta80f5602018-06-12 14:07:07 +0200623 if (attrs[XFRMA_IF_ID])
624 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
Lorenzo Colittibf25c792017-08-11 02:11:33 +0900625
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700626 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (err)
628 goto error;
629
Mathias Krause2f30ea52016-09-08 18:09:57 +0200630 if (attrs[XFRMA_SEC_CTX]) {
631 err = security_xfrm_state_alloc(x,
632 nla_data(attrs[XFRMA_SEC_CTX]));
633 if (err)
634 goto error;
635 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800636
Steffen Klassertd8647b72011-03-08 00:10:27 +0000637 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
638 attrs[XFRMA_REPLAY_ESN_VAL])))
639 goto error;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800642 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800643 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800644 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800645
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000646 if ((err = xfrm_init_replay(x)))
647 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800648
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000649 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000650 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 return x;
653
654error:
655 x->km.state = XFRM_STATE_DEAD;
656 xfrm_state_put(x);
657error_no_put:
658 *errp = err;
659 return NULL;
660}
661
Christoph Hellwig22e70052007-01-02 15:22:30 -0800662static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700663 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800665 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700666 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct xfrm_state *x;
668 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700669 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Thomas Graf35a7aa02007-08-22 14:00:40 -0700671 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (err)
673 return err;
674
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800675 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!x)
677 return err;
678
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700679 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
681 err = xfrm_state_add(x);
682 else
683 err = xfrm_state_update(x);
684
Tetsuo Handa2e710292014-04-22 21:48:30 +0900685 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (err < 0) {
688 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800689 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700690 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700693 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000694 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700695 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700696
697 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700698out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700699 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return err;
701}
702
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800703static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
704 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700705 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700706 int *errp)
707{
708 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000709 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700710 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000711 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700712
713 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
714 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000715 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700716 } else {
717 xfrm_address_t *saddr = NULL;
718
Thomas Graf35a7aa02007-08-22 14:00:40 -0700719 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700720 if (!saddr) {
721 err = -EINVAL;
722 goto out;
723 }
724
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800725 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000726 x = xfrm_state_lookup_byaddr(net, mark,
727 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800728 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700729 }
730
731 out:
732 if (!x && errp)
733 *errp = err;
734 return x;
735}
736
Christoph Hellwig22e70052007-01-02 15:22:30 -0800737static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700738 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800740 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700742 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700743 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700744 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800746 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700748 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
David S. Miller6f68dc32006-06-08 23:58:52 -0700750 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700751 goto out;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700754 err = -EPERM;
755 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700758 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600759
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700760 if (err < 0)
761 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700762
763 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000764 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700765 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700766 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700768out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900769 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700770 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
775{
Mathias Krausef778a632012-09-19 11:33:39 +0000776 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 memcpy(&p->id, &x->id, sizeof(p->id));
778 memcpy(&p->sel, &x->sel, sizeof(p->sel));
779 memcpy(&p->lft, &x->lft, sizeof(p->lft));
780 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400781 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
782 put_unaligned(x->stats.replay, &p->stats.replay);
783 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700784 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 p->mode = x->props.mode;
786 p->replay_window = x->props.replay_window;
787 p->reqid = x->props.reqid;
788 p->family = x->props.family;
789 p->flags = x->props.flags;
790 p->seq = x->km.seq;
791}
792
793struct xfrm_dump_info {
794 struct sk_buff *in_skb;
795 struct sk_buff *out_skb;
796 u32 nlmsg_seq;
797 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798};
799
Thomas Grafc0144be2007-08-22 13:55:43 -0700800static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
801{
Thomas Grafc0144be2007-08-22 13:55:43 -0700802 struct xfrm_user_sec_ctx *uctx;
803 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700804 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700805
806 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
807 if (attr == NULL)
808 return -EMSGSIZE;
809
810 uctx = nla_data(attr);
811 uctx->exttype = XFRMA_SEC_CTX;
812 uctx->len = ctx_size;
813 uctx->ctx_doi = s->ctx_doi;
814 uctx->ctx_alg = s->ctx_alg;
815 uctx->ctx_len = s->ctx_len;
816 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
817
818 return 0;
819}
820
Martin Willi4447bb32009-11-25 00:29:52 +0000821static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
822{
823 struct xfrm_algo *algo;
824 struct nlattr *nla;
825
826 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
827 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
828 if (!nla)
829 return -EMSGSIZE;
830
831 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000832 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000833 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
834 algo->alg_key_len = auth->alg_key_len;
835
836 return 0;
837}
838
Steffen Klassert78f10c52018-06-12 12:44:26 +0200839static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
840{
841 int ret = 0;
842
843 if (m->v | m->m) {
844 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
845 if (!ret)
846 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
847 }
848 return ret;
849}
850
Herbert Xu68325d32007-10-09 13:30:57 -0700851/* Don't change this without updating xfrm_sa_len! */
852static int copy_to_user_state_extra(struct xfrm_state *x,
853 struct xfrm_usersa_info *p,
854 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700856 int ret = 0;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 copy_to_user_state(x, p);
859
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100860 if (x->props.extra_flags) {
861 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
862 x->props.extra_flags);
863 if (ret)
864 goto out;
865 }
866
David S. Miller1d1e34d2012-06-27 21:57:03 -0700867 if (x->coaddr) {
868 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
869 if (ret)
870 goto out;
871 }
872 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200873 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
874 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700875 if (ret)
876 goto out;
877 }
878 if (x->aead) {
879 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
880 if (ret)
881 goto out;
882 }
883 if (x->aalg) {
884 ret = copy_to_user_auth(x->aalg, skb);
885 if (!ret)
886 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
887 xfrm_alg_auth_len(x->aalg), x->aalg);
888 if (ret)
889 goto out;
890 }
891 if (x->ealg) {
892 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
893 if (ret)
894 goto out;
895 }
896 if (x->calg) {
897 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
898 if (ret)
899 goto out;
900 }
901 if (x->encap) {
902 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
903 if (ret)
904 goto out;
905 }
906 if (x->tfcpad) {
907 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
908 if (ret)
909 goto out;
910 }
911 ret = xfrm_mark_put(skb, &x->mark);
912 if (ret)
913 goto out;
Steffen Klassert78f10c52018-06-12 12:44:26 +0200914
915 ret = xfrm_smark_put(skb, &x->props.smark);
916 if (ret)
917 goto out;
918
dingzhif293a5e2014-10-30 09:39:36 +0100919 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700920 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
921 xfrm_replay_state_esn_len(x->replay_esn),
922 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100923 else
924 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
925 &x->replay);
926 if (ret)
927 goto out;
Steffen Klasserta80f5602018-06-12 14:07:07 +0200928
929 if (x->if_id) {
930 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
931 if (ret)
932 goto out;
933 }
934
David S. Miller1d1e34d2012-06-27 21:57:03 -0700935 if (x->security)
936 ret = copy_sec_ctx(x->security, skb);
Lorenzo Colittibf25c792017-08-11 02:11:33 +0900937 if (x->props.output_mark) {
938 ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
939 if (ret)
940 goto out;
941 }
jianzhouf41ab602019-01-03 16:41:57 +0800942
David S. Miller1d1e34d2012-06-27 21:57:03 -0700943out:
944 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700945}
946
947static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
948{
949 struct xfrm_dump_info *sp = ptr;
950 struct sk_buff *in_skb = sp->in_skb;
951 struct sk_buff *skb = sp->out_skb;
952 struct xfrm_usersa_info *p;
953 struct nlmsghdr *nlh;
954 int err;
955
Eric W. Biederman15e47302012-09-07 20:12:54 +0000956 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700957 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
958 if (nlh == NULL)
959 return -EMSGSIZE;
960
961 p = nlmsg_data(nlh);
962
963 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700964 if (err) {
965 nlmsg_cancel(skb, nlh);
966 return err;
967 }
Thomas Graf98250692007-08-22 12:47:26 -0700968 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Timo Teras4c563f72008-02-28 21:31:08 -0800972static int xfrm_dump_sa_done(struct netlink_callback *cb)
973{
974 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800975 struct sock *sk = cb->skb->sk;
976 struct net *net = sock_net(sk);
977
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200978 if (cb->args[0])
979 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800980 return 0;
981}
982
Nicolas Dichteld3623092014-02-14 15:30:36 +0100983static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
985{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800986 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800987 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 struct xfrm_dump_info info;
989
Timo Teras4c563f72008-02-28 21:31:08 -0800990 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
991 sizeof(cb->args) - sizeof(cb->args[0]));
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 info.in_skb = cb->skb;
994 info.out_skb = skb;
995 info.nlmsg_seq = cb->nlh->nlmsg_seq;
996 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800997
998 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100999 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001000 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001001 u8 proto = 0;
1002 int err;
1003
Nicolas Dichteld3623092014-02-14 15:30:36 +01001004 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
1005 xfrma_policy);
1006 if (err < 0)
1007 return err;
1008
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001009 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +02001010 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1011 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +01001012 if (filter == NULL)
1013 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001014 }
1015
1016 if (attrs[XFRMA_PROTO])
1017 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1018
1019 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +02001020 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -08001021 }
1022
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001023 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 return skb->len;
1026}
1027
1028static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1029 struct xfrm_state *x, u32 seq)
1030{
1031 struct xfrm_dump_info info;
1032 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +00001033 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Thomas Graf7deb2262007-08-22 13:57:39 -07001035 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!skb)
1037 return ERR_PTR(-ENOMEM);
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 info.in_skb = in_skb;
1040 info.out_skb = skb;
1041 info.nlmsg_seq = seq;
1042 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Mathias Krause864745d2012-09-13 11:41:26 +00001044 err = dump_one_state(x, 0, &info);
1045 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +00001047 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
1050 return skb;
1051}
1052
Michal Kubecek21ee5432014-06-03 10:26:06 +02001053/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1054 * Must be called with RCU read lock.
1055 */
1056static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1057 u32 pid, unsigned int group)
1058{
1059 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1060
Florian Westphal301a6da2018-06-25 14:00:07 +02001061 if (!nlsk) {
1062 kfree_skb(skb);
1063 return -EPIPE;
1064 }
1065
1066 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
Michal Kubecek21ee5432014-06-03 10:26:06 +02001067}
1068
Thomas Graf7deb2262007-08-22 13:57:39 -07001069static inline size_t xfrm_spdinfo_msgsize(void)
1070{
1071 return NLMSG_ALIGN(4)
1072 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001073 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1074 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1075 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -07001076}
1077
Alexey Dobriyane0710412010-01-23 13:37:10 +00001078static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001079 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001080{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001081 struct xfrmk_spdinfo si;
1082 struct xfrmu_spdinfo spc;
1083 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001084 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001085 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001086 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001087 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001088 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001089
Eric W. Biederman15e47302012-09-07 20:12:54 +00001090 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001091 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001092 return -EMSGSIZE;
1093
1094 f = nlmsg_data(nlh);
1095 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001096 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001097 spc.incnt = si.incnt;
1098 spc.outcnt = si.outcnt;
1099 spc.fwdcnt = si.fwdcnt;
1100 spc.inscnt = si.inscnt;
1101 spc.outscnt = si.outscnt;
1102 spc.fwdscnt = si.fwdscnt;
1103 sph.spdhcnt = si.spdhcnt;
1104 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001105
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001106 do {
1107 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1108
1109 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1110 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1111 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1112 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1113 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1114
David S. Miller1d1e34d2012-06-27 21:57:03 -07001115 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1116 if (!err)
1117 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001118 if (!err)
1119 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1120 if (!err)
1121 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001122 if (err) {
1123 nlmsg_cancel(skb, nlh);
1124 return err;
1125 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001126
Johannes Berg053c0952015-01-16 22:09:00 +01001127 nlmsg_end(skb, nlh);
1128 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001129}
1130
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001131static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1132 struct nlattr **attrs)
1133{
1134 struct net *net = sock_net(skb->sk);
1135 struct xfrmu_spdhthresh *thresh4 = NULL;
1136 struct xfrmu_spdhthresh *thresh6 = NULL;
1137
1138 /* selector prefixlen thresholds to hash policies */
1139 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1140 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1141
1142 if (nla_len(rta) < sizeof(*thresh4))
1143 return -EINVAL;
1144 thresh4 = nla_data(rta);
1145 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1146 return -EINVAL;
1147 }
1148 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1149 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1150
1151 if (nla_len(rta) < sizeof(*thresh6))
1152 return -EINVAL;
1153 thresh6 = nla_data(rta);
1154 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1155 return -EINVAL;
1156 }
1157
1158 if (thresh4 || thresh6) {
1159 write_seqlock(&net->xfrm.policy_hthresh.lock);
1160 if (thresh4) {
1161 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1162 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1163 }
1164 if (thresh6) {
1165 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1166 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1167 }
1168 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1169
1170 xfrm_policy_hash_rebuild(net);
1171 }
1172
1173 return 0;
1174}
1175
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001176static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001177 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001178{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001179 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001180 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001181 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001182 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001183 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001184
Thomas Graf7deb2262007-08-22 13:57:39 -07001185 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001186 if (r_skb == NULL)
1187 return -ENOMEM;
1188
Eric W. Biederman15e47302012-09-07 20:12:54 +00001189 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001190 BUG();
1191
Eric W. Biederman15e47302012-09-07 20:12:54 +00001192 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001193}
1194
Thomas Graf7deb2262007-08-22 13:57:39 -07001195static inline size_t xfrm_sadinfo_msgsize(void)
1196{
1197 return NLMSG_ALIGN(4)
1198 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1199 + nla_total_size(4); /* XFRMA_SAD_CNT */
1200}
1201
Alexey Dobriyane0710412010-01-23 13:37:10 +00001202static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001203 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001204{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001205 struct xfrmk_sadinfo si;
1206 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001207 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001208 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001209 u32 *f;
1210
Eric W. Biederman15e47302012-09-07 20:12:54 +00001211 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001212 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001213 return -EMSGSIZE;
1214
1215 f = nlmsg_data(nlh);
1216 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001217 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001218
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001219 sh.sadhmcnt = si.sadhmcnt;
1220 sh.sadhcnt = si.sadhcnt;
1221
David S. Miller1d1e34d2012-06-27 21:57:03 -07001222 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1223 if (!err)
1224 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1225 if (err) {
1226 nlmsg_cancel(skb, nlh);
1227 return err;
1228 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001229
Johannes Berg053c0952015-01-16 22:09:00 +01001230 nlmsg_end(skb, nlh);
1231 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001232}
1233
1234static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001235 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001236{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001237 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001238 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001239 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001240 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001241 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001242
Thomas Graf7deb2262007-08-22 13:57:39 -07001243 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001244 if (r_skb == NULL)
1245 return -ENOMEM;
1246
Eric W. Biederman15e47302012-09-07 20:12:54 +00001247 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001248 BUG();
1249
Eric W. Biederman15e47302012-09-07 20:12:54 +00001250 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001251}
1252
Christoph Hellwig22e70052007-01-02 15:22:30 -08001253static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001254 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001256 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001257 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct xfrm_state *x;
1259 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001260 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001262 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (x == NULL)
1264 goto out_noput;
1265
1266 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1267 if (IS_ERR(resp_skb)) {
1268 err = PTR_ERR(resp_skb);
1269 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001270 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272 xfrm_state_put(x);
1273out_noput:
1274 return err;
1275}
1276
Christoph Hellwig22e70052007-01-02 15:22:30 -08001277static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001278 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001280 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct xfrm_state *x;
1282 struct xfrm_userspi_info *p;
1283 struct sk_buff *resp_skb;
1284 xfrm_address_t *daddr;
1285 int family;
1286 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001287 u32 mark;
1288 struct xfrm_mark m;
Steffen Klasserta80f5602018-06-12 14:07:07 +02001289 u32 if_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Thomas Graf7b67c852007-08-22 13:53:52 -07001291 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001292 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (err)
1294 goto out_noput;
1295
1296 family = p->info.family;
1297 daddr = &p->info.id.daddr;
1298
1299 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001300
1301 mark = xfrm_mark_get(attrs, &m);
Steffen Klasserta80f5602018-06-12 14:07:07 +02001302
1303 if (attrs[XFRMA_IF_ID])
1304 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001307 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001308 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 xfrm_state_put(x);
1310 x = NULL;
1311 }
1312 }
1313
1314 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001315 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Steffen Klasserta80f5602018-06-12 14:07:07 +02001316 if_id, p->info.id.proto, daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 &p->info.saddr, 1,
1318 family);
1319 err = -ENOENT;
1320 if (x == NULL)
1321 goto out_noput;
1322
Herbert Xu658b2192007-10-09 13:29:52 -07001323 err = xfrm_alloc_spi(x, p->min, p->max);
1324 if (err)
1325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Herbert Xu658b2192007-10-09 13:29:52 -07001327 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (IS_ERR(resp_skb)) {
1329 err = PTR_ERR(resp_skb);
1330 goto out;
1331 }
1332
Eric W. Biederman15e47302012-09-07 20:12:54 +00001333 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335out:
1336 xfrm_state_put(x);
1337out_noput:
1338 return err;
1339}
1340
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001341static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 switch (dir) {
1344 case XFRM_POLICY_IN:
1345 case XFRM_POLICY_OUT:
1346 case XFRM_POLICY_FWD:
1347 break;
1348
1349 default:
1350 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 return 0;
1354}
1355
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001356static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001357{
1358 switch (type) {
1359 case XFRM_POLICY_TYPE_MAIN:
1360#ifdef CONFIG_XFRM_SUB_POLICY
1361 case XFRM_POLICY_TYPE_SUB:
1362#endif
1363 break;
1364
1365 default:
1366 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001367 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001368
1369 return 0;
1370}
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1373{
Fan Due682adf2013-11-07 17:47:48 +08001374 int ret;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 switch (p->share) {
1377 case XFRM_SHARE_ANY:
1378 case XFRM_SHARE_SESSION:
1379 case XFRM_SHARE_USER:
1380 case XFRM_SHARE_UNIQUE:
1381 break;
1382
1383 default:
1384 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 switch (p->action) {
1388 case XFRM_POLICY_ALLOW:
1389 case XFRM_POLICY_BLOCK:
1390 break;
1391
1392 default:
1393 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 switch (p->sel.family) {
1397 case AF_INET:
Steffen Klassert89cb86e2018-08-01 13:45:11 +02001398 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1399 return -EINVAL;
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 break;
1402
1403 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001404#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert89cb86e2018-08-01 13:45:11 +02001405 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1406 return -EINVAL;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409#else
1410 return -EAFNOSUPPORT;
1411#endif
1412
1413 default:
1414 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Fan Due682adf2013-11-07 17:47:48 +08001417 ret = verify_policy_dir(p->dir);
1418 if (ret)
1419 return ret;
YueHaibing7c967212019-02-28 15:18:59 +08001420 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
Fan Due682adf2013-11-07 17:47:48 +08001421 return -EINVAL;
1422
1423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Thomas Graf5424f322007-08-22 14:01:33 -07001426static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001427{
Thomas Graf5424f322007-08-22 14:01:33 -07001428 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001429 struct xfrm_user_sec_ctx *uctx;
1430
1431 if (!rt)
1432 return 0;
1433
Thomas Graf5424f322007-08-22 14:01:33 -07001434 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001435 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1439 int nr)
1440{
1441 int i;
1442
1443 xp->xfrm_nr = nr;
1444 for (i = 0; i < nr; i++, ut++) {
1445 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1446
1447 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1448 memcpy(&t->saddr, &ut->saddr,
1449 sizeof(xfrm_address_t));
1450 t->reqid = ut->reqid;
1451 t->mode = ut->mode;
1452 t->share = ut->share;
1453 t->optional = ut->optional;
1454 t->aalgos = ut->aalgos;
1455 t->ealgos = ut->ealgos;
1456 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001457 /* If all masks are ~0, then we allow all algorithms. */
1458 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001459 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461}
1462
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001463static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1464{
Steffen Klassert9a54c512017-12-08 08:07:25 +01001465 u16 prev_family;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001466 int i;
1467
1468 if (nr > XFRM_MAX_DEPTH)
1469 return -EINVAL;
1470
Steffen Klassert9a54c512017-12-08 08:07:25 +01001471 prev_family = family;
1472
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001473 for (i = 0; i < nr; i++) {
1474 /* We never validated the ut->family value, so many
1475 * applications simply leave it at zero. The check was
1476 * never made and ut->family was ignored because all
1477 * templates could be assumed to have the same family as
1478 * the policy itself. Now that we will have ipv4-in-ipv6
1479 * and ipv6-in-ipv4 tunnels, this is no longer true.
1480 */
1481 if (!ut[i].family)
1482 ut[i].family = family;
1483
Florian Westphala19fd852019-01-09 14:37:34 +01001484 switch (ut[i].mode) {
1485 case XFRM_MODE_TUNNEL:
1486 case XFRM_MODE_BEET:
1487 break;
1488 default:
1489 if (ut[i].family != prev_family)
1490 return -EINVAL;
1491 break;
1492 }
Sean Tranchettid83617a2018-09-07 15:50:31 -06001493 if (ut[i].mode >= XFRM_MODE_MAX)
1494 return -EINVAL;
1495
Steffen Klassert9a54c512017-12-08 08:07:25 +01001496 prev_family = ut[i].family;
1497
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001498 switch (ut[i].family) {
1499 case AF_INET:
1500 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001501#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001502 case AF_INET6:
1503 break;
1504#endif
1505 default:
1506 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001507 }
Cong Wang85552882017-11-27 11:15:16 -08001508
Cong Wang88d08312019-03-22 16:26:19 -07001509 if (!xfrm_id_proto_valid(ut[i].id.proto))
Cong Wang85552882017-11-27 11:15:16 -08001510 return -EINVAL;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001511 }
1512
1513 return 0;
1514}
1515
Thomas Graf5424f322007-08-22 14:01:33 -07001516static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Thomas Graf5424f322007-08-22 14:01:33 -07001518 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (!rt) {
1521 pol->xfrm_nr = 0;
1522 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001523 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1524 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001525 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001527 err = validate_tmpl(nr, utmpl, pol->family);
1528 if (err)
1529 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Thomas Graf5424f322007-08-22 14:01:33 -07001531 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 return 0;
1534}
1535
Thomas Graf5424f322007-08-22 14:01:33 -07001536static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001537{
Thomas Graf5424f322007-08-22 14:01:33 -07001538 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001539 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001540 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001541 int err;
1542
1543 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001544 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001545 type = upt->type;
1546 }
1547
1548 err = verify_policy_type(type);
1549 if (err)
1550 return err;
1551
1552 *tp = type;
1553 return 0;
1554}
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1557{
1558 xp->priority = p->priority;
1559 xp->index = p->index;
1560 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1561 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1562 xp->action = p->action;
1563 xp->flags = p->flags;
1564 xp->family = p->sel.family;
1565 /* XXX xp->share = p->share; */
1566}
1567
1568static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1569{
Mathias Krause7b789832012-09-19 11:33:40 +00001570 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1572 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1573 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1574 p->priority = xp->priority;
1575 p->index = xp->index;
1576 p->sel.family = xp->family;
1577 p->dir = dir;
1578 p->action = xp->action;
1579 p->flags = xp->flags;
1580 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1581}
1582
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001583static 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 -07001584{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001585 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 int err;
1587
1588 if (!xp) {
1589 *errp = -ENOMEM;
1590 return NULL;
1591 }
1592
1593 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001594
Thomas Graf35a7aa02007-08-22 14:00:40 -07001595 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001596 if (err)
1597 goto error;
1598
Thomas Graf35a7aa02007-08-22 14:00:40 -07001599 if (!(err = copy_from_user_tmpl(xp, attrs)))
1600 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001601 if (err)
1602 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001604 xfrm_mark_get(attrs, &xp->mark);
1605
Steffen Klasserta80f5602018-06-12 14:07:07 +02001606 if (attrs[XFRMA_IF_ID])
1607 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001610 error:
1611 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001612 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001613 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001614 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
Christoph Hellwig22e70052007-01-02 15:22:30 -08001617static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001618 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001620 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001621 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001623 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 int err;
1625 int excl;
1626
1627 err = verify_newpolicy_info(p);
1628 if (err)
1629 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001630 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001631 if (err)
1632 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001634 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (!xp)
1636 return err;
1637
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001638 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001639 * Aha! this is anti-netlink really i.e more pfkey derived
1640 * in netlink excl is a flag and you wouldnt need
1641 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1643 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001644 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001647 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 kfree(xp);
1649 return err;
1650 }
1651
Herbert Xuf60f6b82005-06-18 22:44:37 -07001652 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001653 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001654 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001655 km_policy_notify(xp, p->dir, &c);
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 xfrm_pol_put(xp);
1658
1659 return 0;
1660}
1661
1662static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1663{
1664 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1665 int i;
1666
1667 if (xp->xfrm_nr == 0)
1668 return 0;
1669
1670 for (i = 0; i < xp->xfrm_nr; i++) {
1671 struct xfrm_user_tmpl *up = &vec[i];
1672 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1673
Mathias Krause1f868402012-09-19 11:33:41 +00001674 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001676 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1678 up->reqid = kp->reqid;
1679 up->mode = kp->mode;
1680 up->share = kp->share;
1681 up->optional = kp->optional;
1682 up->aalgos = kp->aalgos;
1683 up->ealgos = kp->ealgos;
1684 up->calgos = kp->calgos;
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Thomas Grafc0144be2007-08-22 13:55:43 -07001687 return nla_put(skb, XFRMA_TMPL,
1688 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001689}
1690
Serge Hallyn0d681622006-07-24 23:30:44 -07001691static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1692{
1693 if (x->security) {
1694 return copy_sec_ctx(x->security, skb);
1695 }
1696 return 0;
1697}
1698
1699static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1700{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001701 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001702 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001703 return 0;
1704}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001705static inline size_t userpolicy_type_attrsize(void)
1706{
1707#ifdef CONFIG_XFRM_SUB_POLICY
1708 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1709#else
1710 return 0;
1711#endif
1712}
Serge Hallyn0d681622006-07-24 23:30:44 -07001713
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001714#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001715static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001716{
Eric Dumazet2038a9e2018-06-18 21:35:07 -07001717 struct xfrm_userpolicy_type upt;
1718
1719 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1720 memset(&upt, 0, sizeof(upt));
1721 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001722
Thomas Grafc0144be2007-08-22 13:55:43 -07001723 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001724}
1725
1726#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001727static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001728{
1729 return 0;
1730}
1731#endif
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1734{
1735 struct xfrm_dump_info *sp = ptr;
1736 struct xfrm_userpolicy_info *p;
1737 struct sk_buff *in_skb = sp->in_skb;
1738 struct sk_buff *skb = sp->out_skb;
1739 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Eric W. Biederman15e47302012-09-07 20:12:54 +00001742 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001743 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1744 if (nlh == NULL)
1745 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Thomas Graf7b67c852007-08-22 13:53:52 -07001747 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001749 err = copy_to_user_tmpl(xp, skb);
1750 if (!err)
1751 err = copy_to_user_sec_ctx(xp, skb);
1752 if (!err)
1753 err = copy_to_user_policy_type(xp->type, skb);
1754 if (!err)
1755 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klasserta80f5602018-06-12 14:07:07 +02001756 if (!err)
1757 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001758 if (err) {
1759 nlmsg_cancel(skb, nlh);
1760 return err;
1761 }
Thomas Graf98250692007-08-22 12:47:26 -07001762 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Timo Teras4c563f72008-02-28 21:31:08 -08001766static int xfrm_dump_policy_done(struct netlink_callback *cb)
1767{
Herbert Xu543aabb2017-10-19 20:51:10 +08001768 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Fan Du283bc9f2013-11-07 17:47:50 +08001769 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001770
Fan Du283bc9f2013-11-07 17:47:50 +08001771 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001772 return 0;
1773}
1774
Herbert Xu543aabb2017-10-19 20:51:10 +08001775static int xfrm_dump_policy_start(struct netlink_callback *cb)
1776{
1777 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1778
1779 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1780
1781 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1782 return 0;
1783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1786{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001787 struct net *net = sock_net(skb->sk);
Herbert Xu543aabb2017-10-19 20:51:10 +08001788 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct xfrm_dump_info info;
1790
1791 info.in_skb = cb->skb;
1792 info.out_skb = skb;
1793 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1794 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001795
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001796 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 return skb->len;
1799}
1800
1801static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1802 struct xfrm_policy *xp,
1803 int dir, u32 seq)
1804{
1805 struct xfrm_dump_info info;
1806 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001807 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Thomas Graf7deb2262007-08-22 13:57:39 -07001809 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (!skb)
1811 return ERR_PTR(-ENOMEM);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 info.in_skb = in_skb;
1814 info.out_skb = skb;
1815 info.nlmsg_seq = seq;
1816 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Mathias Krausec2546372012-09-14 09:58:32 +00001818 err = dump_one_policy(xp, dir, 0, &info);
1819 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001821 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823
1824 return skb;
1825}
1826
Christoph Hellwig22e70052007-01-02 15:22:30 -08001827static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001828 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001830 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct xfrm_policy *xp;
1832 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001833 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001835 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001837 struct xfrm_mark m;
1838 u32 mark = xfrm_mark_get(attrs, &m);
Steffen Klasserta80f5602018-06-12 14:07:07 +02001839 u32 if_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Thomas Graf7b67c852007-08-22 13:53:52 -07001841 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1843
Thomas Graf35a7aa02007-08-22 14:00:40 -07001844 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001845 if (err)
1846 return err;
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 err = verify_policy_dir(p->dir);
1849 if (err)
1850 return err;
1851
Steffen Klasserta80f5602018-06-12 14:07:07 +02001852 if (attrs[XFRMA_IF_ID])
1853 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (p->index)
Steffen Klasserta80f5602018-06-12 14:07:07 +02001856 xp = xfrm_policy_byid(net, mark, if_id, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001857 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001858 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001859 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001860
Thomas Graf35a7aa02007-08-22 14:00:40 -07001861 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001862 if (err)
1863 return err;
1864
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001865 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001866 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001867 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001868
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001869 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001870 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001871 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001872 }
Steffen Klasserta80f5602018-06-12 14:07:07 +02001873 xp = xfrm_policy_bysel_ctx(net, mark, if_id, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001874 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001875 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (xp == NULL)
1878 return -ENOENT;
1879
1880 if (!delete) {
1881 struct sk_buff *resp_skb;
1882
1883 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1884 if (IS_ERR(resp_skb)) {
1885 err = PTR_ERR(resp_skb);
1886 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001887 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001888 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001890 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001891 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001892
1893 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001894 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001895
Herbert Xue7443892005-06-18 22:44:18 -07001896 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001897 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001898 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001899 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001900 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001903out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001904 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001905 if (delete && err == 0)
1906 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return err;
1908}
1909
Christoph Hellwig22e70052007-01-02 15:22:30 -08001910static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001911 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001913 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001914 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001915 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001916 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Tetsuo Handa2e710292014-04-22 21:48:30 +09001918 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001919 if (err) {
1920 if (err == -ESRCH) /* empty table */
1921 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001922 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001923 }
Herbert Xubf088672005-06-18 22:44:00 -07001924 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001925 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001926 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001927 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001928 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001929 km_state_notify(NULL, &c);
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
1932}
1933
Steffen Klassertd8647b72011-03-08 00:10:27 +00001934static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001935{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001936 size_t replay_size = x->replay_esn ?
1937 xfrm_replay_state_esn_len(x->replay_esn) :
1938 sizeof(struct xfrm_replay_state);
1939
Thomas Graf7deb2262007-08-22 13:57:39 -07001940 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001941 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001942 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001943 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001944 + nla_total_size(4) /* XFRM_AE_RTHR */
1945 + nla_total_size(4); /* XFRM_AE_ETHR */
1946}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001947
David S. Miller214e0052011-02-24 00:02:38 -05001948static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001949{
1950 struct xfrm_aevent_id *id;
1951 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001952 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001953
Eric W. Biederman15e47302012-09-07 20:12:54 +00001954 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001955 if (nlh == NULL)
1956 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001957
Thomas Graf7b67c852007-08-22 13:53:52 -07001958 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001959 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001960 id->sa_id.spi = x->id.spi;
1961 id->sa_id.family = x->props.family;
1962 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001963 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001964 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001965 id->flags = c->data.aevent;
1966
David S. Millerd0fde792012-03-29 04:02:26 -04001967 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001968 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1969 xfrm_replay_state_esn_len(x->replay_esn),
1970 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001971 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001972 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1973 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001974 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001975 if (err)
1976 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001977 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1978 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001979 if (err)
1980 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001981
David S. Miller1d1e34d2012-06-27 21:57:03 -07001982 if (id->flags & XFRM_AE_RTHR) {
1983 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1984 if (err)
1985 goto out_cancel;
1986 }
1987 if (id->flags & XFRM_AE_ETHR) {
1988 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1989 x->replay_maxage * 10 / HZ);
1990 if (err)
1991 goto out_cancel;
1992 }
1993 err = xfrm_mark_put(skb, &x->mark);
1994 if (err)
1995 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001996
Steffen Klasserta80f5602018-06-12 14:07:07 +02001997 err = xfrm_if_id_put(skb, x->if_id);
1998 if (err)
1999 goto out_cancel;
2000
Johannes Berg053c0952015-01-16 22:09:00 +01002001 nlmsg_end(skb, nlh);
2002 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002003
David S. Miller1d1e34d2012-06-27 21:57:03 -07002004out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002005 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002006 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002007}
2008
Christoph Hellwig22e70052007-01-02 15:22:30 -08002009static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002010 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002011{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002012 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002013 struct xfrm_state *x;
2014 struct sk_buff *r_skb;
2015 int err;
2016 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002017 u32 mark;
2018 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07002019 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002020 struct xfrm_usersa_id *id = &p->sa_id;
2021
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002022 mark = xfrm_mark_get(attrs, &m);
2023
2024 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00002025 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002026 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00002027
2028 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2029 if (r_skb == NULL) {
2030 xfrm_state_put(x);
2031 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002032 }
2033
2034 /*
2035 * XXX: is this lock really needed - none of the other
2036 * gets lock (the concern is things getting updated
2037 * while we are still reading) - jhs
2038 */
2039 spin_lock_bh(&x->lock);
2040 c.data.aevent = p->flags;
2041 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002042 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002043
2044 if (build_aevent(r_skb, x, &c) < 0)
2045 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00002046 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002047 spin_unlock_bh(&x->lock);
2048 xfrm_state_put(x);
2049 return err;
2050}
2051
Christoph Hellwig22e70052007-01-02 15:22:30 -08002052static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002053 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002054{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002055 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002056 struct xfrm_state *x;
2057 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08002058 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002059 u32 mark = 0;
2060 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07002061 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07002062 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00002063 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07002064 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02002065 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2066 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002067
Michael Rossberg4e077232015-09-29 11:25:08 +02002068 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002069 return err;
2070
2071 /* pedantic mode - thou shalt sayeth replaceth */
2072 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2073 return err;
2074
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002075 mark = xfrm_mark_get(attrs, &m);
2076
2077 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 -08002078 if (x == NULL)
2079 return -ESRCH;
2080
2081 if (x->km.state != XFRM_STATE_VALID)
2082 goto out;
2083
Steffen Klassert4479ff72013-09-09 09:39:01 +02002084 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00002085 if (err)
2086 goto out;
2087
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002088 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00002089 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002090 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002091
2092 c.event = nlh->nlmsg_type;
2093 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002094 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002095 c.data.aevent = XFRM_AE_CU;
2096 km_state_notify(x, &c);
2097 err = 0;
2098out:
2099 xfrm_state_put(x);
2100 return err;
2101}
2102
Christoph Hellwig22e70052007-01-02 15:22:30 -08002103static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002104 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002106 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002107 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002108 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002109 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002110
Thomas Graf35a7aa02007-08-22 14:00:40 -07002111 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002112 if (err)
2113 return err;
2114
Tetsuo Handa2e710292014-04-22 21:48:30 +09002115 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002116 if (err) {
2117 if (err == -ESRCH) /* empty table */
2118 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002119 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002120 }
2121
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002122 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002123 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002124 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002125 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002126 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002127 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return 0;
2129}
2130
Christoph Hellwig22e70052007-01-02 15:22:30 -08002131static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002132 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002133{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002134 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002135 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002136 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002137 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002138 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002139 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002140 struct xfrm_mark m;
2141 u32 mark = xfrm_mark_get(attrs, &m);
Steffen Klasserta80f5602018-06-12 14:07:07 +02002142 u32 if_id = 0;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002143
Thomas Graf35a7aa02007-08-22 14:00:40 -07002144 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002145 if (err)
2146 return err;
2147
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002148 err = verify_policy_dir(p->dir);
2149 if (err)
2150 return err;
2151
Steffen Klasserta80f5602018-06-12 14:07:07 +02002152 if (attrs[XFRMA_IF_ID])
2153 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2154
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002155 if (p->index)
Steffen Klasserta80f5602018-06-12 14:07:07 +02002156 xp = xfrm_policy_byid(net, mark, if_id, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002157 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002158 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002159 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002160
Thomas Graf35a7aa02007-08-22 14:00:40 -07002161 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002162 if (err)
2163 return err;
2164
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002165 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002166 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002167 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002168
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002169 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002170 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002171 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002172 }
Steffen Klasserta80f5602018-06-12 14:07:07 +02002173 xp = xfrm_policy_bysel_ctx(net, mark, if_id, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002174 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002175 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002176 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002177 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002178 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002179
Timo Teräsea2dea92010-03-31 00:17:05 +00002180 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002181 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002182
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002183 err = 0;
2184 if (up->hard) {
2185 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002186 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002187 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002188 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002189
2190out:
2191 xfrm_pol_put(xp);
2192 return err;
2193}
2194
Christoph Hellwig22e70052007-01-02 15:22:30 -08002195static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002196 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002197{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002198 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002199 struct xfrm_state *x;
2200 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002201 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002202 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002203 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002204 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002205
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002206 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 -08002207
David S. Miller3a765aa2007-02-26 14:52:21 -08002208 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002209 if (x == NULL)
2210 return err;
2211
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002212 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002213 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002214 if (x->km.state != XFRM_STATE_VALID)
2215 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002216 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002217
Joy Latten161a09e2006-11-27 13:11:54 -06002218 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002219 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002220 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002221 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002222 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002223out:
2224 spin_unlock_bh(&x->lock);
2225 xfrm_state_put(x);
2226 return err;
2227}
2228
Christoph Hellwig22e70052007-01-02 15:22:30 -08002229static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002230 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002231{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002232 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002233 struct xfrm_policy *xp;
2234 struct xfrm_user_tmpl *ut;
2235 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002236 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002237 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002238
Thomas Graf7b67c852007-08-22 13:53:52 -07002239 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002240 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002241 int err = -ENOMEM;
2242
2243 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002244 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002245
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002246 xfrm_mark_get(attrs, &mark);
2247
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002248 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002249 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002250 goto free_state;
Xin Longc2e254e2020-02-09 21:16:38 +08002251 err = verify_sec_ctx_len(attrs);
2252 if (err)
2253 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002254
2255 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002256 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002257 if (!xp)
2258 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002259
2260 memcpy(&x->id, &ua->id, sizeof(ua->id));
2261 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2262 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002263 xp->mark.m = x->mark.m = mark.m;
2264 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002265 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002266 /* extract the templates and for each call km_key */
2267 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2268 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2269 memcpy(&x->id, &t->id, sizeof(x->id));
2270 x->props.mode = t->mode;
2271 x->props.reqid = t->reqid;
2272 x->props.family = ut->family;
2273 t->aalgos = ua->aalgos;
2274 t->ealgos = ua->ealgos;
2275 t->calgos = ua->calgos;
2276 err = km_query(x, t, xp);
2277
2278 }
2279
2280 kfree(x);
2281 kfree(xp);
2282
2283 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002284
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002285free_state:
2286 kfree(x);
2287nomem:
2288 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002289}
2290
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002291#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002292static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002293 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002294 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002295{
Thomas Graf5424f322007-08-22 14:01:33 -07002296 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002297 struct xfrm_user_migrate *um;
2298 int i, num_migrate;
2299
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002300 if (k != NULL) {
2301 struct xfrm_user_kmaddress *uk;
2302
2303 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2304 memcpy(&k->local, &uk->local, sizeof(k->local));
2305 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2306 k->family = uk->family;
2307 k->reserved = uk->reserved;
2308 }
2309
Thomas Graf5424f322007-08-22 14:01:33 -07002310 um = nla_data(rt);
2311 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002312
2313 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2314 return -EINVAL;
2315
2316 for (i = 0; i < num_migrate; i++, um++, ma++) {
2317 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2318 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2319 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2320 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2321
2322 ma->proto = um->proto;
2323 ma->mode = um->mode;
2324 ma->reqid = um->reqid;
2325
2326 ma->old_family = um->old_family;
2327 ma->new_family = um->new_family;
2328 }
2329
2330 *num = i;
2331 return 0;
2332}
2333
2334static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002335 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002336{
Thomas Graf7b67c852007-08-22 13:53:52 -07002337 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002338 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002339 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002340 u8 type;
2341 int err;
2342 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002343 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002344
Thomas Graf35a7aa02007-08-22 14:00:40 -07002345 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002346 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002347
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002348 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2349
Thomas Graf5424f322007-08-22 14:01:33 -07002350 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002351 if (err)
2352 return err;
2353
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002354 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002355 if (err)
2356 return err;
2357
2358 if (!n)
2359 return 0;
2360
Fan Du8d549c42013-11-07 17:47:49 +08002361 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002362
2363 return 0;
2364}
2365#else
2366static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002367 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002368{
2369 return -ENOPROTOOPT;
2370}
2371#endif
2372
2373#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002374static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002375{
2376 struct xfrm_user_migrate um;
2377
2378 memset(&um, 0, sizeof(um));
2379 um.proto = m->proto;
2380 um.mode = m->mode;
2381 um.reqid = m->reqid;
2382 um.old_family = m->old_family;
2383 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2384 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2385 um.new_family = m->new_family;
2386 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2387 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2388
Thomas Grafc0144be2007-08-22 13:55:43 -07002389 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002390}
2391
David S. Miller183cad12011-02-24 00:28:01 -05002392static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002393{
2394 struct xfrm_user_kmaddress uk;
2395
2396 memset(&uk, 0, sizeof(uk));
2397 uk.family = k->family;
2398 uk.reserved = k->reserved;
2399 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002400 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002401
2402 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2403}
2404
2405static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002406{
2407 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002408 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2409 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2410 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002411}
2412
David S. Miller183cad12011-02-24 00:28:01 -05002413static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2414 int num_migrate, const struct xfrm_kmaddress *k,
2415 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002416{
David S. Miller183cad12011-02-24 00:28:01 -05002417 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002418 struct xfrm_userpolicy_id *pol_id;
2419 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002420 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002421
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002422 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2423 if (nlh == NULL)
2424 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002425
Thomas Graf7b67c852007-08-22 13:53:52 -07002426 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002427 /* copy data from selector, dir, and type to the pol_id */
2428 memset(pol_id, 0, sizeof(*pol_id));
2429 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2430 pol_id->dir = dir;
2431
David S. Miller1d1e34d2012-06-27 21:57:03 -07002432 if (k != NULL) {
2433 err = copy_to_user_kmaddress(k, skb);
2434 if (err)
2435 goto out_cancel;
2436 }
2437 err = copy_to_user_policy_type(type, skb);
2438 if (err)
2439 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002440 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002441 err = copy_to_user_migrate(mp, skb);
2442 if (err)
2443 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002444 }
2445
Johannes Berg053c0952015-01-16 22:09:00 +01002446 nlmsg_end(skb, nlh);
2447 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002448
2449out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002450 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002451 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002452}
2453
David S. Miller183cad12011-02-24 00:28:01 -05002454static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2455 const struct xfrm_migrate *m, int num_migrate,
2456 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002457{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002458 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002459 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002460
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002461 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002462 if (skb == NULL)
2463 return -ENOMEM;
2464
2465 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002466 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002467 BUG();
2468
Michal Kubecek21ee5432014-06-03 10:26:06 +02002469 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002470}
2471#else
David S. Miller183cad12011-02-24 00:28:01 -05002472static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2473 const struct xfrm_migrate *m, int num_migrate,
2474 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002475{
2476 return -ENOPROTOOPT;
2477}
2478#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002479
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002480#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002481
2482static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002483 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002484 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2485 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2486 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2487 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2488 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2489 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002490 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002491 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002492 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002493 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002494 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002495 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002496 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002497 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2498 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002499 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002500 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002501 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002502 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002503 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504};
2505
Thomas Graf492b5582005-05-03 14:26:40 -07002506#undef XMSGSIZE
2507
Thomas Grafcf5cb792007-08-22 13:59:04 -07002508static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002509 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2510 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2511 [XFRMA_LASTUSED] = { .type = NLA_U64},
2512 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002513 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002514 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2515 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2516 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2517 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2518 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2519 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2520 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2521 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2522 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2523 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2524 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2525 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2526 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2527 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002528 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002529 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002530 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002531 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002532 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002533 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002534 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Steffen Klassert78f10c52018-06-12 12:44:26 +02002535 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2536 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
Steffen Klasserta80f5602018-06-12 14:07:07 +02002537 [XFRMA_IF_ID] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002538};
2539
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002540static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2541 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2542 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2543};
2544
Mathias Krause05600a72013-02-24 14:10:27 +01002545static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002546 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Herbert Xu543aabb2017-10-19 20:51:10 +08002547 int (*start)(struct netlink_callback *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002549 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002550 const struct nla_policy *nla_pol;
2551 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002552} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2553 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2554 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2555 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002556 .dump = xfrm_dump_sa,
2557 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002558 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2559 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2560 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Herbert Xu543aabb2017-10-19 20:51:10 +08002561 .start = xfrm_dump_policy_start,
Timo Teras4c563f72008-02-28 21:31:08 -08002562 .dump = xfrm_dump_policy,
2563 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002564 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002565 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002566 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002567 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2568 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002569 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002570 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2571 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002572 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2573 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002574 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002575 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002576 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2577 .nla_pol = xfrma_spd_policy,
2578 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002579 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580};
2581
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002582static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002584 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002585 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002586 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002587 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 type -= XFRM_MSG_BASE;
2594 link = &xfrm_dispatch[type];
2595
2596 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002597 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002598 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Thomas Graf492b5582005-05-03 14:26:40 -07002600 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2601 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002602 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002604 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002606 {
2607 struct netlink_dump_control c = {
Herbert Xu543aabb2017-10-19 20:51:10 +08002608 .start = link->start,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002609 .dump = link->dump,
2610 .done = link->done,
2611 };
2612 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
2615
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002616 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2617 link->nla_max ? : XFRMA_MAX,
2618 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002619 if (err < 0)
2620 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002623 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Thomas Graf5424f322007-08-22 14:01:33 -07002625 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002628static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Fan Du283bc9f2013-11-07 17:47:50 +08002630 struct net *net = sock_net(skb->sk);
2631
2632 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002633 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002634 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
Thomas Graf7deb2262007-08-22 13:57:39 -07002637static inline size_t xfrm_expire_msgsize(void)
2638{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002639 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2640 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002641}
2642
David S. Miller214e0052011-02-24 00:02:38 -05002643static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 struct xfrm_user_expire *ue;
2646 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002647 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Eric W. Biederman15e47302012-09-07 20:12:54 +00002649 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002650 if (nlh == NULL)
2651 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Thomas Graf7b67c852007-08-22 13:53:52 -07002653 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002655 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
David S. Miller1d1e34d2012-06-27 21:57:03 -07002657 err = xfrm_mark_put(skb, &x->mark);
2658 if (err)
2659 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002660
Steffen Klasserta80f5602018-06-12 14:07:07 +02002661 err = xfrm_if_id_put(skb, x->if_id);
2662 if (err)
2663 return err;
2664
Johannes Berg053c0952015-01-16 22:09:00 +01002665 nlmsg_end(skb, nlh);
2666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
David S. Miller214e0052011-02-24 00:02:38 -05002669static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002671 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct sk_buff *skb;
2673
Thomas Graf7deb2262007-08-22 13:57:39 -07002674 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (skb == NULL)
2676 return -ENOMEM;
2677
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002678 if (build_expire(skb, x, c) < 0) {
2679 kfree_skb(skb);
2680 return -EMSGSIZE;
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Michal Kubecek21ee5432014-06-03 10:26:06 +02002683 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
David S. Miller214e0052011-02-24 00:02:38 -05002686static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002687{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002688 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002689 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002690
Steffen Klassertd8647b72011-03-08 00:10:27 +00002691 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002692 if (skb == NULL)
2693 return -ENOMEM;
2694
2695 if (build_aevent(skb, x, c) < 0)
2696 BUG();
2697
Michal Kubecek21ee5432014-06-03 10:26:06 +02002698 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002699}
2700
David S. Miller214e0052011-02-24 00:02:38 -05002701static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002702{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002703 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002704 struct xfrm_usersa_flush *p;
2705 struct nlmsghdr *nlh;
2706 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002707 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002708
Thomas Graf7deb2262007-08-22 13:57:39 -07002709 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002710 if (skb == NULL)
2711 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002712
Eric W. Biederman15e47302012-09-07 20:12:54 +00002713 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002714 if (nlh == NULL) {
2715 kfree_skb(skb);
2716 return -EMSGSIZE;
2717 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002718
Thomas Graf7b67c852007-08-22 13:53:52 -07002719 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002720 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002721
Thomas Graf98250692007-08-22 12:47:26 -07002722 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002723
Michal Kubecek21ee5432014-06-03 10:26:06 +02002724 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002725}
2726
Thomas Graf7deb2262007-08-22 13:57:39 -07002727static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002728{
Thomas Graf7deb2262007-08-22 13:57:39 -07002729 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002730 if (x->aead)
2731 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002732 if (x->aalg) {
2733 l += nla_total_size(sizeof(struct xfrm_algo) +
2734 (x->aalg->alg_key_len + 7) / 8);
2735 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2736 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002737 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002738 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002739 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002740 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002741 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002742 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002743 if (x->tfcpad)
2744 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002745 if (x->replay_esn)
2746 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002747 else
2748 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002749 if (x->security)
2750 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2751 x->security->ctx_len);
2752 if (x->coaddr)
2753 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002754 if (x->props.extra_flags)
2755 l += nla_total_size(sizeof(x->props.extra_flags));
Steffen Klassert78f10c52018-06-12 12:44:26 +02002756 if (x->props.smark.v | x->props.smark.m) {
2757 l += nla_total_size(sizeof(x->props.smark.v));
2758 l += nla_total_size(sizeof(x->props.smark.m));
2759 }
Steffen Klasserta80f5602018-06-12 14:07:07 +02002760 if (x->if_id)
2761 l += nla_total_size(sizeof(x->if_id));
Herbert Xu68325d32007-10-09 13:30:57 -07002762
Herbert Xud26f3982007-11-13 21:47:08 -08002763 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002764 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002765
2766 return l;
2767}
2768
David S. Miller214e0052011-02-24 00:02:38 -05002769static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002770{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002771 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002772 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002773 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002774 struct nlmsghdr *nlh;
2775 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002776 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002777 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002778
2779 headlen = sizeof(*p);
2780 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002781 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002782 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002783 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002784 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002785 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002786
Thomas Graf7deb2262007-08-22 13:57:39 -07002787 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002788 if (skb == NULL)
2789 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002790
Eric W. Biederman15e47302012-09-07 20:12:54 +00002791 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002792 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002793 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002794 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002795
Thomas Graf7b67c852007-08-22 13:53:52 -07002796 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002797 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002798 struct nlattr *attr;
2799
Thomas Graf7b67c852007-08-22 13:53:52 -07002800 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002801 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2802 id->spi = x->id.spi;
2803 id->family = x->props.family;
2804 id->proto = x->id.proto;
2805
Thomas Grafc0144be2007-08-22 13:55:43 -07002806 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002807 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002808 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002809 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002810
2811 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002812 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002813 err = copy_to_user_state_extra(x, p, skb);
2814 if (err)
2815 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002816
Thomas Graf98250692007-08-22 12:47:26 -07002817 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002818
Michal Kubecek21ee5432014-06-03 10:26:06 +02002819 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002820
David S. Miller1d1e34d2012-06-27 21:57:03 -07002821out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002822 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002823 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002824}
2825
David S. Miller214e0052011-02-24 00:02:38 -05002826static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002827{
2828
2829 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002830 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002831 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002832 case XFRM_MSG_NEWAE:
2833 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002834 case XFRM_MSG_DELSA:
2835 case XFRM_MSG_UPDSA:
2836 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002837 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002838 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002839 return xfrm_notify_sa_flush(c);
2840 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002841 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2842 c->event);
2843 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002844 }
2845
2846 return 0;
2847
2848}
2849
Thomas Graf7deb2262007-08-22 13:57:39 -07002850static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2851 struct xfrm_policy *xp)
2852{
2853 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2854 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002855 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002856 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2857 + userpolicy_type_attrsize();
2858}
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002861 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002863 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 struct xfrm_user_acquire *ua;
2865 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002866 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002868 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2869 if (nlh == NULL)
2870 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Thomas Graf7b67c852007-08-22 13:53:52 -07002872 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 memcpy(&ua->id, &x->id, sizeof(ua->id));
2874 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2875 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002876 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 ua->aalgos = xt->aalgos;
2878 ua->ealgos = xt->ealgos;
2879 ua->calgos = xt->calgos;
2880 ua->seq = x->km.seq = seq;
2881
David S. Miller1d1e34d2012-06-27 21:57:03 -07002882 err = copy_to_user_tmpl(xp, skb);
2883 if (!err)
2884 err = copy_to_user_state_sec_ctx(x, skb);
2885 if (!err)
2886 err = copy_to_user_policy_type(xp->type, skb);
2887 if (!err)
2888 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klasserta80f5602018-06-12 14:07:07 +02002889 if (!err)
2890 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002891 if (err) {
2892 nlmsg_cancel(skb, nlh);
2893 return err;
2894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Johannes Berg053c0952015-01-16 22:09:00 +01002896 nlmsg_end(skb, nlh);
2897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
2900static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002901 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002903 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Thomas Graf7deb2262007-08-22 13:57:39 -07002906 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (skb == NULL)
2908 return -ENOMEM;
2909
Fan Du65e07362012-08-15 10:13:47 +08002910 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 BUG();
2912
Michal Kubecek21ee5432014-06-03 10:26:06 +02002913 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
2916/* User gives us xfrm_user_policy_info followed by an array of 0
2917 * or more templates.
2918 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002919static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 u8 *data, int len, int *dir)
2921{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002922 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2924 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2925 struct xfrm_policy *xp;
2926 int nr;
2927
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002928 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 case AF_INET:
2930 if (opt != IP_XFRM_POLICY) {
2931 *dir = -EOPNOTSUPP;
2932 return NULL;
2933 }
2934 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002935#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 case AF_INET6:
2937 if (opt != IPV6_XFRM_POLICY) {
2938 *dir = -EOPNOTSUPP;
2939 return NULL;
2940 }
2941 break;
2942#endif
2943 default:
2944 *dir = -EINVAL;
2945 return NULL;
2946 }
2947
2948 *dir = -EINVAL;
2949
2950 if (len < sizeof(*p) ||
2951 verify_newpolicy_info(p))
2952 return NULL;
2953
2954 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002955 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return NULL;
2957
Herbert Xua4f1bac2005-07-26 15:43:17 -07002958 if (p->dir > XFRM_POLICY_OUT)
2959 return NULL;
2960
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002961 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (xp == NULL) {
2963 *dir = -ENOBUFS;
2964 return NULL;
2965 }
2966
2967 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002968 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 copy_templates(xp, ut, nr);
2970
2971 *dir = p->dir;
2972
2973 return xp;
2974}
2975
Thomas Graf7deb2262007-08-22 13:57:39 -07002976static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2977{
2978 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2979 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2980 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002981 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002982 + userpolicy_type_attrsize();
2983}
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002986 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002989 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002990 struct nlmsghdr *nlh;
2991 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Eric W. Biederman15e47302012-09-07 20:12:54 +00002993 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002994 if (nlh == NULL)
2995 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Thomas Graf7b67c852007-08-22 13:53:52 -07002997 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002999 err = copy_to_user_tmpl(xp, skb);
3000 if (!err)
3001 err = copy_to_user_sec_ctx(xp, skb);
3002 if (!err)
3003 err = copy_to_user_policy_type(xp->type, skb);
3004 if (!err)
3005 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klasserta80f5602018-06-12 14:07:07 +02003006 if (!err)
3007 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003008 if (err) {
3009 nlmsg_cancel(skb, nlh);
3010 return err;
3011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 upe->hard = !!hard;
3013
Johannes Berg053c0952015-01-16 22:09:00 +01003014 nlmsg_end(skb, nlh);
3015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
David S. Miller214e0052011-02-24 00:02:38 -05003018static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003020 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Thomas Graf7deb2262007-08-22 13:57:39 -07003023 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (skb == NULL)
3025 return -ENOMEM;
3026
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003027 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 BUG();
3029
Michal Kubecek21ee5432014-06-03 10:26:06 +02003030 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
David S. Miller214e0052011-02-24 00:02:38 -05003033static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003034{
David S. Miller1d1e34d2012-06-27 21:57:03 -07003035 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08003036 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003037 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07003038 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003039 struct nlmsghdr *nlh;
3040 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003041 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07003042
3043 headlen = sizeof(*p);
3044 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07003045 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07003046 headlen = sizeof(*id);
3047 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07003048 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003049 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07003050 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003051
Thomas Graf7deb2262007-08-22 13:57:39 -07003052 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003053 if (skb == NULL)
3054 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003055
Eric W. Biederman15e47302012-09-07 20:12:54 +00003056 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003057 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003058 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003059 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003060
Thomas Graf7b67c852007-08-22 13:53:52 -07003061 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07003062 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07003063 struct nlattr *attr;
3064
Thomas Graf7b67c852007-08-22 13:53:52 -07003065 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07003066 memset(id, 0, sizeof(*id));
3067 id->dir = dir;
3068 if (c->data.byid)
3069 id->index = xp->index;
3070 else
3071 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3072
Thomas Grafc0144be2007-08-22 13:55:43 -07003073 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07003074 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07003075 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003076 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07003077
3078 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07003079 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003080
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003081 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003082 err = copy_to_user_tmpl(xp, skb);
3083 if (!err)
3084 err = copy_to_user_policy_type(xp->type, skb);
3085 if (!err)
3086 err = xfrm_mark_put(skb, &xp->mark);
Steffen Klasserta80f5602018-06-12 14:07:07 +02003087 if (!err)
3088 err = xfrm_if_id_put(skb, xp->if_id);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003089 if (err)
3090 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003091
Thomas Graf98250692007-08-22 12:47:26 -07003092 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003093
Michal Kubecek21ee5432014-06-03 10:26:06 +02003094 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003095
David S. Miller1d1e34d2012-06-27 21:57:03 -07003096out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003097 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003098 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003099}
3100
David S. Miller214e0052011-02-24 00:02:38 -05003101static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003102{
Alexey Dobriyan70678022008-11-25 17:50:36 -08003103 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003104 struct nlmsghdr *nlh;
3105 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003106 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003107
Thomas Graf7deb2262007-08-22 13:57:39 -07003108 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003109 if (skb == NULL)
3110 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003111
Eric W. Biederman15e47302012-09-07 20:12:54 +00003112 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003113 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003114 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003115 goto out_free_skb;
3116 err = copy_to_user_policy_type(c->data.type, skb);
3117 if (err)
3118 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003119
Thomas Graf98250692007-08-22 12:47:26 -07003120 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003121
Michal Kubecek21ee5432014-06-03 10:26:06 +02003122 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003123
David S. Miller1d1e34d2012-06-27 21:57:03 -07003124out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003125 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003126 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003127}
3128
David S. Miller214e0052011-02-24 00:02:38 -05003129static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003130{
3131
3132 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003133 case XFRM_MSG_NEWPOLICY:
3134 case XFRM_MSG_UPDPOLICY:
3135 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003136 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003137 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003138 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003139 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003140 return xfrm_exp_policy_notify(xp, dir, c);
3141 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003142 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3143 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003144 }
3145
3146 return 0;
3147
3148}
3149
Thomas Graf7deb2262007-08-22 13:57:39 -07003150static inline size_t xfrm_report_msgsize(void)
3151{
3152 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3153}
3154
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003155static int build_report(struct sk_buff *skb, u8 proto,
3156 struct xfrm_selector *sel, xfrm_address_t *addr)
3157{
3158 struct xfrm_user_report *ur;
3159 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003160
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003161 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3162 if (nlh == NULL)
3163 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003164
Thomas Graf7b67c852007-08-22 13:53:52 -07003165 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003166 ur->proto = proto;
3167 memcpy(&ur->sel, sel, sizeof(ur->sel));
3168
David S. Miller1d1e34d2012-06-27 21:57:03 -07003169 if (addr) {
3170 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3171 if (err) {
3172 nlmsg_cancel(skb, nlh);
3173 return err;
3174 }
3175 }
Johannes Berg053c0952015-01-16 22:09:00 +01003176 nlmsg_end(skb, nlh);
3177 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003178}
3179
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003180static int xfrm_send_report(struct net *net, u8 proto,
3181 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003182{
3183 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003184
Thomas Graf7deb2262007-08-22 13:57:39 -07003185 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003186 if (skb == NULL)
3187 return -ENOMEM;
3188
3189 if (build_report(skb, proto, sel, addr) < 0)
3190 BUG();
3191
Michal Kubecek21ee5432014-06-03 10:26:06 +02003192 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003193}
3194
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003195static inline size_t xfrm_mapping_msgsize(void)
3196{
3197 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3198}
3199
3200static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3201 xfrm_address_t *new_saddr, __be16 new_sport)
3202{
3203 struct xfrm_user_mapping *um;
3204 struct nlmsghdr *nlh;
3205
3206 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3207 if (nlh == NULL)
3208 return -EMSGSIZE;
3209
3210 um = nlmsg_data(nlh);
3211
3212 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3213 um->id.spi = x->id.spi;
3214 um->id.family = x->props.family;
3215 um->id.proto = x->id.proto;
3216 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3217 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3218 um->new_sport = new_sport;
3219 um->old_sport = x->encap->encap_sport;
3220 um->reqid = x->props.reqid;
3221
Johannes Berg053c0952015-01-16 22:09:00 +01003222 nlmsg_end(skb, nlh);
3223 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003224}
3225
3226static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3227 __be16 sport)
3228{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003229 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003230 struct sk_buff *skb;
3231
3232 if (x->id.proto != IPPROTO_ESP)
3233 return -EINVAL;
3234
3235 if (!x->encap)
3236 return -EINVAL;
3237
3238 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3239 if (skb == NULL)
3240 return -ENOMEM;
3241
3242 if (build_mapping(skb, x, ipaddr, sport) < 0)
3243 BUG();
3244
Michal Kubecek21ee5432014-06-03 10:26:06 +02003245 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003246}
3247
Horia Geanta0f245582014-02-12 16:20:06 +02003248static bool xfrm_is_alive(const struct km_event *c)
3249{
3250 return (bool)xfrm_acquire_is_on(c->net);
3251}
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253static struct xfrm_mgr netlink_mgr = {
3254 .id = "netlink",
3255 .notify = xfrm_send_state_notify,
3256 .acquire = xfrm_send_acquire,
3257 .compile_policy = xfrm_compile_policy,
3258 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003259 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003260 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003261 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003262 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263};
3264
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003265static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Patrick McHardybe336902006-03-20 22:40:54 -08003267 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003268 struct netlink_kernel_cfg cfg = {
3269 .groups = XFRMNLGRP_MAX,
3270 .input = xfrm_netlink_rcv,
3271 };
Patrick McHardybe336902006-03-20 22:40:54 -08003272
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003273 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003274 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003276 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003277 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return 0;
3279}
3280
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003281static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003282{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003283 struct net *net;
3284 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003285 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003286 synchronize_net();
3287 list_for_each_entry(net, net_exit_list, exit_list)
3288 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003289}
3290
3291static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003292 .init = xfrm_user_net_init,
3293 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003294};
3295
3296static int __init xfrm_user_init(void)
3297{
3298 int rv;
3299
3300 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3301
3302 rv = register_pernet_subsys(&xfrm_user_net_ops);
3303 if (rv < 0)
3304 return rv;
3305 rv = xfrm_register_km(&netlink_mgr);
3306 if (rv < 0)
3307 unregister_pernet_subsys(&xfrm_user_net_ops);
3308 return rv;
3309}
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311static void __exit xfrm_user_exit(void)
3312{
3313 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003314 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317module_init(xfrm_user_init);
3318module_exit(xfrm_user_exit);
3319MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003320MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003321