1 // SPDX-License-Identifier: GPL-2.0-only
2 /* xfrm_user.c: User interface to configure xfrm engine.
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
8 * Kazunori MIYAZAWA @USAGI
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
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>
29 #include <net/netlink.h>
31 #include <linux/uaccess.h>
32 #if IS_ENABLED(CONFIG_IPV6)
33 #include <linux/in6.h>
35 #include <asm/unaligned.h>
37 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
39 struct nlattr *rt = attrs[type];
40 struct xfrm_algo *algp;
46 if (nla_len(rt) < (int)xfrm_alg_len(algp))
59 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
63 static int verify_auth_trunc(struct nlattr **attrs)
65 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
66 struct xfrm_algo_auth *algp;
72 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
75 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
79 static int verify_aead(struct nlattr **attrs)
81 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
82 struct xfrm_algo_aead *algp;
88 if (nla_len(rt) < (int)aead_len(algp))
91 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
95 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
96 xfrm_address_t **addrp)
98 struct nlattr *rt = attrs[type];
101 *addrp = nla_data(rt);
104 static inline int verify_sec_ctx_len(struct nlattr **attrs)
106 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
107 struct xfrm_user_sec_ctx *uctx;
113 if (uctx->len > nla_len(rt) ||
114 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
120 static inline int verify_replay(struct xfrm_usersa_info *p,
121 struct nlattr **attrs)
123 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
124 struct xfrm_replay_state_esn *rs;
127 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
131 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
134 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
135 nla_len(rt) != sizeof(*rs))
138 /* As only ESP and AH support ESN feature. */
139 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
142 if (p->replay_window != 0)
148 static int verify_newsa_info(struct xfrm_usersa_info *p,
149 struct nlattr **attrs)
159 #if IS_ENABLED(CONFIG_IPV6)
170 switch (p->sel.family) {
175 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
181 #if IS_ENABLED(CONFIG_IPV6)
182 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
196 switch (p->id.proto) {
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] ||
208 if (attrs[XFRMA_ALG_COMP])
210 if (!attrs[XFRMA_ALG_AUTH] &&
211 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
212 !attrs[XFRMA_ALG_CRYPT] &&
213 !attrs[XFRMA_ALG_AEAD])
215 if ((attrs[XFRMA_ALG_AUTH] ||
216 attrs[XFRMA_ALG_AUTH_TRUNC] ||
217 attrs[XFRMA_ALG_CRYPT]) &&
218 attrs[XFRMA_ALG_AEAD])
220 if (attrs[XFRMA_TFCPAD] &&
221 p->mode != XFRM_MODE_TUNNEL)
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))
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])
256 if ((err = verify_aead(attrs)))
258 if ((err = verify_auth_trunc(attrs)))
260 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
262 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
264 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
266 if ((err = verify_sec_ctx_len(attrs)))
268 if ((err = verify_replay(p, attrs)))
273 case XFRM_MODE_TRANSPORT:
274 case XFRM_MODE_TUNNEL:
275 case XFRM_MODE_ROUTEOPTIMIZATION:
285 if (attrs[XFRMA_MTIMER_THRESH])
286 if (!attrs[XFRMA_ENCAP])
293 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
294 struct xfrm_algo_desc *(*get_byname)(const char *, int),
297 struct xfrm_algo *p, *ualg;
298 struct xfrm_algo_desc *algo;
303 ualg = nla_data(rta);
305 algo = get_byname(ualg->alg_name, 1);
308 *props = algo->desc.sadb_alg_id;
310 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
314 strcpy(p->alg_name, algo->name);
319 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
321 struct xfrm_algo *p, *ualg;
322 struct xfrm_algo_desc *algo;
327 ualg = nla_data(rta);
329 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
332 x->props.ealgo = algo->desc.sadb_alg_id;
334 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
338 strcpy(p->alg_name, algo->name);
340 x->geniv = algo->uinfo.encr.geniv;
344 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
347 struct xfrm_algo *ualg;
348 struct xfrm_algo_auth *p;
349 struct xfrm_algo_desc *algo;
354 ualg = nla_data(rta);
356 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
359 *props = algo->desc.sadb_alg_id;
361 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
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);
374 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
377 struct xfrm_algo_auth *p, *ualg;
378 struct xfrm_algo_desc *algo;
383 ualg = nla_data(rta);
385 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
388 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
390 *props = algo->desc.sadb_alg_id;
392 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
396 strcpy(p->alg_name, algo->name);
397 if (!p->alg_trunc_len)
398 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
404 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
406 struct xfrm_algo_aead *p, *ualg;
407 struct xfrm_algo_desc *algo;
412 ualg = nla_data(rta);
414 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
417 x->props.ealgo = algo->desc.sadb_alg_id;
419 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
423 strcpy(p->alg_name, algo->name);
425 x->geniv = algo->uinfo.aead.geniv;
429 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
432 struct xfrm_replay_state_esn *up;
435 if (!replay_esn || !rp)
439 ulen = xfrm_replay_state_esn_len(up);
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)
448 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
454 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
455 struct xfrm_replay_state_esn **preplay_esn,
458 struct xfrm_replay_state_esn *p, *pp, *up;
459 unsigned int klen, ulen;
465 klen = xfrm_replay_state_esn_len(up);
466 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
468 p = kzalloc(klen, GFP_KERNEL);
472 pp = kzalloc(klen, GFP_KERNEL);
479 memcpy(pp, up, ulen);
487 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
489 unsigned int len = 0;
492 len += sizeof(struct xfrm_user_sec_ctx);
493 len += xfrm_ctx->ctx_len;
498 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
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;
511 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
512 x->sel.family = p->family;
516 * someday when pfkey also has support, we could have the code
517 * somehow made shareable and move it to xfrm_state.c - JHS
520 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
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];
528 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
530 if (re && x->replay_esn && x->preplay_esn) {
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));
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));
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;
556 x->replay_maxage = nla_get_u32(et);
559 x->replay_maxdiff = nla_get_u32(rt);
562 x->mapping_maxage = nla_get_u32(mt);
565 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
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]);
578 static struct xfrm_state *xfrm_state_construct(struct net *net,
579 struct xfrm_usersa_info *p,
580 struct nlattr **attrs,
583 struct xfrm_state *x = xfrm_state_alloc(net);
589 copy_from_user_state(x, p);
591 if (attrs[XFRMA_ENCAP]) {
592 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
593 sizeof(*x->encap), GFP_KERNEL);
594 if (x->encap == NULL)
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)
605 if (attrs[XFRMA_SA_EXTRA_FLAGS])
606 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
608 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
610 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
611 attrs[XFRMA_ALG_AUTH_TRUNC])))
613 if (!x->props.aalgo) {
614 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
615 attrs[XFRMA_ALG_AUTH])))
618 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
620 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
621 xfrm_calg_get_byname,
622 attrs[XFRMA_ALG_COMP])))
625 if (attrs[XFRMA_TFCPAD])
626 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
628 xfrm_mark_get(attrs, &x->mark);
630 xfrm_smark_init(attrs, &x->props.smark);
632 if (attrs[XFRMA_IF_ID])
633 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
635 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
639 if (attrs[XFRMA_SEC_CTX]) {
640 err = security_xfrm_state_alloc(x,
641 nla_data(attrs[XFRMA_SEC_CTX]));
646 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
647 attrs[XFRMA_REPLAY_ESN_VAL])))
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;
655 if ((err = xfrm_init_replay(x)))
658 /* override default values from above */
659 xfrm_update_ae_params(x, attrs, 0);
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]));
672 x->km.state = XFRM_STATE_DEAD;
679 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
680 struct nlattr **attrs)
682 struct net *net = sock_net(skb->sk);
683 struct xfrm_usersa_info *p = nlmsg_data(nlh);
684 struct xfrm_state *x;
688 err = verify_newsa_info(p, attrs);
692 x = xfrm_state_construct(net, p, attrs, &err);
697 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
698 err = xfrm_state_add(x);
700 err = xfrm_state_update(x);
702 xfrm_audit_state_add(x, err ? 0 : 1, true);
705 x->km.state = XFRM_STATE_DEAD;
706 xfrm_dev_state_delete(x);
711 if (x->km.state == XFRM_STATE_VOID)
712 x->km.state = XFRM_STATE_VALID;
714 c.seq = nlh->nlmsg_seq;
715 c.portid = nlh->nlmsg_pid;
716 c.event = nlh->nlmsg_type;
718 km_state_notify(x, &c);
724 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
725 struct xfrm_usersa_id *p,
726 struct nlattr **attrs,
729 struct xfrm_state *x = NULL;
732 u32 mark = xfrm_mark_get(attrs, &m);
734 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
736 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
738 xfrm_address_t *saddr = NULL;
740 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
747 x = xfrm_state_lookup_byaddr(net, mark,
749 p->proto, p->family);
758 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
759 struct nlattr **attrs)
761 struct net *net = sock_net(skb->sk);
762 struct xfrm_state *x;
765 struct xfrm_usersa_id *p = nlmsg_data(nlh);
767 x = xfrm_user_state_lookup(net, p, attrs, &err);
771 if ((err = security_xfrm_state_delete(x)) != 0)
774 if (xfrm_state_kern(x)) {
779 err = xfrm_state_delete(x);
784 c.seq = nlh->nlmsg_seq;
785 c.portid = nlh->nlmsg_pid;
786 c.event = nlh->nlmsg_type;
787 km_state_notify(x, &c);
790 xfrm_audit_state_delete(x, err ? 0 : 1, true);
795 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
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;
814 struct xfrm_dump_info {
815 struct sk_buff *in_skb;
816 struct sk_buff *out_skb;
821 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
823 struct xfrm_user_sec_ctx *uctx;
825 int ctx_size = sizeof(*uctx) + s->ctx_len;
827 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
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);
842 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
844 struct xfrm_user_offload *xuo;
847 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
851 xuo = nla_data(attr);
852 memset(xuo, 0, sizeof(*xuo));
853 xuo->ifindex = xso->dev->ifindex;
854 xuo->flags = xso->flags;
859 static bool xfrm_redact(void)
861 return IS_ENABLED(CONFIG_SECURITY) &&
862 security_locked_down(LOCKDOWN_XFRM_SECRET);
865 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
867 struct xfrm_algo *algo;
868 struct xfrm_algo_auth *ap;
870 bool redact_secret = xfrm_redact();
872 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
873 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
876 algo = nla_data(nla);
877 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
879 if (redact_secret && auth->alg_key_len)
880 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
882 memcpy(algo->alg_key, auth->alg_key,
883 (auth->alg_key_len + 7) / 8);
884 algo->alg_key_len = auth->alg_key_len;
886 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
890 memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
891 if (redact_secret && auth->alg_key_len)
892 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
894 memcpy(ap->alg_key, auth->alg_key,
895 (auth->alg_key_len + 7) / 8);
899 static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
901 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
902 struct xfrm_algo_aead *ap;
903 bool redact_secret = xfrm_redact();
909 strscpy_pad(ap->alg_name, aead->alg_name, sizeof(ap->alg_name));
910 ap->alg_key_len = aead->alg_key_len;
911 ap->alg_icv_len = aead->alg_icv_len;
913 if (redact_secret && aead->alg_key_len)
914 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
916 memcpy(ap->alg_key, aead->alg_key,
917 (aead->alg_key_len + 7) / 8);
921 static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
923 struct xfrm_algo *ap;
924 bool redact_secret = xfrm_redact();
925 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
931 strscpy_pad(ap->alg_name, ealg->alg_name, sizeof(ap->alg_name));
932 ap->alg_key_len = ealg->alg_key_len;
934 if (redact_secret && ealg->alg_key_len)
935 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
937 memcpy(ap->alg_key, ealg->alg_key,
938 (ealg->alg_key_len + 7) / 8);
943 static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb)
945 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg));
946 struct xfrm_algo *ap;
952 strscpy_pad(ap->alg_name, calg->alg_name, sizeof(ap->alg_name));
958 static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb)
960 struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep));
961 struct xfrm_encap_tmpl *uep;
967 memset(uep, 0, sizeof(*uep));
969 uep->encap_type = ep->encap_type;
970 uep->encap_sport = ep->encap_sport;
971 uep->encap_dport = ep->encap_dport;
972 uep->encap_oa = ep->encap_oa;
977 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
982 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
984 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
989 /* Don't change this without updating xfrm_sa_len! */
990 static int copy_to_user_state_extra(struct xfrm_state *x,
991 struct xfrm_usersa_info *p,
996 copy_to_user_state(x, p);
998 if (x->props.extra_flags) {
999 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
1000 x->props.extra_flags);
1006 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
1011 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
1017 ret = copy_to_user_aead(x->aead, skb);
1022 ret = copy_to_user_auth(x->aalg, skb);
1027 ret = copy_to_user_ealg(x->ealg, skb);
1032 ret = copy_to_user_calg(x->calg, skb);
1037 ret = copy_to_user_encap(x->encap, skb);
1042 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1046 ret = xfrm_mark_put(skb, &x->mark);
1050 ret = xfrm_smark_put(skb, &x->props.smark);
1055 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1056 xfrm_replay_state_esn_len(x->replay_esn),
1059 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1064 ret = copy_user_offload(&x->xso, skb);
1068 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1073 ret = copy_sec_ctx(x->security, skb);
1077 if (x->mapping_maxage)
1078 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
1083 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1085 struct xfrm_dump_info *sp = ptr;
1086 struct sk_buff *in_skb = sp->in_skb;
1087 struct sk_buff *skb = sp->out_skb;
1088 struct xfrm_translator *xtr;
1089 struct xfrm_usersa_info *p;
1090 struct nlmsghdr *nlh;
1093 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1094 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1098 p = nlmsg_data(nlh);
1100 err = copy_to_user_state_extra(x, p, skb);
1102 nlmsg_cancel(skb, nlh);
1105 nlmsg_end(skb, nlh);
1107 xtr = xfrm_get_translator();
1109 err = xtr->alloc_compat(skb, nlh);
1111 xfrm_put_translator(xtr);
1113 nlmsg_cancel(skb, nlh);
1121 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1123 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1124 struct sock *sk = cb->skb->sk;
1125 struct net *net = sock_net(sk);
1128 xfrm_state_walk_done(walk, net);
1132 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1134 struct net *net = sock_net(skb->sk);
1135 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1136 struct xfrm_dump_info info;
1138 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1139 sizeof(cb->args) - sizeof(cb->args[0]));
1141 info.in_skb = cb->skb;
1143 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1144 info.nlmsg_flags = NLM_F_MULTI;
1147 struct nlattr *attrs[XFRMA_MAX+1];
1148 struct xfrm_address_filter *filter = NULL;
1152 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1153 xfrma_policy, cb->extack);
1157 if (attrs[XFRMA_ADDRESS_FILTER]) {
1158 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1159 sizeof(*filter), GFP_KERNEL);
1163 /* see addr_match(), (prefix length >> 5) << 2
1164 * will be used to compare xfrm_address_t
1166 if (filter->splen > (sizeof(xfrm_address_t) << 3) ||
1167 filter->dplen > (sizeof(xfrm_address_t) << 3)) {
1173 if (attrs[XFRMA_PROTO])
1174 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1176 xfrm_state_walk_init(walk, proto, filter);
1180 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1185 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1186 struct xfrm_state *x, u32 seq)
1188 struct xfrm_dump_info info;
1189 struct sk_buff *skb;
1192 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1194 return ERR_PTR(-ENOMEM);
1196 info.in_skb = in_skb;
1198 info.nlmsg_seq = seq;
1199 info.nlmsg_flags = 0;
1201 err = dump_one_state(x, 0, &info);
1204 return ERR_PTR(err);
1210 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1211 * Must be called with RCU read lock.
1213 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1214 u32 pid, unsigned int group)
1216 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1217 struct xfrm_translator *xtr;
1224 xtr = xfrm_get_translator();
1226 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1228 xfrm_put_translator(xtr);
1235 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1238 static inline unsigned int xfrm_spdinfo_msgsize(void)
1240 return NLMSG_ALIGN(4)
1241 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1242 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1243 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1244 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1247 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1248 u32 portid, u32 seq, u32 flags)
1250 struct xfrmk_spdinfo si;
1251 struct xfrmu_spdinfo spc;
1252 struct xfrmu_spdhinfo sph;
1253 struct xfrmu_spdhthresh spt4, spt6;
1254 struct nlmsghdr *nlh;
1259 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1260 if (nlh == NULL) /* shouldn't really happen ... */
1263 f = nlmsg_data(nlh);
1265 xfrm_spd_getinfo(net, &si);
1266 spc.incnt = si.incnt;
1267 spc.outcnt = si.outcnt;
1268 spc.fwdcnt = si.fwdcnt;
1269 spc.inscnt = si.inscnt;
1270 spc.outscnt = si.outscnt;
1271 spc.fwdscnt = si.fwdscnt;
1272 sph.spdhcnt = si.spdhcnt;
1273 sph.spdhmcnt = si.spdhmcnt;
1276 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1278 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1279 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1280 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1281 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1282 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1284 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1286 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1288 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1290 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1292 nlmsg_cancel(skb, nlh);
1296 nlmsg_end(skb, nlh);
1300 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1301 struct nlattr **attrs)
1303 struct net *net = sock_net(skb->sk);
1304 struct xfrmu_spdhthresh *thresh4 = NULL;
1305 struct xfrmu_spdhthresh *thresh6 = NULL;
1307 /* selector prefixlen thresholds to hash policies */
1308 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1309 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1311 if (nla_len(rta) < sizeof(*thresh4))
1313 thresh4 = nla_data(rta);
1314 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1317 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1318 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1320 if (nla_len(rta) < sizeof(*thresh6))
1322 thresh6 = nla_data(rta);
1323 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1327 if (thresh4 || thresh6) {
1328 write_seqlock(&net->xfrm.policy_hthresh.lock);
1330 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1331 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1334 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1335 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1337 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1339 xfrm_policy_hash_rebuild(net);
1345 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1346 struct nlattr **attrs)
1348 struct net *net = sock_net(skb->sk);
1349 struct sk_buff *r_skb;
1350 u32 *flags = nlmsg_data(nlh);
1351 u32 sportid = NETLINK_CB(skb).portid;
1352 u32 seq = nlh->nlmsg_seq;
1355 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1359 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1362 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1365 static inline unsigned int xfrm_sadinfo_msgsize(void)
1367 return NLMSG_ALIGN(4)
1368 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1369 + nla_total_size(4); /* XFRMA_SAD_CNT */
1372 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1373 u32 portid, u32 seq, u32 flags)
1375 struct xfrmk_sadinfo si;
1376 struct xfrmu_sadhinfo sh;
1377 struct nlmsghdr *nlh;
1381 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1382 if (nlh == NULL) /* shouldn't really happen ... */
1385 f = nlmsg_data(nlh);
1387 xfrm_sad_getinfo(net, &si);
1389 sh.sadhmcnt = si.sadhmcnt;
1390 sh.sadhcnt = si.sadhcnt;
1392 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1394 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1396 nlmsg_cancel(skb, nlh);
1400 nlmsg_end(skb, nlh);
1404 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1405 struct nlattr **attrs)
1407 struct net *net = sock_net(skb->sk);
1408 struct sk_buff *r_skb;
1409 u32 *flags = nlmsg_data(nlh);
1410 u32 sportid = NETLINK_CB(skb).portid;
1411 u32 seq = nlh->nlmsg_seq;
1414 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1418 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1421 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1424 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1425 struct nlattr **attrs)
1427 struct net *net = sock_net(skb->sk);
1428 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1429 struct xfrm_state *x;
1430 struct sk_buff *resp_skb;
1433 x = xfrm_user_state_lookup(net, p, attrs, &err);
1437 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1438 if (IS_ERR(resp_skb)) {
1439 err = PTR_ERR(resp_skb);
1441 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1448 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1449 struct nlattr **attrs)
1451 struct net *net = sock_net(skb->sk);
1452 struct xfrm_state *x;
1453 struct xfrm_userspi_info *p;
1454 struct xfrm_translator *xtr;
1455 struct sk_buff *resp_skb;
1456 xfrm_address_t *daddr;
1463 p = nlmsg_data(nlh);
1464 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1468 family = p->info.family;
1469 daddr = &p->info.id.daddr;
1473 mark = xfrm_mark_get(attrs, &m);
1475 if (attrs[XFRMA_IF_ID])
1476 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1479 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1480 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1487 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1488 if_id, p->info.id.proto, daddr,
1495 err = xfrm_alloc_spi(x, p->min, p->max);
1499 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1500 if (IS_ERR(resp_skb)) {
1501 err = PTR_ERR(resp_skb);
1505 xtr = xfrm_get_translator();
1507 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1509 xfrm_put_translator(xtr);
1511 kfree_skb(resp_skb);
1516 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1524 static int verify_policy_dir(u8 dir)
1527 case XFRM_POLICY_IN:
1528 case XFRM_POLICY_OUT:
1529 case XFRM_POLICY_FWD:
1539 static int verify_policy_type(u8 type)
1542 case XFRM_POLICY_TYPE_MAIN:
1543 #ifdef CONFIG_XFRM_SUB_POLICY
1544 case XFRM_POLICY_TYPE_SUB:
1555 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1560 case XFRM_SHARE_ANY:
1561 case XFRM_SHARE_SESSION:
1562 case XFRM_SHARE_USER:
1563 case XFRM_SHARE_UNIQUE:
1570 switch (p->action) {
1571 case XFRM_POLICY_ALLOW:
1572 case XFRM_POLICY_BLOCK:
1579 switch (p->sel.family) {
1581 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1587 #if IS_ENABLED(CONFIG_IPV6)
1588 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1593 return -EAFNOSUPPORT;
1600 ret = verify_policy_dir(p->dir);
1603 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1609 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1611 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1612 struct xfrm_user_sec_ctx *uctx;
1617 uctx = nla_data(rt);
1618 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1621 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1627 for (i = 0; i < nr; i++, ut++) {
1628 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1630 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1631 memcpy(&t->saddr, &ut->saddr,
1632 sizeof(xfrm_address_t));
1633 t->reqid = ut->reqid;
1635 t->share = ut->share;
1636 t->optional = ut->optional;
1637 t->aalgos = ut->aalgos;
1638 t->ealgos = ut->ealgos;
1639 t->calgos = ut->calgos;
1640 /* If all masks are ~0, then we allow all algorithms. */
1641 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1642 t->encap_family = ut->family;
1646 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1651 if (nr > XFRM_MAX_DEPTH)
1654 prev_family = family;
1656 for (i = 0; i < nr; i++) {
1657 /* We never validated the ut->family value, so many
1658 * applications simply leave it at zero. The check was
1659 * never made and ut->family was ignored because all
1660 * templates could be assumed to have the same family as
1661 * the policy itself. Now that we will have ipv4-in-ipv6
1662 * and ipv6-in-ipv4 tunnels, this is no longer true.
1665 ut[i].family = family;
1667 switch (ut[i].mode) {
1668 case XFRM_MODE_TUNNEL:
1669 case XFRM_MODE_BEET:
1672 if (ut[i].family != prev_family)
1676 if (ut[i].mode >= XFRM_MODE_MAX)
1679 prev_family = ut[i].family;
1681 switch (ut[i].family) {
1684 #if IS_ENABLED(CONFIG_IPV6)
1692 if (!xfrm_id_proto_valid(ut[i].id.proto))
1699 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1701 struct nlattr *rt = attrs[XFRMA_TMPL];
1706 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1707 int nr = nla_len(rt) / sizeof(*utmpl);
1710 err = validate_tmpl(nr, utmpl, pol->family);
1714 copy_templates(pol, utmpl, nr);
1719 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1721 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1722 struct xfrm_userpolicy_type *upt;
1723 u8 type = XFRM_POLICY_TYPE_MAIN;
1731 err = verify_policy_type(type);
1739 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1741 xp->priority = p->priority;
1742 xp->index = p->index;
1743 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1744 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1745 xp->action = p->action;
1746 xp->flags = p->flags;
1747 xp->family = p->sel.family;
1748 /* XXX xp->share = p->share; */
1751 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1753 memset(p, 0, sizeof(*p));
1754 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1755 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1756 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1757 p->priority = xp->priority;
1758 p->index = xp->index;
1759 p->sel.family = xp->family;
1761 p->action = xp->action;
1762 p->flags = xp->flags;
1763 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1766 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1768 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1776 copy_from_user_policy(xp, p);
1778 err = copy_from_user_policy_type(&xp->type, attrs);
1782 if (!(err = copy_from_user_tmpl(xp, attrs)))
1783 err = copy_from_user_sec_ctx(xp, attrs);
1787 xfrm_mark_get(attrs, &xp->mark);
1789 if (attrs[XFRMA_IF_ID])
1790 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1796 xfrm_policy_destroy(xp);
1800 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1801 struct nlattr **attrs)
1803 struct net *net = sock_net(skb->sk);
1804 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1805 struct xfrm_policy *xp;
1810 err = verify_newpolicy_info(p);
1813 err = verify_sec_ctx_len(attrs);
1817 xp = xfrm_policy_construct(net, p, attrs, &err);
1821 /* shouldn't excl be based on nlh flags??
1822 * Aha! this is anti-netlink really i.e more pfkey derived
1823 * in netlink excl is a flag and you wouldn't need
1824 * a type XFRM_MSG_UPDPOLICY - JHS */
1825 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1826 err = xfrm_policy_insert(p->dir, xp, excl);
1827 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1830 security_xfrm_policy_free(xp->security);
1835 c.event = nlh->nlmsg_type;
1836 c.seq = nlh->nlmsg_seq;
1837 c.portid = nlh->nlmsg_pid;
1838 km_policy_notify(xp, p->dir, &c);
1845 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1847 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1850 if (xp->xfrm_nr == 0)
1853 for (i = 0; i < xp->xfrm_nr; i++) {
1854 struct xfrm_user_tmpl *up = &vec[i];
1855 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1857 memset(up, 0, sizeof(*up));
1858 memcpy(&up->id, &kp->id, sizeof(up->id));
1859 up->family = kp->encap_family;
1860 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1861 up->reqid = kp->reqid;
1862 up->mode = kp->mode;
1863 up->share = kp->share;
1864 up->optional = kp->optional;
1865 up->aalgos = kp->aalgos;
1866 up->ealgos = kp->ealgos;
1867 up->calgos = kp->calgos;
1870 return nla_put(skb, XFRMA_TMPL,
1871 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1874 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1877 return copy_sec_ctx(x->security, skb);
1882 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1885 return copy_sec_ctx(xp->security, skb);
1888 static inline unsigned int userpolicy_type_attrsize(void)
1890 #ifdef CONFIG_XFRM_SUB_POLICY
1891 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1897 #ifdef CONFIG_XFRM_SUB_POLICY
1898 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1900 struct xfrm_userpolicy_type upt;
1902 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1903 memset(&upt, 0, sizeof(upt));
1906 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1910 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1916 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1918 struct xfrm_dump_info *sp = ptr;
1919 struct xfrm_userpolicy_info *p;
1920 struct sk_buff *in_skb = sp->in_skb;
1921 struct sk_buff *skb = sp->out_skb;
1922 struct xfrm_translator *xtr;
1923 struct nlmsghdr *nlh;
1926 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1927 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1931 p = nlmsg_data(nlh);
1932 copy_to_user_policy(xp, p, dir);
1933 err = copy_to_user_tmpl(xp, skb);
1935 err = copy_to_user_sec_ctx(xp, skb);
1937 err = copy_to_user_policy_type(xp->type, skb);
1939 err = xfrm_mark_put(skb, &xp->mark);
1941 err = xfrm_if_id_put(skb, xp->if_id);
1943 nlmsg_cancel(skb, nlh);
1946 nlmsg_end(skb, nlh);
1948 xtr = xfrm_get_translator();
1950 err = xtr->alloc_compat(skb, nlh);
1952 xfrm_put_translator(xtr);
1954 nlmsg_cancel(skb, nlh);
1962 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1964 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1965 struct net *net = sock_net(cb->skb->sk);
1967 xfrm_policy_walk_done(walk, net);
1971 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1973 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1975 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1977 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1981 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1983 struct net *net = sock_net(skb->sk);
1984 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1985 struct xfrm_dump_info info;
1987 info.in_skb = cb->skb;
1989 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1990 info.nlmsg_flags = NLM_F_MULTI;
1992 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1997 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1998 struct xfrm_policy *xp,
2001 struct xfrm_dump_info info;
2002 struct sk_buff *skb;
2005 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2007 return ERR_PTR(-ENOMEM);
2009 info.in_skb = in_skb;
2011 info.nlmsg_seq = seq;
2012 info.nlmsg_flags = 0;
2014 err = dump_one_policy(xp, dir, 0, &info);
2017 return ERR_PTR(err);
2023 static int xfrm_notify_userpolicy(struct net *net)
2025 struct xfrm_userpolicy_default *up;
2026 int len = NLMSG_ALIGN(sizeof(*up));
2027 struct nlmsghdr *nlh;
2028 struct sk_buff *skb;
2031 skb = nlmsg_new(len, GFP_ATOMIC);
2035 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
2041 up = nlmsg_data(nlh);
2042 up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2043 up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2044 up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2046 nlmsg_end(skb, nlh);
2049 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2055 static bool xfrm_userpolicy_is_valid(__u8 policy)
2057 return policy == XFRM_USERPOLICY_BLOCK ||
2058 policy == XFRM_USERPOLICY_ACCEPT;
2061 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2062 struct nlattr **attrs)
2064 struct net *net = sock_net(skb->sk);
2065 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2067 if (xfrm_userpolicy_is_valid(up->in))
2068 net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
2070 if (xfrm_userpolicy_is_valid(up->fwd))
2071 net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
2073 if (xfrm_userpolicy_is_valid(up->out))
2074 net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
2076 rt_genid_bump_all(net);
2078 xfrm_notify_userpolicy(net);
2082 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2083 struct nlattr **attrs)
2085 struct sk_buff *r_skb;
2086 struct nlmsghdr *r_nlh;
2087 struct net *net = sock_net(skb->sk);
2088 struct xfrm_userpolicy_default *r_up;
2089 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2090 u32 portid = NETLINK_CB(skb).portid;
2091 u32 seq = nlh->nlmsg_seq;
2093 r_skb = nlmsg_new(len, GFP_ATOMIC);
2097 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2103 r_up = nlmsg_data(r_nlh);
2104 r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2105 r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2106 r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2107 nlmsg_end(r_skb, r_nlh);
2109 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2112 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2113 struct nlattr **attrs)
2115 struct net *net = sock_net(skb->sk);
2116 struct xfrm_policy *xp;
2117 struct xfrm_userpolicy_id *p;
2118 u8 type = XFRM_POLICY_TYPE_MAIN;
2125 p = nlmsg_data(nlh);
2126 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2128 err = copy_from_user_policy_type(&type, attrs);
2132 err = verify_policy_dir(p->dir);
2136 if (attrs[XFRMA_IF_ID])
2137 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2139 xfrm_mark_get(attrs, &m);
2142 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2143 p->index, delete, &err);
2145 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2146 struct xfrm_sec_ctx *ctx;
2148 err = verify_sec_ctx_len(attrs);
2154 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2156 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2160 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2161 &p->sel, ctx, delete, &err);
2162 security_xfrm_policy_free(ctx);
2168 struct sk_buff *resp_skb;
2170 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2171 if (IS_ERR(resp_skb)) {
2172 err = PTR_ERR(resp_skb);
2174 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
2175 NETLINK_CB(skb).portid);
2178 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2183 c.data.byid = p->index;
2184 c.event = nlh->nlmsg_type;
2185 c.seq = nlh->nlmsg_seq;
2186 c.portid = nlh->nlmsg_pid;
2187 km_policy_notify(xp, p->dir, &c);
2195 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2196 struct nlattr **attrs)
2198 struct net *net = sock_net(skb->sk);
2200 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2203 err = xfrm_state_flush(net, p->proto, true, false);
2205 if (err == -ESRCH) /* empty table */
2209 c.data.proto = p->proto;
2210 c.event = nlh->nlmsg_type;
2211 c.seq = nlh->nlmsg_seq;
2212 c.portid = nlh->nlmsg_pid;
2214 km_state_notify(NULL, &c);
2219 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2221 unsigned int replay_size = x->replay_esn ?
2222 xfrm_replay_state_esn_len(x->replay_esn) :
2223 sizeof(struct xfrm_replay_state);
2225 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2226 + nla_total_size(replay_size)
2227 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2228 + nla_total_size(sizeof(struct xfrm_mark))
2229 + nla_total_size(4) /* XFRM_AE_RTHR */
2230 + nla_total_size(4); /* XFRM_AE_ETHR */
2233 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2235 struct xfrm_aevent_id *id;
2236 struct nlmsghdr *nlh;
2239 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2243 id = nlmsg_data(nlh);
2244 memset(&id->sa_id, 0, sizeof(id->sa_id));
2245 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2246 id->sa_id.spi = x->id.spi;
2247 id->sa_id.family = x->props.family;
2248 id->sa_id.proto = x->id.proto;
2249 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2250 id->reqid = x->props.reqid;
2251 id->flags = c->data.aevent;
2253 if (x->replay_esn) {
2254 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2255 xfrm_replay_state_esn_len(x->replay_esn),
2258 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2263 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2268 if (id->flags & XFRM_AE_RTHR) {
2269 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2273 if (id->flags & XFRM_AE_ETHR) {
2274 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2275 x->replay_maxage * 10 / HZ);
2279 err = xfrm_mark_put(skb, &x->mark);
2283 err = xfrm_if_id_put(skb, x->if_id);
2287 nlmsg_end(skb, nlh);
2291 nlmsg_cancel(skb, nlh);
2295 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2296 struct nlattr **attrs)
2298 struct net *net = sock_net(skb->sk);
2299 struct xfrm_state *x;
2300 struct sk_buff *r_skb;
2305 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2306 struct xfrm_usersa_id *id = &p->sa_id;
2308 mark = xfrm_mark_get(attrs, &m);
2310 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2314 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2315 if (r_skb == NULL) {
2321 * XXX: is this lock really needed - none of the other
2322 * gets lock (the concern is things getting updated
2323 * while we are still reading) - jhs
2325 spin_lock_bh(&x->lock);
2326 c.data.aevent = p->flags;
2327 c.seq = nlh->nlmsg_seq;
2328 c.portid = nlh->nlmsg_pid;
2330 err = build_aevent(r_skb, x, &c);
2333 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2334 spin_unlock_bh(&x->lock);
2339 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2340 struct nlattr **attrs)
2342 struct net *net = sock_net(skb->sk);
2343 struct xfrm_state *x;
2348 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2349 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2350 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2351 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2352 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2353 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2355 if (!lt && !rp && !re && !et && !rt)
2358 /* pedantic mode - thou shalt sayeth replaceth */
2359 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2362 mark = xfrm_mark_get(attrs, &m);
2364 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2368 if (x->km.state != XFRM_STATE_VALID)
2371 err = xfrm_replay_verify_len(x->replay_esn, re);
2375 spin_lock_bh(&x->lock);
2376 xfrm_update_ae_params(x, attrs, 1);
2377 spin_unlock_bh(&x->lock);
2379 c.event = nlh->nlmsg_type;
2380 c.seq = nlh->nlmsg_seq;
2381 c.portid = nlh->nlmsg_pid;
2382 c.data.aevent = XFRM_AE_CU;
2383 km_state_notify(x, &c);
2390 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2391 struct nlattr **attrs)
2393 struct net *net = sock_net(skb->sk);
2395 u8 type = XFRM_POLICY_TYPE_MAIN;
2398 err = copy_from_user_policy_type(&type, attrs);
2402 err = xfrm_policy_flush(net, type, true);
2404 if (err == -ESRCH) /* empty table */
2410 c.event = nlh->nlmsg_type;
2411 c.seq = nlh->nlmsg_seq;
2412 c.portid = nlh->nlmsg_pid;
2414 km_policy_notify(NULL, 0, &c);
2418 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2419 struct nlattr **attrs)
2421 struct net *net = sock_net(skb->sk);
2422 struct xfrm_policy *xp;
2423 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2424 struct xfrm_userpolicy_info *p = &up->pol;
2425 u8 type = XFRM_POLICY_TYPE_MAIN;
2430 err = copy_from_user_policy_type(&type, attrs);
2434 err = verify_policy_dir(p->dir);
2438 if (attrs[XFRMA_IF_ID])
2439 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2441 xfrm_mark_get(attrs, &m);
2444 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2447 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2448 struct xfrm_sec_ctx *ctx;
2450 err = verify_sec_ctx_len(attrs);
2456 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2458 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2462 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2463 &p->sel, ctx, 0, &err);
2464 security_xfrm_policy_free(ctx);
2469 if (unlikely(xp->walk.dead))
2474 xfrm_policy_delete(xp, p->dir);
2475 xfrm_audit_policy_delete(xp, 1, true);
2477 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2484 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2485 struct nlattr **attrs)
2487 struct net *net = sock_net(skb->sk);
2488 struct xfrm_state *x;
2490 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2491 struct xfrm_usersa_info *p = &ue->state;
2493 u32 mark = xfrm_mark_get(attrs, &m);
2495 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2501 spin_lock_bh(&x->lock);
2503 if (x->km.state != XFRM_STATE_VALID)
2505 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2508 __xfrm_state_delete(x);
2509 xfrm_audit_state_delete(x, 1, true);
2513 spin_unlock_bh(&x->lock);
2518 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2519 struct nlattr **attrs)
2521 struct net *net = sock_net(skb->sk);
2522 struct xfrm_policy *xp;
2523 struct xfrm_user_tmpl *ut;
2525 struct nlattr *rt = attrs[XFRMA_TMPL];
2526 struct xfrm_mark mark;
2528 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2529 struct xfrm_state *x = xfrm_state_alloc(net);
2535 xfrm_mark_get(attrs, &mark);
2537 err = verify_newpolicy_info(&ua->policy);
2540 err = verify_sec_ctx_len(attrs);
2545 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2549 memcpy(&x->id, &ua->id, sizeof(ua->id));
2550 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2551 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2552 xp->mark.m = x->mark.m = mark.m;
2553 xp->mark.v = x->mark.v = mark.v;
2555 /* extract the templates and for each call km_key */
2556 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2557 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2558 memcpy(&x->id, &t->id, sizeof(x->id));
2559 x->props.mode = t->mode;
2560 x->props.reqid = t->reqid;
2561 x->props.family = ut->family;
2562 t->aalgos = ua->aalgos;
2563 t->ealgos = ua->ealgos;
2564 t->calgos = ua->calgos;
2565 err = km_query(x, t, xp);
2580 #ifdef CONFIG_XFRM_MIGRATE
2581 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2582 struct xfrm_kmaddress *k,
2583 struct nlattr **attrs, int *num)
2585 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2586 struct xfrm_user_migrate *um;
2590 struct xfrm_user_kmaddress *uk;
2592 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2593 memcpy(&k->local, &uk->local, sizeof(k->local));
2594 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2595 k->family = uk->family;
2596 k->reserved = uk->reserved;
2600 num_migrate = nla_len(rt) / sizeof(*um);
2602 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2605 for (i = 0; i < num_migrate; i++, um++, ma++) {
2606 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2607 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2608 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2609 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2611 ma->proto = um->proto;
2612 ma->mode = um->mode;
2613 ma->reqid = um->reqid;
2615 ma->old_family = um->old_family;
2616 ma->new_family = um->new_family;
2623 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2624 struct nlattr **attrs)
2626 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2627 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2628 struct xfrm_kmaddress km, *kmp;
2632 struct net *net = sock_net(skb->sk);
2633 struct xfrm_encap_tmpl *encap = NULL;
2636 if (attrs[XFRMA_MIGRATE] == NULL)
2639 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2641 err = copy_from_user_policy_type(&type, attrs);
2645 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2652 if (attrs[XFRMA_ENCAP]) {
2653 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2654 sizeof(*encap), GFP_KERNEL);
2659 if (attrs[XFRMA_IF_ID])
2660 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2662 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
2669 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2670 struct nlattr **attrs)
2672 return -ENOPROTOOPT;
2676 #ifdef CONFIG_XFRM_MIGRATE
2677 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2679 struct xfrm_user_migrate um;
2681 memset(&um, 0, sizeof(um));
2682 um.proto = m->proto;
2684 um.reqid = m->reqid;
2685 um.old_family = m->old_family;
2686 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2687 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2688 um.new_family = m->new_family;
2689 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2690 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2692 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2695 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2697 struct xfrm_user_kmaddress uk;
2699 memset(&uk, 0, sizeof(uk));
2700 uk.family = k->family;
2701 uk.reserved = k->reserved;
2702 memcpy(&uk.local, &k->local, sizeof(uk.local));
2703 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2705 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2708 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2711 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2712 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2713 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2714 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2715 + userpolicy_type_attrsize();
2718 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2719 int num_migrate, const struct xfrm_kmaddress *k,
2720 const struct xfrm_selector *sel,
2721 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2723 const struct xfrm_migrate *mp;
2724 struct xfrm_userpolicy_id *pol_id;
2725 struct nlmsghdr *nlh;
2728 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2732 pol_id = nlmsg_data(nlh);
2733 /* copy data from selector, dir, and type to the pol_id */
2734 memset(pol_id, 0, sizeof(*pol_id));
2735 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2739 err = copy_to_user_kmaddress(k, skb);
2744 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2748 err = copy_to_user_policy_type(type, skb);
2751 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2752 err = copy_to_user_migrate(mp, skb);
2757 nlmsg_end(skb, nlh);
2761 nlmsg_cancel(skb, nlh);
2765 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2766 const struct xfrm_migrate *m, int num_migrate,
2767 const struct xfrm_kmaddress *k,
2768 const struct xfrm_encap_tmpl *encap)
2770 struct net *net = &init_net;
2771 struct sk_buff *skb;
2774 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2780 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2783 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2786 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2787 const struct xfrm_migrate *m, int num_migrate,
2788 const struct xfrm_kmaddress *k,
2789 const struct xfrm_encap_tmpl *encap)
2791 return -ENOPROTOOPT;
2795 #define XMSGSIZE(type) sizeof(struct type)
2797 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2798 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2799 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2800 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2801 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2802 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2803 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2804 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2805 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2806 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2807 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2808 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2809 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2810 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2811 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2812 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2813 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2814 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2815 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2816 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2817 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2818 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2819 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2820 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2822 EXPORT_SYMBOL_GPL(xfrm_msg_min);
2826 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2827 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2828 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2829 [XFRMA_LASTUSED] = { .type = NLA_U64},
2830 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2831 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2832 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2833 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2834 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2835 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2836 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2837 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_user_sec_ctx) },
2838 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2839 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2840 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2841 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2842 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2843 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2844 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2845 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2846 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2847 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2848 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2849 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2850 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2851 [XFRMA_PROTO] = { .type = NLA_U8 },
2852 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2853 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2854 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2855 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2856 [XFRMA_IF_ID] = { .type = NLA_U32 },
2857 [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 },
2859 EXPORT_SYMBOL_GPL(xfrma_policy);
2861 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2862 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2863 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2866 static const struct xfrm_link {
2867 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2868 int (*start)(struct netlink_callback *);
2869 int (*dump)(struct sk_buff *, struct netlink_callback *);
2870 int (*done)(struct netlink_callback *);
2871 const struct nla_policy *nla_pol;
2873 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2874 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2875 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2876 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2877 .dump = xfrm_dump_sa,
2878 .done = xfrm_dump_sa_done },
2879 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2880 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2881 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2882 .start = xfrm_dump_policy_start,
2883 .dump = xfrm_dump_policy,
2884 .done = xfrm_dump_policy_done },
2885 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2886 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2887 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2888 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2889 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2890 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2891 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2892 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2893 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2894 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2895 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2896 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2897 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2898 .nla_pol = xfrma_spd_policy,
2899 .nla_max = XFRMA_SPD_MAX },
2900 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2901 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
2902 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
2905 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2906 struct netlink_ext_ack *extack)
2908 struct net *net = sock_net(skb->sk);
2909 struct nlattr *attrs[XFRMA_MAX+1];
2910 const struct xfrm_link *link;
2911 struct nlmsghdr *nlh64 = NULL;
2914 type = nlh->nlmsg_type;
2915 if (type > XFRM_MSG_MAX)
2918 type -= XFRM_MSG_BASE;
2919 link = &xfrm_dispatch[type];
2921 /* All operations require privileges, even GET */
2922 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2925 if (in_compat_syscall()) {
2926 struct xfrm_translator *xtr = xfrm_get_translator();
2931 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2932 link->nla_pol, extack);
2933 xfrm_put_translator(xtr);
2935 return PTR_ERR(nlh64);
2940 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2941 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2942 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2943 struct netlink_dump_control c = {
2944 .start = link->start,
2949 if (link->dump == NULL) {
2954 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2958 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2959 link->nla_max ? : XFRMA_MAX,
2960 link->nla_pol ? : xfrma_policy, extack);
2964 if (link->doit == NULL) {
2969 err = link->doit(skb, nlh, attrs);
2971 /* We need to free skb allocated in xfrm_alloc_compat() before
2972 * returning from this function, because consume_skb() won't take
2973 * care of frag_list since netlink destructor sets
2974 * sbk->head to NULL. (see netlink_skb_destructor())
2976 if (skb_has_frag_list(skb)) {
2977 kfree_skb(skb_shinfo(skb)->frag_list);
2978 skb_shinfo(skb)->frag_list = NULL;
2986 static void xfrm_netlink_rcv(struct sk_buff *skb)
2988 struct net *net = sock_net(skb->sk);
2990 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2991 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2992 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2995 static inline unsigned int xfrm_expire_msgsize(void)
2997 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2998 + nla_total_size(sizeof(struct xfrm_mark));
3001 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
3003 struct xfrm_user_expire *ue;
3004 struct nlmsghdr *nlh;
3007 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
3011 ue = nlmsg_data(nlh);
3012 copy_to_user_state(x, &ue->state);
3013 ue->hard = (c->data.hard != 0) ? 1 : 0;
3014 /* clear the padding bytes */
3015 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
3017 err = xfrm_mark_put(skb, &x->mark);
3021 err = xfrm_if_id_put(skb, x->if_id);
3025 nlmsg_end(skb, nlh);
3029 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
3031 struct net *net = xs_net(x);
3032 struct sk_buff *skb;
3034 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
3038 if (build_expire(skb, x, c) < 0) {
3043 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3046 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3048 struct net *net = xs_net(x);
3049 struct sk_buff *skb;
3052 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3056 err = build_aevent(skb, x, c);
3059 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3062 static int xfrm_notify_sa_flush(const struct km_event *c)
3064 struct net *net = c->net;
3065 struct xfrm_usersa_flush *p;
3066 struct nlmsghdr *nlh;
3067 struct sk_buff *skb;
3068 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
3070 skb = nlmsg_new(len, GFP_ATOMIC);
3074 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
3080 p = nlmsg_data(nlh);
3081 p->proto = c->data.proto;
3083 nlmsg_end(skb, nlh);
3085 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3088 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
3092 l += nla_total_size(aead_len(x->aead));
3094 l += nla_total_size(sizeof(struct xfrm_algo) +
3095 (x->aalg->alg_key_len + 7) / 8);
3096 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3099 l += nla_total_size(xfrm_alg_len(x->ealg));
3101 l += nla_total_size(sizeof(*x->calg));
3103 l += nla_total_size(sizeof(*x->encap));
3105 l += nla_total_size(sizeof(x->tfcpad));
3107 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3109 l += nla_total_size(sizeof(struct xfrm_replay_state));
3111 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3112 x->security->ctx_len);
3114 l += nla_total_size(sizeof(*x->coaddr));
3115 if (x->props.extra_flags)
3116 l += nla_total_size(sizeof(x->props.extra_flags));
3118 l += nla_total_size(sizeof(struct xfrm_user_offload));
3119 if (x->props.smark.v | x->props.smark.m) {
3120 l += nla_total_size(sizeof(x->props.smark.v));
3121 l += nla_total_size(sizeof(x->props.smark.m));
3124 l += nla_total_size(sizeof(x->if_id));
3126 /* Must count x->lastused as it may become non-zero behind our back. */
3127 l += nla_total_size_64bit(sizeof(u64));
3129 if (x->mapping_maxage)
3130 l += nla_total_size(sizeof(x->mapping_maxage));
3135 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
3137 struct net *net = xs_net(x);
3138 struct xfrm_usersa_info *p;
3139 struct xfrm_usersa_id *id;
3140 struct nlmsghdr *nlh;
3141 struct sk_buff *skb;
3142 unsigned int len = xfrm_sa_len(x);
3143 unsigned int headlen;
3146 headlen = sizeof(*p);
3147 if (c->event == XFRM_MSG_DELSA) {
3148 len += nla_total_size(headlen);
3149 headlen = sizeof(*id);
3150 len += nla_total_size(sizeof(struct xfrm_mark));
3152 len += NLMSG_ALIGN(headlen);
3154 skb = nlmsg_new(len, GFP_ATOMIC);
3158 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3163 p = nlmsg_data(nlh);
3164 if (c->event == XFRM_MSG_DELSA) {
3165 struct nlattr *attr;
3167 id = nlmsg_data(nlh);
3168 memset(id, 0, sizeof(*id));
3169 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3170 id->spi = x->id.spi;
3171 id->family = x->props.family;
3172 id->proto = x->id.proto;
3174 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
3181 err = copy_to_user_state_extra(x, p, skb);
3185 nlmsg_end(skb, nlh);
3187 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3194 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
3198 case XFRM_MSG_EXPIRE:
3199 return xfrm_exp_state_notify(x, c);
3200 case XFRM_MSG_NEWAE:
3201 return xfrm_aevent_state_notify(x, c);
3202 case XFRM_MSG_DELSA:
3203 case XFRM_MSG_UPDSA:
3204 case XFRM_MSG_NEWSA:
3205 return xfrm_notify_sa(x, c);
3206 case XFRM_MSG_FLUSHSA:
3207 return xfrm_notify_sa_flush(c);
3209 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3218 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3219 struct xfrm_policy *xp)
3221 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3222 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3223 + nla_total_size(sizeof(struct xfrm_mark))
3224 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3225 + userpolicy_type_attrsize();
3228 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
3229 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3231 __u32 seq = xfrm_get_acqseq();
3232 struct xfrm_user_acquire *ua;
3233 struct nlmsghdr *nlh;
3236 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3240 ua = nlmsg_data(nlh);
3241 memcpy(&ua->id, &x->id, sizeof(ua->id));
3242 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3243 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3244 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3245 ua->aalgos = xt->aalgos;
3246 ua->ealgos = xt->ealgos;
3247 ua->calgos = xt->calgos;
3248 ua->seq = x->km.seq = seq;
3250 err = copy_to_user_tmpl(xp, skb);
3252 err = copy_to_user_state_sec_ctx(x, skb);
3254 err = copy_to_user_policy_type(xp->type, skb);
3256 err = xfrm_mark_put(skb, &xp->mark);
3258 err = xfrm_if_id_put(skb, xp->if_id);
3260 nlmsg_cancel(skb, nlh);
3264 nlmsg_end(skb, nlh);
3268 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3269 struct xfrm_policy *xp)
3271 struct net *net = xs_net(x);
3272 struct sk_buff *skb;
3275 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3279 err = build_acquire(skb, x, xt, xp);
3282 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3285 /* User gives us xfrm_user_policy_info followed by an array of 0
3286 * or more templates.
3288 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3289 u8 *data, int len, int *dir)
3291 struct net *net = sock_net(sk);
3292 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3293 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3294 struct xfrm_policy *xp;
3297 switch (sk->sk_family) {
3299 if (opt != IP_XFRM_POLICY) {
3304 #if IS_ENABLED(CONFIG_IPV6)
3306 if (opt != IPV6_XFRM_POLICY) {
3319 if (len < sizeof(*p) ||
3320 verify_newpolicy_info(p))
3323 nr = ((len - sizeof(*p)) / sizeof(*ut));
3324 if (validate_tmpl(nr, ut, p->sel.family))
3327 if (p->dir > XFRM_POLICY_OUT)
3330 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3336 copy_from_user_policy(xp, p);
3337 xp->type = XFRM_POLICY_TYPE_MAIN;
3338 copy_templates(xp, ut, nr);
3345 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3347 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3348 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3349 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3350 + nla_total_size(sizeof(struct xfrm_mark))
3351 + userpolicy_type_attrsize();
3354 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3355 int dir, const struct km_event *c)
3357 struct xfrm_user_polexpire *upe;
3358 int hard = c->data.hard;
3359 struct nlmsghdr *nlh;
3362 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3366 upe = nlmsg_data(nlh);
3367 copy_to_user_policy(xp, &upe->pol, dir);
3368 err = copy_to_user_tmpl(xp, skb);
3370 err = copy_to_user_sec_ctx(xp, skb);
3372 err = copy_to_user_policy_type(xp->type, skb);
3374 err = xfrm_mark_put(skb, &xp->mark);
3376 err = xfrm_if_id_put(skb, xp->if_id);
3378 nlmsg_cancel(skb, nlh);
3383 nlmsg_end(skb, nlh);
3387 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3389 struct net *net = xp_net(xp);
3390 struct sk_buff *skb;
3393 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3397 err = build_polexpire(skb, xp, dir, c);
3400 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3403 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3405 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3406 struct net *net = xp_net(xp);
3407 struct xfrm_userpolicy_info *p;
3408 struct xfrm_userpolicy_id *id;
3409 struct nlmsghdr *nlh;
3410 struct sk_buff *skb;
3411 unsigned int headlen;
3414 headlen = sizeof(*p);
3415 if (c->event == XFRM_MSG_DELPOLICY) {
3416 len += nla_total_size(headlen);
3417 headlen = sizeof(*id);
3419 len += userpolicy_type_attrsize();
3420 len += nla_total_size(sizeof(struct xfrm_mark));
3421 len += NLMSG_ALIGN(headlen);
3423 skb = nlmsg_new(len, GFP_ATOMIC);
3427 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3432 p = nlmsg_data(nlh);
3433 if (c->event == XFRM_MSG_DELPOLICY) {
3434 struct nlattr *attr;
3436 id = nlmsg_data(nlh);
3437 memset(id, 0, sizeof(*id));
3440 id->index = xp->index;
3442 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3444 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3452 copy_to_user_policy(xp, p, dir);
3453 err = copy_to_user_tmpl(xp, skb);
3455 err = copy_to_user_policy_type(xp->type, skb);
3457 err = xfrm_mark_put(skb, &xp->mark);
3459 err = xfrm_if_id_put(skb, xp->if_id);
3463 nlmsg_end(skb, nlh);
3465 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3472 static int xfrm_notify_policy_flush(const struct km_event *c)
3474 struct net *net = c->net;
3475 struct nlmsghdr *nlh;
3476 struct sk_buff *skb;
3479 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3483 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3487 err = copy_to_user_policy_type(c->data.type, skb);
3491 nlmsg_end(skb, nlh);
3493 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3500 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3504 case XFRM_MSG_NEWPOLICY:
3505 case XFRM_MSG_UPDPOLICY:
3506 case XFRM_MSG_DELPOLICY:
3507 return xfrm_notify_policy(xp, dir, c);
3508 case XFRM_MSG_FLUSHPOLICY:
3509 return xfrm_notify_policy_flush(c);
3510 case XFRM_MSG_POLEXPIRE:
3511 return xfrm_exp_policy_notify(xp, dir, c);
3513 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3521 static inline unsigned int xfrm_report_msgsize(void)
3523 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3526 static int build_report(struct sk_buff *skb, u8 proto,
3527 struct xfrm_selector *sel, xfrm_address_t *addr)
3529 struct xfrm_user_report *ur;
3530 struct nlmsghdr *nlh;
3532 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3536 ur = nlmsg_data(nlh);
3538 memcpy(&ur->sel, sel, sizeof(ur->sel));
3541 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3543 nlmsg_cancel(skb, nlh);
3547 nlmsg_end(skb, nlh);
3551 static int xfrm_send_report(struct net *net, u8 proto,
3552 struct xfrm_selector *sel, xfrm_address_t *addr)
3554 struct sk_buff *skb;
3557 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3561 err = build_report(skb, proto, sel, addr);
3564 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3567 static inline unsigned int xfrm_mapping_msgsize(void)
3569 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3572 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3573 xfrm_address_t *new_saddr, __be16 new_sport)
3575 struct xfrm_user_mapping *um;
3576 struct nlmsghdr *nlh;
3578 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3582 um = nlmsg_data(nlh);
3584 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3585 um->id.spi = x->id.spi;
3586 um->id.family = x->props.family;
3587 um->id.proto = x->id.proto;
3588 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3589 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3590 um->new_sport = new_sport;
3591 um->old_sport = x->encap->encap_sport;
3592 um->reqid = x->props.reqid;
3594 nlmsg_end(skb, nlh);
3598 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3601 struct net *net = xs_net(x);
3602 struct sk_buff *skb;
3605 if (x->id.proto != IPPROTO_ESP)
3611 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3615 err = build_mapping(skb, x, ipaddr, sport);
3618 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3621 static bool xfrm_is_alive(const struct km_event *c)
3623 return (bool)xfrm_acquire_is_on(c->net);
3626 static struct xfrm_mgr netlink_mgr = {
3627 .notify = xfrm_send_state_notify,
3628 .acquire = xfrm_send_acquire,
3629 .compile_policy = xfrm_compile_policy,
3630 .notify_policy = xfrm_send_policy_notify,
3631 .report = xfrm_send_report,
3632 .migrate = xfrm_send_migrate,
3633 .new_mapping = xfrm_send_mapping,
3634 .is_alive = xfrm_is_alive,
3637 static int __net_init xfrm_user_net_init(struct net *net)
3640 struct netlink_kernel_cfg cfg = {
3641 .groups = XFRMNLGRP_MAX,
3642 .input = xfrm_netlink_rcv,
3645 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3648 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3649 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3653 static void __net_exit xfrm_user_net_pre_exit(struct net *net)
3655 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3658 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3662 list_for_each_entry(net, net_exit_list, exit_list)
3663 netlink_kernel_release(net->xfrm.nlsk_stash);
3666 static struct pernet_operations xfrm_user_net_ops = {
3667 .init = xfrm_user_net_init,
3668 .pre_exit = xfrm_user_net_pre_exit,
3669 .exit_batch = xfrm_user_net_exit,
3672 static int __init xfrm_user_init(void)
3676 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3678 rv = register_pernet_subsys(&xfrm_user_net_ops);
3681 rv = xfrm_register_km(&netlink_mgr);
3683 unregister_pernet_subsys(&xfrm_user_net_ops);
3687 static void __exit xfrm_user_exit(void)
3689 xfrm_unregister_km(&netlink_mgr);
3690 unregister_pernet_subsys(&xfrm_user_net_ops);
3693 module_init(xfrm_user_init);
3694 module_exit(xfrm_user_exit);
3695 MODULE_LICENSE("GPL");
3696 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);