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 <asm/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[CRYPTO_MAX_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[CRYPTO_MAX_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[CRYPTO_MAX_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 err = __xfrm_init_state(x, false);
612 if (attrs[XFRMA_SEC_CTX]) {
613 err = security_xfrm_state_alloc(x,
614 nla_data(attrs[XFRMA_SEC_CTX]));
619 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
620 attrs[XFRMA_REPLAY_ESN_VAL])))
624 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
625 /* sysctl_xfrm_aevent_etime is in 100ms units */
626 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
628 if ((err = xfrm_init_replay(x)))
631 /* override default values from above */
632 xfrm_update_ae_params(x, attrs, 0);
637 x->km.state = XFRM_STATE_DEAD;
644 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
645 struct nlattr **attrs)
647 struct net *net = sock_net(skb->sk);
648 struct xfrm_usersa_info *p = nlmsg_data(nlh);
649 struct xfrm_state *x;
653 err = verify_newsa_info(p, attrs);
657 x = xfrm_state_construct(net, p, attrs, &err);
662 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
663 err = xfrm_state_add(x);
665 err = xfrm_state_update(x);
667 xfrm_audit_state_add(x, err ? 0 : 1, true);
670 x->km.state = XFRM_STATE_DEAD;
675 c.seq = nlh->nlmsg_seq;
676 c.portid = nlh->nlmsg_pid;
677 c.event = nlh->nlmsg_type;
679 km_state_notify(x, &c);
685 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
686 struct xfrm_usersa_id *p,
687 struct nlattr **attrs,
690 struct xfrm_state *x = NULL;
693 u32 mark = xfrm_mark_get(attrs, &m);
695 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
697 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
699 xfrm_address_t *saddr = NULL;
701 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
708 x = xfrm_state_lookup_byaddr(net, mark,
710 p->proto, p->family);
719 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
720 struct nlattr **attrs)
722 struct net *net = sock_net(skb->sk);
723 struct xfrm_state *x;
726 struct xfrm_usersa_id *p = nlmsg_data(nlh);
728 x = xfrm_user_state_lookup(net, p, attrs, &err);
732 if ((err = security_xfrm_state_delete(x)) != 0)
735 if (xfrm_state_kern(x)) {
740 err = xfrm_state_delete(x);
745 c.seq = nlh->nlmsg_seq;
746 c.portid = nlh->nlmsg_pid;
747 c.event = nlh->nlmsg_type;
748 km_state_notify(x, &c);
751 xfrm_audit_state_delete(x, err ? 0 : 1, true);
756 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
758 memset(p, 0, sizeof(*p));
759 memcpy(&p->id, &x->id, sizeof(p->id));
760 memcpy(&p->sel, &x->sel, sizeof(p->sel));
761 memcpy(&p->lft, &x->lft, sizeof(p->lft));
762 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
763 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
764 put_unaligned(x->stats.replay, &p->stats.replay);
765 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
766 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
767 p->mode = x->props.mode;
768 p->replay_window = x->props.replay_window;
769 p->reqid = x->props.reqid;
770 p->family = x->props.family;
771 p->flags = x->props.flags;
775 struct xfrm_dump_info {
776 struct sk_buff *in_skb;
777 struct sk_buff *out_skb;
782 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
784 struct xfrm_user_sec_ctx *uctx;
786 int ctx_size = sizeof(*uctx) + s->ctx_len;
788 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
792 uctx = nla_data(attr);
793 uctx->exttype = XFRMA_SEC_CTX;
794 uctx->len = ctx_size;
795 uctx->ctx_doi = s->ctx_doi;
796 uctx->ctx_alg = s->ctx_alg;
797 uctx->ctx_len = s->ctx_len;
798 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
803 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
805 struct xfrm_algo *algo;
808 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
809 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
813 algo = nla_data(nla);
814 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
815 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
816 algo->alg_key_len = auth->alg_key_len;
821 /* Don't change this without updating xfrm_sa_len! */
822 static int copy_to_user_state_extra(struct xfrm_state *x,
823 struct xfrm_usersa_info *p,
828 copy_to_user_state(x, p);
830 if (x->props.extra_flags) {
831 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
832 x->props.extra_flags);
838 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
843 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
848 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
853 ret = copy_to_user_auth(x->aalg, skb);
855 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
856 xfrm_alg_auth_len(x->aalg), x->aalg);
861 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
866 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
871 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
876 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
880 ret = xfrm_mark_put(skb, &x->mark);
884 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
885 xfrm_replay_state_esn_len(x->replay_esn),
888 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
893 ret = copy_sec_ctx(x->security, skb);
898 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
900 struct xfrm_dump_info *sp = ptr;
901 struct sk_buff *in_skb = sp->in_skb;
902 struct sk_buff *skb = sp->out_skb;
903 struct xfrm_usersa_info *p;
904 struct nlmsghdr *nlh;
907 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
908 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
914 err = copy_to_user_state_extra(x, p, skb);
916 nlmsg_cancel(skb, nlh);
923 static int xfrm_dump_sa_done(struct netlink_callback *cb)
925 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
926 struct sock *sk = cb->skb->sk;
927 struct net *net = sock_net(sk);
930 xfrm_state_walk_done(walk, net);
934 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
935 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
937 struct net *net = sock_net(skb->sk);
938 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
939 struct xfrm_dump_info info;
941 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
942 sizeof(cb->args) - sizeof(cb->args[0]));
944 info.in_skb = cb->skb;
946 info.nlmsg_seq = cb->nlh->nlmsg_seq;
947 info.nlmsg_flags = NLM_F_MULTI;
950 struct nlattr *attrs[XFRMA_MAX+1];
951 struct xfrm_address_filter *filter = NULL;
955 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
960 if (attrs[XFRMA_ADDRESS_FILTER]) {
961 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
962 sizeof(*filter), GFP_KERNEL);
967 if (attrs[XFRMA_PROTO])
968 proto = nla_get_u8(attrs[XFRMA_PROTO]);
970 xfrm_state_walk_init(walk, proto, filter);
974 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
979 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
980 struct xfrm_state *x, u32 seq)
982 struct xfrm_dump_info info;
986 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
988 return ERR_PTR(-ENOMEM);
990 info.in_skb = in_skb;
992 info.nlmsg_seq = seq;
993 info.nlmsg_flags = 0;
995 err = dump_one_state(x, 0, &info);
1004 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1005 * Must be called with RCU read lock.
1007 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1008 u32 pid, unsigned int group)
1010 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1017 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1020 static inline size_t xfrm_spdinfo_msgsize(void)
1022 return NLMSG_ALIGN(4)
1023 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1024 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1025 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1026 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1029 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1030 u32 portid, u32 seq, u32 flags)
1032 struct xfrmk_spdinfo si;
1033 struct xfrmu_spdinfo spc;
1034 struct xfrmu_spdhinfo sph;
1035 struct xfrmu_spdhthresh spt4, spt6;
1036 struct nlmsghdr *nlh;
1041 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1042 if (nlh == NULL) /* shouldn't really happen ... */
1045 f = nlmsg_data(nlh);
1047 xfrm_spd_getinfo(net, &si);
1048 spc.incnt = si.incnt;
1049 spc.outcnt = si.outcnt;
1050 spc.fwdcnt = si.fwdcnt;
1051 spc.inscnt = si.inscnt;
1052 spc.outscnt = si.outscnt;
1053 spc.fwdscnt = si.fwdscnt;
1054 sph.spdhcnt = si.spdhcnt;
1055 sph.spdhmcnt = si.spdhmcnt;
1058 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1060 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1061 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1062 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1063 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1064 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1066 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1068 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1070 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1072 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1074 nlmsg_cancel(skb, nlh);
1078 nlmsg_end(skb, nlh);
1082 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1083 struct nlattr **attrs)
1085 struct net *net = sock_net(skb->sk);
1086 struct xfrmu_spdhthresh *thresh4 = NULL;
1087 struct xfrmu_spdhthresh *thresh6 = NULL;
1089 /* selector prefixlen thresholds to hash policies */
1090 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1091 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1093 if (nla_len(rta) < sizeof(*thresh4))
1095 thresh4 = nla_data(rta);
1096 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1099 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1100 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1102 if (nla_len(rta) < sizeof(*thresh6))
1104 thresh6 = nla_data(rta);
1105 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1109 if (thresh4 || thresh6) {
1110 write_seqlock(&net->xfrm.policy_hthresh.lock);
1112 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1113 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1116 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1117 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1119 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1121 xfrm_policy_hash_rebuild(net);
1127 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1128 struct nlattr **attrs)
1130 struct net *net = sock_net(skb->sk);
1131 struct sk_buff *r_skb;
1132 u32 *flags = nlmsg_data(nlh);
1133 u32 sportid = NETLINK_CB(skb).portid;
1134 u32 seq = nlh->nlmsg_seq;
1136 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1140 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1143 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1146 static inline size_t xfrm_sadinfo_msgsize(void)
1148 return NLMSG_ALIGN(4)
1149 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1150 + nla_total_size(4); /* XFRMA_SAD_CNT */
1153 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1154 u32 portid, u32 seq, u32 flags)
1156 struct xfrmk_sadinfo si;
1157 struct xfrmu_sadhinfo sh;
1158 struct nlmsghdr *nlh;
1162 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1163 if (nlh == NULL) /* shouldn't really happen ... */
1166 f = nlmsg_data(nlh);
1168 xfrm_sad_getinfo(net, &si);
1170 sh.sadhmcnt = si.sadhmcnt;
1171 sh.sadhcnt = si.sadhcnt;
1173 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1175 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1177 nlmsg_cancel(skb, nlh);
1181 nlmsg_end(skb, nlh);
1185 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1186 struct nlattr **attrs)
1188 struct net *net = sock_net(skb->sk);
1189 struct sk_buff *r_skb;
1190 u32 *flags = nlmsg_data(nlh);
1191 u32 sportid = NETLINK_CB(skb).portid;
1192 u32 seq = nlh->nlmsg_seq;
1194 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1198 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1201 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1204 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1205 struct nlattr **attrs)
1207 struct net *net = sock_net(skb->sk);
1208 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1209 struct xfrm_state *x;
1210 struct sk_buff *resp_skb;
1213 x = xfrm_user_state_lookup(net, p, attrs, &err);
1217 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1218 if (IS_ERR(resp_skb)) {
1219 err = PTR_ERR(resp_skb);
1221 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1228 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1229 struct nlattr **attrs)
1231 struct net *net = sock_net(skb->sk);
1232 struct xfrm_state *x;
1233 struct xfrm_userspi_info *p;
1234 struct sk_buff *resp_skb;
1235 xfrm_address_t *daddr;
1241 p = nlmsg_data(nlh);
1242 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1246 family = p->info.family;
1247 daddr = &p->info.id.daddr;
1251 mark = xfrm_mark_get(attrs, &m);
1253 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1254 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1261 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1262 p->info.id.proto, daddr,
1269 err = xfrm_alloc_spi(x, p->min, p->max);
1273 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1274 if (IS_ERR(resp_skb)) {
1275 err = PTR_ERR(resp_skb);
1279 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1287 static int verify_policy_dir(u8 dir)
1290 case XFRM_POLICY_IN:
1291 case XFRM_POLICY_OUT:
1292 case XFRM_POLICY_FWD:
1302 static int verify_policy_type(u8 type)
1305 case XFRM_POLICY_TYPE_MAIN:
1306 #ifdef CONFIG_XFRM_SUB_POLICY
1307 case XFRM_POLICY_TYPE_SUB:
1318 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1323 case XFRM_SHARE_ANY:
1324 case XFRM_SHARE_SESSION:
1325 case XFRM_SHARE_USER:
1326 case XFRM_SHARE_UNIQUE:
1333 switch (p->action) {
1334 case XFRM_POLICY_ALLOW:
1335 case XFRM_POLICY_BLOCK:
1342 switch (p->sel.family) {
1344 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1350 #if IS_ENABLED(CONFIG_IPV6)
1351 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1356 return -EAFNOSUPPORT;
1363 ret = verify_policy_dir(p->dir);
1366 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1372 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1374 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1375 struct xfrm_user_sec_ctx *uctx;
1380 uctx = nla_data(rt);
1381 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1384 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1390 for (i = 0; i < nr; i++, ut++) {
1391 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1393 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1394 memcpy(&t->saddr, &ut->saddr,
1395 sizeof(xfrm_address_t));
1396 t->reqid = ut->reqid;
1398 t->share = ut->share;
1399 t->optional = ut->optional;
1400 t->aalgos = ut->aalgos;
1401 t->ealgos = ut->ealgos;
1402 t->calgos = ut->calgos;
1403 /* If all masks are ~0, then we allow all algorithms. */
1404 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1405 t->encap_family = ut->family;
1409 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1414 if (nr > XFRM_MAX_DEPTH)
1417 prev_family = family;
1419 for (i = 0; i < nr; i++) {
1420 /* We never validated the ut->family value, so many
1421 * applications simply leave it at zero. The check was
1422 * never made and ut->family was ignored because all
1423 * templates could be assumed to have the same family as
1424 * the policy itself. Now that we will have ipv4-in-ipv6
1425 * and ipv6-in-ipv4 tunnels, this is no longer true.
1428 ut[i].family = family;
1430 switch (ut[i].mode) {
1431 case XFRM_MODE_TUNNEL:
1432 case XFRM_MODE_BEET:
1435 if (ut[i].family != prev_family)
1439 if (ut[i].mode >= XFRM_MODE_MAX)
1442 prev_family = ut[i].family;
1444 switch (ut[i].family) {
1447 #if IS_ENABLED(CONFIG_IPV6)
1455 if (!xfrm_id_proto_valid(ut[i].id.proto))
1462 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1464 struct nlattr *rt = attrs[XFRMA_TMPL];
1469 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1470 int nr = nla_len(rt) / sizeof(*utmpl);
1473 err = validate_tmpl(nr, utmpl, pol->family);
1477 copy_templates(pol, utmpl, nr);
1482 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1484 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1485 struct xfrm_userpolicy_type *upt;
1486 u8 type = XFRM_POLICY_TYPE_MAIN;
1494 err = verify_policy_type(type);
1502 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1504 xp->priority = p->priority;
1505 xp->index = p->index;
1506 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1507 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1508 xp->action = p->action;
1509 xp->flags = p->flags;
1510 xp->family = p->sel.family;
1511 /* XXX xp->share = p->share; */
1514 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1516 memset(p, 0, sizeof(*p));
1517 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1518 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1519 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1520 p->priority = xp->priority;
1521 p->index = xp->index;
1522 p->sel.family = xp->family;
1524 p->action = xp->action;
1525 p->flags = xp->flags;
1526 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1529 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1531 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1539 copy_from_user_policy(xp, p);
1541 err = copy_from_user_policy_type(&xp->type, attrs);
1545 if (!(err = copy_from_user_tmpl(xp, attrs)))
1546 err = copy_from_user_sec_ctx(xp, attrs);
1550 xfrm_mark_get(attrs, &xp->mark);
1556 xfrm_policy_destroy(xp);
1560 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1561 struct nlattr **attrs)
1563 struct net *net = sock_net(skb->sk);
1564 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1565 struct xfrm_policy *xp;
1570 err = verify_newpolicy_info(p);
1573 err = verify_sec_ctx_len(attrs);
1577 xp = xfrm_policy_construct(net, p, attrs, &err);
1581 /* shouldn't excl be based on nlh flags??
1582 * Aha! this is anti-netlink really i.e more pfkey derived
1583 * in netlink excl is a flag and you wouldnt need
1584 * a type XFRM_MSG_UPDPOLICY - JHS */
1585 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1586 err = xfrm_policy_insert(p->dir, xp, excl);
1587 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1590 security_xfrm_policy_free(xp->security);
1595 c.event = nlh->nlmsg_type;
1596 c.seq = nlh->nlmsg_seq;
1597 c.portid = nlh->nlmsg_pid;
1598 km_policy_notify(xp, p->dir, &c);
1605 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1607 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1610 if (xp->xfrm_nr == 0)
1613 for (i = 0; i < xp->xfrm_nr; i++) {
1614 struct xfrm_user_tmpl *up = &vec[i];
1615 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1617 memset(up, 0, sizeof(*up));
1618 memcpy(&up->id, &kp->id, sizeof(up->id));
1619 up->family = kp->encap_family;
1620 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1621 up->reqid = kp->reqid;
1622 up->mode = kp->mode;
1623 up->share = kp->share;
1624 up->optional = kp->optional;
1625 up->aalgos = kp->aalgos;
1626 up->ealgos = kp->ealgos;
1627 up->calgos = kp->calgos;
1630 return nla_put(skb, XFRMA_TMPL,
1631 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1634 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1637 return copy_sec_ctx(x->security, skb);
1642 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1645 return copy_sec_ctx(xp->security, skb);
1648 static inline size_t userpolicy_type_attrsize(void)
1650 #ifdef CONFIG_XFRM_SUB_POLICY
1651 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1657 #ifdef CONFIG_XFRM_SUB_POLICY
1658 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1660 struct xfrm_userpolicy_type upt;
1662 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1663 memset(&upt, 0, sizeof(upt));
1666 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1670 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1676 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1678 struct xfrm_dump_info *sp = ptr;
1679 struct xfrm_userpolicy_info *p;
1680 struct sk_buff *in_skb = sp->in_skb;
1681 struct sk_buff *skb = sp->out_skb;
1682 struct nlmsghdr *nlh;
1685 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1686 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1690 p = nlmsg_data(nlh);
1691 copy_to_user_policy(xp, p, dir);
1692 err = copy_to_user_tmpl(xp, skb);
1694 err = copy_to_user_sec_ctx(xp, skb);
1696 err = copy_to_user_policy_type(xp->type, skb);
1698 err = xfrm_mark_put(skb, &xp->mark);
1700 nlmsg_cancel(skb, nlh);
1703 nlmsg_end(skb, nlh);
1707 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1709 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1710 struct net *net = sock_net(cb->skb->sk);
1712 xfrm_policy_walk_done(walk, net);
1716 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1718 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1720 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1722 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1726 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1728 struct net *net = sock_net(skb->sk);
1729 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1730 struct xfrm_dump_info info;
1732 info.in_skb = cb->skb;
1734 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1735 info.nlmsg_flags = NLM_F_MULTI;
1737 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1742 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1743 struct xfrm_policy *xp,
1746 struct xfrm_dump_info info;
1747 struct sk_buff *skb;
1750 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1752 return ERR_PTR(-ENOMEM);
1754 info.in_skb = in_skb;
1756 info.nlmsg_seq = seq;
1757 info.nlmsg_flags = 0;
1759 err = dump_one_policy(xp, dir, 0, &info);
1762 return ERR_PTR(err);
1768 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1769 struct nlattr **attrs)
1771 struct net *net = sock_net(skb->sk);
1772 struct xfrm_policy *xp;
1773 struct xfrm_userpolicy_id *p;
1774 u8 type = XFRM_POLICY_TYPE_MAIN;
1779 u32 mark = xfrm_mark_get(attrs, &m);
1781 p = nlmsg_data(nlh);
1782 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1784 err = copy_from_user_policy_type(&type, attrs);
1788 err = verify_policy_dir(p->dir);
1793 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1795 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1796 struct xfrm_sec_ctx *ctx;
1798 err = verify_sec_ctx_len(attrs);
1804 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1806 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1810 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1812 security_xfrm_policy_free(ctx);
1818 struct sk_buff *resp_skb;
1820 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1821 if (IS_ERR(resp_skb)) {
1822 err = PTR_ERR(resp_skb);
1824 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1825 NETLINK_CB(skb).portid);
1828 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1833 c.data.byid = p->index;
1834 c.event = nlh->nlmsg_type;
1835 c.seq = nlh->nlmsg_seq;
1836 c.portid = nlh->nlmsg_pid;
1837 km_policy_notify(xp, p->dir, &c);
1842 if (delete && err == 0)
1843 xfrm_garbage_collect(net);
1847 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1848 struct nlattr **attrs)
1850 struct net *net = sock_net(skb->sk);
1852 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1855 err = xfrm_state_flush(net, p->proto, true);
1857 if (err == -ESRCH) /* empty table */
1861 c.data.proto = p->proto;
1862 c.event = nlh->nlmsg_type;
1863 c.seq = nlh->nlmsg_seq;
1864 c.portid = nlh->nlmsg_pid;
1866 km_state_notify(NULL, &c);
1871 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1873 size_t replay_size = x->replay_esn ?
1874 xfrm_replay_state_esn_len(x->replay_esn) :
1875 sizeof(struct xfrm_replay_state);
1877 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1878 + nla_total_size(replay_size)
1879 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1880 + nla_total_size(sizeof(struct xfrm_mark))
1881 + nla_total_size(4) /* XFRM_AE_RTHR */
1882 + nla_total_size(4); /* XFRM_AE_ETHR */
1885 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1887 struct xfrm_aevent_id *id;
1888 struct nlmsghdr *nlh;
1891 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1895 id = nlmsg_data(nlh);
1896 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1897 id->sa_id.spi = x->id.spi;
1898 id->sa_id.family = x->props.family;
1899 id->sa_id.proto = x->id.proto;
1900 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1901 id->reqid = x->props.reqid;
1902 id->flags = c->data.aevent;
1904 if (x->replay_esn) {
1905 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1906 xfrm_replay_state_esn_len(x->replay_esn),
1909 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1914 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1918 if (id->flags & XFRM_AE_RTHR) {
1919 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1923 if (id->flags & XFRM_AE_ETHR) {
1924 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1925 x->replay_maxage * 10 / HZ);
1929 err = xfrm_mark_put(skb, &x->mark);
1933 nlmsg_end(skb, nlh);
1937 nlmsg_cancel(skb, nlh);
1941 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1942 struct nlattr **attrs)
1944 struct net *net = sock_net(skb->sk);
1945 struct xfrm_state *x;
1946 struct sk_buff *r_skb;
1951 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1952 struct xfrm_usersa_id *id = &p->sa_id;
1954 mark = xfrm_mark_get(attrs, &m);
1956 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1960 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1961 if (r_skb == NULL) {
1967 * XXX: is this lock really needed - none of the other
1968 * gets lock (the concern is things getting updated
1969 * while we are still reading) - jhs
1971 spin_lock_bh(&x->lock);
1972 c.data.aevent = p->flags;
1973 c.seq = nlh->nlmsg_seq;
1974 c.portid = nlh->nlmsg_pid;
1976 if (build_aevent(r_skb, x, &c) < 0)
1978 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1979 spin_unlock_bh(&x->lock);
1984 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1985 struct nlattr **attrs)
1987 struct net *net = sock_net(skb->sk);
1988 struct xfrm_state *x;
1993 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1994 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1995 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1996 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1997 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1998 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2000 if (!lt && !rp && !re && !et && !rt)
2003 /* pedantic mode - thou shalt sayeth replaceth */
2004 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2007 mark = xfrm_mark_get(attrs, &m);
2009 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2013 if (x->km.state != XFRM_STATE_VALID)
2016 err = xfrm_replay_verify_len(x->replay_esn, re);
2020 spin_lock_bh(&x->lock);
2021 xfrm_update_ae_params(x, attrs, 1);
2022 spin_unlock_bh(&x->lock);
2024 c.event = nlh->nlmsg_type;
2025 c.seq = nlh->nlmsg_seq;
2026 c.portid = nlh->nlmsg_pid;
2027 c.data.aevent = XFRM_AE_CU;
2028 km_state_notify(x, &c);
2035 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2036 struct nlattr **attrs)
2038 struct net *net = sock_net(skb->sk);
2040 u8 type = XFRM_POLICY_TYPE_MAIN;
2043 err = copy_from_user_policy_type(&type, attrs);
2047 err = xfrm_policy_flush(net, type, true);
2049 if (err == -ESRCH) /* empty table */
2055 c.event = nlh->nlmsg_type;
2056 c.seq = nlh->nlmsg_seq;
2057 c.portid = nlh->nlmsg_pid;
2059 km_policy_notify(NULL, 0, &c);
2063 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2064 struct nlattr **attrs)
2066 struct net *net = sock_net(skb->sk);
2067 struct xfrm_policy *xp;
2068 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2069 struct xfrm_userpolicy_info *p = &up->pol;
2070 u8 type = XFRM_POLICY_TYPE_MAIN;
2073 u32 mark = xfrm_mark_get(attrs, &m);
2075 err = copy_from_user_policy_type(&type, attrs);
2079 err = verify_policy_dir(p->dir);
2084 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
2086 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2087 struct xfrm_sec_ctx *ctx;
2089 err = verify_sec_ctx_len(attrs);
2095 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2097 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2101 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
2102 &p->sel, ctx, 0, &err);
2103 security_xfrm_policy_free(ctx);
2108 if (unlikely(xp->walk.dead))
2113 xfrm_policy_delete(xp, p->dir);
2114 xfrm_audit_policy_delete(xp, 1, true);
2116 // reset the timers here?
2117 WARN(1, "Don't know what to do with soft policy expire\n");
2119 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2126 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2127 struct nlattr **attrs)
2129 struct net *net = sock_net(skb->sk);
2130 struct xfrm_state *x;
2132 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2133 struct xfrm_usersa_info *p = &ue->state;
2135 u32 mark = xfrm_mark_get(attrs, &m);
2137 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2143 spin_lock_bh(&x->lock);
2145 if (x->km.state != XFRM_STATE_VALID)
2147 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2150 __xfrm_state_delete(x);
2151 xfrm_audit_state_delete(x, 1, true);
2155 spin_unlock_bh(&x->lock);
2160 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2161 struct nlattr **attrs)
2163 struct net *net = sock_net(skb->sk);
2164 struct xfrm_policy *xp;
2165 struct xfrm_user_tmpl *ut;
2167 struct nlattr *rt = attrs[XFRMA_TMPL];
2168 struct xfrm_mark mark;
2170 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2171 struct xfrm_state *x = xfrm_state_alloc(net);
2177 xfrm_mark_get(attrs, &mark);
2179 err = verify_newpolicy_info(&ua->policy);
2182 err = verify_sec_ctx_len(attrs);
2187 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2191 memcpy(&x->id, &ua->id, sizeof(ua->id));
2192 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2193 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2194 xp->mark.m = x->mark.m = mark.m;
2195 xp->mark.v = x->mark.v = mark.v;
2197 /* extract the templates and for each call km_key */
2198 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2199 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2200 memcpy(&x->id, &t->id, sizeof(x->id));
2201 x->props.mode = t->mode;
2202 x->props.reqid = t->reqid;
2203 x->props.family = ut->family;
2204 t->aalgos = ua->aalgos;
2205 t->ealgos = ua->ealgos;
2206 t->calgos = ua->calgos;
2207 err = km_query(x, t, xp);
2217 WARN(1, "BAD policy passed\n");
2224 #ifdef CONFIG_XFRM_MIGRATE
2225 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2226 struct xfrm_kmaddress *k,
2227 struct nlattr **attrs, int *num)
2229 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2230 struct xfrm_user_migrate *um;
2234 struct xfrm_user_kmaddress *uk;
2236 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2237 memcpy(&k->local, &uk->local, sizeof(k->local));
2238 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2239 k->family = uk->family;
2240 k->reserved = uk->reserved;
2244 num_migrate = nla_len(rt) / sizeof(*um);
2246 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2249 for (i = 0; i < num_migrate; i++, um++, ma++) {
2250 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2251 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2252 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2253 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2255 ma->proto = um->proto;
2256 ma->mode = um->mode;
2257 ma->reqid = um->reqid;
2259 ma->old_family = um->old_family;
2260 ma->new_family = um->new_family;
2267 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2268 struct nlattr **attrs)
2270 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2271 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2272 struct xfrm_kmaddress km, *kmp;
2276 struct net *net = sock_net(skb->sk);
2278 if (attrs[XFRMA_MIGRATE] == NULL)
2281 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2283 err = copy_from_user_policy_type(&type, attrs);
2287 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2294 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2299 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2300 struct nlattr **attrs)
2302 return -ENOPROTOOPT;
2306 #ifdef CONFIG_XFRM_MIGRATE
2307 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2309 struct xfrm_user_migrate um;
2311 memset(&um, 0, sizeof(um));
2312 um.proto = m->proto;
2314 um.reqid = m->reqid;
2315 um.old_family = m->old_family;
2316 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2317 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2318 um.new_family = m->new_family;
2319 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2320 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2322 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2325 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2327 struct xfrm_user_kmaddress uk;
2329 memset(&uk, 0, sizeof(uk));
2330 uk.family = k->family;
2331 uk.reserved = k->reserved;
2332 memcpy(&uk.local, &k->local, sizeof(uk.local));
2333 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2335 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2338 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2340 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2341 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2342 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2343 + userpolicy_type_attrsize();
2346 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2347 int num_migrate, const struct xfrm_kmaddress *k,
2348 const struct xfrm_selector *sel, u8 dir, u8 type)
2350 const struct xfrm_migrate *mp;
2351 struct xfrm_userpolicy_id *pol_id;
2352 struct nlmsghdr *nlh;
2355 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2359 pol_id = nlmsg_data(nlh);
2360 /* copy data from selector, dir, and type to the pol_id */
2361 memset(pol_id, 0, sizeof(*pol_id));
2362 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2366 err = copy_to_user_kmaddress(k, skb);
2370 err = copy_to_user_policy_type(type, skb);
2373 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2374 err = copy_to_user_migrate(mp, skb);
2379 nlmsg_end(skb, nlh);
2383 nlmsg_cancel(skb, nlh);
2387 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2388 const struct xfrm_migrate *m, int num_migrate,
2389 const struct xfrm_kmaddress *k)
2391 struct net *net = &init_net;
2392 struct sk_buff *skb;
2394 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2399 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2402 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2405 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2406 const struct xfrm_migrate *m, int num_migrate,
2407 const struct xfrm_kmaddress *k)
2409 return -ENOPROTOOPT;
2413 #define XMSGSIZE(type) sizeof(struct type)
2415 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2416 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2417 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2418 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2419 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2420 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2421 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2422 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2423 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2424 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2425 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2426 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2427 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2428 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2429 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2430 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2431 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2432 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2433 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2434 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2435 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2436 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2441 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2442 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2443 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2444 [XFRMA_LASTUSED] = { .type = NLA_U64},
2445 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2446 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2447 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2448 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2449 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2450 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2451 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2452 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2453 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2454 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2455 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2456 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2457 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2458 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2459 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2460 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2461 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2462 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2463 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2464 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2465 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2466 [XFRMA_PROTO] = { .type = NLA_U8 },
2467 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2470 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2471 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2472 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2475 static const struct xfrm_link {
2476 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2477 int (*start)(struct netlink_callback *);
2478 int (*dump)(struct sk_buff *, struct netlink_callback *);
2479 int (*done)(struct netlink_callback *);
2480 const struct nla_policy *nla_pol;
2482 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2483 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2484 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2485 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2486 .dump = xfrm_dump_sa,
2487 .done = xfrm_dump_sa_done },
2488 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2489 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2490 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2491 .start = xfrm_dump_policy_start,
2492 .dump = xfrm_dump_policy,
2493 .done = xfrm_dump_policy_done },
2494 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2495 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2496 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2497 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2498 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2499 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2500 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2501 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2502 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2503 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2504 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2505 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2506 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2507 .nla_pol = xfrma_spd_policy,
2508 .nla_max = XFRMA_SPD_MAX },
2509 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2512 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2514 struct net *net = sock_net(skb->sk);
2515 struct nlattr *attrs[XFRMA_MAX+1];
2516 const struct xfrm_link *link;
2519 #ifdef CONFIG_COMPAT
2520 if (is_compat_task())
2524 type = nlh->nlmsg_type;
2525 if (type > XFRM_MSG_MAX)
2528 type -= XFRM_MSG_BASE;
2529 link = &xfrm_dispatch[type];
2531 /* All operations require privileges, even GET */
2532 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2535 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2536 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2537 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2538 if (link->dump == NULL)
2542 struct netlink_dump_control c = {
2543 .start = link->start,
2547 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2551 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2552 link->nla_max ? : XFRMA_MAX,
2553 link->nla_pol ? : xfrma_policy);
2557 if (link->doit == NULL)
2560 return link->doit(skb, nlh, attrs);
2563 static void xfrm_netlink_rcv(struct sk_buff *skb)
2565 struct net *net = sock_net(skb->sk);
2567 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2568 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2569 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2572 static inline size_t xfrm_expire_msgsize(void)
2574 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2575 + nla_total_size(sizeof(struct xfrm_mark));
2578 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2580 struct xfrm_user_expire *ue;
2581 struct nlmsghdr *nlh;
2584 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2588 ue = nlmsg_data(nlh);
2589 copy_to_user_state(x, &ue->state);
2590 ue->hard = (c->data.hard != 0) ? 1 : 0;
2592 err = xfrm_mark_put(skb, &x->mark);
2596 nlmsg_end(skb, nlh);
2600 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2602 struct net *net = xs_net(x);
2603 struct sk_buff *skb;
2605 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2609 if (build_expire(skb, x, c) < 0) {
2614 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2617 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2619 struct net *net = xs_net(x);
2620 struct sk_buff *skb;
2622 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2626 if (build_aevent(skb, x, c) < 0)
2629 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2632 static int xfrm_notify_sa_flush(const struct km_event *c)
2634 struct net *net = c->net;
2635 struct xfrm_usersa_flush *p;
2636 struct nlmsghdr *nlh;
2637 struct sk_buff *skb;
2638 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2640 skb = nlmsg_new(len, GFP_ATOMIC);
2644 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2650 p = nlmsg_data(nlh);
2651 p->proto = c->data.proto;
2653 nlmsg_end(skb, nlh);
2655 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2658 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2662 l += nla_total_size(aead_len(x->aead));
2664 l += nla_total_size(sizeof(struct xfrm_algo) +
2665 (x->aalg->alg_key_len + 7) / 8);
2666 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2669 l += nla_total_size(xfrm_alg_len(x->ealg));
2671 l += nla_total_size(sizeof(*x->calg));
2673 l += nla_total_size(sizeof(*x->encap));
2675 l += nla_total_size(sizeof(x->tfcpad));
2677 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2679 l += nla_total_size(sizeof(struct xfrm_replay_state));
2681 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2682 x->security->ctx_len);
2684 l += nla_total_size(sizeof(*x->coaddr));
2685 if (x->props.extra_flags)
2686 l += nla_total_size(sizeof(x->props.extra_flags));
2688 /* Must count x->lastused as it may become non-zero behind our back. */
2689 l += nla_total_size(sizeof(u64));
2694 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2696 struct net *net = xs_net(x);
2697 struct xfrm_usersa_info *p;
2698 struct xfrm_usersa_id *id;
2699 struct nlmsghdr *nlh;
2700 struct sk_buff *skb;
2701 int len = xfrm_sa_len(x);
2704 headlen = sizeof(*p);
2705 if (c->event == XFRM_MSG_DELSA) {
2706 len += nla_total_size(headlen);
2707 headlen = sizeof(*id);
2708 len += nla_total_size(sizeof(struct xfrm_mark));
2710 len += NLMSG_ALIGN(headlen);
2712 skb = nlmsg_new(len, GFP_ATOMIC);
2716 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2721 p = nlmsg_data(nlh);
2722 if (c->event == XFRM_MSG_DELSA) {
2723 struct nlattr *attr;
2725 id = nlmsg_data(nlh);
2726 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2727 id->spi = x->id.spi;
2728 id->family = x->props.family;
2729 id->proto = x->id.proto;
2731 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2738 err = copy_to_user_state_extra(x, p, skb);
2742 nlmsg_end(skb, nlh);
2744 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2751 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2755 case XFRM_MSG_EXPIRE:
2756 return xfrm_exp_state_notify(x, c);
2757 case XFRM_MSG_NEWAE:
2758 return xfrm_aevent_state_notify(x, c);
2759 case XFRM_MSG_DELSA:
2760 case XFRM_MSG_UPDSA:
2761 case XFRM_MSG_NEWSA:
2762 return xfrm_notify_sa(x, c);
2763 case XFRM_MSG_FLUSHSA:
2764 return xfrm_notify_sa_flush(c);
2766 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2775 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2776 struct xfrm_policy *xp)
2778 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2779 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2780 + nla_total_size(sizeof(struct xfrm_mark))
2781 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2782 + userpolicy_type_attrsize();
2785 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2786 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2788 __u32 seq = xfrm_get_acqseq();
2789 struct xfrm_user_acquire *ua;
2790 struct nlmsghdr *nlh;
2793 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2797 ua = nlmsg_data(nlh);
2798 memcpy(&ua->id, &x->id, sizeof(ua->id));
2799 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2800 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2801 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2802 ua->aalgos = xt->aalgos;
2803 ua->ealgos = xt->ealgos;
2804 ua->calgos = xt->calgos;
2805 ua->seq = x->km.seq = seq;
2807 err = copy_to_user_tmpl(xp, skb);
2809 err = copy_to_user_state_sec_ctx(x, skb);
2811 err = copy_to_user_policy_type(xp->type, skb);
2813 err = xfrm_mark_put(skb, &xp->mark);
2815 nlmsg_cancel(skb, nlh);
2819 nlmsg_end(skb, nlh);
2823 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2824 struct xfrm_policy *xp)
2826 struct net *net = xs_net(x);
2827 struct sk_buff *skb;
2829 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2833 if (build_acquire(skb, x, xt, xp) < 0)
2836 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2839 /* User gives us xfrm_user_policy_info followed by an array of 0
2840 * or more templates.
2842 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2843 u8 *data, int len, int *dir)
2845 struct net *net = sock_net(sk);
2846 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2847 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2848 struct xfrm_policy *xp;
2851 switch (sk->sk_family) {
2853 if (opt != IP_XFRM_POLICY) {
2858 #if IS_ENABLED(CONFIG_IPV6)
2860 if (opt != IPV6_XFRM_POLICY) {
2873 if (len < sizeof(*p) ||
2874 verify_newpolicy_info(p))
2877 nr = ((len - sizeof(*p)) / sizeof(*ut));
2878 if (validate_tmpl(nr, ut, p->sel.family))
2881 if (p->dir > XFRM_POLICY_OUT)
2884 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2890 copy_from_user_policy(xp, p);
2891 xp->type = XFRM_POLICY_TYPE_MAIN;
2892 copy_templates(xp, ut, nr);
2899 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2901 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2902 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2903 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2904 + nla_total_size(sizeof(struct xfrm_mark))
2905 + userpolicy_type_attrsize();
2908 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2909 int dir, const struct km_event *c)
2911 struct xfrm_user_polexpire *upe;
2912 int hard = c->data.hard;
2913 struct nlmsghdr *nlh;
2916 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2920 upe = nlmsg_data(nlh);
2921 copy_to_user_policy(xp, &upe->pol, dir);
2922 err = copy_to_user_tmpl(xp, skb);
2924 err = copy_to_user_sec_ctx(xp, skb);
2926 err = copy_to_user_policy_type(xp->type, skb);
2928 err = xfrm_mark_put(skb, &xp->mark);
2930 nlmsg_cancel(skb, nlh);
2935 nlmsg_end(skb, nlh);
2939 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2941 struct net *net = xp_net(xp);
2942 struct sk_buff *skb;
2944 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2948 if (build_polexpire(skb, xp, dir, c) < 0)
2951 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2954 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2956 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2957 struct net *net = xp_net(xp);
2958 struct xfrm_userpolicy_info *p;
2959 struct xfrm_userpolicy_id *id;
2960 struct nlmsghdr *nlh;
2961 struct sk_buff *skb;
2964 headlen = sizeof(*p);
2965 if (c->event == XFRM_MSG_DELPOLICY) {
2966 len += nla_total_size(headlen);
2967 headlen = sizeof(*id);
2969 len += userpolicy_type_attrsize();
2970 len += nla_total_size(sizeof(struct xfrm_mark));
2971 len += NLMSG_ALIGN(headlen);
2973 skb = nlmsg_new(len, GFP_ATOMIC);
2977 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2982 p = nlmsg_data(nlh);
2983 if (c->event == XFRM_MSG_DELPOLICY) {
2984 struct nlattr *attr;
2986 id = nlmsg_data(nlh);
2987 memset(id, 0, sizeof(*id));
2990 id->index = xp->index;
2992 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2994 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3002 copy_to_user_policy(xp, p, dir);
3003 err = copy_to_user_tmpl(xp, skb);
3005 err = copy_to_user_policy_type(xp->type, skb);
3007 err = xfrm_mark_put(skb, &xp->mark);
3011 nlmsg_end(skb, nlh);
3013 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3020 static int xfrm_notify_policy_flush(const struct km_event *c)
3022 struct net *net = c->net;
3023 struct nlmsghdr *nlh;
3024 struct sk_buff *skb;
3027 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3031 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3035 err = copy_to_user_policy_type(c->data.type, skb);
3039 nlmsg_end(skb, nlh);
3041 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3048 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3052 case XFRM_MSG_NEWPOLICY:
3053 case XFRM_MSG_UPDPOLICY:
3054 case XFRM_MSG_DELPOLICY:
3055 return xfrm_notify_policy(xp, dir, c);
3056 case XFRM_MSG_FLUSHPOLICY:
3057 return xfrm_notify_policy_flush(c);
3058 case XFRM_MSG_POLEXPIRE:
3059 return xfrm_exp_policy_notify(xp, dir, c);
3061 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3069 static inline size_t xfrm_report_msgsize(void)
3071 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3074 static int build_report(struct sk_buff *skb, u8 proto,
3075 struct xfrm_selector *sel, xfrm_address_t *addr)
3077 struct xfrm_user_report *ur;
3078 struct nlmsghdr *nlh;
3080 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3084 ur = nlmsg_data(nlh);
3086 memcpy(&ur->sel, sel, sizeof(ur->sel));
3089 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3091 nlmsg_cancel(skb, nlh);
3095 nlmsg_end(skb, nlh);
3099 static int xfrm_send_report(struct net *net, u8 proto,
3100 struct xfrm_selector *sel, xfrm_address_t *addr)
3102 struct sk_buff *skb;
3104 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3108 if (build_report(skb, proto, sel, addr) < 0)
3111 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3114 static inline size_t xfrm_mapping_msgsize(void)
3116 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3119 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3120 xfrm_address_t *new_saddr, __be16 new_sport)
3122 struct xfrm_user_mapping *um;
3123 struct nlmsghdr *nlh;
3125 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3129 um = nlmsg_data(nlh);
3131 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3132 um->id.spi = x->id.spi;
3133 um->id.family = x->props.family;
3134 um->id.proto = x->id.proto;
3135 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3136 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3137 um->new_sport = new_sport;
3138 um->old_sport = x->encap->encap_sport;
3139 um->reqid = x->props.reqid;
3141 nlmsg_end(skb, nlh);
3145 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3148 struct net *net = xs_net(x);
3149 struct sk_buff *skb;
3151 if (x->id.proto != IPPROTO_ESP)
3157 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3161 if (build_mapping(skb, x, ipaddr, sport) < 0)
3164 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3167 static bool xfrm_is_alive(const struct km_event *c)
3169 return (bool)xfrm_acquire_is_on(c->net);
3172 static struct xfrm_mgr netlink_mgr = {
3174 .notify = xfrm_send_state_notify,
3175 .acquire = xfrm_send_acquire,
3176 .compile_policy = xfrm_compile_policy,
3177 .notify_policy = xfrm_send_policy_notify,
3178 .report = xfrm_send_report,
3179 .migrate = xfrm_send_migrate,
3180 .new_mapping = xfrm_send_mapping,
3181 .is_alive = xfrm_is_alive,
3184 static int __net_init xfrm_user_net_init(struct net *net)
3187 struct netlink_kernel_cfg cfg = {
3188 .groups = XFRMNLGRP_MAX,
3189 .input = xfrm_netlink_rcv,
3192 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3195 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3196 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3200 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3203 list_for_each_entry(net, net_exit_list, exit_list)
3204 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3206 list_for_each_entry(net, net_exit_list, exit_list)
3207 netlink_kernel_release(net->xfrm.nlsk_stash);
3210 static struct pernet_operations xfrm_user_net_ops = {
3211 .init = xfrm_user_net_init,
3212 .exit_batch = xfrm_user_net_exit,
3215 static int __init xfrm_user_init(void)
3219 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3221 rv = register_pernet_subsys(&xfrm_user_net_ops);
3224 rv = xfrm_register_km(&netlink_mgr);
3226 unregister_pernet_subsys(&xfrm_user_net_ops);
3230 static void __exit xfrm_user_exit(void)
3232 xfrm_unregister_km(&netlink_mgr);
3233 unregister_pernet_subsys(&xfrm_user_net_ops);
3236 module_init(xfrm_user_init);
3237 module_exit(xfrm_user_exit);
3238 MODULE_LICENSE("GPL");
3239 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);