blob: 1ece01cd67a4289ea5cc9bd983307ec15e13f766 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/* xfrm_user.c: User interface to configure xfrm engine.
3 *
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 *
6 * Changes:
7 * Mitsuru KANDA @USAGI
8 * Kazunori MIYAZAWA @USAGI
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10 * IPv6 support
11 *
12 */
13
14#include <linux/crypto.h>
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/slab.h>
19#include <linux/socket.h>
20#include <linux/string.h>
21#include <linux/net.h>
22#include <linux/skbuff.h>
23#include <linux/pfkeyv2.h>
24#include <linux/ipsec.h>
25#include <linux/init.h>
26#include <linux/security.h>
27#include <net/sock.h>
28#include <net/xfrm.h>
29#include <net/netlink.h>
30#include <net/ah.h>
31#include <linux/uaccess.h>
32#if IS_ENABLED(CONFIG_IPV6)
33#include <linux/in6.h>
34#endif
35#include <asm/unaligned.h>
36
37static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
38{
39 struct nlattr *rt = attrs[type];
40 struct xfrm_algo *algp;
41
42 if (!rt)
43 return 0;
44
45 algp = nla_data(rt);
46 if (nla_len(rt) < (int)xfrm_alg_len(algp))
47 return -EINVAL;
48
49 switch (type) {
50 case XFRMA_ALG_AUTH:
51 case XFRMA_ALG_CRYPT:
52 case XFRMA_ALG_COMP:
53 break;
54
55 default:
56 return -EINVAL;
57 }
58
59 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
60 return 0;
61}
62
63static int verify_auth_trunc(struct nlattr **attrs)
64{
65 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
66 struct xfrm_algo_auth *algp;
67
68 if (!rt)
69 return 0;
70
71 algp = nla_data(rt);
72 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
73 return -EINVAL;
74
75 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
76 return 0;
77}
78
79static int verify_aead(struct nlattr **attrs)
80{
81 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
82 struct xfrm_algo_aead *algp;
83
84 if (!rt)
85 return 0;
86
87 algp = nla_data(rt);
88 if (nla_len(rt) < (int)aead_len(algp))
89 return -EINVAL;
90
91 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
92 return 0;
93}
94
95static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
96 xfrm_address_t **addrp)
97{
98 struct nlattr *rt = attrs[type];
99
100 if (rt && addrp)
101 *addrp = nla_data(rt);
102}
103
104static inline int verify_sec_ctx_len(struct nlattr **attrs)
105{
106 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
107 struct xfrm_user_sec_ctx *uctx;
108
109 if (!rt)
110 return 0;
111
112 uctx = nla_data(rt);
Olivier Deprez0e641232021-09-23 10:07:05 +0200113 if (uctx->len > nla_len(rt) ||
114 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115 return -EINVAL;
116
117 return 0;
118}
119
120static inline int verify_replay(struct xfrm_usersa_info *p,
121 struct nlattr **attrs)
122{
123 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
124 struct xfrm_replay_state_esn *rs;
125
126 if (!rt)
127 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
128
129 rs = nla_data(rt);
130
131 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
132 return -EINVAL;
133
134 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
135 nla_len(rt) != sizeof(*rs))
136 return -EINVAL;
137
138 /* As only ESP and AH support ESN feature. */
139 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
140 return -EINVAL;
141
142 if (p->replay_window != 0)
143 return -EINVAL;
144
145 return 0;
146}
147
148static int verify_newsa_info(struct xfrm_usersa_info *p,
149 struct nlattr **attrs)
150{
151 int err;
152
153 err = -EINVAL;
154 switch (p->family) {
155 case AF_INET:
David Brazdil0f672f62019-12-10 10:32:29 +0000156 break;
157
158 case AF_INET6:
159#if IS_ENABLED(CONFIG_IPV6)
160 break;
161#else
162 err = -EAFNOSUPPORT;
163 goto out;
164#endif
165
166 default:
167 goto out;
168 }
169
170 switch (p->sel.family) {
171 case AF_UNSPEC:
172 break;
173
174 case AF_INET:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000175 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
176 goto out;
177
178 break;
179
180 case AF_INET6:
181#if IS_ENABLED(CONFIG_IPV6)
182 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
183 goto out;
184
185 break;
186#else
187 err = -EAFNOSUPPORT;
188 goto out;
189#endif
190
191 default:
192 goto out;
193 }
194
195 err = -EINVAL;
196 switch (p->id.proto) {
197 case IPPROTO_AH:
198 if ((!attrs[XFRMA_ALG_AUTH] &&
199 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
200 attrs[XFRMA_ALG_AEAD] ||
201 attrs[XFRMA_ALG_CRYPT] ||
202 attrs[XFRMA_ALG_COMP] ||
203 attrs[XFRMA_TFCPAD])
204 goto out;
205 break;
206
207 case IPPROTO_ESP:
208 if (attrs[XFRMA_ALG_COMP])
209 goto out;
210 if (!attrs[XFRMA_ALG_AUTH] &&
211 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
212 !attrs[XFRMA_ALG_CRYPT] &&
213 !attrs[XFRMA_ALG_AEAD])
214 goto out;
215 if ((attrs[XFRMA_ALG_AUTH] ||
216 attrs[XFRMA_ALG_AUTH_TRUNC] ||
217 attrs[XFRMA_ALG_CRYPT]) &&
218 attrs[XFRMA_ALG_AEAD])
219 goto out;
220 if (attrs[XFRMA_TFCPAD] &&
221 p->mode != XFRM_MODE_TUNNEL)
222 goto out;
223 break;
224
225 case IPPROTO_COMP:
226 if (!attrs[XFRMA_ALG_COMP] ||
227 attrs[XFRMA_ALG_AEAD] ||
228 attrs[XFRMA_ALG_AUTH] ||
229 attrs[XFRMA_ALG_AUTH_TRUNC] ||
230 attrs[XFRMA_ALG_CRYPT] ||
231 attrs[XFRMA_TFCPAD] ||
232 (ntohl(p->id.spi) >= 0x10000))
233 goto out;
234 break;
235
236#if IS_ENABLED(CONFIG_IPV6)
237 case IPPROTO_DSTOPTS:
238 case IPPROTO_ROUTING:
239 if (attrs[XFRMA_ALG_COMP] ||
240 attrs[XFRMA_ALG_AUTH] ||
241 attrs[XFRMA_ALG_AUTH_TRUNC] ||
242 attrs[XFRMA_ALG_AEAD] ||
243 attrs[XFRMA_ALG_CRYPT] ||
244 attrs[XFRMA_ENCAP] ||
245 attrs[XFRMA_SEC_CTX] ||
246 attrs[XFRMA_TFCPAD] ||
247 !attrs[XFRMA_COADDR])
248 goto out;
249 break;
250#endif
251
252 default:
253 goto out;
254 }
255
256 if ((err = verify_aead(attrs)))
257 goto out;
258 if ((err = verify_auth_trunc(attrs)))
259 goto out;
260 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
261 goto out;
262 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
263 goto out;
264 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
265 goto out;
266 if ((err = verify_sec_ctx_len(attrs)))
267 goto out;
268 if ((err = verify_replay(p, attrs)))
269 goto out;
270
271 err = -EINVAL;
272 switch (p->mode) {
273 case XFRM_MODE_TRANSPORT:
274 case XFRM_MODE_TUNNEL:
275 case XFRM_MODE_ROUTEOPTIMIZATION:
276 case XFRM_MODE_BEET:
277 break;
278
279 default:
280 goto out;
281 }
282
283 err = 0;
284
Olivier Deprez157378f2022-04-04 15:47:50 +0200285 if (attrs[XFRMA_MTIMER_THRESH])
286 if (!attrs[XFRMA_ENCAP])
287 err = -EINVAL;
288
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289out:
290 return err;
291}
292
293static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
294 struct xfrm_algo_desc *(*get_byname)(const char *, int),
295 struct nlattr *rta)
296{
297 struct xfrm_algo *p, *ualg;
298 struct xfrm_algo_desc *algo;
299
300 if (!rta)
301 return 0;
302
303 ualg = nla_data(rta);
304
305 algo = get_byname(ualg->alg_name, 1);
306 if (!algo)
307 return -ENOSYS;
308 *props = algo->desc.sadb_alg_id;
309
310 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
311 if (!p)
312 return -ENOMEM;
313
314 strcpy(p->alg_name, algo->name);
315 *algpp = p;
316 return 0;
317}
318
319static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
320{
321 struct xfrm_algo *p, *ualg;
322 struct xfrm_algo_desc *algo;
323
324 if (!rta)
325 return 0;
326
327 ualg = nla_data(rta);
328
329 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
330 if (!algo)
331 return -ENOSYS;
332 x->props.ealgo = algo->desc.sadb_alg_id;
333
334 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
335 if (!p)
336 return -ENOMEM;
337
338 strcpy(p->alg_name, algo->name);
339 x->ealg = p;
340 x->geniv = algo->uinfo.encr.geniv;
341 return 0;
342}
343
344static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
345 struct nlattr *rta)
346{
347 struct xfrm_algo *ualg;
348 struct xfrm_algo_auth *p;
349 struct xfrm_algo_desc *algo;
350
351 if (!rta)
352 return 0;
353
354 ualg = nla_data(rta);
355
356 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
357 if (!algo)
358 return -ENOSYS;
359 *props = algo->desc.sadb_alg_id;
360
361 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
362 if (!p)
363 return -ENOMEM;
364
365 strcpy(p->alg_name, algo->name);
366 p->alg_key_len = ualg->alg_key_len;
367 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
368 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
369
370 *algpp = p;
371 return 0;
372}
373
374static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
375 struct nlattr *rta)
376{
377 struct xfrm_algo_auth *p, *ualg;
378 struct xfrm_algo_desc *algo;
379
380 if (!rta)
381 return 0;
382
383 ualg = nla_data(rta);
384
385 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
386 if (!algo)
387 return -ENOSYS;
388 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
389 return -EINVAL;
390 *props = algo->desc.sadb_alg_id;
391
392 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
393 if (!p)
394 return -ENOMEM;
395
396 strcpy(p->alg_name, algo->name);
397 if (!p->alg_trunc_len)
398 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
399
400 *algpp = p;
401 return 0;
402}
403
404static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
405{
406 struct xfrm_algo_aead *p, *ualg;
407 struct xfrm_algo_desc *algo;
408
409 if (!rta)
410 return 0;
411
412 ualg = nla_data(rta);
413
414 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
415 if (!algo)
416 return -ENOSYS;
417 x->props.ealgo = algo->desc.sadb_alg_id;
418
419 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
420 if (!p)
421 return -ENOMEM;
422
423 strcpy(p->alg_name, algo->name);
424 x->aead = p;
425 x->geniv = algo->uinfo.aead.geniv;
426 return 0;
427}
428
429static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
430 struct nlattr *rp)
431{
432 struct xfrm_replay_state_esn *up;
433 unsigned int ulen;
434
435 if (!replay_esn || !rp)
436 return 0;
437
438 up = nla_data(rp);
439 ulen = xfrm_replay_state_esn_len(up);
440
441 /* Check the overall length and the internal bitmap length to avoid
442 * potential overflow. */
443 if (nla_len(rp) < (int)ulen ||
444 xfrm_replay_state_esn_len(replay_esn) != ulen ||
445 replay_esn->bmp_len != up->bmp_len)
446 return -EINVAL;
447
448 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
449 return -EINVAL;
450
451 return 0;
452}
453
454static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
455 struct xfrm_replay_state_esn **preplay_esn,
456 struct nlattr *rta)
457{
458 struct xfrm_replay_state_esn *p, *pp, *up;
459 unsigned int klen, ulen;
460
461 if (!rta)
462 return 0;
463
464 up = nla_data(rta);
465 klen = xfrm_replay_state_esn_len(up);
466 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
467
468 p = kzalloc(klen, GFP_KERNEL);
469 if (!p)
470 return -ENOMEM;
471
472 pp = kzalloc(klen, GFP_KERNEL);
473 if (!pp) {
474 kfree(p);
475 return -ENOMEM;
476 }
477
478 memcpy(p, up, ulen);
479 memcpy(pp, up, ulen);
480
481 *replay_esn = p;
482 *preplay_esn = pp;
483
484 return 0;
485}
486
487static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
488{
489 unsigned int len = 0;
490
491 if (xfrm_ctx) {
492 len += sizeof(struct xfrm_user_sec_ctx);
493 len += xfrm_ctx->ctx_len;
494 }
495 return len;
496}
497
498static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
499{
500 memcpy(&x->id, &p->id, sizeof(x->id));
501 memcpy(&x->sel, &p->sel, sizeof(x->sel));
502 memcpy(&x->lft, &p->lft, sizeof(x->lft));
503 x->props.mode = p->mode;
504 x->props.replay_window = min_t(unsigned int, p->replay_window,
505 sizeof(x->replay.bitmap) * 8);
506 x->props.reqid = p->reqid;
507 x->props.family = p->family;
508 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
509 x->props.flags = p->flags;
510
511 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
512 x->sel.family = p->family;
513}
514
515/*
516 * someday when pfkey also has support, we could have the code
517 * somehow made shareable and move it to xfrm_state.c - JHS
518 *
519*/
520static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
521 int update_esn)
522{
523 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
524 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
525 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
526 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
527 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Olivier Deprez157378f2022-04-04 15:47:50 +0200528 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000529
530 if (re) {
531 struct xfrm_replay_state_esn *replay_esn;
532 replay_esn = nla_data(re);
533 memcpy(x->replay_esn, replay_esn,
534 xfrm_replay_state_esn_len(replay_esn));
535 memcpy(x->preplay_esn, replay_esn,
536 xfrm_replay_state_esn_len(replay_esn));
537 }
538
539 if (rp) {
540 struct xfrm_replay_state *replay;
541 replay = nla_data(rp);
542 memcpy(&x->replay, replay, sizeof(*replay));
543 memcpy(&x->preplay, replay, sizeof(*replay));
544 }
545
546 if (lt) {
547 struct xfrm_lifetime_cur *ltime;
548 ltime = nla_data(lt);
549 x->curlft.bytes = ltime->bytes;
550 x->curlft.packets = ltime->packets;
551 x->curlft.add_time = ltime->add_time;
552 x->curlft.use_time = ltime->use_time;
553 }
554
555 if (et)
556 x->replay_maxage = nla_get_u32(et);
557
558 if (rt)
559 x->replay_maxdiff = nla_get_u32(rt);
Olivier Deprez157378f2022-04-04 15:47:50 +0200560
561 if (mt)
562 x->mapping_maxage = nla_get_u32(mt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563}
564
565static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
566{
567 if (attrs[XFRMA_SET_MARK]) {
568 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
569 if (attrs[XFRMA_SET_MARK_MASK])
570 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
571 else
572 m->m = 0xffffffff;
573 } else {
574 m->v = m->m = 0;
575 }
576}
577
578static struct xfrm_state *xfrm_state_construct(struct net *net,
579 struct xfrm_usersa_info *p,
580 struct nlattr **attrs,
581 int *errp)
582{
583 struct xfrm_state *x = xfrm_state_alloc(net);
584 int err = -ENOMEM;
585
586 if (!x)
587 goto error_no_put;
588
589 copy_from_user_state(x, p);
590
Olivier Deprez0e641232021-09-23 10:07:05 +0200591 if (attrs[XFRMA_ENCAP]) {
592 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
593 sizeof(*x->encap), GFP_KERNEL);
594 if (x->encap == NULL)
595 goto error;
596 }
597
598 if (attrs[XFRMA_COADDR]) {
599 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
600 sizeof(*x->coaddr), GFP_KERNEL);
601 if (x->coaddr == NULL)
602 goto error;
603 }
604
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000605 if (attrs[XFRMA_SA_EXTRA_FLAGS])
606 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
607
608 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
609 goto error;
610 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
611 attrs[XFRMA_ALG_AUTH_TRUNC])))
612 goto error;
613 if (!x->props.aalgo) {
614 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
615 attrs[XFRMA_ALG_AUTH])))
616 goto error;
617 }
618 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
619 goto error;
620 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
621 xfrm_calg_get_byname,
622 attrs[XFRMA_ALG_COMP])))
623 goto error;
624
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000625 if (attrs[XFRMA_TFCPAD])
626 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
627
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000628 xfrm_mark_get(attrs, &x->mark);
629
630 xfrm_smark_init(attrs, &x->props.smark);
631
632 if (attrs[XFRMA_IF_ID])
633 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
634
635 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
636 if (err)
637 goto error;
638
639 if (attrs[XFRMA_SEC_CTX]) {
640 err = security_xfrm_state_alloc(x,
641 nla_data(attrs[XFRMA_SEC_CTX]));
642 if (err)
643 goto error;
644 }
645
646 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
647 attrs[XFRMA_REPLAY_ESN_VAL])))
648 goto error;
649
650 x->km.seq = p->seq;
651 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
652 /* sysctl_xfrm_aevent_etime is in 100ms units */
653 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
654
655 if ((err = xfrm_init_replay(x)))
656 goto error;
657
658 /* override default values from above */
659 xfrm_update_ae_params(x, attrs, 0);
660
661 /* configure the hardware if offload is requested */
662 if (attrs[XFRMA_OFFLOAD_DEV]) {
663 err = xfrm_dev_state_add(net, x,
664 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
665 if (err)
666 goto error;
667 }
668
669 return x;
670
671error:
672 x->km.state = XFRM_STATE_DEAD;
673 xfrm_state_put(x);
674error_no_put:
675 *errp = err;
676 return NULL;
677}
678
679static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
680 struct nlattr **attrs)
681{
682 struct net *net = sock_net(skb->sk);
683 struct xfrm_usersa_info *p = nlmsg_data(nlh);
684 struct xfrm_state *x;
685 int err;
686 struct km_event c;
687
688 err = verify_newsa_info(p, attrs);
689 if (err)
690 return err;
691
692 x = xfrm_state_construct(net, p, attrs, &err);
693 if (!x)
694 return err;
695
696 xfrm_state_hold(x);
697 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
698 err = xfrm_state_add(x);
699 else
700 err = xfrm_state_update(x);
701
702 xfrm_audit_state_add(x, err ? 0 : 1, true);
703
704 if (err < 0) {
705 x->km.state = XFRM_STATE_DEAD;
706 xfrm_dev_state_delete(x);
707 __xfrm_state_put(x);
708 goto out;
709 }
710
711 if (x->km.state == XFRM_STATE_VOID)
712 x->km.state = XFRM_STATE_VALID;
713
714 c.seq = nlh->nlmsg_seq;
715 c.portid = nlh->nlmsg_pid;
716 c.event = nlh->nlmsg_type;
717
718 km_state_notify(x, &c);
719out:
720 xfrm_state_put(x);
721 return err;
722}
723
724static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
725 struct xfrm_usersa_id *p,
726 struct nlattr **attrs,
727 int *errp)
728{
729 struct xfrm_state *x = NULL;
730 struct xfrm_mark m;
731 int err;
732 u32 mark = xfrm_mark_get(attrs, &m);
733
734 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
735 err = -ESRCH;
736 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
737 } else {
738 xfrm_address_t *saddr = NULL;
739
740 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
741 if (!saddr) {
742 err = -EINVAL;
743 goto out;
744 }
745
746 err = -ESRCH;
747 x = xfrm_state_lookup_byaddr(net, mark,
748 &p->daddr, saddr,
749 p->proto, p->family);
750 }
751
752 out:
753 if (!x && errp)
754 *errp = err;
755 return x;
756}
757
758static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
759 struct nlattr **attrs)
760{
761 struct net *net = sock_net(skb->sk);
762 struct xfrm_state *x;
763 int err = -ESRCH;
764 struct km_event c;
765 struct xfrm_usersa_id *p = nlmsg_data(nlh);
766
767 x = xfrm_user_state_lookup(net, p, attrs, &err);
768 if (x == NULL)
769 return err;
770
771 if ((err = security_xfrm_state_delete(x)) != 0)
772 goto out;
773
774 if (xfrm_state_kern(x)) {
775 err = -EPERM;
776 goto out;
777 }
778
779 err = xfrm_state_delete(x);
780
781 if (err < 0)
782 goto out;
783
784 c.seq = nlh->nlmsg_seq;
785 c.portid = nlh->nlmsg_pid;
786 c.event = nlh->nlmsg_type;
787 km_state_notify(x, &c);
788
789out:
790 xfrm_audit_state_delete(x, err ? 0 : 1, true);
791 xfrm_state_put(x);
792 return err;
793}
794
795static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
796{
797 memset(p, 0, sizeof(*p));
798 memcpy(&p->id, &x->id, sizeof(p->id));
799 memcpy(&p->sel, &x->sel, sizeof(p->sel));
800 memcpy(&p->lft, &x->lft, sizeof(p->lft));
801 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
802 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
803 put_unaligned(x->stats.replay, &p->stats.replay);
804 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
805 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
806 p->mode = x->props.mode;
807 p->replay_window = x->props.replay_window;
808 p->reqid = x->props.reqid;
809 p->family = x->props.family;
810 p->flags = x->props.flags;
811 p->seq = x->km.seq;
812}
813
814struct xfrm_dump_info {
815 struct sk_buff *in_skb;
816 struct sk_buff *out_skb;
817 u32 nlmsg_seq;
818 u16 nlmsg_flags;
819};
820
821static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
822{
823 struct xfrm_user_sec_ctx *uctx;
824 struct nlattr *attr;
825 int ctx_size = sizeof(*uctx) + s->ctx_len;
826
827 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
828 if (attr == NULL)
829 return -EMSGSIZE;
830
831 uctx = nla_data(attr);
832 uctx->exttype = XFRMA_SEC_CTX;
833 uctx->len = ctx_size;
834 uctx->ctx_doi = s->ctx_doi;
835 uctx->ctx_alg = s->ctx_alg;
836 uctx->ctx_len = s->ctx_len;
837 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
838
839 return 0;
840}
841
842static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
843{
844 struct xfrm_user_offload *xuo;
845 struct nlattr *attr;
846
847 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
848 if (attr == NULL)
849 return -EMSGSIZE;
850
851 xuo = nla_data(attr);
852 memset(xuo, 0, sizeof(*xuo));
853 xuo->ifindex = xso->dev->ifindex;
854 xuo->flags = xso->flags;
855
856 return 0;
857}
858
859static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
860{
861 struct xfrm_algo *algo;
862 struct nlattr *nla;
863
864 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
865 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
866 if (!nla)
867 return -EMSGSIZE;
868
869 algo = nla_data(nla);
870 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
871 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
872 algo->alg_key_len = auth->alg_key_len;
873
874 return 0;
875}
876
877static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
878{
879 int ret = 0;
880
881 if (m->v | m->m) {
882 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
883 if (!ret)
884 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
885 }
886 return ret;
887}
888
889/* Don't change this without updating xfrm_sa_len! */
890static int copy_to_user_state_extra(struct xfrm_state *x,
891 struct xfrm_usersa_info *p,
892 struct sk_buff *skb)
893{
894 int ret = 0;
895
896 copy_to_user_state(x, p);
897
898 if (x->props.extra_flags) {
899 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
900 x->props.extra_flags);
901 if (ret)
902 goto out;
903 }
904
905 if (x->coaddr) {
906 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
907 if (ret)
908 goto out;
909 }
910 if (x->lastused) {
911 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
912 XFRMA_PAD);
913 if (ret)
914 goto out;
915 }
916 if (x->aead) {
917 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
918 if (ret)
919 goto out;
920 }
921 if (x->aalg) {
922 ret = copy_to_user_auth(x->aalg, skb);
923 if (!ret)
924 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
925 xfrm_alg_auth_len(x->aalg), x->aalg);
926 if (ret)
927 goto out;
928 }
929 if (x->ealg) {
930 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
931 if (ret)
932 goto out;
933 }
934 if (x->calg) {
935 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
936 if (ret)
937 goto out;
938 }
939 if (x->encap) {
940 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
941 if (ret)
942 goto out;
943 }
944 if (x->tfcpad) {
945 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
946 if (ret)
947 goto out;
948 }
949 ret = xfrm_mark_put(skb, &x->mark);
950 if (ret)
951 goto out;
952
953 ret = xfrm_smark_put(skb, &x->props.smark);
954 if (ret)
955 goto out;
956
957 if (x->replay_esn)
958 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
959 xfrm_replay_state_esn_len(x->replay_esn),
960 x->replay_esn);
961 else
962 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
963 &x->replay);
964 if (ret)
965 goto out;
966 if(x->xso.dev)
967 ret = copy_user_offload(&x->xso, skb);
968 if (ret)
969 goto out;
970 if (x->if_id) {
971 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
972 if (ret)
973 goto out;
974 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200975 if (x->security) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000976 ret = copy_sec_ctx(x->security, skb);
Olivier Deprez157378f2022-04-04 15:47:50 +0200977 if (ret)
978 goto out;
979 }
980 if (x->mapping_maxage)
981 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000982out:
983 return ret;
984}
985
986static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
987{
988 struct xfrm_dump_info *sp = ptr;
989 struct sk_buff *in_skb = sp->in_skb;
990 struct sk_buff *skb = sp->out_skb;
Olivier Deprez157378f2022-04-04 15:47:50 +0200991 struct xfrm_translator *xtr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000992 struct xfrm_usersa_info *p;
993 struct nlmsghdr *nlh;
994 int err;
995
996 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
997 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
998 if (nlh == NULL)
999 return -EMSGSIZE;
1000
1001 p = nlmsg_data(nlh);
1002
1003 err = copy_to_user_state_extra(x, p, skb);
1004 if (err) {
1005 nlmsg_cancel(skb, nlh);
1006 return err;
1007 }
1008 nlmsg_end(skb, nlh);
Olivier Deprez157378f2022-04-04 15:47:50 +02001009
1010 xtr = xfrm_get_translator();
1011 if (xtr) {
1012 err = xtr->alloc_compat(skb, nlh);
1013
1014 xfrm_put_translator(xtr);
1015 if (err) {
1016 nlmsg_cancel(skb, nlh);
1017 return err;
1018 }
1019 }
1020
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001021 return 0;
1022}
1023
1024static int xfrm_dump_sa_done(struct netlink_callback *cb)
1025{
1026 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1027 struct sock *sk = cb->skb->sk;
1028 struct net *net = sock_net(sk);
1029
1030 if (cb->args[0])
1031 xfrm_state_walk_done(walk, net);
1032 return 0;
1033}
1034
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1036{
1037 struct net *net = sock_net(skb->sk);
1038 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1039 struct xfrm_dump_info info;
1040
1041 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1042 sizeof(cb->args) - sizeof(cb->args[0]));
1043
1044 info.in_skb = cb->skb;
1045 info.out_skb = skb;
1046 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1047 info.nlmsg_flags = NLM_F_MULTI;
1048
1049 if (!cb->args[0]) {
1050 struct nlattr *attrs[XFRMA_MAX+1];
1051 struct xfrm_address_filter *filter = NULL;
1052 u8 proto = 0;
1053 int err;
1054
David Brazdil0f672f62019-12-10 10:32:29 +00001055 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1056 xfrma_policy, cb->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057 if (err < 0)
1058 return err;
1059
1060 if (attrs[XFRMA_ADDRESS_FILTER]) {
1061 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1062 sizeof(*filter), GFP_KERNEL);
1063 if (filter == NULL)
1064 return -ENOMEM;
1065 }
1066
1067 if (attrs[XFRMA_PROTO])
1068 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1069
1070 xfrm_state_walk_init(walk, proto, filter);
1071 cb->args[0] = 1;
1072 }
1073
1074 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1075
1076 return skb->len;
1077}
1078
1079static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1080 struct xfrm_state *x, u32 seq)
1081{
1082 struct xfrm_dump_info info;
1083 struct sk_buff *skb;
1084 int err;
1085
1086 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1087 if (!skb)
1088 return ERR_PTR(-ENOMEM);
1089
1090 info.in_skb = in_skb;
1091 info.out_skb = skb;
1092 info.nlmsg_seq = seq;
1093 info.nlmsg_flags = 0;
1094
1095 err = dump_one_state(x, 0, &info);
1096 if (err) {
1097 kfree_skb(skb);
1098 return ERR_PTR(err);
1099 }
1100
1101 return skb;
1102}
1103
1104/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1105 * Must be called with RCU read lock.
1106 */
1107static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1108 u32 pid, unsigned int group)
1109{
1110 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
Olivier Deprez157378f2022-04-04 15:47:50 +02001111 struct xfrm_translator *xtr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001112
1113 if (!nlsk) {
1114 kfree_skb(skb);
1115 return -EPIPE;
1116 }
1117
Olivier Deprez157378f2022-04-04 15:47:50 +02001118 xtr = xfrm_get_translator();
1119 if (xtr) {
1120 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1121
1122 xfrm_put_translator(xtr);
1123 if (err) {
1124 kfree_skb(skb);
1125 return err;
1126 }
1127 }
1128
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001129 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1130}
1131
1132static inline unsigned int xfrm_spdinfo_msgsize(void)
1133{
1134 return NLMSG_ALIGN(4)
1135 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1136 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1137 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1138 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1139}
1140
1141static int build_spdinfo(struct sk_buff *skb, struct net *net,
1142 u32 portid, u32 seq, u32 flags)
1143{
1144 struct xfrmk_spdinfo si;
1145 struct xfrmu_spdinfo spc;
1146 struct xfrmu_spdhinfo sph;
1147 struct xfrmu_spdhthresh spt4, spt6;
1148 struct nlmsghdr *nlh;
1149 int err;
1150 u32 *f;
1151 unsigned lseq;
1152
1153 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1154 if (nlh == NULL) /* shouldn't really happen ... */
1155 return -EMSGSIZE;
1156
1157 f = nlmsg_data(nlh);
1158 *f = flags;
1159 xfrm_spd_getinfo(net, &si);
1160 spc.incnt = si.incnt;
1161 spc.outcnt = si.outcnt;
1162 spc.fwdcnt = si.fwdcnt;
1163 spc.inscnt = si.inscnt;
1164 spc.outscnt = si.outscnt;
1165 spc.fwdscnt = si.fwdscnt;
1166 sph.spdhcnt = si.spdhcnt;
1167 sph.spdhmcnt = si.spdhmcnt;
1168
1169 do {
1170 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1171
1172 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1173 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1174 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1175 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1176 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1177
1178 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1179 if (!err)
1180 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1181 if (!err)
1182 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1183 if (!err)
1184 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1185 if (err) {
1186 nlmsg_cancel(skb, nlh);
1187 return err;
1188 }
1189
1190 nlmsg_end(skb, nlh);
1191 return 0;
1192}
1193
1194static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1195 struct nlattr **attrs)
1196{
1197 struct net *net = sock_net(skb->sk);
1198 struct xfrmu_spdhthresh *thresh4 = NULL;
1199 struct xfrmu_spdhthresh *thresh6 = NULL;
1200
1201 /* selector prefixlen thresholds to hash policies */
1202 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1203 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1204
1205 if (nla_len(rta) < sizeof(*thresh4))
1206 return -EINVAL;
1207 thresh4 = nla_data(rta);
1208 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1209 return -EINVAL;
1210 }
1211 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1212 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1213
1214 if (nla_len(rta) < sizeof(*thresh6))
1215 return -EINVAL;
1216 thresh6 = nla_data(rta);
1217 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1218 return -EINVAL;
1219 }
1220
1221 if (thresh4 || thresh6) {
1222 write_seqlock(&net->xfrm.policy_hthresh.lock);
1223 if (thresh4) {
1224 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1225 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1226 }
1227 if (thresh6) {
1228 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1229 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1230 }
1231 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1232
1233 xfrm_policy_hash_rebuild(net);
1234 }
1235
1236 return 0;
1237}
1238
1239static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1240 struct nlattr **attrs)
1241{
1242 struct net *net = sock_net(skb->sk);
1243 struct sk_buff *r_skb;
1244 u32 *flags = nlmsg_data(nlh);
1245 u32 sportid = NETLINK_CB(skb).portid;
1246 u32 seq = nlh->nlmsg_seq;
1247 int err;
1248
1249 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1250 if (r_skb == NULL)
1251 return -ENOMEM;
1252
1253 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1254 BUG_ON(err < 0);
1255
1256 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1257}
1258
1259static inline unsigned int xfrm_sadinfo_msgsize(void)
1260{
1261 return NLMSG_ALIGN(4)
1262 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1263 + nla_total_size(4); /* XFRMA_SAD_CNT */
1264}
1265
1266static int build_sadinfo(struct sk_buff *skb, struct net *net,
1267 u32 portid, u32 seq, u32 flags)
1268{
1269 struct xfrmk_sadinfo si;
1270 struct xfrmu_sadhinfo sh;
1271 struct nlmsghdr *nlh;
1272 int err;
1273 u32 *f;
1274
1275 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1276 if (nlh == NULL) /* shouldn't really happen ... */
1277 return -EMSGSIZE;
1278
1279 f = nlmsg_data(nlh);
1280 *f = flags;
1281 xfrm_sad_getinfo(net, &si);
1282
1283 sh.sadhmcnt = si.sadhmcnt;
1284 sh.sadhcnt = si.sadhcnt;
1285
1286 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1287 if (!err)
1288 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1289 if (err) {
1290 nlmsg_cancel(skb, nlh);
1291 return err;
1292 }
1293
1294 nlmsg_end(skb, nlh);
1295 return 0;
1296}
1297
1298static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1299 struct nlattr **attrs)
1300{
1301 struct net *net = sock_net(skb->sk);
1302 struct sk_buff *r_skb;
1303 u32 *flags = nlmsg_data(nlh);
1304 u32 sportid = NETLINK_CB(skb).portid;
1305 u32 seq = nlh->nlmsg_seq;
1306 int err;
1307
1308 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1309 if (r_skb == NULL)
1310 return -ENOMEM;
1311
1312 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1313 BUG_ON(err < 0);
1314
1315 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1316}
1317
1318static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1319 struct nlattr **attrs)
1320{
1321 struct net *net = sock_net(skb->sk);
1322 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1323 struct xfrm_state *x;
1324 struct sk_buff *resp_skb;
1325 int err = -ESRCH;
1326
1327 x = xfrm_user_state_lookup(net, p, attrs, &err);
1328 if (x == NULL)
1329 goto out_noput;
1330
1331 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1332 if (IS_ERR(resp_skb)) {
1333 err = PTR_ERR(resp_skb);
1334 } else {
1335 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1336 }
1337 xfrm_state_put(x);
1338out_noput:
1339 return err;
1340}
1341
1342static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1343 struct nlattr **attrs)
1344{
1345 struct net *net = sock_net(skb->sk);
1346 struct xfrm_state *x;
1347 struct xfrm_userspi_info *p;
Olivier Deprez157378f2022-04-04 15:47:50 +02001348 struct xfrm_translator *xtr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001349 struct sk_buff *resp_skb;
1350 xfrm_address_t *daddr;
1351 int family;
1352 int err;
1353 u32 mark;
1354 struct xfrm_mark m;
1355 u32 if_id = 0;
1356
1357 p = nlmsg_data(nlh);
1358 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1359 if (err)
1360 goto out_noput;
1361
1362 family = p->info.family;
1363 daddr = &p->info.id.daddr;
1364
1365 x = NULL;
1366
1367 mark = xfrm_mark_get(attrs, &m);
1368
1369 if (attrs[XFRMA_IF_ID])
1370 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1371
1372 if (p->info.seq) {
1373 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1374 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1375 xfrm_state_put(x);
1376 x = NULL;
1377 }
1378 }
1379
1380 if (!x)
1381 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1382 if_id, p->info.id.proto, daddr,
1383 &p->info.saddr, 1,
1384 family);
1385 err = -ENOENT;
1386 if (x == NULL)
1387 goto out_noput;
1388
1389 err = xfrm_alloc_spi(x, p->min, p->max);
1390 if (err)
1391 goto out;
1392
1393 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1394 if (IS_ERR(resp_skb)) {
1395 err = PTR_ERR(resp_skb);
1396 goto out;
1397 }
1398
Olivier Deprez157378f2022-04-04 15:47:50 +02001399 xtr = xfrm_get_translator();
1400 if (xtr) {
1401 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1402
1403 xfrm_put_translator(xtr);
1404 if (err) {
1405 kfree_skb(resp_skb);
1406 goto out;
1407 }
1408 }
1409
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001410 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1411
1412out:
1413 xfrm_state_put(x);
1414out_noput:
1415 return err;
1416}
1417
1418static int verify_policy_dir(u8 dir)
1419{
1420 switch (dir) {
1421 case XFRM_POLICY_IN:
1422 case XFRM_POLICY_OUT:
1423 case XFRM_POLICY_FWD:
1424 break;
1425
1426 default:
1427 return -EINVAL;
1428 }
1429
1430 return 0;
1431}
1432
1433static int verify_policy_type(u8 type)
1434{
1435 switch (type) {
1436 case XFRM_POLICY_TYPE_MAIN:
1437#ifdef CONFIG_XFRM_SUB_POLICY
1438 case XFRM_POLICY_TYPE_SUB:
1439#endif
1440 break;
1441
1442 default:
1443 return -EINVAL;
1444 }
1445
1446 return 0;
1447}
1448
1449static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1450{
1451 int ret;
1452
1453 switch (p->share) {
1454 case XFRM_SHARE_ANY:
1455 case XFRM_SHARE_SESSION:
1456 case XFRM_SHARE_USER:
1457 case XFRM_SHARE_UNIQUE:
1458 break;
1459
1460 default:
1461 return -EINVAL;
1462 }
1463
1464 switch (p->action) {
1465 case XFRM_POLICY_ALLOW:
1466 case XFRM_POLICY_BLOCK:
1467 break;
1468
1469 default:
1470 return -EINVAL;
1471 }
1472
1473 switch (p->sel.family) {
1474 case AF_INET:
1475 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1476 return -EINVAL;
1477
1478 break;
1479
1480 case AF_INET6:
1481#if IS_ENABLED(CONFIG_IPV6)
1482 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1483 return -EINVAL;
1484
1485 break;
1486#else
1487 return -EAFNOSUPPORT;
1488#endif
1489
1490 default:
1491 return -EINVAL;
1492 }
1493
1494 ret = verify_policy_dir(p->dir);
1495 if (ret)
1496 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +00001497 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001498 return -EINVAL;
1499
1500 return 0;
1501}
1502
1503static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1504{
1505 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1506 struct xfrm_user_sec_ctx *uctx;
1507
1508 if (!rt)
1509 return 0;
1510
1511 uctx = nla_data(rt);
1512 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1513}
1514
1515static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1516 int nr)
1517{
1518 int i;
1519
1520 xp->xfrm_nr = nr;
1521 for (i = 0; i < nr; i++, ut++) {
1522 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1523
1524 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1525 memcpy(&t->saddr, &ut->saddr,
1526 sizeof(xfrm_address_t));
1527 t->reqid = ut->reqid;
1528 t->mode = ut->mode;
1529 t->share = ut->share;
1530 t->optional = ut->optional;
1531 t->aalgos = ut->aalgos;
1532 t->ealgos = ut->ealgos;
1533 t->calgos = ut->calgos;
1534 /* If all masks are ~0, then we allow all algorithms. */
1535 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1536 t->encap_family = ut->family;
1537 }
1538}
1539
1540static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1541{
1542 u16 prev_family;
1543 int i;
1544
1545 if (nr > XFRM_MAX_DEPTH)
1546 return -EINVAL;
1547
1548 prev_family = family;
1549
1550 for (i = 0; i < nr; i++) {
1551 /* We never validated the ut->family value, so many
1552 * applications simply leave it at zero. The check was
1553 * never made and ut->family was ignored because all
1554 * templates could be assumed to have the same family as
1555 * the policy itself. Now that we will have ipv4-in-ipv6
1556 * and ipv6-in-ipv4 tunnels, this is no longer true.
1557 */
1558 if (!ut[i].family)
1559 ut[i].family = family;
1560
David Brazdil0f672f62019-12-10 10:32:29 +00001561 switch (ut[i].mode) {
1562 case XFRM_MODE_TUNNEL:
1563 case XFRM_MODE_BEET:
1564 break;
1565 default:
1566 if (ut[i].family != prev_family)
1567 return -EINVAL;
1568 break;
1569 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001570 if (ut[i].mode >= XFRM_MODE_MAX)
1571 return -EINVAL;
1572
1573 prev_family = ut[i].family;
1574
1575 switch (ut[i].family) {
1576 case AF_INET:
1577 break;
1578#if IS_ENABLED(CONFIG_IPV6)
1579 case AF_INET6:
1580 break;
1581#endif
1582 default:
1583 return -EINVAL;
1584 }
1585
David Brazdil0f672f62019-12-10 10:32:29 +00001586 if (!xfrm_id_proto_valid(ut[i].id.proto))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001587 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001588 }
1589
1590 return 0;
1591}
1592
1593static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1594{
1595 struct nlattr *rt = attrs[XFRMA_TMPL];
1596
1597 if (!rt) {
1598 pol->xfrm_nr = 0;
1599 } else {
1600 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1601 int nr = nla_len(rt) / sizeof(*utmpl);
1602 int err;
1603
1604 err = validate_tmpl(nr, utmpl, pol->family);
1605 if (err)
1606 return err;
1607
1608 copy_templates(pol, utmpl, nr);
1609 }
1610 return 0;
1611}
1612
1613static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1614{
1615 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1616 struct xfrm_userpolicy_type *upt;
1617 u8 type = XFRM_POLICY_TYPE_MAIN;
1618 int err;
1619
1620 if (rt) {
1621 upt = nla_data(rt);
1622 type = upt->type;
1623 }
1624
1625 err = verify_policy_type(type);
1626 if (err)
1627 return err;
1628
1629 *tp = type;
1630 return 0;
1631}
1632
1633static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1634{
1635 xp->priority = p->priority;
1636 xp->index = p->index;
1637 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1638 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1639 xp->action = p->action;
1640 xp->flags = p->flags;
1641 xp->family = p->sel.family;
1642 /* XXX xp->share = p->share; */
1643}
1644
1645static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1646{
1647 memset(p, 0, sizeof(*p));
1648 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1649 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1650 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1651 p->priority = xp->priority;
1652 p->index = xp->index;
1653 p->sel.family = xp->family;
1654 p->dir = dir;
1655 p->action = xp->action;
1656 p->flags = xp->flags;
1657 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1658}
1659
1660static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1661{
1662 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1663 int err;
1664
1665 if (!xp) {
1666 *errp = -ENOMEM;
1667 return NULL;
1668 }
1669
1670 copy_from_user_policy(xp, p);
1671
1672 err = copy_from_user_policy_type(&xp->type, attrs);
1673 if (err)
1674 goto error;
1675
1676 if (!(err = copy_from_user_tmpl(xp, attrs)))
1677 err = copy_from_user_sec_ctx(xp, attrs);
1678 if (err)
1679 goto error;
1680
1681 xfrm_mark_get(attrs, &xp->mark);
1682
1683 if (attrs[XFRMA_IF_ID])
1684 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1685
1686 return xp;
1687 error:
1688 *errp = err;
1689 xp->walk.dead = 1;
1690 xfrm_policy_destroy(xp);
1691 return NULL;
1692}
1693
1694static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1695 struct nlattr **attrs)
1696{
1697 struct net *net = sock_net(skb->sk);
1698 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1699 struct xfrm_policy *xp;
1700 struct km_event c;
1701 int err;
1702 int excl;
1703
1704 err = verify_newpolicy_info(p);
1705 if (err)
1706 return err;
1707 err = verify_sec_ctx_len(attrs);
1708 if (err)
1709 return err;
1710
1711 xp = xfrm_policy_construct(net, p, attrs, &err);
1712 if (!xp)
1713 return err;
1714
1715 /* shouldn't excl be based on nlh flags??
1716 * Aha! this is anti-netlink really i.e more pfkey derived
1717 * in netlink excl is a flag and you wouldnt need
1718 * a type XFRM_MSG_UPDPOLICY - JHS */
1719 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1720 err = xfrm_policy_insert(p->dir, xp, excl);
1721 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1722
1723 if (err) {
1724 security_xfrm_policy_free(xp->security);
1725 kfree(xp);
1726 return err;
1727 }
1728
1729 c.event = nlh->nlmsg_type;
1730 c.seq = nlh->nlmsg_seq;
1731 c.portid = nlh->nlmsg_pid;
1732 km_policy_notify(xp, p->dir, &c);
1733
1734 xfrm_pol_put(xp);
1735
1736 return 0;
1737}
1738
1739static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1740{
1741 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1742 int i;
1743
1744 if (xp->xfrm_nr == 0)
1745 return 0;
1746
1747 for (i = 0; i < xp->xfrm_nr; i++) {
1748 struct xfrm_user_tmpl *up = &vec[i];
1749 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1750
1751 memset(up, 0, sizeof(*up));
1752 memcpy(&up->id, &kp->id, sizeof(up->id));
1753 up->family = kp->encap_family;
1754 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1755 up->reqid = kp->reqid;
1756 up->mode = kp->mode;
1757 up->share = kp->share;
1758 up->optional = kp->optional;
1759 up->aalgos = kp->aalgos;
1760 up->ealgos = kp->ealgos;
1761 up->calgos = kp->calgos;
1762 }
1763
1764 return nla_put(skb, XFRMA_TMPL,
1765 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1766}
1767
1768static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1769{
1770 if (x->security) {
1771 return copy_sec_ctx(x->security, skb);
1772 }
1773 return 0;
1774}
1775
1776static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1777{
1778 if (xp->security)
1779 return copy_sec_ctx(xp->security, skb);
1780 return 0;
1781}
1782static inline unsigned int userpolicy_type_attrsize(void)
1783{
1784#ifdef CONFIG_XFRM_SUB_POLICY
1785 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1786#else
1787 return 0;
1788#endif
1789}
1790
1791#ifdef CONFIG_XFRM_SUB_POLICY
1792static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1793{
1794 struct xfrm_userpolicy_type upt;
1795
1796 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1797 memset(&upt, 0, sizeof(upt));
1798 upt.type = type;
1799
1800 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1801}
1802
1803#else
1804static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1805{
1806 return 0;
1807}
1808#endif
1809
1810static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1811{
1812 struct xfrm_dump_info *sp = ptr;
1813 struct xfrm_userpolicy_info *p;
1814 struct sk_buff *in_skb = sp->in_skb;
1815 struct sk_buff *skb = sp->out_skb;
Olivier Deprez157378f2022-04-04 15:47:50 +02001816 struct xfrm_translator *xtr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001817 struct nlmsghdr *nlh;
1818 int err;
1819
1820 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1821 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1822 if (nlh == NULL)
1823 return -EMSGSIZE;
1824
1825 p = nlmsg_data(nlh);
1826 copy_to_user_policy(xp, p, dir);
1827 err = copy_to_user_tmpl(xp, skb);
1828 if (!err)
1829 err = copy_to_user_sec_ctx(xp, skb);
1830 if (!err)
1831 err = copy_to_user_policy_type(xp->type, skb);
1832 if (!err)
1833 err = xfrm_mark_put(skb, &xp->mark);
1834 if (!err)
1835 err = xfrm_if_id_put(skb, xp->if_id);
1836 if (err) {
1837 nlmsg_cancel(skb, nlh);
1838 return err;
1839 }
1840 nlmsg_end(skb, nlh);
Olivier Deprez157378f2022-04-04 15:47:50 +02001841
1842 xtr = xfrm_get_translator();
1843 if (xtr) {
1844 err = xtr->alloc_compat(skb, nlh);
1845
1846 xfrm_put_translator(xtr);
1847 if (err) {
1848 nlmsg_cancel(skb, nlh);
1849 return err;
1850 }
1851 }
1852
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001853 return 0;
1854}
1855
1856static int xfrm_dump_policy_done(struct netlink_callback *cb)
1857{
1858 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1859 struct net *net = sock_net(cb->skb->sk);
1860
1861 xfrm_policy_walk_done(walk, net);
1862 return 0;
1863}
1864
1865static int xfrm_dump_policy_start(struct netlink_callback *cb)
1866{
1867 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1868
1869 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1870
1871 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1872 return 0;
1873}
1874
1875static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1876{
1877 struct net *net = sock_net(skb->sk);
1878 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1879 struct xfrm_dump_info info;
1880
1881 info.in_skb = cb->skb;
1882 info.out_skb = skb;
1883 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1884 info.nlmsg_flags = NLM_F_MULTI;
1885
1886 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1887
1888 return skb->len;
1889}
1890
1891static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1892 struct xfrm_policy *xp,
1893 int dir, u32 seq)
1894{
1895 struct xfrm_dump_info info;
1896 struct sk_buff *skb;
1897 int err;
1898
1899 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1900 if (!skb)
1901 return ERR_PTR(-ENOMEM);
1902
1903 info.in_skb = in_skb;
1904 info.out_skb = skb;
1905 info.nlmsg_seq = seq;
1906 info.nlmsg_flags = 0;
1907
1908 err = dump_one_policy(xp, dir, 0, &info);
1909 if (err) {
1910 kfree_skb(skb);
1911 return ERR_PTR(err);
1912 }
1913
1914 return skb;
1915}
1916
1917static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1918 struct nlattr **attrs)
1919{
1920 struct net *net = sock_net(skb->sk);
1921 struct xfrm_policy *xp;
1922 struct xfrm_userpolicy_id *p;
1923 u8 type = XFRM_POLICY_TYPE_MAIN;
1924 int err;
1925 struct km_event c;
1926 int delete;
1927 struct xfrm_mark m;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001928 u32 if_id = 0;
1929
1930 p = nlmsg_data(nlh);
1931 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1932
1933 err = copy_from_user_policy_type(&type, attrs);
1934 if (err)
1935 return err;
1936
1937 err = verify_policy_dir(p->dir);
1938 if (err)
1939 return err;
1940
1941 if (attrs[XFRMA_IF_ID])
1942 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1943
Olivier Deprez0e641232021-09-23 10:07:05 +02001944 xfrm_mark_get(attrs, &m);
1945
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001946 if (p->index)
Olivier Deprez0e641232021-09-23 10:07:05 +02001947 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
1948 p->index, delete, &err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001949 else {
1950 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1951 struct xfrm_sec_ctx *ctx;
1952
1953 err = verify_sec_ctx_len(attrs);
1954 if (err)
1955 return err;
1956
1957 ctx = NULL;
1958 if (rt) {
1959 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1960
1961 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1962 if (err)
1963 return err;
1964 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001965 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
1966 &p->sel, ctx, delete, &err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001967 security_xfrm_policy_free(ctx);
1968 }
1969 if (xp == NULL)
1970 return -ENOENT;
1971
1972 if (!delete) {
1973 struct sk_buff *resp_skb;
1974
1975 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1976 if (IS_ERR(resp_skb)) {
1977 err = PTR_ERR(resp_skb);
1978 } else {
1979 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1980 NETLINK_CB(skb).portid);
1981 }
1982 } else {
1983 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1984
1985 if (err != 0)
1986 goto out;
1987
1988 c.data.byid = p->index;
1989 c.event = nlh->nlmsg_type;
1990 c.seq = nlh->nlmsg_seq;
1991 c.portid = nlh->nlmsg_pid;
1992 km_policy_notify(xp, p->dir, &c);
1993 }
1994
1995out:
1996 xfrm_pol_put(xp);
1997 return err;
1998}
1999
2000static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2001 struct nlattr **attrs)
2002{
2003 struct net *net = sock_net(skb->sk);
2004 struct km_event c;
2005 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2006 int err;
2007
David Brazdil0f672f62019-12-10 10:32:29 +00002008 err = xfrm_state_flush(net, p->proto, true, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002009 if (err) {
2010 if (err == -ESRCH) /* empty table */
2011 return 0;
2012 return err;
2013 }
2014 c.data.proto = p->proto;
2015 c.event = nlh->nlmsg_type;
2016 c.seq = nlh->nlmsg_seq;
2017 c.portid = nlh->nlmsg_pid;
2018 c.net = net;
2019 km_state_notify(NULL, &c);
2020
2021 return 0;
2022}
2023
2024static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2025{
2026 unsigned int replay_size = x->replay_esn ?
2027 xfrm_replay_state_esn_len(x->replay_esn) :
2028 sizeof(struct xfrm_replay_state);
2029
2030 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2031 + nla_total_size(replay_size)
2032 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2033 + nla_total_size(sizeof(struct xfrm_mark))
2034 + nla_total_size(4) /* XFRM_AE_RTHR */
2035 + nla_total_size(4); /* XFRM_AE_ETHR */
2036}
2037
2038static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2039{
2040 struct xfrm_aevent_id *id;
2041 struct nlmsghdr *nlh;
2042 int err;
2043
2044 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2045 if (nlh == NULL)
2046 return -EMSGSIZE;
2047
2048 id = nlmsg_data(nlh);
2049 memset(&id->sa_id, 0, sizeof(id->sa_id));
2050 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2051 id->sa_id.spi = x->id.spi;
2052 id->sa_id.family = x->props.family;
2053 id->sa_id.proto = x->id.proto;
2054 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2055 id->reqid = x->props.reqid;
2056 id->flags = c->data.aevent;
2057
2058 if (x->replay_esn) {
2059 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2060 xfrm_replay_state_esn_len(x->replay_esn),
2061 x->replay_esn);
2062 } else {
2063 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2064 &x->replay);
2065 }
2066 if (err)
2067 goto out_cancel;
2068 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2069 XFRMA_PAD);
2070 if (err)
2071 goto out_cancel;
2072
2073 if (id->flags & XFRM_AE_RTHR) {
2074 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2075 if (err)
2076 goto out_cancel;
2077 }
2078 if (id->flags & XFRM_AE_ETHR) {
2079 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2080 x->replay_maxage * 10 / HZ);
2081 if (err)
2082 goto out_cancel;
2083 }
2084 err = xfrm_mark_put(skb, &x->mark);
2085 if (err)
2086 goto out_cancel;
2087
2088 err = xfrm_if_id_put(skb, x->if_id);
2089 if (err)
2090 goto out_cancel;
2091
2092 nlmsg_end(skb, nlh);
2093 return 0;
2094
2095out_cancel:
2096 nlmsg_cancel(skb, nlh);
2097 return err;
2098}
2099
2100static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2101 struct nlattr **attrs)
2102{
2103 struct net *net = sock_net(skb->sk);
2104 struct xfrm_state *x;
2105 struct sk_buff *r_skb;
2106 int err;
2107 struct km_event c;
2108 u32 mark;
2109 struct xfrm_mark m;
2110 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2111 struct xfrm_usersa_id *id = &p->sa_id;
2112
2113 mark = xfrm_mark_get(attrs, &m);
2114
2115 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2116 if (x == NULL)
2117 return -ESRCH;
2118
2119 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2120 if (r_skb == NULL) {
2121 xfrm_state_put(x);
2122 return -ENOMEM;
2123 }
2124
2125 /*
2126 * XXX: is this lock really needed - none of the other
2127 * gets lock (the concern is things getting updated
2128 * while we are still reading) - jhs
2129 */
2130 spin_lock_bh(&x->lock);
2131 c.data.aevent = p->flags;
2132 c.seq = nlh->nlmsg_seq;
2133 c.portid = nlh->nlmsg_pid;
2134
2135 err = build_aevent(r_skb, x, &c);
2136 BUG_ON(err < 0);
2137
2138 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2139 spin_unlock_bh(&x->lock);
2140 xfrm_state_put(x);
2141 return err;
2142}
2143
2144static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2145 struct nlattr **attrs)
2146{
2147 struct net *net = sock_net(skb->sk);
2148 struct xfrm_state *x;
2149 struct km_event c;
2150 int err = -EINVAL;
2151 u32 mark = 0;
2152 struct xfrm_mark m;
2153 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2154 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2155 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2156 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2157 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2158 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2159
2160 if (!lt && !rp && !re && !et && !rt)
2161 return err;
2162
2163 /* pedantic mode - thou shalt sayeth replaceth */
2164 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2165 return err;
2166
2167 mark = xfrm_mark_get(attrs, &m);
2168
2169 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2170 if (x == NULL)
2171 return -ESRCH;
2172
2173 if (x->km.state != XFRM_STATE_VALID)
2174 goto out;
2175
2176 err = xfrm_replay_verify_len(x->replay_esn, re);
2177 if (err)
2178 goto out;
2179
2180 spin_lock_bh(&x->lock);
2181 xfrm_update_ae_params(x, attrs, 1);
2182 spin_unlock_bh(&x->lock);
2183
2184 c.event = nlh->nlmsg_type;
2185 c.seq = nlh->nlmsg_seq;
2186 c.portid = nlh->nlmsg_pid;
2187 c.data.aevent = XFRM_AE_CU;
2188 km_state_notify(x, &c);
2189 err = 0;
2190out:
2191 xfrm_state_put(x);
2192 return err;
2193}
2194
2195static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2196 struct nlattr **attrs)
2197{
2198 struct net *net = sock_net(skb->sk);
2199 struct km_event c;
2200 u8 type = XFRM_POLICY_TYPE_MAIN;
2201 int err;
2202
2203 err = copy_from_user_policy_type(&type, attrs);
2204 if (err)
2205 return err;
2206
2207 err = xfrm_policy_flush(net, type, true);
2208 if (err) {
2209 if (err == -ESRCH) /* empty table */
2210 return 0;
2211 return err;
2212 }
2213
2214 c.data.type = type;
2215 c.event = nlh->nlmsg_type;
2216 c.seq = nlh->nlmsg_seq;
2217 c.portid = nlh->nlmsg_pid;
2218 c.net = net;
2219 km_policy_notify(NULL, 0, &c);
2220 return 0;
2221}
2222
2223static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2224 struct nlattr **attrs)
2225{
2226 struct net *net = sock_net(skb->sk);
2227 struct xfrm_policy *xp;
2228 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2229 struct xfrm_userpolicy_info *p = &up->pol;
2230 u8 type = XFRM_POLICY_TYPE_MAIN;
2231 int err = -ENOENT;
2232 struct xfrm_mark m;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002233 u32 if_id = 0;
2234
2235 err = copy_from_user_policy_type(&type, attrs);
2236 if (err)
2237 return err;
2238
2239 err = verify_policy_dir(p->dir);
2240 if (err)
2241 return err;
2242
2243 if (attrs[XFRMA_IF_ID])
2244 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2245
Olivier Deprez0e641232021-09-23 10:07:05 +02002246 xfrm_mark_get(attrs, &m);
2247
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002248 if (p->index)
Olivier Deprez0e641232021-09-23 10:07:05 +02002249 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2250 0, &err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002251 else {
2252 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2253 struct xfrm_sec_ctx *ctx;
2254
2255 err = verify_sec_ctx_len(attrs);
2256 if (err)
2257 return err;
2258
2259 ctx = NULL;
2260 if (rt) {
2261 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2262
2263 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2264 if (err)
2265 return err;
2266 }
Olivier Deprez0e641232021-09-23 10:07:05 +02002267 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002268 &p->sel, ctx, 0, &err);
2269 security_xfrm_policy_free(ctx);
2270 }
2271 if (xp == NULL)
2272 return -ENOENT;
2273
2274 if (unlikely(xp->walk.dead))
2275 goto out;
2276
2277 err = 0;
2278 if (up->hard) {
2279 xfrm_policy_delete(xp, p->dir);
2280 xfrm_audit_policy_delete(xp, 1, true);
2281 }
2282 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2283
2284out:
2285 xfrm_pol_put(xp);
2286 return err;
2287}
2288
2289static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2290 struct nlattr **attrs)
2291{
2292 struct net *net = sock_net(skb->sk);
2293 struct xfrm_state *x;
2294 int err;
2295 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2296 struct xfrm_usersa_info *p = &ue->state;
2297 struct xfrm_mark m;
2298 u32 mark = xfrm_mark_get(attrs, &m);
2299
2300 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2301
2302 err = -ENOENT;
2303 if (x == NULL)
2304 return err;
2305
2306 spin_lock_bh(&x->lock);
2307 err = -EINVAL;
2308 if (x->km.state != XFRM_STATE_VALID)
2309 goto out;
2310 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2311
2312 if (ue->hard) {
2313 __xfrm_state_delete(x);
2314 xfrm_audit_state_delete(x, 1, true);
2315 }
2316 err = 0;
2317out:
2318 spin_unlock_bh(&x->lock);
2319 xfrm_state_put(x);
2320 return err;
2321}
2322
2323static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2324 struct nlattr **attrs)
2325{
2326 struct net *net = sock_net(skb->sk);
2327 struct xfrm_policy *xp;
2328 struct xfrm_user_tmpl *ut;
2329 int i;
2330 struct nlattr *rt = attrs[XFRMA_TMPL];
2331 struct xfrm_mark mark;
2332
2333 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2334 struct xfrm_state *x = xfrm_state_alloc(net);
2335 int err = -ENOMEM;
2336
2337 if (!x)
2338 goto nomem;
2339
2340 xfrm_mark_get(attrs, &mark);
2341
2342 err = verify_newpolicy_info(&ua->policy);
2343 if (err)
2344 goto free_state;
Olivier Deprez0e641232021-09-23 10:07:05 +02002345 err = verify_sec_ctx_len(attrs);
2346 if (err)
2347 goto free_state;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002348
2349 /* build an XP */
2350 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2351 if (!xp)
2352 goto free_state;
2353
2354 memcpy(&x->id, &ua->id, sizeof(ua->id));
2355 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2356 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2357 xp->mark.m = x->mark.m = mark.m;
2358 xp->mark.v = x->mark.v = mark.v;
2359 ut = nla_data(rt);
2360 /* extract the templates and for each call km_key */
2361 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2362 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2363 memcpy(&x->id, &t->id, sizeof(x->id));
2364 x->props.mode = t->mode;
2365 x->props.reqid = t->reqid;
2366 x->props.family = ut->family;
2367 t->aalgos = ua->aalgos;
2368 t->ealgos = ua->ealgos;
2369 t->calgos = ua->calgos;
2370 err = km_query(x, t, xp);
2371
2372 }
2373
2374 xfrm_state_free(x);
2375 kfree(xp);
2376
2377 return 0;
2378
2379free_state:
2380 xfrm_state_free(x);
2381nomem:
2382 return err;
2383}
2384
2385#ifdef CONFIG_XFRM_MIGRATE
2386static int copy_from_user_migrate(struct xfrm_migrate *ma,
2387 struct xfrm_kmaddress *k,
2388 struct nlattr **attrs, int *num)
2389{
2390 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2391 struct xfrm_user_migrate *um;
2392 int i, num_migrate;
2393
2394 if (k != NULL) {
2395 struct xfrm_user_kmaddress *uk;
2396
2397 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2398 memcpy(&k->local, &uk->local, sizeof(k->local));
2399 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2400 k->family = uk->family;
2401 k->reserved = uk->reserved;
2402 }
2403
2404 um = nla_data(rt);
2405 num_migrate = nla_len(rt) / sizeof(*um);
2406
2407 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2408 return -EINVAL;
2409
2410 for (i = 0; i < num_migrate; i++, um++, ma++) {
2411 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2412 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2413 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2414 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2415
2416 ma->proto = um->proto;
2417 ma->mode = um->mode;
2418 ma->reqid = um->reqid;
2419
2420 ma->old_family = um->old_family;
2421 ma->new_family = um->new_family;
2422 }
2423
2424 *num = i;
2425 return 0;
2426}
2427
2428static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2429 struct nlattr **attrs)
2430{
2431 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2432 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2433 struct xfrm_kmaddress km, *kmp;
2434 u8 type;
2435 int err;
2436 int n = 0;
2437 struct net *net = sock_net(skb->sk);
2438 struct xfrm_encap_tmpl *encap = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02002439 u32 if_id = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002440
2441 if (attrs[XFRMA_MIGRATE] == NULL)
2442 return -EINVAL;
2443
2444 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2445
2446 err = copy_from_user_policy_type(&type, attrs);
2447 if (err)
2448 return err;
2449
2450 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2451 if (err)
2452 return err;
2453
2454 if (!n)
2455 return 0;
2456
2457 if (attrs[XFRMA_ENCAP]) {
2458 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2459 sizeof(*encap), GFP_KERNEL);
2460 if (!encap)
2461 return 0;
2462 }
2463
Olivier Deprez157378f2022-04-04 15:47:50 +02002464 if (attrs[XFRMA_IF_ID])
2465 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2466
2467 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002468
2469 kfree(encap);
2470
2471 return err;
2472}
2473#else
2474static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2475 struct nlattr **attrs)
2476{
2477 return -ENOPROTOOPT;
2478}
2479#endif
2480
2481#ifdef CONFIG_XFRM_MIGRATE
2482static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2483{
2484 struct xfrm_user_migrate um;
2485
2486 memset(&um, 0, sizeof(um));
2487 um.proto = m->proto;
2488 um.mode = m->mode;
2489 um.reqid = m->reqid;
2490 um.old_family = m->old_family;
2491 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2492 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2493 um.new_family = m->new_family;
2494 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2495 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2496
2497 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2498}
2499
2500static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2501{
2502 struct xfrm_user_kmaddress uk;
2503
2504 memset(&uk, 0, sizeof(uk));
2505 uk.family = k->family;
2506 uk.reserved = k->reserved;
2507 memcpy(&uk.local, &k->local, sizeof(uk.local));
2508 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2509
2510 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2511}
2512
2513static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2514 int with_encp)
2515{
2516 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2517 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2518 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2519 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2520 + userpolicy_type_attrsize();
2521}
2522
2523static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2524 int num_migrate, const struct xfrm_kmaddress *k,
2525 const struct xfrm_selector *sel,
2526 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2527{
2528 const struct xfrm_migrate *mp;
2529 struct xfrm_userpolicy_id *pol_id;
2530 struct nlmsghdr *nlh;
2531 int i, err;
2532
2533 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2534 if (nlh == NULL)
2535 return -EMSGSIZE;
2536
2537 pol_id = nlmsg_data(nlh);
2538 /* copy data from selector, dir, and type to the pol_id */
2539 memset(pol_id, 0, sizeof(*pol_id));
2540 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2541 pol_id->dir = dir;
2542
2543 if (k != NULL) {
2544 err = copy_to_user_kmaddress(k, skb);
2545 if (err)
2546 goto out_cancel;
2547 }
2548 if (encap) {
2549 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2550 if (err)
2551 goto out_cancel;
2552 }
2553 err = copy_to_user_policy_type(type, skb);
2554 if (err)
2555 goto out_cancel;
2556 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2557 err = copy_to_user_migrate(mp, skb);
2558 if (err)
2559 goto out_cancel;
2560 }
2561
2562 nlmsg_end(skb, nlh);
2563 return 0;
2564
2565out_cancel:
2566 nlmsg_cancel(skb, nlh);
2567 return err;
2568}
2569
2570static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2571 const struct xfrm_migrate *m, int num_migrate,
2572 const struct xfrm_kmaddress *k,
2573 const struct xfrm_encap_tmpl *encap)
2574{
2575 struct net *net = &init_net;
2576 struct sk_buff *skb;
2577 int err;
2578
2579 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2580 GFP_ATOMIC);
2581 if (skb == NULL)
2582 return -ENOMEM;
2583
2584 /* build migrate */
2585 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2586 BUG_ON(err < 0);
2587
2588 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2589}
2590#else
2591static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2592 const struct xfrm_migrate *m, int num_migrate,
2593 const struct xfrm_kmaddress *k,
2594 const struct xfrm_encap_tmpl *encap)
2595{
2596 return -ENOPROTOOPT;
2597}
2598#endif
2599
2600#define XMSGSIZE(type) sizeof(struct type)
2601
Olivier Deprez157378f2022-04-04 15:47:50 +02002602const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002603 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2604 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2605 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2606 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2607 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2608 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2609 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2610 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2611 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2612 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2613 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2614 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2615 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2616 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2617 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2618 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2619 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2620 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2621 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2622 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2623 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2624};
Olivier Deprez157378f2022-04-04 15:47:50 +02002625EXPORT_SYMBOL_GPL(xfrm_msg_min);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002626
2627#undef XMSGSIZE
2628
Olivier Deprez157378f2022-04-04 15:47:50 +02002629const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002630 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2631 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2632 [XFRMA_LASTUSED] = { .type = NLA_U64},
2633 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2634 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2635 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2636 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2637 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2638 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2639 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2640 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2641 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2642 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2643 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2644 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2645 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2646 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2647 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2648 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2649 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2650 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2651 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2652 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2653 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2654 [XFRMA_PROTO] = { .type = NLA_U8 },
2655 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2656 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2657 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2658 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2659 [XFRMA_IF_ID] = { .type = NLA_U32 },
2660};
Olivier Deprez157378f2022-04-04 15:47:50 +02002661EXPORT_SYMBOL_GPL(xfrma_policy);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002662
2663static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2664 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2665 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2666};
2667
2668static const struct xfrm_link {
2669 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2670 int (*start)(struct netlink_callback *);
2671 int (*dump)(struct sk_buff *, struct netlink_callback *);
2672 int (*done)(struct netlink_callback *);
2673 const struct nla_policy *nla_pol;
2674 int nla_max;
2675} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2676 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2677 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2678 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2679 .dump = xfrm_dump_sa,
2680 .done = xfrm_dump_sa_done },
2681 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2682 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2683 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2684 .start = xfrm_dump_policy_start,
2685 .dump = xfrm_dump_policy,
2686 .done = xfrm_dump_policy_done },
2687 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2688 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2689 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2690 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2691 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2692 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2693 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2694 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2695 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2696 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2697 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2698 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2699 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2700 .nla_pol = xfrma_spd_policy,
2701 .nla_max = XFRMA_SPD_MAX },
2702 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2703};
2704
2705static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2706 struct netlink_ext_ack *extack)
2707{
2708 struct net *net = sock_net(skb->sk);
2709 struct nlattr *attrs[XFRMA_MAX+1];
2710 const struct xfrm_link *link;
Olivier Deprez157378f2022-04-04 15:47:50 +02002711 struct nlmsghdr *nlh64 = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002712 int type, err;
2713
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002714 type = nlh->nlmsg_type;
2715 if (type > XFRM_MSG_MAX)
2716 return -EINVAL;
2717
2718 type -= XFRM_MSG_BASE;
2719 link = &xfrm_dispatch[type];
2720
2721 /* All operations require privileges, even GET */
2722 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2723 return -EPERM;
2724
Olivier Deprez157378f2022-04-04 15:47:50 +02002725 if (in_compat_syscall()) {
2726 struct xfrm_translator *xtr = xfrm_get_translator();
2727
2728 if (!xtr)
2729 return -EOPNOTSUPP;
2730
2731 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2732 link->nla_pol, extack);
2733 xfrm_put_translator(xtr);
2734 if (IS_ERR(nlh64))
2735 return PTR_ERR(nlh64);
2736 if (nlh64)
2737 nlh = nlh64;
2738 }
2739
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002740 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2741 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2742 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002743 struct netlink_dump_control c = {
2744 .start = link->start,
2745 .dump = link->dump,
2746 .done = link->done,
2747 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002748
Olivier Deprez157378f2022-04-04 15:47:50 +02002749 if (link->dump == NULL) {
2750 err = -EINVAL;
2751 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002752 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002753
2754 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2755 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002756 }
2757
David Brazdil0f672f62019-12-10 10:32:29 +00002758 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2759 link->nla_max ? : XFRMA_MAX,
2760 link->nla_pol ? : xfrma_policy, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002761 if (err < 0)
Olivier Deprez157378f2022-04-04 15:47:50 +02002762 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002763
Olivier Deprez157378f2022-04-04 15:47:50 +02002764 if (link->doit == NULL) {
2765 err = -EINVAL;
2766 goto err;
2767 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002768
Olivier Deprez157378f2022-04-04 15:47:50 +02002769 err = link->doit(skb, nlh, attrs);
2770
2771 /* We need to free skb allocated in xfrm_alloc_compat() before
2772 * returning from this function, because consume_skb() won't take
2773 * care of frag_list since netlink destructor sets
2774 * sbk->head to NULL. (see netlink_skb_destructor())
2775 */
2776 if (skb_has_frag_list(skb)) {
2777 kfree_skb(skb_shinfo(skb)->frag_list);
2778 skb_shinfo(skb)->frag_list = NULL;
2779 }
2780
2781err:
2782 kvfree(nlh64);
2783 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002784}
2785
2786static void xfrm_netlink_rcv(struct sk_buff *skb)
2787{
2788 struct net *net = sock_net(skb->sk);
2789
2790 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2791 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2792 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2793}
2794
2795static inline unsigned int xfrm_expire_msgsize(void)
2796{
2797 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2798 + nla_total_size(sizeof(struct xfrm_mark));
2799}
2800
2801static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2802{
2803 struct xfrm_user_expire *ue;
2804 struct nlmsghdr *nlh;
2805 int err;
2806
2807 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2808 if (nlh == NULL)
2809 return -EMSGSIZE;
2810
2811 ue = nlmsg_data(nlh);
2812 copy_to_user_state(x, &ue->state);
2813 ue->hard = (c->data.hard != 0) ? 1 : 0;
2814 /* clear the padding bytes */
2815 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2816
2817 err = xfrm_mark_put(skb, &x->mark);
2818 if (err)
2819 return err;
2820
2821 err = xfrm_if_id_put(skb, x->if_id);
2822 if (err)
2823 return err;
2824
2825 nlmsg_end(skb, nlh);
2826 return 0;
2827}
2828
2829static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2830{
2831 struct net *net = xs_net(x);
2832 struct sk_buff *skb;
2833
2834 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2835 if (skb == NULL)
2836 return -ENOMEM;
2837
2838 if (build_expire(skb, x, c) < 0) {
2839 kfree_skb(skb);
2840 return -EMSGSIZE;
2841 }
2842
2843 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2844}
2845
2846static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2847{
2848 struct net *net = xs_net(x);
2849 struct sk_buff *skb;
2850 int err;
2851
2852 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2853 if (skb == NULL)
2854 return -ENOMEM;
2855
2856 err = build_aevent(skb, x, c);
2857 BUG_ON(err < 0);
2858
2859 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2860}
2861
2862static int xfrm_notify_sa_flush(const struct km_event *c)
2863{
2864 struct net *net = c->net;
2865 struct xfrm_usersa_flush *p;
2866 struct nlmsghdr *nlh;
2867 struct sk_buff *skb;
2868 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2869
2870 skb = nlmsg_new(len, GFP_ATOMIC);
2871 if (skb == NULL)
2872 return -ENOMEM;
2873
2874 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2875 if (nlh == NULL) {
2876 kfree_skb(skb);
2877 return -EMSGSIZE;
2878 }
2879
2880 p = nlmsg_data(nlh);
2881 p->proto = c->data.proto;
2882
2883 nlmsg_end(skb, nlh);
2884
2885 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2886}
2887
2888static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
2889{
2890 unsigned int l = 0;
2891 if (x->aead)
2892 l += nla_total_size(aead_len(x->aead));
2893 if (x->aalg) {
2894 l += nla_total_size(sizeof(struct xfrm_algo) +
2895 (x->aalg->alg_key_len + 7) / 8);
2896 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2897 }
2898 if (x->ealg)
2899 l += nla_total_size(xfrm_alg_len(x->ealg));
2900 if (x->calg)
2901 l += nla_total_size(sizeof(*x->calg));
2902 if (x->encap)
2903 l += nla_total_size(sizeof(*x->encap));
2904 if (x->tfcpad)
2905 l += nla_total_size(sizeof(x->tfcpad));
2906 if (x->replay_esn)
2907 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2908 else
2909 l += nla_total_size(sizeof(struct xfrm_replay_state));
2910 if (x->security)
2911 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2912 x->security->ctx_len);
2913 if (x->coaddr)
2914 l += nla_total_size(sizeof(*x->coaddr));
2915 if (x->props.extra_flags)
2916 l += nla_total_size(sizeof(x->props.extra_flags));
2917 if (x->xso.dev)
Olivier Deprez157378f2022-04-04 15:47:50 +02002918 l += nla_total_size(sizeof(struct xfrm_user_offload));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002919 if (x->props.smark.v | x->props.smark.m) {
2920 l += nla_total_size(sizeof(x->props.smark.v));
2921 l += nla_total_size(sizeof(x->props.smark.m));
2922 }
2923 if (x->if_id)
2924 l += nla_total_size(sizeof(x->if_id));
2925
2926 /* Must count x->lastused as it may become non-zero behind our back. */
2927 l += nla_total_size_64bit(sizeof(u64));
2928
Olivier Deprez157378f2022-04-04 15:47:50 +02002929 if (x->mapping_maxage)
2930 l += nla_total_size(sizeof(x->mapping_maxage));
2931
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002932 return l;
2933}
2934
2935static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2936{
2937 struct net *net = xs_net(x);
2938 struct xfrm_usersa_info *p;
2939 struct xfrm_usersa_id *id;
2940 struct nlmsghdr *nlh;
2941 struct sk_buff *skb;
2942 unsigned int len = xfrm_sa_len(x);
2943 unsigned int headlen;
2944 int err;
2945
2946 headlen = sizeof(*p);
2947 if (c->event == XFRM_MSG_DELSA) {
2948 len += nla_total_size(headlen);
2949 headlen = sizeof(*id);
2950 len += nla_total_size(sizeof(struct xfrm_mark));
2951 }
2952 len += NLMSG_ALIGN(headlen);
2953
2954 skb = nlmsg_new(len, GFP_ATOMIC);
2955 if (skb == NULL)
2956 return -ENOMEM;
2957
2958 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2959 err = -EMSGSIZE;
2960 if (nlh == NULL)
2961 goto out_free_skb;
2962
2963 p = nlmsg_data(nlh);
2964 if (c->event == XFRM_MSG_DELSA) {
2965 struct nlattr *attr;
2966
2967 id = nlmsg_data(nlh);
2968 memset(id, 0, sizeof(*id));
2969 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2970 id->spi = x->id.spi;
2971 id->family = x->props.family;
2972 id->proto = x->id.proto;
2973
2974 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2975 err = -EMSGSIZE;
2976 if (attr == NULL)
2977 goto out_free_skb;
2978
2979 p = nla_data(attr);
2980 }
2981 err = copy_to_user_state_extra(x, p, skb);
2982 if (err)
2983 goto out_free_skb;
2984
2985 nlmsg_end(skb, nlh);
2986
2987 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2988
2989out_free_skb:
2990 kfree_skb(skb);
2991 return err;
2992}
2993
2994static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2995{
2996
2997 switch (c->event) {
2998 case XFRM_MSG_EXPIRE:
2999 return xfrm_exp_state_notify(x, c);
3000 case XFRM_MSG_NEWAE:
3001 return xfrm_aevent_state_notify(x, c);
3002 case XFRM_MSG_DELSA:
3003 case XFRM_MSG_UPDSA:
3004 case XFRM_MSG_NEWSA:
3005 return xfrm_notify_sa(x, c);
3006 case XFRM_MSG_FLUSHSA:
3007 return xfrm_notify_sa_flush(c);
3008 default:
3009 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3010 c->event);
3011 break;
3012 }
3013
3014 return 0;
3015
3016}
3017
3018static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3019 struct xfrm_policy *xp)
3020{
3021 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3022 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3023 + nla_total_size(sizeof(struct xfrm_mark))
3024 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3025 + userpolicy_type_attrsize();
3026}
3027
3028static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
3029 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3030{
3031 __u32 seq = xfrm_get_acqseq();
3032 struct xfrm_user_acquire *ua;
3033 struct nlmsghdr *nlh;
3034 int err;
3035
3036 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3037 if (nlh == NULL)
3038 return -EMSGSIZE;
3039
3040 ua = nlmsg_data(nlh);
3041 memcpy(&ua->id, &x->id, sizeof(ua->id));
3042 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3043 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3044 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3045 ua->aalgos = xt->aalgos;
3046 ua->ealgos = xt->ealgos;
3047 ua->calgos = xt->calgos;
3048 ua->seq = x->km.seq = seq;
3049
3050 err = copy_to_user_tmpl(xp, skb);
3051 if (!err)
3052 err = copy_to_user_state_sec_ctx(x, skb);
3053 if (!err)
3054 err = copy_to_user_policy_type(xp->type, skb);
3055 if (!err)
3056 err = xfrm_mark_put(skb, &xp->mark);
3057 if (!err)
3058 err = xfrm_if_id_put(skb, xp->if_id);
3059 if (err) {
3060 nlmsg_cancel(skb, nlh);
3061 return err;
3062 }
3063
3064 nlmsg_end(skb, nlh);
3065 return 0;
3066}
3067
3068static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3069 struct xfrm_policy *xp)
3070{
3071 struct net *net = xs_net(x);
3072 struct sk_buff *skb;
3073 int err;
3074
3075 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3076 if (skb == NULL)
3077 return -ENOMEM;
3078
3079 err = build_acquire(skb, x, xt, xp);
3080 BUG_ON(err < 0);
3081
3082 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3083}
3084
3085/* User gives us xfrm_user_policy_info followed by an array of 0
3086 * or more templates.
3087 */
3088static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3089 u8 *data, int len, int *dir)
3090{
3091 struct net *net = sock_net(sk);
3092 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3093 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3094 struct xfrm_policy *xp;
3095 int nr;
3096
3097 switch (sk->sk_family) {
3098 case AF_INET:
3099 if (opt != IP_XFRM_POLICY) {
3100 *dir = -EOPNOTSUPP;
3101 return NULL;
3102 }
3103 break;
3104#if IS_ENABLED(CONFIG_IPV6)
3105 case AF_INET6:
3106 if (opt != IPV6_XFRM_POLICY) {
3107 *dir = -EOPNOTSUPP;
3108 return NULL;
3109 }
3110 break;
3111#endif
3112 default:
3113 *dir = -EINVAL;
3114 return NULL;
3115 }
3116
3117 *dir = -EINVAL;
3118
3119 if (len < sizeof(*p) ||
3120 verify_newpolicy_info(p))
3121 return NULL;
3122
3123 nr = ((len - sizeof(*p)) / sizeof(*ut));
3124 if (validate_tmpl(nr, ut, p->sel.family))
3125 return NULL;
3126
3127 if (p->dir > XFRM_POLICY_OUT)
3128 return NULL;
3129
3130 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3131 if (xp == NULL) {
3132 *dir = -ENOBUFS;
3133 return NULL;
3134 }
3135
3136 copy_from_user_policy(xp, p);
3137 xp->type = XFRM_POLICY_TYPE_MAIN;
3138 copy_templates(xp, ut, nr);
3139
3140 *dir = p->dir;
3141
3142 return xp;
3143}
3144
3145static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3146{
3147 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3148 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3149 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3150 + nla_total_size(sizeof(struct xfrm_mark))
3151 + userpolicy_type_attrsize();
3152}
3153
3154static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3155 int dir, const struct km_event *c)
3156{
3157 struct xfrm_user_polexpire *upe;
3158 int hard = c->data.hard;
3159 struct nlmsghdr *nlh;
3160 int err;
3161
3162 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3163 if (nlh == NULL)
3164 return -EMSGSIZE;
3165
3166 upe = nlmsg_data(nlh);
3167 copy_to_user_policy(xp, &upe->pol, dir);
3168 err = copy_to_user_tmpl(xp, skb);
3169 if (!err)
3170 err = copy_to_user_sec_ctx(xp, skb);
3171 if (!err)
3172 err = copy_to_user_policy_type(xp->type, skb);
3173 if (!err)
3174 err = xfrm_mark_put(skb, &xp->mark);
3175 if (!err)
3176 err = xfrm_if_id_put(skb, xp->if_id);
3177 if (err) {
3178 nlmsg_cancel(skb, nlh);
3179 return err;
3180 }
3181 upe->hard = !!hard;
3182
3183 nlmsg_end(skb, nlh);
3184 return 0;
3185}
3186
3187static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3188{
3189 struct net *net = xp_net(xp);
3190 struct sk_buff *skb;
3191 int err;
3192
3193 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3194 if (skb == NULL)
3195 return -ENOMEM;
3196
3197 err = build_polexpire(skb, xp, dir, c);
3198 BUG_ON(err < 0);
3199
3200 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3201}
3202
3203static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3204{
3205 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3206 struct net *net = xp_net(xp);
3207 struct xfrm_userpolicy_info *p;
3208 struct xfrm_userpolicy_id *id;
3209 struct nlmsghdr *nlh;
3210 struct sk_buff *skb;
3211 unsigned int headlen;
3212 int err;
3213
3214 headlen = sizeof(*p);
3215 if (c->event == XFRM_MSG_DELPOLICY) {
3216 len += nla_total_size(headlen);
3217 headlen = sizeof(*id);
3218 }
3219 len += userpolicy_type_attrsize();
3220 len += nla_total_size(sizeof(struct xfrm_mark));
3221 len += NLMSG_ALIGN(headlen);
3222
3223 skb = nlmsg_new(len, GFP_ATOMIC);
3224 if (skb == NULL)
3225 return -ENOMEM;
3226
3227 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3228 err = -EMSGSIZE;
3229 if (nlh == NULL)
3230 goto out_free_skb;
3231
3232 p = nlmsg_data(nlh);
3233 if (c->event == XFRM_MSG_DELPOLICY) {
3234 struct nlattr *attr;
3235
3236 id = nlmsg_data(nlh);
3237 memset(id, 0, sizeof(*id));
3238 id->dir = dir;
3239 if (c->data.byid)
3240 id->index = xp->index;
3241 else
3242 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3243
3244 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3245 err = -EMSGSIZE;
3246 if (attr == NULL)
3247 goto out_free_skb;
3248
3249 p = nla_data(attr);
3250 }
3251
3252 copy_to_user_policy(xp, p, dir);
3253 err = copy_to_user_tmpl(xp, skb);
3254 if (!err)
3255 err = copy_to_user_policy_type(xp->type, skb);
3256 if (!err)
3257 err = xfrm_mark_put(skb, &xp->mark);
3258 if (!err)
3259 err = xfrm_if_id_put(skb, xp->if_id);
3260 if (err)
3261 goto out_free_skb;
3262
3263 nlmsg_end(skb, nlh);
3264
3265 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3266
3267out_free_skb:
3268 kfree_skb(skb);
3269 return err;
3270}
3271
3272static int xfrm_notify_policy_flush(const struct km_event *c)
3273{
3274 struct net *net = c->net;
3275 struct nlmsghdr *nlh;
3276 struct sk_buff *skb;
3277 int err;
3278
3279 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3280 if (skb == NULL)
3281 return -ENOMEM;
3282
3283 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3284 err = -EMSGSIZE;
3285 if (nlh == NULL)
3286 goto out_free_skb;
3287 err = copy_to_user_policy_type(c->data.type, skb);
3288 if (err)
3289 goto out_free_skb;
3290
3291 nlmsg_end(skb, nlh);
3292
3293 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3294
3295out_free_skb:
3296 kfree_skb(skb);
3297 return err;
3298}
3299
3300static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3301{
3302
3303 switch (c->event) {
3304 case XFRM_MSG_NEWPOLICY:
3305 case XFRM_MSG_UPDPOLICY:
3306 case XFRM_MSG_DELPOLICY:
3307 return xfrm_notify_policy(xp, dir, c);
3308 case XFRM_MSG_FLUSHPOLICY:
3309 return xfrm_notify_policy_flush(c);
3310 case XFRM_MSG_POLEXPIRE:
3311 return xfrm_exp_policy_notify(xp, dir, c);
3312 default:
3313 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3314 c->event);
3315 }
3316
3317 return 0;
3318
3319}
3320
3321static inline unsigned int xfrm_report_msgsize(void)
3322{
3323 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3324}
3325
3326static int build_report(struct sk_buff *skb, u8 proto,
3327 struct xfrm_selector *sel, xfrm_address_t *addr)
3328{
3329 struct xfrm_user_report *ur;
3330 struct nlmsghdr *nlh;
3331
3332 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3333 if (nlh == NULL)
3334 return -EMSGSIZE;
3335
3336 ur = nlmsg_data(nlh);
3337 ur->proto = proto;
3338 memcpy(&ur->sel, sel, sizeof(ur->sel));
3339
3340 if (addr) {
3341 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3342 if (err) {
3343 nlmsg_cancel(skb, nlh);
3344 return err;
3345 }
3346 }
3347 nlmsg_end(skb, nlh);
3348 return 0;
3349}
3350
3351static int xfrm_send_report(struct net *net, u8 proto,
3352 struct xfrm_selector *sel, xfrm_address_t *addr)
3353{
3354 struct sk_buff *skb;
3355 int err;
3356
3357 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3358 if (skb == NULL)
3359 return -ENOMEM;
3360
3361 err = build_report(skb, proto, sel, addr);
3362 BUG_ON(err < 0);
3363
3364 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3365}
3366
3367static inline unsigned int xfrm_mapping_msgsize(void)
3368{
3369 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3370}
3371
3372static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3373 xfrm_address_t *new_saddr, __be16 new_sport)
3374{
3375 struct xfrm_user_mapping *um;
3376 struct nlmsghdr *nlh;
3377
3378 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3379 if (nlh == NULL)
3380 return -EMSGSIZE;
3381
3382 um = nlmsg_data(nlh);
3383
3384 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3385 um->id.spi = x->id.spi;
3386 um->id.family = x->props.family;
3387 um->id.proto = x->id.proto;
3388 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3389 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3390 um->new_sport = new_sport;
3391 um->old_sport = x->encap->encap_sport;
3392 um->reqid = x->props.reqid;
3393
3394 nlmsg_end(skb, nlh);
3395 return 0;
3396}
3397
3398static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3399 __be16 sport)
3400{
3401 struct net *net = xs_net(x);
3402 struct sk_buff *skb;
3403 int err;
3404
3405 if (x->id.proto != IPPROTO_ESP)
3406 return -EINVAL;
3407
3408 if (!x->encap)
3409 return -EINVAL;
3410
3411 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3412 if (skb == NULL)
3413 return -ENOMEM;
3414
3415 err = build_mapping(skb, x, ipaddr, sport);
3416 BUG_ON(err < 0);
3417
3418 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3419}
3420
3421static bool xfrm_is_alive(const struct km_event *c)
3422{
3423 return (bool)xfrm_acquire_is_on(c->net);
3424}
3425
3426static struct xfrm_mgr netlink_mgr = {
3427 .notify = xfrm_send_state_notify,
3428 .acquire = xfrm_send_acquire,
3429 .compile_policy = xfrm_compile_policy,
3430 .notify_policy = xfrm_send_policy_notify,
3431 .report = xfrm_send_report,
3432 .migrate = xfrm_send_migrate,
3433 .new_mapping = xfrm_send_mapping,
3434 .is_alive = xfrm_is_alive,
3435};
3436
3437static int __net_init xfrm_user_net_init(struct net *net)
3438{
3439 struct sock *nlsk;
3440 struct netlink_kernel_cfg cfg = {
3441 .groups = XFRMNLGRP_MAX,
3442 .input = xfrm_netlink_rcv,
3443 };
3444
3445 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3446 if (nlsk == NULL)
3447 return -ENOMEM;
3448 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3449 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3450 return 0;
3451}
3452
3453static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3454{
3455 struct net *net;
3456 list_for_each_entry(net, net_exit_list, exit_list)
3457 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3458 synchronize_net();
3459 list_for_each_entry(net, net_exit_list, exit_list)
3460 netlink_kernel_release(net->xfrm.nlsk_stash);
3461}
3462
3463static struct pernet_operations xfrm_user_net_ops = {
3464 .init = xfrm_user_net_init,
3465 .exit_batch = xfrm_user_net_exit,
3466};
3467
3468static int __init xfrm_user_init(void)
3469{
3470 int rv;
3471
3472 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3473
3474 rv = register_pernet_subsys(&xfrm_user_net_ops);
3475 if (rv < 0)
3476 return rv;
3477 rv = xfrm_register_km(&netlink_mgr);
3478 if (rv < 0)
3479 unregister_pernet_subsys(&xfrm_user_net_ops);
3480 return rv;
3481}
3482
3483static void __exit xfrm_user_exit(void)
3484{
3485 xfrm_unregister_km(&netlink_mgr);
3486 unregister_pernet_subsys(&xfrm_user_net_ops);
3487}
3488
3489module_init(xfrm_user_init);
3490module_exit(xfrm_user_exit);
3491MODULE_LICENSE("GPL");
3492MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);