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);
996 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
999 static inline size_t xfrm_spdinfo_msgsize(void)
1001 return NLMSG_ALIGN(4)
1002 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1003 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1004 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1005 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1008 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1009 u32 portid, u32 seq, u32 flags)
1011 struct xfrmk_spdinfo si;
1012 struct xfrmu_spdinfo spc;
1013 struct xfrmu_spdhinfo sph;
1014 struct xfrmu_spdhthresh spt4, spt6;
1015 struct nlmsghdr *nlh;
1020 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1021 if (nlh == NULL) /* shouldn't really happen ... */
1024 f = nlmsg_data(nlh);
1026 xfrm_spd_getinfo(net, &si);
1027 spc.incnt = si.incnt;
1028 spc.outcnt = si.outcnt;
1029 spc.fwdcnt = si.fwdcnt;
1030 spc.inscnt = si.inscnt;
1031 spc.outscnt = si.outscnt;
1032 spc.fwdscnt = si.fwdscnt;
1033 sph.spdhcnt = si.spdhcnt;
1034 sph.spdhmcnt = si.spdhmcnt;
1037 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1039 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1040 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1041 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1042 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1043 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1045 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1047 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1049 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1051 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1053 nlmsg_cancel(skb, nlh);
1057 nlmsg_end(skb, nlh);
1061 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1062 struct nlattr **attrs)
1064 struct net *net = sock_net(skb->sk);
1065 struct xfrmu_spdhthresh *thresh4 = NULL;
1066 struct xfrmu_spdhthresh *thresh6 = NULL;
1068 /* selector prefixlen thresholds to hash policies */
1069 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1070 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1072 if (nla_len(rta) < sizeof(*thresh4))
1074 thresh4 = nla_data(rta);
1075 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1078 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1079 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1081 if (nla_len(rta) < sizeof(*thresh6))
1083 thresh6 = nla_data(rta);
1084 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1088 if (thresh4 || thresh6) {
1089 write_seqlock(&net->xfrm.policy_hthresh.lock);
1091 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1092 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1095 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1096 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1098 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1100 xfrm_policy_hash_rebuild(net);
1106 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1107 struct nlattr **attrs)
1109 struct net *net = sock_net(skb->sk);
1110 struct sk_buff *r_skb;
1111 u32 *flags = nlmsg_data(nlh);
1112 u32 sportid = NETLINK_CB(skb).portid;
1113 u32 seq = nlh->nlmsg_seq;
1115 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1119 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1122 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1125 static inline size_t xfrm_sadinfo_msgsize(void)
1127 return NLMSG_ALIGN(4)
1128 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1129 + nla_total_size(4); /* XFRMA_SAD_CNT */
1132 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1133 u32 portid, u32 seq, u32 flags)
1135 struct xfrmk_sadinfo si;
1136 struct xfrmu_sadhinfo sh;
1137 struct nlmsghdr *nlh;
1141 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1142 if (nlh == NULL) /* shouldn't really happen ... */
1145 f = nlmsg_data(nlh);
1147 xfrm_sad_getinfo(net, &si);
1149 sh.sadhmcnt = si.sadhmcnt;
1150 sh.sadhcnt = si.sadhcnt;
1152 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1154 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1156 nlmsg_cancel(skb, nlh);
1160 nlmsg_end(skb, nlh);
1164 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1165 struct nlattr **attrs)
1167 struct net *net = sock_net(skb->sk);
1168 struct sk_buff *r_skb;
1169 u32 *flags = nlmsg_data(nlh);
1170 u32 sportid = NETLINK_CB(skb).portid;
1171 u32 seq = nlh->nlmsg_seq;
1173 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1177 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1180 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1183 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1184 struct nlattr **attrs)
1186 struct net *net = sock_net(skb->sk);
1187 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1188 struct xfrm_state *x;
1189 struct sk_buff *resp_skb;
1192 x = xfrm_user_state_lookup(net, p, attrs, &err);
1196 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1197 if (IS_ERR(resp_skb)) {
1198 err = PTR_ERR(resp_skb);
1200 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1207 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1208 struct nlattr **attrs)
1210 struct net *net = sock_net(skb->sk);
1211 struct xfrm_state *x;
1212 struct xfrm_userspi_info *p;
1213 struct sk_buff *resp_skb;
1214 xfrm_address_t *daddr;
1220 p = nlmsg_data(nlh);
1221 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1225 family = p->info.family;
1226 daddr = &p->info.id.daddr;
1230 mark = xfrm_mark_get(attrs, &m);
1232 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1233 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1240 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1241 p->info.id.proto, daddr,
1248 err = xfrm_alloc_spi(x, p->min, p->max);
1252 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1253 if (IS_ERR(resp_skb)) {
1254 err = PTR_ERR(resp_skb);
1258 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1266 static int verify_policy_dir(u8 dir)
1269 case XFRM_POLICY_IN:
1270 case XFRM_POLICY_OUT:
1271 case XFRM_POLICY_FWD:
1281 static int verify_policy_type(u8 type)
1284 case XFRM_POLICY_TYPE_MAIN:
1285 #ifdef CONFIG_XFRM_SUB_POLICY
1286 case XFRM_POLICY_TYPE_SUB:
1297 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1302 case XFRM_SHARE_ANY:
1303 case XFRM_SHARE_SESSION:
1304 case XFRM_SHARE_USER:
1305 case XFRM_SHARE_UNIQUE:
1312 switch (p->action) {
1313 case XFRM_POLICY_ALLOW:
1314 case XFRM_POLICY_BLOCK:
1321 switch (p->sel.family) {
1326 #if IS_ENABLED(CONFIG_IPV6)
1329 return -EAFNOSUPPORT;
1336 ret = verify_policy_dir(p->dir);
1339 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1345 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1347 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1348 struct xfrm_user_sec_ctx *uctx;
1353 uctx = nla_data(rt);
1354 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1357 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1363 for (i = 0; i < nr; i++, ut++) {
1364 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1366 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1367 memcpy(&t->saddr, &ut->saddr,
1368 sizeof(xfrm_address_t));
1369 t->reqid = ut->reqid;
1371 t->share = ut->share;
1372 t->optional = ut->optional;
1373 t->aalgos = ut->aalgos;
1374 t->ealgos = ut->ealgos;
1375 t->calgos = ut->calgos;
1376 /* If all masks are ~0, then we allow all algorithms. */
1377 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1378 t->encap_family = ut->family;
1382 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1387 if (nr > XFRM_MAX_DEPTH)
1390 prev_family = family;
1392 for (i = 0; i < nr; i++) {
1393 /* We never validated the ut->family value, so many
1394 * applications simply leave it at zero. The check was
1395 * never made and ut->family was ignored because all
1396 * templates could be assumed to have the same family as
1397 * the policy itself. Now that we will have ipv4-in-ipv6
1398 * and ipv6-in-ipv4 tunnels, this is no longer true.
1401 ut[i].family = family;
1403 if ((ut[i].mode == XFRM_MODE_TRANSPORT) &&
1404 (ut[i].family != prev_family))
1407 prev_family = ut[i].family;
1409 switch (ut[i].family) {
1412 #if IS_ENABLED(CONFIG_IPV6)
1420 switch (ut[i].id.proto) {
1424 #if IS_ENABLED(CONFIG_IPV6)
1425 case IPPROTO_ROUTING:
1426 case IPPROTO_DSTOPTS:
1428 case IPSEC_PROTO_ANY:
1439 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1441 struct nlattr *rt = attrs[XFRMA_TMPL];
1446 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1447 int nr = nla_len(rt) / sizeof(*utmpl);
1450 err = validate_tmpl(nr, utmpl, pol->family);
1454 copy_templates(pol, utmpl, nr);
1459 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1461 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1462 struct xfrm_userpolicy_type *upt;
1463 u8 type = XFRM_POLICY_TYPE_MAIN;
1471 err = verify_policy_type(type);
1479 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1481 xp->priority = p->priority;
1482 xp->index = p->index;
1483 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1484 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1485 xp->action = p->action;
1486 xp->flags = p->flags;
1487 xp->family = p->sel.family;
1488 /* XXX xp->share = p->share; */
1491 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1493 memset(p, 0, sizeof(*p));
1494 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1495 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1496 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1497 p->priority = xp->priority;
1498 p->index = xp->index;
1499 p->sel.family = xp->family;
1501 p->action = xp->action;
1502 p->flags = xp->flags;
1503 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1506 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1508 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1516 copy_from_user_policy(xp, p);
1518 err = copy_from_user_policy_type(&xp->type, attrs);
1522 if (!(err = copy_from_user_tmpl(xp, attrs)))
1523 err = copy_from_user_sec_ctx(xp, attrs);
1527 xfrm_mark_get(attrs, &xp->mark);
1533 xfrm_policy_destroy(xp);
1537 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1538 struct nlattr **attrs)
1540 struct net *net = sock_net(skb->sk);
1541 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1542 struct xfrm_policy *xp;
1547 err = verify_newpolicy_info(p);
1550 err = verify_sec_ctx_len(attrs);
1554 xp = xfrm_policy_construct(net, p, attrs, &err);
1558 /* shouldn't excl be based on nlh flags??
1559 * Aha! this is anti-netlink really i.e more pfkey derived
1560 * in netlink excl is a flag and you wouldnt need
1561 * a type XFRM_MSG_UPDPOLICY - JHS */
1562 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1563 err = xfrm_policy_insert(p->dir, xp, excl);
1564 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1567 security_xfrm_policy_free(xp->security);
1572 c.event = nlh->nlmsg_type;
1573 c.seq = nlh->nlmsg_seq;
1574 c.portid = nlh->nlmsg_pid;
1575 km_policy_notify(xp, p->dir, &c);
1582 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1584 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1587 if (xp->xfrm_nr == 0)
1590 for (i = 0; i < xp->xfrm_nr; i++) {
1591 struct xfrm_user_tmpl *up = &vec[i];
1592 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1594 memset(up, 0, sizeof(*up));
1595 memcpy(&up->id, &kp->id, sizeof(up->id));
1596 up->family = kp->encap_family;
1597 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1598 up->reqid = kp->reqid;
1599 up->mode = kp->mode;
1600 up->share = kp->share;
1601 up->optional = kp->optional;
1602 up->aalgos = kp->aalgos;
1603 up->ealgos = kp->ealgos;
1604 up->calgos = kp->calgos;
1607 return nla_put(skb, XFRMA_TMPL,
1608 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1611 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1614 return copy_sec_ctx(x->security, skb);
1619 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1622 return copy_sec_ctx(xp->security, skb);
1625 static inline size_t userpolicy_type_attrsize(void)
1627 #ifdef CONFIG_XFRM_SUB_POLICY
1628 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1634 #ifdef CONFIG_XFRM_SUB_POLICY
1635 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1637 struct xfrm_userpolicy_type upt;
1639 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1640 memset(&upt, 0, sizeof(upt));
1643 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1647 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1653 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1655 struct xfrm_dump_info *sp = ptr;
1656 struct xfrm_userpolicy_info *p;
1657 struct sk_buff *in_skb = sp->in_skb;
1658 struct sk_buff *skb = sp->out_skb;
1659 struct nlmsghdr *nlh;
1662 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1663 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1667 p = nlmsg_data(nlh);
1668 copy_to_user_policy(xp, p, dir);
1669 err = copy_to_user_tmpl(xp, skb);
1671 err = copy_to_user_sec_ctx(xp, skb);
1673 err = copy_to_user_policy_type(xp->type, skb);
1675 err = xfrm_mark_put(skb, &xp->mark);
1677 nlmsg_cancel(skb, nlh);
1680 nlmsg_end(skb, nlh);
1684 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1686 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1687 struct net *net = sock_net(cb->skb->sk);
1689 xfrm_policy_walk_done(walk, net);
1693 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1695 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1697 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1699 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1703 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1705 struct net *net = sock_net(skb->sk);
1706 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1707 struct xfrm_dump_info info;
1709 info.in_skb = cb->skb;
1711 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1712 info.nlmsg_flags = NLM_F_MULTI;
1714 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1719 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1720 struct xfrm_policy *xp,
1723 struct xfrm_dump_info info;
1724 struct sk_buff *skb;
1727 err = verify_policy_dir(dir);
1729 return ERR_PTR(err);
1731 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1733 return ERR_PTR(-ENOMEM);
1735 info.in_skb = in_skb;
1737 info.nlmsg_seq = seq;
1738 info.nlmsg_flags = 0;
1740 err = dump_one_policy(xp, dir, 0, &info);
1743 return ERR_PTR(err);
1749 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1750 struct nlattr **attrs)
1752 struct net *net = sock_net(skb->sk);
1753 struct xfrm_policy *xp;
1754 struct xfrm_userpolicy_id *p;
1755 u8 type = XFRM_POLICY_TYPE_MAIN;
1760 u32 mark = xfrm_mark_get(attrs, &m);
1762 p = nlmsg_data(nlh);
1763 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1765 err = copy_from_user_policy_type(&type, attrs);
1769 err = verify_policy_dir(p->dir);
1774 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1776 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1777 struct xfrm_sec_ctx *ctx;
1779 err = verify_sec_ctx_len(attrs);
1785 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1787 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1791 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1793 security_xfrm_policy_free(ctx);
1799 struct sk_buff *resp_skb;
1801 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1802 if (IS_ERR(resp_skb)) {
1803 err = PTR_ERR(resp_skb);
1805 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1806 NETLINK_CB(skb).portid);
1809 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1814 c.data.byid = p->index;
1815 c.event = nlh->nlmsg_type;
1816 c.seq = nlh->nlmsg_seq;
1817 c.portid = nlh->nlmsg_pid;
1818 km_policy_notify(xp, p->dir, &c);
1823 if (delete && err == 0)
1824 xfrm_garbage_collect(net);
1828 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1829 struct nlattr **attrs)
1831 struct net *net = sock_net(skb->sk);
1833 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1836 err = xfrm_state_flush(net, p->proto, true);
1838 if (err == -ESRCH) /* empty table */
1842 c.data.proto = p->proto;
1843 c.event = nlh->nlmsg_type;
1844 c.seq = nlh->nlmsg_seq;
1845 c.portid = nlh->nlmsg_pid;
1847 km_state_notify(NULL, &c);
1852 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1854 size_t replay_size = x->replay_esn ?
1855 xfrm_replay_state_esn_len(x->replay_esn) :
1856 sizeof(struct xfrm_replay_state);
1858 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1859 + nla_total_size(replay_size)
1860 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1861 + nla_total_size(sizeof(struct xfrm_mark))
1862 + nla_total_size(4) /* XFRM_AE_RTHR */
1863 + nla_total_size(4); /* XFRM_AE_ETHR */
1866 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1868 struct xfrm_aevent_id *id;
1869 struct nlmsghdr *nlh;
1872 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1876 id = nlmsg_data(nlh);
1877 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1878 id->sa_id.spi = x->id.spi;
1879 id->sa_id.family = x->props.family;
1880 id->sa_id.proto = x->id.proto;
1881 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1882 id->reqid = x->props.reqid;
1883 id->flags = c->data.aevent;
1885 if (x->replay_esn) {
1886 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1887 xfrm_replay_state_esn_len(x->replay_esn),
1890 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1895 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1899 if (id->flags & XFRM_AE_RTHR) {
1900 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1904 if (id->flags & XFRM_AE_ETHR) {
1905 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1906 x->replay_maxage * 10 / HZ);
1910 err = xfrm_mark_put(skb, &x->mark);
1914 nlmsg_end(skb, nlh);
1918 nlmsg_cancel(skb, nlh);
1922 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1923 struct nlattr **attrs)
1925 struct net *net = sock_net(skb->sk);
1926 struct xfrm_state *x;
1927 struct sk_buff *r_skb;
1932 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1933 struct xfrm_usersa_id *id = &p->sa_id;
1935 mark = xfrm_mark_get(attrs, &m);
1937 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1941 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1942 if (r_skb == NULL) {
1948 * XXX: is this lock really needed - none of the other
1949 * gets lock (the concern is things getting updated
1950 * while we are still reading) - jhs
1952 spin_lock_bh(&x->lock);
1953 c.data.aevent = p->flags;
1954 c.seq = nlh->nlmsg_seq;
1955 c.portid = nlh->nlmsg_pid;
1957 if (build_aevent(r_skb, x, &c) < 0)
1959 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1960 spin_unlock_bh(&x->lock);
1965 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1966 struct nlattr **attrs)
1968 struct net *net = sock_net(skb->sk);
1969 struct xfrm_state *x;
1974 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1975 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1976 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1977 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1978 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1979 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
1981 if (!lt && !rp && !re && !et && !rt)
1984 /* pedantic mode - thou shalt sayeth replaceth */
1985 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1988 mark = xfrm_mark_get(attrs, &m);
1990 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1994 if (x->km.state != XFRM_STATE_VALID)
1997 err = xfrm_replay_verify_len(x->replay_esn, re);
2001 spin_lock_bh(&x->lock);
2002 xfrm_update_ae_params(x, attrs, 1);
2003 spin_unlock_bh(&x->lock);
2005 c.event = nlh->nlmsg_type;
2006 c.seq = nlh->nlmsg_seq;
2007 c.portid = nlh->nlmsg_pid;
2008 c.data.aevent = XFRM_AE_CU;
2009 km_state_notify(x, &c);
2016 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2017 struct nlattr **attrs)
2019 struct net *net = sock_net(skb->sk);
2021 u8 type = XFRM_POLICY_TYPE_MAIN;
2024 err = copy_from_user_policy_type(&type, attrs);
2028 err = xfrm_policy_flush(net, type, true);
2030 if (err == -ESRCH) /* empty table */
2036 c.event = nlh->nlmsg_type;
2037 c.seq = nlh->nlmsg_seq;
2038 c.portid = nlh->nlmsg_pid;
2040 km_policy_notify(NULL, 0, &c);
2044 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2045 struct nlattr **attrs)
2047 struct net *net = sock_net(skb->sk);
2048 struct xfrm_policy *xp;
2049 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2050 struct xfrm_userpolicy_info *p = &up->pol;
2051 u8 type = XFRM_POLICY_TYPE_MAIN;
2054 u32 mark = xfrm_mark_get(attrs, &m);
2056 err = copy_from_user_policy_type(&type, attrs);
2060 err = verify_policy_dir(p->dir);
2065 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
2067 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2068 struct xfrm_sec_ctx *ctx;
2070 err = verify_sec_ctx_len(attrs);
2076 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2078 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2082 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
2083 &p->sel, ctx, 0, &err);
2084 security_xfrm_policy_free(ctx);
2089 if (unlikely(xp->walk.dead))
2094 xfrm_policy_delete(xp, p->dir);
2095 xfrm_audit_policy_delete(xp, 1, true);
2097 // reset the timers here?
2098 WARN(1, "Don't know what to do with soft policy expire\n");
2100 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2107 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2108 struct nlattr **attrs)
2110 struct net *net = sock_net(skb->sk);
2111 struct xfrm_state *x;
2113 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2114 struct xfrm_usersa_info *p = &ue->state;
2116 u32 mark = xfrm_mark_get(attrs, &m);
2118 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2124 spin_lock_bh(&x->lock);
2126 if (x->km.state != XFRM_STATE_VALID)
2128 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2131 __xfrm_state_delete(x);
2132 xfrm_audit_state_delete(x, 1, true);
2136 spin_unlock_bh(&x->lock);
2141 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2142 struct nlattr **attrs)
2144 struct net *net = sock_net(skb->sk);
2145 struct xfrm_policy *xp;
2146 struct xfrm_user_tmpl *ut;
2148 struct nlattr *rt = attrs[XFRMA_TMPL];
2149 struct xfrm_mark mark;
2151 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2152 struct xfrm_state *x = xfrm_state_alloc(net);
2158 xfrm_mark_get(attrs, &mark);
2160 err = verify_newpolicy_info(&ua->policy);
2165 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2169 memcpy(&x->id, &ua->id, sizeof(ua->id));
2170 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2171 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2172 xp->mark.m = x->mark.m = mark.m;
2173 xp->mark.v = x->mark.v = mark.v;
2175 /* extract the templates and for each call km_key */
2176 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2177 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2178 memcpy(&x->id, &t->id, sizeof(x->id));
2179 x->props.mode = t->mode;
2180 x->props.reqid = t->reqid;
2181 x->props.family = ut->family;
2182 t->aalgos = ua->aalgos;
2183 t->ealgos = ua->ealgos;
2184 t->calgos = ua->calgos;
2185 err = km_query(x, t, xp);
2195 WARN(1, "BAD policy passed\n");
2202 #ifdef CONFIG_XFRM_MIGRATE
2203 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2204 struct xfrm_kmaddress *k,
2205 struct nlattr **attrs, int *num)
2207 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2208 struct xfrm_user_migrate *um;
2212 struct xfrm_user_kmaddress *uk;
2214 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2215 memcpy(&k->local, &uk->local, sizeof(k->local));
2216 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2217 k->family = uk->family;
2218 k->reserved = uk->reserved;
2222 num_migrate = nla_len(rt) / sizeof(*um);
2224 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2227 for (i = 0; i < num_migrate; i++, um++, ma++) {
2228 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2229 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2230 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2231 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2233 ma->proto = um->proto;
2234 ma->mode = um->mode;
2235 ma->reqid = um->reqid;
2237 ma->old_family = um->old_family;
2238 ma->new_family = um->new_family;
2245 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2246 struct nlattr **attrs)
2248 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2249 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2250 struct xfrm_kmaddress km, *kmp;
2254 struct net *net = sock_net(skb->sk);
2256 err = verify_policy_dir(pi->dir);
2260 if (attrs[XFRMA_MIGRATE] == NULL)
2263 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2265 err = copy_from_user_policy_type(&type, attrs);
2269 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2276 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2281 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2282 struct nlattr **attrs)
2284 return -ENOPROTOOPT;
2288 #ifdef CONFIG_XFRM_MIGRATE
2289 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2291 struct xfrm_user_migrate um;
2293 memset(&um, 0, sizeof(um));
2294 um.proto = m->proto;
2296 um.reqid = m->reqid;
2297 um.old_family = m->old_family;
2298 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2299 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2300 um.new_family = m->new_family;
2301 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2302 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2304 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2307 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2309 struct xfrm_user_kmaddress uk;
2311 memset(&uk, 0, sizeof(uk));
2312 uk.family = k->family;
2313 uk.reserved = k->reserved;
2314 memcpy(&uk.local, &k->local, sizeof(uk.local));
2315 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2317 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2320 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2322 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2323 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2324 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2325 + userpolicy_type_attrsize();
2328 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2329 int num_migrate, const struct xfrm_kmaddress *k,
2330 const struct xfrm_selector *sel, u8 dir, u8 type)
2332 const struct xfrm_migrate *mp;
2333 struct xfrm_userpolicy_id *pol_id;
2334 struct nlmsghdr *nlh;
2337 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2341 pol_id = nlmsg_data(nlh);
2342 /* copy data from selector, dir, and type to the pol_id */
2343 memset(pol_id, 0, sizeof(*pol_id));
2344 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2348 err = copy_to_user_kmaddress(k, skb);
2352 err = copy_to_user_policy_type(type, skb);
2355 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2356 err = copy_to_user_migrate(mp, skb);
2361 nlmsg_end(skb, nlh);
2365 nlmsg_cancel(skb, nlh);
2369 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2370 const struct xfrm_migrate *m, int num_migrate,
2371 const struct xfrm_kmaddress *k)
2373 struct net *net = &init_net;
2374 struct sk_buff *skb;
2377 err = verify_policy_dir(dir);
2381 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2386 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2389 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2392 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2393 const struct xfrm_migrate *m, int num_migrate,
2394 const struct xfrm_kmaddress *k)
2396 return -ENOPROTOOPT;
2400 #define XMSGSIZE(type) sizeof(struct type)
2402 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2403 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2404 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2405 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2406 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2407 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2408 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2409 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2410 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2411 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2412 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2413 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2414 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2415 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2416 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2417 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2418 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2419 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2420 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2421 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2422 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2423 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2428 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2429 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2430 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2431 [XFRMA_LASTUSED] = { .type = NLA_U64},
2432 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2433 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2434 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2435 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2436 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2437 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2438 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2439 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2440 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2441 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2442 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2443 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2444 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2445 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2446 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2447 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2448 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2449 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2450 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2451 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2452 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2453 [XFRMA_PROTO] = { .type = NLA_U8 },
2454 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2455 [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 },
2458 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2459 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2460 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2463 static const struct xfrm_link {
2464 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2465 int (*start)(struct netlink_callback *);
2466 int (*dump)(struct sk_buff *, struct netlink_callback *);
2467 int (*done)(struct netlink_callback *);
2468 const struct nla_policy *nla_pol;
2470 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2471 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2472 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2473 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2474 .dump = xfrm_dump_sa,
2475 .done = xfrm_dump_sa_done },
2476 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2477 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2478 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2479 .start = xfrm_dump_policy_start,
2480 .dump = xfrm_dump_policy,
2481 .done = xfrm_dump_policy_done },
2482 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2483 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2484 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2485 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2486 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2487 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2488 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2489 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2490 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2491 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2492 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2493 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2494 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2495 .nla_pol = xfrma_spd_policy,
2496 .nla_max = XFRMA_SPD_MAX },
2497 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2500 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2502 struct net *net = sock_net(skb->sk);
2503 struct nlattr *attrs[XFRMA_MAX+1];
2504 const struct xfrm_link *link;
2507 #ifdef CONFIG_COMPAT
2508 if (is_compat_task())
2512 type = nlh->nlmsg_type;
2513 if (type > XFRM_MSG_MAX)
2516 type -= XFRM_MSG_BASE;
2517 link = &xfrm_dispatch[type];
2519 /* All operations require privileges, even GET */
2520 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2523 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2524 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2525 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2526 if (link->dump == NULL)
2530 struct netlink_dump_control c = {
2531 .start = link->start,
2535 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2539 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2540 link->nla_max ? : XFRMA_MAX,
2541 link->nla_pol ? : xfrma_policy);
2545 if (link->doit == NULL)
2548 return link->doit(skb, nlh, attrs);
2551 static void xfrm_netlink_rcv(struct sk_buff *skb)
2553 struct net *net = sock_net(skb->sk);
2555 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2556 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2557 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2560 static inline size_t xfrm_expire_msgsize(void)
2562 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2563 + nla_total_size(sizeof(struct xfrm_mark));
2566 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2568 struct xfrm_user_expire *ue;
2569 struct nlmsghdr *nlh;
2572 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2576 ue = nlmsg_data(nlh);
2577 copy_to_user_state(x, &ue->state);
2578 ue->hard = (c->data.hard != 0) ? 1 : 0;
2580 err = xfrm_mark_put(skb, &x->mark);
2584 nlmsg_end(skb, nlh);
2588 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2590 struct net *net = xs_net(x);
2591 struct sk_buff *skb;
2593 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2597 if (build_expire(skb, x, c) < 0) {
2602 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2605 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2607 struct net *net = xs_net(x);
2608 struct sk_buff *skb;
2610 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2614 if (build_aevent(skb, x, c) < 0)
2617 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2620 static int xfrm_notify_sa_flush(const struct km_event *c)
2622 struct net *net = c->net;
2623 struct xfrm_usersa_flush *p;
2624 struct nlmsghdr *nlh;
2625 struct sk_buff *skb;
2626 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2628 skb = nlmsg_new(len, GFP_ATOMIC);
2632 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2638 p = nlmsg_data(nlh);
2639 p->proto = c->data.proto;
2641 nlmsg_end(skb, nlh);
2643 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2646 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2650 l += nla_total_size(aead_len(x->aead));
2652 l += nla_total_size(sizeof(struct xfrm_algo) +
2653 (x->aalg->alg_key_len + 7) / 8);
2654 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2657 l += nla_total_size(xfrm_alg_len(x->ealg));
2659 l += nla_total_size(sizeof(*x->calg));
2661 l += nla_total_size(sizeof(*x->encap));
2663 l += nla_total_size(sizeof(x->tfcpad));
2665 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2667 l += nla_total_size(sizeof(struct xfrm_replay_state));
2669 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2670 x->security->ctx_len);
2672 l += nla_total_size(sizeof(*x->coaddr));
2673 if (x->props.extra_flags)
2674 l += nla_total_size(sizeof(x->props.extra_flags));
2675 if (x->props.output_mark)
2676 l += nla_total_size(sizeof(x->props.output_mark));
2678 /* Must count x->lastused as it may become non-zero behind our back. */
2679 l += nla_total_size(sizeof(u64));
2684 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2686 struct net *net = xs_net(x);
2687 struct xfrm_usersa_info *p;
2688 struct xfrm_usersa_id *id;
2689 struct nlmsghdr *nlh;
2690 struct sk_buff *skb;
2691 int len = xfrm_sa_len(x);
2694 headlen = sizeof(*p);
2695 if (c->event == XFRM_MSG_DELSA) {
2696 len += nla_total_size(headlen);
2697 headlen = sizeof(*id);
2698 len += nla_total_size(sizeof(struct xfrm_mark));
2700 len += NLMSG_ALIGN(headlen);
2702 skb = nlmsg_new(len, GFP_ATOMIC);
2706 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2711 p = nlmsg_data(nlh);
2712 if (c->event == XFRM_MSG_DELSA) {
2713 struct nlattr *attr;
2715 id = nlmsg_data(nlh);
2716 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2717 id->spi = x->id.spi;
2718 id->family = x->props.family;
2719 id->proto = x->id.proto;
2721 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2728 err = copy_to_user_state_extra(x, p, skb);
2732 nlmsg_end(skb, nlh);
2734 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2741 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2745 case XFRM_MSG_EXPIRE:
2746 return xfrm_exp_state_notify(x, c);
2747 case XFRM_MSG_NEWAE:
2748 return xfrm_aevent_state_notify(x, c);
2749 case XFRM_MSG_DELSA:
2750 case XFRM_MSG_UPDSA:
2751 case XFRM_MSG_NEWSA:
2752 return xfrm_notify_sa(x, c);
2753 case XFRM_MSG_FLUSHSA:
2754 return xfrm_notify_sa_flush(c);
2756 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2765 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2766 struct xfrm_policy *xp)
2768 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2769 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2770 + nla_total_size(sizeof(struct xfrm_mark))
2771 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2772 + userpolicy_type_attrsize();
2775 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2776 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2778 __u32 seq = xfrm_get_acqseq();
2779 struct xfrm_user_acquire *ua;
2780 struct nlmsghdr *nlh;
2783 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2787 ua = nlmsg_data(nlh);
2788 memcpy(&ua->id, &x->id, sizeof(ua->id));
2789 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2790 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2791 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2792 ua->aalgos = xt->aalgos;
2793 ua->ealgos = xt->ealgos;
2794 ua->calgos = xt->calgos;
2795 ua->seq = x->km.seq = seq;
2797 err = copy_to_user_tmpl(xp, skb);
2799 err = copy_to_user_state_sec_ctx(x, skb);
2801 err = copy_to_user_policy_type(xp->type, skb);
2803 err = xfrm_mark_put(skb, &xp->mark);
2805 nlmsg_cancel(skb, nlh);
2809 nlmsg_end(skb, nlh);
2813 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2814 struct xfrm_policy *xp)
2816 struct net *net = xs_net(x);
2817 struct sk_buff *skb;
2819 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2823 if (build_acquire(skb, x, xt, xp) < 0)
2826 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2829 /* User gives us xfrm_user_policy_info followed by an array of 0
2830 * or more templates.
2832 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2833 u8 *data, int len, int *dir)
2835 struct net *net = sock_net(sk);
2836 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2837 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2838 struct xfrm_policy *xp;
2841 switch (sk->sk_family) {
2843 if (opt != IP_XFRM_POLICY) {
2848 #if IS_ENABLED(CONFIG_IPV6)
2850 if (opt != IPV6_XFRM_POLICY) {
2863 if (len < sizeof(*p) ||
2864 verify_newpolicy_info(p))
2867 nr = ((len - sizeof(*p)) / sizeof(*ut));
2868 if (validate_tmpl(nr, ut, p->sel.family))
2871 if (p->dir > XFRM_POLICY_OUT)
2874 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2880 copy_from_user_policy(xp, p);
2881 xp->type = XFRM_POLICY_TYPE_MAIN;
2882 copy_templates(xp, ut, nr);
2889 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2891 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2892 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2893 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2894 + nla_total_size(sizeof(struct xfrm_mark))
2895 + userpolicy_type_attrsize();
2898 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2899 int dir, const struct km_event *c)
2901 struct xfrm_user_polexpire *upe;
2902 int hard = c->data.hard;
2903 struct nlmsghdr *nlh;
2906 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2910 upe = nlmsg_data(nlh);
2911 copy_to_user_policy(xp, &upe->pol, dir);
2912 err = copy_to_user_tmpl(xp, skb);
2914 err = copy_to_user_sec_ctx(xp, skb);
2916 err = copy_to_user_policy_type(xp->type, skb);
2918 err = xfrm_mark_put(skb, &xp->mark);
2920 nlmsg_cancel(skb, nlh);
2925 nlmsg_end(skb, nlh);
2929 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2931 struct net *net = xp_net(xp);
2932 struct sk_buff *skb;
2934 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2938 if (build_polexpire(skb, xp, dir, c) < 0)
2941 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2944 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2946 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2947 struct net *net = xp_net(xp);
2948 struct xfrm_userpolicy_info *p;
2949 struct xfrm_userpolicy_id *id;
2950 struct nlmsghdr *nlh;
2951 struct sk_buff *skb;
2954 headlen = sizeof(*p);
2955 if (c->event == XFRM_MSG_DELPOLICY) {
2956 len += nla_total_size(headlen);
2957 headlen = sizeof(*id);
2959 len += userpolicy_type_attrsize();
2960 len += nla_total_size(sizeof(struct xfrm_mark));
2961 len += NLMSG_ALIGN(headlen);
2963 skb = nlmsg_new(len, GFP_ATOMIC);
2967 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2972 p = nlmsg_data(nlh);
2973 if (c->event == XFRM_MSG_DELPOLICY) {
2974 struct nlattr *attr;
2976 id = nlmsg_data(nlh);
2977 memset(id, 0, sizeof(*id));
2980 id->index = xp->index;
2982 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2984 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2992 copy_to_user_policy(xp, p, dir);
2993 err = copy_to_user_tmpl(xp, skb);
2995 err = copy_to_user_policy_type(xp->type, skb);
2997 err = xfrm_mark_put(skb, &xp->mark);
3001 nlmsg_end(skb, nlh);
3003 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3010 static int xfrm_notify_policy_flush(const struct km_event *c)
3012 struct net *net = c->net;
3013 struct nlmsghdr *nlh;
3014 struct sk_buff *skb;
3017 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3021 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3025 err = copy_to_user_policy_type(c->data.type, skb);
3029 nlmsg_end(skb, nlh);
3031 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3038 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3042 err = verify_policy_dir(dir);
3047 case XFRM_MSG_NEWPOLICY:
3048 case XFRM_MSG_UPDPOLICY:
3049 case XFRM_MSG_DELPOLICY:
3050 return xfrm_notify_policy(xp, dir, c);
3051 case XFRM_MSG_FLUSHPOLICY:
3052 return xfrm_notify_policy_flush(c);
3053 case XFRM_MSG_POLEXPIRE:
3054 return xfrm_exp_policy_notify(xp, dir, c);
3056 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3064 static inline size_t xfrm_report_msgsize(void)
3066 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3069 static int build_report(struct sk_buff *skb, u8 proto,
3070 struct xfrm_selector *sel, xfrm_address_t *addr)
3072 struct xfrm_user_report *ur;
3073 struct nlmsghdr *nlh;
3075 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3079 ur = nlmsg_data(nlh);
3081 memcpy(&ur->sel, sel, sizeof(ur->sel));
3084 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3086 nlmsg_cancel(skb, nlh);
3090 nlmsg_end(skb, nlh);
3094 static int xfrm_send_report(struct net *net, u8 proto,
3095 struct xfrm_selector *sel, xfrm_address_t *addr)
3097 struct sk_buff *skb;
3099 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3103 if (build_report(skb, proto, sel, addr) < 0)
3106 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3109 static inline size_t xfrm_mapping_msgsize(void)
3111 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3114 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3115 xfrm_address_t *new_saddr, __be16 new_sport)
3117 struct xfrm_user_mapping *um;
3118 struct nlmsghdr *nlh;
3120 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3124 um = nlmsg_data(nlh);
3126 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3127 um->id.spi = x->id.spi;
3128 um->id.family = x->props.family;
3129 um->id.proto = x->id.proto;
3130 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3131 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3132 um->new_sport = new_sport;
3133 um->old_sport = x->encap->encap_sport;
3134 um->reqid = x->props.reqid;
3136 nlmsg_end(skb, nlh);
3140 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3143 struct net *net = xs_net(x);
3144 struct sk_buff *skb;
3146 if (x->id.proto != IPPROTO_ESP)
3152 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3156 if (build_mapping(skb, x, ipaddr, sport) < 0)
3159 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3162 static bool xfrm_is_alive(const struct km_event *c)
3164 return (bool)xfrm_acquire_is_on(c->net);
3167 static struct xfrm_mgr netlink_mgr = {
3169 .notify = xfrm_send_state_notify,
3170 .acquire = xfrm_send_acquire,
3171 .compile_policy = xfrm_compile_policy,
3172 .notify_policy = xfrm_send_policy_notify,
3173 .report = xfrm_send_report,
3174 .migrate = xfrm_send_migrate,
3175 .new_mapping = xfrm_send_mapping,
3176 .is_alive = xfrm_is_alive,
3179 static int __net_init xfrm_user_net_init(struct net *net)
3182 struct netlink_kernel_cfg cfg = {
3183 .groups = XFRMNLGRP_MAX,
3184 .input = xfrm_netlink_rcv,
3187 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3190 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3191 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3195 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3198 list_for_each_entry(net, net_exit_list, exit_list)
3199 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3201 list_for_each_entry(net, net_exit_list, exit_list)
3202 netlink_kernel_release(net->xfrm.nlsk_stash);
3205 static struct pernet_operations xfrm_user_net_ops = {
3206 .init = xfrm_user_net_init,
3207 .exit_batch = xfrm_user_net_exit,
3210 static int __init xfrm_user_init(void)
3214 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3216 rv = register_pernet_subsys(&xfrm_user_net_ops);
3219 rv = xfrm_register_km(&netlink_mgr);
3221 unregister_pernet_subsys(&xfrm_user_net_ops);
3225 static void __exit xfrm_user_exit(void)
3227 xfrm_unregister_km(&netlink_mgr);
3228 unregister_pernet_subsys(&xfrm_user_net_ops);
3231 module_init(xfrm_user_init);
3232 module_exit(xfrm_user_exit);
3233 MODULE_LICENSE("GPL");
3234 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);