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] ||
185 if (attrs[XFRMA_ALG_COMP])
187 if (!attrs[XFRMA_ALG_AUTH] &&
188 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
189 !attrs[XFRMA_ALG_CRYPT] &&
190 !attrs[XFRMA_ALG_AEAD])
192 if ((attrs[XFRMA_ALG_AUTH] ||
193 attrs[XFRMA_ALG_AUTH_TRUNC] ||
194 attrs[XFRMA_ALG_CRYPT]) &&
195 attrs[XFRMA_ALG_AEAD])
197 if (attrs[XFRMA_TFCPAD] &&
198 p->mode != XFRM_MODE_TUNNEL)
203 if (!attrs[XFRMA_ALG_COMP] ||
204 attrs[XFRMA_ALG_AEAD] ||
205 attrs[XFRMA_ALG_AUTH] ||
206 attrs[XFRMA_ALG_AUTH_TRUNC] ||
207 attrs[XFRMA_ALG_CRYPT] ||
208 attrs[XFRMA_TFCPAD] ||
209 (ntohl(p->id.spi) >= 0x10000))
213 #if IS_ENABLED(CONFIG_IPV6)
214 case IPPROTO_DSTOPTS:
215 case IPPROTO_ROUTING:
216 if (attrs[XFRMA_ALG_COMP] ||
217 attrs[XFRMA_ALG_AUTH] ||
218 attrs[XFRMA_ALG_AUTH_TRUNC] ||
219 attrs[XFRMA_ALG_AEAD] ||
220 attrs[XFRMA_ALG_CRYPT] ||
221 attrs[XFRMA_ENCAP] ||
222 attrs[XFRMA_SEC_CTX] ||
223 attrs[XFRMA_TFCPAD] ||
224 !attrs[XFRMA_COADDR])
233 if ((err = verify_aead(attrs)))
235 if ((err = verify_auth_trunc(attrs)))
237 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
239 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
241 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
243 if ((err = verify_sec_ctx_len(attrs)))
245 if ((err = verify_replay(p, attrs)))
250 case XFRM_MODE_TRANSPORT:
251 case XFRM_MODE_TUNNEL:
252 case XFRM_MODE_ROUTEOPTIMIZATION:
266 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
267 struct xfrm_algo_desc *(*get_byname)(const char *, int),
270 struct xfrm_algo *p, *ualg;
271 struct xfrm_algo_desc *algo;
276 ualg = nla_data(rta);
278 algo = get_byname(ualg->alg_name, 1);
281 *props = algo->desc.sadb_alg_id;
283 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
287 strcpy(p->alg_name, algo->name);
292 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
295 struct xfrm_algo *ualg;
296 struct xfrm_algo_auth *p;
297 struct xfrm_algo_desc *algo;
302 ualg = nla_data(rta);
304 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
307 *props = algo->desc.sadb_alg_id;
309 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
313 strcpy(p->alg_name, algo->name);
314 p->alg_key_len = ualg->alg_key_len;
315 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
316 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
322 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
325 struct xfrm_algo_auth *p, *ualg;
326 struct xfrm_algo_desc *algo;
331 ualg = nla_data(rta);
333 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
336 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
337 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
339 *props = algo->desc.sadb_alg_id;
341 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
345 strcpy(p->alg_name, algo->name);
346 if (!p->alg_trunc_len)
347 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
353 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
356 struct xfrm_algo_aead *p, *ualg;
357 struct xfrm_algo_desc *algo;
362 ualg = nla_data(rta);
364 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
367 *props = algo->desc.sadb_alg_id;
369 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
373 strcpy(p->alg_name, algo->name);
378 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
381 struct xfrm_replay_state_esn *up;
384 if (!replay_esn || !rp)
388 ulen = xfrm_replay_state_esn_len(up);
390 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
396 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
397 struct xfrm_replay_state_esn **preplay_esn,
400 struct xfrm_replay_state_esn *p, *pp, *up;
407 klen = xfrm_replay_state_esn_len(up);
408 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
410 p = kzalloc(klen, GFP_KERNEL);
414 pp = kzalloc(klen, GFP_KERNEL);
421 memcpy(pp, up, ulen);
429 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
434 len += sizeof(struct xfrm_user_sec_ctx);
435 len += xfrm_ctx->ctx_len;
440 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
442 memcpy(&x->id, &p->id, sizeof(x->id));
443 memcpy(&x->sel, &p->sel, sizeof(x->sel));
444 memcpy(&x->lft, &p->lft, sizeof(x->lft));
445 x->props.mode = p->mode;
446 x->props.replay_window = min_t(unsigned int, p->replay_window,
447 sizeof(x->replay.bitmap) * 8);
448 x->props.reqid = p->reqid;
449 x->props.family = p->family;
450 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
451 x->props.flags = p->flags;
453 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
454 x->sel.family = p->family;
458 * someday when pfkey also has support, we could have the code
459 * somehow made shareable and move it to xfrm_state.c - JHS
462 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
465 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
466 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
467 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
468 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
469 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
472 struct xfrm_replay_state_esn *replay_esn;
473 replay_esn = nla_data(re);
474 memcpy(x->replay_esn, replay_esn,
475 xfrm_replay_state_esn_len(replay_esn));
476 memcpy(x->preplay_esn, replay_esn,
477 xfrm_replay_state_esn_len(replay_esn));
481 struct xfrm_replay_state *replay;
482 replay = nla_data(rp);
483 memcpy(&x->replay, replay, sizeof(*replay));
484 memcpy(&x->preplay, replay, sizeof(*replay));
488 struct xfrm_lifetime_cur *ltime;
489 ltime = nla_data(lt);
490 x->curlft.bytes = ltime->bytes;
491 x->curlft.packets = ltime->packets;
492 x->curlft.add_time = ltime->add_time;
493 x->curlft.use_time = ltime->use_time;
497 x->replay_maxage = nla_get_u32(et);
500 x->replay_maxdiff = nla_get_u32(rt);
503 static struct xfrm_state *xfrm_state_construct(struct net *net,
504 struct xfrm_usersa_info *p,
505 struct nlattr **attrs,
508 struct xfrm_state *x = xfrm_state_alloc(net);
514 copy_from_user_state(x, p);
516 if (attrs[XFRMA_SA_EXTRA_FLAGS])
517 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
519 if ((err = attach_aead(&x->aead, &x->props.ealgo,
520 attrs[XFRMA_ALG_AEAD])))
522 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
523 attrs[XFRMA_ALG_AUTH_TRUNC])))
525 if (!x->props.aalgo) {
526 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
527 attrs[XFRMA_ALG_AUTH])))
530 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
531 xfrm_ealg_get_byname,
532 attrs[XFRMA_ALG_CRYPT])))
534 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
535 xfrm_calg_get_byname,
536 attrs[XFRMA_ALG_COMP])))
539 if (attrs[XFRMA_ENCAP]) {
540 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
541 sizeof(*x->encap), GFP_KERNEL);
542 if (x->encap == NULL)
546 if (attrs[XFRMA_TFCPAD])
547 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
549 if (attrs[XFRMA_COADDR]) {
550 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
551 sizeof(*x->coaddr), GFP_KERNEL);
552 if (x->coaddr == NULL)
556 xfrm_mark_get(attrs, &x->mark);
558 err = __xfrm_init_state(x, false);
562 if (attrs[XFRMA_SEC_CTX] &&
563 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
566 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
567 attrs[XFRMA_REPLAY_ESN_VAL])))
571 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
572 /* sysctl_xfrm_aevent_etime is in 100ms units */
573 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
575 if ((err = xfrm_init_replay(x)))
578 /* override default values from above */
579 xfrm_update_ae_params(x, attrs, 0);
584 x->km.state = XFRM_STATE_DEAD;
591 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
592 struct nlattr **attrs)
594 struct net *net = sock_net(skb->sk);
595 struct xfrm_usersa_info *p = nlmsg_data(nlh);
596 struct xfrm_state *x;
600 err = verify_newsa_info(p, attrs);
604 x = xfrm_state_construct(net, p, attrs, &err);
609 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
610 err = xfrm_state_add(x);
612 err = xfrm_state_update(x);
614 xfrm_audit_state_add(x, err ? 0 : 1, true);
617 x->km.state = XFRM_STATE_DEAD;
622 c.seq = nlh->nlmsg_seq;
623 c.portid = nlh->nlmsg_pid;
624 c.event = nlh->nlmsg_type;
626 km_state_notify(x, &c);
632 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
633 struct xfrm_usersa_id *p,
634 struct nlattr **attrs,
637 struct xfrm_state *x = NULL;
640 u32 mark = xfrm_mark_get(attrs, &m);
642 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
644 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
646 xfrm_address_t *saddr = NULL;
648 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
655 x = xfrm_state_lookup_byaddr(net, mark,
657 p->proto, p->family);
666 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
667 struct nlattr **attrs)
669 struct net *net = sock_net(skb->sk);
670 struct xfrm_state *x;
673 struct xfrm_usersa_id *p = nlmsg_data(nlh);
675 x = xfrm_user_state_lookup(net, p, attrs, &err);
679 if ((err = security_xfrm_state_delete(x)) != 0)
682 if (xfrm_state_kern(x)) {
687 err = xfrm_state_delete(x);
692 c.seq = nlh->nlmsg_seq;
693 c.portid = nlh->nlmsg_pid;
694 c.event = nlh->nlmsg_type;
695 km_state_notify(x, &c);
698 xfrm_audit_state_delete(x, err ? 0 : 1, true);
703 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
705 memset(p, 0, sizeof(*p));
706 memcpy(&p->id, &x->id, sizeof(p->id));
707 memcpy(&p->sel, &x->sel, sizeof(p->sel));
708 memcpy(&p->lft, &x->lft, sizeof(p->lft));
709 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
710 memcpy(&p->stats, &x->stats, sizeof(p->stats));
711 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
712 p->mode = x->props.mode;
713 p->replay_window = x->props.replay_window;
714 p->reqid = x->props.reqid;
715 p->family = x->props.family;
716 p->flags = x->props.flags;
720 struct xfrm_dump_info {
721 struct sk_buff *in_skb;
722 struct sk_buff *out_skb;
727 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
729 struct xfrm_user_sec_ctx *uctx;
731 int ctx_size = sizeof(*uctx) + s->ctx_len;
733 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
737 uctx = nla_data(attr);
738 uctx->exttype = XFRMA_SEC_CTX;
739 uctx->len = ctx_size;
740 uctx->ctx_doi = s->ctx_doi;
741 uctx->ctx_alg = s->ctx_alg;
742 uctx->ctx_len = s->ctx_len;
743 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
748 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
750 struct xfrm_algo *algo;
753 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
754 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
758 algo = nla_data(nla);
759 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
760 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
761 algo->alg_key_len = auth->alg_key_len;
766 /* Don't change this without updating xfrm_sa_len! */
767 static int copy_to_user_state_extra(struct xfrm_state *x,
768 struct xfrm_usersa_info *p,
773 copy_to_user_state(x, p);
775 if (x->props.extra_flags) {
776 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
777 x->props.extra_flags);
783 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
788 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
793 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
798 ret = copy_to_user_auth(x->aalg, skb);
800 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
801 xfrm_alg_auth_len(x->aalg), x->aalg);
806 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
811 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
816 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
821 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
825 ret = xfrm_mark_put(skb, &x->mark);
829 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
830 xfrm_replay_state_esn_len(x->replay_esn),
836 ret = copy_sec_ctx(x->security, skb);
841 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
843 struct xfrm_dump_info *sp = ptr;
844 struct sk_buff *in_skb = sp->in_skb;
845 struct sk_buff *skb = sp->out_skb;
846 struct xfrm_usersa_info *p;
847 struct nlmsghdr *nlh;
850 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
851 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
857 err = copy_to_user_state_extra(x, p, skb);
859 nlmsg_cancel(skb, nlh);
866 static int xfrm_dump_sa_done(struct netlink_callback *cb)
868 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
869 struct sock *sk = cb->skb->sk;
870 struct net *net = sock_net(sk);
872 xfrm_state_walk_done(walk, net);
876 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
877 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
879 struct net *net = sock_net(skb->sk);
880 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
881 struct xfrm_dump_info info;
883 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
884 sizeof(cb->args) - sizeof(cb->args[0]));
886 info.in_skb = cb->skb;
888 info.nlmsg_seq = cb->nlh->nlmsg_seq;
889 info.nlmsg_flags = NLM_F_MULTI;
892 struct nlattr *attrs[XFRMA_MAX+1];
893 struct xfrm_address_filter *filter = NULL;
899 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
904 if (attrs[XFRMA_ADDRESS_FILTER]) {
905 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
909 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
913 if (attrs[XFRMA_PROTO])
914 proto = nla_get_u8(attrs[XFRMA_PROTO]);
916 xfrm_state_walk_init(walk, proto, filter);
919 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
924 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
925 struct xfrm_state *x, u32 seq)
927 struct xfrm_dump_info info;
931 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
933 return ERR_PTR(-ENOMEM);
935 info.in_skb = in_skb;
937 info.nlmsg_seq = seq;
938 info.nlmsg_flags = 0;
940 err = dump_one_state(x, 0, &info);
949 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
950 * Must be called with RCU read lock.
952 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
953 u32 pid, unsigned int group)
955 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
958 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
963 static inline size_t xfrm_spdinfo_msgsize(void)
965 return NLMSG_ALIGN(4)
966 + nla_total_size(sizeof(struct xfrmu_spdinfo))
967 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
970 static int build_spdinfo(struct sk_buff *skb, struct net *net,
971 u32 portid, u32 seq, u32 flags)
973 struct xfrmk_spdinfo si;
974 struct xfrmu_spdinfo spc;
975 struct xfrmu_spdhinfo sph;
976 struct nlmsghdr *nlh;
980 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
981 if (nlh == NULL) /* shouldn't really happen ... */
986 xfrm_spd_getinfo(net, &si);
987 spc.incnt = si.incnt;
988 spc.outcnt = si.outcnt;
989 spc.fwdcnt = si.fwdcnt;
990 spc.inscnt = si.inscnt;
991 spc.outscnt = si.outscnt;
992 spc.fwdscnt = si.fwdscnt;
993 sph.spdhcnt = si.spdhcnt;
994 sph.spdhmcnt = si.spdhmcnt;
996 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
998 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1000 nlmsg_cancel(skb, nlh);
1004 return nlmsg_end(skb, nlh);
1007 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1008 struct nlattr **attrs)
1010 struct net *net = sock_net(skb->sk);
1011 struct sk_buff *r_skb;
1012 u32 *flags = nlmsg_data(nlh);
1013 u32 sportid = NETLINK_CB(skb).portid;
1014 u32 seq = nlh->nlmsg_seq;
1016 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1020 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1023 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1026 static inline size_t xfrm_sadinfo_msgsize(void)
1028 return NLMSG_ALIGN(4)
1029 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1030 + nla_total_size(4); /* XFRMA_SAD_CNT */
1033 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1034 u32 portid, u32 seq, u32 flags)
1036 struct xfrmk_sadinfo si;
1037 struct xfrmu_sadhinfo sh;
1038 struct nlmsghdr *nlh;
1042 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1043 if (nlh == NULL) /* shouldn't really happen ... */
1046 f = nlmsg_data(nlh);
1048 xfrm_sad_getinfo(net, &si);
1050 sh.sadhmcnt = si.sadhmcnt;
1051 sh.sadhcnt = si.sadhcnt;
1053 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1055 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1057 nlmsg_cancel(skb, nlh);
1061 return nlmsg_end(skb, nlh);
1064 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1065 struct nlattr **attrs)
1067 struct net *net = sock_net(skb->sk);
1068 struct sk_buff *r_skb;
1069 u32 *flags = nlmsg_data(nlh);
1070 u32 sportid = NETLINK_CB(skb).portid;
1071 u32 seq = nlh->nlmsg_seq;
1073 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1077 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1080 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1083 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1084 struct nlattr **attrs)
1086 struct net *net = sock_net(skb->sk);
1087 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1088 struct xfrm_state *x;
1089 struct sk_buff *resp_skb;
1092 x = xfrm_user_state_lookup(net, p, attrs, &err);
1096 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1097 if (IS_ERR(resp_skb)) {
1098 err = PTR_ERR(resp_skb);
1100 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1107 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1108 struct nlattr **attrs)
1110 struct net *net = sock_net(skb->sk);
1111 struct xfrm_state *x;
1112 struct xfrm_userspi_info *p;
1113 struct sk_buff *resp_skb;
1114 xfrm_address_t *daddr;
1120 p = nlmsg_data(nlh);
1121 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1125 family = p->info.family;
1126 daddr = &p->info.id.daddr;
1130 mark = xfrm_mark_get(attrs, &m);
1132 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1133 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1140 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1141 p->info.id.proto, daddr,
1148 err = xfrm_alloc_spi(x, p->min, p->max);
1152 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1153 if (IS_ERR(resp_skb)) {
1154 err = PTR_ERR(resp_skb);
1158 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1166 static int verify_policy_dir(u8 dir)
1169 case XFRM_POLICY_IN:
1170 case XFRM_POLICY_OUT:
1171 case XFRM_POLICY_FWD:
1181 static int verify_policy_type(u8 type)
1184 case XFRM_POLICY_TYPE_MAIN:
1185 #ifdef CONFIG_XFRM_SUB_POLICY
1186 case XFRM_POLICY_TYPE_SUB:
1197 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1202 case XFRM_SHARE_ANY:
1203 case XFRM_SHARE_SESSION:
1204 case XFRM_SHARE_USER:
1205 case XFRM_SHARE_UNIQUE:
1212 switch (p->action) {
1213 case XFRM_POLICY_ALLOW:
1214 case XFRM_POLICY_BLOCK:
1221 switch (p->sel.family) {
1226 #if IS_ENABLED(CONFIG_IPV6)
1229 return -EAFNOSUPPORT;
1236 ret = verify_policy_dir(p->dir);
1239 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1245 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1247 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1248 struct xfrm_user_sec_ctx *uctx;
1253 uctx = nla_data(rt);
1254 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1257 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1263 for (i = 0; i < nr; i++, ut++) {
1264 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1266 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1267 memcpy(&t->saddr, &ut->saddr,
1268 sizeof(xfrm_address_t));
1269 t->reqid = ut->reqid;
1271 t->share = ut->share;
1272 t->optional = ut->optional;
1273 t->aalgos = ut->aalgos;
1274 t->ealgos = ut->ealgos;
1275 t->calgos = ut->calgos;
1276 /* If all masks are ~0, then we allow all algorithms. */
1277 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1278 t->encap_family = ut->family;
1282 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1286 if (nr > XFRM_MAX_DEPTH)
1289 for (i = 0; i < nr; i++) {
1290 /* We never validated the ut->family value, so many
1291 * applications simply leave it at zero. The check was
1292 * never made and ut->family was ignored because all
1293 * templates could be assumed to have the same family as
1294 * the policy itself. Now that we will have ipv4-in-ipv6
1295 * and ipv6-in-ipv4 tunnels, this is no longer true.
1298 ut[i].family = family;
1300 switch (ut[i].family) {
1303 #if IS_ENABLED(CONFIG_IPV6)
1315 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1317 struct nlattr *rt = attrs[XFRMA_TMPL];
1322 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1323 int nr = nla_len(rt) / sizeof(*utmpl);
1326 err = validate_tmpl(nr, utmpl, pol->family);
1330 copy_templates(pol, utmpl, nr);
1335 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1337 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1338 struct xfrm_userpolicy_type *upt;
1339 u8 type = XFRM_POLICY_TYPE_MAIN;
1347 err = verify_policy_type(type);
1355 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1357 xp->priority = p->priority;
1358 xp->index = p->index;
1359 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1360 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1361 xp->action = p->action;
1362 xp->flags = p->flags;
1363 xp->family = p->sel.family;
1364 /* XXX xp->share = p->share; */
1367 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1369 memset(p, 0, sizeof(*p));
1370 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1371 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1372 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1373 p->priority = xp->priority;
1374 p->index = xp->index;
1375 p->sel.family = xp->family;
1377 p->action = xp->action;
1378 p->flags = xp->flags;
1379 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1382 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1384 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1392 copy_from_user_policy(xp, p);
1394 err = copy_from_user_policy_type(&xp->type, attrs);
1398 if (!(err = copy_from_user_tmpl(xp, attrs)))
1399 err = copy_from_user_sec_ctx(xp, attrs);
1403 xfrm_mark_get(attrs, &xp->mark);
1409 xfrm_policy_destroy(xp);
1413 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1414 struct nlattr **attrs)
1416 struct net *net = sock_net(skb->sk);
1417 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1418 struct xfrm_policy *xp;
1423 err = verify_newpolicy_info(p);
1426 err = verify_sec_ctx_len(attrs);
1430 xp = xfrm_policy_construct(net, p, attrs, &err);
1434 /* shouldn't excl be based on nlh flags??
1435 * Aha! this is anti-netlink really i.e more pfkey derived
1436 * in netlink excl is a flag and you wouldnt need
1437 * a type XFRM_MSG_UPDPOLICY - JHS */
1438 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1439 err = xfrm_policy_insert(p->dir, xp, excl);
1440 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1443 security_xfrm_policy_free(xp->security);
1448 c.event = nlh->nlmsg_type;
1449 c.seq = nlh->nlmsg_seq;
1450 c.portid = nlh->nlmsg_pid;
1451 km_policy_notify(xp, p->dir, &c);
1458 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1460 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1463 if (xp->xfrm_nr == 0)
1466 for (i = 0; i < xp->xfrm_nr; i++) {
1467 struct xfrm_user_tmpl *up = &vec[i];
1468 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1470 memset(up, 0, sizeof(*up));
1471 memcpy(&up->id, &kp->id, sizeof(up->id));
1472 up->family = kp->encap_family;
1473 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1474 up->reqid = kp->reqid;
1475 up->mode = kp->mode;
1476 up->share = kp->share;
1477 up->optional = kp->optional;
1478 up->aalgos = kp->aalgos;
1479 up->ealgos = kp->ealgos;
1480 up->calgos = kp->calgos;
1483 return nla_put(skb, XFRMA_TMPL,
1484 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1487 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1490 return copy_sec_ctx(x->security, skb);
1495 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1498 return copy_sec_ctx(xp->security, skb);
1501 static inline size_t userpolicy_type_attrsize(void)
1503 #ifdef CONFIG_XFRM_SUB_POLICY
1504 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1510 #ifdef CONFIG_XFRM_SUB_POLICY
1511 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1513 struct xfrm_userpolicy_type upt = {
1517 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1521 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1527 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1529 struct xfrm_dump_info *sp = ptr;
1530 struct xfrm_userpolicy_info *p;
1531 struct sk_buff *in_skb = sp->in_skb;
1532 struct sk_buff *skb = sp->out_skb;
1533 struct nlmsghdr *nlh;
1536 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1537 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1541 p = nlmsg_data(nlh);
1542 copy_to_user_policy(xp, p, dir);
1543 err = copy_to_user_tmpl(xp, skb);
1545 err = copy_to_user_sec_ctx(xp, skb);
1547 err = copy_to_user_policy_type(xp->type, skb);
1549 err = xfrm_mark_put(skb, &xp->mark);
1551 nlmsg_cancel(skb, nlh);
1554 nlmsg_end(skb, nlh);
1558 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1560 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1561 struct net *net = sock_net(cb->skb->sk);
1563 xfrm_policy_walk_done(walk, net);
1567 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1569 struct net *net = sock_net(skb->sk);
1570 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1571 struct xfrm_dump_info info;
1573 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1574 sizeof(cb->args) - sizeof(cb->args[0]));
1576 info.in_skb = cb->skb;
1578 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1579 info.nlmsg_flags = NLM_F_MULTI;
1583 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1586 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1591 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1592 struct xfrm_policy *xp,
1595 struct xfrm_dump_info info;
1596 struct sk_buff *skb;
1599 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1601 return ERR_PTR(-ENOMEM);
1603 info.in_skb = in_skb;
1605 info.nlmsg_seq = seq;
1606 info.nlmsg_flags = 0;
1608 err = dump_one_policy(xp, dir, 0, &info);
1611 return ERR_PTR(err);
1617 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1618 struct nlattr **attrs)
1620 struct net *net = sock_net(skb->sk);
1621 struct xfrm_policy *xp;
1622 struct xfrm_userpolicy_id *p;
1623 u8 type = XFRM_POLICY_TYPE_MAIN;
1628 u32 mark = xfrm_mark_get(attrs, &m);
1630 p = nlmsg_data(nlh);
1631 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1633 err = copy_from_user_policy_type(&type, attrs);
1637 err = verify_policy_dir(p->dir);
1642 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1644 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1645 struct xfrm_sec_ctx *ctx;
1647 err = verify_sec_ctx_len(attrs);
1653 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1655 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1659 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1661 security_xfrm_policy_free(ctx);
1667 struct sk_buff *resp_skb;
1669 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1670 if (IS_ERR(resp_skb)) {
1671 err = PTR_ERR(resp_skb);
1673 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1674 NETLINK_CB(skb).portid);
1677 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1682 c.data.byid = p->index;
1683 c.event = nlh->nlmsg_type;
1684 c.seq = nlh->nlmsg_seq;
1685 c.portid = nlh->nlmsg_pid;
1686 km_policy_notify(xp, p->dir, &c);
1691 if (delete && err == 0)
1692 xfrm_garbage_collect(net);
1696 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1697 struct nlattr **attrs)
1699 struct net *net = sock_net(skb->sk);
1701 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1704 err = xfrm_state_flush(net, p->proto, true);
1706 if (err == -ESRCH) /* empty table */
1710 c.data.proto = p->proto;
1711 c.event = nlh->nlmsg_type;
1712 c.seq = nlh->nlmsg_seq;
1713 c.portid = nlh->nlmsg_pid;
1715 km_state_notify(NULL, &c);
1720 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1722 size_t replay_size = x->replay_esn ?
1723 xfrm_replay_state_esn_len(x->replay_esn) :
1724 sizeof(struct xfrm_replay_state);
1726 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1727 + nla_total_size(replay_size)
1728 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1729 + nla_total_size(sizeof(struct xfrm_mark))
1730 + nla_total_size(4) /* XFRM_AE_RTHR */
1731 + nla_total_size(4); /* XFRM_AE_ETHR */
1734 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1736 struct xfrm_aevent_id *id;
1737 struct nlmsghdr *nlh;
1740 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1744 id = nlmsg_data(nlh);
1745 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1746 id->sa_id.spi = x->id.spi;
1747 id->sa_id.family = x->props.family;
1748 id->sa_id.proto = x->id.proto;
1749 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1750 id->reqid = x->props.reqid;
1751 id->flags = c->data.aevent;
1753 if (x->replay_esn) {
1754 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1755 xfrm_replay_state_esn_len(x->replay_esn),
1758 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1763 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1767 if (id->flags & XFRM_AE_RTHR) {
1768 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1772 if (id->flags & XFRM_AE_ETHR) {
1773 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1774 x->replay_maxage * 10 / HZ);
1778 err = xfrm_mark_put(skb, &x->mark);
1782 return nlmsg_end(skb, nlh);
1785 nlmsg_cancel(skb, nlh);
1789 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1790 struct nlattr **attrs)
1792 struct net *net = sock_net(skb->sk);
1793 struct xfrm_state *x;
1794 struct sk_buff *r_skb;
1799 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1800 struct xfrm_usersa_id *id = &p->sa_id;
1802 mark = xfrm_mark_get(attrs, &m);
1804 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1808 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1809 if (r_skb == NULL) {
1815 * XXX: is this lock really needed - none of the other
1816 * gets lock (the concern is things getting updated
1817 * while we are still reading) - jhs
1819 spin_lock_bh(&x->lock);
1820 c.data.aevent = p->flags;
1821 c.seq = nlh->nlmsg_seq;
1822 c.portid = nlh->nlmsg_pid;
1824 if (build_aevent(r_skb, x, &c) < 0)
1826 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1827 spin_unlock_bh(&x->lock);
1832 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1833 struct nlattr **attrs)
1835 struct net *net = sock_net(skb->sk);
1836 struct xfrm_state *x;
1841 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1842 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1843 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1844 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1846 if (!lt && !rp && !re)
1849 /* pedantic mode - thou shalt sayeth replaceth */
1850 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1853 mark = xfrm_mark_get(attrs, &m);
1855 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1859 if (x->km.state != XFRM_STATE_VALID)
1862 err = xfrm_replay_verify_len(x->replay_esn, re);
1866 spin_lock_bh(&x->lock);
1867 xfrm_update_ae_params(x, attrs, 1);
1868 spin_unlock_bh(&x->lock);
1870 c.event = nlh->nlmsg_type;
1871 c.seq = nlh->nlmsg_seq;
1872 c.portid = nlh->nlmsg_pid;
1873 c.data.aevent = XFRM_AE_CU;
1874 km_state_notify(x, &c);
1881 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1882 struct nlattr **attrs)
1884 struct net *net = sock_net(skb->sk);
1886 u8 type = XFRM_POLICY_TYPE_MAIN;
1889 err = copy_from_user_policy_type(&type, attrs);
1893 err = xfrm_policy_flush(net, type, true);
1895 if (err == -ESRCH) /* empty table */
1901 c.event = nlh->nlmsg_type;
1902 c.seq = nlh->nlmsg_seq;
1903 c.portid = nlh->nlmsg_pid;
1905 km_policy_notify(NULL, 0, &c);
1909 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1910 struct nlattr **attrs)
1912 struct net *net = sock_net(skb->sk);
1913 struct xfrm_policy *xp;
1914 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1915 struct xfrm_userpolicy_info *p = &up->pol;
1916 u8 type = XFRM_POLICY_TYPE_MAIN;
1919 u32 mark = xfrm_mark_get(attrs, &m);
1921 err = copy_from_user_policy_type(&type, attrs);
1925 err = verify_policy_dir(p->dir);
1930 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1932 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1933 struct xfrm_sec_ctx *ctx;
1935 err = verify_sec_ctx_len(attrs);
1941 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1943 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1947 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1948 &p->sel, ctx, 0, &err);
1949 security_xfrm_policy_free(ctx);
1954 if (unlikely(xp->walk.dead))
1959 xfrm_policy_delete(xp, p->dir);
1960 xfrm_audit_policy_delete(xp, 1, true);
1962 // reset the timers here?
1963 WARN(1, "Dont know what to do with soft policy expire\n");
1965 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
1972 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1973 struct nlattr **attrs)
1975 struct net *net = sock_net(skb->sk);
1976 struct xfrm_state *x;
1978 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1979 struct xfrm_usersa_info *p = &ue->state;
1981 u32 mark = xfrm_mark_get(attrs, &m);
1983 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1989 spin_lock_bh(&x->lock);
1991 if (x->km.state != XFRM_STATE_VALID)
1993 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
1996 __xfrm_state_delete(x);
1997 xfrm_audit_state_delete(x, 1, true);
2001 spin_unlock_bh(&x->lock);
2006 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2007 struct nlattr **attrs)
2009 struct net *net = sock_net(skb->sk);
2010 struct xfrm_policy *xp;
2011 struct xfrm_user_tmpl *ut;
2013 struct nlattr *rt = attrs[XFRMA_TMPL];
2014 struct xfrm_mark mark;
2016 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2017 struct xfrm_state *x = xfrm_state_alloc(net);
2023 xfrm_mark_get(attrs, &mark);
2025 err = verify_newpolicy_info(&ua->policy);
2030 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2034 memcpy(&x->id, &ua->id, sizeof(ua->id));
2035 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2036 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2037 xp->mark.m = x->mark.m = mark.m;
2038 xp->mark.v = x->mark.v = mark.v;
2040 /* extract the templates and for each call km_key */
2041 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2042 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2043 memcpy(&x->id, &t->id, sizeof(x->id));
2044 x->props.mode = t->mode;
2045 x->props.reqid = t->reqid;
2046 x->props.family = ut->family;
2047 t->aalgos = ua->aalgos;
2048 t->ealgos = ua->ealgos;
2049 t->calgos = ua->calgos;
2050 err = km_query(x, t, xp);
2060 WARN(1, "BAD policy passed\n");
2067 #ifdef CONFIG_XFRM_MIGRATE
2068 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2069 struct xfrm_kmaddress *k,
2070 struct nlattr **attrs, int *num)
2072 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2073 struct xfrm_user_migrate *um;
2077 struct xfrm_user_kmaddress *uk;
2079 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2080 memcpy(&k->local, &uk->local, sizeof(k->local));
2081 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2082 k->family = uk->family;
2083 k->reserved = uk->reserved;
2087 num_migrate = nla_len(rt) / sizeof(*um);
2089 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2092 for (i = 0; i < num_migrate; i++, um++, ma++) {
2093 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2094 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2095 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2096 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2098 ma->proto = um->proto;
2099 ma->mode = um->mode;
2100 ma->reqid = um->reqid;
2102 ma->old_family = um->old_family;
2103 ma->new_family = um->new_family;
2110 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2111 struct nlattr **attrs)
2113 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2114 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2115 struct xfrm_kmaddress km, *kmp;
2119 struct net *net = sock_net(skb->sk);
2121 if (attrs[XFRMA_MIGRATE] == NULL)
2124 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2126 err = copy_from_user_policy_type(&type, attrs);
2130 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2137 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2142 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2143 struct nlattr **attrs)
2145 return -ENOPROTOOPT;
2149 #ifdef CONFIG_XFRM_MIGRATE
2150 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2152 struct xfrm_user_migrate um;
2154 memset(&um, 0, sizeof(um));
2155 um.proto = m->proto;
2157 um.reqid = m->reqid;
2158 um.old_family = m->old_family;
2159 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2160 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2161 um.new_family = m->new_family;
2162 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2163 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2165 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2168 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2170 struct xfrm_user_kmaddress uk;
2172 memset(&uk, 0, sizeof(uk));
2173 uk.family = k->family;
2174 uk.reserved = k->reserved;
2175 memcpy(&uk.local, &k->local, sizeof(uk.local));
2176 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2178 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2181 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2183 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2184 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2185 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2186 + userpolicy_type_attrsize();
2189 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2190 int num_migrate, const struct xfrm_kmaddress *k,
2191 const struct xfrm_selector *sel, u8 dir, u8 type)
2193 const struct xfrm_migrate *mp;
2194 struct xfrm_userpolicy_id *pol_id;
2195 struct nlmsghdr *nlh;
2198 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2202 pol_id = nlmsg_data(nlh);
2203 /* copy data from selector, dir, and type to the pol_id */
2204 memset(pol_id, 0, sizeof(*pol_id));
2205 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2209 err = copy_to_user_kmaddress(k, skb);
2213 err = copy_to_user_policy_type(type, skb);
2216 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2217 err = copy_to_user_migrate(mp, skb);
2222 return nlmsg_end(skb, nlh);
2225 nlmsg_cancel(skb, nlh);
2229 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2230 const struct xfrm_migrate *m, int num_migrate,
2231 const struct xfrm_kmaddress *k)
2233 struct net *net = &init_net;
2234 struct sk_buff *skb;
2236 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2241 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2244 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2247 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2248 const struct xfrm_migrate *m, int num_migrate,
2249 const struct xfrm_kmaddress *k)
2251 return -ENOPROTOOPT;
2255 #define XMSGSIZE(type) sizeof(struct type)
2257 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2258 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2259 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2260 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2261 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2262 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2263 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2264 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2265 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2266 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2267 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2268 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2269 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2270 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2271 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2272 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2273 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2274 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2275 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2276 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2277 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2282 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2283 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2284 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2285 [XFRMA_LASTUSED] = { .type = NLA_U64},
2286 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2287 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2288 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2289 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2290 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2291 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2292 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2293 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2294 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2295 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2296 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2297 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2298 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2299 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2300 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2301 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2302 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2303 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2304 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2305 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2306 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2307 [XFRMA_PROTO] = { .type = NLA_U8 },
2308 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2311 static const struct xfrm_link {
2312 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2313 int (*dump)(struct sk_buff *, struct netlink_callback *);
2314 int (*done)(struct netlink_callback *);
2315 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2316 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2317 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2318 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2319 .dump = xfrm_dump_sa,
2320 .done = xfrm_dump_sa_done },
2321 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2322 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2323 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2324 .dump = xfrm_dump_policy,
2325 .done = xfrm_dump_policy_done },
2326 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2327 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2328 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2329 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2330 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2331 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2332 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2333 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2334 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2335 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2336 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2337 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2338 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2341 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2343 struct net *net = sock_net(skb->sk);
2344 struct nlattr *attrs[XFRMA_MAX+1];
2345 const struct xfrm_link *link;
2348 type = nlh->nlmsg_type;
2349 if (type > XFRM_MSG_MAX)
2352 type -= XFRM_MSG_BASE;
2353 link = &xfrm_dispatch[type];
2355 /* All operations require privileges, even GET */
2356 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2359 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2360 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2361 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2362 if (link->dump == NULL)
2366 struct netlink_dump_control c = {
2370 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2374 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2379 if (link->doit == NULL)
2382 return link->doit(skb, nlh, attrs);
2385 static void xfrm_netlink_rcv(struct sk_buff *skb)
2387 struct net *net = sock_net(skb->sk);
2389 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2390 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2391 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2394 static inline size_t xfrm_expire_msgsize(void)
2396 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2397 + nla_total_size(sizeof(struct xfrm_mark));
2400 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2402 struct xfrm_user_expire *ue;
2403 struct nlmsghdr *nlh;
2406 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2410 ue = nlmsg_data(nlh);
2411 copy_to_user_state(x, &ue->state);
2412 ue->hard = (c->data.hard != 0) ? 1 : 0;
2414 err = xfrm_mark_put(skb, &x->mark);
2418 return nlmsg_end(skb, nlh);
2421 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2423 struct net *net = xs_net(x);
2424 struct sk_buff *skb;
2426 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2430 if (build_expire(skb, x, c) < 0) {
2435 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2438 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2440 struct net *net = xs_net(x);
2441 struct sk_buff *skb;
2443 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2447 if (build_aevent(skb, x, c) < 0)
2450 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2453 static int xfrm_notify_sa_flush(const struct km_event *c)
2455 struct net *net = c->net;
2456 struct xfrm_usersa_flush *p;
2457 struct nlmsghdr *nlh;
2458 struct sk_buff *skb;
2459 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2461 skb = nlmsg_new(len, GFP_ATOMIC);
2465 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2471 p = nlmsg_data(nlh);
2472 p->proto = c->data.proto;
2474 nlmsg_end(skb, nlh);
2476 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2479 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2483 l += nla_total_size(aead_len(x->aead));
2485 l += nla_total_size(sizeof(struct xfrm_algo) +
2486 (x->aalg->alg_key_len + 7) / 8);
2487 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2490 l += nla_total_size(xfrm_alg_len(x->ealg));
2492 l += nla_total_size(sizeof(*x->calg));
2494 l += nla_total_size(sizeof(*x->encap));
2496 l += nla_total_size(sizeof(x->tfcpad));
2498 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2500 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2501 x->security->ctx_len);
2503 l += nla_total_size(sizeof(*x->coaddr));
2504 if (x->props.extra_flags)
2505 l += nla_total_size(sizeof(x->props.extra_flags));
2507 /* Must count x->lastused as it may become non-zero behind our back. */
2508 l += nla_total_size(sizeof(u64));
2513 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2515 struct net *net = xs_net(x);
2516 struct xfrm_usersa_info *p;
2517 struct xfrm_usersa_id *id;
2518 struct nlmsghdr *nlh;
2519 struct sk_buff *skb;
2520 int len = xfrm_sa_len(x);
2523 headlen = sizeof(*p);
2524 if (c->event == XFRM_MSG_DELSA) {
2525 len += nla_total_size(headlen);
2526 headlen = sizeof(*id);
2527 len += nla_total_size(sizeof(struct xfrm_mark));
2529 len += NLMSG_ALIGN(headlen);
2531 skb = nlmsg_new(len, GFP_ATOMIC);
2535 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2540 p = nlmsg_data(nlh);
2541 if (c->event == XFRM_MSG_DELSA) {
2542 struct nlattr *attr;
2544 id = nlmsg_data(nlh);
2545 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2546 id->spi = x->id.spi;
2547 id->family = x->props.family;
2548 id->proto = x->id.proto;
2550 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2557 err = copy_to_user_state_extra(x, p, skb);
2561 nlmsg_end(skb, nlh);
2563 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2570 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2574 case XFRM_MSG_EXPIRE:
2575 return xfrm_exp_state_notify(x, c);
2576 case XFRM_MSG_NEWAE:
2577 return xfrm_aevent_state_notify(x, c);
2578 case XFRM_MSG_DELSA:
2579 case XFRM_MSG_UPDSA:
2580 case XFRM_MSG_NEWSA:
2581 return xfrm_notify_sa(x, c);
2582 case XFRM_MSG_FLUSHSA:
2583 return xfrm_notify_sa_flush(c);
2585 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2594 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2595 struct xfrm_policy *xp)
2597 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2598 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2599 + nla_total_size(sizeof(struct xfrm_mark))
2600 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2601 + userpolicy_type_attrsize();
2604 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2605 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2607 __u32 seq = xfrm_get_acqseq();
2608 struct xfrm_user_acquire *ua;
2609 struct nlmsghdr *nlh;
2612 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2616 ua = nlmsg_data(nlh);
2617 memcpy(&ua->id, &x->id, sizeof(ua->id));
2618 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2619 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2620 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2621 ua->aalgos = xt->aalgos;
2622 ua->ealgos = xt->ealgos;
2623 ua->calgos = xt->calgos;
2624 ua->seq = x->km.seq = seq;
2626 err = copy_to_user_tmpl(xp, skb);
2628 err = copy_to_user_state_sec_ctx(x, skb);
2630 err = copy_to_user_policy_type(xp->type, skb);
2632 err = xfrm_mark_put(skb, &xp->mark);
2634 nlmsg_cancel(skb, nlh);
2638 return nlmsg_end(skb, nlh);
2641 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2642 struct xfrm_policy *xp)
2644 struct net *net = xs_net(x);
2645 struct sk_buff *skb;
2647 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2651 if (build_acquire(skb, x, xt, xp) < 0)
2654 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2657 /* User gives us xfrm_user_policy_info followed by an array of 0
2658 * or more templates.
2660 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2661 u8 *data, int len, int *dir)
2663 struct net *net = sock_net(sk);
2664 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2665 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2666 struct xfrm_policy *xp;
2669 switch (sk->sk_family) {
2671 if (opt != IP_XFRM_POLICY) {
2676 #if IS_ENABLED(CONFIG_IPV6)
2678 if (opt != IPV6_XFRM_POLICY) {
2691 if (len < sizeof(*p) ||
2692 verify_newpolicy_info(p))
2695 nr = ((len - sizeof(*p)) / sizeof(*ut));
2696 if (validate_tmpl(nr, ut, p->sel.family))
2699 if (p->dir > XFRM_POLICY_OUT)
2702 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2708 copy_from_user_policy(xp, p);
2709 xp->type = XFRM_POLICY_TYPE_MAIN;
2710 copy_templates(xp, ut, nr);
2717 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2719 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2720 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2721 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2722 + nla_total_size(sizeof(struct xfrm_mark))
2723 + userpolicy_type_attrsize();
2726 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2727 int dir, const struct km_event *c)
2729 struct xfrm_user_polexpire *upe;
2730 int hard = c->data.hard;
2731 struct nlmsghdr *nlh;
2734 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2738 upe = nlmsg_data(nlh);
2739 copy_to_user_policy(xp, &upe->pol, dir);
2740 err = copy_to_user_tmpl(xp, skb);
2742 err = copy_to_user_sec_ctx(xp, skb);
2744 err = copy_to_user_policy_type(xp->type, skb);
2746 err = xfrm_mark_put(skb, &xp->mark);
2748 nlmsg_cancel(skb, nlh);
2753 return nlmsg_end(skb, nlh);
2756 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2758 struct net *net = xp_net(xp);
2759 struct sk_buff *skb;
2761 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2765 if (build_polexpire(skb, xp, dir, c) < 0)
2768 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2771 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2773 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2774 struct net *net = xp_net(xp);
2775 struct xfrm_userpolicy_info *p;
2776 struct xfrm_userpolicy_id *id;
2777 struct nlmsghdr *nlh;
2778 struct sk_buff *skb;
2781 headlen = sizeof(*p);
2782 if (c->event == XFRM_MSG_DELPOLICY) {
2783 len += nla_total_size(headlen);
2784 headlen = sizeof(*id);
2786 len += userpolicy_type_attrsize();
2787 len += nla_total_size(sizeof(struct xfrm_mark));
2788 len += NLMSG_ALIGN(headlen);
2790 skb = nlmsg_new(len, GFP_ATOMIC);
2794 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2799 p = nlmsg_data(nlh);
2800 if (c->event == XFRM_MSG_DELPOLICY) {
2801 struct nlattr *attr;
2803 id = nlmsg_data(nlh);
2804 memset(id, 0, sizeof(*id));
2807 id->index = xp->index;
2809 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2811 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2819 copy_to_user_policy(xp, p, dir);
2820 err = copy_to_user_tmpl(xp, skb);
2822 err = copy_to_user_policy_type(xp->type, skb);
2824 err = xfrm_mark_put(skb, &xp->mark);
2828 nlmsg_end(skb, nlh);
2830 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2837 static int xfrm_notify_policy_flush(const struct km_event *c)
2839 struct net *net = c->net;
2840 struct nlmsghdr *nlh;
2841 struct sk_buff *skb;
2844 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2848 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2852 err = copy_to_user_policy_type(c->data.type, skb);
2856 nlmsg_end(skb, nlh);
2858 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2865 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2869 case XFRM_MSG_NEWPOLICY:
2870 case XFRM_MSG_UPDPOLICY:
2871 case XFRM_MSG_DELPOLICY:
2872 return xfrm_notify_policy(xp, dir, c);
2873 case XFRM_MSG_FLUSHPOLICY:
2874 return xfrm_notify_policy_flush(c);
2875 case XFRM_MSG_POLEXPIRE:
2876 return xfrm_exp_policy_notify(xp, dir, c);
2878 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2886 static inline size_t xfrm_report_msgsize(void)
2888 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2891 static int build_report(struct sk_buff *skb, u8 proto,
2892 struct xfrm_selector *sel, xfrm_address_t *addr)
2894 struct xfrm_user_report *ur;
2895 struct nlmsghdr *nlh;
2897 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2901 ur = nlmsg_data(nlh);
2903 memcpy(&ur->sel, sel, sizeof(ur->sel));
2906 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2908 nlmsg_cancel(skb, nlh);
2912 return nlmsg_end(skb, nlh);
2915 static int xfrm_send_report(struct net *net, u8 proto,
2916 struct xfrm_selector *sel, xfrm_address_t *addr)
2918 struct sk_buff *skb;
2920 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2924 if (build_report(skb, proto, sel, addr) < 0)
2927 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
2930 static inline size_t xfrm_mapping_msgsize(void)
2932 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2935 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2936 xfrm_address_t *new_saddr, __be16 new_sport)
2938 struct xfrm_user_mapping *um;
2939 struct nlmsghdr *nlh;
2941 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2945 um = nlmsg_data(nlh);
2947 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2948 um->id.spi = x->id.spi;
2949 um->id.family = x->props.family;
2950 um->id.proto = x->id.proto;
2951 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2952 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2953 um->new_sport = new_sport;
2954 um->old_sport = x->encap->encap_sport;
2955 um->reqid = x->props.reqid;
2957 return nlmsg_end(skb, nlh);
2960 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2963 struct net *net = xs_net(x);
2964 struct sk_buff *skb;
2966 if (x->id.proto != IPPROTO_ESP)
2972 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2976 if (build_mapping(skb, x, ipaddr, sport) < 0)
2979 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
2982 static bool xfrm_is_alive(const struct km_event *c)
2984 return (bool)xfrm_acquire_is_on(c->net);
2987 static struct xfrm_mgr netlink_mgr = {
2989 .notify = xfrm_send_state_notify,
2990 .acquire = xfrm_send_acquire,
2991 .compile_policy = xfrm_compile_policy,
2992 .notify_policy = xfrm_send_policy_notify,
2993 .report = xfrm_send_report,
2994 .migrate = xfrm_send_migrate,
2995 .new_mapping = xfrm_send_mapping,
2996 .is_alive = xfrm_is_alive,
2999 static int __net_init xfrm_user_net_init(struct net *net)
3002 struct netlink_kernel_cfg cfg = {
3003 .groups = XFRMNLGRP_MAX,
3004 .input = xfrm_netlink_rcv,
3007 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3010 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3011 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3015 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3018 list_for_each_entry(net, net_exit_list, exit_list)
3019 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3021 list_for_each_entry(net, net_exit_list, exit_list)
3022 netlink_kernel_release(net->xfrm.nlsk_stash);
3025 static struct pernet_operations xfrm_user_net_ops = {
3026 .init = xfrm_user_net_init,
3027 .exit_batch = xfrm_user_net_exit,
3030 static int __init xfrm_user_init(void)
3034 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3036 rv = register_pernet_subsys(&xfrm_user_net_ops);
3039 rv = xfrm_register_km(&netlink_mgr);
3041 unregister_pernet_subsys(&xfrm_user_net_ops);
3045 static void __exit xfrm_user_exit(void)
3047 xfrm_unregister_km(&netlink_mgr);
3048 unregister_pernet_subsys(&xfrm_user_net_ops);
3051 module_init(xfrm_user_init);
3052 module_exit(xfrm_user_exit);
3053 MODULE_LICENSE("GPL");
3054 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);