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>
35 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
37 struct nlattr *rt = attrs[type];
38 struct xfrm_algo *algp;
44 if (nla_len(rt) < xfrm_alg_len(algp))
57 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
61 static int verify_auth_trunc(struct nlattr **attrs)
63 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
64 struct xfrm_algo_auth *algp;
70 if (nla_len(rt) < xfrm_alg_auth_len(algp))
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
77 static int verify_aead(struct nlattr **attrs)
79 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
80 struct xfrm_algo_aead *algp;
86 if (nla_len(rt) < aead_len(algp))
89 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
93 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
94 xfrm_address_t **addrp)
96 struct nlattr *rt = attrs[type];
99 *addrp = nla_data(rt);
102 static inline int verify_sec_ctx_len(struct nlattr **attrs)
104 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
105 struct xfrm_user_sec_ctx *uctx;
111 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
117 static inline int verify_replay(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
120 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
121 struct xfrm_replay_state_esn *rs;
123 if (p->flags & XFRM_STATE_ESN) {
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))
140 /* As only ESP and AH support ESN feature. */
141 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
144 if (p->replay_window != 0)
150 static int verify_newsa_info(struct xfrm_usersa_info *p,
151 struct nlattr **attrs)
161 #if IS_ENABLED(CONFIG_IPV6)
173 switch (p->id.proto) {
175 if ((!attrs[XFRMA_ALG_AUTH] &&
176 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
177 attrs[XFRMA_ALG_AEAD] ||
178 attrs[XFRMA_ALG_CRYPT] ||
179 attrs[XFRMA_ALG_COMP] ||
180 attrs[XFRMA_TFCPAD] ||
181 (ntohl(p->id.spi) >= 0x10000))
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] ||
214 #if IS_ENABLED(CONFIG_IPV6)
215 case IPPROTO_DSTOPTS:
216 case IPPROTO_ROUTING:
217 if (attrs[XFRMA_ALG_COMP] ||
218 attrs[XFRMA_ALG_AUTH] ||
219 attrs[XFRMA_ALG_AUTH_TRUNC] ||
220 attrs[XFRMA_ALG_AEAD] ||
221 attrs[XFRMA_ALG_CRYPT] ||
222 attrs[XFRMA_ENCAP] ||
223 attrs[XFRMA_SEC_CTX] ||
224 attrs[XFRMA_TFCPAD] ||
225 !attrs[XFRMA_COADDR])
234 if ((err = verify_aead(attrs)))
236 if ((err = verify_auth_trunc(attrs)))
238 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
240 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
242 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
244 if ((err = verify_sec_ctx_len(attrs)))
246 if ((err = verify_replay(p, attrs)))
251 case XFRM_MODE_TRANSPORT:
252 case XFRM_MODE_TUNNEL:
253 case XFRM_MODE_ROUTEOPTIMIZATION:
267 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
268 struct xfrm_algo_desc *(*get_byname)(const char *, int),
271 struct xfrm_algo *p, *ualg;
272 struct xfrm_algo_desc *algo;
277 ualg = nla_data(rta);
279 algo = get_byname(ualg->alg_name, 1);
282 *props = algo->desc.sadb_alg_id;
284 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
288 strcpy(p->alg_name, algo->name);
293 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
296 struct xfrm_algo *ualg;
297 struct xfrm_algo_auth *p;
298 struct xfrm_algo_desc *algo;
303 ualg = nla_data(rta);
305 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
308 *props = algo->desc.sadb_alg_id;
310 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
314 strcpy(p->alg_name, algo->name);
315 p->alg_key_len = ualg->alg_key_len;
316 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
317 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
323 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
326 struct xfrm_algo_auth *p, *ualg;
327 struct xfrm_algo_desc *algo;
332 ualg = nla_data(rta);
334 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
337 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
338 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
340 *props = algo->desc.sadb_alg_id;
342 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
346 strcpy(p->alg_name, algo->name);
347 if (!p->alg_trunc_len)
348 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
354 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
357 struct xfrm_algo_aead *p, *ualg;
358 struct xfrm_algo_desc *algo;
363 ualg = nla_data(rta);
365 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
368 *props = algo->desc.sadb_alg_id;
370 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
374 strcpy(p->alg_name, algo->name);
379 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
382 struct xfrm_replay_state_esn *up;
385 if (!replay_esn || !rp)
389 ulen = xfrm_replay_state_esn_len(up);
391 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
397 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
398 struct xfrm_replay_state_esn **preplay_esn,
401 struct xfrm_replay_state_esn *p, *pp, *up;
408 klen = xfrm_replay_state_esn_len(up);
409 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
411 p = kzalloc(klen, GFP_KERNEL);
415 pp = kzalloc(klen, GFP_KERNEL);
422 memcpy(pp, up, ulen);
430 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
435 len += sizeof(struct xfrm_user_sec_ctx);
436 len += xfrm_ctx->ctx_len;
441 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
443 memcpy(&x->id, &p->id, sizeof(x->id));
444 memcpy(&x->sel, &p->sel, sizeof(x->sel));
445 memcpy(&x->lft, &p->lft, sizeof(x->lft));
446 x->props.mode = p->mode;
447 x->props.replay_window = min_t(unsigned int, p->replay_window,
448 sizeof(x->replay.bitmap) * 8);
449 x->props.reqid = p->reqid;
450 x->props.family = p->family;
451 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
452 x->props.flags = p->flags;
454 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
455 x->sel.family = p->family;
459 * someday when pfkey also has support, we could have the code
460 * somehow made shareable and move it to xfrm_state.c - JHS
463 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
466 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
467 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
468 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
469 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
470 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
473 struct xfrm_replay_state_esn *replay_esn;
474 replay_esn = nla_data(re);
475 memcpy(x->replay_esn, replay_esn,
476 xfrm_replay_state_esn_len(replay_esn));
477 memcpy(x->preplay_esn, replay_esn,
478 xfrm_replay_state_esn_len(replay_esn));
482 struct xfrm_replay_state *replay;
483 replay = nla_data(rp);
484 memcpy(&x->replay, replay, sizeof(*replay));
485 memcpy(&x->preplay, replay, sizeof(*replay));
489 struct xfrm_lifetime_cur *ltime;
490 ltime = nla_data(lt);
491 x->curlft.bytes = ltime->bytes;
492 x->curlft.packets = ltime->packets;
493 x->curlft.add_time = ltime->add_time;
494 x->curlft.use_time = ltime->use_time;
498 x->replay_maxage = nla_get_u32(et);
501 x->replay_maxdiff = nla_get_u32(rt);
504 static struct xfrm_state *xfrm_state_construct(struct net *net,
505 struct xfrm_usersa_info *p,
506 struct nlattr **attrs,
509 struct xfrm_state *x = xfrm_state_alloc(net);
515 copy_from_user_state(x, p);
517 if (attrs[XFRMA_SA_EXTRA_FLAGS])
518 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
520 if ((err = attach_aead(&x->aead, &x->props.ealgo,
521 attrs[XFRMA_ALG_AEAD])))
523 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
524 attrs[XFRMA_ALG_AUTH_TRUNC])))
526 if (!x->props.aalgo) {
527 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
528 attrs[XFRMA_ALG_AUTH])))
531 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
532 xfrm_ealg_get_byname,
533 attrs[XFRMA_ALG_CRYPT])))
535 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
536 xfrm_calg_get_byname,
537 attrs[XFRMA_ALG_COMP])))
540 if (attrs[XFRMA_ENCAP]) {
541 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
542 sizeof(*x->encap), GFP_KERNEL);
543 if (x->encap == NULL)
547 if (attrs[XFRMA_TFCPAD])
548 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
550 if (attrs[XFRMA_COADDR]) {
551 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
552 sizeof(*x->coaddr), GFP_KERNEL);
553 if (x->coaddr == NULL)
557 xfrm_mark_get(attrs, &x->mark);
559 err = __xfrm_init_state(x, false);
563 if (attrs[XFRMA_SEC_CTX] &&
564 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
567 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
568 attrs[XFRMA_REPLAY_ESN_VAL])))
572 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
573 /* sysctl_xfrm_aevent_etime is in 100ms units */
574 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
576 if ((err = xfrm_init_replay(x)))
579 /* override default values from above */
580 xfrm_update_ae_params(x, attrs, 0);
585 x->km.state = XFRM_STATE_DEAD;
592 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
593 struct nlattr **attrs)
595 struct net *net = sock_net(skb->sk);
596 struct xfrm_usersa_info *p = nlmsg_data(nlh);
597 struct xfrm_state *x;
601 err = verify_newsa_info(p, attrs);
605 x = xfrm_state_construct(net, p, attrs, &err);
610 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
611 err = xfrm_state_add(x);
613 err = xfrm_state_update(x);
615 xfrm_audit_state_add(x, err ? 0 : 1, true);
618 x->km.state = XFRM_STATE_DEAD;
623 c.seq = nlh->nlmsg_seq;
624 c.portid = nlh->nlmsg_pid;
625 c.event = nlh->nlmsg_type;
627 km_state_notify(x, &c);
633 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
634 struct xfrm_usersa_id *p,
635 struct nlattr **attrs,
638 struct xfrm_state *x = NULL;
641 u32 mark = xfrm_mark_get(attrs, &m);
643 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
645 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
647 xfrm_address_t *saddr = NULL;
649 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
656 x = xfrm_state_lookup_byaddr(net, mark,
658 p->proto, p->family);
667 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
668 struct nlattr **attrs)
670 struct net *net = sock_net(skb->sk);
671 struct xfrm_state *x;
674 struct xfrm_usersa_id *p = nlmsg_data(nlh);
676 x = xfrm_user_state_lookup(net, p, attrs, &err);
680 if ((err = security_xfrm_state_delete(x)) != 0)
683 if (xfrm_state_kern(x)) {
688 err = xfrm_state_delete(x);
693 c.seq = nlh->nlmsg_seq;
694 c.portid = nlh->nlmsg_pid;
695 c.event = nlh->nlmsg_type;
696 km_state_notify(x, &c);
699 xfrm_audit_state_delete(x, err ? 0 : 1, true);
704 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
706 memset(p, 0, sizeof(*p));
707 memcpy(&p->id, &x->id, sizeof(p->id));
708 memcpy(&p->sel, &x->sel, sizeof(p->sel));
709 memcpy(&p->lft, &x->lft, sizeof(p->lft));
710 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
711 memcpy(&p->stats, &x->stats, sizeof(p->stats));
712 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
713 p->mode = x->props.mode;
714 p->replay_window = x->props.replay_window;
715 p->reqid = x->props.reqid;
716 p->family = x->props.family;
717 p->flags = x->props.flags;
721 struct xfrm_dump_info {
722 struct sk_buff *in_skb;
723 struct sk_buff *out_skb;
728 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
730 struct xfrm_user_sec_ctx *uctx;
732 int ctx_size = sizeof(*uctx) + s->ctx_len;
734 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
738 uctx = nla_data(attr);
739 uctx->exttype = XFRMA_SEC_CTX;
740 uctx->len = ctx_size;
741 uctx->ctx_doi = s->ctx_doi;
742 uctx->ctx_alg = s->ctx_alg;
743 uctx->ctx_len = s->ctx_len;
744 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
749 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
751 struct xfrm_algo *algo;
754 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
755 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
759 algo = nla_data(nla);
760 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
761 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
762 algo->alg_key_len = auth->alg_key_len;
767 /* Don't change this without updating xfrm_sa_len! */
768 static int copy_to_user_state_extra(struct xfrm_state *x,
769 struct xfrm_usersa_info *p,
774 copy_to_user_state(x, p);
776 if (x->props.extra_flags) {
777 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
778 x->props.extra_flags);
784 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
789 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
794 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
799 ret = copy_to_user_auth(x->aalg, skb);
801 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
802 xfrm_alg_auth_len(x->aalg), x->aalg);
807 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
812 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
817 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
822 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
826 ret = xfrm_mark_put(skb, &x->mark);
830 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
831 xfrm_replay_state_esn_len(x->replay_esn),
837 ret = copy_sec_ctx(x->security, skb);
842 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
844 struct xfrm_dump_info *sp = ptr;
845 struct sk_buff *in_skb = sp->in_skb;
846 struct sk_buff *skb = sp->out_skb;
847 struct xfrm_usersa_info *p;
848 struct nlmsghdr *nlh;
851 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
852 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
858 err = copy_to_user_state_extra(x, p, skb);
860 nlmsg_cancel(skb, nlh);
867 static int xfrm_dump_sa_done(struct netlink_callback *cb)
869 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
870 struct sock *sk = cb->skb->sk;
871 struct net *net = sock_net(sk);
873 xfrm_state_walk_done(walk, net);
877 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
878 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
880 struct net *net = sock_net(skb->sk);
881 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
882 struct xfrm_dump_info info;
884 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
885 sizeof(cb->args) - sizeof(cb->args[0]));
887 info.in_skb = cb->skb;
889 info.nlmsg_seq = cb->nlh->nlmsg_seq;
890 info.nlmsg_flags = NLM_F_MULTI;
893 struct nlattr *attrs[XFRMA_MAX+1];
894 struct xfrm_address_filter *filter = NULL;
900 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
905 if (attrs[XFRMA_ADDRESS_FILTER]) {
906 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
910 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
914 if (attrs[XFRMA_PROTO])
915 proto = nla_get_u8(attrs[XFRMA_PROTO]);
917 xfrm_state_walk_init(walk, proto, filter);
920 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
925 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
926 struct xfrm_state *x, u32 seq)
928 struct xfrm_dump_info info;
932 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
934 return ERR_PTR(-ENOMEM);
936 info.in_skb = in_skb;
938 info.nlmsg_seq = seq;
939 info.nlmsg_flags = 0;
941 err = dump_one_state(x, 0, &info);
950 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
951 * Must be called with RCU read lock.
953 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
954 u32 pid, unsigned int group)
956 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
959 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
964 static inline size_t xfrm_spdinfo_msgsize(void)
966 return NLMSG_ALIGN(4)
967 + nla_total_size(sizeof(struct xfrmu_spdinfo))
968 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
971 static int build_spdinfo(struct sk_buff *skb, struct net *net,
972 u32 portid, u32 seq, u32 flags)
974 struct xfrmk_spdinfo si;
975 struct xfrmu_spdinfo spc;
976 struct xfrmu_spdhinfo sph;
977 struct nlmsghdr *nlh;
981 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
982 if (nlh == NULL) /* shouldn't really happen ... */
987 xfrm_spd_getinfo(net, &si);
988 spc.incnt = si.incnt;
989 spc.outcnt = si.outcnt;
990 spc.fwdcnt = si.fwdcnt;
991 spc.inscnt = si.inscnt;
992 spc.outscnt = si.outscnt;
993 spc.fwdscnt = si.fwdscnt;
994 sph.spdhcnt = si.spdhcnt;
995 sph.spdhmcnt = si.spdhmcnt;
997 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
999 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1001 nlmsg_cancel(skb, nlh);
1005 return nlmsg_end(skb, nlh);
1008 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1009 struct nlattr **attrs)
1011 struct net *net = sock_net(skb->sk);
1012 struct sk_buff *r_skb;
1013 u32 *flags = nlmsg_data(nlh);
1014 u32 sportid = NETLINK_CB(skb).portid;
1015 u32 seq = nlh->nlmsg_seq;
1017 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1021 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1024 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1027 static inline size_t xfrm_sadinfo_msgsize(void)
1029 return NLMSG_ALIGN(4)
1030 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1031 + nla_total_size(4); /* XFRMA_SAD_CNT */
1034 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1035 u32 portid, u32 seq, u32 flags)
1037 struct xfrmk_sadinfo si;
1038 struct xfrmu_sadhinfo sh;
1039 struct nlmsghdr *nlh;
1043 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1044 if (nlh == NULL) /* shouldn't really happen ... */
1047 f = nlmsg_data(nlh);
1049 xfrm_sad_getinfo(net, &si);
1051 sh.sadhmcnt = si.sadhmcnt;
1052 sh.sadhcnt = si.sadhcnt;
1054 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1056 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1058 nlmsg_cancel(skb, nlh);
1062 return nlmsg_end(skb, nlh);
1065 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1066 struct nlattr **attrs)
1068 struct net *net = sock_net(skb->sk);
1069 struct sk_buff *r_skb;
1070 u32 *flags = nlmsg_data(nlh);
1071 u32 sportid = NETLINK_CB(skb).portid;
1072 u32 seq = nlh->nlmsg_seq;
1074 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1078 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1081 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1084 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1085 struct nlattr **attrs)
1087 struct net *net = sock_net(skb->sk);
1088 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1089 struct xfrm_state *x;
1090 struct sk_buff *resp_skb;
1093 x = xfrm_user_state_lookup(net, p, attrs, &err);
1097 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1098 if (IS_ERR(resp_skb)) {
1099 err = PTR_ERR(resp_skb);
1101 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1108 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1109 struct nlattr **attrs)
1111 struct net *net = sock_net(skb->sk);
1112 struct xfrm_state *x;
1113 struct xfrm_userspi_info *p;
1114 struct sk_buff *resp_skb;
1115 xfrm_address_t *daddr;
1121 p = nlmsg_data(nlh);
1122 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1126 family = p->info.family;
1127 daddr = &p->info.id.daddr;
1131 mark = xfrm_mark_get(attrs, &m);
1133 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1134 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1141 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1142 p->info.id.proto, daddr,
1149 err = xfrm_alloc_spi(x, p->min, p->max);
1153 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1154 if (IS_ERR(resp_skb)) {
1155 err = PTR_ERR(resp_skb);
1159 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1167 static int verify_policy_dir(u8 dir)
1170 case XFRM_POLICY_IN:
1171 case XFRM_POLICY_OUT:
1172 case XFRM_POLICY_FWD:
1182 static int verify_policy_type(u8 type)
1185 case XFRM_POLICY_TYPE_MAIN:
1186 #ifdef CONFIG_XFRM_SUB_POLICY
1187 case XFRM_POLICY_TYPE_SUB:
1198 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1203 case XFRM_SHARE_ANY:
1204 case XFRM_SHARE_SESSION:
1205 case XFRM_SHARE_USER:
1206 case XFRM_SHARE_UNIQUE:
1213 switch (p->action) {
1214 case XFRM_POLICY_ALLOW:
1215 case XFRM_POLICY_BLOCK:
1222 switch (p->sel.family) {
1227 #if IS_ENABLED(CONFIG_IPV6)
1230 return -EAFNOSUPPORT;
1237 ret = verify_policy_dir(p->dir);
1240 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1246 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1248 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1249 struct xfrm_user_sec_ctx *uctx;
1254 uctx = nla_data(rt);
1255 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1258 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1264 for (i = 0; i < nr; i++, ut++) {
1265 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1267 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1268 memcpy(&t->saddr, &ut->saddr,
1269 sizeof(xfrm_address_t));
1270 t->reqid = ut->reqid;
1272 t->share = ut->share;
1273 t->optional = ut->optional;
1274 t->aalgos = ut->aalgos;
1275 t->ealgos = ut->ealgos;
1276 t->calgos = ut->calgos;
1277 /* If all masks are ~0, then we allow all algorithms. */
1278 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1279 t->encap_family = ut->family;
1283 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1287 if (nr > XFRM_MAX_DEPTH)
1290 for (i = 0; i < nr; i++) {
1291 /* We never validated the ut->family value, so many
1292 * applications simply leave it at zero. The check was
1293 * never made and ut->family was ignored because all
1294 * templates could be assumed to have the same family as
1295 * the policy itself. Now that we will have ipv4-in-ipv6
1296 * and ipv6-in-ipv4 tunnels, this is no longer true.
1299 ut[i].family = family;
1301 switch (ut[i].family) {
1304 #if IS_ENABLED(CONFIG_IPV6)
1316 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1318 struct nlattr *rt = attrs[XFRMA_TMPL];
1323 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1324 int nr = nla_len(rt) / sizeof(*utmpl);
1327 err = validate_tmpl(nr, utmpl, pol->family);
1331 copy_templates(pol, utmpl, nr);
1336 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1338 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1339 struct xfrm_userpolicy_type *upt;
1340 u8 type = XFRM_POLICY_TYPE_MAIN;
1348 err = verify_policy_type(type);
1356 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1358 xp->priority = p->priority;
1359 xp->index = p->index;
1360 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1361 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1362 xp->action = p->action;
1363 xp->flags = p->flags;
1364 xp->family = p->sel.family;
1365 /* XXX xp->share = p->share; */
1368 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1370 memset(p, 0, sizeof(*p));
1371 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1372 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1373 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1374 p->priority = xp->priority;
1375 p->index = xp->index;
1376 p->sel.family = xp->family;
1378 p->action = xp->action;
1379 p->flags = xp->flags;
1380 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1383 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1385 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1393 copy_from_user_policy(xp, p);
1395 err = copy_from_user_policy_type(&xp->type, attrs);
1399 if (!(err = copy_from_user_tmpl(xp, attrs)))
1400 err = copy_from_user_sec_ctx(xp, attrs);
1404 xfrm_mark_get(attrs, &xp->mark);
1410 xfrm_policy_destroy(xp);
1414 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1415 struct nlattr **attrs)
1417 struct net *net = sock_net(skb->sk);
1418 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1419 struct xfrm_policy *xp;
1424 err = verify_newpolicy_info(p);
1427 err = verify_sec_ctx_len(attrs);
1431 xp = xfrm_policy_construct(net, p, attrs, &err);
1435 /* shouldn't excl be based on nlh flags??
1436 * Aha! this is anti-netlink really i.e more pfkey derived
1437 * in netlink excl is a flag and you wouldnt need
1438 * a type XFRM_MSG_UPDPOLICY - JHS */
1439 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1440 err = xfrm_policy_insert(p->dir, xp, excl);
1441 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1444 security_xfrm_policy_free(xp->security);
1449 c.event = nlh->nlmsg_type;
1450 c.seq = nlh->nlmsg_seq;
1451 c.portid = nlh->nlmsg_pid;
1452 km_policy_notify(xp, p->dir, &c);
1459 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1461 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1464 if (xp->xfrm_nr == 0)
1467 for (i = 0; i < xp->xfrm_nr; i++) {
1468 struct xfrm_user_tmpl *up = &vec[i];
1469 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1471 memset(up, 0, sizeof(*up));
1472 memcpy(&up->id, &kp->id, sizeof(up->id));
1473 up->family = kp->encap_family;
1474 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1475 up->reqid = kp->reqid;
1476 up->mode = kp->mode;
1477 up->share = kp->share;
1478 up->optional = kp->optional;
1479 up->aalgos = kp->aalgos;
1480 up->ealgos = kp->ealgos;
1481 up->calgos = kp->calgos;
1484 return nla_put(skb, XFRMA_TMPL,
1485 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1488 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1491 return copy_sec_ctx(x->security, skb);
1496 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1499 return copy_sec_ctx(xp->security, skb);
1502 static inline size_t userpolicy_type_attrsize(void)
1504 #ifdef CONFIG_XFRM_SUB_POLICY
1505 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1511 #ifdef CONFIG_XFRM_SUB_POLICY
1512 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1514 struct xfrm_userpolicy_type upt = {
1518 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1522 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1528 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1530 struct xfrm_dump_info *sp = ptr;
1531 struct xfrm_userpolicy_info *p;
1532 struct sk_buff *in_skb = sp->in_skb;
1533 struct sk_buff *skb = sp->out_skb;
1534 struct nlmsghdr *nlh;
1537 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1538 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1542 p = nlmsg_data(nlh);
1543 copy_to_user_policy(xp, p, dir);
1544 err = copy_to_user_tmpl(xp, skb);
1546 err = copy_to_user_sec_ctx(xp, skb);
1548 err = copy_to_user_policy_type(xp->type, skb);
1550 err = xfrm_mark_put(skb, &xp->mark);
1552 nlmsg_cancel(skb, nlh);
1555 nlmsg_end(skb, nlh);
1559 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1561 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1562 struct net *net = sock_net(cb->skb->sk);
1564 xfrm_policy_walk_done(walk, net);
1568 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1570 struct net *net = sock_net(skb->sk);
1571 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1572 struct xfrm_dump_info info;
1574 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1575 sizeof(cb->args) - sizeof(cb->args[0]));
1577 info.in_skb = cb->skb;
1579 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1580 info.nlmsg_flags = NLM_F_MULTI;
1584 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1587 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1592 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1593 struct xfrm_policy *xp,
1596 struct xfrm_dump_info info;
1597 struct sk_buff *skb;
1600 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1602 return ERR_PTR(-ENOMEM);
1604 info.in_skb = in_skb;
1606 info.nlmsg_seq = seq;
1607 info.nlmsg_flags = 0;
1609 err = dump_one_policy(xp, dir, 0, &info);
1612 return ERR_PTR(err);
1618 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1619 struct nlattr **attrs)
1621 struct net *net = sock_net(skb->sk);
1622 struct xfrm_policy *xp;
1623 struct xfrm_userpolicy_id *p;
1624 u8 type = XFRM_POLICY_TYPE_MAIN;
1629 u32 mark = xfrm_mark_get(attrs, &m);
1631 p = nlmsg_data(nlh);
1632 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1634 err = copy_from_user_policy_type(&type, attrs);
1638 err = verify_policy_dir(p->dir);
1643 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1645 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1646 struct xfrm_sec_ctx *ctx;
1648 err = verify_sec_ctx_len(attrs);
1654 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1656 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1660 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1662 security_xfrm_policy_free(ctx);
1668 struct sk_buff *resp_skb;
1670 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1671 if (IS_ERR(resp_skb)) {
1672 err = PTR_ERR(resp_skb);
1674 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1675 NETLINK_CB(skb).portid);
1678 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1683 c.data.byid = p->index;
1684 c.event = nlh->nlmsg_type;
1685 c.seq = nlh->nlmsg_seq;
1686 c.portid = nlh->nlmsg_pid;
1687 km_policy_notify(xp, p->dir, &c);
1692 if (delete && err == 0)
1693 xfrm_garbage_collect(net);
1697 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1698 struct nlattr **attrs)
1700 struct net *net = sock_net(skb->sk);
1702 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1705 err = xfrm_state_flush(net, p->proto, true);
1707 if (err == -ESRCH) /* empty table */
1711 c.data.proto = p->proto;
1712 c.event = nlh->nlmsg_type;
1713 c.seq = nlh->nlmsg_seq;
1714 c.portid = nlh->nlmsg_pid;
1716 km_state_notify(NULL, &c);
1721 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1723 size_t replay_size = x->replay_esn ?
1724 xfrm_replay_state_esn_len(x->replay_esn) :
1725 sizeof(struct xfrm_replay_state);
1727 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1728 + nla_total_size(replay_size)
1729 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1730 + nla_total_size(sizeof(struct xfrm_mark))
1731 + nla_total_size(4) /* XFRM_AE_RTHR */
1732 + nla_total_size(4); /* XFRM_AE_ETHR */
1735 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1737 struct xfrm_aevent_id *id;
1738 struct nlmsghdr *nlh;
1741 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1745 id = nlmsg_data(nlh);
1746 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1747 id->sa_id.spi = x->id.spi;
1748 id->sa_id.family = x->props.family;
1749 id->sa_id.proto = x->id.proto;
1750 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1751 id->reqid = x->props.reqid;
1752 id->flags = c->data.aevent;
1754 if (x->replay_esn) {
1755 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1756 xfrm_replay_state_esn_len(x->replay_esn),
1759 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1764 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1768 if (id->flags & XFRM_AE_RTHR) {
1769 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1773 if (id->flags & XFRM_AE_ETHR) {
1774 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1775 x->replay_maxage * 10 / HZ);
1779 err = xfrm_mark_put(skb, &x->mark);
1783 return nlmsg_end(skb, nlh);
1786 nlmsg_cancel(skb, nlh);
1790 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1791 struct nlattr **attrs)
1793 struct net *net = sock_net(skb->sk);
1794 struct xfrm_state *x;
1795 struct sk_buff *r_skb;
1800 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1801 struct xfrm_usersa_id *id = &p->sa_id;
1803 mark = xfrm_mark_get(attrs, &m);
1805 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1809 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1810 if (r_skb == NULL) {
1816 * XXX: is this lock really needed - none of the other
1817 * gets lock (the concern is things getting updated
1818 * while we are still reading) - jhs
1820 spin_lock_bh(&x->lock);
1821 c.data.aevent = p->flags;
1822 c.seq = nlh->nlmsg_seq;
1823 c.portid = nlh->nlmsg_pid;
1825 if (build_aevent(r_skb, x, &c) < 0)
1827 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1828 spin_unlock_bh(&x->lock);
1833 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1834 struct nlattr **attrs)
1836 struct net *net = sock_net(skb->sk);
1837 struct xfrm_state *x;
1842 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1843 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1844 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1845 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1847 if (!lt && !rp && !re)
1850 /* pedantic mode - thou shalt sayeth replaceth */
1851 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1854 mark = xfrm_mark_get(attrs, &m);
1856 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1860 if (x->km.state != XFRM_STATE_VALID)
1863 err = xfrm_replay_verify_len(x->replay_esn, re);
1867 spin_lock_bh(&x->lock);
1868 xfrm_update_ae_params(x, attrs, 1);
1869 spin_unlock_bh(&x->lock);
1871 c.event = nlh->nlmsg_type;
1872 c.seq = nlh->nlmsg_seq;
1873 c.portid = nlh->nlmsg_pid;
1874 c.data.aevent = XFRM_AE_CU;
1875 km_state_notify(x, &c);
1882 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1883 struct nlattr **attrs)
1885 struct net *net = sock_net(skb->sk);
1887 u8 type = XFRM_POLICY_TYPE_MAIN;
1890 err = copy_from_user_policy_type(&type, attrs);
1894 err = xfrm_policy_flush(net, type, true);
1896 if (err == -ESRCH) /* empty table */
1902 c.event = nlh->nlmsg_type;
1903 c.seq = nlh->nlmsg_seq;
1904 c.portid = nlh->nlmsg_pid;
1906 km_policy_notify(NULL, 0, &c);
1910 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1911 struct nlattr **attrs)
1913 struct net *net = sock_net(skb->sk);
1914 struct xfrm_policy *xp;
1915 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1916 struct xfrm_userpolicy_info *p = &up->pol;
1917 u8 type = XFRM_POLICY_TYPE_MAIN;
1920 u32 mark = xfrm_mark_get(attrs, &m);
1922 err = copy_from_user_policy_type(&type, attrs);
1926 err = verify_policy_dir(p->dir);
1931 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1933 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1934 struct xfrm_sec_ctx *ctx;
1936 err = verify_sec_ctx_len(attrs);
1942 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1944 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1948 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1949 &p->sel, ctx, 0, &err);
1950 security_xfrm_policy_free(ctx);
1955 if (unlikely(xp->walk.dead))
1960 xfrm_policy_delete(xp, p->dir);
1961 xfrm_audit_policy_delete(xp, 1, true);
1963 // reset the timers here?
1964 WARN(1, "Dont know what to do with soft policy expire\n");
1966 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
1973 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1974 struct nlattr **attrs)
1976 struct net *net = sock_net(skb->sk);
1977 struct xfrm_state *x;
1979 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1980 struct xfrm_usersa_info *p = &ue->state;
1982 u32 mark = xfrm_mark_get(attrs, &m);
1984 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1990 spin_lock_bh(&x->lock);
1992 if (x->km.state != XFRM_STATE_VALID)
1994 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
1997 __xfrm_state_delete(x);
1998 xfrm_audit_state_delete(x, 1, true);
2002 spin_unlock_bh(&x->lock);
2007 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2008 struct nlattr **attrs)
2010 struct net *net = sock_net(skb->sk);
2011 struct xfrm_policy *xp;
2012 struct xfrm_user_tmpl *ut;
2014 struct nlattr *rt = attrs[XFRMA_TMPL];
2015 struct xfrm_mark mark;
2017 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2018 struct xfrm_state *x = xfrm_state_alloc(net);
2024 xfrm_mark_get(attrs, &mark);
2026 err = verify_newpolicy_info(&ua->policy);
2031 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2035 memcpy(&x->id, &ua->id, sizeof(ua->id));
2036 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2037 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2038 xp->mark.m = x->mark.m = mark.m;
2039 xp->mark.v = x->mark.v = mark.v;
2041 /* extract the templates and for each call km_key */
2042 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2043 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2044 memcpy(&x->id, &t->id, sizeof(x->id));
2045 x->props.mode = t->mode;
2046 x->props.reqid = t->reqid;
2047 x->props.family = ut->family;
2048 t->aalgos = ua->aalgos;
2049 t->ealgos = ua->ealgos;
2050 t->calgos = ua->calgos;
2051 err = km_query(x, t, xp);
2061 WARN(1, "BAD policy passed\n");
2068 #ifdef CONFIG_XFRM_MIGRATE
2069 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2070 struct xfrm_kmaddress *k,
2071 struct nlattr **attrs, int *num)
2073 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2074 struct xfrm_user_migrate *um;
2078 struct xfrm_user_kmaddress *uk;
2080 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2081 memcpy(&k->local, &uk->local, sizeof(k->local));
2082 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2083 k->family = uk->family;
2084 k->reserved = uk->reserved;
2088 num_migrate = nla_len(rt) / sizeof(*um);
2090 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2093 for (i = 0; i < num_migrate; i++, um++, ma++) {
2094 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2095 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2096 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2097 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2099 ma->proto = um->proto;
2100 ma->mode = um->mode;
2101 ma->reqid = um->reqid;
2103 ma->old_family = um->old_family;
2104 ma->new_family = um->new_family;
2111 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2112 struct nlattr **attrs)
2114 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2115 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2116 struct xfrm_kmaddress km, *kmp;
2120 struct net *net = sock_net(skb->sk);
2122 if (attrs[XFRMA_MIGRATE] == NULL)
2125 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2127 err = copy_from_user_policy_type(&type, attrs);
2131 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2138 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2143 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2144 struct nlattr **attrs)
2146 return -ENOPROTOOPT;
2150 #ifdef CONFIG_XFRM_MIGRATE
2151 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2153 struct xfrm_user_migrate um;
2155 memset(&um, 0, sizeof(um));
2156 um.proto = m->proto;
2158 um.reqid = m->reqid;
2159 um.old_family = m->old_family;
2160 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2161 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2162 um.new_family = m->new_family;
2163 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2164 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2166 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2169 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2171 struct xfrm_user_kmaddress uk;
2173 memset(&uk, 0, sizeof(uk));
2174 uk.family = k->family;
2175 uk.reserved = k->reserved;
2176 memcpy(&uk.local, &k->local, sizeof(uk.local));
2177 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2179 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2182 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2184 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2185 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2186 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2187 + userpolicy_type_attrsize();
2190 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2191 int num_migrate, const struct xfrm_kmaddress *k,
2192 const struct xfrm_selector *sel, u8 dir, u8 type)
2194 const struct xfrm_migrate *mp;
2195 struct xfrm_userpolicy_id *pol_id;
2196 struct nlmsghdr *nlh;
2199 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2203 pol_id = nlmsg_data(nlh);
2204 /* copy data from selector, dir, and type to the pol_id */
2205 memset(pol_id, 0, sizeof(*pol_id));
2206 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2210 err = copy_to_user_kmaddress(k, skb);
2214 err = copy_to_user_policy_type(type, skb);
2217 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2218 err = copy_to_user_migrate(mp, skb);
2223 return nlmsg_end(skb, nlh);
2226 nlmsg_cancel(skb, nlh);
2230 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2231 const struct xfrm_migrate *m, int num_migrate,
2232 const struct xfrm_kmaddress *k)
2234 struct net *net = &init_net;
2235 struct sk_buff *skb;
2237 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2242 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2245 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2248 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2249 const struct xfrm_migrate *m, int num_migrate,
2250 const struct xfrm_kmaddress *k)
2252 return -ENOPROTOOPT;
2256 #define XMSGSIZE(type) sizeof(struct type)
2258 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2259 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2260 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2261 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2262 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2263 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2264 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2265 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2266 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2267 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2268 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2269 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2270 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2271 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2272 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2273 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2274 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2275 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2276 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2277 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2278 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2283 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2284 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2285 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2286 [XFRMA_LASTUSED] = { .type = NLA_U64},
2287 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2288 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2289 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2290 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2291 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2292 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2293 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2294 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2295 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2296 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2297 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2298 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2299 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2300 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2301 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2302 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2303 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2304 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2305 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2306 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2307 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2308 [XFRMA_PROTO] = { .type = NLA_U8 },
2309 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2312 static const struct xfrm_link {
2313 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2314 int (*dump)(struct sk_buff *, struct netlink_callback *);
2315 int (*done)(struct netlink_callback *);
2316 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2317 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2318 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2319 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2320 .dump = xfrm_dump_sa,
2321 .done = xfrm_dump_sa_done },
2322 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2323 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2324 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2325 .dump = xfrm_dump_policy,
2326 .done = xfrm_dump_policy_done },
2327 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2328 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2329 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2330 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2331 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2332 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2333 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2334 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2335 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2336 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2337 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2338 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2339 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2342 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2344 struct net *net = sock_net(skb->sk);
2345 struct nlattr *attrs[XFRMA_MAX+1];
2346 const struct xfrm_link *link;
2349 type = nlh->nlmsg_type;
2350 if (type > XFRM_MSG_MAX)
2353 type -= XFRM_MSG_BASE;
2354 link = &xfrm_dispatch[type];
2356 /* All operations require privileges, even GET */
2357 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2360 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2361 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2362 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2363 if (link->dump == NULL)
2367 struct netlink_dump_control c = {
2371 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2375 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2380 if (link->doit == NULL)
2383 return link->doit(skb, nlh, attrs);
2386 static void xfrm_netlink_rcv(struct sk_buff *skb)
2388 struct net *net = sock_net(skb->sk);
2390 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2391 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2392 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2395 static inline size_t xfrm_expire_msgsize(void)
2397 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2398 + nla_total_size(sizeof(struct xfrm_mark));
2401 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2403 struct xfrm_user_expire *ue;
2404 struct nlmsghdr *nlh;
2407 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2411 ue = nlmsg_data(nlh);
2412 copy_to_user_state(x, &ue->state);
2413 ue->hard = (c->data.hard != 0) ? 1 : 0;
2415 err = xfrm_mark_put(skb, &x->mark);
2419 return nlmsg_end(skb, nlh);
2422 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2424 struct net *net = xs_net(x);
2425 struct sk_buff *skb;
2427 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2431 if (build_expire(skb, x, c) < 0) {
2436 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2439 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2441 struct net *net = xs_net(x);
2442 struct sk_buff *skb;
2444 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2448 if (build_aevent(skb, x, c) < 0)
2451 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2454 static int xfrm_notify_sa_flush(const struct km_event *c)
2456 struct net *net = c->net;
2457 struct xfrm_usersa_flush *p;
2458 struct nlmsghdr *nlh;
2459 struct sk_buff *skb;
2460 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2462 skb = nlmsg_new(len, GFP_ATOMIC);
2466 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2472 p = nlmsg_data(nlh);
2473 p->proto = c->data.proto;
2475 nlmsg_end(skb, nlh);
2477 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2480 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2484 l += nla_total_size(aead_len(x->aead));
2486 l += nla_total_size(sizeof(struct xfrm_algo) +
2487 (x->aalg->alg_key_len + 7) / 8);
2488 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2491 l += nla_total_size(xfrm_alg_len(x->ealg));
2493 l += nla_total_size(sizeof(*x->calg));
2495 l += nla_total_size(sizeof(*x->encap));
2497 l += nla_total_size(sizeof(x->tfcpad));
2499 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2501 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2502 x->security->ctx_len);
2504 l += nla_total_size(sizeof(*x->coaddr));
2505 if (x->props.extra_flags)
2506 l += nla_total_size(sizeof(x->props.extra_flags));
2508 /* Must count x->lastused as it may become non-zero behind our back. */
2509 l += nla_total_size(sizeof(u64));
2514 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2516 struct net *net = xs_net(x);
2517 struct xfrm_usersa_info *p;
2518 struct xfrm_usersa_id *id;
2519 struct nlmsghdr *nlh;
2520 struct sk_buff *skb;
2521 int len = xfrm_sa_len(x);
2524 headlen = sizeof(*p);
2525 if (c->event == XFRM_MSG_DELSA) {
2526 len += nla_total_size(headlen);
2527 headlen = sizeof(*id);
2528 len += nla_total_size(sizeof(struct xfrm_mark));
2530 len += NLMSG_ALIGN(headlen);
2532 skb = nlmsg_new(len, GFP_ATOMIC);
2536 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2541 p = nlmsg_data(nlh);
2542 if (c->event == XFRM_MSG_DELSA) {
2543 struct nlattr *attr;
2545 id = nlmsg_data(nlh);
2546 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2547 id->spi = x->id.spi;
2548 id->family = x->props.family;
2549 id->proto = x->id.proto;
2551 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2558 err = copy_to_user_state_extra(x, p, skb);
2562 nlmsg_end(skb, nlh);
2564 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2571 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2575 case XFRM_MSG_EXPIRE:
2576 return xfrm_exp_state_notify(x, c);
2577 case XFRM_MSG_NEWAE:
2578 return xfrm_aevent_state_notify(x, c);
2579 case XFRM_MSG_DELSA:
2580 case XFRM_MSG_UPDSA:
2581 case XFRM_MSG_NEWSA:
2582 return xfrm_notify_sa(x, c);
2583 case XFRM_MSG_FLUSHSA:
2584 return xfrm_notify_sa_flush(c);
2586 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2595 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2596 struct xfrm_policy *xp)
2598 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2599 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2600 + nla_total_size(sizeof(struct xfrm_mark))
2601 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2602 + userpolicy_type_attrsize();
2605 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2606 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2608 __u32 seq = xfrm_get_acqseq();
2609 struct xfrm_user_acquire *ua;
2610 struct nlmsghdr *nlh;
2613 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2617 ua = nlmsg_data(nlh);
2618 memcpy(&ua->id, &x->id, sizeof(ua->id));
2619 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2620 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2621 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2622 ua->aalgos = xt->aalgos;
2623 ua->ealgos = xt->ealgos;
2624 ua->calgos = xt->calgos;
2625 ua->seq = x->km.seq = seq;
2627 err = copy_to_user_tmpl(xp, skb);
2629 err = copy_to_user_state_sec_ctx(x, skb);
2631 err = copy_to_user_policy_type(xp->type, skb);
2633 err = xfrm_mark_put(skb, &xp->mark);
2635 nlmsg_cancel(skb, nlh);
2639 return nlmsg_end(skb, nlh);
2642 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2643 struct xfrm_policy *xp)
2645 struct net *net = xs_net(x);
2646 struct sk_buff *skb;
2648 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2652 if (build_acquire(skb, x, xt, xp) < 0)
2655 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2658 /* User gives us xfrm_user_policy_info followed by an array of 0
2659 * or more templates.
2661 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2662 u8 *data, int len, int *dir)
2664 struct net *net = sock_net(sk);
2665 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2666 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2667 struct xfrm_policy *xp;
2670 switch (sk->sk_family) {
2672 if (opt != IP_XFRM_POLICY) {
2677 #if IS_ENABLED(CONFIG_IPV6)
2679 if (opt != IPV6_XFRM_POLICY) {
2692 if (len < sizeof(*p) ||
2693 verify_newpolicy_info(p))
2696 nr = ((len - sizeof(*p)) / sizeof(*ut));
2697 if (validate_tmpl(nr, ut, p->sel.family))
2700 if (p->dir > XFRM_POLICY_OUT)
2703 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2709 copy_from_user_policy(xp, p);
2710 xp->type = XFRM_POLICY_TYPE_MAIN;
2711 copy_templates(xp, ut, nr);
2718 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2720 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2721 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2722 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2723 + nla_total_size(sizeof(struct xfrm_mark))
2724 + userpolicy_type_attrsize();
2727 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2728 int dir, const struct km_event *c)
2730 struct xfrm_user_polexpire *upe;
2731 int hard = c->data.hard;
2732 struct nlmsghdr *nlh;
2735 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2739 upe = nlmsg_data(nlh);
2740 copy_to_user_policy(xp, &upe->pol, dir);
2741 err = copy_to_user_tmpl(xp, skb);
2743 err = copy_to_user_sec_ctx(xp, skb);
2745 err = copy_to_user_policy_type(xp->type, skb);
2747 err = xfrm_mark_put(skb, &xp->mark);
2749 nlmsg_cancel(skb, nlh);
2754 return nlmsg_end(skb, nlh);
2757 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2759 struct net *net = xp_net(xp);
2760 struct sk_buff *skb;
2762 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2766 if (build_polexpire(skb, xp, dir, c) < 0)
2769 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2772 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2774 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2775 struct net *net = xp_net(xp);
2776 struct xfrm_userpolicy_info *p;
2777 struct xfrm_userpolicy_id *id;
2778 struct nlmsghdr *nlh;
2779 struct sk_buff *skb;
2782 headlen = sizeof(*p);
2783 if (c->event == XFRM_MSG_DELPOLICY) {
2784 len += nla_total_size(headlen);
2785 headlen = sizeof(*id);
2787 len += userpolicy_type_attrsize();
2788 len += nla_total_size(sizeof(struct xfrm_mark));
2789 len += NLMSG_ALIGN(headlen);
2791 skb = nlmsg_new(len, GFP_ATOMIC);
2795 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2800 p = nlmsg_data(nlh);
2801 if (c->event == XFRM_MSG_DELPOLICY) {
2802 struct nlattr *attr;
2804 id = nlmsg_data(nlh);
2805 memset(id, 0, sizeof(*id));
2808 id->index = xp->index;
2810 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2812 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2820 copy_to_user_policy(xp, p, dir);
2821 err = copy_to_user_tmpl(xp, skb);
2823 err = copy_to_user_policy_type(xp->type, skb);
2825 err = xfrm_mark_put(skb, &xp->mark);
2829 nlmsg_end(skb, nlh);
2831 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2838 static int xfrm_notify_policy_flush(const struct km_event *c)
2840 struct net *net = c->net;
2841 struct nlmsghdr *nlh;
2842 struct sk_buff *skb;
2845 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2849 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2853 err = copy_to_user_policy_type(c->data.type, skb);
2857 nlmsg_end(skb, nlh);
2859 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2866 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2870 case XFRM_MSG_NEWPOLICY:
2871 case XFRM_MSG_UPDPOLICY:
2872 case XFRM_MSG_DELPOLICY:
2873 return xfrm_notify_policy(xp, dir, c);
2874 case XFRM_MSG_FLUSHPOLICY:
2875 return xfrm_notify_policy_flush(c);
2876 case XFRM_MSG_POLEXPIRE:
2877 return xfrm_exp_policy_notify(xp, dir, c);
2879 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2887 static inline size_t xfrm_report_msgsize(void)
2889 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2892 static int build_report(struct sk_buff *skb, u8 proto,
2893 struct xfrm_selector *sel, xfrm_address_t *addr)
2895 struct xfrm_user_report *ur;
2896 struct nlmsghdr *nlh;
2898 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2902 ur = nlmsg_data(nlh);
2904 memcpy(&ur->sel, sel, sizeof(ur->sel));
2907 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2909 nlmsg_cancel(skb, nlh);
2913 return nlmsg_end(skb, nlh);
2916 static int xfrm_send_report(struct net *net, u8 proto,
2917 struct xfrm_selector *sel, xfrm_address_t *addr)
2919 struct sk_buff *skb;
2921 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2925 if (build_report(skb, proto, sel, addr) < 0)
2928 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
2931 static inline size_t xfrm_mapping_msgsize(void)
2933 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2936 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2937 xfrm_address_t *new_saddr, __be16 new_sport)
2939 struct xfrm_user_mapping *um;
2940 struct nlmsghdr *nlh;
2942 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2946 um = nlmsg_data(nlh);
2948 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2949 um->id.spi = x->id.spi;
2950 um->id.family = x->props.family;
2951 um->id.proto = x->id.proto;
2952 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2953 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2954 um->new_sport = new_sport;
2955 um->old_sport = x->encap->encap_sport;
2956 um->reqid = x->props.reqid;
2958 return nlmsg_end(skb, nlh);
2961 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2964 struct net *net = xs_net(x);
2965 struct sk_buff *skb;
2967 if (x->id.proto != IPPROTO_ESP)
2973 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2977 if (build_mapping(skb, x, ipaddr, sport) < 0)
2980 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
2983 static bool xfrm_is_alive(const struct km_event *c)
2985 return (bool)xfrm_acquire_is_on(c->net);
2988 static struct xfrm_mgr netlink_mgr = {
2990 .notify = xfrm_send_state_notify,
2991 .acquire = xfrm_send_acquire,
2992 .compile_policy = xfrm_compile_policy,
2993 .notify_policy = xfrm_send_policy_notify,
2994 .report = xfrm_send_report,
2995 .migrate = xfrm_send_migrate,
2996 .new_mapping = xfrm_send_mapping,
2997 .is_alive = xfrm_is_alive,
3000 static int __net_init xfrm_user_net_init(struct net *net)
3003 struct netlink_kernel_cfg cfg = {
3004 .groups = XFRMNLGRP_MAX,
3005 .input = xfrm_netlink_rcv,
3008 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3011 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3012 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3016 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3019 list_for_each_entry(net, net_exit_list, exit_list)
3020 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3022 list_for_each_entry(net, net_exit_list, exit_list)
3023 netlink_kernel_release(net->xfrm.nlsk_stash);
3026 static struct pernet_operations xfrm_user_net_ops = {
3027 .init = xfrm_user_net_init,
3028 .exit_batch = xfrm_user_net_exit,
3031 static int __init xfrm_user_init(void)
3035 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3037 rv = register_pernet_subsys(&xfrm_user_net_ops);
3040 rv = xfrm_register_km(&netlink_mgr);
3042 unregister_pernet_subsys(&xfrm_user_net_ops);
3046 static void __exit xfrm_user_exit(void)
3048 xfrm_unregister_km(&netlink_mgr);
3049 unregister_pernet_subsys(&xfrm_user_net_ops);
3052 module_init(xfrm_user_init);
3053 module_exit(xfrm_user_exit);
3054 MODULE_LICENSE("GPL");
3055 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);