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];
524 if (re && x->replay_esn && x->preplay_esn) {
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);
1004 /* see addr_match(), (prefix length >> 5) << 2
1005 * will be used to compare xfrm_address_t
1007 if (filter->splen > (sizeof(xfrm_address_t) << 3) ||
1008 filter->dplen > (sizeof(xfrm_address_t) << 3)) {
1014 if (attrs[XFRMA_PROTO])
1015 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1017 xfrm_state_walk_init(walk, proto, filter);
1021 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1026 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1027 struct xfrm_state *x, u32 seq)
1029 struct xfrm_dump_info info;
1030 struct sk_buff *skb;
1033 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1035 return ERR_PTR(-ENOMEM);
1037 info.in_skb = in_skb;
1039 info.nlmsg_seq = seq;
1040 info.nlmsg_flags = 0;
1042 err = dump_one_state(x, 0, &info);
1045 return ERR_PTR(err);
1051 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1052 * Must be called with RCU read lock.
1054 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1055 u32 pid, unsigned int group)
1057 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1064 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1067 static inline size_t xfrm_spdinfo_msgsize(void)
1069 return NLMSG_ALIGN(4)
1070 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1071 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1072 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1073 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1076 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1077 u32 portid, u32 seq, u32 flags)
1079 struct xfrmk_spdinfo si;
1080 struct xfrmu_spdinfo spc;
1081 struct xfrmu_spdhinfo sph;
1082 struct xfrmu_spdhthresh spt4, spt6;
1083 struct nlmsghdr *nlh;
1088 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1089 if (nlh == NULL) /* shouldn't really happen ... */
1092 f = nlmsg_data(nlh);
1094 xfrm_spd_getinfo(net, &si);
1095 spc.incnt = si.incnt;
1096 spc.outcnt = si.outcnt;
1097 spc.fwdcnt = si.fwdcnt;
1098 spc.inscnt = si.inscnt;
1099 spc.outscnt = si.outscnt;
1100 spc.fwdscnt = si.fwdscnt;
1101 sph.spdhcnt = si.spdhcnt;
1102 sph.spdhmcnt = si.spdhmcnt;
1105 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1107 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1108 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1109 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1110 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1111 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1113 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1115 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1117 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1119 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1121 nlmsg_cancel(skb, nlh);
1125 nlmsg_end(skb, nlh);
1129 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1130 struct nlattr **attrs)
1132 struct net *net = sock_net(skb->sk);
1133 struct xfrmu_spdhthresh *thresh4 = NULL;
1134 struct xfrmu_spdhthresh *thresh6 = NULL;
1136 /* selector prefixlen thresholds to hash policies */
1137 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1138 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1140 if (nla_len(rta) < sizeof(*thresh4))
1142 thresh4 = nla_data(rta);
1143 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1146 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1147 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1149 if (nla_len(rta) < sizeof(*thresh6))
1151 thresh6 = nla_data(rta);
1152 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1156 if (thresh4 || thresh6) {
1157 write_seqlock(&net->xfrm.policy_hthresh.lock);
1159 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1160 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1163 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1164 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1166 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1168 xfrm_policy_hash_rebuild(net);
1174 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1175 struct nlattr **attrs)
1177 struct net *net = sock_net(skb->sk);
1178 struct sk_buff *r_skb;
1179 u32 *flags = nlmsg_data(nlh);
1180 u32 sportid = NETLINK_CB(skb).portid;
1181 u32 seq = nlh->nlmsg_seq;
1183 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1187 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1190 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1193 static inline size_t xfrm_sadinfo_msgsize(void)
1195 return NLMSG_ALIGN(4)
1196 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1197 + nla_total_size(4); /* XFRMA_SAD_CNT */
1200 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1201 u32 portid, u32 seq, u32 flags)
1203 struct xfrmk_sadinfo si;
1204 struct xfrmu_sadhinfo sh;
1205 struct nlmsghdr *nlh;
1209 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1210 if (nlh == NULL) /* shouldn't really happen ... */
1213 f = nlmsg_data(nlh);
1215 xfrm_sad_getinfo(net, &si);
1217 sh.sadhmcnt = si.sadhmcnt;
1218 sh.sadhcnt = si.sadhcnt;
1220 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1222 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1224 nlmsg_cancel(skb, nlh);
1228 nlmsg_end(skb, nlh);
1232 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1233 struct nlattr **attrs)
1235 struct net *net = sock_net(skb->sk);
1236 struct sk_buff *r_skb;
1237 u32 *flags = nlmsg_data(nlh);
1238 u32 sportid = NETLINK_CB(skb).portid;
1239 u32 seq = nlh->nlmsg_seq;
1241 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1245 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1248 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1251 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1252 struct nlattr **attrs)
1254 struct net *net = sock_net(skb->sk);
1255 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1256 struct xfrm_state *x;
1257 struct sk_buff *resp_skb;
1260 x = xfrm_user_state_lookup(net, p, attrs, &err);
1264 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1265 if (IS_ERR(resp_skb)) {
1266 err = PTR_ERR(resp_skb);
1268 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1275 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1276 struct nlattr **attrs)
1278 struct net *net = sock_net(skb->sk);
1279 struct xfrm_state *x;
1280 struct xfrm_userspi_info *p;
1281 struct sk_buff *resp_skb;
1282 xfrm_address_t *daddr;
1288 p = nlmsg_data(nlh);
1289 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1293 family = p->info.family;
1294 daddr = &p->info.id.daddr;
1298 mark = xfrm_mark_get(attrs, &m);
1300 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1301 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1308 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1309 p->info.id.proto, daddr,
1316 err = xfrm_alloc_spi(x, p->min, p->max);
1320 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1321 if (IS_ERR(resp_skb)) {
1322 err = PTR_ERR(resp_skb);
1326 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1334 static int verify_policy_dir(u8 dir)
1337 case XFRM_POLICY_IN:
1338 case XFRM_POLICY_OUT:
1339 case XFRM_POLICY_FWD:
1349 static int verify_policy_type(u8 type)
1352 case XFRM_POLICY_TYPE_MAIN:
1353 #ifdef CONFIG_XFRM_SUB_POLICY
1354 case XFRM_POLICY_TYPE_SUB:
1365 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1370 case XFRM_SHARE_ANY:
1371 case XFRM_SHARE_SESSION:
1372 case XFRM_SHARE_USER:
1373 case XFRM_SHARE_UNIQUE:
1380 switch (p->action) {
1381 case XFRM_POLICY_ALLOW:
1382 case XFRM_POLICY_BLOCK:
1389 switch (p->sel.family) {
1391 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1397 #if IS_ENABLED(CONFIG_IPV6)
1398 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1403 return -EAFNOSUPPORT;
1410 ret = verify_policy_dir(p->dir);
1413 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1419 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1421 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1422 struct xfrm_user_sec_ctx *uctx;
1427 uctx = nla_data(rt);
1428 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1431 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1437 for (i = 0; i < nr; i++, ut++) {
1438 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1440 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1441 memcpy(&t->saddr, &ut->saddr,
1442 sizeof(xfrm_address_t));
1443 t->reqid = ut->reqid;
1445 t->share = ut->share;
1446 t->optional = ut->optional;
1447 t->aalgos = ut->aalgos;
1448 t->ealgos = ut->ealgos;
1449 t->calgos = ut->calgos;
1450 /* If all masks are ~0, then we allow all algorithms. */
1451 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1452 t->encap_family = ut->family;
1456 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1461 if (nr > XFRM_MAX_DEPTH)
1464 prev_family = family;
1466 for (i = 0; i < nr; i++) {
1467 /* We never validated the ut->family value, so many
1468 * applications simply leave it at zero. The check was
1469 * never made and ut->family was ignored because all
1470 * templates could be assumed to have the same family as
1471 * the policy itself. Now that we will have ipv4-in-ipv6
1472 * and ipv6-in-ipv4 tunnels, this is no longer true.
1475 ut[i].family = family;
1477 switch (ut[i].mode) {
1478 case XFRM_MODE_TUNNEL:
1479 case XFRM_MODE_BEET:
1482 if (ut[i].family != prev_family)
1486 if (ut[i].mode >= XFRM_MODE_MAX)
1489 prev_family = ut[i].family;
1491 switch (ut[i].family) {
1494 #if IS_ENABLED(CONFIG_IPV6)
1502 if (!xfrm_id_proto_valid(ut[i].id.proto))
1509 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1511 struct nlattr *rt = attrs[XFRMA_TMPL];
1516 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1517 int nr = nla_len(rt) / sizeof(*utmpl);
1520 err = validate_tmpl(nr, utmpl, pol->family);
1524 copy_templates(pol, utmpl, nr);
1529 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1531 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1532 struct xfrm_userpolicy_type *upt;
1533 u8 type = XFRM_POLICY_TYPE_MAIN;
1541 err = verify_policy_type(type);
1549 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1551 xp->priority = p->priority;
1552 xp->index = p->index;
1553 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1554 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1555 xp->action = p->action;
1556 xp->flags = p->flags;
1557 xp->family = p->sel.family;
1558 /* XXX xp->share = p->share; */
1561 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1563 memset(p, 0, sizeof(*p));
1564 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1565 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1566 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1567 p->priority = xp->priority;
1568 p->index = xp->index;
1569 p->sel.family = xp->family;
1571 p->action = xp->action;
1572 p->flags = xp->flags;
1573 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1576 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1578 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1586 copy_from_user_policy(xp, p);
1588 err = copy_from_user_policy_type(&xp->type, attrs);
1592 if (!(err = copy_from_user_tmpl(xp, attrs)))
1593 err = copy_from_user_sec_ctx(xp, attrs);
1597 xfrm_mark_get(attrs, &xp->mark);
1603 xfrm_policy_destroy(xp);
1607 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1608 struct nlattr **attrs)
1610 struct net *net = sock_net(skb->sk);
1611 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1612 struct xfrm_policy *xp;
1617 err = verify_newpolicy_info(p);
1620 err = verify_sec_ctx_len(attrs);
1624 xp = xfrm_policy_construct(net, p, attrs, &err);
1628 /* shouldn't excl be based on nlh flags??
1629 * Aha! this is anti-netlink really i.e more pfkey derived
1630 * in netlink excl is a flag and you wouldnt need
1631 * a type XFRM_MSG_UPDPOLICY - JHS */
1632 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1633 err = xfrm_policy_insert(p->dir, xp, excl);
1634 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1637 security_xfrm_policy_free(xp->security);
1642 c.event = nlh->nlmsg_type;
1643 c.seq = nlh->nlmsg_seq;
1644 c.portid = nlh->nlmsg_pid;
1645 km_policy_notify(xp, p->dir, &c);
1652 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1654 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1657 if (xp->xfrm_nr == 0)
1660 for (i = 0; i < xp->xfrm_nr; i++) {
1661 struct xfrm_user_tmpl *up = &vec[i];
1662 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1664 memset(up, 0, sizeof(*up));
1665 memcpy(&up->id, &kp->id, sizeof(up->id));
1666 up->family = kp->encap_family;
1667 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1668 up->reqid = kp->reqid;
1669 up->mode = kp->mode;
1670 up->share = kp->share;
1671 up->optional = kp->optional;
1672 up->aalgos = kp->aalgos;
1673 up->ealgos = kp->ealgos;
1674 up->calgos = kp->calgos;
1677 return nla_put(skb, XFRMA_TMPL,
1678 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1681 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1684 return copy_sec_ctx(x->security, skb);
1689 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1692 return copy_sec_ctx(xp->security, skb);
1695 static inline size_t userpolicy_type_attrsize(void)
1697 #ifdef CONFIG_XFRM_SUB_POLICY
1698 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1704 #ifdef CONFIG_XFRM_SUB_POLICY
1705 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1707 struct xfrm_userpolicy_type upt;
1709 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1710 memset(&upt, 0, sizeof(upt));
1713 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1717 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1723 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1725 struct xfrm_dump_info *sp = ptr;
1726 struct xfrm_userpolicy_info *p;
1727 struct sk_buff *in_skb = sp->in_skb;
1728 struct sk_buff *skb = sp->out_skb;
1729 struct nlmsghdr *nlh;
1732 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1733 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1737 p = nlmsg_data(nlh);
1738 copy_to_user_policy(xp, p, dir);
1739 err = copy_to_user_tmpl(xp, skb);
1741 err = copy_to_user_sec_ctx(xp, skb);
1743 err = copy_to_user_policy_type(xp->type, skb);
1745 err = xfrm_mark_put(skb, &xp->mark);
1747 nlmsg_cancel(skb, nlh);
1750 nlmsg_end(skb, nlh);
1754 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1756 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1757 struct net *net = sock_net(cb->skb->sk);
1759 xfrm_policy_walk_done(walk, net);
1763 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1765 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1767 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1769 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1773 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1775 struct net *net = sock_net(skb->sk);
1776 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1777 struct xfrm_dump_info info;
1779 info.in_skb = cb->skb;
1781 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1782 info.nlmsg_flags = NLM_F_MULTI;
1784 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1789 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1790 struct xfrm_policy *xp,
1793 struct xfrm_dump_info info;
1794 struct sk_buff *skb;
1797 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1799 return ERR_PTR(-ENOMEM);
1801 info.in_skb = in_skb;
1803 info.nlmsg_seq = seq;
1804 info.nlmsg_flags = 0;
1806 err = dump_one_policy(xp, dir, 0, &info);
1809 return ERR_PTR(err);
1815 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1816 struct nlattr **attrs)
1818 struct net *net = sock_net(skb->sk);
1819 struct xfrm_policy *xp;
1820 struct xfrm_userpolicy_id *p;
1821 u8 type = XFRM_POLICY_TYPE_MAIN;
1827 p = nlmsg_data(nlh);
1828 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1830 err = copy_from_user_policy_type(&type, attrs);
1834 err = verify_policy_dir(p->dir);
1838 xfrm_mark_get(attrs, &m);
1841 xp = xfrm_policy_byid(net, &m, type, p->dir, p->index, delete, &err);
1843 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1844 struct xfrm_sec_ctx *ctx;
1846 err = verify_sec_ctx_len(attrs);
1852 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1854 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1858 xp = xfrm_policy_bysel_ctx(net, &m, type, p->dir, &p->sel,
1860 security_xfrm_policy_free(ctx);
1866 struct sk_buff *resp_skb;
1868 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1869 if (IS_ERR(resp_skb)) {
1870 err = PTR_ERR(resp_skb);
1872 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1873 NETLINK_CB(skb).portid);
1876 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1881 c.data.byid = p->index;
1882 c.event = nlh->nlmsg_type;
1883 c.seq = nlh->nlmsg_seq;
1884 c.portid = nlh->nlmsg_pid;
1885 km_policy_notify(xp, p->dir, &c);
1893 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1894 struct nlattr **attrs)
1896 struct net *net = sock_net(skb->sk);
1898 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1901 err = xfrm_state_flush(net, p->proto, true);
1903 if (err == -ESRCH) /* empty table */
1907 c.data.proto = p->proto;
1908 c.event = nlh->nlmsg_type;
1909 c.seq = nlh->nlmsg_seq;
1910 c.portid = nlh->nlmsg_pid;
1912 km_state_notify(NULL, &c);
1917 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1919 size_t replay_size = x->replay_esn ?
1920 xfrm_replay_state_esn_len(x->replay_esn) :
1921 sizeof(struct xfrm_replay_state);
1923 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1924 + nla_total_size(replay_size)
1925 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
1926 + nla_total_size(sizeof(struct xfrm_mark))
1927 + nla_total_size(4) /* XFRM_AE_RTHR */
1928 + nla_total_size(4); /* XFRM_AE_ETHR */
1931 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1933 struct xfrm_aevent_id *id;
1934 struct nlmsghdr *nlh;
1937 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1941 id = nlmsg_data(nlh);
1942 memset(&id->sa_id, 0, sizeof(id->sa_id));
1943 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1944 id->sa_id.spi = x->id.spi;
1945 id->sa_id.family = x->props.family;
1946 id->sa_id.proto = x->id.proto;
1947 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1948 id->reqid = x->props.reqid;
1949 id->flags = c->data.aevent;
1951 if (x->replay_esn) {
1952 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1953 xfrm_replay_state_esn_len(x->replay_esn),
1956 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1961 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1966 if (id->flags & XFRM_AE_RTHR) {
1967 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1971 if (id->flags & XFRM_AE_ETHR) {
1972 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1973 x->replay_maxage * 10 / HZ);
1977 err = xfrm_mark_put(skb, &x->mark);
1981 nlmsg_end(skb, nlh);
1985 nlmsg_cancel(skb, nlh);
1989 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1990 struct nlattr **attrs)
1992 struct net *net = sock_net(skb->sk);
1993 struct xfrm_state *x;
1994 struct sk_buff *r_skb;
1999 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2000 struct xfrm_usersa_id *id = &p->sa_id;
2002 mark = xfrm_mark_get(attrs, &m);
2004 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2008 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2009 if (r_skb == NULL) {
2015 * XXX: is this lock really needed - none of the other
2016 * gets lock (the concern is things getting updated
2017 * while we are still reading) - jhs
2019 spin_lock_bh(&x->lock);
2020 c.data.aevent = p->flags;
2021 c.seq = nlh->nlmsg_seq;
2022 c.portid = nlh->nlmsg_pid;
2024 if (build_aevent(r_skb, x, &c) < 0)
2026 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2027 spin_unlock_bh(&x->lock);
2032 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2033 struct nlattr **attrs)
2035 struct net *net = sock_net(skb->sk);
2036 struct xfrm_state *x;
2041 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2042 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2043 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2044 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2045 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2046 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2048 if (!lt && !rp && !re && !et && !rt)
2051 /* pedantic mode - thou shalt sayeth replaceth */
2052 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2055 mark = xfrm_mark_get(attrs, &m);
2057 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2061 if (x->km.state != XFRM_STATE_VALID)
2064 err = xfrm_replay_verify_len(x->replay_esn, re);
2068 spin_lock_bh(&x->lock);
2069 xfrm_update_ae_params(x, attrs, 1);
2070 spin_unlock_bh(&x->lock);
2072 c.event = nlh->nlmsg_type;
2073 c.seq = nlh->nlmsg_seq;
2074 c.portid = nlh->nlmsg_pid;
2075 c.data.aevent = XFRM_AE_CU;
2076 km_state_notify(x, &c);
2083 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2084 struct nlattr **attrs)
2086 struct net *net = sock_net(skb->sk);
2088 u8 type = XFRM_POLICY_TYPE_MAIN;
2091 err = copy_from_user_policy_type(&type, attrs);
2095 err = xfrm_policy_flush(net, type, true);
2097 if (err == -ESRCH) /* empty table */
2103 c.event = nlh->nlmsg_type;
2104 c.seq = nlh->nlmsg_seq;
2105 c.portid = nlh->nlmsg_pid;
2107 km_policy_notify(NULL, 0, &c);
2111 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2112 struct nlattr **attrs)
2114 struct net *net = sock_net(skb->sk);
2115 struct xfrm_policy *xp;
2116 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2117 struct xfrm_userpolicy_info *p = &up->pol;
2118 u8 type = XFRM_POLICY_TYPE_MAIN;
2122 err = copy_from_user_policy_type(&type, attrs);
2126 err = verify_policy_dir(p->dir);
2130 xfrm_mark_get(attrs, &m);
2133 xp = xfrm_policy_byid(net, &m, type, p->dir, p->index, 0, &err);
2135 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2136 struct xfrm_sec_ctx *ctx;
2138 err = verify_sec_ctx_len(attrs);
2144 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2146 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2150 xp = xfrm_policy_bysel_ctx(net, &m, type, p->dir,
2151 &p->sel, ctx, 0, &err);
2152 security_xfrm_policy_free(ctx);
2157 if (unlikely(xp->walk.dead))
2162 xfrm_policy_delete(xp, p->dir);
2163 xfrm_audit_policy_delete(xp, 1, true);
2165 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2172 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2173 struct nlattr **attrs)
2175 struct net *net = sock_net(skb->sk);
2176 struct xfrm_state *x;
2178 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2179 struct xfrm_usersa_info *p = &ue->state;
2181 u32 mark = xfrm_mark_get(attrs, &m);
2183 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2189 spin_lock_bh(&x->lock);
2191 if (x->km.state != XFRM_STATE_VALID)
2193 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2196 __xfrm_state_delete(x);
2197 xfrm_audit_state_delete(x, 1, true);
2201 spin_unlock_bh(&x->lock);
2206 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2207 struct nlattr **attrs)
2209 struct net *net = sock_net(skb->sk);
2210 struct xfrm_policy *xp;
2211 struct xfrm_user_tmpl *ut;
2213 struct nlattr *rt = attrs[XFRMA_TMPL];
2214 struct xfrm_mark mark;
2216 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2217 struct xfrm_state *x = xfrm_state_alloc(net);
2223 xfrm_mark_get(attrs, &mark);
2225 err = verify_newpolicy_info(&ua->policy);
2228 err = verify_sec_ctx_len(attrs);
2233 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2237 memcpy(&x->id, &ua->id, sizeof(ua->id));
2238 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2239 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2240 xp->mark.m = x->mark.m = mark.m;
2241 xp->mark.v = x->mark.v = mark.v;
2243 /* extract the templates and for each call km_key */
2244 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2245 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2246 memcpy(&x->id, &t->id, sizeof(x->id));
2247 x->props.mode = t->mode;
2248 x->props.reqid = t->reqid;
2249 x->props.family = ut->family;
2250 t->aalgos = ua->aalgos;
2251 t->ealgos = ua->ealgos;
2252 t->calgos = ua->calgos;
2253 err = km_query(x, t, xp);
2268 #ifdef CONFIG_XFRM_MIGRATE
2269 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2270 struct xfrm_kmaddress *k,
2271 struct nlattr **attrs, int *num)
2273 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2274 struct xfrm_user_migrate *um;
2278 struct xfrm_user_kmaddress *uk;
2280 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2281 memcpy(&k->local, &uk->local, sizeof(k->local));
2282 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2283 k->family = uk->family;
2284 k->reserved = uk->reserved;
2288 num_migrate = nla_len(rt) / sizeof(*um);
2290 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2293 for (i = 0; i < num_migrate; i++, um++, ma++) {
2294 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2295 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2296 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2297 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2299 ma->proto = um->proto;
2300 ma->mode = um->mode;
2301 ma->reqid = um->reqid;
2303 ma->old_family = um->old_family;
2304 ma->new_family = um->new_family;
2311 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2312 struct nlattr **attrs)
2314 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2315 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2316 struct xfrm_kmaddress km, *kmp;
2320 struct net *net = sock_net(skb->sk);
2321 struct xfrm_encap_tmpl *encap = NULL;
2323 if (attrs[XFRMA_MIGRATE] == NULL)
2326 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2328 err = copy_from_user_policy_type(&type, attrs);
2332 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2339 if (attrs[XFRMA_ENCAP]) {
2340 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2341 sizeof(*encap), GFP_KERNEL);
2346 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2353 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2354 struct nlattr **attrs)
2356 return -ENOPROTOOPT;
2360 #ifdef CONFIG_XFRM_MIGRATE
2361 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2363 struct xfrm_user_migrate um;
2365 memset(&um, 0, sizeof(um));
2366 um.proto = m->proto;
2368 um.reqid = m->reqid;
2369 um.old_family = m->old_family;
2370 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2371 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2372 um.new_family = m->new_family;
2373 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2374 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2376 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2379 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2381 struct xfrm_user_kmaddress uk;
2383 memset(&uk, 0, sizeof(uk));
2384 uk.family = k->family;
2385 uk.reserved = k->reserved;
2386 memcpy(&uk.local, &k->local, sizeof(uk.local));
2387 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2389 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2392 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma,
2395 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2396 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2397 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2398 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2399 + userpolicy_type_attrsize();
2402 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2403 int num_migrate, const struct xfrm_kmaddress *k,
2404 const struct xfrm_selector *sel,
2405 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2407 const struct xfrm_migrate *mp;
2408 struct xfrm_userpolicy_id *pol_id;
2409 struct nlmsghdr *nlh;
2412 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2416 pol_id = nlmsg_data(nlh);
2417 /* copy data from selector, dir, and type to the pol_id */
2418 memset(pol_id, 0, sizeof(*pol_id));
2419 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2423 err = copy_to_user_kmaddress(k, skb);
2428 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2432 err = copy_to_user_policy_type(type, skb);
2435 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2436 err = copy_to_user_migrate(mp, skb);
2441 nlmsg_end(skb, nlh);
2445 nlmsg_cancel(skb, nlh);
2449 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2450 const struct xfrm_migrate *m, int num_migrate,
2451 const struct xfrm_kmaddress *k,
2452 const struct xfrm_encap_tmpl *encap)
2454 struct net *net = &init_net;
2455 struct sk_buff *skb;
2457 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2463 if (build_migrate(skb, m, num_migrate, k, sel, encap, dir, type) < 0)
2466 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2469 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2470 const struct xfrm_migrate *m, int num_migrate,
2471 const struct xfrm_kmaddress *k,
2472 const struct xfrm_encap_tmpl *encap)
2474 return -ENOPROTOOPT;
2478 #define XMSGSIZE(type) sizeof(struct type)
2480 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2481 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2482 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2483 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2484 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2485 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2486 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2487 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2488 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2489 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2490 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2491 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2492 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2493 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2494 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2495 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2496 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2497 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2498 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2499 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2500 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2501 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2506 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2507 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2508 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2509 [XFRMA_LASTUSED] = { .type = NLA_U64},
2510 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2511 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2512 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2513 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2514 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2515 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2516 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2517 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_user_sec_ctx) },
2518 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2519 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2520 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2521 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2522 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2523 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2524 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2525 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2526 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2527 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2528 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2529 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2530 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2531 [XFRMA_PROTO] = { .type = NLA_U8 },
2532 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2533 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2534 [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 },
2537 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2538 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2539 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2542 static const struct xfrm_link {
2543 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2544 int (*start)(struct netlink_callback *);
2545 int (*dump)(struct sk_buff *, struct netlink_callback *);
2546 int (*done)(struct netlink_callback *);
2547 const struct nla_policy *nla_pol;
2549 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2550 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2551 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2552 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2553 .dump = xfrm_dump_sa,
2554 .done = xfrm_dump_sa_done },
2555 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2556 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2557 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2558 .start = xfrm_dump_policy_start,
2559 .dump = xfrm_dump_policy,
2560 .done = xfrm_dump_policy_done },
2561 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2562 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2563 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2564 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2565 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2566 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2567 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2568 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2569 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2570 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2571 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2572 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2573 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2574 .nla_pol = xfrma_spd_policy,
2575 .nla_max = XFRMA_SPD_MAX },
2576 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2579 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2580 struct netlink_ext_ack *extack)
2582 struct net *net = sock_net(skb->sk);
2583 struct nlattr *attrs[XFRMA_MAX+1];
2584 const struct xfrm_link *link;
2587 #ifdef CONFIG_COMPAT
2588 if (in_compat_syscall())
2592 type = nlh->nlmsg_type;
2593 if (type > XFRM_MSG_MAX)
2596 type -= XFRM_MSG_BASE;
2597 link = &xfrm_dispatch[type];
2599 /* All operations require privileges, even GET */
2600 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2603 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2604 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2605 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2606 if (link->dump == NULL)
2610 struct netlink_dump_control c = {
2611 .start = link->start,
2615 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2619 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2620 link->nla_max ? : XFRMA_MAX,
2621 link->nla_pol ? : xfrma_policy, extack);
2625 if (link->doit == NULL)
2628 return link->doit(skb, nlh, attrs);
2631 static void xfrm_netlink_rcv(struct sk_buff *skb)
2633 struct net *net = sock_net(skb->sk);
2635 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2636 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2637 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2640 static inline size_t xfrm_expire_msgsize(void)
2642 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2643 + nla_total_size(sizeof(struct xfrm_mark));
2646 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2648 struct xfrm_user_expire *ue;
2649 struct nlmsghdr *nlh;
2652 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2656 ue = nlmsg_data(nlh);
2657 copy_to_user_state(x, &ue->state);
2658 ue->hard = (c->data.hard != 0) ? 1 : 0;
2659 /* clear the padding bytes */
2660 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2662 err = xfrm_mark_put(skb, &x->mark);
2666 nlmsg_end(skb, nlh);
2670 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2672 struct net *net = xs_net(x);
2673 struct sk_buff *skb;
2675 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2679 if (build_expire(skb, x, c) < 0) {
2684 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2687 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2689 struct net *net = xs_net(x);
2690 struct sk_buff *skb;
2692 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2696 if (build_aevent(skb, x, c) < 0)
2699 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2702 static int xfrm_notify_sa_flush(const struct km_event *c)
2704 struct net *net = c->net;
2705 struct xfrm_usersa_flush *p;
2706 struct nlmsghdr *nlh;
2707 struct sk_buff *skb;
2708 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2710 skb = nlmsg_new(len, GFP_ATOMIC);
2714 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2720 p = nlmsg_data(nlh);
2721 p->proto = c->data.proto;
2723 nlmsg_end(skb, nlh);
2725 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2728 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2732 l += nla_total_size(aead_len(x->aead));
2734 l += nla_total_size(sizeof(struct xfrm_algo) +
2735 (x->aalg->alg_key_len + 7) / 8);
2736 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2739 l += nla_total_size(xfrm_alg_len(x->ealg));
2741 l += nla_total_size(sizeof(*x->calg));
2743 l += nla_total_size(sizeof(*x->encap));
2745 l += nla_total_size(sizeof(x->tfcpad));
2747 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2749 l += nla_total_size(sizeof(struct xfrm_replay_state));
2751 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2752 x->security->ctx_len);
2754 l += nla_total_size(sizeof(*x->coaddr));
2755 if (x->props.extra_flags)
2756 l += nla_total_size(sizeof(x->props.extra_flags));
2758 l += nla_total_size(sizeof(x->xso));
2759 if (x->props.output_mark)
2760 l += nla_total_size(sizeof(x->props.output_mark));
2762 /* Must count x->lastused as it may become non-zero behind our back. */
2763 l += nla_total_size_64bit(sizeof(u64));
2768 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2770 struct net *net = xs_net(x);
2771 struct xfrm_usersa_info *p;
2772 struct xfrm_usersa_id *id;
2773 struct nlmsghdr *nlh;
2774 struct sk_buff *skb;
2775 int len = xfrm_sa_len(x);
2778 headlen = sizeof(*p);
2779 if (c->event == XFRM_MSG_DELSA) {
2780 len += nla_total_size(headlen);
2781 headlen = sizeof(*id);
2782 len += nla_total_size(sizeof(struct xfrm_mark));
2784 len += NLMSG_ALIGN(headlen);
2786 skb = nlmsg_new(len, GFP_ATOMIC);
2790 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2795 p = nlmsg_data(nlh);
2796 if (c->event == XFRM_MSG_DELSA) {
2797 struct nlattr *attr;
2799 id = nlmsg_data(nlh);
2800 memset(id, 0, sizeof(*id));
2801 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2802 id->spi = x->id.spi;
2803 id->family = x->props.family;
2804 id->proto = x->id.proto;
2806 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2813 err = copy_to_user_state_extra(x, p, skb);
2817 nlmsg_end(skb, nlh);
2819 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2826 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2830 case XFRM_MSG_EXPIRE:
2831 return xfrm_exp_state_notify(x, c);
2832 case XFRM_MSG_NEWAE:
2833 return xfrm_aevent_state_notify(x, c);
2834 case XFRM_MSG_DELSA:
2835 case XFRM_MSG_UPDSA:
2836 case XFRM_MSG_NEWSA:
2837 return xfrm_notify_sa(x, c);
2838 case XFRM_MSG_FLUSHSA:
2839 return xfrm_notify_sa_flush(c);
2841 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2850 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2851 struct xfrm_policy *xp)
2853 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2854 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2855 + nla_total_size(sizeof(struct xfrm_mark))
2856 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2857 + userpolicy_type_attrsize();
2860 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2861 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2863 __u32 seq = xfrm_get_acqseq();
2864 struct xfrm_user_acquire *ua;
2865 struct nlmsghdr *nlh;
2868 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2872 ua = nlmsg_data(nlh);
2873 memcpy(&ua->id, &x->id, sizeof(ua->id));
2874 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2875 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2876 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2877 ua->aalgos = xt->aalgos;
2878 ua->ealgos = xt->ealgos;
2879 ua->calgos = xt->calgos;
2880 ua->seq = x->km.seq = seq;
2882 err = copy_to_user_tmpl(xp, skb);
2884 err = copy_to_user_state_sec_ctx(x, skb);
2886 err = copy_to_user_policy_type(xp->type, skb);
2888 err = xfrm_mark_put(skb, &xp->mark);
2890 nlmsg_cancel(skb, nlh);
2894 nlmsg_end(skb, nlh);
2898 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2899 struct xfrm_policy *xp)
2901 struct net *net = xs_net(x);
2902 struct sk_buff *skb;
2904 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2908 if (build_acquire(skb, x, xt, xp) < 0)
2911 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2914 /* User gives us xfrm_user_policy_info followed by an array of 0
2915 * or more templates.
2917 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2918 u8 *data, int len, int *dir)
2920 struct net *net = sock_net(sk);
2921 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2922 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2923 struct xfrm_policy *xp;
2926 switch (sk->sk_family) {
2928 if (opt != IP_XFRM_POLICY) {
2933 #if IS_ENABLED(CONFIG_IPV6)
2935 if (opt != IPV6_XFRM_POLICY) {
2948 if (len < sizeof(*p) ||
2949 verify_newpolicy_info(p))
2952 nr = ((len - sizeof(*p)) / sizeof(*ut));
2953 if (validate_tmpl(nr, ut, p->sel.family))
2956 if (p->dir > XFRM_POLICY_OUT)
2959 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2965 copy_from_user_policy(xp, p);
2966 xp->type = XFRM_POLICY_TYPE_MAIN;
2967 copy_templates(xp, ut, nr);
2974 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2976 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2977 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2978 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2979 + nla_total_size(sizeof(struct xfrm_mark))
2980 + userpolicy_type_attrsize();
2983 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2984 int dir, const struct km_event *c)
2986 struct xfrm_user_polexpire *upe;
2987 int hard = c->data.hard;
2988 struct nlmsghdr *nlh;
2991 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2995 upe = nlmsg_data(nlh);
2996 copy_to_user_policy(xp, &upe->pol, dir);
2997 err = copy_to_user_tmpl(xp, skb);
2999 err = copy_to_user_sec_ctx(xp, skb);
3001 err = copy_to_user_policy_type(xp->type, skb);
3003 err = xfrm_mark_put(skb, &xp->mark);
3005 nlmsg_cancel(skb, nlh);
3010 nlmsg_end(skb, nlh);
3014 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3016 struct net *net = xp_net(xp);
3017 struct sk_buff *skb;
3019 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3023 if (build_polexpire(skb, xp, dir, c) < 0)
3026 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3029 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3031 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3032 struct net *net = xp_net(xp);
3033 struct xfrm_userpolicy_info *p;
3034 struct xfrm_userpolicy_id *id;
3035 struct nlmsghdr *nlh;
3036 struct sk_buff *skb;
3039 headlen = sizeof(*p);
3040 if (c->event == XFRM_MSG_DELPOLICY) {
3041 len += nla_total_size(headlen);
3042 headlen = sizeof(*id);
3044 len += userpolicy_type_attrsize();
3045 len += nla_total_size(sizeof(struct xfrm_mark));
3046 len += NLMSG_ALIGN(headlen);
3048 skb = nlmsg_new(len, GFP_ATOMIC);
3052 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3057 p = nlmsg_data(nlh);
3058 if (c->event == XFRM_MSG_DELPOLICY) {
3059 struct nlattr *attr;
3061 id = nlmsg_data(nlh);
3062 memset(id, 0, sizeof(*id));
3065 id->index = xp->index;
3067 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3069 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3077 copy_to_user_policy(xp, p, dir);
3078 err = copy_to_user_tmpl(xp, skb);
3080 err = copy_to_user_policy_type(xp->type, skb);
3082 err = xfrm_mark_put(skb, &xp->mark);
3086 nlmsg_end(skb, nlh);
3088 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3095 static int xfrm_notify_policy_flush(const struct km_event *c)
3097 struct net *net = c->net;
3098 struct nlmsghdr *nlh;
3099 struct sk_buff *skb;
3102 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3106 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3110 err = copy_to_user_policy_type(c->data.type, skb);
3114 nlmsg_end(skb, nlh);
3116 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3123 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3127 case XFRM_MSG_NEWPOLICY:
3128 case XFRM_MSG_UPDPOLICY:
3129 case XFRM_MSG_DELPOLICY:
3130 return xfrm_notify_policy(xp, dir, c);
3131 case XFRM_MSG_FLUSHPOLICY:
3132 return xfrm_notify_policy_flush(c);
3133 case XFRM_MSG_POLEXPIRE:
3134 return xfrm_exp_policy_notify(xp, dir, c);
3136 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3144 static inline size_t xfrm_report_msgsize(void)
3146 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3149 static int build_report(struct sk_buff *skb, u8 proto,
3150 struct xfrm_selector *sel, xfrm_address_t *addr)
3152 struct xfrm_user_report *ur;
3153 struct nlmsghdr *nlh;
3155 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3159 ur = nlmsg_data(nlh);
3161 memcpy(&ur->sel, sel, sizeof(ur->sel));
3164 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3166 nlmsg_cancel(skb, nlh);
3170 nlmsg_end(skb, nlh);
3174 static int xfrm_send_report(struct net *net, u8 proto,
3175 struct xfrm_selector *sel, xfrm_address_t *addr)
3177 struct sk_buff *skb;
3179 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3183 if (build_report(skb, proto, sel, addr) < 0)
3186 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3189 static inline size_t xfrm_mapping_msgsize(void)
3191 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3194 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3195 xfrm_address_t *new_saddr, __be16 new_sport)
3197 struct xfrm_user_mapping *um;
3198 struct nlmsghdr *nlh;
3200 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3204 um = nlmsg_data(nlh);
3206 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3207 um->id.spi = x->id.spi;
3208 um->id.family = x->props.family;
3209 um->id.proto = x->id.proto;
3210 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3211 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3212 um->new_sport = new_sport;
3213 um->old_sport = x->encap->encap_sport;
3214 um->reqid = x->props.reqid;
3216 nlmsg_end(skb, nlh);
3220 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3223 struct net *net = xs_net(x);
3224 struct sk_buff *skb;
3226 if (x->id.proto != IPPROTO_ESP)
3232 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3236 if (build_mapping(skb, x, ipaddr, sport) < 0)
3239 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3242 static bool xfrm_is_alive(const struct km_event *c)
3244 return (bool)xfrm_acquire_is_on(c->net);
3247 static struct xfrm_mgr netlink_mgr = {
3248 .notify = xfrm_send_state_notify,
3249 .acquire = xfrm_send_acquire,
3250 .compile_policy = xfrm_compile_policy,
3251 .notify_policy = xfrm_send_policy_notify,
3252 .report = xfrm_send_report,
3253 .migrate = xfrm_send_migrate,
3254 .new_mapping = xfrm_send_mapping,
3255 .is_alive = xfrm_is_alive,
3258 static int __net_init xfrm_user_net_init(struct net *net)
3261 struct netlink_kernel_cfg cfg = {
3262 .groups = XFRMNLGRP_MAX,
3263 .input = xfrm_netlink_rcv,
3266 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3269 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3270 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3274 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3277 list_for_each_entry(net, net_exit_list, exit_list)
3278 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3280 list_for_each_entry(net, net_exit_list, exit_list)
3281 netlink_kernel_release(net->xfrm.nlsk_stash);
3284 static struct pernet_operations xfrm_user_net_ops = {
3285 .init = xfrm_user_net_init,
3286 .exit_batch = xfrm_user_net_exit,
3289 static int __init xfrm_user_init(void)
3293 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3295 rv = register_pernet_subsys(&xfrm_user_net_ops);
3298 rv = xfrm_register_km(&netlink_mgr);
3300 unregister_pernet_subsys(&xfrm_user_net_ops);
3304 static void __exit xfrm_user_exit(void)
3306 xfrm_unregister_km(&netlink_mgr);
3307 unregister_pernet_subsys(&xfrm_user_net_ops);
3310 module_init(xfrm_user_init);
3311 module_exit(xfrm_user_exit);
3312 MODULE_LICENSE("GPL");
3313 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);