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 != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
118 static inline int verify_replay(struct xfrm_usersa_info *p,
119 struct nlattr **attrs)
121 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
122 struct xfrm_replay_state_esn *rs;
125 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
136 /* As only ESP and AH support ESN feature. */
137 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
140 if (p->replay_window != 0)
146 static int verify_newsa_info(struct xfrm_usersa_info *p,
147 struct nlattr **attrs)
157 #if IS_ENABLED(CONFIG_IPV6)
169 switch (p->id.proto) {
171 if ((!attrs[XFRMA_ALG_AUTH] &&
172 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
173 attrs[XFRMA_ALG_AEAD] ||
174 attrs[XFRMA_ALG_CRYPT] ||
175 attrs[XFRMA_ALG_COMP] ||
181 if (attrs[XFRMA_ALG_COMP])
183 if (!attrs[XFRMA_ALG_AUTH] &&
184 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
185 !attrs[XFRMA_ALG_CRYPT] &&
186 !attrs[XFRMA_ALG_AEAD])
188 if ((attrs[XFRMA_ALG_AUTH] ||
189 attrs[XFRMA_ALG_AUTH_TRUNC] ||
190 attrs[XFRMA_ALG_CRYPT]) &&
191 attrs[XFRMA_ALG_AEAD])
193 if (attrs[XFRMA_TFCPAD] &&
194 p->mode != XFRM_MODE_TUNNEL)
199 if (!attrs[XFRMA_ALG_COMP] ||
200 attrs[XFRMA_ALG_AEAD] ||
201 attrs[XFRMA_ALG_AUTH] ||
202 attrs[XFRMA_ALG_AUTH_TRUNC] ||
203 attrs[XFRMA_ALG_CRYPT] ||
204 attrs[XFRMA_TFCPAD] ||
205 (ntohl(p->id.spi) >= 0x10000))
209 #if IS_ENABLED(CONFIG_IPV6)
210 case IPPROTO_DSTOPTS:
211 case IPPROTO_ROUTING:
212 if (attrs[XFRMA_ALG_COMP] ||
213 attrs[XFRMA_ALG_AUTH] ||
214 attrs[XFRMA_ALG_AUTH_TRUNC] ||
215 attrs[XFRMA_ALG_AEAD] ||
216 attrs[XFRMA_ALG_CRYPT] ||
217 attrs[XFRMA_ENCAP] ||
218 attrs[XFRMA_SEC_CTX] ||
219 attrs[XFRMA_TFCPAD] ||
220 !attrs[XFRMA_COADDR])
229 if ((err = verify_aead(attrs)))
231 if ((err = verify_auth_trunc(attrs)))
233 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
235 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
237 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
239 if ((err = verify_sec_ctx_len(attrs)))
241 if ((err = verify_replay(p, attrs)))
246 case XFRM_MODE_TRANSPORT:
247 case XFRM_MODE_TUNNEL:
248 case XFRM_MODE_ROUTEOPTIMIZATION:
262 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
263 struct xfrm_algo_desc *(*get_byname)(const char *, int),
266 struct xfrm_algo *p, *ualg;
267 struct xfrm_algo_desc *algo;
272 ualg = nla_data(rta);
274 algo = get_byname(ualg->alg_name, 1);
277 *props = algo->desc.sadb_alg_id;
279 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
283 strcpy(p->alg_name, algo->name);
288 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
290 struct xfrm_algo *p, *ualg;
291 struct xfrm_algo_desc *algo;
296 ualg = nla_data(rta);
298 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
301 x->props.ealgo = algo->desc.sadb_alg_id;
303 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
307 strcpy(p->alg_name, algo->name);
309 x->geniv = algo->uinfo.encr.geniv;
313 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
316 struct xfrm_algo *ualg;
317 struct xfrm_algo_auth *p;
318 struct xfrm_algo_desc *algo;
323 ualg = nla_data(rta);
325 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
328 *props = algo->desc.sadb_alg_id;
330 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
334 strcpy(p->alg_name, algo->name);
335 p->alg_key_len = ualg->alg_key_len;
336 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
337 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
343 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
346 struct xfrm_algo_auth *p, *ualg;
347 struct xfrm_algo_desc *algo;
352 ualg = nla_data(rta);
354 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
357 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
359 *props = algo->desc.sadb_alg_id;
361 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
365 strcpy(p->alg_name, algo->name);
366 if (!p->alg_trunc_len)
367 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
373 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
375 struct xfrm_algo_aead *p, *ualg;
376 struct xfrm_algo_desc *algo;
381 ualg = nla_data(rta);
383 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
386 x->props.ealgo = algo->desc.sadb_alg_id;
388 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
392 strcpy(p->alg_name, algo->name);
394 x->geniv = algo->uinfo.aead.geniv;
398 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
401 struct xfrm_replay_state_esn *up;
404 if (!replay_esn || !rp)
408 ulen = xfrm_replay_state_esn_len(up);
410 /* Check the overall length and the internal bitmap length to avoid
411 * potential overflow. */
412 if (nla_len(rp) < ulen ||
413 xfrm_replay_state_esn_len(replay_esn) != ulen ||
414 replay_esn->bmp_len != up->bmp_len)
417 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
423 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
424 struct xfrm_replay_state_esn **preplay_esn,
427 struct xfrm_replay_state_esn *p, *pp, *up;
434 klen = xfrm_replay_state_esn_len(up);
435 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
437 p = kzalloc(klen, GFP_KERNEL);
441 pp = kzalloc(klen, GFP_KERNEL);
448 memcpy(pp, up, ulen);
456 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
461 len += sizeof(struct xfrm_user_sec_ctx);
462 len += xfrm_ctx->ctx_len;
467 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
469 memcpy(&x->id, &p->id, sizeof(x->id));
470 memcpy(&x->sel, &p->sel, sizeof(x->sel));
471 memcpy(&x->lft, &p->lft, sizeof(x->lft));
472 x->props.mode = p->mode;
473 x->props.replay_window = min_t(unsigned int, p->replay_window,
474 sizeof(x->replay.bitmap) * 8);
475 x->props.reqid = p->reqid;
476 x->props.family = p->family;
477 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
478 x->props.flags = p->flags;
480 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
481 x->sel.family = p->family;
485 * someday when pfkey also has support, we could have the code
486 * somehow made shareable and move it to xfrm_state.c - JHS
489 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
492 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
493 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
494 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
495 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
496 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
499 struct xfrm_replay_state_esn *replay_esn;
500 replay_esn = nla_data(re);
501 memcpy(x->replay_esn, replay_esn,
502 xfrm_replay_state_esn_len(replay_esn));
503 memcpy(x->preplay_esn, replay_esn,
504 xfrm_replay_state_esn_len(replay_esn));
508 struct xfrm_replay_state *replay;
509 replay = nla_data(rp);
510 memcpy(&x->replay, replay, sizeof(*replay));
511 memcpy(&x->preplay, replay, sizeof(*replay));
515 struct xfrm_lifetime_cur *ltime;
516 ltime = nla_data(lt);
517 x->curlft.bytes = ltime->bytes;
518 x->curlft.packets = ltime->packets;
519 x->curlft.add_time = ltime->add_time;
520 x->curlft.use_time = ltime->use_time;
524 x->replay_maxage = nla_get_u32(et);
527 x->replay_maxdiff = nla_get_u32(rt);
530 static struct xfrm_state *xfrm_state_construct(struct net *net,
531 struct xfrm_usersa_info *p,
532 struct nlattr **attrs,
535 struct xfrm_state *x = xfrm_state_alloc(net);
541 copy_from_user_state(x, p);
543 if (attrs[XFRMA_SA_EXTRA_FLAGS])
544 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
546 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
548 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
549 attrs[XFRMA_ALG_AUTH_TRUNC])))
551 if (!x->props.aalgo) {
552 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
553 attrs[XFRMA_ALG_AUTH])))
556 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
558 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
559 xfrm_calg_get_byname,
560 attrs[XFRMA_ALG_COMP])))
563 if (attrs[XFRMA_ENCAP]) {
564 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
565 sizeof(*x->encap), GFP_KERNEL);
566 if (x->encap == NULL)
570 if (attrs[XFRMA_TFCPAD])
571 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
573 if (attrs[XFRMA_COADDR]) {
574 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
575 sizeof(*x->coaddr), GFP_KERNEL);
576 if (x->coaddr == NULL)
580 xfrm_mark_get(attrs, &x->mark);
582 if (attrs[XFRMA_OUTPUT_MARK])
583 x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
585 err = __xfrm_init_state(x, false);
589 if (attrs[XFRMA_SEC_CTX] &&
590 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
593 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
594 attrs[XFRMA_REPLAY_ESN_VAL])))
598 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
599 /* sysctl_xfrm_aevent_etime is in 100ms units */
600 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
602 if ((err = xfrm_init_replay(x)))
605 /* override default values from above */
606 xfrm_update_ae_params(x, attrs, 0);
611 x->km.state = XFRM_STATE_DEAD;
618 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
619 struct nlattr **attrs)
621 struct net *net = sock_net(skb->sk);
622 struct xfrm_usersa_info *p = nlmsg_data(nlh);
623 struct xfrm_state *x;
627 err = verify_newsa_info(p, attrs);
631 x = xfrm_state_construct(net, p, attrs, &err);
636 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
637 err = xfrm_state_add(x);
639 err = xfrm_state_update(x);
641 xfrm_audit_state_add(x, err ? 0 : 1, true);
644 x->km.state = XFRM_STATE_DEAD;
649 c.seq = nlh->nlmsg_seq;
650 c.portid = nlh->nlmsg_pid;
651 c.event = nlh->nlmsg_type;
653 km_state_notify(x, &c);
659 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
660 struct xfrm_usersa_id *p,
661 struct nlattr **attrs,
664 struct xfrm_state *x = NULL;
667 u32 mark = xfrm_mark_get(attrs, &m);
669 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
671 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
673 xfrm_address_t *saddr = NULL;
675 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
682 x = xfrm_state_lookup_byaddr(net, mark,
684 p->proto, p->family);
693 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
694 struct nlattr **attrs)
696 struct net *net = sock_net(skb->sk);
697 struct xfrm_state *x;
700 struct xfrm_usersa_id *p = nlmsg_data(nlh);
702 x = xfrm_user_state_lookup(net, p, attrs, &err);
706 if ((err = security_xfrm_state_delete(x)) != 0)
709 if (xfrm_state_kern(x)) {
714 err = xfrm_state_delete(x);
719 c.seq = nlh->nlmsg_seq;
720 c.portid = nlh->nlmsg_pid;
721 c.event = nlh->nlmsg_type;
722 km_state_notify(x, &c);
725 xfrm_audit_state_delete(x, err ? 0 : 1, true);
730 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
732 memset(p, 0, sizeof(*p));
733 memcpy(&p->id, &x->id, sizeof(p->id));
734 memcpy(&p->sel, &x->sel, sizeof(p->sel));
735 memcpy(&p->lft, &x->lft, sizeof(p->lft));
736 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
737 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
738 put_unaligned(x->stats.replay, &p->stats.replay);
739 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
740 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
741 p->mode = x->props.mode;
742 p->replay_window = x->props.replay_window;
743 p->reqid = x->props.reqid;
744 p->family = x->props.family;
745 p->flags = x->props.flags;
749 struct xfrm_dump_info {
750 struct sk_buff *in_skb;
751 struct sk_buff *out_skb;
756 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
758 struct xfrm_user_sec_ctx *uctx;
760 int ctx_size = sizeof(*uctx) + s->ctx_len;
762 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
766 uctx = nla_data(attr);
767 uctx->exttype = XFRMA_SEC_CTX;
768 uctx->len = ctx_size;
769 uctx->ctx_doi = s->ctx_doi;
770 uctx->ctx_alg = s->ctx_alg;
771 uctx->ctx_len = s->ctx_len;
772 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
777 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
779 struct xfrm_algo *algo;
782 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
783 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
787 algo = nla_data(nla);
788 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
789 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
790 algo->alg_key_len = auth->alg_key_len;
795 /* Don't change this without updating xfrm_sa_len! */
796 static int copy_to_user_state_extra(struct xfrm_state *x,
797 struct xfrm_usersa_info *p,
802 copy_to_user_state(x, p);
804 if (x->props.extra_flags) {
805 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
806 x->props.extra_flags);
812 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
817 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
822 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
827 ret = copy_to_user_auth(x->aalg, skb);
829 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
830 xfrm_alg_auth_len(x->aalg), x->aalg);
835 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
840 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
845 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
850 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
854 ret = xfrm_mark_put(skb, &x->mark);
858 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
859 xfrm_replay_state_esn_len(x->replay_esn),
862 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
866 if (x->props.output_mark) {
867 ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
872 ret = copy_sec_ctx(x->security, skb);
877 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
879 struct xfrm_dump_info *sp = ptr;
880 struct sk_buff *in_skb = sp->in_skb;
881 struct sk_buff *skb = sp->out_skb;
882 struct xfrm_usersa_info *p;
883 struct nlmsghdr *nlh;
886 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
887 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
893 err = copy_to_user_state_extra(x, p, skb);
895 nlmsg_cancel(skb, nlh);
902 static int xfrm_dump_sa_done(struct netlink_callback *cb)
904 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
905 struct sock *sk = cb->skb->sk;
906 struct net *net = sock_net(sk);
908 xfrm_state_walk_done(walk, net);
912 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
913 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
915 struct net *net = sock_net(skb->sk);
916 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
917 struct xfrm_dump_info info;
919 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
920 sizeof(cb->args) - sizeof(cb->args[0]));
922 info.in_skb = cb->skb;
924 info.nlmsg_seq = cb->nlh->nlmsg_seq;
925 info.nlmsg_flags = NLM_F_MULTI;
928 struct nlattr *attrs[XFRMA_MAX+1];
929 struct xfrm_address_filter *filter = NULL;
935 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
940 if (attrs[XFRMA_ADDRESS_FILTER]) {
941 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
942 sizeof(*filter), GFP_KERNEL);
947 if (attrs[XFRMA_PROTO])
948 proto = nla_get_u8(attrs[XFRMA_PROTO]);
950 xfrm_state_walk_init(walk, proto, filter);
953 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
958 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
959 struct xfrm_state *x, u32 seq)
961 struct xfrm_dump_info info;
965 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
967 return ERR_PTR(-ENOMEM);
969 info.in_skb = in_skb;
971 info.nlmsg_seq = seq;
972 info.nlmsg_flags = 0;
974 err = dump_one_state(x, 0, &info);
983 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
984 * Must be called with RCU read lock.
986 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
987 u32 pid, unsigned int group)
989 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
992 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
997 static inline size_t xfrm_spdinfo_msgsize(void)
999 return NLMSG_ALIGN(4)
1000 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1001 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1002 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1003 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1006 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1007 u32 portid, u32 seq, u32 flags)
1009 struct xfrmk_spdinfo si;
1010 struct xfrmu_spdinfo spc;
1011 struct xfrmu_spdhinfo sph;
1012 struct xfrmu_spdhthresh spt4, spt6;
1013 struct nlmsghdr *nlh;
1018 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1019 if (nlh == NULL) /* shouldn't really happen ... */
1022 f = nlmsg_data(nlh);
1024 xfrm_spd_getinfo(net, &si);
1025 spc.incnt = si.incnt;
1026 spc.outcnt = si.outcnt;
1027 spc.fwdcnt = si.fwdcnt;
1028 spc.inscnt = si.inscnt;
1029 spc.outscnt = si.outscnt;
1030 spc.fwdscnt = si.fwdscnt;
1031 sph.spdhcnt = si.spdhcnt;
1032 sph.spdhmcnt = si.spdhmcnt;
1035 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1037 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1038 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1039 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1040 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1041 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1043 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1045 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1047 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1049 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1051 nlmsg_cancel(skb, nlh);
1055 nlmsg_end(skb, nlh);
1059 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1060 struct nlattr **attrs)
1062 struct net *net = sock_net(skb->sk);
1063 struct xfrmu_spdhthresh *thresh4 = NULL;
1064 struct xfrmu_spdhthresh *thresh6 = NULL;
1066 /* selector prefixlen thresholds to hash policies */
1067 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1068 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1070 if (nla_len(rta) < sizeof(*thresh4))
1072 thresh4 = nla_data(rta);
1073 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1076 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1077 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1079 if (nla_len(rta) < sizeof(*thresh6))
1081 thresh6 = nla_data(rta);
1082 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1086 if (thresh4 || thresh6) {
1087 write_seqlock(&net->xfrm.policy_hthresh.lock);
1089 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1090 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1093 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1094 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1096 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1098 xfrm_policy_hash_rebuild(net);
1104 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1105 struct nlattr **attrs)
1107 struct net *net = sock_net(skb->sk);
1108 struct sk_buff *r_skb;
1109 u32 *flags = nlmsg_data(nlh);
1110 u32 sportid = NETLINK_CB(skb).portid;
1111 u32 seq = nlh->nlmsg_seq;
1113 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1117 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1120 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1123 static inline size_t xfrm_sadinfo_msgsize(void)
1125 return NLMSG_ALIGN(4)
1126 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1127 + nla_total_size(4); /* XFRMA_SAD_CNT */
1130 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1131 u32 portid, u32 seq, u32 flags)
1133 struct xfrmk_sadinfo si;
1134 struct xfrmu_sadhinfo sh;
1135 struct nlmsghdr *nlh;
1139 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1140 if (nlh == NULL) /* shouldn't really happen ... */
1143 f = nlmsg_data(nlh);
1145 xfrm_sad_getinfo(net, &si);
1147 sh.sadhmcnt = si.sadhmcnt;
1148 sh.sadhcnt = si.sadhcnt;
1150 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1152 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1154 nlmsg_cancel(skb, nlh);
1158 nlmsg_end(skb, nlh);
1162 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1163 struct nlattr **attrs)
1165 struct net *net = sock_net(skb->sk);
1166 struct sk_buff *r_skb;
1167 u32 *flags = nlmsg_data(nlh);
1168 u32 sportid = NETLINK_CB(skb).portid;
1169 u32 seq = nlh->nlmsg_seq;
1171 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1175 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1178 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1181 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1182 struct nlattr **attrs)
1184 struct net *net = sock_net(skb->sk);
1185 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1186 struct xfrm_state *x;
1187 struct sk_buff *resp_skb;
1190 x = xfrm_user_state_lookup(net, p, attrs, &err);
1194 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1195 if (IS_ERR(resp_skb)) {
1196 err = PTR_ERR(resp_skb);
1198 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1205 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1206 struct nlattr **attrs)
1208 struct net *net = sock_net(skb->sk);
1209 struct xfrm_state *x;
1210 struct xfrm_userspi_info *p;
1211 struct sk_buff *resp_skb;
1212 xfrm_address_t *daddr;
1218 p = nlmsg_data(nlh);
1219 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1223 family = p->info.family;
1224 daddr = &p->info.id.daddr;
1228 mark = xfrm_mark_get(attrs, &m);
1230 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1231 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1238 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1239 p->info.id.proto, daddr,
1246 err = xfrm_alloc_spi(x, p->min, p->max);
1250 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1251 if (IS_ERR(resp_skb)) {
1252 err = PTR_ERR(resp_skb);
1256 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1264 static int verify_policy_dir(u8 dir)
1267 case XFRM_POLICY_IN:
1268 case XFRM_POLICY_OUT:
1269 case XFRM_POLICY_FWD:
1279 static int verify_policy_type(u8 type)
1282 case XFRM_POLICY_TYPE_MAIN:
1283 #ifdef CONFIG_XFRM_SUB_POLICY
1284 case XFRM_POLICY_TYPE_SUB:
1295 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1300 case XFRM_SHARE_ANY:
1301 case XFRM_SHARE_SESSION:
1302 case XFRM_SHARE_USER:
1303 case XFRM_SHARE_UNIQUE:
1310 switch (p->action) {
1311 case XFRM_POLICY_ALLOW:
1312 case XFRM_POLICY_BLOCK:
1319 switch (p->sel.family) {
1324 #if IS_ENABLED(CONFIG_IPV6)
1327 return -EAFNOSUPPORT;
1334 ret = verify_policy_dir(p->dir);
1337 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1343 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1345 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1346 struct xfrm_user_sec_ctx *uctx;
1351 uctx = nla_data(rt);
1352 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1355 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1361 for (i = 0; i < nr; i++, ut++) {
1362 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1364 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1365 memcpy(&t->saddr, &ut->saddr,
1366 sizeof(xfrm_address_t));
1367 t->reqid = ut->reqid;
1369 t->share = ut->share;
1370 t->optional = ut->optional;
1371 t->aalgos = ut->aalgos;
1372 t->ealgos = ut->ealgos;
1373 t->calgos = ut->calgos;
1374 /* If all masks are ~0, then we allow all algorithms. */
1375 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1376 t->encap_family = ut->family;
1380 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1385 if (nr > XFRM_MAX_DEPTH)
1388 prev_family = family;
1390 for (i = 0; i < nr; i++) {
1391 /* We never validated the ut->family value, so many
1392 * applications simply leave it at zero. The check was
1393 * never made and ut->family was ignored because all
1394 * templates could be assumed to have the same family as
1395 * the policy itself. Now that we will have ipv4-in-ipv6
1396 * and ipv6-in-ipv4 tunnels, this is no longer true.
1399 ut[i].family = family;
1401 if ((ut[i].mode == XFRM_MODE_TRANSPORT) &&
1402 (ut[i].family != prev_family))
1405 prev_family = ut[i].family;
1407 switch (ut[i].family) {
1410 #if IS_ENABLED(CONFIG_IPV6)
1418 switch (ut[i].id.proto) {
1422 #if IS_ENABLED(CONFIG_IPV6)
1423 case IPPROTO_ROUTING:
1424 case IPPROTO_DSTOPTS:
1426 case IPSEC_PROTO_ANY:
1437 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1439 struct nlattr *rt = attrs[XFRMA_TMPL];
1444 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1445 int nr = nla_len(rt) / sizeof(*utmpl);
1448 err = validate_tmpl(nr, utmpl, pol->family);
1452 copy_templates(pol, utmpl, nr);
1457 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1459 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1460 struct xfrm_userpolicy_type *upt;
1461 u8 type = XFRM_POLICY_TYPE_MAIN;
1469 err = verify_policy_type(type);
1477 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1479 xp->priority = p->priority;
1480 xp->index = p->index;
1481 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1482 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1483 xp->action = p->action;
1484 xp->flags = p->flags;
1485 xp->family = p->sel.family;
1486 /* XXX xp->share = p->share; */
1489 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1491 memset(p, 0, sizeof(*p));
1492 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1493 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1494 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1495 p->priority = xp->priority;
1496 p->index = xp->index;
1497 p->sel.family = xp->family;
1499 p->action = xp->action;
1500 p->flags = xp->flags;
1501 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1504 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1506 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1514 copy_from_user_policy(xp, p);
1516 err = copy_from_user_policy_type(&xp->type, attrs);
1520 if (!(err = copy_from_user_tmpl(xp, attrs)))
1521 err = copy_from_user_sec_ctx(xp, attrs);
1525 xfrm_mark_get(attrs, &xp->mark);
1531 xfrm_policy_destroy(xp);
1535 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1536 struct nlattr **attrs)
1538 struct net *net = sock_net(skb->sk);
1539 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1540 struct xfrm_policy *xp;
1545 err = verify_newpolicy_info(p);
1548 err = verify_sec_ctx_len(attrs);
1552 xp = xfrm_policy_construct(net, p, attrs, &err);
1556 /* shouldn't excl be based on nlh flags??
1557 * Aha! this is anti-netlink really i.e more pfkey derived
1558 * in netlink excl is a flag and you wouldnt need
1559 * a type XFRM_MSG_UPDPOLICY - JHS */
1560 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1561 err = xfrm_policy_insert(p->dir, xp, excl);
1562 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1565 security_xfrm_policy_free(xp->security);
1570 c.event = nlh->nlmsg_type;
1571 c.seq = nlh->nlmsg_seq;
1572 c.portid = nlh->nlmsg_pid;
1573 km_policy_notify(xp, p->dir, &c);
1580 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1582 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1585 if (xp->xfrm_nr == 0)
1588 for (i = 0; i < xp->xfrm_nr; i++) {
1589 struct xfrm_user_tmpl *up = &vec[i];
1590 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1592 memset(up, 0, sizeof(*up));
1593 memcpy(&up->id, &kp->id, sizeof(up->id));
1594 up->family = kp->encap_family;
1595 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1596 up->reqid = kp->reqid;
1597 up->mode = kp->mode;
1598 up->share = kp->share;
1599 up->optional = kp->optional;
1600 up->aalgos = kp->aalgos;
1601 up->ealgos = kp->ealgos;
1602 up->calgos = kp->calgos;
1605 return nla_put(skb, XFRMA_TMPL,
1606 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1609 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1612 return copy_sec_ctx(x->security, skb);
1617 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1620 return copy_sec_ctx(xp->security, skb);
1623 static inline size_t userpolicy_type_attrsize(void)
1625 #ifdef CONFIG_XFRM_SUB_POLICY
1626 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1632 #ifdef CONFIG_XFRM_SUB_POLICY
1633 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1635 struct xfrm_userpolicy_type upt = {
1639 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1643 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1649 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1651 struct xfrm_dump_info *sp = ptr;
1652 struct xfrm_userpolicy_info *p;
1653 struct sk_buff *in_skb = sp->in_skb;
1654 struct sk_buff *skb = sp->out_skb;
1655 struct nlmsghdr *nlh;
1658 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1659 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1663 p = nlmsg_data(nlh);
1664 copy_to_user_policy(xp, p, dir);
1665 err = copy_to_user_tmpl(xp, skb);
1667 err = copy_to_user_sec_ctx(xp, skb);
1669 err = copy_to_user_policy_type(xp->type, skb);
1671 err = xfrm_mark_put(skb, &xp->mark);
1673 nlmsg_cancel(skb, nlh);
1676 nlmsg_end(skb, nlh);
1680 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1682 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1683 struct net *net = sock_net(cb->skb->sk);
1685 xfrm_policy_walk_done(walk, net);
1689 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1691 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1693 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1695 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1699 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1701 struct net *net = sock_net(skb->sk);
1702 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1703 struct xfrm_dump_info info;
1705 info.in_skb = cb->skb;
1707 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1708 info.nlmsg_flags = NLM_F_MULTI;
1710 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1715 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1716 struct xfrm_policy *xp,
1719 struct xfrm_dump_info info;
1720 struct sk_buff *skb;
1723 err = verify_policy_dir(dir);
1725 return ERR_PTR(err);
1727 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1729 return ERR_PTR(-ENOMEM);
1731 info.in_skb = in_skb;
1733 info.nlmsg_seq = seq;
1734 info.nlmsg_flags = 0;
1736 err = dump_one_policy(xp, dir, 0, &info);
1739 return ERR_PTR(err);
1745 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1746 struct nlattr **attrs)
1748 struct net *net = sock_net(skb->sk);
1749 struct xfrm_policy *xp;
1750 struct xfrm_userpolicy_id *p;
1751 u8 type = XFRM_POLICY_TYPE_MAIN;
1756 u32 mark = xfrm_mark_get(attrs, &m);
1758 p = nlmsg_data(nlh);
1759 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1761 err = copy_from_user_policy_type(&type, attrs);
1765 err = verify_policy_dir(p->dir);
1770 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1772 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1773 struct xfrm_sec_ctx *ctx;
1775 err = verify_sec_ctx_len(attrs);
1781 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1783 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1787 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1789 security_xfrm_policy_free(ctx);
1795 struct sk_buff *resp_skb;
1797 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1798 if (IS_ERR(resp_skb)) {
1799 err = PTR_ERR(resp_skb);
1801 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1802 NETLINK_CB(skb).portid);
1805 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1810 c.data.byid = p->index;
1811 c.event = nlh->nlmsg_type;
1812 c.seq = nlh->nlmsg_seq;
1813 c.portid = nlh->nlmsg_pid;
1814 km_policy_notify(xp, p->dir, &c);
1819 if (delete && err == 0)
1820 xfrm_garbage_collect(net);
1824 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1825 struct nlattr **attrs)
1827 struct net *net = sock_net(skb->sk);
1829 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1832 err = xfrm_state_flush(net, p->proto, true);
1834 if (err == -ESRCH) /* empty table */
1838 c.data.proto = p->proto;
1839 c.event = nlh->nlmsg_type;
1840 c.seq = nlh->nlmsg_seq;
1841 c.portid = nlh->nlmsg_pid;
1843 km_state_notify(NULL, &c);
1848 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1850 size_t replay_size = x->replay_esn ?
1851 xfrm_replay_state_esn_len(x->replay_esn) :
1852 sizeof(struct xfrm_replay_state);
1854 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1855 + nla_total_size(replay_size)
1856 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1857 + nla_total_size(sizeof(struct xfrm_mark))
1858 + nla_total_size(4) /* XFRM_AE_RTHR */
1859 + nla_total_size(4); /* XFRM_AE_ETHR */
1862 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1864 struct xfrm_aevent_id *id;
1865 struct nlmsghdr *nlh;
1868 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1872 id = nlmsg_data(nlh);
1873 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1874 id->sa_id.spi = x->id.spi;
1875 id->sa_id.family = x->props.family;
1876 id->sa_id.proto = x->id.proto;
1877 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1878 id->reqid = x->props.reqid;
1879 id->flags = c->data.aevent;
1881 if (x->replay_esn) {
1882 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1883 xfrm_replay_state_esn_len(x->replay_esn),
1886 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1891 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1895 if (id->flags & XFRM_AE_RTHR) {
1896 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1900 if (id->flags & XFRM_AE_ETHR) {
1901 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1902 x->replay_maxage * 10 / HZ);
1906 err = xfrm_mark_put(skb, &x->mark);
1910 nlmsg_end(skb, nlh);
1914 nlmsg_cancel(skb, nlh);
1918 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1919 struct nlattr **attrs)
1921 struct net *net = sock_net(skb->sk);
1922 struct xfrm_state *x;
1923 struct sk_buff *r_skb;
1928 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1929 struct xfrm_usersa_id *id = &p->sa_id;
1931 mark = xfrm_mark_get(attrs, &m);
1933 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1937 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1938 if (r_skb == NULL) {
1944 * XXX: is this lock really needed - none of the other
1945 * gets lock (the concern is things getting updated
1946 * while we are still reading) - jhs
1948 spin_lock_bh(&x->lock);
1949 c.data.aevent = p->flags;
1950 c.seq = nlh->nlmsg_seq;
1951 c.portid = nlh->nlmsg_pid;
1953 if (build_aevent(r_skb, x, &c) < 0)
1955 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1956 spin_unlock_bh(&x->lock);
1961 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1962 struct nlattr **attrs)
1964 struct net *net = sock_net(skb->sk);
1965 struct xfrm_state *x;
1970 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1971 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1972 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1973 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1974 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1975 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
1977 if (!lt && !rp && !re && !et && !rt)
1980 /* pedantic mode - thou shalt sayeth replaceth */
1981 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1984 mark = xfrm_mark_get(attrs, &m);
1986 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1990 if (x->km.state != XFRM_STATE_VALID)
1993 err = xfrm_replay_verify_len(x->replay_esn, re);
1997 spin_lock_bh(&x->lock);
1998 xfrm_update_ae_params(x, attrs, 1);
1999 spin_unlock_bh(&x->lock);
2001 c.event = nlh->nlmsg_type;
2002 c.seq = nlh->nlmsg_seq;
2003 c.portid = nlh->nlmsg_pid;
2004 c.data.aevent = XFRM_AE_CU;
2005 km_state_notify(x, &c);
2012 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2013 struct nlattr **attrs)
2015 struct net *net = sock_net(skb->sk);
2017 u8 type = XFRM_POLICY_TYPE_MAIN;
2020 err = copy_from_user_policy_type(&type, attrs);
2024 err = xfrm_policy_flush(net, type, true);
2026 if (err == -ESRCH) /* empty table */
2032 c.event = nlh->nlmsg_type;
2033 c.seq = nlh->nlmsg_seq;
2034 c.portid = nlh->nlmsg_pid;
2036 km_policy_notify(NULL, 0, &c);
2040 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2041 struct nlattr **attrs)
2043 struct net *net = sock_net(skb->sk);
2044 struct xfrm_policy *xp;
2045 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2046 struct xfrm_userpolicy_info *p = &up->pol;
2047 u8 type = XFRM_POLICY_TYPE_MAIN;
2050 u32 mark = xfrm_mark_get(attrs, &m);
2052 err = copy_from_user_policy_type(&type, attrs);
2056 err = verify_policy_dir(p->dir);
2061 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
2063 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2064 struct xfrm_sec_ctx *ctx;
2066 err = verify_sec_ctx_len(attrs);
2072 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2074 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2078 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
2079 &p->sel, ctx, 0, &err);
2080 security_xfrm_policy_free(ctx);
2085 if (unlikely(xp->walk.dead))
2090 xfrm_policy_delete(xp, p->dir);
2091 xfrm_audit_policy_delete(xp, 1, true);
2093 // reset the timers here?
2094 WARN(1, "Don't know what to do with soft policy expire\n");
2096 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2103 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2104 struct nlattr **attrs)
2106 struct net *net = sock_net(skb->sk);
2107 struct xfrm_state *x;
2109 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2110 struct xfrm_usersa_info *p = &ue->state;
2112 u32 mark = xfrm_mark_get(attrs, &m);
2114 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2120 spin_lock_bh(&x->lock);
2122 if (x->km.state != XFRM_STATE_VALID)
2124 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2127 __xfrm_state_delete(x);
2128 xfrm_audit_state_delete(x, 1, true);
2132 spin_unlock_bh(&x->lock);
2137 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2138 struct nlattr **attrs)
2140 struct net *net = sock_net(skb->sk);
2141 struct xfrm_policy *xp;
2142 struct xfrm_user_tmpl *ut;
2144 struct nlattr *rt = attrs[XFRMA_TMPL];
2145 struct xfrm_mark mark;
2147 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2148 struct xfrm_state *x = xfrm_state_alloc(net);
2154 xfrm_mark_get(attrs, &mark);
2156 err = verify_newpolicy_info(&ua->policy);
2161 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2165 memcpy(&x->id, &ua->id, sizeof(ua->id));
2166 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2167 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2168 xp->mark.m = x->mark.m = mark.m;
2169 xp->mark.v = x->mark.v = mark.v;
2171 /* extract the templates and for each call km_key */
2172 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2173 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2174 memcpy(&x->id, &t->id, sizeof(x->id));
2175 x->props.mode = t->mode;
2176 x->props.reqid = t->reqid;
2177 x->props.family = ut->family;
2178 t->aalgos = ua->aalgos;
2179 t->ealgos = ua->ealgos;
2180 t->calgos = ua->calgos;
2181 err = km_query(x, t, xp);
2191 WARN(1, "BAD policy passed\n");
2198 #ifdef CONFIG_XFRM_MIGRATE
2199 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2200 struct xfrm_kmaddress *k,
2201 struct nlattr **attrs, int *num)
2203 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2204 struct xfrm_user_migrate *um;
2208 struct xfrm_user_kmaddress *uk;
2210 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2211 memcpy(&k->local, &uk->local, sizeof(k->local));
2212 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2213 k->family = uk->family;
2214 k->reserved = uk->reserved;
2218 num_migrate = nla_len(rt) / sizeof(*um);
2220 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2223 for (i = 0; i < num_migrate; i++, um++, ma++) {
2224 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2225 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2226 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2227 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2229 ma->proto = um->proto;
2230 ma->mode = um->mode;
2231 ma->reqid = um->reqid;
2233 ma->old_family = um->old_family;
2234 ma->new_family = um->new_family;
2241 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2242 struct nlattr **attrs)
2244 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2245 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2246 struct xfrm_kmaddress km, *kmp;
2250 struct net *net = sock_net(skb->sk);
2252 err = verify_policy_dir(pi->dir);
2256 if (attrs[XFRMA_MIGRATE] == NULL)
2259 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2261 err = copy_from_user_policy_type(&type, attrs);
2265 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2272 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2277 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2278 struct nlattr **attrs)
2280 return -ENOPROTOOPT;
2284 #ifdef CONFIG_XFRM_MIGRATE
2285 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2287 struct xfrm_user_migrate um;
2289 memset(&um, 0, sizeof(um));
2290 um.proto = m->proto;
2292 um.reqid = m->reqid;
2293 um.old_family = m->old_family;
2294 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2295 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2296 um.new_family = m->new_family;
2297 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2298 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2300 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2303 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2305 struct xfrm_user_kmaddress uk;
2307 memset(&uk, 0, sizeof(uk));
2308 uk.family = k->family;
2309 uk.reserved = k->reserved;
2310 memcpy(&uk.local, &k->local, sizeof(uk.local));
2311 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2313 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2316 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2318 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2319 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2320 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2321 + userpolicy_type_attrsize();
2324 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2325 int num_migrate, const struct xfrm_kmaddress *k,
2326 const struct xfrm_selector *sel, u8 dir, u8 type)
2328 const struct xfrm_migrate *mp;
2329 struct xfrm_userpolicy_id *pol_id;
2330 struct nlmsghdr *nlh;
2333 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2337 pol_id = nlmsg_data(nlh);
2338 /* copy data from selector, dir, and type to the pol_id */
2339 memset(pol_id, 0, sizeof(*pol_id));
2340 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2344 err = copy_to_user_kmaddress(k, skb);
2348 err = copy_to_user_policy_type(type, skb);
2351 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2352 err = copy_to_user_migrate(mp, skb);
2357 nlmsg_end(skb, nlh);
2361 nlmsg_cancel(skb, nlh);
2365 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2366 const struct xfrm_migrate *m, int num_migrate,
2367 const struct xfrm_kmaddress *k)
2369 struct net *net = &init_net;
2370 struct sk_buff *skb;
2373 err = verify_policy_dir(dir);
2377 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2382 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2385 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2388 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2389 const struct xfrm_migrate *m, int num_migrate,
2390 const struct xfrm_kmaddress *k)
2392 return -ENOPROTOOPT;
2396 #define XMSGSIZE(type) sizeof(struct type)
2398 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2399 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2400 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2401 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2402 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2403 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2404 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2405 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2406 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2407 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2408 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2409 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2410 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2411 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2412 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2413 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2414 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2415 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2416 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2417 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2418 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2419 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2424 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2425 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2426 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2427 [XFRMA_LASTUSED] = { .type = NLA_U64},
2428 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2429 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2430 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2431 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2432 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2433 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2434 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2435 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2436 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2437 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2438 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2439 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2440 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2441 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2442 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2443 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2444 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2445 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2446 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2447 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2448 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2449 [XFRMA_PROTO] = { .type = NLA_U8 },
2450 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2451 [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 },
2454 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2455 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2456 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2459 static const struct xfrm_link {
2460 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2461 int (*start)(struct netlink_callback *);
2462 int (*dump)(struct sk_buff *, struct netlink_callback *);
2463 int (*done)(struct netlink_callback *);
2464 const struct nla_policy *nla_pol;
2466 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2467 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2468 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2469 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2470 .dump = xfrm_dump_sa,
2471 .done = xfrm_dump_sa_done },
2472 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2473 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2474 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2475 .start = xfrm_dump_policy_start,
2476 .dump = xfrm_dump_policy,
2477 .done = xfrm_dump_policy_done },
2478 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2479 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2480 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2481 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2482 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2483 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2484 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2485 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2486 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2487 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2488 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2489 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2490 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2491 .nla_pol = xfrma_spd_policy,
2492 .nla_max = XFRMA_SPD_MAX },
2493 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2496 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2498 struct net *net = sock_net(skb->sk);
2499 struct nlattr *attrs[XFRMA_MAX+1];
2500 const struct xfrm_link *link;
2503 #ifdef CONFIG_COMPAT
2504 if (is_compat_task())
2508 type = nlh->nlmsg_type;
2509 if (type > XFRM_MSG_MAX)
2512 type -= XFRM_MSG_BASE;
2513 link = &xfrm_dispatch[type];
2515 /* All operations require privileges, even GET */
2516 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2519 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2520 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2521 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2522 if (link->dump == NULL)
2526 struct netlink_dump_control c = {
2527 .start = link->start,
2531 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2535 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2536 link->nla_max ? : XFRMA_MAX,
2537 link->nla_pol ? : xfrma_policy);
2541 if (link->doit == NULL)
2544 return link->doit(skb, nlh, attrs);
2547 static void xfrm_netlink_rcv(struct sk_buff *skb)
2549 struct net *net = sock_net(skb->sk);
2551 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2552 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2553 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2556 static inline size_t xfrm_expire_msgsize(void)
2558 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2559 + nla_total_size(sizeof(struct xfrm_mark));
2562 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2564 struct xfrm_user_expire *ue;
2565 struct nlmsghdr *nlh;
2568 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2572 ue = nlmsg_data(nlh);
2573 copy_to_user_state(x, &ue->state);
2574 ue->hard = (c->data.hard != 0) ? 1 : 0;
2576 err = xfrm_mark_put(skb, &x->mark);
2580 nlmsg_end(skb, nlh);
2584 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2586 struct net *net = xs_net(x);
2587 struct sk_buff *skb;
2589 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2593 if (build_expire(skb, x, c) < 0) {
2598 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2601 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2603 struct net *net = xs_net(x);
2604 struct sk_buff *skb;
2606 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2610 if (build_aevent(skb, x, c) < 0)
2613 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2616 static int xfrm_notify_sa_flush(const struct km_event *c)
2618 struct net *net = c->net;
2619 struct xfrm_usersa_flush *p;
2620 struct nlmsghdr *nlh;
2621 struct sk_buff *skb;
2622 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2624 skb = nlmsg_new(len, GFP_ATOMIC);
2628 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2634 p = nlmsg_data(nlh);
2635 p->proto = c->data.proto;
2637 nlmsg_end(skb, nlh);
2639 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2642 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2646 l += nla_total_size(aead_len(x->aead));
2648 l += nla_total_size(sizeof(struct xfrm_algo) +
2649 (x->aalg->alg_key_len + 7) / 8);
2650 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2653 l += nla_total_size(xfrm_alg_len(x->ealg));
2655 l += nla_total_size(sizeof(*x->calg));
2657 l += nla_total_size(sizeof(*x->encap));
2659 l += nla_total_size(sizeof(x->tfcpad));
2661 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2663 l += nla_total_size(sizeof(struct xfrm_replay_state));
2665 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2666 x->security->ctx_len);
2668 l += nla_total_size(sizeof(*x->coaddr));
2669 if (x->props.extra_flags)
2670 l += nla_total_size(sizeof(x->props.extra_flags));
2671 if (x->props.output_mark)
2672 l += nla_total_size(sizeof(x->props.output_mark));
2674 /* Must count x->lastused as it may become non-zero behind our back. */
2675 l += nla_total_size(sizeof(u64));
2680 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2682 struct net *net = xs_net(x);
2683 struct xfrm_usersa_info *p;
2684 struct xfrm_usersa_id *id;
2685 struct nlmsghdr *nlh;
2686 struct sk_buff *skb;
2687 int len = xfrm_sa_len(x);
2690 headlen = sizeof(*p);
2691 if (c->event == XFRM_MSG_DELSA) {
2692 len += nla_total_size(headlen);
2693 headlen = sizeof(*id);
2694 len += nla_total_size(sizeof(struct xfrm_mark));
2696 len += NLMSG_ALIGN(headlen);
2698 skb = nlmsg_new(len, GFP_ATOMIC);
2702 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2707 p = nlmsg_data(nlh);
2708 if (c->event == XFRM_MSG_DELSA) {
2709 struct nlattr *attr;
2711 id = nlmsg_data(nlh);
2712 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2713 id->spi = x->id.spi;
2714 id->family = x->props.family;
2715 id->proto = x->id.proto;
2717 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2724 err = copy_to_user_state_extra(x, p, skb);
2728 nlmsg_end(skb, nlh);
2730 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2737 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2741 case XFRM_MSG_EXPIRE:
2742 return xfrm_exp_state_notify(x, c);
2743 case XFRM_MSG_NEWAE:
2744 return xfrm_aevent_state_notify(x, c);
2745 case XFRM_MSG_DELSA:
2746 case XFRM_MSG_UPDSA:
2747 case XFRM_MSG_NEWSA:
2748 return xfrm_notify_sa(x, c);
2749 case XFRM_MSG_FLUSHSA:
2750 return xfrm_notify_sa_flush(c);
2752 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2761 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2762 struct xfrm_policy *xp)
2764 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2765 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2766 + nla_total_size(sizeof(struct xfrm_mark))
2767 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2768 + userpolicy_type_attrsize();
2771 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2772 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2774 __u32 seq = xfrm_get_acqseq();
2775 struct xfrm_user_acquire *ua;
2776 struct nlmsghdr *nlh;
2779 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2783 ua = nlmsg_data(nlh);
2784 memcpy(&ua->id, &x->id, sizeof(ua->id));
2785 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2786 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2787 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2788 ua->aalgos = xt->aalgos;
2789 ua->ealgos = xt->ealgos;
2790 ua->calgos = xt->calgos;
2791 ua->seq = x->km.seq = seq;
2793 err = copy_to_user_tmpl(xp, skb);
2795 err = copy_to_user_state_sec_ctx(x, skb);
2797 err = copy_to_user_policy_type(xp->type, skb);
2799 err = xfrm_mark_put(skb, &xp->mark);
2801 nlmsg_cancel(skb, nlh);
2805 nlmsg_end(skb, nlh);
2809 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2810 struct xfrm_policy *xp)
2812 struct net *net = xs_net(x);
2813 struct sk_buff *skb;
2815 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2819 if (build_acquire(skb, x, xt, xp) < 0)
2822 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2825 /* User gives us xfrm_user_policy_info followed by an array of 0
2826 * or more templates.
2828 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2829 u8 *data, int len, int *dir)
2831 struct net *net = sock_net(sk);
2832 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2833 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2834 struct xfrm_policy *xp;
2837 switch (sk->sk_family) {
2839 if (opt != IP_XFRM_POLICY) {
2844 #if IS_ENABLED(CONFIG_IPV6)
2846 if (opt != IPV6_XFRM_POLICY) {
2859 if (len < sizeof(*p) ||
2860 verify_newpolicy_info(p))
2863 nr = ((len - sizeof(*p)) / sizeof(*ut));
2864 if (validate_tmpl(nr, ut, p->sel.family))
2867 if (p->dir > XFRM_POLICY_OUT)
2870 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2876 copy_from_user_policy(xp, p);
2877 xp->type = XFRM_POLICY_TYPE_MAIN;
2878 copy_templates(xp, ut, nr);
2885 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2887 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2888 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2889 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2890 + nla_total_size(sizeof(struct xfrm_mark))
2891 + userpolicy_type_attrsize();
2894 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2895 int dir, const struct km_event *c)
2897 struct xfrm_user_polexpire *upe;
2898 int hard = c->data.hard;
2899 struct nlmsghdr *nlh;
2902 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2906 upe = nlmsg_data(nlh);
2907 copy_to_user_policy(xp, &upe->pol, dir);
2908 err = copy_to_user_tmpl(xp, skb);
2910 err = copy_to_user_sec_ctx(xp, skb);
2912 err = copy_to_user_policy_type(xp->type, skb);
2914 err = xfrm_mark_put(skb, &xp->mark);
2916 nlmsg_cancel(skb, nlh);
2921 nlmsg_end(skb, nlh);
2925 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2927 struct net *net = xp_net(xp);
2928 struct sk_buff *skb;
2930 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2934 if (build_polexpire(skb, xp, dir, c) < 0)
2937 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2940 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2942 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2943 struct net *net = xp_net(xp);
2944 struct xfrm_userpolicy_info *p;
2945 struct xfrm_userpolicy_id *id;
2946 struct nlmsghdr *nlh;
2947 struct sk_buff *skb;
2950 headlen = sizeof(*p);
2951 if (c->event == XFRM_MSG_DELPOLICY) {
2952 len += nla_total_size(headlen);
2953 headlen = sizeof(*id);
2955 len += userpolicy_type_attrsize();
2956 len += nla_total_size(sizeof(struct xfrm_mark));
2957 len += NLMSG_ALIGN(headlen);
2959 skb = nlmsg_new(len, GFP_ATOMIC);
2963 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2968 p = nlmsg_data(nlh);
2969 if (c->event == XFRM_MSG_DELPOLICY) {
2970 struct nlattr *attr;
2972 id = nlmsg_data(nlh);
2973 memset(id, 0, sizeof(*id));
2976 id->index = xp->index;
2978 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2980 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2988 copy_to_user_policy(xp, p, dir);
2989 err = copy_to_user_tmpl(xp, skb);
2991 err = copy_to_user_policy_type(xp->type, skb);
2993 err = xfrm_mark_put(skb, &xp->mark);
2997 nlmsg_end(skb, nlh);
2999 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3006 static int xfrm_notify_policy_flush(const struct km_event *c)
3008 struct net *net = c->net;
3009 struct nlmsghdr *nlh;
3010 struct sk_buff *skb;
3013 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3017 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3021 err = copy_to_user_policy_type(c->data.type, skb);
3025 nlmsg_end(skb, nlh);
3027 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3034 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3038 err = verify_policy_dir(dir);
3043 case XFRM_MSG_NEWPOLICY:
3044 case XFRM_MSG_UPDPOLICY:
3045 case XFRM_MSG_DELPOLICY:
3046 return xfrm_notify_policy(xp, dir, c);
3047 case XFRM_MSG_FLUSHPOLICY:
3048 return xfrm_notify_policy_flush(c);
3049 case XFRM_MSG_POLEXPIRE:
3050 return xfrm_exp_policy_notify(xp, dir, c);
3052 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3060 static inline size_t xfrm_report_msgsize(void)
3062 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3065 static int build_report(struct sk_buff *skb, u8 proto,
3066 struct xfrm_selector *sel, xfrm_address_t *addr)
3068 struct xfrm_user_report *ur;
3069 struct nlmsghdr *nlh;
3071 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3075 ur = nlmsg_data(nlh);
3077 memcpy(&ur->sel, sel, sizeof(ur->sel));
3080 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3082 nlmsg_cancel(skb, nlh);
3086 nlmsg_end(skb, nlh);
3090 static int xfrm_send_report(struct net *net, u8 proto,
3091 struct xfrm_selector *sel, xfrm_address_t *addr)
3093 struct sk_buff *skb;
3095 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3099 if (build_report(skb, proto, sel, addr) < 0)
3102 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3105 static inline size_t xfrm_mapping_msgsize(void)
3107 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3110 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3111 xfrm_address_t *new_saddr, __be16 new_sport)
3113 struct xfrm_user_mapping *um;
3114 struct nlmsghdr *nlh;
3116 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3120 um = nlmsg_data(nlh);
3122 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3123 um->id.spi = x->id.spi;
3124 um->id.family = x->props.family;
3125 um->id.proto = x->id.proto;
3126 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3127 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3128 um->new_sport = new_sport;
3129 um->old_sport = x->encap->encap_sport;
3130 um->reqid = x->props.reqid;
3132 nlmsg_end(skb, nlh);
3136 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3139 struct net *net = xs_net(x);
3140 struct sk_buff *skb;
3142 if (x->id.proto != IPPROTO_ESP)
3148 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3152 if (build_mapping(skb, x, ipaddr, sport) < 0)
3155 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3158 static bool xfrm_is_alive(const struct km_event *c)
3160 return (bool)xfrm_acquire_is_on(c->net);
3163 static struct xfrm_mgr netlink_mgr = {
3165 .notify = xfrm_send_state_notify,
3166 .acquire = xfrm_send_acquire,
3167 .compile_policy = xfrm_compile_policy,
3168 .notify_policy = xfrm_send_policy_notify,
3169 .report = xfrm_send_report,
3170 .migrate = xfrm_send_migrate,
3171 .new_mapping = xfrm_send_mapping,
3172 .is_alive = xfrm_is_alive,
3175 static int __net_init xfrm_user_net_init(struct net *net)
3178 struct netlink_kernel_cfg cfg = {
3179 .groups = XFRMNLGRP_MAX,
3180 .input = xfrm_netlink_rcv,
3183 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3186 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3187 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3191 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3194 list_for_each_entry(net, net_exit_list, exit_list)
3195 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3197 list_for_each_entry(net, net_exit_list, exit_list)
3198 netlink_kernel_release(net->xfrm.nlsk_stash);
3201 static struct pernet_operations xfrm_user_net_ops = {
3202 .init = xfrm_user_net_init,
3203 .exit_batch = xfrm_user_net_exit,
3206 static int __init xfrm_user_init(void)
3210 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3212 rv = register_pernet_subsys(&xfrm_user_net_ops);
3215 rv = xfrm_register_km(&netlink_mgr);
3217 unregister_pernet_subsys(&xfrm_user_net_ops);
3221 static void __exit xfrm_user_exit(void)
3223 xfrm_unregister_km(&netlink_mgr);
3224 unregister_pernet_subsys(&xfrm_user_net_ops);
3227 module_init(xfrm_user_init);
3228 module_exit(xfrm_user_exit);
3229 MODULE_LICENSE("GPL");
3230 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);