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 inline int aead_len(struct xfrm_algo_aead *alg)
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
40 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
42 struct nlattr *rt = attrs[type];
43 struct xfrm_algo *algp;
49 if (nla_len(rt) < xfrm_alg_len(algp))
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
66 static int verify_auth_trunc(struct nlattr **attrs)
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
82 static int verify_aead(struct nlattr **attrs)
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
91 if (nla_len(rt) < aead_len(algp))
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
98 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
99 xfrm_address_t **addrp)
101 struct nlattr *rt = attrs[type];
104 *addrp = nla_data(rt);
107 static inline int verify_sec_ctx_len(struct nlattr **attrs)
109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
110 struct xfrm_user_sec_ctx *uctx;
116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
122 static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
126 struct xfrm_replay_state_esn *rs;
128 if (p->flags & XFRM_STATE_ESN) {
134 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
137 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
138 nla_len(rt) != sizeof(*rs))
145 if (p->id.proto != IPPROTO_ESP)
148 if (p->replay_window != 0)
154 static int verify_newsa_info(struct xfrm_usersa_info *p,
155 struct nlattr **attrs)
165 #if IS_ENABLED(CONFIG_IPV6)
177 switch (p->id.proto) {
179 if ((!attrs[XFRMA_ALG_AUTH] &&
180 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
181 attrs[XFRMA_ALG_AEAD] ||
182 attrs[XFRMA_ALG_CRYPT] ||
183 attrs[XFRMA_ALG_COMP] ||
189 if (attrs[XFRMA_ALG_COMP])
191 if (!attrs[XFRMA_ALG_AUTH] &&
192 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
193 !attrs[XFRMA_ALG_CRYPT] &&
194 !attrs[XFRMA_ALG_AEAD])
196 if ((attrs[XFRMA_ALG_AUTH] ||
197 attrs[XFRMA_ALG_AUTH_TRUNC] ||
198 attrs[XFRMA_ALG_CRYPT]) &&
199 attrs[XFRMA_ALG_AEAD])
201 if (attrs[XFRMA_TFCPAD] &&
202 p->mode != XFRM_MODE_TUNNEL)
207 if (!attrs[XFRMA_ALG_COMP] ||
208 attrs[XFRMA_ALG_AEAD] ||
209 attrs[XFRMA_ALG_AUTH] ||
210 attrs[XFRMA_ALG_AUTH_TRUNC] ||
211 attrs[XFRMA_ALG_CRYPT] ||
216 #if IS_ENABLED(CONFIG_IPV6)
217 case IPPROTO_DSTOPTS:
218 case IPPROTO_ROUTING:
219 if (attrs[XFRMA_ALG_COMP] ||
220 attrs[XFRMA_ALG_AUTH] ||
221 attrs[XFRMA_ALG_AUTH_TRUNC] ||
222 attrs[XFRMA_ALG_AEAD] ||
223 attrs[XFRMA_ALG_CRYPT] ||
224 attrs[XFRMA_ENCAP] ||
225 attrs[XFRMA_SEC_CTX] ||
226 attrs[XFRMA_TFCPAD] ||
227 !attrs[XFRMA_COADDR])
236 if ((err = verify_aead(attrs)))
238 if ((err = verify_auth_trunc(attrs)))
240 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
242 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
244 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
246 if ((err = verify_sec_ctx_len(attrs)))
248 if ((err = verify_replay(p, attrs)))
253 case XFRM_MODE_TRANSPORT:
254 case XFRM_MODE_TUNNEL:
255 case XFRM_MODE_ROUTEOPTIMIZATION:
269 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
270 struct xfrm_algo_desc *(*get_byname)(const char *, int),
273 struct xfrm_algo *p, *ualg;
274 struct xfrm_algo_desc *algo;
279 ualg = nla_data(rta);
281 algo = get_byname(ualg->alg_name, 1);
284 *props = algo->desc.sadb_alg_id;
286 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
290 strcpy(p->alg_name, algo->name);
295 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
298 struct xfrm_algo *ualg;
299 struct xfrm_algo_auth *p;
300 struct xfrm_algo_desc *algo;
305 ualg = nla_data(rta);
307 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
310 *props = algo->desc.sadb_alg_id;
312 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
316 strcpy(p->alg_name, algo->name);
317 p->alg_key_len = ualg->alg_key_len;
318 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
319 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
325 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
328 struct xfrm_algo_auth *p, *ualg;
329 struct xfrm_algo_desc *algo;
334 ualg = nla_data(rta);
336 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
339 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
342 *props = algo->desc.sadb_alg_id;
344 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
348 strcpy(p->alg_name, algo->name);
349 if (!p->alg_trunc_len)
350 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
356 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
359 struct xfrm_algo_aead *p, *ualg;
360 struct xfrm_algo_desc *algo;
365 ualg = nla_data(rta);
367 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
370 *props = algo->desc.sadb_alg_id;
372 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
376 strcpy(p->alg_name, algo->name);
381 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
384 struct xfrm_replay_state_esn *up;
387 if (!replay_esn || !rp)
391 ulen = xfrm_replay_state_esn_len(up);
393 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
399 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
400 struct xfrm_replay_state_esn **preplay_esn,
403 struct xfrm_replay_state_esn *p, *pp, *up;
410 klen = xfrm_replay_state_esn_len(up);
411 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
413 p = kzalloc(klen, GFP_KERNEL);
417 pp = kzalloc(klen, GFP_KERNEL);
424 memcpy(pp, up, ulen);
432 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
437 len += sizeof(struct xfrm_user_sec_ctx);
438 len += xfrm_ctx->ctx_len;
443 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
445 memcpy(&x->id, &p->id, sizeof(x->id));
446 memcpy(&x->sel, &p->sel, sizeof(x->sel));
447 memcpy(&x->lft, &p->lft, sizeof(x->lft));
448 x->props.mode = p->mode;
449 x->props.replay_window = p->replay_window;
450 x->props.reqid = p->reqid;
451 x->props.family = p->family;
452 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
453 x->props.flags = p->flags;
455 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
456 x->sel.family = p->family;
460 * someday when pfkey also has support, we could have the code
461 * somehow made shareable and move it to xfrm_state.c - JHS
464 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
467 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
468 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
469 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
470 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
471 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
474 struct xfrm_replay_state_esn *replay_esn;
475 replay_esn = nla_data(re);
476 memcpy(x->replay_esn, replay_esn,
477 xfrm_replay_state_esn_len(replay_esn));
478 memcpy(x->preplay_esn, replay_esn,
479 xfrm_replay_state_esn_len(replay_esn));
483 struct xfrm_replay_state *replay;
484 replay = nla_data(rp);
485 memcpy(&x->replay, replay, sizeof(*replay));
486 memcpy(&x->preplay, replay, sizeof(*replay));
490 struct xfrm_lifetime_cur *ltime;
491 ltime = nla_data(lt);
492 x->curlft.bytes = ltime->bytes;
493 x->curlft.packets = ltime->packets;
494 x->curlft.add_time = ltime->add_time;
495 x->curlft.use_time = ltime->use_time;
499 x->replay_maxage = nla_get_u32(et);
502 x->replay_maxdiff = nla_get_u32(rt);
505 static struct xfrm_state *xfrm_state_construct(struct net *net,
506 struct xfrm_usersa_info *p,
507 struct nlattr **attrs,
510 struct xfrm_state *x = xfrm_state_alloc(net);
516 copy_from_user_state(x, p);
518 if ((err = attach_aead(&x->aead, &x->props.ealgo,
519 attrs[XFRMA_ALG_AEAD])))
521 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
522 attrs[XFRMA_ALG_AUTH_TRUNC])))
524 if (!x->props.aalgo) {
525 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
526 attrs[XFRMA_ALG_AUTH])))
529 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
530 xfrm_ealg_get_byname,
531 attrs[XFRMA_ALG_CRYPT])))
533 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
534 xfrm_calg_get_byname,
535 attrs[XFRMA_ALG_COMP])))
538 if (attrs[XFRMA_ENCAP]) {
539 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
540 sizeof(*x->encap), GFP_KERNEL);
541 if (x->encap == NULL)
545 if (attrs[XFRMA_TFCPAD])
546 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
548 if (attrs[XFRMA_COADDR]) {
549 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
550 sizeof(*x->coaddr), GFP_KERNEL);
551 if (x->coaddr == NULL)
555 xfrm_mark_get(attrs, &x->mark);
557 err = __xfrm_init_state(x, false);
561 if (attrs[XFRMA_SEC_CTX] &&
562 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
565 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
566 attrs[XFRMA_REPLAY_ESN_VAL])))
570 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
571 /* sysctl_xfrm_aevent_etime is in 100ms units */
572 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
574 if ((err = xfrm_init_replay(x)))
577 /* override default values from above */
578 xfrm_update_ae_params(x, attrs, 0);
583 x->km.state = XFRM_STATE_DEAD;
590 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
591 struct nlattr **attrs)
593 struct net *net = sock_net(skb->sk);
594 struct xfrm_usersa_info *p = nlmsg_data(nlh);
595 struct xfrm_state *x;
598 uid_t loginuid = audit_get_loginuid(current);
599 u32 sessionid = audit_get_sessionid(current);
602 err = verify_newsa_info(p, attrs);
606 x = xfrm_state_construct(net, p, attrs, &err);
611 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
612 err = xfrm_state_add(x);
614 err = xfrm_state_update(x);
616 security_task_getsecid(current, &sid);
617 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
620 x->km.state = XFRM_STATE_DEAD;
625 c.seq = nlh->nlmsg_seq;
626 c.pid = nlh->nlmsg_pid;
627 c.event = nlh->nlmsg_type;
629 km_state_notify(x, &c);
635 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
636 struct xfrm_usersa_id *p,
637 struct nlattr **attrs,
640 struct xfrm_state *x = NULL;
643 u32 mark = xfrm_mark_get(attrs, &m);
645 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
647 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
649 xfrm_address_t *saddr = NULL;
651 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
658 x = xfrm_state_lookup_byaddr(net, mark,
660 p->proto, p->family);
669 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
670 struct nlattr **attrs)
672 struct net *net = sock_net(skb->sk);
673 struct xfrm_state *x;
676 struct xfrm_usersa_id *p = nlmsg_data(nlh);
677 uid_t loginuid = audit_get_loginuid(current);
678 u32 sessionid = audit_get_sessionid(current);
681 x = xfrm_user_state_lookup(net, p, attrs, &err);
685 if ((err = security_xfrm_state_delete(x)) != 0)
688 if (xfrm_state_kern(x)) {
693 err = xfrm_state_delete(x);
698 c.seq = nlh->nlmsg_seq;
699 c.pid = nlh->nlmsg_pid;
700 c.event = nlh->nlmsg_type;
701 km_state_notify(x, &c);
704 security_task_getsecid(current, &sid);
705 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
710 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
712 memset(p, 0, sizeof(*p));
713 memcpy(&p->id, &x->id, sizeof(p->id));
714 memcpy(&p->sel, &x->sel, sizeof(p->sel));
715 memcpy(&p->lft, &x->lft, sizeof(p->lft));
716 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
717 memcpy(&p->stats, &x->stats, sizeof(p->stats));
718 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
719 p->mode = x->props.mode;
720 p->replay_window = x->props.replay_window;
721 p->reqid = x->props.reqid;
722 p->family = x->props.family;
723 p->flags = x->props.flags;
727 struct xfrm_dump_info {
728 struct sk_buff *in_skb;
729 struct sk_buff *out_skb;
734 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
736 struct xfrm_user_sec_ctx *uctx;
738 int ctx_size = sizeof(*uctx) + s->ctx_len;
740 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
744 uctx = nla_data(attr);
745 uctx->exttype = XFRMA_SEC_CTX;
746 uctx->len = ctx_size;
747 uctx->ctx_doi = s->ctx_doi;
748 uctx->ctx_alg = s->ctx_alg;
749 uctx->ctx_len = s->ctx_len;
750 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
755 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
757 struct xfrm_algo *algo;
760 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
761 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
765 algo = nla_data(nla);
766 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
767 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
768 algo->alg_key_len = auth->alg_key_len;
773 /* Don't change this without updating xfrm_sa_len! */
774 static int copy_to_user_state_extra(struct xfrm_state *x,
775 struct xfrm_usersa_info *p,
780 copy_to_user_state(x, p);
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).pid, 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 xfrm_state_walk_done(walk);
873 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
875 struct net *net = sock_net(skb->sk);
876 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
877 struct xfrm_dump_info info;
879 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
880 sizeof(cb->args) - sizeof(cb->args[0]));
882 info.in_skb = cb->skb;
884 info.nlmsg_seq = cb->nlh->nlmsg_seq;
885 info.nlmsg_flags = NLM_F_MULTI;
889 xfrm_state_walk_init(walk, 0);
892 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
897 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
898 struct xfrm_state *x, u32 seq)
900 struct xfrm_dump_info info;
904 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
906 return ERR_PTR(-ENOMEM);
908 info.in_skb = in_skb;
910 info.nlmsg_seq = seq;
911 info.nlmsg_flags = 0;
913 err = dump_one_state(x, 0, &info);
922 static inline size_t xfrm_spdinfo_msgsize(void)
924 return NLMSG_ALIGN(4)
925 + nla_total_size(sizeof(struct xfrmu_spdinfo))
926 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
929 static int build_spdinfo(struct sk_buff *skb, struct net *net,
930 u32 pid, u32 seq, u32 flags)
932 struct xfrmk_spdinfo si;
933 struct xfrmu_spdinfo spc;
934 struct xfrmu_spdhinfo sph;
935 struct nlmsghdr *nlh;
939 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
940 if (nlh == NULL) /* shouldn't really happen ... */
945 xfrm_spd_getinfo(net, &si);
946 spc.incnt = si.incnt;
947 spc.outcnt = si.outcnt;
948 spc.fwdcnt = si.fwdcnt;
949 spc.inscnt = si.inscnt;
950 spc.outscnt = si.outscnt;
951 spc.fwdscnt = si.fwdscnt;
952 sph.spdhcnt = si.spdhcnt;
953 sph.spdhmcnt = si.spdhmcnt;
955 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
957 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
959 nlmsg_cancel(skb, nlh);
963 return nlmsg_end(skb, nlh);
966 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
967 struct nlattr **attrs)
969 struct net *net = sock_net(skb->sk);
970 struct sk_buff *r_skb;
971 u32 *flags = nlmsg_data(nlh);
972 u32 spid = NETLINK_CB(skb).pid;
973 u32 seq = nlh->nlmsg_seq;
975 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
979 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
982 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
985 static inline size_t xfrm_sadinfo_msgsize(void)
987 return NLMSG_ALIGN(4)
988 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
989 + nla_total_size(4); /* XFRMA_SAD_CNT */
992 static int build_sadinfo(struct sk_buff *skb, struct net *net,
993 u32 pid, u32 seq, u32 flags)
995 struct xfrmk_sadinfo si;
996 struct xfrmu_sadhinfo sh;
997 struct nlmsghdr *nlh;
1001 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1002 if (nlh == NULL) /* shouldn't really happen ... */
1005 f = nlmsg_data(nlh);
1007 xfrm_sad_getinfo(net, &si);
1009 sh.sadhmcnt = si.sadhmcnt;
1010 sh.sadhcnt = si.sadhcnt;
1012 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1014 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1016 nlmsg_cancel(skb, nlh);
1020 return nlmsg_end(skb, nlh);
1023 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1024 struct nlattr **attrs)
1026 struct net *net = sock_net(skb->sk);
1027 struct sk_buff *r_skb;
1028 u32 *flags = nlmsg_data(nlh);
1029 u32 spid = NETLINK_CB(skb).pid;
1030 u32 seq = nlh->nlmsg_seq;
1032 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1036 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
1039 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
1042 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1043 struct nlattr **attrs)
1045 struct net *net = sock_net(skb->sk);
1046 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1047 struct xfrm_state *x;
1048 struct sk_buff *resp_skb;
1051 x = xfrm_user_state_lookup(net, p, attrs, &err);
1055 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1056 if (IS_ERR(resp_skb)) {
1057 err = PTR_ERR(resp_skb);
1059 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1066 static int verify_userspi_info(struct xfrm_userspi_info *p)
1068 switch (p->info.id.proto) {
1074 /* IPCOMP spi is 16-bits. */
1075 if (p->max >= 0x10000)
1083 if (p->min > p->max)
1089 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1090 struct nlattr **attrs)
1092 struct net *net = sock_net(skb->sk);
1093 struct xfrm_state *x;
1094 struct xfrm_userspi_info *p;
1095 struct sk_buff *resp_skb;
1096 xfrm_address_t *daddr;
1102 p = nlmsg_data(nlh);
1103 err = verify_userspi_info(p);
1107 family = p->info.family;
1108 daddr = &p->info.id.daddr;
1112 mark = xfrm_mark_get(attrs, &m);
1114 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1115 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1122 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1123 p->info.id.proto, daddr,
1130 err = xfrm_alloc_spi(x, p->min, p->max);
1134 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1135 if (IS_ERR(resp_skb)) {
1136 err = PTR_ERR(resp_skb);
1140 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1148 static int verify_policy_dir(u8 dir)
1151 case XFRM_POLICY_IN:
1152 case XFRM_POLICY_OUT:
1153 case XFRM_POLICY_FWD:
1163 static int verify_policy_type(u8 type)
1166 case XFRM_POLICY_TYPE_MAIN:
1167 #ifdef CONFIG_XFRM_SUB_POLICY
1168 case XFRM_POLICY_TYPE_SUB:
1179 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1182 case XFRM_SHARE_ANY:
1183 case XFRM_SHARE_SESSION:
1184 case XFRM_SHARE_USER:
1185 case XFRM_SHARE_UNIQUE:
1192 switch (p->action) {
1193 case XFRM_POLICY_ALLOW:
1194 case XFRM_POLICY_BLOCK:
1201 switch (p->sel.family) {
1206 #if IS_ENABLED(CONFIG_IPV6)
1209 return -EAFNOSUPPORT;
1216 return verify_policy_dir(p->dir);
1219 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1221 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1222 struct xfrm_user_sec_ctx *uctx;
1227 uctx = nla_data(rt);
1228 return security_xfrm_policy_alloc(&pol->security, uctx);
1231 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1237 for (i = 0; i < nr; i++, ut++) {
1238 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1240 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1241 memcpy(&t->saddr, &ut->saddr,
1242 sizeof(xfrm_address_t));
1243 t->reqid = ut->reqid;
1245 t->share = ut->share;
1246 t->optional = ut->optional;
1247 t->aalgos = ut->aalgos;
1248 t->ealgos = ut->ealgos;
1249 t->calgos = ut->calgos;
1250 /* If all masks are ~0, then we allow all algorithms. */
1251 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1252 t->encap_family = ut->family;
1256 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1260 if (nr > XFRM_MAX_DEPTH)
1263 for (i = 0; i < nr; i++) {
1264 /* We never validated the ut->family value, so many
1265 * applications simply leave it at zero. The check was
1266 * never made and ut->family was ignored because all
1267 * templates could be assumed to have the same family as
1268 * the policy itself. Now that we will have ipv4-in-ipv6
1269 * and ipv6-in-ipv4 tunnels, this is no longer true.
1272 ut[i].family = family;
1274 switch (ut[i].family) {
1277 #if IS_ENABLED(CONFIG_IPV6)
1289 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1291 struct nlattr *rt = attrs[XFRMA_TMPL];
1296 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1297 int nr = nla_len(rt) / sizeof(*utmpl);
1300 err = validate_tmpl(nr, utmpl, pol->family);
1304 copy_templates(pol, utmpl, nr);
1309 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1311 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1312 struct xfrm_userpolicy_type *upt;
1313 u8 type = XFRM_POLICY_TYPE_MAIN;
1321 err = verify_policy_type(type);
1329 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1331 xp->priority = p->priority;
1332 xp->index = p->index;
1333 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1334 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1335 xp->action = p->action;
1336 xp->flags = p->flags;
1337 xp->family = p->sel.family;
1338 /* XXX xp->share = p->share; */
1341 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1343 memset(p, 0, sizeof(*p));
1344 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1345 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1346 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1347 p->priority = xp->priority;
1348 p->index = xp->index;
1349 p->sel.family = xp->family;
1351 p->action = xp->action;
1352 p->flags = xp->flags;
1353 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1356 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1358 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1366 copy_from_user_policy(xp, p);
1368 err = copy_from_user_policy_type(&xp->type, attrs);
1372 if (!(err = copy_from_user_tmpl(xp, attrs)))
1373 err = copy_from_user_sec_ctx(xp, attrs);
1377 xfrm_mark_get(attrs, &xp->mark);
1383 xfrm_policy_destroy(xp);
1387 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1388 struct nlattr **attrs)
1390 struct net *net = sock_net(skb->sk);
1391 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1392 struct xfrm_policy *xp;
1396 uid_t loginuid = audit_get_loginuid(current);
1397 u32 sessionid = audit_get_sessionid(current);
1400 err = verify_newpolicy_info(p);
1403 err = verify_sec_ctx_len(attrs);
1407 xp = xfrm_policy_construct(net, p, attrs, &err);
1411 /* shouldn't excl be based on nlh flags??
1412 * Aha! this is anti-netlink really i.e more pfkey derived
1413 * in netlink excl is a flag and you wouldnt need
1414 * a type XFRM_MSG_UPDPOLICY - JHS */
1415 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1416 err = xfrm_policy_insert(p->dir, xp, excl);
1417 security_task_getsecid(current, &sid);
1418 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1421 security_xfrm_policy_free(xp->security);
1426 c.event = nlh->nlmsg_type;
1427 c.seq = nlh->nlmsg_seq;
1428 c.pid = nlh->nlmsg_pid;
1429 km_policy_notify(xp, p->dir, &c);
1436 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1438 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1441 if (xp->xfrm_nr == 0)
1444 for (i = 0; i < xp->xfrm_nr; i++) {
1445 struct xfrm_user_tmpl *up = &vec[i];
1446 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1448 memset(up, 0, sizeof(*up));
1449 memcpy(&up->id, &kp->id, sizeof(up->id));
1450 up->family = kp->encap_family;
1451 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1452 up->reqid = kp->reqid;
1453 up->mode = kp->mode;
1454 up->share = kp->share;
1455 up->optional = kp->optional;
1456 up->aalgos = kp->aalgos;
1457 up->ealgos = kp->ealgos;
1458 up->calgos = kp->calgos;
1461 return nla_put(skb, XFRMA_TMPL,
1462 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1465 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1468 return copy_sec_ctx(x->security, skb);
1473 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1476 return copy_sec_ctx(xp->security, skb);
1479 static inline size_t userpolicy_type_attrsize(void)
1481 #ifdef CONFIG_XFRM_SUB_POLICY
1482 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1488 #ifdef CONFIG_XFRM_SUB_POLICY
1489 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1491 struct xfrm_userpolicy_type upt = {
1495 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1499 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1505 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1507 struct xfrm_dump_info *sp = ptr;
1508 struct xfrm_userpolicy_info *p;
1509 struct sk_buff *in_skb = sp->in_skb;
1510 struct sk_buff *skb = sp->out_skb;
1511 struct nlmsghdr *nlh;
1514 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1515 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1519 p = nlmsg_data(nlh);
1520 copy_to_user_policy(xp, p, dir);
1521 err = copy_to_user_tmpl(xp, skb);
1523 err = copy_to_user_sec_ctx(xp, skb);
1525 err = copy_to_user_policy_type(xp->type, skb);
1527 err = xfrm_mark_put(skb, &xp->mark);
1529 nlmsg_cancel(skb, nlh);
1532 nlmsg_end(skb, nlh);
1536 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1538 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1540 xfrm_policy_walk_done(walk);
1544 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1546 struct net *net = sock_net(skb->sk);
1547 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1548 struct xfrm_dump_info info;
1550 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1551 sizeof(cb->args) - sizeof(cb->args[0]));
1553 info.in_skb = cb->skb;
1555 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1556 info.nlmsg_flags = NLM_F_MULTI;
1560 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1563 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1568 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1569 struct xfrm_policy *xp,
1572 struct xfrm_dump_info info;
1573 struct sk_buff *skb;
1576 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1578 return ERR_PTR(-ENOMEM);
1580 info.in_skb = in_skb;
1582 info.nlmsg_seq = seq;
1583 info.nlmsg_flags = 0;
1585 err = dump_one_policy(xp, dir, 0, &info);
1588 return ERR_PTR(err);
1594 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1595 struct nlattr **attrs)
1597 struct net *net = sock_net(skb->sk);
1598 struct xfrm_policy *xp;
1599 struct xfrm_userpolicy_id *p;
1600 u8 type = XFRM_POLICY_TYPE_MAIN;
1605 u32 mark = xfrm_mark_get(attrs, &m);
1607 p = nlmsg_data(nlh);
1608 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1610 err = copy_from_user_policy_type(&type, attrs);
1614 err = verify_policy_dir(p->dir);
1619 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1621 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1622 struct xfrm_sec_ctx *ctx;
1624 err = verify_sec_ctx_len(attrs);
1630 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1632 err = security_xfrm_policy_alloc(&ctx, uctx);
1636 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1638 security_xfrm_policy_free(ctx);
1644 struct sk_buff *resp_skb;
1646 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1647 if (IS_ERR(resp_skb)) {
1648 err = PTR_ERR(resp_skb);
1650 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1651 NETLINK_CB(skb).pid);
1654 uid_t loginuid = audit_get_loginuid(current);
1655 u32 sessionid = audit_get_sessionid(current);
1658 security_task_getsecid(current, &sid);
1659 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1665 c.data.byid = p->index;
1666 c.event = nlh->nlmsg_type;
1667 c.seq = nlh->nlmsg_seq;
1668 c.pid = nlh->nlmsg_pid;
1669 km_policy_notify(xp, p->dir, &c);
1677 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1678 struct nlattr **attrs)
1680 struct net *net = sock_net(skb->sk);
1682 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1683 struct xfrm_audit audit_info;
1686 audit_info.loginuid = audit_get_loginuid(current);
1687 audit_info.sessionid = audit_get_sessionid(current);
1688 security_task_getsecid(current, &audit_info.secid);
1689 err = xfrm_state_flush(net, p->proto, &audit_info);
1691 if (err == -ESRCH) /* empty table */
1695 c.data.proto = p->proto;
1696 c.event = nlh->nlmsg_type;
1697 c.seq = nlh->nlmsg_seq;
1698 c.pid = nlh->nlmsg_pid;
1700 km_state_notify(NULL, &c);
1705 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1707 size_t replay_size = x->replay_esn ?
1708 xfrm_replay_state_esn_len(x->replay_esn) :
1709 sizeof(struct xfrm_replay_state);
1711 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1712 + nla_total_size(replay_size)
1713 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1714 + nla_total_size(sizeof(struct xfrm_mark))
1715 + nla_total_size(4) /* XFRM_AE_RTHR */
1716 + nla_total_size(4); /* XFRM_AE_ETHR */
1719 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1721 struct xfrm_aevent_id *id;
1722 struct nlmsghdr *nlh;
1725 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1729 id = nlmsg_data(nlh);
1730 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1731 id->sa_id.spi = x->id.spi;
1732 id->sa_id.family = x->props.family;
1733 id->sa_id.proto = x->id.proto;
1734 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1735 id->reqid = x->props.reqid;
1736 id->flags = c->data.aevent;
1738 if (x->replay_esn) {
1739 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1740 xfrm_replay_state_esn_len(x->replay_esn),
1743 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1748 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1752 if (id->flags & XFRM_AE_RTHR) {
1753 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1757 if (id->flags & XFRM_AE_ETHR) {
1758 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1759 x->replay_maxage * 10 / HZ);
1763 err = xfrm_mark_put(skb, &x->mark);
1767 return nlmsg_end(skb, nlh);
1770 nlmsg_cancel(skb, nlh);
1774 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1775 struct nlattr **attrs)
1777 struct net *net = sock_net(skb->sk);
1778 struct xfrm_state *x;
1779 struct sk_buff *r_skb;
1784 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1785 struct xfrm_usersa_id *id = &p->sa_id;
1787 mark = xfrm_mark_get(attrs, &m);
1789 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1793 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1794 if (r_skb == NULL) {
1800 * XXX: is this lock really needed - none of the other
1801 * gets lock (the concern is things getting updated
1802 * while we are still reading) - jhs
1804 spin_lock_bh(&x->lock);
1805 c.data.aevent = p->flags;
1806 c.seq = nlh->nlmsg_seq;
1807 c.pid = nlh->nlmsg_pid;
1809 if (build_aevent(r_skb, x, &c) < 0)
1811 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1812 spin_unlock_bh(&x->lock);
1817 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1818 struct nlattr **attrs)
1820 struct net *net = sock_net(skb->sk);
1821 struct xfrm_state *x;
1826 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1827 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1828 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1829 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1831 if (!lt && !rp && !re)
1834 /* pedantic mode - thou shalt sayeth replaceth */
1835 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1838 mark = xfrm_mark_get(attrs, &m);
1840 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1844 if (x->km.state != XFRM_STATE_VALID)
1847 err = xfrm_replay_verify_len(x->replay_esn, rp);
1851 spin_lock_bh(&x->lock);
1852 xfrm_update_ae_params(x, attrs, 1);
1853 spin_unlock_bh(&x->lock);
1855 c.event = nlh->nlmsg_type;
1856 c.seq = nlh->nlmsg_seq;
1857 c.pid = nlh->nlmsg_pid;
1858 c.data.aevent = XFRM_AE_CU;
1859 km_state_notify(x, &c);
1866 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1867 struct nlattr **attrs)
1869 struct net *net = sock_net(skb->sk);
1871 u8 type = XFRM_POLICY_TYPE_MAIN;
1873 struct xfrm_audit audit_info;
1875 err = copy_from_user_policy_type(&type, attrs);
1879 audit_info.loginuid = audit_get_loginuid(current);
1880 audit_info.sessionid = audit_get_sessionid(current);
1881 security_task_getsecid(current, &audit_info.secid);
1882 err = xfrm_policy_flush(net, type, &audit_info);
1884 if (err == -ESRCH) /* empty table */
1890 c.event = nlh->nlmsg_type;
1891 c.seq = nlh->nlmsg_seq;
1892 c.pid = nlh->nlmsg_pid;
1894 km_policy_notify(NULL, 0, &c);
1898 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1899 struct nlattr **attrs)
1901 struct net *net = sock_net(skb->sk);
1902 struct xfrm_policy *xp;
1903 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1904 struct xfrm_userpolicy_info *p = &up->pol;
1905 u8 type = XFRM_POLICY_TYPE_MAIN;
1908 u32 mark = xfrm_mark_get(attrs, &m);
1910 err = copy_from_user_policy_type(&type, attrs);
1914 err = verify_policy_dir(p->dir);
1919 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1921 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1922 struct xfrm_sec_ctx *ctx;
1924 err = verify_sec_ctx_len(attrs);
1930 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1932 err = security_xfrm_policy_alloc(&ctx, uctx);
1936 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1937 &p->sel, ctx, 0, &err);
1938 security_xfrm_policy_free(ctx);
1943 if (unlikely(xp->walk.dead))
1948 uid_t loginuid = audit_get_loginuid(current);
1949 u32 sessionid = audit_get_sessionid(current);
1952 security_task_getsecid(current, &sid);
1953 xfrm_policy_delete(xp, p->dir);
1954 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1957 // reset the timers here?
1958 WARN(1, "Dont know what to do with soft policy expire\n");
1960 km_policy_expired(xp, p->dir, up->hard, current->pid);
1967 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1968 struct nlattr **attrs)
1970 struct net *net = sock_net(skb->sk);
1971 struct xfrm_state *x;
1973 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1974 struct xfrm_usersa_info *p = &ue->state;
1976 u32 mark = xfrm_mark_get(attrs, &m);
1978 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1984 spin_lock_bh(&x->lock);
1986 if (x->km.state != XFRM_STATE_VALID)
1988 km_state_expired(x, ue->hard, current->pid);
1991 uid_t loginuid = audit_get_loginuid(current);
1992 u32 sessionid = audit_get_sessionid(current);
1995 security_task_getsecid(current, &sid);
1996 __xfrm_state_delete(x);
1997 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
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;
2120 if (attrs[XFRMA_MIGRATE] == NULL)
2123 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2125 err = copy_from_user_policy_type(&type, attrs);
2129 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2136 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2141 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2142 struct nlattr **attrs)
2144 return -ENOPROTOOPT;
2148 #ifdef CONFIG_XFRM_MIGRATE
2149 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2151 struct xfrm_user_migrate um;
2153 memset(&um, 0, sizeof(um));
2154 um.proto = m->proto;
2156 um.reqid = m->reqid;
2157 um.old_family = m->old_family;
2158 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2159 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2160 um.new_family = m->new_family;
2161 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2162 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2164 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2167 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2169 struct xfrm_user_kmaddress uk;
2171 memset(&uk, 0, sizeof(uk));
2172 uk.family = k->family;
2173 uk.reserved = k->reserved;
2174 memcpy(&uk.local, &k->local, sizeof(uk.local));
2175 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2177 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2180 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2182 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2183 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2184 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2185 + userpolicy_type_attrsize();
2188 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2189 int num_migrate, const struct xfrm_kmaddress *k,
2190 const struct xfrm_selector *sel, u8 dir, u8 type)
2192 const struct xfrm_migrate *mp;
2193 struct xfrm_userpolicy_id *pol_id;
2194 struct nlmsghdr *nlh;
2197 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2201 pol_id = nlmsg_data(nlh);
2202 /* copy data from selector, dir, and type to the pol_id */
2203 memset(pol_id, 0, sizeof(*pol_id));
2204 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2208 err = copy_to_user_kmaddress(k, skb);
2212 err = copy_to_user_policy_type(type, skb);
2215 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2216 err = copy_to_user_migrate(mp, skb);
2221 return nlmsg_end(skb, nlh);
2224 nlmsg_cancel(skb, nlh);
2228 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2229 const struct xfrm_migrate *m, int num_migrate,
2230 const struct xfrm_kmaddress *k)
2232 struct net *net = &init_net;
2233 struct sk_buff *skb;
2235 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2240 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2243 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2246 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2247 const struct xfrm_migrate *m, int num_migrate,
2248 const struct xfrm_kmaddress *k)
2250 return -ENOPROTOOPT;
2254 #define XMSGSIZE(type) sizeof(struct type)
2256 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2257 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2258 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2259 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2260 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2261 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2262 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2263 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2264 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2265 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2266 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2267 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2268 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2269 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2270 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2271 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2272 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2273 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2274 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2275 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2276 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2281 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2282 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2283 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2284 [XFRMA_LASTUSED] = { .type = NLA_U64},
2285 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2286 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2287 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2288 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2289 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2290 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2291 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2292 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2293 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2294 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2295 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2296 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2297 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2298 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2299 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2300 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2301 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2302 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2303 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2304 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2307 static struct xfrm_link {
2308 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2309 int (*dump)(struct sk_buff *, struct netlink_callback *);
2310 int (*done)(struct netlink_callback *);
2311 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2312 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2313 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2314 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2315 .dump = xfrm_dump_sa,
2316 .done = xfrm_dump_sa_done },
2317 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2318 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2319 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2320 .dump = xfrm_dump_policy,
2321 .done = xfrm_dump_policy_done },
2322 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2323 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2324 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2325 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2326 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2327 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2328 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2329 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2330 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2331 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2332 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2333 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2334 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2337 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2339 struct net *net = sock_net(skb->sk);
2340 struct nlattr *attrs[XFRMA_MAX+1];
2341 struct xfrm_link *link;
2344 type = nlh->nlmsg_type;
2345 if (type > XFRM_MSG_MAX)
2348 type -= XFRM_MSG_BASE;
2349 link = &xfrm_dispatch[type];
2351 /* All operations require privileges, even GET */
2352 if (!capable(CAP_NET_ADMIN))
2355 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2356 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2357 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2358 if (link->dump == NULL)
2362 struct netlink_dump_control c = {
2366 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2370 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2375 if (link->doit == NULL)
2378 return link->doit(skb, nlh, attrs);
2381 static void xfrm_netlink_rcv(struct sk_buff *skb)
2383 mutex_lock(&xfrm_cfg_mutex);
2384 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2385 mutex_unlock(&xfrm_cfg_mutex);
2388 static inline size_t xfrm_expire_msgsize(void)
2390 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2391 + nla_total_size(sizeof(struct xfrm_mark));
2394 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2396 struct xfrm_user_expire *ue;
2397 struct nlmsghdr *nlh;
2400 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2404 ue = nlmsg_data(nlh);
2405 copy_to_user_state(x, &ue->state);
2406 ue->hard = (c->data.hard != 0) ? 1 : 0;
2408 err = xfrm_mark_put(skb, &x->mark);
2412 return nlmsg_end(skb, nlh);
2415 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2417 struct net *net = xs_net(x);
2418 struct sk_buff *skb;
2420 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2424 if (build_expire(skb, x, c) < 0) {
2429 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2432 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2434 struct net *net = xs_net(x);
2435 struct sk_buff *skb;
2437 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2441 if (build_aevent(skb, x, c) < 0)
2444 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2447 static int xfrm_notify_sa_flush(const struct km_event *c)
2449 struct net *net = c->net;
2450 struct xfrm_usersa_flush *p;
2451 struct nlmsghdr *nlh;
2452 struct sk_buff *skb;
2453 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2455 skb = nlmsg_new(len, GFP_ATOMIC);
2459 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2465 p = nlmsg_data(nlh);
2466 p->proto = c->data.proto;
2468 nlmsg_end(skb, nlh);
2470 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2473 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2477 l += nla_total_size(aead_len(x->aead));
2479 l += nla_total_size(sizeof(struct xfrm_algo) +
2480 (x->aalg->alg_key_len + 7) / 8);
2481 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2484 l += nla_total_size(xfrm_alg_len(x->ealg));
2486 l += nla_total_size(sizeof(*x->calg));
2488 l += nla_total_size(sizeof(*x->encap));
2490 l += nla_total_size(sizeof(x->tfcpad));
2492 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2494 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2495 x->security->ctx_len);
2497 l += nla_total_size(sizeof(*x->coaddr));
2499 /* Must count x->lastused as it may become non-zero behind our back. */
2500 l += nla_total_size(sizeof(u64));
2505 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2507 struct net *net = xs_net(x);
2508 struct xfrm_usersa_info *p;
2509 struct xfrm_usersa_id *id;
2510 struct nlmsghdr *nlh;
2511 struct sk_buff *skb;
2512 int len = xfrm_sa_len(x);
2515 headlen = sizeof(*p);
2516 if (c->event == XFRM_MSG_DELSA) {
2517 len += nla_total_size(headlen);
2518 headlen = sizeof(*id);
2519 len += nla_total_size(sizeof(struct xfrm_mark));
2521 len += NLMSG_ALIGN(headlen);
2523 skb = nlmsg_new(len, GFP_ATOMIC);
2527 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2532 p = nlmsg_data(nlh);
2533 if (c->event == XFRM_MSG_DELSA) {
2534 struct nlattr *attr;
2536 id = nlmsg_data(nlh);
2537 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2538 id->spi = x->id.spi;
2539 id->family = x->props.family;
2540 id->proto = x->id.proto;
2542 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2549 err = copy_to_user_state_extra(x, p, skb);
2553 nlmsg_end(skb, nlh);
2555 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2562 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2566 case XFRM_MSG_EXPIRE:
2567 return xfrm_exp_state_notify(x, c);
2568 case XFRM_MSG_NEWAE:
2569 return xfrm_aevent_state_notify(x, c);
2570 case XFRM_MSG_DELSA:
2571 case XFRM_MSG_UPDSA:
2572 case XFRM_MSG_NEWSA:
2573 return xfrm_notify_sa(x, c);
2574 case XFRM_MSG_FLUSHSA:
2575 return xfrm_notify_sa_flush(c);
2577 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2586 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2587 struct xfrm_policy *xp)
2589 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2590 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2591 + nla_total_size(sizeof(struct xfrm_mark))
2592 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2593 + userpolicy_type_attrsize();
2596 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2597 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2600 __u32 seq = xfrm_get_acqseq();
2601 struct xfrm_user_acquire *ua;
2602 struct nlmsghdr *nlh;
2605 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2609 ua = nlmsg_data(nlh);
2610 memcpy(&ua->id, &x->id, sizeof(ua->id));
2611 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2612 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2613 copy_to_user_policy(xp, &ua->policy, dir);
2614 ua->aalgos = xt->aalgos;
2615 ua->ealgos = xt->ealgos;
2616 ua->calgos = xt->calgos;
2617 ua->seq = x->km.seq = seq;
2619 err = copy_to_user_tmpl(xp, skb);
2621 err = copy_to_user_state_sec_ctx(x, skb);
2623 err = copy_to_user_policy_type(xp->type, skb);
2625 err = xfrm_mark_put(skb, &xp->mark);
2627 nlmsg_cancel(skb, nlh);
2631 return nlmsg_end(skb, nlh);
2634 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2635 struct xfrm_policy *xp, int dir)
2637 struct net *net = xs_net(x);
2638 struct sk_buff *skb;
2640 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2644 if (build_acquire(skb, x, xt, xp, dir) < 0)
2647 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2650 /* User gives us xfrm_user_policy_info followed by an array of 0
2651 * or more templates.
2653 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2654 u8 *data, int len, int *dir)
2656 struct net *net = sock_net(sk);
2657 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2658 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2659 struct xfrm_policy *xp;
2662 switch (sk->sk_family) {
2664 if (opt != IP_XFRM_POLICY) {
2669 #if IS_ENABLED(CONFIG_IPV6)
2671 if (opt != IPV6_XFRM_POLICY) {
2684 if (len < sizeof(*p) ||
2685 verify_newpolicy_info(p))
2688 nr = ((len - sizeof(*p)) / sizeof(*ut));
2689 if (validate_tmpl(nr, ut, p->sel.family))
2692 if (p->dir > XFRM_POLICY_OUT)
2695 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2701 copy_from_user_policy(xp, p);
2702 xp->type = XFRM_POLICY_TYPE_MAIN;
2703 copy_templates(xp, ut, nr);
2710 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2712 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2713 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2714 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2715 + nla_total_size(sizeof(struct xfrm_mark))
2716 + userpolicy_type_attrsize();
2719 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2720 int dir, const struct km_event *c)
2722 struct xfrm_user_polexpire *upe;
2723 int hard = c->data.hard;
2724 struct nlmsghdr *nlh;
2727 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2731 upe = nlmsg_data(nlh);
2732 copy_to_user_policy(xp, &upe->pol, dir);
2733 err = copy_to_user_tmpl(xp, skb);
2735 err = copy_to_user_sec_ctx(xp, skb);
2737 err = copy_to_user_policy_type(xp->type, skb);
2739 err = xfrm_mark_put(skb, &xp->mark);
2741 nlmsg_cancel(skb, nlh);
2746 return nlmsg_end(skb, nlh);
2749 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2751 struct net *net = xp_net(xp);
2752 struct sk_buff *skb;
2754 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2758 if (build_polexpire(skb, xp, dir, c) < 0)
2761 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2764 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2766 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2767 struct net *net = xp_net(xp);
2768 struct xfrm_userpolicy_info *p;
2769 struct xfrm_userpolicy_id *id;
2770 struct nlmsghdr *nlh;
2771 struct sk_buff *skb;
2774 headlen = sizeof(*p);
2775 if (c->event == XFRM_MSG_DELPOLICY) {
2776 len += nla_total_size(headlen);
2777 headlen = sizeof(*id);
2779 len += userpolicy_type_attrsize();
2780 len += nla_total_size(sizeof(struct xfrm_mark));
2781 len += NLMSG_ALIGN(headlen);
2783 skb = nlmsg_new(len, GFP_ATOMIC);
2787 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2792 p = nlmsg_data(nlh);
2793 if (c->event == XFRM_MSG_DELPOLICY) {
2794 struct nlattr *attr;
2796 id = nlmsg_data(nlh);
2797 memset(id, 0, sizeof(*id));
2800 id->index = xp->index;
2802 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2804 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2812 copy_to_user_policy(xp, p, dir);
2813 err = copy_to_user_tmpl(xp, skb);
2815 err = copy_to_user_policy_type(xp->type, skb);
2817 err = xfrm_mark_put(skb, &xp->mark);
2821 nlmsg_end(skb, nlh);
2823 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2830 static int xfrm_notify_policy_flush(const struct km_event *c)
2832 struct net *net = c->net;
2833 struct nlmsghdr *nlh;
2834 struct sk_buff *skb;
2837 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2841 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2845 err = copy_to_user_policy_type(c->data.type, skb);
2849 nlmsg_end(skb, nlh);
2851 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2858 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2862 case XFRM_MSG_NEWPOLICY:
2863 case XFRM_MSG_UPDPOLICY:
2864 case XFRM_MSG_DELPOLICY:
2865 return xfrm_notify_policy(xp, dir, c);
2866 case XFRM_MSG_FLUSHPOLICY:
2867 return xfrm_notify_policy_flush(c);
2868 case XFRM_MSG_POLEXPIRE:
2869 return xfrm_exp_policy_notify(xp, dir, c);
2871 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2879 static inline size_t xfrm_report_msgsize(void)
2881 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2884 static int build_report(struct sk_buff *skb, u8 proto,
2885 struct xfrm_selector *sel, xfrm_address_t *addr)
2887 struct xfrm_user_report *ur;
2888 struct nlmsghdr *nlh;
2890 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2894 ur = nlmsg_data(nlh);
2896 memcpy(&ur->sel, sel, sizeof(ur->sel));
2899 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2901 nlmsg_cancel(skb, nlh);
2905 return nlmsg_end(skb, nlh);
2908 static int xfrm_send_report(struct net *net, u8 proto,
2909 struct xfrm_selector *sel, xfrm_address_t *addr)
2911 struct sk_buff *skb;
2913 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2917 if (build_report(skb, proto, sel, addr) < 0)
2920 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2923 static inline size_t xfrm_mapping_msgsize(void)
2925 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2928 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2929 xfrm_address_t *new_saddr, __be16 new_sport)
2931 struct xfrm_user_mapping *um;
2932 struct nlmsghdr *nlh;
2934 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2938 um = nlmsg_data(nlh);
2940 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2941 um->id.spi = x->id.spi;
2942 um->id.family = x->props.family;
2943 um->id.proto = x->id.proto;
2944 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2945 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2946 um->new_sport = new_sport;
2947 um->old_sport = x->encap->encap_sport;
2948 um->reqid = x->props.reqid;
2950 return nlmsg_end(skb, nlh);
2953 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2956 struct net *net = xs_net(x);
2957 struct sk_buff *skb;
2959 if (x->id.proto != IPPROTO_ESP)
2965 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2969 if (build_mapping(skb, x, ipaddr, sport) < 0)
2972 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2975 static struct xfrm_mgr netlink_mgr = {
2977 .notify = xfrm_send_state_notify,
2978 .acquire = xfrm_send_acquire,
2979 .compile_policy = xfrm_compile_policy,
2980 .notify_policy = xfrm_send_policy_notify,
2981 .report = xfrm_send_report,
2982 .migrate = xfrm_send_migrate,
2983 .new_mapping = xfrm_send_mapping,
2986 static int __net_init xfrm_user_net_init(struct net *net)
2989 struct netlink_kernel_cfg cfg = {
2990 .groups = XFRMNLGRP_MAX,
2991 .input = xfrm_netlink_rcv,
2994 nlsk = netlink_kernel_create(net, NETLINK_XFRM, THIS_MODULE, &cfg);
2997 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2998 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3002 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3005 list_for_each_entry(net, net_exit_list, exit_list)
3006 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3008 list_for_each_entry(net, net_exit_list, exit_list)
3009 netlink_kernel_release(net->xfrm.nlsk_stash);
3012 static struct pernet_operations xfrm_user_net_ops = {
3013 .init = xfrm_user_net_init,
3014 .exit_batch = xfrm_user_net_exit,
3017 static int __init xfrm_user_init(void)
3021 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3023 rv = register_pernet_subsys(&xfrm_user_net_ops);
3026 rv = xfrm_register_km(&netlink_mgr);
3028 unregister_pernet_subsys(&xfrm_user_net_ops);
3032 static void __exit xfrm_user_exit(void)
3034 xfrm_unregister_km(&netlink_mgr);
3035 unregister_pernet_subsys(&xfrm_user_net_ops);
3038 module_init(xfrm_user_init);
3039 module_exit(xfrm_user_exit);
3040 MODULE_LICENSE("GPL");
3041 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);