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 (attrs[XFRMA_SA_EXTRA_FLAGS])
519 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
521 if ((err = attach_aead(&x->aead, &x->props.ealgo,
522 attrs[XFRMA_ALG_AEAD])))
524 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
525 attrs[XFRMA_ALG_AUTH_TRUNC])))
527 if (!x->props.aalgo) {
528 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
529 attrs[XFRMA_ALG_AUTH])))
532 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
533 xfrm_ealg_get_byname,
534 attrs[XFRMA_ALG_CRYPT])))
536 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
537 xfrm_calg_get_byname,
538 attrs[XFRMA_ALG_COMP])))
541 if (attrs[XFRMA_ENCAP]) {
542 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
543 sizeof(*x->encap), GFP_KERNEL);
544 if (x->encap == NULL)
548 if (attrs[XFRMA_TFCPAD])
549 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
551 if (attrs[XFRMA_COADDR]) {
552 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
553 sizeof(*x->coaddr), GFP_KERNEL);
554 if (x->coaddr == NULL)
558 xfrm_mark_get(attrs, &x->mark);
560 err = __xfrm_init_state(x, false);
564 if (attrs[XFRMA_SEC_CTX] &&
565 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
568 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
569 attrs[XFRMA_REPLAY_ESN_VAL])))
573 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
574 /* sysctl_xfrm_aevent_etime is in 100ms units */
575 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
577 if ((err = xfrm_init_replay(x)))
580 /* override default values from above */
581 xfrm_update_ae_params(x, attrs, 0);
586 x->km.state = XFRM_STATE_DEAD;
593 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
594 struct nlattr **attrs)
596 struct net *net = sock_net(skb->sk);
597 struct xfrm_usersa_info *p = nlmsg_data(nlh);
598 struct xfrm_state *x;
601 kuid_t loginuid = audit_get_loginuid(current);
602 u32 sessionid = audit_get_sessionid(current);
605 err = verify_newsa_info(p, attrs);
609 x = xfrm_state_construct(net, p, attrs, &err);
614 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
615 err = xfrm_state_add(x);
617 err = xfrm_state_update(x);
619 security_task_getsecid(current, &sid);
620 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
623 x->km.state = XFRM_STATE_DEAD;
628 c.seq = nlh->nlmsg_seq;
629 c.portid = nlh->nlmsg_pid;
630 c.event = nlh->nlmsg_type;
632 km_state_notify(x, &c);
638 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
639 struct xfrm_usersa_id *p,
640 struct nlattr **attrs,
643 struct xfrm_state *x = NULL;
646 u32 mark = xfrm_mark_get(attrs, &m);
648 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
650 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
652 xfrm_address_t *saddr = NULL;
654 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
661 x = xfrm_state_lookup_byaddr(net, mark,
663 p->proto, p->family);
672 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
673 struct nlattr **attrs)
675 struct net *net = sock_net(skb->sk);
676 struct xfrm_state *x;
679 struct xfrm_usersa_id *p = nlmsg_data(nlh);
680 kuid_t loginuid = audit_get_loginuid(current);
681 u32 sessionid = audit_get_sessionid(current);
684 x = xfrm_user_state_lookup(net, p, attrs, &err);
688 if ((err = security_xfrm_state_delete(x)) != 0)
691 if (xfrm_state_kern(x)) {
696 err = xfrm_state_delete(x);
701 c.seq = nlh->nlmsg_seq;
702 c.portid = nlh->nlmsg_pid;
703 c.event = nlh->nlmsg_type;
704 km_state_notify(x, &c);
707 security_task_getsecid(current, &sid);
708 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
713 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
715 memset(p, 0, sizeof(*p));
716 memcpy(&p->id, &x->id, sizeof(p->id));
717 memcpy(&p->sel, &x->sel, sizeof(p->sel));
718 memcpy(&p->lft, &x->lft, sizeof(p->lft));
719 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
720 memcpy(&p->stats, &x->stats, sizeof(p->stats));
721 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
722 p->mode = x->props.mode;
723 p->replay_window = x->props.replay_window;
724 p->reqid = x->props.reqid;
725 p->family = x->props.family;
726 p->flags = x->props.flags;
730 struct xfrm_dump_info {
731 struct sk_buff *in_skb;
732 struct sk_buff *out_skb;
737 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
739 struct xfrm_user_sec_ctx *uctx;
741 int ctx_size = sizeof(*uctx) + s->ctx_len;
743 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
747 uctx = nla_data(attr);
748 uctx->exttype = XFRMA_SEC_CTX;
749 uctx->len = ctx_size;
750 uctx->ctx_doi = s->ctx_doi;
751 uctx->ctx_alg = s->ctx_alg;
752 uctx->ctx_len = s->ctx_len;
753 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
758 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
760 struct xfrm_algo *algo;
763 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
764 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
768 algo = nla_data(nla);
769 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
770 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
771 algo->alg_key_len = auth->alg_key_len;
776 /* Don't change this without updating xfrm_sa_len! */
777 static int copy_to_user_state_extra(struct xfrm_state *x,
778 struct xfrm_usersa_info *p,
783 copy_to_user_state(x, p);
785 if (x->props.extra_flags) {
786 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
787 x->props.extra_flags);
793 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
798 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
803 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
808 ret = copy_to_user_auth(x->aalg, skb);
810 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
811 xfrm_alg_auth_len(x->aalg), x->aalg);
816 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
821 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
826 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
831 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
835 ret = xfrm_mark_put(skb, &x->mark);
839 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
840 xfrm_replay_state_esn_len(x->replay_esn),
846 ret = copy_sec_ctx(x->security, skb);
851 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
853 struct xfrm_dump_info *sp = ptr;
854 struct sk_buff *in_skb = sp->in_skb;
855 struct sk_buff *skb = sp->out_skb;
856 struct xfrm_usersa_info *p;
857 struct nlmsghdr *nlh;
860 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
861 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
867 err = copy_to_user_state_extra(x, p, skb);
869 nlmsg_cancel(skb, nlh);
876 static int xfrm_dump_sa_done(struct netlink_callback *cb)
878 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
879 xfrm_state_walk_done(walk);
883 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
885 struct net *net = sock_net(skb->sk);
886 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
887 struct xfrm_dump_info info;
889 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
890 sizeof(cb->args) - sizeof(cb->args[0]));
892 info.in_skb = cb->skb;
894 info.nlmsg_seq = cb->nlh->nlmsg_seq;
895 info.nlmsg_flags = NLM_F_MULTI;
899 xfrm_state_walk_init(walk, 0);
902 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
907 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
908 struct xfrm_state *x, u32 seq)
910 struct xfrm_dump_info info;
914 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
916 return ERR_PTR(-ENOMEM);
918 info.in_skb = in_skb;
920 info.nlmsg_seq = seq;
921 info.nlmsg_flags = 0;
923 err = dump_one_state(x, 0, &info);
932 static inline size_t xfrm_spdinfo_msgsize(void)
934 return NLMSG_ALIGN(4)
935 + nla_total_size(sizeof(struct xfrmu_spdinfo))
936 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
939 static int build_spdinfo(struct sk_buff *skb, struct net *net,
940 u32 portid, u32 seq, u32 flags)
942 struct xfrmk_spdinfo si;
943 struct xfrmu_spdinfo spc;
944 struct xfrmu_spdhinfo sph;
945 struct nlmsghdr *nlh;
949 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
950 if (nlh == NULL) /* shouldn't really happen ... */
955 xfrm_spd_getinfo(net, &si);
956 spc.incnt = si.incnt;
957 spc.outcnt = si.outcnt;
958 spc.fwdcnt = si.fwdcnt;
959 spc.inscnt = si.inscnt;
960 spc.outscnt = si.outscnt;
961 spc.fwdscnt = si.fwdscnt;
962 sph.spdhcnt = si.spdhcnt;
963 sph.spdhmcnt = si.spdhmcnt;
965 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
967 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
969 nlmsg_cancel(skb, nlh);
973 return nlmsg_end(skb, nlh);
976 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
977 struct nlattr **attrs)
979 struct net *net = sock_net(skb->sk);
980 struct sk_buff *r_skb;
981 u32 *flags = nlmsg_data(nlh);
982 u32 sportid = NETLINK_CB(skb).portid;
983 u32 seq = nlh->nlmsg_seq;
985 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
989 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
992 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
995 static inline size_t xfrm_sadinfo_msgsize(void)
997 return NLMSG_ALIGN(4)
998 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
999 + nla_total_size(4); /* XFRMA_SAD_CNT */
1002 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1003 u32 portid, u32 seq, u32 flags)
1005 struct xfrmk_sadinfo si;
1006 struct xfrmu_sadhinfo sh;
1007 struct nlmsghdr *nlh;
1011 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1012 if (nlh == NULL) /* shouldn't really happen ... */
1015 f = nlmsg_data(nlh);
1017 xfrm_sad_getinfo(net, &si);
1019 sh.sadhmcnt = si.sadhmcnt;
1020 sh.sadhcnt = si.sadhcnt;
1022 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1024 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1026 nlmsg_cancel(skb, nlh);
1030 return nlmsg_end(skb, nlh);
1033 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1034 struct nlattr **attrs)
1036 struct net *net = sock_net(skb->sk);
1037 struct sk_buff *r_skb;
1038 u32 *flags = nlmsg_data(nlh);
1039 u32 sportid = NETLINK_CB(skb).portid;
1040 u32 seq = nlh->nlmsg_seq;
1042 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1046 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1049 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1052 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1053 struct nlattr **attrs)
1055 struct net *net = sock_net(skb->sk);
1056 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1057 struct xfrm_state *x;
1058 struct sk_buff *resp_skb;
1061 x = xfrm_user_state_lookup(net, p, attrs, &err);
1065 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1066 if (IS_ERR(resp_skb)) {
1067 err = PTR_ERR(resp_skb);
1069 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1076 static int verify_userspi_info(struct xfrm_userspi_info *p)
1078 switch (p->info.id.proto) {
1084 /* IPCOMP spi is 16-bits. */
1085 if (p->max >= 0x10000)
1093 if (p->min > p->max)
1099 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1100 struct nlattr **attrs)
1102 struct net *net = sock_net(skb->sk);
1103 struct xfrm_state *x;
1104 struct xfrm_userspi_info *p;
1105 struct sk_buff *resp_skb;
1106 xfrm_address_t *daddr;
1112 p = nlmsg_data(nlh);
1113 err = verify_userspi_info(p);
1117 family = p->info.family;
1118 daddr = &p->info.id.daddr;
1122 mark = xfrm_mark_get(attrs, &m);
1124 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1125 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1132 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1133 p->info.id.proto, daddr,
1140 err = xfrm_alloc_spi(x, p->min, p->max);
1144 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1145 if (IS_ERR(resp_skb)) {
1146 err = PTR_ERR(resp_skb);
1150 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1158 static int verify_policy_dir(u8 dir)
1161 case XFRM_POLICY_IN:
1162 case XFRM_POLICY_OUT:
1163 case XFRM_POLICY_FWD:
1173 static int verify_policy_type(u8 type)
1176 case XFRM_POLICY_TYPE_MAIN:
1177 #ifdef CONFIG_XFRM_SUB_POLICY
1178 case XFRM_POLICY_TYPE_SUB:
1189 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1192 case XFRM_SHARE_ANY:
1193 case XFRM_SHARE_SESSION:
1194 case XFRM_SHARE_USER:
1195 case XFRM_SHARE_UNIQUE:
1202 switch (p->action) {
1203 case XFRM_POLICY_ALLOW:
1204 case XFRM_POLICY_BLOCK:
1211 switch (p->sel.family) {
1216 #if IS_ENABLED(CONFIG_IPV6)
1219 return -EAFNOSUPPORT;
1226 return verify_policy_dir(p->dir);
1229 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1231 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1232 struct xfrm_user_sec_ctx *uctx;
1237 uctx = nla_data(rt);
1238 return security_xfrm_policy_alloc(&pol->security, uctx);
1241 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1247 for (i = 0; i < nr; i++, ut++) {
1248 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1250 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1251 memcpy(&t->saddr, &ut->saddr,
1252 sizeof(xfrm_address_t));
1253 t->reqid = ut->reqid;
1255 t->share = ut->share;
1256 t->optional = ut->optional;
1257 t->aalgos = ut->aalgos;
1258 t->ealgos = ut->ealgos;
1259 t->calgos = ut->calgos;
1260 /* If all masks are ~0, then we allow all algorithms. */
1261 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1262 t->encap_family = ut->family;
1266 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1270 if (nr > XFRM_MAX_DEPTH)
1273 for (i = 0; i < nr; i++) {
1274 /* We never validated the ut->family value, so many
1275 * applications simply leave it at zero. The check was
1276 * never made and ut->family was ignored because all
1277 * templates could be assumed to have the same family as
1278 * the policy itself. Now that we will have ipv4-in-ipv6
1279 * and ipv6-in-ipv4 tunnels, this is no longer true.
1282 ut[i].family = family;
1284 switch (ut[i].family) {
1287 #if IS_ENABLED(CONFIG_IPV6)
1299 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1301 struct nlattr *rt = attrs[XFRMA_TMPL];
1306 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1307 int nr = nla_len(rt) / sizeof(*utmpl);
1310 err = validate_tmpl(nr, utmpl, pol->family);
1314 copy_templates(pol, utmpl, nr);
1319 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1321 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1322 struct xfrm_userpolicy_type *upt;
1323 u8 type = XFRM_POLICY_TYPE_MAIN;
1331 err = verify_policy_type(type);
1339 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1341 xp->priority = p->priority;
1342 xp->index = p->index;
1343 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1344 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1345 xp->action = p->action;
1346 xp->flags = p->flags;
1347 xp->family = p->sel.family;
1348 /* XXX xp->share = p->share; */
1351 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1353 memset(p, 0, sizeof(*p));
1354 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1355 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1356 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1357 p->priority = xp->priority;
1358 p->index = xp->index;
1359 p->sel.family = xp->family;
1361 p->action = xp->action;
1362 p->flags = xp->flags;
1363 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1366 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1368 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1376 copy_from_user_policy(xp, p);
1378 err = copy_from_user_policy_type(&xp->type, attrs);
1382 if (!(err = copy_from_user_tmpl(xp, attrs)))
1383 err = copy_from_user_sec_ctx(xp, attrs);
1387 xfrm_mark_get(attrs, &xp->mark);
1393 xfrm_policy_destroy(xp);
1397 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1398 struct nlattr **attrs)
1400 struct net *net = sock_net(skb->sk);
1401 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1402 struct xfrm_policy *xp;
1406 kuid_t loginuid = audit_get_loginuid(current);
1407 u32 sessionid = audit_get_sessionid(current);
1410 err = verify_newpolicy_info(p);
1413 err = verify_sec_ctx_len(attrs);
1417 xp = xfrm_policy_construct(net, p, attrs, &err);
1421 /* shouldn't excl be based on nlh flags??
1422 * Aha! this is anti-netlink really i.e more pfkey derived
1423 * in netlink excl is a flag and you wouldnt need
1424 * a type XFRM_MSG_UPDPOLICY - JHS */
1425 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1426 err = xfrm_policy_insert(p->dir, xp, excl);
1427 security_task_getsecid(current, &sid);
1428 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1431 security_xfrm_policy_free(xp->security);
1436 c.event = nlh->nlmsg_type;
1437 c.seq = nlh->nlmsg_seq;
1438 c.portid = nlh->nlmsg_pid;
1439 km_policy_notify(xp, p->dir, &c);
1446 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1448 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1451 if (xp->xfrm_nr == 0)
1454 for (i = 0; i < xp->xfrm_nr; i++) {
1455 struct xfrm_user_tmpl *up = &vec[i];
1456 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1458 memset(up, 0, sizeof(*up));
1459 memcpy(&up->id, &kp->id, sizeof(up->id));
1460 up->family = kp->encap_family;
1461 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1462 up->reqid = kp->reqid;
1463 up->mode = kp->mode;
1464 up->share = kp->share;
1465 up->optional = kp->optional;
1466 up->aalgos = kp->aalgos;
1467 up->ealgos = kp->ealgos;
1468 up->calgos = kp->calgos;
1471 return nla_put(skb, XFRMA_TMPL,
1472 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1475 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1478 return copy_sec_ctx(x->security, skb);
1483 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1486 return copy_sec_ctx(xp->security, skb);
1489 static inline size_t userpolicy_type_attrsize(void)
1491 #ifdef CONFIG_XFRM_SUB_POLICY
1492 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1498 #ifdef CONFIG_XFRM_SUB_POLICY
1499 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1501 struct xfrm_userpolicy_type upt = {
1505 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1509 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1515 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1517 struct xfrm_dump_info *sp = ptr;
1518 struct xfrm_userpolicy_info *p;
1519 struct sk_buff *in_skb = sp->in_skb;
1520 struct sk_buff *skb = sp->out_skb;
1521 struct nlmsghdr *nlh;
1524 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1525 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1529 p = nlmsg_data(nlh);
1530 copy_to_user_policy(xp, p, dir);
1531 err = copy_to_user_tmpl(xp, skb);
1533 err = copy_to_user_sec_ctx(xp, skb);
1535 err = copy_to_user_policy_type(xp->type, skb);
1537 err = xfrm_mark_put(skb, &xp->mark);
1539 nlmsg_cancel(skb, nlh);
1542 nlmsg_end(skb, nlh);
1546 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1548 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1550 xfrm_policy_walk_done(walk);
1554 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1556 struct net *net = sock_net(skb->sk);
1557 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1558 struct xfrm_dump_info info;
1560 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1561 sizeof(cb->args) - sizeof(cb->args[0]));
1563 info.in_skb = cb->skb;
1565 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1566 info.nlmsg_flags = NLM_F_MULTI;
1570 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1573 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1578 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1579 struct xfrm_policy *xp,
1582 struct xfrm_dump_info info;
1583 struct sk_buff *skb;
1586 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1588 return ERR_PTR(-ENOMEM);
1590 info.in_skb = in_skb;
1592 info.nlmsg_seq = seq;
1593 info.nlmsg_flags = 0;
1595 err = dump_one_policy(xp, dir, 0, &info);
1598 return ERR_PTR(err);
1604 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1605 struct nlattr **attrs)
1607 struct net *net = sock_net(skb->sk);
1608 struct xfrm_policy *xp;
1609 struct xfrm_userpolicy_id *p;
1610 u8 type = XFRM_POLICY_TYPE_MAIN;
1615 u32 mark = xfrm_mark_get(attrs, &m);
1617 p = nlmsg_data(nlh);
1618 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1620 err = copy_from_user_policy_type(&type, attrs);
1624 err = verify_policy_dir(p->dir);
1629 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1631 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1632 struct xfrm_sec_ctx *ctx;
1634 err = verify_sec_ctx_len(attrs);
1640 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1642 err = security_xfrm_policy_alloc(&ctx, uctx);
1646 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1648 security_xfrm_policy_free(ctx);
1654 struct sk_buff *resp_skb;
1656 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1657 if (IS_ERR(resp_skb)) {
1658 err = PTR_ERR(resp_skb);
1660 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1661 NETLINK_CB(skb).portid);
1664 kuid_t loginuid = audit_get_loginuid(current);
1665 u32 sessionid = audit_get_sessionid(current);
1668 security_task_getsecid(current, &sid);
1669 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1675 c.data.byid = p->index;
1676 c.event = nlh->nlmsg_type;
1677 c.seq = nlh->nlmsg_seq;
1678 c.portid = nlh->nlmsg_pid;
1679 km_policy_notify(xp, p->dir, &c);
1684 if (delete && err == 0)
1685 xfrm_garbage_collect(net);
1689 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1690 struct nlattr **attrs)
1692 struct net *net = sock_net(skb->sk);
1694 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1695 struct xfrm_audit audit_info;
1698 audit_info.loginuid = audit_get_loginuid(current);
1699 audit_info.sessionid = audit_get_sessionid(current);
1700 security_task_getsecid(current, &audit_info.secid);
1701 err = xfrm_state_flush(net, p->proto, &audit_info);
1703 if (err == -ESRCH) /* empty table */
1707 c.data.proto = p->proto;
1708 c.event = nlh->nlmsg_type;
1709 c.seq = nlh->nlmsg_seq;
1710 c.portid = nlh->nlmsg_pid;
1712 km_state_notify(NULL, &c);
1717 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1719 size_t replay_size = x->replay_esn ?
1720 xfrm_replay_state_esn_len(x->replay_esn) :
1721 sizeof(struct xfrm_replay_state);
1723 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1724 + nla_total_size(replay_size)
1725 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1726 + nla_total_size(sizeof(struct xfrm_mark))
1727 + nla_total_size(4) /* XFRM_AE_RTHR */
1728 + nla_total_size(4); /* XFRM_AE_ETHR */
1731 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1733 struct xfrm_aevent_id *id;
1734 struct nlmsghdr *nlh;
1737 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1741 id = nlmsg_data(nlh);
1742 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1743 id->sa_id.spi = x->id.spi;
1744 id->sa_id.family = x->props.family;
1745 id->sa_id.proto = x->id.proto;
1746 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1747 id->reqid = x->props.reqid;
1748 id->flags = c->data.aevent;
1750 if (x->replay_esn) {
1751 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1752 xfrm_replay_state_esn_len(x->replay_esn),
1755 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1760 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1764 if (id->flags & XFRM_AE_RTHR) {
1765 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1769 if (id->flags & XFRM_AE_ETHR) {
1770 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1771 x->replay_maxage * 10 / HZ);
1775 err = xfrm_mark_put(skb, &x->mark);
1779 return nlmsg_end(skb, nlh);
1782 nlmsg_cancel(skb, nlh);
1786 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1787 struct nlattr **attrs)
1789 struct net *net = sock_net(skb->sk);
1790 struct xfrm_state *x;
1791 struct sk_buff *r_skb;
1796 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1797 struct xfrm_usersa_id *id = &p->sa_id;
1799 mark = xfrm_mark_get(attrs, &m);
1801 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1805 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1806 if (r_skb == NULL) {
1812 * XXX: is this lock really needed - none of the other
1813 * gets lock (the concern is things getting updated
1814 * while we are still reading) - jhs
1816 spin_lock_bh(&x->lock);
1817 c.data.aevent = p->flags;
1818 c.seq = nlh->nlmsg_seq;
1819 c.portid = nlh->nlmsg_pid;
1821 if (build_aevent(r_skb, x, &c) < 0)
1823 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1824 spin_unlock_bh(&x->lock);
1829 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1830 struct nlattr **attrs)
1832 struct net *net = sock_net(skb->sk);
1833 struct xfrm_state *x;
1838 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1839 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1840 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1841 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1843 if (!lt && !rp && !re)
1846 /* pedantic mode - thou shalt sayeth replaceth */
1847 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1850 mark = xfrm_mark_get(attrs, &m);
1852 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1856 if (x->km.state != XFRM_STATE_VALID)
1859 err = xfrm_replay_verify_len(x->replay_esn, rp);
1863 spin_lock_bh(&x->lock);
1864 xfrm_update_ae_params(x, attrs, 1);
1865 spin_unlock_bh(&x->lock);
1867 c.event = nlh->nlmsg_type;
1868 c.seq = nlh->nlmsg_seq;
1869 c.portid = nlh->nlmsg_pid;
1870 c.data.aevent = XFRM_AE_CU;
1871 km_state_notify(x, &c);
1878 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1879 struct nlattr **attrs)
1881 struct net *net = sock_net(skb->sk);
1883 u8 type = XFRM_POLICY_TYPE_MAIN;
1885 struct xfrm_audit audit_info;
1887 err = copy_from_user_policy_type(&type, attrs);
1891 audit_info.loginuid = audit_get_loginuid(current);
1892 audit_info.sessionid = audit_get_sessionid(current);
1893 security_task_getsecid(current, &audit_info.secid);
1894 err = xfrm_policy_flush(net, type, &audit_info);
1896 if (err == -ESRCH) /* empty table */
1902 c.event = nlh->nlmsg_type;
1903 c.seq = nlh->nlmsg_seq;
1904 c.portid = nlh->nlmsg_pid;
1906 km_policy_notify(NULL, 0, &c);
1910 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1911 struct nlattr **attrs)
1913 struct net *net = sock_net(skb->sk);
1914 struct xfrm_policy *xp;
1915 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1916 struct xfrm_userpolicy_info *p = &up->pol;
1917 u8 type = XFRM_POLICY_TYPE_MAIN;
1920 u32 mark = xfrm_mark_get(attrs, &m);
1922 err = copy_from_user_policy_type(&type, attrs);
1926 err = verify_policy_dir(p->dir);
1931 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1933 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1934 struct xfrm_sec_ctx *ctx;
1936 err = verify_sec_ctx_len(attrs);
1942 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1944 err = security_xfrm_policy_alloc(&ctx, uctx);
1948 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1949 &p->sel, ctx, 0, &err);
1950 security_xfrm_policy_free(ctx);
1955 if (unlikely(xp->walk.dead))
1960 kuid_t loginuid = audit_get_loginuid(current);
1961 u32 sessionid = audit_get_sessionid(current);
1964 security_task_getsecid(current, &sid);
1965 xfrm_policy_delete(xp, p->dir);
1966 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1969 // reset the timers here?
1970 WARN(1, "Dont know what to do with soft policy expire\n");
1972 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
1979 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1980 struct nlattr **attrs)
1982 struct net *net = sock_net(skb->sk);
1983 struct xfrm_state *x;
1985 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1986 struct xfrm_usersa_info *p = &ue->state;
1988 u32 mark = xfrm_mark_get(attrs, &m);
1990 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1996 spin_lock_bh(&x->lock);
1998 if (x->km.state != XFRM_STATE_VALID)
2000 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2003 kuid_t loginuid = audit_get_loginuid(current);
2004 u32 sessionid = audit_get_sessionid(current);
2007 security_task_getsecid(current, &sid);
2008 __xfrm_state_delete(x);
2009 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
2013 spin_unlock_bh(&x->lock);
2018 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2019 struct nlattr **attrs)
2021 struct net *net = sock_net(skb->sk);
2022 struct xfrm_policy *xp;
2023 struct xfrm_user_tmpl *ut;
2025 struct nlattr *rt = attrs[XFRMA_TMPL];
2026 struct xfrm_mark mark;
2028 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2029 struct xfrm_state *x = xfrm_state_alloc(net);
2035 xfrm_mark_get(attrs, &mark);
2037 err = verify_newpolicy_info(&ua->policy);
2042 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2046 memcpy(&x->id, &ua->id, sizeof(ua->id));
2047 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2048 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2049 xp->mark.m = x->mark.m = mark.m;
2050 xp->mark.v = x->mark.v = mark.v;
2052 /* extract the templates and for each call km_key */
2053 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2054 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2055 memcpy(&x->id, &t->id, sizeof(x->id));
2056 x->props.mode = t->mode;
2057 x->props.reqid = t->reqid;
2058 x->props.family = ut->family;
2059 t->aalgos = ua->aalgos;
2060 t->ealgos = ua->ealgos;
2061 t->calgos = ua->calgos;
2062 err = km_query(x, t, xp);
2072 WARN(1, "BAD policy passed\n");
2079 #ifdef CONFIG_XFRM_MIGRATE
2080 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2081 struct xfrm_kmaddress *k,
2082 struct nlattr **attrs, int *num)
2084 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2085 struct xfrm_user_migrate *um;
2089 struct xfrm_user_kmaddress *uk;
2091 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2092 memcpy(&k->local, &uk->local, sizeof(k->local));
2093 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2094 k->family = uk->family;
2095 k->reserved = uk->reserved;
2099 num_migrate = nla_len(rt) / sizeof(*um);
2101 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2104 for (i = 0; i < num_migrate; i++, um++, ma++) {
2105 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2106 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2107 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2108 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2110 ma->proto = um->proto;
2111 ma->mode = um->mode;
2112 ma->reqid = um->reqid;
2114 ma->old_family = um->old_family;
2115 ma->new_family = um->new_family;
2122 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2123 struct nlattr **attrs)
2125 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2126 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2127 struct xfrm_kmaddress km, *kmp;
2132 if (attrs[XFRMA_MIGRATE] == NULL)
2135 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2137 err = copy_from_user_policy_type(&type, attrs);
2141 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2148 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2153 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2154 struct nlattr **attrs)
2156 return -ENOPROTOOPT;
2160 #ifdef CONFIG_XFRM_MIGRATE
2161 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2163 struct xfrm_user_migrate um;
2165 memset(&um, 0, sizeof(um));
2166 um.proto = m->proto;
2168 um.reqid = m->reqid;
2169 um.old_family = m->old_family;
2170 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2171 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2172 um.new_family = m->new_family;
2173 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2174 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2176 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2179 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2181 struct xfrm_user_kmaddress uk;
2183 memset(&uk, 0, sizeof(uk));
2184 uk.family = k->family;
2185 uk.reserved = k->reserved;
2186 memcpy(&uk.local, &k->local, sizeof(uk.local));
2187 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2189 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2192 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2194 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2195 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2196 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2197 + userpolicy_type_attrsize();
2200 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2201 int num_migrate, const struct xfrm_kmaddress *k,
2202 const struct xfrm_selector *sel, u8 dir, u8 type)
2204 const struct xfrm_migrate *mp;
2205 struct xfrm_userpolicy_id *pol_id;
2206 struct nlmsghdr *nlh;
2209 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2213 pol_id = nlmsg_data(nlh);
2214 /* copy data from selector, dir, and type to the pol_id */
2215 memset(pol_id, 0, sizeof(*pol_id));
2216 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2220 err = copy_to_user_kmaddress(k, skb);
2224 err = copy_to_user_policy_type(type, skb);
2227 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2228 err = copy_to_user_migrate(mp, skb);
2233 return nlmsg_end(skb, nlh);
2236 nlmsg_cancel(skb, nlh);
2240 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2241 const struct xfrm_migrate *m, int num_migrate,
2242 const struct xfrm_kmaddress *k)
2244 struct net *net = &init_net;
2245 struct sk_buff *skb;
2247 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2252 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2255 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2258 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2259 const struct xfrm_migrate *m, int num_migrate,
2260 const struct xfrm_kmaddress *k)
2262 return -ENOPROTOOPT;
2266 #define XMSGSIZE(type) sizeof(struct type)
2268 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2269 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2270 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2271 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2272 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2273 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2274 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2275 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2276 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2277 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2278 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2279 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2280 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2281 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2282 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2283 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2284 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2285 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2286 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2287 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2288 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2293 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2294 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2295 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2296 [XFRMA_LASTUSED] = { .type = NLA_U64},
2297 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2298 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2299 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2300 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2301 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2302 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2303 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2304 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2305 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2306 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2307 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2308 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2309 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2310 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2311 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2312 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2313 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2314 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2315 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2316 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2317 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2320 static const struct xfrm_link {
2321 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2322 int (*dump)(struct sk_buff *, struct netlink_callback *);
2323 int (*done)(struct netlink_callback *);
2324 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2325 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2326 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2327 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2328 .dump = xfrm_dump_sa,
2329 .done = xfrm_dump_sa_done },
2330 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2331 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2332 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2333 .dump = xfrm_dump_policy,
2334 .done = xfrm_dump_policy_done },
2335 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2336 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2337 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2338 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2339 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2340 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2341 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2342 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2343 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2344 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2345 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2346 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2347 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2350 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2352 struct net *net = sock_net(skb->sk);
2353 struct nlattr *attrs[XFRMA_MAX+1];
2354 const struct xfrm_link *link;
2357 type = nlh->nlmsg_type;
2358 if (type > XFRM_MSG_MAX)
2361 type -= XFRM_MSG_BASE;
2362 link = &xfrm_dispatch[type];
2364 /* All operations require privileges, even GET */
2365 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2368 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2369 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2370 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2371 if (link->dump == NULL)
2375 struct netlink_dump_control c = {
2379 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2383 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2388 if (link->doit == NULL)
2391 return link->doit(skb, nlh, attrs);
2394 static void xfrm_netlink_rcv(struct sk_buff *skb)
2396 mutex_lock(&xfrm_cfg_mutex);
2397 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2398 mutex_unlock(&xfrm_cfg_mutex);
2401 static inline size_t xfrm_expire_msgsize(void)
2403 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2404 + nla_total_size(sizeof(struct xfrm_mark));
2407 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2409 struct xfrm_user_expire *ue;
2410 struct nlmsghdr *nlh;
2413 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2417 ue = nlmsg_data(nlh);
2418 copy_to_user_state(x, &ue->state);
2419 ue->hard = (c->data.hard != 0) ? 1 : 0;
2421 err = xfrm_mark_put(skb, &x->mark);
2425 return nlmsg_end(skb, nlh);
2428 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2430 struct net *net = xs_net(x);
2431 struct sk_buff *skb;
2433 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2437 if (build_expire(skb, x, c) < 0) {
2442 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2445 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2447 struct net *net = xs_net(x);
2448 struct sk_buff *skb;
2450 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2454 if (build_aevent(skb, x, c) < 0)
2457 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2460 static int xfrm_notify_sa_flush(const struct km_event *c)
2462 struct net *net = c->net;
2463 struct xfrm_usersa_flush *p;
2464 struct nlmsghdr *nlh;
2465 struct sk_buff *skb;
2466 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2468 skb = nlmsg_new(len, GFP_ATOMIC);
2472 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2478 p = nlmsg_data(nlh);
2479 p->proto = c->data.proto;
2481 nlmsg_end(skb, nlh);
2483 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2486 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2490 l += nla_total_size(aead_len(x->aead));
2492 l += nla_total_size(sizeof(struct xfrm_algo) +
2493 (x->aalg->alg_key_len + 7) / 8);
2494 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2497 l += nla_total_size(xfrm_alg_len(x->ealg));
2499 l += nla_total_size(sizeof(*x->calg));
2501 l += nla_total_size(sizeof(*x->encap));
2503 l += nla_total_size(sizeof(x->tfcpad));
2505 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2507 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2508 x->security->ctx_len);
2510 l += nla_total_size(sizeof(*x->coaddr));
2511 if (x->props.extra_flags)
2512 l += nla_total_size(sizeof(x->props.extra_flags));
2514 /* Must count x->lastused as it may become non-zero behind our back. */
2515 l += nla_total_size(sizeof(u64));
2520 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2522 struct net *net = xs_net(x);
2523 struct xfrm_usersa_info *p;
2524 struct xfrm_usersa_id *id;
2525 struct nlmsghdr *nlh;
2526 struct sk_buff *skb;
2527 int len = xfrm_sa_len(x);
2530 headlen = sizeof(*p);
2531 if (c->event == XFRM_MSG_DELSA) {
2532 len += nla_total_size(headlen);
2533 headlen = sizeof(*id);
2534 len += nla_total_size(sizeof(struct xfrm_mark));
2536 len += NLMSG_ALIGN(headlen);
2538 skb = nlmsg_new(len, GFP_ATOMIC);
2542 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2547 p = nlmsg_data(nlh);
2548 if (c->event == XFRM_MSG_DELSA) {
2549 struct nlattr *attr;
2551 id = nlmsg_data(nlh);
2552 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2553 id->spi = x->id.spi;
2554 id->family = x->props.family;
2555 id->proto = x->id.proto;
2557 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2564 err = copy_to_user_state_extra(x, p, skb);
2568 nlmsg_end(skb, nlh);
2570 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2577 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2581 case XFRM_MSG_EXPIRE:
2582 return xfrm_exp_state_notify(x, c);
2583 case XFRM_MSG_NEWAE:
2584 return xfrm_aevent_state_notify(x, c);
2585 case XFRM_MSG_DELSA:
2586 case XFRM_MSG_UPDSA:
2587 case XFRM_MSG_NEWSA:
2588 return xfrm_notify_sa(x, c);
2589 case XFRM_MSG_FLUSHSA:
2590 return xfrm_notify_sa_flush(c);
2592 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2601 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2602 struct xfrm_policy *xp)
2604 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2605 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2606 + nla_total_size(sizeof(struct xfrm_mark))
2607 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2608 + userpolicy_type_attrsize();
2611 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2612 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2614 __u32 seq = xfrm_get_acqseq();
2615 struct xfrm_user_acquire *ua;
2616 struct nlmsghdr *nlh;
2619 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2623 ua = nlmsg_data(nlh);
2624 memcpy(&ua->id, &x->id, sizeof(ua->id));
2625 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2626 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2627 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2628 ua->aalgos = xt->aalgos;
2629 ua->ealgos = xt->ealgos;
2630 ua->calgos = xt->calgos;
2631 ua->seq = x->km.seq = seq;
2633 err = copy_to_user_tmpl(xp, skb);
2635 err = copy_to_user_state_sec_ctx(x, skb);
2637 err = copy_to_user_policy_type(xp->type, skb);
2639 err = xfrm_mark_put(skb, &xp->mark);
2641 nlmsg_cancel(skb, nlh);
2645 return nlmsg_end(skb, nlh);
2648 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2649 struct xfrm_policy *xp)
2651 struct net *net = xs_net(x);
2652 struct sk_buff *skb;
2654 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2658 if (build_acquire(skb, x, xt, xp) < 0)
2661 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2664 /* User gives us xfrm_user_policy_info followed by an array of 0
2665 * or more templates.
2667 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2668 u8 *data, int len, int *dir)
2670 struct net *net = sock_net(sk);
2671 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2672 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2673 struct xfrm_policy *xp;
2676 switch (sk->sk_family) {
2678 if (opt != IP_XFRM_POLICY) {
2683 #if IS_ENABLED(CONFIG_IPV6)
2685 if (opt != IPV6_XFRM_POLICY) {
2698 if (len < sizeof(*p) ||
2699 verify_newpolicy_info(p))
2702 nr = ((len - sizeof(*p)) / sizeof(*ut));
2703 if (validate_tmpl(nr, ut, p->sel.family))
2706 if (p->dir > XFRM_POLICY_OUT)
2709 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2715 copy_from_user_policy(xp, p);
2716 xp->type = XFRM_POLICY_TYPE_MAIN;
2717 copy_templates(xp, ut, nr);
2724 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2726 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2727 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2728 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2729 + nla_total_size(sizeof(struct xfrm_mark))
2730 + userpolicy_type_attrsize();
2733 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2734 int dir, const struct km_event *c)
2736 struct xfrm_user_polexpire *upe;
2737 int hard = c->data.hard;
2738 struct nlmsghdr *nlh;
2741 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2745 upe = nlmsg_data(nlh);
2746 copy_to_user_policy(xp, &upe->pol, dir);
2747 err = copy_to_user_tmpl(xp, skb);
2749 err = copy_to_user_sec_ctx(xp, skb);
2751 err = copy_to_user_policy_type(xp->type, skb);
2753 err = xfrm_mark_put(skb, &xp->mark);
2755 nlmsg_cancel(skb, nlh);
2760 return nlmsg_end(skb, nlh);
2763 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2765 struct net *net = xp_net(xp);
2766 struct sk_buff *skb;
2768 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2772 if (build_polexpire(skb, xp, dir, c) < 0)
2775 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2778 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2780 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2781 struct net *net = xp_net(xp);
2782 struct xfrm_userpolicy_info *p;
2783 struct xfrm_userpolicy_id *id;
2784 struct nlmsghdr *nlh;
2785 struct sk_buff *skb;
2788 headlen = sizeof(*p);
2789 if (c->event == XFRM_MSG_DELPOLICY) {
2790 len += nla_total_size(headlen);
2791 headlen = sizeof(*id);
2793 len += userpolicy_type_attrsize();
2794 len += nla_total_size(sizeof(struct xfrm_mark));
2795 len += NLMSG_ALIGN(headlen);
2797 skb = nlmsg_new(len, GFP_ATOMIC);
2801 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2806 p = nlmsg_data(nlh);
2807 if (c->event == XFRM_MSG_DELPOLICY) {
2808 struct nlattr *attr;
2810 id = nlmsg_data(nlh);
2811 memset(id, 0, sizeof(*id));
2814 id->index = xp->index;
2816 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2818 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2826 copy_to_user_policy(xp, p, dir);
2827 err = copy_to_user_tmpl(xp, skb);
2829 err = copy_to_user_policy_type(xp->type, skb);
2831 err = xfrm_mark_put(skb, &xp->mark);
2835 nlmsg_end(skb, nlh);
2837 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2844 static int xfrm_notify_policy_flush(const struct km_event *c)
2846 struct net *net = c->net;
2847 struct nlmsghdr *nlh;
2848 struct sk_buff *skb;
2851 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2855 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2859 err = copy_to_user_policy_type(c->data.type, skb);
2863 nlmsg_end(skb, nlh);
2865 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2872 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2876 case XFRM_MSG_NEWPOLICY:
2877 case XFRM_MSG_UPDPOLICY:
2878 case XFRM_MSG_DELPOLICY:
2879 return xfrm_notify_policy(xp, dir, c);
2880 case XFRM_MSG_FLUSHPOLICY:
2881 return xfrm_notify_policy_flush(c);
2882 case XFRM_MSG_POLEXPIRE:
2883 return xfrm_exp_policy_notify(xp, dir, c);
2885 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2893 static inline size_t xfrm_report_msgsize(void)
2895 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2898 static int build_report(struct sk_buff *skb, u8 proto,
2899 struct xfrm_selector *sel, xfrm_address_t *addr)
2901 struct xfrm_user_report *ur;
2902 struct nlmsghdr *nlh;
2904 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2908 ur = nlmsg_data(nlh);
2910 memcpy(&ur->sel, sel, sizeof(ur->sel));
2913 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2915 nlmsg_cancel(skb, nlh);
2919 return nlmsg_end(skb, nlh);
2922 static int xfrm_send_report(struct net *net, u8 proto,
2923 struct xfrm_selector *sel, xfrm_address_t *addr)
2925 struct sk_buff *skb;
2927 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2931 if (build_report(skb, proto, sel, addr) < 0)
2934 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2937 static inline size_t xfrm_mapping_msgsize(void)
2939 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2942 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2943 xfrm_address_t *new_saddr, __be16 new_sport)
2945 struct xfrm_user_mapping *um;
2946 struct nlmsghdr *nlh;
2948 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2952 um = nlmsg_data(nlh);
2954 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2955 um->id.spi = x->id.spi;
2956 um->id.family = x->props.family;
2957 um->id.proto = x->id.proto;
2958 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2959 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2960 um->new_sport = new_sport;
2961 um->old_sport = x->encap->encap_sport;
2962 um->reqid = x->props.reqid;
2964 return nlmsg_end(skb, nlh);
2967 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2970 struct net *net = xs_net(x);
2971 struct sk_buff *skb;
2973 if (x->id.proto != IPPROTO_ESP)
2979 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2983 if (build_mapping(skb, x, ipaddr, sport) < 0)
2986 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2989 static struct xfrm_mgr netlink_mgr = {
2991 .notify = xfrm_send_state_notify,
2992 .acquire = xfrm_send_acquire,
2993 .compile_policy = xfrm_compile_policy,
2994 .notify_policy = xfrm_send_policy_notify,
2995 .report = xfrm_send_report,
2996 .migrate = xfrm_send_migrate,
2997 .new_mapping = xfrm_send_mapping,
3000 static int __net_init xfrm_user_net_init(struct net *net)
3003 struct netlink_kernel_cfg cfg = {
3004 .groups = XFRMNLGRP_MAX,
3005 .input = xfrm_netlink_rcv,
3008 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3011 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3012 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3016 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3019 list_for_each_entry(net, net_exit_list, exit_list)
3020 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3022 list_for_each_entry(net, net_exit_list, exit_list)
3023 netlink_kernel_release(net->xfrm.nlsk_stash);
3026 static struct pernet_operations xfrm_user_net_ops = {
3027 .init = xfrm_user_net_init,
3028 .exit_batch = xfrm_user_net_exit,
3031 static int __init xfrm_user_init(void)
3035 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3037 rv = register_pernet_subsys(&xfrm_user_net_ops);
3040 rv = xfrm_register_km(&netlink_mgr);
3042 unregister_pernet_subsys(&xfrm_user_net_ops);
3046 static void __exit xfrm_user_exit(void)
3048 xfrm_unregister_km(&netlink_mgr);
3049 unregister_pernet_subsys(&xfrm_user_net_ops);
3052 module_init(xfrm_user_init);
3053 module_exit(xfrm_user_exit);
3054 MODULE_LICENSE("GPL");
3055 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);