1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
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>
28 #include <net/netlink.h>
30 #include <linux/uaccess.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
34 #include <asm/unaligned.h>
36 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
38 struct nlattr *rt = attrs[type];
39 struct xfrm_algo *algp;
45 if (nla_len(rt) < xfrm_alg_len(algp))
58 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
62 static int verify_auth_trunc(struct nlattr **attrs)
64 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
65 struct xfrm_algo_auth *algp;
71 if (nla_len(rt) < xfrm_alg_auth_len(algp))
74 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
78 static int verify_aead(struct nlattr **attrs)
80 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
81 struct xfrm_algo_aead *algp;
87 if (nla_len(rt) < aead_len(algp))
90 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
94 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
95 xfrm_address_t **addrp)
97 struct nlattr *rt = attrs[type];
100 *addrp = nla_data(rt);
103 static inline int verify_sec_ctx_len(struct nlattr **attrs)
105 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
106 struct xfrm_user_sec_ctx *uctx;
112 if (uctx->len > nla_len(rt) ||
113 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
119 static inline int verify_replay(struct xfrm_usersa_info *p,
120 struct nlattr **attrs)
122 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
123 struct xfrm_replay_state_esn *rs;
126 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
130 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
133 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
134 nla_len(rt) != sizeof(*rs))
137 /* As only ESP and AH support ESN feature. */
138 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
141 if (p->replay_window != 0)
147 static int verify_newsa_info(struct xfrm_usersa_info *p,
148 struct nlattr **attrs)
158 #if IS_ENABLED(CONFIG_IPV6)
169 switch (p->sel.family) {
174 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
180 #if IS_ENABLED(CONFIG_IPV6)
181 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
195 switch (p->id.proto) {
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] ||
207 if (attrs[XFRMA_ALG_COMP])
209 if (!attrs[XFRMA_ALG_AUTH] &&
210 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
211 !attrs[XFRMA_ALG_CRYPT] &&
212 !attrs[XFRMA_ALG_AEAD])
214 if ((attrs[XFRMA_ALG_AUTH] ||
215 attrs[XFRMA_ALG_AUTH_TRUNC] ||
216 attrs[XFRMA_ALG_CRYPT]) &&
217 attrs[XFRMA_ALG_AEAD])
219 if (attrs[XFRMA_TFCPAD] &&
220 p->mode != XFRM_MODE_TUNNEL)
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))
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])
255 if ((err = verify_aead(attrs)))
257 if ((err = verify_auth_trunc(attrs)))
259 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
261 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
263 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
265 if ((err = verify_sec_ctx_len(attrs)))
267 if ((err = verify_replay(p, attrs)))
272 case XFRM_MODE_TRANSPORT:
273 case XFRM_MODE_TUNNEL:
274 case XFRM_MODE_ROUTEOPTIMIZATION:
288 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
289 struct xfrm_algo_desc *(*get_byname)(const char *, int),
292 struct xfrm_algo *p, *ualg;
293 struct xfrm_algo_desc *algo;
298 ualg = nla_data(rta);
300 algo = get_byname(ualg->alg_name, 1);
303 *props = algo->desc.sadb_alg_id;
305 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
309 strcpy(p->alg_name, algo->name);
314 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
316 struct xfrm_algo *p, *ualg;
317 struct xfrm_algo_desc *algo;
322 ualg = nla_data(rta);
324 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
327 x->props.ealgo = algo->desc.sadb_alg_id;
329 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
333 strcpy(p->alg_name, algo->name);
335 x->geniv = algo->uinfo.encr.geniv;
339 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
342 struct xfrm_algo *ualg;
343 struct xfrm_algo_auth *p;
344 struct xfrm_algo_desc *algo;
349 ualg = nla_data(rta);
351 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
354 *props = algo->desc.sadb_alg_id;
356 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
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);
369 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
372 struct xfrm_algo_auth *p, *ualg;
373 struct xfrm_algo_desc *algo;
378 ualg = nla_data(rta);
380 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
383 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
385 *props = algo->desc.sadb_alg_id;
387 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
391 strcpy(p->alg_name, algo->name);
392 if (!p->alg_trunc_len)
393 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
399 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
401 struct xfrm_algo_aead *p, *ualg;
402 struct xfrm_algo_desc *algo;
407 ualg = nla_data(rta);
409 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
412 x->props.ealgo = algo->desc.sadb_alg_id;
414 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
418 strcpy(p->alg_name, algo->name);
420 x->geniv = algo->uinfo.aead.geniv;
424 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
427 struct xfrm_replay_state_esn *up;
430 if (!replay_esn || !rp)
434 ulen = xfrm_replay_state_esn_len(up);
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)
443 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
449 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
450 struct xfrm_replay_state_esn **preplay_esn,
453 struct xfrm_replay_state_esn *p, *pp, *up;
460 klen = xfrm_replay_state_esn_len(up);
461 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
463 p = kzalloc(klen, GFP_KERNEL);
467 pp = kzalloc(klen, GFP_KERNEL);
474 memcpy(pp, up, ulen);
482 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
487 len += sizeof(struct xfrm_user_sec_ctx);
488 len += xfrm_ctx->ctx_len;
493 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
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;
506 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
507 x->sel.family = p->family;
511 * someday when pfkey also has support, we could have the code
512 * somehow made shareable and move it to xfrm_state.c - JHS
515 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
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];
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));
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));
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;
550 x->replay_maxage = nla_get_u32(et);
553 x->replay_maxdiff = nla_get_u32(rt);
556 static struct xfrm_state *xfrm_state_construct(struct net *net,
557 struct xfrm_usersa_info *p,
558 struct nlattr **attrs,
561 struct xfrm_state *x = xfrm_state_alloc(net);
567 copy_from_user_state(x, p);
569 if (attrs[XFRMA_ENCAP]) {
570 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
571 sizeof(*x->encap), GFP_KERNEL);
572 if (x->encap == NULL)
576 if (attrs[XFRMA_COADDR]) {
577 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
578 sizeof(*x->coaddr), GFP_KERNEL);
579 if (x->coaddr == NULL)
583 if (attrs[XFRMA_SA_EXTRA_FLAGS])
584 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
586 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
588 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
589 attrs[XFRMA_ALG_AUTH_TRUNC])))
591 if (!x->props.aalgo) {
592 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
593 attrs[XFRMA_ALG_AUTH])))
596 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
598 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
599 xfrm_calg_get_byname,
600 attrs[XFRMA_ALG_COMP])))
603 if (attrs[XFRMA_TFCPAD])
604 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
606 xfrm_mark_get(attrs, &x->mark);
608 if (attrs[XFRMA_OUTPUT_MARK])
609 x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
611 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
615 if (attrs[XFRMA_SEC_CTX]) {
616 err = security_xfrm_state_alloc(x,
617 nla_data(attrs[XFRMA_SEC_CTX]));
622 if (attrs[XFRMA_OFFLOAD_DEV]) {
623 err = xfrm_dev_state_add(net, x,
624 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
629 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
630 attrs[XFRMA_REPLAY_ESN_VAL])))
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;
638 if ((err = xfrm_init_replay(x)))
641 /* override default values from above */
642 xfrm_update_ae_params(x, attrs, 0);
647 x->km.state = XFRM_STATE_DEAD;
654 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
655 struct nlattr **attrs)
657 struct net *net = sock_net(skb->sk);
658 struct xfrm_usersa_info *p = nlmsg_data(nlh);
659 struct xfrm_state *x;
663 err = verify_newsa_info(p, attrs);
667 x = xfrm_state_construct(net, p, attrs, &err);
672 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
673 err = xfrm_state_add(x);
675 err = xfrm_state_update(x);
677 xfrm_audit_state_add(x, err ? 0 : 1, true);
680 x->km.state = XFRM_STATE_DEAD;
681 xfrm_dev_state_delete(x);
686 c.seq = nlh->nlmsg_seq;
687 c.portid = nlh->nlmsg_pid;
688 c.event = nlh->nlmsg_type;
690 km_state_notify(x, &c);
696 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
697 struct xfrm_usersa_id *p,
698 struct nlattr **attrs,
701 struct xfrm_state *x = NULL;
704 u32 mark = xfrm_mark_get(attrs, &m);
706 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
708 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
710 xfrm_address_t *saddr = NULL;
712 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
719 x = xfrm_state_lookup_byaddr(net, mark,
721 p->proto, p->family);
730 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
731 struct nlattr **attrs)
733 struct net *net = sock_net(skb->sk);
734 struct xfrm_state *x;
737 struct xfrm_usersa_id *p = nlmsg_data(nlh);
739 x = xfrm_user_state_lookup(net, p, attrs, &err);
743 if ((err = security_xfrm_state_delete(x)) != 0)
746 if (xfrm_state_kern(x)) {
751 err = xfrm_state_delete(x);
756 c.seq = nlh->nlmsg_seq;
757 c.portid = nlh->nlmsg_pid;
758 c.event = nlh->nlmsg_type;
759 km_state_notify(x, &c);
762 xfrm_audit_state_delete(x, err ? 0 : 1, true);
767 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
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;
786 struct xfrm_dump_info {
787 struct sk_buff *in_skb;
788 struct sk_buff *out_skb;
793 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
795 struct xfrm_user_sec_ctx *uctx;
797 int ctx_size = sizeof(*uctx) + s->ctx_len;
799 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
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);
814 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
816 struct xfrm_user_offload *xuo;
819 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
823 xuo = nla_data(attr);
824 memset(xuo, 0, sizeof(*xuo));
825 xuo->ifindex = xso->dev->ifindex;
826 xuo->flags = xso->flags;
831 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
833 struct xfrm_algo *algo;
836 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
837 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
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;
849 /* Don't change this without updating xfrm_sa_len! */
850 static int copy_to_user_state_extra(struct xfrm_state *x,
851 struct xfrm_usersa_info *p,
856 copy_to_user_state(x, p);
858 if (x->props.extra_flags) {
859 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
860 x->props.extra_flags);
866 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
871 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
877 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
882 ret = copy_to_user_auth(x->aalg, skb);
884 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
885 xfrm_alg_auth_len(x->aalg), x->aalg);
890 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
895 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
900 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
905 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
909 ret = xfrm_mark_put(skb, &x->mark);
913 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
914 xfrm_replay_state_esn_len(x->replay_esn),
917 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
922 ret = copy_user_offload(&x->xso, skb);
925 if (x->props.output_mark) {
926 ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
931 ret = copy_sec_ctx(x->security, skb);
936 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
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;
945 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
946 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
952 err = copy_to_user_state_extra(x, p, skb);
954 nlmsg_cancel(skb, nlh);
961 static int xfrm_dump_sa_done(struct netlink_callback *cb)
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);
968 xfrm_state_walk_done(walk, net);
972 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
973 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
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;
979 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
980 sizeof(cb->args) - sizeof(cb->args[0]));
982 info.in_skb = cb->skb;
984 info.nlmsg_seq = cb->nlh->nlmsg_seq;
985 info.nlmsg_flags = NLM_F_MULTI;
988 struct nlattr *attrs[XFRMA_MAX+1];
989 struct xfrm_address_filter *filter = NULL;
993 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
998 if (attrs[XFRMA_ADDRESS_FILTER]) {
999 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1000 sizeof(*filter), GFP_KERNEL);
1005 if (attrs[XFRMA_PROTO])
1006 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1008 xfrm_state_walk_init(walk, proto, filter);
1012 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1017 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1018 struct xfrm_state *x, u32 seq)
1020 struct xfrm_dump_info info;
1021 struct sk_buff *skb;
1024 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1026 return ERR_PTR(-ENOMEM);
1028 info.in_skb = in_skb;
1030 info.nlmsg_seq = seq;
1031 info.nlmsg_flags = 0;
1033 err = dump_one_state(x, 0, &info);
1036 return ERR_PTR(err);
1042 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1043 * Must be called with RCU read lock.
1045 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1046 u32 pid, unsigned int group)
1048 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1055 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1058 static inline size_t xfrm_spdinfo_msgsize(void)
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));
1067 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1068 u32 portid, u32 seq, u32 flags)
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;
1079 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1080 if (nlh == NULL) /* shouldn't really happen ... */
1083 f = nlmsg_data(nlh);
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;
1096 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
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));
1104 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1106 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1108 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1110 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1112 nlmsg_cancel(skb, nlh);
1116 nlmsg_end(skb, nlh);
1120 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1121 struct nlattr **attrs)
1123 struct net *net = sock_net(skb->sk);
1124 struct xfrmu_spdhthresh *thresh4 = NULL;
1125 struct xfrmu_spdhthresh *thresh6 = NULL;
1127 /* selector prefixlen thresholds to hash policies */
1128 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1129 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1131 if (nla_len(rta) < sizeof(*thresh4))
1133 thresh4 = nla_data(rta);
1134 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1137 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1138 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1140 if (nla_len(rta) < sizeof(*thresh6))
1142 thresh6 = nla_data(rta);
1143 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1147 if (thresh4 || thresh6) {
1148 write_seqlock(&net->xfrm.policy_hthresh.lock);
1150 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1151 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1154 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1155 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1157 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1159 xfrm_policy_hash_rebuild(net);
1165 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1166 struct nlattr **attrs)
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;
1174 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1178 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1181 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1184 static inline size_t xfrm_sadinfo_msgsize(void)
1186 return NLMSG_ALIGN(4)
1187 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1188 + nla_total_size(4); /* XFRMA_SAD_CNT */
1191 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1192 u32 portid, u32 seq, u32 flags)
1194 struct xfrmk_sadinfo si;
1195 struct xfrmu_sadhinfo sh;
1196 struct nlmsghdr *nlh;
1200 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1201 if (nlh == NULL) /* shouldn't really happen ... */
1204 f = nlmsg_data(nlh);
1206 xfrm_sad_getinfo(net, &si);
1208 sh.sadhmcnt = si.sadhmcnt;
1209 sh.sadhcnt = si.sadhcnt;
1211 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1213 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1215 nlmsg_cancel(skb, nlh);
1219 nlmsg_end(skb, nlh);
1223 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1224 struct nlattr **attrs)
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;
1232 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1236 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1239 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1242 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1243 struct nlattr **attrs)
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;
1251 x = xfrm_user_state_lookup(net, p, attrs, &err);
1255 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1256 if (IS_ERR(resp_skb)) {
1257 err = PTR_ERR(resp_skb);
1259 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1266 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1267 struct nlattr **attrs)
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;
1279 p = nlmsg_data(nlh);
1280 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1284 family = p->info.family;
1285 daddr = &p->info.id.daddr;
1289 mark = xfrm_mark_get(attrs, &m);
1291 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1292 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1299 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1300 p->info.id.proto, daddr,
1307 err = xfrm_alloc_spi(x, p->min, p->max);
1311 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1312 if (IS_ERR(resp_skb)) {
1313 err = PTR_ERR(resp_skb);
1317 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1325 static int verify_policy_dir(u8 dir)
1328 case XFRM_POLICY_IN:
1329 case XFRM_POLICY_OUT:
1330 case XFRM_POLICY_FWD:
1340 static int verify_policy_type(u8 type)
1343 case XFRM_POLICY_TYPE_MAIN:
1344 #ifdef CONFIG_XFRM_SUB_POLICY
1345 case XFRM_POLICY_TYPE_SUB:
1356 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1361 case XFRM_SHARE_ANY:
1362 case XFRM_SHARE_SESSION:
1363 case XFRM_SHARE_USER:
1364 case XFRM_SHARE_UNIQUE:
1371 switch (p->action) {
1372 case XFRM_POLICY_ALLOW:
1373 case XFRM_POLICY_BLOCK:
1380 switch (p->sel.family) {
1382 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1388 #if IS_ENABLED(CONFIG_IPV6)
1389 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1394 return -EAFNOSUPPORT;
1401 ret = verify_policy_dir(p->dir);
1404 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1410 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1412 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1413 struct xfrm_user_sec_ctx *uctx;
1418 uctx = nla_data(rt);
1419 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1422 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1428 for (i = 0; i < nr; i++, ut++) {
1429 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
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;
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;
1447 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1452 if (nr > XFRM_MAX_DEPTH)
1455 prev_family = family;
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.
1466 ut[i].family = family;
1468 switch (ut[i].mode) {
1469 case XFRM_MODE_TUNNEL:
1470 case XFRM_MODE_BEET:
1473 if (ut[i].family != prev_family)
1477 if (ut[i].mode >= XFRM_MODE_MAX)
1480 prev_family = ut[i].family;
1482 switch (ut[i].family) {
1485 #if IS_ENABLED(CONFIG_IPV6)
1493 if (!xfrm_id_proto_valid(ut[i].id.proto))
1500 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1502 struct nlattr *rt = attrs[XFRMA_TMPL];
1507 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1508 int nr = nla_len(rt) / sizeof(*utmpl);
1511 err = validate_tmpl(nr, utmpl, pol->family);
1515 copy_templates(pol, utmpl, nr);
1520 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1522 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1523 struct xfrm_userpolicy_type *upt;
1524 u8 type = XFRM_POLICY_TYPE_MAIN;
1532 err = verify_policy_type(type);
1540 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
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; */
1552 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
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;
1562 p->action = xp->action;
1563 p->flags = xp->flags;
1564 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1567 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1569 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1577 copy_from_user_policy(xp, p);
1579 err = copy_from_user_policy_type(&xp->type, attrs);
1583 if (!(err = copy_from_user_tmpl(xp, attrs)))
1584 err = copy_from_user_sec_ctx(xp, attrs);
1588 xfrm_mark_get(attrs, &xp->mark);
1594 xfrm_policy_destroy(xp);
1598 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1599 struct nlattr **attrs)
1601 struct net *net = sock_net(skb->sk);
1602 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1603 struct xfrm_policy *xp;
1608 err = verify_newpolicy_info(p);
1611 err = verify_sec_ctx_len(attrs);
1615 xp = xfrm_policy_construct(net, p, attrs, &err);
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);
1628 security_xfrm_policy_free(xp->security);
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);
1643 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1645 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1648 if (xp->xfrm_nr == 0)
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];
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;
1668 return nla_put(skb, XFRMA_TMPL,
1669 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1672 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1675 return copy_sec_ctx(x->security, skb);
1680 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1683 return copy_sec_ctx(xp->security, skb);
1686 static inline size_t userpolicy_type_attrsize(void)
1688 #ifdef CONFIG_XFRM_SUB_POLICY
1689 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1695 #ifdef CONFIG_XFRM_SUB_POLICY
1696 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1698 struct xfrm_userpolicy_type upt;
1700 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1701 memset(&upt, 0, sizeof(upt));
1704 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1708 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1714 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
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;
1723 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1724 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1728 p = nlmsg_data(nlh);
1729 copy_to_user_policy(xp, p, dir);
1730 err = copy_to_user_tmpl(xp, skb);
1732 err = copy_to_user_sec_ctx(xp, skb);
1734 err = copy_to_user_policy_type(xp->type, skb);
1736 err = xfrm_mark_put(skb, &xp->mark);
1738 nlmsg_cancel(skb, nlh);
1741 nlmsg_end(skb, nlh);
1745 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1747 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1748 struct net *net = sock_net(cb->skb->sk);
1750 xfrm_policy_walk_done(walk, net);
1754 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1756 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1758 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1760 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1764 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
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;
1770 info.in_skb = cb->skb;
1772 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1773 info.nlmsg_flags = NLM_F_MULTI;
1775 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1780 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1781 struct xfrm_policy *xp,
1784 struct xfrm_dump_info info;
1785 struct sk_buff *skb;
1788 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1790 return ERR_PTR(-ENOMEM);
1792 info.in_skb = in_skb;
1794 info.nlmsg_seq = seq;
1795 info.nlmsg_flags = 0;
1797 err = dump_one_policy(xp, dir, 0, &info);
1800 return ERR_PTR(err);
1806 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1807 struct nlattr **attrs)
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;
1818 p = nlmsg_data(nlh);
1819 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1821 err = copy_from_user_policy_type(&type, attrs);
1825 err = verify_policy_dir(p->dir);
1829 xfrm_mark_get(attrs, &m);
1832 xp = xfrm_policy_byid(net, &m, type, p->dir, p->index, delete, &err);
1834 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1835 struct xfrm_sec_ctx *ctx;
1837 err = verify_sec_ctx_len(attrs);
1843 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1845 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1849 xp = xfrm_policy_bysel_ctx(net, &m, type, p->dir, &p->sel,
1851 security_xfrm_policy_free(ctx);
1857 struct sk_buff *resp_skb;
1859 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1860 if (IS_ERR(resp_skb)) {
1861 err = PTR_ERR(resp_skb);
1863 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1864 NETLINK_CB(skb).portid);
1867 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1872 c.data.byid = p->index;
1873 c.event = nlh->nlmsg_type;
1874 c.seq = nlh->nlmsg_seq;
1875 c.portid = nlh->nlmsg_pid;
1876 km_policy_notify(xp, p->dir, &c);
1884 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1885 struct nlattr **attrs)
1887 struct net *net = sock_net(skb->sk);
1889 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1892 err = xfrm_state_flush(net, p->proto, true);
1894 if (err == -ESRCH) /* empty table */
1898 c.data.proto = p->proto;
1899 c.event = nlh->nlmsg_type;
1900 c.seq = nlh->nlmsg_seq;
1901 c.portid = nlh->nlmsg_pid;
1903 km_state_notify(NULL, &c);
1908 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1910 size_t replay_size = x->replay_esn ?
1911 xfrm_replay_state_esn_len(x->replay_esn) :
1912 sizeof(struct xfrm_replay_state);
1914 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1915 + nla_total_size(replay_size)
1916 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
1917 + nla_total_size(sizeof(struct xfrm_mark))
1918 + nla_total_size(4) /* XFRM_AE_RTHR */
1919 + nla_total_size(4); /* XFRM_AE_ETHR */
1922 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1924 struct xfrm_aevent_id *id;
1925 struct nlmsghdr *nlh;
1928 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1932 id = nlmsg_data(nlh);
1933 memset(&id->sa_id, 0, sizeof(id->sa_id));
1934 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1935 id->sa_id.spi = x->id.spi;
1936 id->sa_id.family = x->props.family;
1937 id->sa_id.proto = x->id.proto;
1938 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1939 id->reqid = x->props.reqid;
1940 id->flags = c->data.aevent;
1942 if (x->replay_esn) {
1943 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1944 xfrm_replay_state_esn_len(x->replay_esn),
1947 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1952 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1957 if (id->flags & XFRM_AE_RTHR) {
1958 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1962 if (id->flags & XFRM_AE_ETHR) {
1963 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1964 x->replay_maxage * 10 / HZ);
1968 err = xfrm_mark_put(skb, &x->mark);
1972 nlmsg_end(skb, nlh);
1976 nlmsg_cancel(skb, nlh);
1980 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1981 struct nlattr **attrs)
1983 struct net *net = sock_net(skb->sk);
1984 struct xfrm_state *x;
1985 struct sk_buff *r_skb;
1990 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1991 struct xfrm_usersa_id *id = &p->sa_id;
1993 mark = xfrm_mark_get(attrs, &m);
1995 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1999 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2000 if (r_skb == NULL) {
2006 * XXX: is this lock really needed - none of the other
2007 * gets lock (the concern is things getting updated
2008 * while we are still reading) - jhs
2010 spin_lock_bh(&x->lock);
2011 c.data.aevent = p->flags;
2012 c.seq = nlh->nlmsg_seq;
2013 c.portid = nlh->nlmsg_pid;
2015 if (build_aevent(r_skb, x, &c) < 0)
2017 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2018 spin_unlock_bh(&x->lock);
2023 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2024 struct nlattr **attrs)
2026 struct net *net = sock_net(skb->sk);
2027 struct xfrm_state *x;
2032 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2033 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2034 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2035 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2036 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2037 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2039 if (!lt && !rp && !re && !et && !rt)
2042 /* pedantic mode - thou shalt sayeth replaceth */
2043 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2046 mark = xfrm_mark_get(attrs, &m);
2048 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2052 if (x->km.state != XFRM_STATE_VALID)
2055 err = xfrm_replay_verify_len(x->replay_esn, re);
2059 spin_lock_bh(&x->lock);
2060 xfrm_update_ae_params(x, attrs, 1);
2061 spin_unlock_bh(&x->lock);
2063 c.event = nlh->nlmsg_type;
2064 c.seq = nlh->nlmsg_seq;
2065 c.portid = nlh->nlmsg_pid;
2066 c.data.aevent = XFRM_AE_CU;
2067 km_state_notify(x, &c);
2074 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2075 struct nlattr **attrs)
2077 struct net *net = sock_net(skb->sk);
2079 u8 type = XFRM_POLICY_TYPE_MAIN;
2082 err = copy_from_user_policy_type(&type, attrs);
2086 err = xfrm_policy_flush(net, type, true);
2088 if (err == -ESRCH) /* empty table */
2094 c.event = nlh->nlmsg_type;
2095 c.seq = nlh->nlmsg_seq;
2096 c.portid = nlh->nlmsg_pid;
2098 km_policy_notify(NULL, 0, &c);
2102 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2103 struct nlattr **attrs)
2105 struct net *net = sock_net(skb->sk);
2106 struct xfrm_policy *xp;
2107 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2108 struct xfrm_userpolicy_info *p = &up->pol;
2109 u8 type = XFRM_POLICY_TYPE_MAIN;
2113 err = copy_from_user_policy_type(&type, attrs);
2117 err = verify_policy_dir(p->dir);
2121 xfrm_mark_get(attrs, &m);
2124 xp = xfrm_policy_byid(net, &m, type, p->dir, p->index, 0, &err);
2126 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2127 struct xfrm_sec_ctx *ctx;
2129 err = verify_sec_ctx_len(attrs);
2135 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2137 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2141 xp = xfrm_policy_bysel_ctx(net, &m, type, p->dir,
2142 &p->sel, ctx, 0, &err);
2143 security_xfrm_policy_free(ctx);
2148 if (unlikely(xp->walk.dead))
2153 xfrm_policy_delete(xp, p->dir);
2154 xfrm_audit_policy_delete(xp, 1, true);
2156 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2163 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2164 struct nlattr **attrs)
2166 struct net *net = sock_net(skb->sk);
2167 struct xfrm_state *x;
2169 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2170 struct xfrm_usersa_info *p = &ue->state;
2172 u32 mark = xfrm_mark_get(attrs, &m);
2174 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2180 spin_lock_bh(&x->lock);
2182 if (x->km.state != XFRM_STATE_VALID)
2184 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2187 __xfrm_state_delete(x);
2188 xfrm_audit_state_delete(x, 1, true);
2192 spin_unlock_bh(&x->lock);
2197 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2198 struct nlattr **attrs)
2200 struct net *net = sock_net(skb->sk);
2201 struct xfrm_policy *xp;
2202 struct xfrm_user_tmpl *ut;
2204 struct nlattr *rt = attrs[XFRMA_TMPL];
2205 struct xfrm_mark mark;
2207 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2208 struct xfrm_state *x = xfrm_state_alloc(net);
2214 xfrm_mark_get(attrs, &mark);
2216 err = verify_newpolicy_info(&ua->policy);
2219 err = verify_sec_ctx_len(attrs);
2224 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2228 memcpy(&x->id, &ua->id, sizeof(ua->id));
2229 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2230 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2231 xp->mark.m = x->mark.m = mark.m;
2232 xp->mark.v = x->mark.v = mark.v;
2234 /* extract the templates and for each call km_key */
2235 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2236 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2237 memcpy(&x->id, &t->id, sizeof(x->id));
2238 x->props.mode = t->mode;
2239 x->props.reqid = t->reqid;
2240 x->props.family = ut->family;
2241 t->aalgos = ua->aalgos;
2242 t->ealgos = ua->ealgos;
2243 t->calgos = ua->calgos;
2244 err = km_query(x, t, xp);
2259 #ifdef CONFIG_XFRM_MIGRATE
2260 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2261 struct xfrm_kmaddress *k,
2262 struct nlattr **attrs, int *num)
2264 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2265 struct xfrm_user_migrate *um;
2269 struct xfrm_user_kmaddress *uk;
2271 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2272 memcpy(&k->local, &uk->local, sizeof(k->local));
2273 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2274 k->family = uk->family;
2275 k->reserved = uk->reserved;
2279 num_migrate = nla_len(rt) / sizeof(*um);
2281 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2284 for (i = 0; i < num_migrate; i++, um++, ma++) {
2285 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2286 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2287 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2288 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2290 ma->proto = um->proto;
2291 ma->mode = um->mode;
2292 ma->reqid = um->reqid;
2294 ma->old_family = um->old_family;
2295 ma->new_family = um->new_family;
2302 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2303 struct nlattr **attrs)
2305 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2306 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2307 struct xfrm_kmaddress km, *kmp;
2311 struct net *net = sock_net(skb->sk);
2312 struct xfrm_encap_tmpl *encap = NULL;
2314 if (attrs[XFRMA_MIGRATE] == NULL)
2317 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2319 err = copy_from_user_policy_type(&type, attrs);
2323 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2330 if (attrs[XFRMA_ENCAP]) {
2331 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2332 sizeof(*encap), GFP_KERNEL);
2337 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2344 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2345 struct nlattr **attrs)
2347 return -ENOPROTOOPT;
2351 #ifdef CONFIG_XFRM_MIGRATE
2352 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2354 struct xfrm_user_migrate um;
2356 memset(&um, 0, sizeof(um));
2357 um.proto = m->proto;
2359 um.reqid = m->reqid;
2360 um.old_family = m->old_family;
2361 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2362 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2363 um.new_family = m->new_family;
2364 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2365 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2367 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2370 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2372 struct xfrm_user_kmaddress uk;
2374 memset(&uk, 0, sizeof(uk));
2375 uk.family = k->family;
2376 uk.reserved = k->reserved;
2377 memcpy(&uk.local, &k->local, sizeof(uk.local));
2378 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2380 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2383 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma,
2386 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2387 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2388 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2389 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2390 + userpolicy_type_attrsize();
2393 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2394 int num_migrate, const struct xfrm_kmaddress *k,
2395 const struct xfrm_selector *sel,
2396 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2398 const struct xfrm_migrate *mp;
2399 struct xfrm_userpolicy_id *pol_id;
2400 struct nlmsghdr *nlh;
2403 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2407 pol_id = nlmsg_data(nlh);
2408 /* copy data from selector, dir, and type to the pol_id */
2409 memset(pol_id, 0, sizeof(*pol_id));
2410 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2414 err = copy_to_user_kmaddress(k, skb);
2419 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2423 err = copy_to_user_policy_type(type, skb);
2426 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2427 err = copy_to_user_migrate(mp, skb);
2432 nlmsg_end(skb, nlh);
2436 nlmsg_cancel(skb, nlh);
2440 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2441 const struct xfrm_migrate *m, int num_migrate,
2442 const struct xfrm_kmaddress *k,
2443 const struct xfrm_encap_tmpl *encap)
2445 struct net *net = &init_net;
2446 struct sk_buff *skb;
2448 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2454 if (build_migrate(skb, m, num_migrate, k, sel, encap, dir, type) < 0)
2457 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2460 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2461 const struct xfrm_migrate *m, int num_migrate,
2462 const struct xfrm_kmaddress *k,
2463 const struct xfrm_encap_tmpl *encap)
2465 return -ENOPROTOOPT;
2469 #define XMSGSIZE(type) sizeof(struct type)
2471 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2472 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2473 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2474 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2475 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2476 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2477 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2478 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2479 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2480 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2481 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2482 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2483 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2484 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2485 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2486 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2487 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2488 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2489 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2490 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2491 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2492 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2497 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2498 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2499 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2500 [XFRMA_LASTUSED] = { .type = NLA_U64},
2501 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2502 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2503 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2504 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2505 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2506 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2507 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2508 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2509 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2510 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2511 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2512 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2513 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2514 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2515 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2516 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2517 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2518 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2519 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2520 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2521 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2522 [XFRMA_PROTO] = { .type = NLA_U8 },
2523 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2524 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2525 [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 },
2528 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2529 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2530 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2533 static const struct xfrm_link {
2534 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2535 int (*start)(struct netlink_callback *);
2536 int (*dump)(struct sk_buff *, struct netlink_callback *);
2537 int (*done)(struct netlink_callback *);
2538 const struct nla_policy *nla_pol;
2540 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2541 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2542 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2543 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2544 .dump = xfrm_dump_sa,
2545 .done = xfrm_dump_sa_done },
2546 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2547 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2548 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2549 .start = xfrm_dump_policy_start,
2550 .dump = xfrm_dump_policy,
2551 .done = xfrm_dump_policy_done },
2552 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2553 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2554 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2555 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2556 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2557 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2558 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2559 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2560 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2561 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2562 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2563 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2564 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2565 .nla_pol = xfrma_spd_policy,
2566 .nla_max = XFRMA_SPD_MAX },
2567 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2570 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2571 struct netlink_ext_ack *extack)
2573 struct net *net = sock_net(skb->sk);
2574 struct nlattr *attrs[XFRMA_MAX+1];
2575 const struct xfrm_link *link;
2578 #ifdef CONFIG_COMPAT
2579 if (in_compat_syscall())
2583 type = nlh->nlmsg_type;
2584 if (type > XFRM_MSG_MAX)
2587 type -= XFRM_MSG_BASE;
2588 link = &xfrm_dispatch[type];
2590 /* All operations require privileges, even GET */
2591 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2594 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2595 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2596 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2597 if (link->dump == NULL)
2601 struct netlink_dump_control c = {
2602 .start = link->start,
2606 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2610 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2611 link->nla_max ? : XFRMA_MAX,
2612 link->nla_pol ? : xfrma_policy, extack);
2616 if (link->doit == NULL)
2619 return link->doit(skb, nlh, attrs);
2622 static void xfrm_netlink_rcv(struct sk_buff *skb)
2624 struct net *net = sock_net(skb->sk);
2626 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2627 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2628 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2631 static inline size_t xfrm_expire_msgsize(void)
2633 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2634 + nla_total_size(sizeof(struct xfrm_mark));
2637 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2639 struct xfrm_user_expire *ue;
2640 struct nlmsghdr *nlh;
2643 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2647 ue = nlmsg_data(nlh);
2648 copy_to_user_state(x, &ue->state);
2649 ue->hard = (c->data.hard != 0) ? 1 : 0;
2650 /* clear the padding bytes */
2651 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2653 err = xfrm_mark_put(skb, &x->mark);
2657 nlmsg_end(skb, nlh);
2661 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2663 struct net *net = xs_net(x);
2664 struct sk_buff *skb;
2666 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2670 if (build_expire(skb, x, c) < 0) {
2675 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2678 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2680 struct net *net = xs_net(x);
2681 struct sk_buff *skb;
2683 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2687 if (build_aevent(skb, x, c) < 0)
2690 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2693 static int xfrm_notify_sa_flush(const struct km_event *c)
2695 struct net *net = c->net;
2696 struct xfrm_usersa_flush *p;
2697 struct nlmsghdr *nlh;
2698 struct sk_buff *skb;
2699 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2701 skb = nlmsg_new(len, GFP_ATOMIC);
2705 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2711 p = nlmsg_data(nlh);
2712 p->proto = c->data.proto;
2714 nlmsg_end(skb, nlh);
2716 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2719 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2723 l += nla_total_size(aead_len(x->aead));
2725 l += nla_total_size(sizeof(struct xfrm_algo) +
2726 (x->aalg->alg_key_len + 7) / 8);
2727 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2730 l += nla_total_size(xfrm_alg_len(x->ealg));
2732 l += nla_total_size(sizeof(*x->calg));
2734 l += nla_total_size(sizeof(*x->encap));
2736 l += nla_total_size(sizeof(x->tfcpad));
2738 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2740 l += nla_total_size(sizeof(struct xfrm_replay_state));
2742 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2743 x->security->ctx_len);
2745 l += nla_total_size(sizeof(*x->coaddr));
2746 if (x->props.extra_flags)
2747 l += nla_total_size(sizeof(x->props.extra_flags));
2749 l += nla_total_size(sizeof(x->xso));
2750 if (x->props.output_mark)
2751 l += nla_total_size(sizeof(x->props.output_mark));
2753 /* Must count x->lastused as it may become non-zero behind our back. */
2754 l += nla_total_size_64bit(sizeof(u64));
2759 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2761 struct net *net = xs_net(x);
2762 struct xfrm_usersa_info *p;
2763 struct xfrm_usersa_id *id;
2764 struct nlmsghdr *nlh;
2765 struct sk_buff *skb;
2766 int len = xfrm_sa_len(x);
2769 headlen = sizeof(*p);
2770 if (c->event == XFRM_MSG_DELSA) {
2771 len += nla_total_size(headlen);
2772 headlen = sizeof(*id);
2773 len += nla_total_size(sizeof(struct xfrm_mark));
2775 len += NLMSG_ALIGN(headlen);
2777 skb = nlmsg_new(len, GFP_ATOMIC);
2781 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2786 p = nlmsg_data(nlh);
2787 if (c->event == XFRM_MSG_DELSA) {
2788 struct nlattr *attr;
2790 id = nlmsg_data(nlh);
2791 memset(id, 0, sizeof(*id));
2792 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2793 id->spi = x->id.spi;
2794 id->family = x->props.family;
2795 id->proto = x->id.proto;
2797 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2804 err = copy_to_user_state_extra(x, p, skb);
2808 nlmsg_end(skb, nlh);
2810 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2817 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2821 case XFRM_MSG_EXPIRE:
2822 return xfrm_exp_state_notify(x, c);
2823 case XFRM_MSG_NEWAE:
2824 return xfrm_aevent_state_notify(x, c);
2825 case XFRM_MSG_DELSA:
2826 case XFRM_MSG_UPDSA:
2827 case XFRM_MSG_NEWSA:
2828 return xfrm_notify_sa(x, c);
2829 case XFRM_MSG_FLUSHSA:
2830 return xfrm_notify_sa_flush(c);
2832 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2841 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2842 struct xfrm_policy *xp)
2844 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2845 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2846 + nla_total_size(sizeof(struct xfrm_mark))
2847 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2848 + userpolicy_type_attrsize();
2851 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2852 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2854 __u32 seq = xfrm_get_acqseq();
2855 struct xfrm_user_acquire *ua;
2856 struct nlmsghdr *nlh;
2859 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2863 ua = nlmsg_data(nlh);
2864 memcpy(&ua->id, &x->id, sizeof(ua->id));
2865 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2866 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2867 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2868 ua->aalgos = xt->aalgos;
2869 ua->ealgos = xt->ealgos;
2870 ua->calgos = xt->calgos;
2871 ua->seq = x->km.seq = seq;
2873 err = copy_to_user_tmpl(xp, skb);
2875 err = copy_to_user_state_sec_ctx(x, skb);
2877 err = copy_to_user_policy_type(xp->type, skb);
2879 err = xfrm_mark_put(skb, &xp->mark);
2881 nlmsg_cancel(skb, nlh);
2885 nlmsg_end(skb, nlh);
2889 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2890 struct xfrm_policy *xp)
2892 struct net *net = xs_net(x);
2893 struct sk_buff *skb;
2895 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2899 if (build_acquire(skb, x, xt, xp) < 0)
2902 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2905 /* User gives us xfrm_user_policy_info followed by an array of 0
2906 * or more templates.
2908 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2909 u8 *data, int len, int *dir)
2911 struct net *net = sock_net(sk);
2912 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2913 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2914 struct xfrm_policy *xp;
2917 switch (sk->sk_family) {
2919 if (opt != IP_XFRM_POLICY) {
2924 #if IS_ENABLED(CONFIG_IPV6)
2926 if (opt != IPV6_XFRM_POLICY) {
2939 if (len < sizeof(*p) ||
2940 verify_newpolicy_info(p))
2943 nr = ((len - sizeof(*p)) / sizeof(*ut));
2944 if (validate_tmpl(nr, ut, p->sel.family))
2947 if (p->dir > XFRM_POLICY_OUT)
2950 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2956 copy_from_user_policy(xp, p);
2957 xp->type = XFRM_POLICY_TYPE_MAIN;
2958 copy_templates(xp, ut, nr);
2965 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2967 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2968 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2969 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2970 + nla_total_size(sizeof(struct xfrm_mark))
2971 + userpolicy_type_attrsize();
2974 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2975 int dir, const struct km_event *c)
2977 struct xfrm_user_polexpire *upe;
2978 int hard = c->data.hard;
2979 struct nlmsghdr *nlh;
2982 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2986 upe = nlmsg_data(nlh);
2987 copy_to_user_policy(xp, &upe->pol, dir);
2988 err = copy_to_user_tmpl(xp, skb);
2990 err = copy_to_user_sec_ctx(xp, skb);
2992 err = copy_to_user_policy_type(xp->type, skb);
2994 err = xfrm_mark_put(skb, &xp->mark);
2996 nlmsg_cancel(skb, nlh);
3001 nlmsg_end(skb, nlh);
3005 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3007 struct net *net = xp_net(xp);
3008 struct sk_buff *skb;
3010 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3014 if (build_polexpire(skb, xp, dir, c) < 0)
3017 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3020 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3022 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3023 struct net *net = xp_net(xp);
3024 struct xfrm_userpolicy_info *p;
3025 struct xfrm_userpolicy_id *id;
3026 struct nlmsghdr *nlh;
3027 struct sk_buff *skb;
3030 headlen = sizeof(*p);
3031 if (c->event == XFRM_MSG_DELPOLICY) {
3032 len += nla_total_size(headlen);
3033 headlen = sizeof(*id);
3035 len += userpolicy_type_attrsize();
3036 len += nla_total_size(sizeof(struct xfrm_mark));
3037 len += NLMSG_ALIGN(headlen);
3039 skb = nlmsg_new(len, GFP_ATOMIC);
3043 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3048 p = nlmsg_data(nlh);
3049 if (c->event == XFRM_MSG_DELPOLICY) {
3050 struct nlattr *attr;
3052 id = nlmsg_data(nlh);
3053 memset(id, 0, sizeof(*id));
3056 id->index = xp->index;
3058 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3060 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3068 copy_to_user_policy(xp, p, dir);
3069 err = copy_to_user_tmpl(xp, skb);
3071 err = copy_to_user_policy_type(xp->type, skb);
3073 err = xfrm_mark_put(skb, &xp->mark);
3077 nlmsg_end(skb, nlh);
3079 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3086 static int xfrm_notify_policy_flush(const struct km_event *c)
3088 struct net *net = c->net;
3089 struct nlmsghdr *nlh;
3090 struct sk_buff *skb;
3093 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3097 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3101 err = copy_to_user_policy_type(c->data.type, skb);
3105 nlmsg_end(skb, nlh);
3107 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3114 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3118 case XFRM_MSG_NEWPOLICY:
3119 case XFRM_MSG_UPDPOLICY:
3120 case XFRM_MSG_DELPOLICY:
3121 return xfrm_notify_policy(xp, dir, c);
3122 case XFRM_MSG_FLUSHPOLICY:
3123 return xfrm_notify_policy_flush(c);
3124 case XFRM_MSG_POLEXPIRE:
3125 return xfrm_exp_policy_notify(xp, dir, c);
3127 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3135 static inline size_t xfrm_report_msgsize(void)
3137 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3140 static int build_report(struct sk_buff *skb, u8 proto,
3141 struct xfrm_selector *sel, xfrm_address_t *addr)
3143 struct xfrm_user_report *ur;
3144 struct nlmsghdr *nlh;
3146 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3150 ur = nlmsg_data(nlh);
3152 memcpy(&ur->sel, sel, sizeof(ur->sel));
3155 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3157 nlmsg_cancel(skb, nlh);
3161 nlmsg_end(skb, nlh);
3165 static int xfrm_send_report(struct net *net, u8 proto,
3166 struct xfrm_selector *sel, xfrm_address_t *addr)
3168 struct sk_buff *skb;
3170 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3174 if (build_report(skb, proto, sel, addr) < 0)
3177 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3180 static inline size_t xfrm_mapping_msgsize(void)
3182 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3185 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3186 xfrm_address_t *new_saddr, __be16 new_sport)
3188 struct xfrm_user_mapping *um;
3189 struct nlmsghdr *nlh;
3191 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3195 um = nlmsg_data(nlh);
3197 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3198 um->id.spi = x->id.spi;
3199 um->id.family = x->props.family;
3200 um->id.proto = x->id.proto;
3201 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3202 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3203 um->new_sport = new_sport;
3204 um->old_sport = x->encap->encap_sport;
3205 um->reqid = x->props.reqid;
3207 nlmsg_end(skb, nlh);
3211 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3214 struct net *net = xs_net(x);
3215 struct sk_buff *skb;
3217 if (x->id.proto != IPPROTO_ESP)
3223 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3227 if (build_mapping(skb, x, ipaddr, sport) < 0)
3230 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3233 static bool xfrm_is_alive(const struct km_event *c)
3235 return (bool)xfrm_acquire_is_on(c->net);
3238 static struct xfrm_mgr netlink_mgr = {
3239 .notify = xfrm_send_state_notify,
3240 .acquire = xfrm_send_acquire,
3241 .compile_policy = xfrm_compile_policy,
3242 .notify_policy = xfrm_send_policy_notify,
3243 .report = xfrm_send_report,
3244 .migrate = xfrm_send_migrate,
3245 .new_mapping = xfrm_send_mapping,
3246 .is_alive = xfrm_is_alive,
3249 static int __net_init xfrm_user_net_init(struct net *net)
3252 struct netlink_kernel_cfg cfg = {
3253 .groups = XFRMNLGRP_MAX,
3254 .input = xfrm_netlink_rcv,
3257 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3260 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3261 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3265 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3268 list_for_each_entry(net, net_exit_list, exit_list)
3269 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3271 list_for_each_entry(net, net_exit_list, exit_list)
3272 netlink_kernel_release(net->xfrm.nlsk_stash);
3275 static struct pernet_operations xfrm_user_net_ops = {
3276 .init = xfrm_user_net_init,
3277 .exit_batch = xfrm_user_net_exit,
3280 static int __init xfrm_user_init(void)
3284 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3286 rv = register_pernet_subsys(&xfrm_user_net_ops);
3289 rv = xfrm_register_km(&netlink_mgr);
3291 unregister_pernet_subsys(&xfrm_user_net_ops);
3295 static void __exit xfrm_user_exit(void)
3297 xfrm_unregister_km(&netlink_mgr);
3298 unregister_pernet_subsys(&xfrm_user_net_ops);
3301 module_init(xfrm_user_init);
3302 module_exit(xfrm_user_exit);
3303 MODULE_LICENSE("GPL");
3304 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);