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