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)
154 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
160 #if IS_ENABLED(CONFIG_IPV6)
161 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
175 switch (p->id.proto) {
177 if ((!attrs[XFRMA_ALG_AUTH] &&
178 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
179 attrs[XFRMA_ALG_AEAD] ||
180 attrs[XFRMA_ALG_CRYPT] ||
181 attrs[XFRMA_ALG_COMP] ||
187 if (attrs[XFRMA_ALG_COMP])
189 if (!attrs[XFRMA_ALG_AUTH] &&
190 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
191 !attrs[XFRMA_ALG_CRYPT] &&
192 !attrs[XFRMA_ALG_AEAD])
194 if ((attrs[XFRMA_ALG_AUTH] ||
195 attrs[XFRMA_ALG_AUTH_TRUNC] ||
196 attrs[XFRMA_ALG_CRYPT]) &&
197 attrs[XFRMA_ALG_AEAD])
199 if (attrs[XFRMA_TFCPAD] &&
200 p->mode != XFRM_MODE_TUNNEL)
205 if (!attrs[XFRMA_ALG_COMP] ||
206 attrs[XFRMA_ALG_AEAD] ||
207 attrs[XFRMA_ALG_AUTH] ||
208 attrs[XFRMA_ALG_AUTH_TRUNC] ||
209 attrs[XFRMA_ALG_CRYPT] ||
210 attrs[XFRMA_TFCPAD] ||
211 (ntohl(p->id.spi) >= 0x10000))
215 #if IS_ENABLED(CONFIG_IPV6)
216 case IPPROTO_DSTOPTS:
217 case IPPROTO_ROUTING:
218 if (attrs[XFRMA_ALG_COMP] ||
219 attrs[XFRMA_ALG_AUTH] ||
220 attrs[XFRMA_ALG_AUTH_TRUNC] ||
221 attrs[XFRMA_ALG_AEAD] ||
222 attrs[XFRMA_ALG_CRYPT] ||
223 attrs[XFRMA_ENCAP] ||
224 attrs[XFRMA_SEC_CTX] ||
225 attrs[XFRMA_TFCPAD] ||
226 !attrs[XFRMA_COADDR])
235 if ((err = verify_aead(attrs)))
237 if ((err = verify_auth_trunc(attrs)))
239 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
241 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
243 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
245 if ((err = verify_sec_ctx_len(attrs)))
247 if ((err = verify_replay(p, attrs)))
252 case XFRM_MODE_TRANSPORT:
253 case XFRM_MODE_TUNNEL:
254 case XFRM_MODE_ROUTEOPTIMIZATION:
268 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
269 struct xfrm_algo_desc *(*get_byname)(const char *, int),
272 struct xfrm_algo *p, *ualg;
273 struct xfrm_algo_desc *algo;
278 ualg = nla_data(rta);
280 algo = get_byname(ualg->alg_name, 1);
283 *props = algo->desc.sadb_alg_id;
285 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
289 strcpy(p->alg_name, algo->name);
294 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
296 struct xfrm_algo *p, *ualg;
297 struct xfrm_algo_desc *algo;
302 ualg = nla_data(rta);
304 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
307 x->props.ealgo = algo->desc.sadb_alg_id;
309 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
313 strcpy(p->alg_name, algo->name);
315 x->geniv = algo->uinfo.encr.geniv;
319 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
322 struct xfrm_algo *ualg;
323 struct xfrm_algo_auth *p;
324 struct xfrm_algo_desc *algo;
329 ualg = nla_data(rta);
331 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
334 *props = algo->desc.sadb_alg_id;
336 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
340 strcpy(p->alg_name, algo->name);
341 p->alg_key_len = ualg->alg_key_len;
342 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
343 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
349 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
352 struct xfrm_algo_auth *p, *ualg;
353 struct xfrm_algo_desc *algo;
358 ualg = nla_data(rta);
360 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
363 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
365 *props = algo->desc.sadb_alg_id;
367 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
371 strcpy(p->alg_name, algo->name);
372 if (!p->alg_trunc_len)
373 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
379 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
381 struct xfrm_algo_aead *p, *ualg;
382 struct xfrm_algo_desc *algo;
387 ualg = nla_data(rta);
389 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
392 x->props.ealgo = algo->desc.sadb_alg_id;
394 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
398 strcpy(p->alg_name, algo->name);
400 x->geniv = algo->uinfo.aead.geniv;
404 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
407 struct xfrm_replay_state_esn *up;
410 if (!replay_esn || !rp)
414 ulen = xfrm_replay_state_esn_len(up);
416 /* Check the overall length and the internal bitmap length to avoid
417 * potential overflow. */
418 if (nla_len(rp) < ulen ||
419 xfrm_replay_state_esn_len(replay_esn) != ulen ||
420 replay_esn->bmp_len != up->bmp_len)
423 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
429 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
430 struct xfrm_replay_state_esn **preplay_esn,
433 struct xfrm_replay_state_esn *p, *pp, *up;
440 klen = xfrm_replay_state_esn_len(up);
441 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
443 p = kzalloc(klen, GFP_KERNEL);
447 pp = kzalloc(klen, GFP_KERNEL);
454 memcpy(pp, up, ulen);
462 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
467 len += sizeof(struct xfrm_user_sec_ctx);
468 len += xfrm_ctx->ctx_len;
473 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
475 memcpy(&x->id, &p->id, sizeof(x->id));
476 memcpy(&x->sel, &p->sel, sizeof(x->sel));
477 memcpy(&x->lft, &p->lft, sizeof(x->lft));
478 x->props.mode = p->mode;
479 x->props.replay_window = min_t(unsigned int, p->replay_window,
480 sizeof(x->replay.bitmap) * 8);
481 x->props.reqid = p->reqid;
482 x->props.family = p->family;
483 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
484 x->props.flags = p->flags;
486 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
487 x->sel.family = p->family;
491 * someday when pfkey also has support, we could have the code
492 * somehow made shareable and move it to xfrm_state.c - JHS
495 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
498 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
499 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
500 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
501 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
502 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
505 struct xfrm_replay_state_esn *replay_esn;
506 replay_esn = nla_data(re);
507 memcpy(x->replay_esn, replay_esn,
508 xfrm_replay_state_esn_len(replay_esn));
509 memcpy(x->preplay_esn, replay_esn,
510 xfrm_replay_state_esn_len(replay_esn));
514 struct xfrm_replay_state *replay;
515 replay = nla_data(rp);
516 memcpy(&x->replay, replay, sizeof(*replay));
517 memcpy(&x->preplay, replay, sizeof(*replay));
521 struct xfrm_lifetime_cur *ltime;
522 ltime = nla_data(lt);
523 x->curlft.bytes = ltime->bytes;
524 x->curlft.packets = ltime->packets;
525 x->curlft.add_time = ltime->add_time;
526 x->curlft.use_time = ltime->use_time;
530 x->replay_maxage = nla_get_u32(et);
533 x->replay_maxdiff = nla_get_u32(rt);
536 static struct xfrm_state *xfrm_state_construct(struct net *net,
537 struct xfrm_usersa_info *p,
538 struct nlattr **attrs,
541 struct xfrm_state *x = xfrm_state_alloc(net);
547 copy_from_user_state(x, p);
549 if (attrs[XFRMA_SA_EXTRA_FLAGS])
550 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
552 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
554 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
555 attrs[XFRMA_ALG_AUTH_TRUNC])))
557 if (!x->props.aalgo) {
558 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
559 attrs[XFRMA_ALG_AUTH])))
562 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
564 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
565 xfrm_calg_get_byname,
566 attrs[XFRMA_ALG_COMP])))
569 if (attrs[XFRMA_ENCAP]) {
570 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
571 sizeof(*x->encap), GFP_KERNEL);
572 if (x->encap == NULL)
576 if (attrs[XFRMA_TFCPAD])
577 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
579 if (attrs[XFRMA_COADDR]) {
580 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
581 sizeof(*x->coaddr), GFP_KERNEL);
582 if (x->coaddr == NULL)
586 xfrm_mark_get(attrs, &x->mark);
588 if (attrs[XFRMA_OUTPUT_MARK])
589 x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
591 err = __xfrm_init_state(x, false);
595 if (attrs[XFRMA_SEC_CTX] &&
596 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
599 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
600 attrs[XFRMA_REPLAY_ESN_VAL])))
604 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
605 /* sysctl_xfrm_aevent_etime is in 100ms units */
606 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
608 if ((err = xfrm_init_replay(x)))
611 /* override default values from above */
612 xfrm_update_ae_params(x, attrs, 0);
617 x->km.state = XFRM_STATE_DEAD;
624 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
625 struct nlattr **attrs)
627 struct net *net = sock_net(skb->sk);
628 struct xfrm_usersa_info *p = nlmsg_data(nlh);
629 struct xfrm_state *x;
633 err = verify_newsa_info(p, attrs);
637 x = xfrm_state_construct(net, p, attrs, &err);
642 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
643 err = xfrm_state_add(x);
645 err = xfrm_state_update(x);
647 xfrm_audit_state_add(x, err ? 0 : 1, true);
650 x->km.state = XFRM_STATE_DEAD;
655 c.seq = nlh->nlmsg_seq;
656 c.portid = nlh->nlmsg_pid;
657 c.event = nlh->nlmsg_type;
659 km_state_notify(x, &c);
665 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
666 struct xfrm_usersa_id *p,
667 struct nlattr **attrs,
670 struct xfrm_state *x = NULL;
673 u32 mark = xfrm_mark_get(attrs, &m);
675 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
677 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
679 xfrm_address_t *saddr = NULL;
681 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
688 x = xfrm_state_lookup_byaddr(net, mark,
690 p->proto, p->family);
699 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
700 struct nlattr **attrs)
702 struct net *net = sock_net(skb->sk);
703 struct xfrm_state *x;
706 struct xfrm_usersa_id *p = nlmsg_data(nlh);
708 x = xfrm_user_state_lookup(net, p, attrs, &err);
712 if ((err = security_xfrm_state_delete(x)) != 0)
715 if (xfrm_state_kern(x)) {
720 err = xfrm_state_delete(x);
725 c.seq = nlh->nlmsg_seq;
726 c.portid = nlh->nlmsg_pid;
727 c.event = nlh->nlmsg_type;
728 km_state_notify(x, &c);
731 xfrm_audit_state_delete(x, err ? 0 : 1, true);
736 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
738 memset(p, 0, sizeof(*p));
739 memcpy(&p->id, &x->id, sizeof(p->id));
740 memcpy(&p->sel, &x->sel, sizeof(p->sel));
741 memcpy(&p->lft, &x->lft, sizeof(p->lft));
742 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
743 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
744 put_unaligned(x->stats.replay, &p->stats.replay);
745 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
746 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
747 p->mode = x->props.mode;
748 p->replay_window = x->props.replay_window;
749 p->reqid = x->props.reqid;
750 p->family = x->props.family;
751 p->flags = x->props.flags;
755 struct xfrm_dump_info {
756 struct sk_buff *in_skb;
757 struct sk_buff *out_skb;
762 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
764 struct xfrm_user_sec_ctx *uctx;
766 int ctx_size = sizeof(*uctx) + s->ctx_len;
768 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
772 uctx = nla_data(attr);
773 uctx->exttype = XFRMA_SEC_CTX;
774 uctx->len = ctx_size;
775 uctx->ctx_doi = s->ctx_doi;
776 uctx->ctx_alg = s->ctx_alg;
777 uctx->ctx_len = s->ctx_len;
778 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
783 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
785 struct xfrm_algo *algo;
788 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
789 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
793 algo = nla_data(nla);
794 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
795 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
796 algo->alg_key_len = auth->alg_key_len;
801 /* Don't change this without updating xfrm_sa_len! */
802 static int copy_to_user_state_extra(struct xfrm_state *x,
803 struct xfrm_usersa_info *p,
808 copy_to_user_state(x, p);
810 if (x->props.extra_flags) {
811 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
812 x->props.extra_flags);
818 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
823 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
828 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
833 ret = copy_to_user_auth(x->aalg, skb);
835 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
836 xfrm_alg_auth_len(x->aalg), x->aalg);
841 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
846 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
851 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
856 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
860 ret = xfrm_mark_put(skb, &x->mark);
864 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
865 xfrm_replay_state_esn_len(x->replay_esn),
868 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
872 if (x->props.output_mark) {
873 ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
878 ret = copy_sec_ctx(x->security, skb);
883 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
885 struct xfrm_dump_info *sp = ptr;
886 struct sk_buff *in_skb = sp->in_skb;
887 struct sk_buff *skb = sp->out_skb;
888 struct xfrm_usersa_info *p;
889 struct nlmsghdr *nlh;
892 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
893 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
899 err = copy_to_user_state_extra(x, p, skb);
901 nlmsg_cancel(skb, nlh);
908 static int xfrm_dump_sa_done(struct netlink_callback *cb)
910 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
911 struct sock *sk = cb->skb->sk;
912 struct net *net = sock_net(sk);
914 xfrm_state_walk_done(walk, net);
918 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
919 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
921 struct net *net = sock_net(skb->sk);
922 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
923 struct xfrm_dump_info info;
925 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
926 sizeof(cb->args) - sizeof(cb->args[0]));
928 info.in_skb = cb->skb;
930 info.nlmsg_seq = cb->nlh->nlmsg_seq;
931 info.nlmsg_flags = NLM_F_MULTI;
934 struct nlattr *attrs[XFRMA_MAX+1];
935 struct xfrm_address_filter *filter = NULL;
941 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
946 if (attrs[XFRMA_ADDRESS_FILTER]) {
947 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
948 sizeof(*filter), GFP_KERNEL);
953 if (attrs[XFRMA_PROTO])
954 proto = nla_get_u8(attrs[XFRMA_PROTO]);
956 xfrm_state_walk_init(walk, proto, filter);
959 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
964 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
965 struct xfrm_state *x, u32 seq)
967 struct xfrm_dump_info info;
971 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
973 return ERR_PTR(-ENOMEM);
975 info.in_skb = in_skb;
977 info.nlmsg_seq = seq;
978 info.nlmsg_flags = 0;
980 err = dump_one_state(x, 0, &info);
989 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
990 * Must be called with RCU read lock.
992 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
993 u32 pid, unsigned int group)
995 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1002 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1005 static inline size_t xfrm_spdinfo_msgsize(void)
1007 return NLMSG_ALIGN(4)
1008 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1009 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1010 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1011 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1014 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1015 u32 portid, u32 seq, u32 flags)
1017 struct xfrmk_spdinfo si;
1018 struct xfrmu_spdinfo spc;
1019 struct xfrmu_spdhinfo sph;
1020 struct xfrmu_spdhthresh spt4, spt6;
1021 struct nlmsghdr *nlh;
1026 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1027 if (nlh == NULL) /* shouldn't really happen ... */
1030 f = nlmsg_data(nlh);
1032 xfrm_spd_getinfo(net, &si);
1033 spc.incnt = si.incnt;
1034 spc.outcnt = si.outcnt;
1035 spc.fwdcnt = si.fwdcnt;
1036 spc.inscnt = si.inscnt;
1037 spc.outscnt = si.outscnt;
1038 spc.fwdscnt = si.fwdscnt;
1039 sph.spdhcnt = si.spdhcnt;
1040 sph.spdhmcnt = si.spdhmcnt;
1043 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1045 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1046 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1047 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1048 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1049 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1051 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1053 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1055 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1057 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1059 nlmsg_cancel(skb, nlh);
1063 nlmsg_end(skb, nlh);
1067 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1068 struct nlattr **attrs)
1070 struct net *net = sock_net(skb->sk);
1071 struct xfrmu_spdhthresh *thresh4 = NULL;
1072 struct xfrmu_spdhthresh *thresh6 = NULL;
1074 /* selector prefixlen thresholds to hash policies */
1075 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1076 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1078 if (nla_len(rta) < sizeof(*thresh4))
1080 thresh4 = nla_data(rta);
1081 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1084 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1085 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1087 if (nla_len(rta) < sizeof(*thresh6))
1089 thresh6 = nla_data(rta);
1090 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1094 if (thresh4 || thresh6) {
1095 write_seqlock(&net->xfrm.policy_hthresh.lock);
1097 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1098 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1101 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1102 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1104 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1106 xfrm_policy_hash_rebuild(net);
1112 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1113 struct nlattr **attrs)
1115 struct net *net = sock_net(skb->sk);
1116 struct sk_buff *r_skb;
1117 u32 *flags = nlmsg_data(nlh);
1118 u32 sportid = NETLINK_CB(skb).portid;
1119 u32 seq = nlh->nlmsg_seq;
1121 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1125 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1128 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1131 static inline size_t xfrm_sadinfo_msgsize(void)
1133 return NLMSG_ALIGN(4)
1134 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1135 + nla_total_size(4); /* XFRMA_SAD_CNT */
1138 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1139 u32 portid, u32 seq, u32 flags)
1141 struct xfrmk_sadinfo si;
1142 struct xfrmu_sadhinfo sh;
1143 struct nlmsghdr *nlh;
1147 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1148 if (nlh == NULL) /* shouldn't really happen ... */
1151 f = nlmsg_data(nlh);
1153 xfrm_sad_getinfo(net, &si);
1155 sh.sadhmcnt = si.sadhmcnt;
1156 sh.sadhcnt = si.sadhcnt;
1158 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1160 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1162 nlmsg_cancel(skb, nlh);
1166 nlmsg_end(skb, nlh);
1170 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1171 struct nlattr **attrs)
1173 struct net *net = sock_net(skb->sk);
1174 struct sk_buff *r_skb;
1175 u32 *flags = nlmsg_data(nlh);
1176 u32 sportid = NETLINK_CB(skb).portid;
1177 u32 seq = nlh->nlmsg_seq;
1179 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1183 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1186 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1189 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1190 struct nlattr **attrs)
1192 struct net *net = sock_net(skb->sk);
1193 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1194 struct xfrm_state *x;
1195 struct sk_buff *resp_skb;
1198 x = xfrm_user_state_lookup(net, p, attrs, &err);
1202 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1203 if (IS_ERR(resp_skb)) {
1204 err = PTR_ERR(resp_skb);
1206 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1213 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1214 struct nlattr **attrs)
1216 struct net *net = sock_net(skb->sk);
1217 struct xfrm_state *x;
1218 struct xfrm_userspi_info *p;
1219 struct sk_buff *resp_skb;
1220 xfrm_address_t *daddr;
1226 p = nlmsg_data(nlh);
1227 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1231 family = p->info.family;
1232 daddr = &p->info.id.daddr;
1236 mark = xfrm_mark_get(attrs, &m);
1238 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1239 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1246 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1247 p->info.id.proto, daddr,
1254 err = xfrm_alloc_spi(x, p->min, p->max);
1258 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1259 if (IS_ERR(resp_skb)) {
1260 err = PTR_ERR(resp_skb);
1264 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1272 static int verify_policy_dir(u8 dir)
1275 case XFRM_POLICY_IN:
1276 case XFRM_POLICY_OUT:
1277 case XFRM_POLICY_FWD:
1287 static int verify_policy_type(u8 type)
1290 case XFRM_POLICY_TYPE_MAIN:
1291 #ifdef CONFIG_XFRM_SUB_POLICY
1292 case XFRM_POLICY_TYPE_SUB:
1303 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1308 case XFRM_SHARE_ANY:
1309 case XFRM_SHARE_SESSION:
1310 case XFRM_SHARE_USER:
1311 case XFRM_SHARE_UNIQUE:
1318 switch (p->action) {
1319 case XFRM_POLICY_ALLOW:
1320 case XFRM_POLICY_BLOCK:
1327 switch (p->sel.family) {
1329 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1335 #if IS_ENABLED(CONFIG_IPV6)
1336 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1341 return -EAFNOSUPPORT;
1348 ret = verify_policy_dir(p->dir);
1351 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1357 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1359 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1360 struct xfrm_user_sec_ctx *uctx;
1365 uctx = nla_data(rt);
1366 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1369 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1375 for (i = 0; i < nr; i++, ut++) {
1376 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1378 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1379 memcpy(&t->saddr, &ut->saddr,
1380 sizeof(xfrm_address_t));
1381 t->reqid = ut->reqid;
1383 t->share = ut->share;
1384 t->optional = ut->optional;
1385 t->aalgos = ut->aalgos;
1386 t->ealgos = ut->ealgos;
1387 t->calgos = ut->calgos;
1388 /* If all masks are ~0, then we allow all algorithms. */
1389 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1390 t->encap_family = ut->family;
1394 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1399 if (nr > XFRM_MAX_DEPTH)
1402 prev_family = family;
1404 for (i = 0; i < nr; i++) {
1405 /* We never validated the ut->family value, so many
1406 * applications simply leave it at zero. The check was
1407 * never made and ut->family was ignored because all
1408 * templates could be assumed to have the same family as
1409 * the policy itself. Now that we will have ipv4-in-ipv6
1410 * and ipv6-in-ipv4 tunnels, this is no longer true.
1413 ut[i].family = family;
1415 if ((ut[i].mode == XFRM_MODE_TRANSPORT) &&
1416 (ut[i].family != prev_family))
1419 if (ut[i].mode >= XFRM_MODE_MAX)
1422 prev_family = ut[i].family;
1424 switch (ut[i].family) {
1427 #if IS_ENABLED(CONFIG_IPV6)
1435 switch (ut[i].id.proto) {
1439 #if IS_ENABLED(CONFIG_IPV6)
1440 case IPPROTO_ROUTING:
1441 case IPPROTO_DSTOPTS:
1443 case IPSEC_PROTO_ANY:
1454 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1456 struct nlattr *rt = attrs[XFRMA_TMPL];
1461 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1462 int nr = nla_len(rt) / sizeof(*utmpl);
1465 err = validate_tmpl(nr, utmpl, pol->family);
1469 copy_templates(pol, utmpl, nr);
1474 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1476 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1477 struct xfrm_userpolicy_type *upt;
1478 u8 type = XFRM_POLICY_TYPE_MAIN;
1486 err = verify_policy_type(type);
1494 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1496 xp->priority = p->priority;
1497 xp->index = p->index;
1498 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1499 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1500 xp->action = p->action;
1501 xp->flags = p->flags;
1502 xp->family = p->sel.family;
1503 /* XXX xp->share = p->share; */
1506 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1508 memset(p, 0, sizeof(*p));
1509 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1510 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1511 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1512 p->priority = xp->priority;
1513 p->index = xp->index;
1514 p->sel.family = xp->family;
1516 p->action = xp->action;
1517 p->flags = xp->flags;
1518 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1521 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1523 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1531 copy_from_user_policy(xp, p);
1533 err = copy_from_user_policy_type(&xp->type, attrs);
1537 if (!(err = copy_from_user_tmpl(xp, attrs)))
1538 err = copy_from_user_sec_ctx(xp, attrs);
1542 xfrm_mark_get(attrs, &xp->mark);
1548 xfrm_policy_destroy(xp);
1552 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1553 struct nlattr **attrs)
1555 struct net *net = sock_net(skb->sk);
1556 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1557 struct xfrm_policy *xp;
1562 err = verify_newpolicy_info(p);
1565 err = verify_sec_ctx_len(attrs);
1569 xp = xfrm_policy_construct(net, p, attrs, &err);
1573 /* shouldn't excl be based on nlh flags??
1574 * Aha! this is anti-netlink really i.e more pfkey derived
1575 * in netlink excl is a flag and you wouldnt need
1576 * a type XFRM_MSG_UPDPOLICY - JHS */
1577 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1578 err = xfrm_policy_insert(p->dir, xp, excl);
1579 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1582 security_xfrm_policy_free(xp->security);
1587 c.event = nlh->nlmsg_type;
1588 c.seq = nlh->nlmsg_seq;
1589 c.portid = nlh->nlmsg_pid;
1590 km_policy_notify(xp, p->dir, &c);
1597 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1599 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1602 if (xp->xfrm_nr == 0)
1605 for (i = 0; i < xp->xfrm_nr; i++) {
1606 struct xfrm_user_tmpl *up = &vec[i];
1607 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1609 memset(up, 0, sizeof(*up));
1610 memcpy(&up->id, &kp->id, sizeof(up->id));
1611 up->family = kp->encap_family;
1612 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1613 up->reqid = kp->reqid;
1614 up->mode = kp->mode;
1615 up->share = kp->share;
1616 up->optional = kp->optional;
1617 up->aalgos = kp->aalgos;
1618 up->ealgos = kp->ealgos;
1619 up->calgos = kp->calgos;
1622 return nla_put(skb, XFRMA_TMPL,
1623 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1626 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1629 return copy_sec_ctx(x->security, skb);
1634 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1637 return copy_sec_ctx(xp->security, skb);
1640 static inline size_t userpolicy_type_attrsize(void)
1642 #ifdef CONFIG_XFRM_SUB_POLICY
1643 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1649 #ifdef CONFIG_XFRM_SUB_POLICY
1650 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1652 struct xfrm_userpolicy_type upt;
1654 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1655 memset(&upt, 0, sizeof(upt));
1658 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1662 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1668 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1670 struct xfrm_dump_info *sp = ptr;
1671 struct xfrm_userpolicy_info *p;
1672 struct sk_buff *in_skb = sp->in_skb;
1673 struct sk_buff *skb = sp->out_skb;
1674 struct nlmsghdr *nlh;
1677 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1678 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1682 p = nlmsg_data(nlh);
1683 copy_to_user_policy(xp, p, dir);
1684 err = copy_to_user_tmpl(xp, skb);
1686 err = copy_to_user_sec_ctx(xp, skb);
1688 err = copy_to_user_policy_type(xp->type, skb);
1690 err = xfrm_mark_put(skb, &xp->mark);
1692 nlmsg_cancel(skb, nlh);
1695 nlmsg_end(skb, nlh);
1699 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1701 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1702 struct net *net = sock_net(cb->skb->sk);
1704 xfrm_policy_walk_done(walk, net);
1708 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1710 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1712 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1714 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1718 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1720 struct net *net = sock_net(skb->sk);
1721 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1722 struct xfrm_dump_info info;
1724 info.in_skb = cb->skb;
1726 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1727 info.nlmsg_flags = NLM_F_MULTI;
1729 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1734 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1735 struct xfrm_policy *xp,
1738 struct xfrm_dump_info info;
1739 struct sk_buff *skb;
1742 err = verify_policy_dir(dir);
1744 return ERR_PTR(err);
1746 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1748 return ERR_PTR(-ENOMEM);
1750 info.in_skb = in_skb;
1752 info.nlmsg_seq = seq;
1753 info.nlmsg_flags = 0;
1755 err = dump_one_policy(xp, dir, 0, &info);
1758 return ERR_PTR(err);
1764 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1765 struct nlattr **attrs)
1767 struct net *net = sock_net(skb->sk);
1768 struct xfrm_policy *xp;
1769 struct xfrm_userpolicy_id *p;
1770 u8 type = XFRM_POLICY_TYPE_MAIN;
1775 u32 mark = xfrm_mark_get(attrs, &m);
1777 p = nlmsg_data(nlh);
1778 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1780 err = copy_from_user_policy_type(&type, attrs);
1784 err = verify_policy_dir(p->dir);
1789 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1791 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1792 struct xfrm_sec_ctx *ctx;
1794 err = verify_sec_ctx_len(attrs);
1800 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1802 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1806 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1808 security_xfrm_policy_free(ctx);
1814 struct sk_buff *resp_skb;
1816 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1817 if (IS_ERR(resp_skb)) {
1818 err = PTR_ERR(resp_skb);
1820 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1821 NETLINK_CB(skb).portid);
1824 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1829 c.data.byid = p->index;
1830 c.event = nlh->nlmsg_type;
1831 c.seq = nlh->nlmsg_seq;
1832 c.portid = nlh->nlmsg_pid;
1833 km_policy_notify(xp, p->dir, &c);
1838 if (delete && err == 0)
1839 xfrm_garbage_collect(net);
1843 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1844 struct nlattr **attrs)
1846 struct net *net = sock_net(skb->sk);
1848 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1851 err = xfrm_state_flush(net, p->proto, true);
1853 if (err == -ESRCH) /* empty table */
1857 c.data.proto = p->proto;
1858 c.event = nlh->nlmsg_type;
1859 c.seq = nlh->nlmsg_seq;
1860 c.portid = nlh->nlmsg_pid;
1862 km_state_notify(NULL, &c);
1867 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1869 size_t replay_size = x->replay_esn ?
1870 xfrm_replay_state_esn_len(x->replay_esn) :
1871 sizeof(struct xfrm_replay_state);
1873 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1874 + nla_total_size(replay_size)
1875 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1876 + nla_total_size(sizeof(struct xfrm_mark))
1877 + nla_total_size(4) /* XFRM_AE_RTHR */
1878 + nla_total_size(4); /* XFRM_AE_ETHR */
1881 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1883 struct xfrm_aevent_id *id;
1884 struct nlmsghdr *nlh;
1887 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1891 id = nlmsg_data(nlh);
1892 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1893 id->sa_id.spi = x->id.spi;
1894 id->sa_id.family = x->props.family;
1895 id->sa_id.proto = x->id.proto;
1896 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1897 id->reqid = x->props.reqid;
1898 id->flags = c->data.aevent;
1900 if (x->replay_esn) {
1901 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1902 xfrm_replay_state_esn_len(x->replay_esn),
1905 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1910 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1914 if (id->flags & XFRM_AE_RTHR) {
1915 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1919 if (id->flags & XFRM_AE_ETHR) {
1920 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1921 x->replay_maxage * 10 / HZ);
1925 err = xfrm_mark_put(skb, &x->mark);
1929 nlmsg_end(skb, nlh);
1933 nlmsg_cancel(skb, nlh);
1937 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1938 struct nlattr **attrs)
1940 struct net *net = sock_net(skb->sk);
1941 struct xfrm_state *x;
1942 struct sk_buff *r_skb;
1947 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1948 struct xfrm_usersa_id *id = &p->sa_id;
1950 mark = xfrm_mark_get(attrs, &m);
1952 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1956 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1957 if (r_skb == NULL) {
1963 * XXX: is this lock really needed - none of the other
1964 * gets lock (the concern is things getting updated
1965 * while we are still reading) - jhs
1967 spin_lock_bh(&x->lock);
1968 c.data.aevent = p->flags;
1969 c.seq = nlh->nlmsg_seq;
1970 c.portid = nlh->nlmsg_pid;
1972 if (build_aevent(r_skb, x, &c) < 0)
1974 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1975 spin_unlock_bh(&x->lock);
1980 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1981 struct nlattr **attrs)
1983 struct net *net = sock_net(skb->sk);
1984 struct xfrm_state *x;
1989 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1990 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1991 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1992 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1993 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1994 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
1996 if (!lt && !rp && !re && !et && !rt)
1999 /* pedantic mode - thou shalt sayeth replaceth */
2000 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2003 mark = xfrm_mark_get(attrs, &m);
2005 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2009 if (x->km.state != XFRM_STATE_VALID)
2012 err = xfrm_replay_verify_len(x->replay_esn, re);
2016 spin_lock_bh(&x->lock);
2017 xfrm_update_ae_params(x, attrs, 1);
2018 spin_unlock_bh(&x->lock);
2020 c.event = nlh->nlmsg_type;
2021 c.seq = nlh->nlmsg_seq;
2022 c.portid = nlh->nlmsg_pid;
2023 c.data.aevent = XFRM_AE_CU;
2024 km_state_notify(x, &c);
2031 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2032 struct nlattr **attrs)
2034 struct net *net = sock_net(skb->sk);
2036 u8 type = XFRM_POLICY_TYPE_MAIN;
2039 err = copy_from_user_policy_type(&type, attrs);
2043 err = xfrm_policy_flush(net, type, true);
2045 if (err == -ESRCH) /* empty table */
2051 c.event = nlh->nlmsg_type;
2052 c.seq = nlh->nlmsg_seq;
2053 c.portid = nlh->nlmsg_pid;
2055 km_policy_notify(NULL, 0, &c);
2059 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2060 struct nlattr **attrs)
2062 struct net *net = sock_net(skb->sk);
2063 struct xfrm_policy *xp;
2064 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2065 struct xfrm_userpolicy_info *p = &up->pol;
2066 u8 type = XFRM_POLICY_TYPE_MAIN;
2069 u32 mark = xfrm_mark_get(attrs, &m);
2071 err = copy_from_user_policy_type(&type, attrs);
2075 err = verify_policy_dir(p->dir);
2080 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
2082 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2083 struct xfrm_sec_ctx *ctx;
2085 err = verify_sec_ctx_len(attrs);
2091 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2093 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2097 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
2098 &p->sel, ctx, 0, &err);
2099 security_xfrm_policy_free(ctx);
2104 if (unlikely(xp->walk.dead))
2109 xfrm_policy_delete(xp, p->dir);
2110 xfrm_audit_policy_delete(xp, 1, true);
2112 // reset the timers here?
2113 WARN(1, "Don't know what to do with soft policy expire\n");
2115 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2122 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2123 struct nlattr **attrs)
2125 struct net *net = sock_net(skb->sk);
2126 struct xfrm_state *x;
2128 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2129 struct xfrm_usersa_info *p = &ue->state;
2131 u32 mark = xfrm_mark_get(attrs, &m);
2133 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2139 spin_lock_bh(&x->lock);
2141 if (x->km.state != XFRM_STATE_VALID)
2143 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2146 __xfrm_state_delete(x);
2147 xfrm_audit_state_delete(x, 1, true);
2151 spin_unlock_bh(&x->lock);
2156 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2157 struct nlattr **attrs)
2159 struct net *net = sock_net(skb->sk);
2160 struct xfrm_policy *xp;
2161 struct xfrm_user_tmpl *ut;
2163 struct nlattr *rt = attrs[XFRMA_TMPL];
2164 struct xfrm_mark mark;
2166 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2167 struct xfrm_state *x = xfrm_state_alloc(net);
2173 xfrm_mark_get(attrs, &mark);
2175 err = verify_newpolicy_info(&ua->policy);
2180 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2184 memcpy(&x->id, &ua->id, sizeof(ua->id));
2185 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2186 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2187 xp->mark.m = x->mark.m = mark.m;
2188 xp->mark.v = x->mark.v = mark.v;
2190 /* extract the templates and for each call km_key */
2191 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2192 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2193 memcpy(&x->id, &t->id, sizeof(x->id));
2194 x->props.mode = t->mode;
2195 x->props.reqid = t->reqid;
2196 x->props.family = ut->family;
2197 t->aalgos = ua->aalgos;
2198 t->ealgos = ua->ealgos;
2199 t->calgos = ua->calgos;
2200 err = km_query(x, t, xp);
2210 WARN(1, "BAD policy passed\n");
2217 #ifdef CONFIG_XFRM_MIGRATE
2218 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2219 struct xfrm_kmaddress *k,
2220 struct nlattr **attrs, int *num)
2222 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2223 struct xfrm_user_migrate *um;
2227 struct xfrm_user_kmaddress *uk;
2229 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2230 memcpy(&k->local, &uk->local, sizeof(k->local));
2231 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2232 k->family = uk->family;
2233 k->reserved = uk->reserved;
2237 num_migrate = nla_len(rt) / sizeof(*um);
2239 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2242 for (i = 0; i < num_migrate; i++, um++, ma++) {
2243 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2244 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2245 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2246 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2248 ma->proto = um->proto;
2249 ma->mode = um->mode;
2250 ma->reqid = um->reqid;
2252 ma->old_family = um->old_family;
2253 ma->new_family = um->new_family;
2260 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2261 struct nlattr **attrs)
2263 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2264 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2265 struct xfrm_kmaddress km, *kmp;
2269 struct net *net = sock_net(skb->sk);
2271 err = verify_policy_dir(pi->dir);
2275 if (attrs[XFRMA_MIGRATE] == NULL)
2278 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2280 err = copy_from_user_policy_type(&type, attrs);
2284 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2291 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2296 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2297 struct nlattr **attrs)
2299 return -ENOPROTOOPT;
2303 #ifdef CONFIG_XFRM_MIGRATE
2304 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2306 struct xfrm_user_migrate um;
2308 memset(&um, 0, sizeof(um));
2309 um.proto = m->proto;
2311 um.reqid = m->reqid;
2312 um.old_family = m->old_family;
2313 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2314 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2315 um.new_family = m->new_family;
2316 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2317 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2319 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2322 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2324 struct xfrm_user_kmaddress uk;
2326 memset(&uk, 0, sizeof(uk));
2327 uk.family = k->family;
2328 uk.reserved = k->reserved;
2329 memcpy(&uk.local, &k->local, sizeof(uk.local));
2330 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2332 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2335 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2337 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2338 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2339 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2340 + userpolicy_type_attrsize();
2343 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2344 int num_migrate, const struct xfrm_kmaddress *k,
2345 const struct xfrm_selector *sel, u8 dir, u8 type)
2347 const struct xfrm_migrate *mp;
2348 struct xfrm_userpolicy_id *pol_id;
2349 struct nlmsghdr *nlh;
2352 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2356 pol_id = nlmsg_data(nlh);
2357 /* copy data from selector, dir, and type to the pol_id */
2358 memset(pol_id, 0, sizeof(*pol_id));
2359 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2363 err = copy_to_user_kmaddress(k, skb);
2367 err = copy_to_user_policy_type(type, skb);
2370 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2371 err = copy_to_user_migrate(mp, skb);
2376 nlmsg_end(skb, nlh);
2380 nlmsg_cancel(skb, nlh);
2384 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2385 const struct xfrm_migrate *m, int num_migrate,
2386 const struct xfrm_kmaddress *k)
2388 struct net *net = &init_net;
2389 struct sk_buff *skb;
2392 err = verify_policy_dir(dir);
2396 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2401 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2404 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2407 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2408 const struct xfrm_migrate *m, int num_migrate,
2409 const struct xfrm_kmaddress *k)
2411 return -ENOPROTOOPT;
2415 #define XMSGSIZE(type) sizeof(struct type)
2417 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2418 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2419 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2420 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2421 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2422 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2423 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2424 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2425 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2426 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2427 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2428 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2429 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2430 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2431 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2432 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2433 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2434 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2435 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2436 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2437 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2438 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2443 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2444 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2445 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2446 [XFRMA_LASTUSED] = { .type = NLA_U64},
2447 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2448 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2449 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2450 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2451 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2452 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2453 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2454 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2455 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2456 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2457 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2458 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2459 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2460 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2461 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2462 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2463 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2464 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2465 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2466 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2467 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2468 [XFRMA_PROTO] = { .type = NLA_U8 },
2469 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2470 [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 },
2473 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2474 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2475 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2478 static const struct xfrm_link {
2479 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2480 int (*start)(struct netlink_callback *);
2481 int (*dump)(struct sk_buff *, struct netlink_callback *);
2482 int (*done)(struct netlink_callback *);
2483 const struct nla_policy *nla_pol;
2485 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2486 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2487 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2488 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2489 .dump = xfrm_dump_sa,
2490 .done = xfrm_dump_sa_done },
2491 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2492 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2493 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2494 .start = xfrm_dump_policy_start,
2495 .dump = xfrm_dump_policy,
2496 .done = xfrm_dump_policy_done },
2497 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2498 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2499 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2500 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2501 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2502 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2503 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2504 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2505 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2506 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2507 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2508 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2509 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2510 .nla_pol = xfrma_spd_policy,
2511 .nla_max = XFRMA_SPD_MAX },
2512 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2515 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2517 struct net *net = sock_net(skb->sk);
2518 struct nlattr *attrs[XFRMA_MAX+1];
2519 const struct xfrm_link *link;
2522 #ifdef CONFIG_COMPAT
2523 if (is_compat_task())
2527 type = nlh->nlmsg_type;
2528 if (type > XFRM_MSG_MAX)
2531 type -= XFRM_MSG_BASE;
2532 link = &xfrm_dispatch[type];
2534 /* All operations require privileges, even GET */
2535 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2538 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2539 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2540 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2541 if (link->dump == NULL)
2545 struct netlink_dump_control c = {
2546 .start = link->start,
2550 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2554 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2555 link->nla_max ? : XFRMA_MAX,
2556 link->nla_pol ? : xfrma_policy);
2560 if (link->doit == NULL)
2563 return link->doit(skb, nlh, attrs);
2566 static void xfrm_netlink_rcv(struct sk_buff *skb)
2568 struct net *net = sock_net(skb->sk);
2570 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2571 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2572 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2575 static inline size_t xfrm_expire_msgsize(void)
2577 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2578 + nla_total_size(sizeof(struct xfrm_mark));
2581 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2583 struct xfrm_user_expire *ue;
2584 struct nlmsghdr *nlh;
2587 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2591 ue = nlmsg_data(nlh);
2592 copy_to_user_state(x, &ue->state);
2593 ue->hard = (c->data.hard != 0) ? 1 : 0;
2595 err = xfrm_mark_put(skb, &x->mark);
2599 nlmsg_end(skb, nlh);
2603 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2605 struct net *net = xs_net(x);
2606 struct sk_buff *skb;
2608 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2612 if (build_expire(skb, x, c) < 0) {
2617 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2620 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2622 struct net *net = xs_net(x);
2623 struct sk_buff *skb;
2625 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2629 if (build_aevent(skb, x, c) < 0)
2632 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2635 static int xfrm_notify_sa_flush(const struct km_event *c)
2637 struct net *net = c->net;
2638 struct xfrm_usersa_flush *p;
2639 struct nlmsghdr *nlh;
2640 struct sk_buff *skb;
2641 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2643 skb = nlmsg_new(len, GFP_ATOMIC);
2647 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2653 p = nlmsg_data(nlh);
2654 p->proto = c->data.proto;
2656 nlmsg_end(skb, nlh);
2658 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2661 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2665 l += nla_total_size(aead_len(x->aead));
2667 l += nla_total_size(sizeof(struct xfrm_algo) +
2668 (x->aalg->alg_key_len + 7) / 8);
2669 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2672 l += nla_total_size(xfrm_alg_len(x->ealg));
2674 l += nla_total_size(sizeof(*x->calg));
2676 l += nla_total_size(sizeof(*x->encap));
2678 l += nla_total_size(sizeof(x->tfcpad));
2680 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2682 l += nla_total_size(sizeof(struct xfrm_replay_state));
2684 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2685 x->security->ctx_len);
2687 l += nla_total_size(sizeof(*x->coaddr));
2688 if (x->props.extra_flags)
2689 l += nla_total_size(sizeof(x->props.extra_flags));
2690 if (x->props.output_mark)
2691 l += nla_total_size(sizeof(x->props.output_mark));
2693 /* Must count x->lastused as it may become non-zero behind our back. */
2694 l += nla_total_size(sizeof(u64));
2699 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2701 struct net *net = xs_net(x);
2702 struct xfrm_usersa_info *p;
2703 struct xfrm_usersa_id *id;
2704 struct nlmsghdr *nlh;
2705 struct sk_buff *skb;
2706 int len = xfrm_sa_len(x);
2709 headlen = sizeof(*p);
2710 if (c->event == XFRM_MSG_DELSA) {
2711 len += nla_total_size(headlen);
2712 headlen = sizeof(*id);
2713 len += nla_total_size(sizeof(struct xfrm_mark));
2715 len += NLMSG_ALIGN(headlen);
2717 skb = nlmsg_new(len, GFP_ATOMIC);
2721 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2726 p = nlmsg_data(nlh);
2727 if (c->event == XFRM_MSG_DELSA) {
2728 struct nlattr *attr;
2730 id = nlmsg_data(nlh);
2731 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2732 id->spi = x->id.spi;
2733 id->family = x->props.family;
2734 id->proto = x->id.proto;
2736 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2743 err = copy_to_user_state_extra(x, p, skb);
2747 nlmsg_end(skb, nlh);
2749 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2756 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2760 case XFRM_MSG_EXPIRE:
2761 return xfrm_exp_state_notify(x, c);
2762 case XFRM_MSG_NEWAE:
2763 return xfrm_aevent_state_notify(x, c);
2764 case XFRM_MSG_DELSA:
2765 case XFRM_MSG_UPDSA:
2766 case XFRM_MSG_NEWSA:
2767 return xfrm_notify_sa(x, c);
2768 case XFRM_MSG_FLUSHSA:
2769 return xfrm_notify_sa_flush(c);
2771 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2780 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2781 struct xfrm_policy *xp)
2783 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2784 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2785 + nla_total_size(sizeof(struct xfrm_mark))
2786 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2787 + userpolicy_type_attrsize();
2790 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2791 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2793 __u32 seq = xfrm_get_acqseq();
2794 struct xfrm_user_acquire *ua;
2795 struct nlmsghdr *nlh;
2798 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2802 ua = nlmsg_data(nlh);
2803 memcpy(&ua->id, &x->id, sizeof(ua->id));
2804 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2805 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2806 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2807 ua->aalgos = xt->aalgos;
2808 ua->ealgos = xt->ealgos;
2809 ua->calgos = xt->calgos;
2810 ua->seq = x->km.seq = seq;
2812 err = copy_to_user_tmpl(xp, skb);
2814 err = copy_to_user_state_sec_ctx(x, skb);
2816 err = copy_to_user_policy_type(xp->type, skb);
2818 err = xfrm_mark_put(skb, &xp->mark);
2820 nlmsg_cancel(skb, nlh);
2824 nlmsg_end(skb, nlh);
2828 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2829 struct xfrm_policy *xp)
2831 struct net *net = xs_net(x);
2832 struct sk_buff *skb;
2834 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2838 if (build_acquire(skb, x, xt, xp) < 0)
2841 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2844 /* User gives us xfrm_user_policy_info followed by an array of 0
2845 * or more templates.
2847 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2848 u8 *data, int len, int *dir)
2850 struct net *net = sock_net(sk);
2851 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2852 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2853 struct xfrm_policy *xp;
2856 switch (sk->sk_family) {
2858 if (opt != IP_XFRM_POLICY) {
2863 #if IS_ENABLED(CONFIG_IPV6)
2865 if (opt != IPV6_XFRM_POLICY) {
2878 if (len < sizeof(*p) ||
2879 verify_newpolicy_info(p))
2882 nr = ((len - sizeof(*p)) / sizeof(*ut));
2883 if (validate_tmpl(nr, ut, p->sel.family))
2886 if (p->dir > XFRM_POLICY_OUT)
2889 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2895 copy_from_user_policy(xp, p);
2896 xp->type = XFRM_POLICY_TYPE_MAIN;
2897 copy_templates(xp, ut, nr);
2904 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2906 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2907 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2908 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2909 + nla_total_size(sizeof(struct xfrm_mark))
2910 + userpolicy_type_attrsize();
2913 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2914 int dir, const struct km_event *c)
2916 struct xfrm_user_polexpire *upe;
2917 int hard = c->data.hard;
2918 struct nlmsghdr *nlh;
2921 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2925 upe = nlmsg_data(nlh);
2926 copy_to_user_policy(xp, &upe->pol, dir);
2927 err = copy_to_user_tmpl(xp, skb);
2929 err = copy_to_user_sec_ctx(xp, skb);
2931 err = copy_to_user_policy_type(xp->type, skb);
2933 err = xfrm_mark_put(skb, &xp->mark);
2935 nlmsg_cancel(skb, nlh);
2940 nlmsg_end(skb, nlh);
2944 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2946 struct net *net = xp_net(xp);
2947 struct sk_buff *skb;
2949 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2953 if (build_polexpire(skb, xp, dir, c) < 0)
2956 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2959 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2961 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2962 struct net *net = xp_net(xp);
2963 struct xfrm_userpolicy_info *p;
2964 struct xfrm_userpolicy_id *id;
2965 struct nlmsghdr *nlh;
2966 struct sk_buff *skb;
2969 headlen = sizeof(*p);
2970 if (c->event == XFRM_MSG_DELPOLICY) {
2971 len += nla_total_size(headlen);
2972 headlen = sizeof(*id);
2974 len += userpolicy_type_attrsize();
2975 len += nla_total_size(sizeof(struct xfrm_mark));
2976 len += NLMSG_ALIGN(headlen);
2978 skb = nlmsg_new(len, GFP_ATOMIC);
2982 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2987 p = nlmsg_data(nlh);
2988 if (c->event == XFRM_MSG_DELPOLICY) {
2989 struct nlattr *attr;
2991 id = nlmsg_data(nlh);
2992 memset(id, 0, sizeof(*id));
2995 id->index = xp->index;
2997 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2999 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3007 copy_to_user_policy(xp, p, dir);
3008 err = copy_to_user_tmpl(xp, skb);
3010 err = copy_to_user_policy_type(xp->type, skb);
3012 err = xfrm_mark_put(skb, &xp->mark);
3016 nlmsg_end(skb, nlh);
3018 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3025 static int xfrm_notify_policy_flush(const struct km_event *c)
3027 struct net *net = c->net;
3028 struct nlmsghdr *nlh;
3029 struct sk_buff *skb;
3032 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3036 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3040 err = copy_to_user_policy_type(c->data.type, skb);
3044 nlmsg_end(skb, nlh);
3046 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3053 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3057 err = verify_policy_dir(dir);
3062 case XFRM_MSG_NEWPOLICY:
3063 case XFRM_MSG_UPDPOLICY:
3064 case XFRM_MSG_DELPOLICY:
3065 return xfrm_notify_policy(xp, dir, c);
3066 case XFRM_MSG_FLUSHPOLICY:
3067 return xfrm_notify_policy_flush(c);
3068 case XFRM_MSG_POLEXPIRE:
3069 return xfrm_exp_policy_notify(xp, dir, c);
3071 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3079 static inline size_t xfrm_report_msgsize(void)
3081 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3084 static int build_report(struct sk_buff *skb, u8 proto,
3085 struct xfrm_selector *sel, xfrm_address_t *addr)
3087 struct xfrm_user_report *ur;
3088 struct nlmsghdr *nlh;
3090 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3094 ur = nlmsg_data(nlh);
3096 memcpy(&ur->sel, sel, sizeof(ur->sel));
3099 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3101 nlmsg_cancel(skb, nlh);
3105 nlmsg_end(skb, nlh);
3109 static int xfrm_send_report(struct net *net, u8 proto,
3110 struct xfrm_selector *sel, xfrm_address_t *addr)
3112 struct sk_buff *skb;
3114 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3118 if (build_report(skb, proto, sel, addr) < 0)
3121 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3124 static inline size_t xfrm_mapping_msgsize(void)
3126 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3129 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3130 xfrm_address_t *new_saddr, __be16 new_sport)
3132 struct xfrm_user_mapping *um;
3133 struct nlmsghdr *nlh;
3135 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3139 um = nlmsg_data(nlh);
3141 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3142 um->id.spi = x->id.spi;
3143 um->id.family = x->props.family;
3144 um->id.proto = x->id.proto;
3145 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3146 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3147 um->new_sport = new_sport;
3148 um->old_sport = x->encap->encap_sport;
3149 um->reqid = x->props.reqid;
3151 nlmsg_end(skb, nlh);
3155 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3158 struct net *net = xs_net(x);
3159 struct sk_buff *skb;
3161 if (x->id.proto != IPPROTO_ESP)
3167 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3171 if (build_mapping(skb, x, ipaddr, sport) < 0)
3174 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3177 static bool xfrm_is_alive(const struct km_event *c)
3179 return (bool)xfrm_acquire_is_on(c->net);
3182 static struct xfrm_mgr netlink_mgr = {
3184 .notify = xfrm_send_state_notify,
3185 .acquire = xfrm_send_acquire,
3186 .compile_policy = xfrm_compile_policy,
3187 .notify_policy = xfrm_send_policy_notify,
3188 .report = xfrm_send_report,
3189 .migrate = xfrm_send_migrate,
3190 .new_mapping = xfrm_send_mapping,
3191 .is_alive = xfrm_is_alive,
3194 static int __net_init xfrm_user_net_init(struct net *net)
3197 struct netlink_kernel_cfg cfg = {
3198 .groups = XFRMNLGRP_MAX,
3199 .input = xfrm_netlink_rcv,
3202 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3205 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3206 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3210 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3213 list_for_each_entry(net, net_exit_list, exit_list)
3214 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3216 list_for_each_entry(net, net_exit_list, exit_list)
3217 netlink_kernel_release(net->xfrm.nlsk_stash);
3220 static struct pernet_operations xfrm_user_net_ops = {
3221 .init = xfrm_user_net_init,
3222 .exit_batch = xfrm_user_net_exit,
3225 static int __init xfrm_user_init(void)
3229 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3231 rv = register_pernet_subsys(&xfrm_user_net_ops);
3234 rv = xfrm_register_km(&netlink_mgr);
3236 unregister_pernet_subsys(&xfrm_user_net_ops);
3240 static void __exit xfrm_user_exit(void)
3242 xfrm_unregister_km(&netlink_mgr);
3243 unregister_pernet_subsys(&xfrm_user_net_ops);
3246 module_init(xfrm_user_init);
3247 module_exit(xfrm_user_exit);
3248 MODULE_LICENSE("GPL");
3249 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);