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 = min_t(unsigned int, p->replay_window,
450 sizeof(x->replay.bitmap) * 8);
451 x->props.reqid = p->reqid;
452 x->props.family = p->family;
453 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
454 x->props.flags = p->flags;
456 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
457 x->sel.family = p->family;
461 * someday when pfkey also has support, we could have the code
462 * somehow made shareable and move it to xfrm_state.c - JHS
465 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
468 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
469 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
470 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
471 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
472 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
475 struct xfrm_replay_state_esn *replay_esn;
476 replay_esn = nla_data(re);
477 memcpy(x->replay_esn, replay_esn,
478 xfrm_replay_state_esn_len(replay_esn));
479 memcpy(x->preplay_esn, replay_esn,
480 xfrm_replay_state_esn_len(replay_esn));
484 struct xfrm_replay_state *replay;
485 replay = nla_data(rp);
486 memcpy(&x->replay, replay, sizeof(*replay));
487 memcpy(&x->preplay, replay, sizeof(*replay));
491 struct xfrm_lifetime_cur *ltime;
492 ltime = nla_data(lt);
493 x->curlft.bytes = ltime->bytes;
494 x->curlft.packets = ltime->packets;
495 x->curlft.add_time = ltime->add_time;
496 x->curlft.use_time = ltime->use_time;
500 x->replay_maxage = nla_get_u32(et);
503 x->replay_maxdiff = nla_get_u32(rt);
506 static struct xfrm_state *xfrm_state_construct(struct net *net,
507 struct xfrm_usersa_info *p,
508 struct nlattr **attrs,
511 struct xfrm_state *x = xfrm_state_alloc(net);
517 copy_from_user_state(x, p);
519 if (attrs[XFRMA_SA_EXTRA_FLAGS])
520 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
522 if ((err = attach_aead(&x->aead, &x->props.ealgo,
523 attrs[XFRMA_ALG_AEAD])))
525 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
526 attrs[XFRMA_ALG_AUTH_TRUNC])))
528 if (!x->props.aalgo) {
529 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
530 attrs[XFRMA_ALG_AUTH])))
533 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
534 xfrm_ealg_get_byname,
535 attrs[XFRMA_ALG_CRYPT])))
537 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
538 xfrm_calg_get_byname,
539 attrs[XFRMA_ALG_COMP])))
542 if (attrs[XFRMA_ENCAP]) {
543 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
544 sizeof(*x->encap), GFP_KERNEL);
545 if (x->encap == NULL)
549 if (attrs[XFRMA_TFCPAD])
550 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
552 if (attrs[XFRMA_COADDR]) {
553 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
554 sizeof(*x->coaddr), GFP_KERNEL);
555 if (x->coaddr == NULL)
559 xfrm_mark_get(attrs, &x->mark);
561 err = __xfrm_init_state(x, false);
565 if (attrs[XFRMA_SEC_CTX] &&
566 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
569 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
570 attrs[XFRMA_REPLAY_ESN_VAL])))
574 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
575 /* sysctl_xfrm_aevent_etime is in 100ms units */
576 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
578 if ((err = xfrm_init_replay(x)))
581 /* override default values from above */
582 xfrm_update_ae_params(x, attrs, 0);
587 x->km.state = XFRM_STATE_DEAD;
594 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
595 struct nlattr **attrs)
597 struct net *net = sock_net(skb->sk);
598 struct xfrm_usersa_info *p = nlmsg_data(nlh);
599 struct xfrm_state *x;
602 kuid_t loginuid = audit_get_loginuid(current);
603 unsigned int sessionid = audit_get_sessionid(current);
606 err = verify_newsa_info(p, attrs);
610 x = xfrm_state_construct(net, p, attrs, &err);
615 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
616 err = xfrm_state_add(x);
618 err = xfrm_state_update(x);
620 security_task_getsecid(current, &sid);
621 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
624 x->km.state = XFRM_STATE_DEAD;
629 c.seq = nlh->nlmsg_seq;
630 c.portid = nlh->nlmsg_pid;
631 c.event = nlh->nlmsg_type;
633 km_state_notify(x, &c);
639 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
640 struct xfrm_usersa_id *p,
641 struct nlattr **attrs,
644 struct xfrm_state *x = NULL;
647 u32 mark = xfrm_mark_get(attrs, &m);
649 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
651 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
653 xfrm_address_t *saddr = NULL;
655 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
662 x = xfrm_state_lookup_byaddr(net, mark,
664 p->proto, p->family);
673 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
674 struct nlattr **attrs)
676 struct net *net = sock_net(skb->sk);
677 struct xfrm_state *x;
680 struct xfrm_usersa_id *p = nlmsg_data(nlh);
681 kuid_t loginuid = audit_get_loginuid(current);
682 unsigned int sessionid = audit_get_sessionid(current);
685 x = xfrm_user_state_lookup(net, p, attrs, &err);
689 if ((err = security_xfrm_state_delete(x)) != 0)
692 if (xfrm_state_kern(x)) {
697 err = xfrm_state_delete(x);
702 c.seq = nlh->nlmsg_seq;
703 c.portid = nlh->nlmsg_pid;
704 c.event = nlh->nlmsg_type;
705 km_state_notify(x, &c);
708 security_task_getsecid(current, &sid);
709 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
714 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
716 memset(p, 0, sizeof(*p));
717 memcpy(&p->id, &x->id, sizeof(p->id));
718 memcpy(&p->sel, &x->sel, sizeof(p->sel));
719 memcpy(&p->lft, &x->lft, sizeof(p->lft));
720 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
721 memcpy(&p->stats, &x->stats, sizeof(p->stats));
722 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
723 p->mode = x->props.mode;
724 p->replay_window = x->props.replay_window;
725 p->reqid = x->props.reqid;
726 p->family = x->props.family;
727 p->flags = x->props.flags;
731 struct xfrm_dump_info {
732 struct sk_buff *in_skb;
733 struct sk_buff *out_skb;
738 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
740 struct xfrm_user_sec_ctx *uctx;
742 int ctx_size = sizeof(*uctx) + s->ctx_len;
744 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
748 uctx = nla_data(attr);
749 uctx->exttype = XFRMA_SEC_CTX;
750 uctx->len = ctx_size;
751 uctx->ctx_doi = s->ctx_doi;
752 uctx->ctx_alg = s->ctx_alg;
753 uctx->ctx_len = s->ctx_len;
754 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
759 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
761 struct xfrm_algo *algo;
764 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
765 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
769 algo = nla_data(nla);
770 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
771 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
772 algo->alg_key_len = auth->alg_key_len;
777 /* Don't change this without updating xfrm_sa_len! */
778 static int copy_to_user_state_extra(struct xfrm_state *x,
779 struct xfrm_usersa_info *p,
784 copy_to_user_state(x, p);
786 if (x->props.extra_flags) {
787 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
788 x->props.extra_flags);
794 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
799 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
804 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
809 ret = copy_to_user_auth(x->aalg, skb);
811 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
812 xfrm_alg_auth_len(x->aalg), x->aalg);
817 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
822 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
827 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
832 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
836 ret = xfrm_mark_put(skb, &x->mark);
840 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
841 xfrm_replay_state_esn_len(x->replay_esn),
847 ret = copy_sec_ctx(x->security, skb);
852 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
854 struct xfrm_dump_info *sp = ptr;
855 struct sk_buff *in_skb = sp->in_skb;
856 struct sk_buff *skb = sp->out_skb;
857 struct xfrm_usersa_info *p;
858 struct nlmsghdr *nlh;
861 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
862 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
868 err = copy_to_user_state_extra(x, p, skb);
870 nlmsg_cancel(skb, nlh);
877 static int xfrm_dump_sa_done(struct netlink_callback *cb)
879 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
880 xfrm_state_walk_done(walk);
884 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
886 struct net *net = sock_net(skb->sk);
887 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
888 struct xfrm_dump_info info;
890 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
891 sizeof(cb->args) - sizeof(cb->args[0]));
893 info.in_skb = cb->skb;
895 info.nlmsg_seq = cb->nlh->nlmsg_seq;
896 info.nlmsg_flags = NLM_F_MULTI;
900 xfrm_state_walk_init(walk, 0);
903 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
908 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
909 struct xfrm_state *x, u32 seq)
911 struct xfrm_dump_info info;
915 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
917 return ERR_PTR(-ENOMEM);
919 info.in_skb = in_skb;
921 info.nlmsg_seq = seq;
922 info.nlmsg_flags = 0;
924 err = dump_one_state(x, 0, &info);
933 static inline size_t xfrm_spdinfo_msgsize(void)
935 return NLMSG_ALIGN(4)
936 + nla_total_size(sizeof(struct xfrmu_spdinfo))
937 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
940 static int build_spdinfo(struct sk_buff *skb, struct net *net,
941 u32 portid, u32 seq, u32 flags)
943 struct xfrmk_spdinfo si;
944 struct xfrmu_spdinfo spc;
945 struct xfrmu_spdhinfo sph;
946 struct nlmsghdr *nlh;
950 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
951 if (nlh == NULL) /* shouldn't really happen ... */
956 xfrm_spd_getinfo(net, &si);
957 spc.incnt = si.incnt;
958 spc.outcnt = si.outcnt;
959 spc.fwdcnt = si.fwdcnt;
960 spc.inscnt = si.inscnt;
961 spc.outscnt = si.outscnt;
962 spc.fwdscnt = si.fwdscnt;
963 sph.spdhcnt = si.spdhcnt;
964 sph.spdhmcnt = si.spdhmcnt;
966 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
968 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
970 nlmsg_cancel(skb, nlh);
974 return nlmsg_end(skb, nlh);
977 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
978 struct nlattr **attrs)
980 struct net *net = sock_net(skb->sk);
981 struct sk_buff *r_skb;
982 u32 *flags = nlmsg_data(nlh);
983 u32 sportid = NETLINK_CB(skb).portid;
984 u32 seq = nlh->nlmsg_seq;
986 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
990 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
993 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
996 static inline size_t xfrm_sadinfo_msgsize(void)
998 return NLMSG_ALIGN(4)
999 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1000 + nla_total_size(4); /* XFRMA_SAD_CNT */
1003 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1004 u32 portid, u32 seq, u32 flags)
1006 struct xfrmk_sadinfo si;
1007 struct xfrmu_sadhinfo sh;
1008 struct nlmsghdr *nlh;
1012 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1013 if (nlh == NULL) /* shouldn't really happen ... */
1016 f = nlmsg_data(nlh);
1018 xfrm_sad_getinfo(net, &si);
1020 sh.sadhmcnt = si.sadhmcnt;
1021 sh.sadhcnt = si.sadhcnt;
1023 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1025 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1027 nlmsg_cancel(skb, nlh);
1031 return nlmsg_end(skb, nlh);
1034 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1035 struct nlattr **attrs)
1037 struct net *net = sock_net(skb->sk);
1038 struct sk_buff *r_skb;
1039 u32 *flags = nlmsg_data(nlh);
1040 u32 sportid = NETLINK_CB(skb).portid;
1041 u32 seq = nlh->nlmsg_seq;
1043 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1047 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1050 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1053 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1054 struct nlattr **attrs)
1056 struct net *net = sock_net(skb->sk);
1057 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1058 struct xfrm_state *x;
1059 struct sk_buff *resp_skb;
1062 x = xfrm_user_state_lookup(net, p, attrs, &err);
1066 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1067 if (IS_ERR(resp_skb)) {
1068 err = PTR_ERR(resp_skb);
1070 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1077 static int verify_userspi_info(struct xfrm_userspi_info *p)
1079 switch (p->info.id.proto) {
1085 /* IPCOMP spi is 16-bits. */
1086 if (p->max >= 0x10000)
1094 if (p->min > p->max)
1100 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1101 struct nlattr **attrs)
1103 struct net *net = sock_net(skb->sk);
1104 struct xfrm_state *x;
1105 struct xfrm_userspi_info *p;
1106 struct sk_buff *resp_skb;
1107 xfrm_address_t *daddr;
1113 p = nlmsg_data(nlh);
1114 err = verify_userspi_info(p);
1118 family = p->info.family;
1119 daddr = &p->info.id.daddr;
1123 mark = xfrm_mark_get(attrs, &m);
1125 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1126 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1133 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1134 p->info.id.proto, daddr,
1141 err = xfrm_alloc_spi(x, p->min, p->max);
1145 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1146 if (IS_ERR(resp_skb)) {
1147 err = PTR_ERR(resp_skb);
1151 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1159 static int verify_policy_dir(u8 dir)
1162 case XFRM_POLICY_IN:
1163 case XFRM_POLICY_OUT:
1164 case XFRM_POLICY_FWD:
1174 static int verify_policy_type(u8 type)
1177 case XFRM_POLICY_TYPE_MAIN:
1178 #ifdef CONFIG_XFRM_SUB_POLICY
1179 case XFRM_POLICY_TYPE_SUB:
1190 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1193 case XFRM_SHARE_ANY:
1194 case XFRM_SHARE_SESSION:
1195 case XFRM_SHARE_USER:
1196 case XFRM_SHARE_UNIQUE:
1203 switch (p->action) {
1204 case XFRM_POLICY_ALLOW:
1205 case XFRM_POLICY_BLOCK:
1212 switch (p->sel.family) {
1217 #if IS_ENABLED(CONFIG_IPV6)
1220 return -EAFNOSUPPORT;
1227 return verify_policy_dir(p->dir);
1230 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1232 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1233 struct xfrm_user_sec_ctx *uctx;
1238 uctx = nla_data(rt);
1239 return security_xfrm_policy_alloc(&pol->security, uctx);
1242 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1248 for (i = 0; i < nr; i++, ut++) {
1249 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1251 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1252 memcpy(&t->saddr, &ut->saddr,
1253 sizeof(xfrm_address_t));
1254 t->reqid = ut->reqid;
1256 t->share = ut->share;
1257 t->optional = ut->optional;
1258 t->aalgos = ut->aalgos;
1259 t->ealgos = ut->ealgos;
1260 t->calgos = ut->calgos;
1261 /* If all masks are ~0, then we allow all algorithms. */
1262 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1263 t->encap_family = ut->family;
1267 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1271 if (nr > XFRM_MAX_DEPTH)
1274 for (i = 0; i < nr; i++) {
1275 /* We never validated the ut->family value, so many
1276 * applications simply leave it at zero. The check was
1277 * never made and ut->family was ignored because all
1278 * templates could be assumed to have the same family as
1279 * the policy itself. Now that we will have ipv4-in-ipv6
1280 * and ipv6-in-ipv4 tunnels, this is no longer true.
1283 ut[i].family = family;
1285 switch (ut[i].family) {
1288 #if IS_ENABLED(CONFIG_IPV6)
1300 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1302 struct nlattr *rt = attrs[XFRMA_TMPL];
1307 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1308 int nr = nla_len(rt) / sizeof(*utmpl);
1311 err = validate_tmpl(nr, utmpl, pol->family);
1315 copy_templates(pol, utmpl, nr);
1320 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1322 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1323 struct xfrm_userpolicy_type *upt;
1324 u8 type = XFRM_POLICY_TYPE_MAIN;
1332 err = verify_policy_type(type);
1340 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1342 xp->priority = p->priority;
1343 xp->index = p->index;
1344 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1345 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1346 xp->action = p->action;
1347 xp->flags = p->flags;
1348 xp->family = p->sel.family;
1349 /* XXX xp->share = p->share; */
1352 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1354 memset(p, 0, sizeof(*p));
1355 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1356 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1357 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1358 p->priority = xp->priority;
1359 p->index = xp->index;
1360 p->sel.family = xp->family;
1362 p->action = xp->action;
1363 p->flags = xp->flags;
1364 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1367 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1369 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1377 copy_from_user_policy(xp, p);
1379 err = copy_from_user_policy_type(&xp->type, attrs);
1383 if (!(err = copy_from_user_tmpl(xp, attrs)))
1384 err = copy_from_user_sec_ctx(xp, attrs);
1388 xfrm_mark_get(attrs, &xp->mark);
1394 xfrm_policy_destroy(xp);
1398 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1399 struct nlattr **attrs)
1401 struct net *net = sock_net(skb->sk);
1402 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1403 struct xfrm_policy *xp;
1407 kuid_t loginuid = audit_get_loginuid(current);
1408 unsigned int sessionid = audit_get_sessionid(current);
1411 err = verify_newpolicy_info(p);
1414 err = verify_sec_ctx_len(attrs);
1418 xp = xfrm_policy_construct(net, p, attrs, &err);
1422 /* shouldn't excl be based on nlh flags??
1423 * Aha! this is anti-netlink really i.e more pfkey derived
1424 * in netlink excl is a flag and you wouldnt need
1425 * a type XFRM_MSG_UPDPOLICY - JHS */
1426 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1427 err = xfrm_policy_insert(p->dir, xp, excl);
1428 security_task_getsecid(current, &sid);
1429 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1432 security_xfrm_policy_free(xp->security);
1437 c.event = nlh->nlmsg_type;
1438 c.seq = nlh->nlmsg_seq;
1439 c.portid = nlh->nlmsg_pid;
1440 km_policy_notify(xp, p->dir, &c);
1447 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1449 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1452 if (xp->xfrm_nr == 0)
1455 for (i = 0; i < xp->xfrm_nr; i++) {
1456 struct xfrm_user_tmpl *up = &vec[i];
1457 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1459 memset(up, 0, sizeof(*up));
1460 memcpy(&up->id, &kp->id, sizeof(up->id));
1461 up->family = kp->encap_family;
1462 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1463 up->reqid = kp->reqid;
1464 up->mode = kp->mode;
1465 up->share = kp->share;
1466 up->optional = kp->optional;
1467 up->aalgos = kp->aalgos;
1468 up->ealgos = kp->ealgos;
1469 up->calgos = kp->calgos;
1472 return nla_put(skb, XFRMA_TMPL,
1473 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1476 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1479 return copy_sec_ctx(x->security, skb);
1484 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1487 return copy_sec_ctx(xp->security, skb);
1490 static inline size_t userpolicy_type_attrsize(void)
1492 #ifdef CONFIG_XFRM_SUB_POLICY
1493 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1499 #ifdef CONFIG_XFRM_SUB_POLICY
1500 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1502 struct xfrm_userpolicy_type upt = {
1506 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1510 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1516 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1518 struct xfrm_dump_info *sp = ptr;
1519 struct xfrm_userpolicy_info *p;
1520 struct sk_buff *in_skb = sp->in_skb;
1521 struct sk_buff *skb = sp->out_skb;
1522 struct nlmsghdr *nlh;
1525 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1526 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1530 p = nlmsg_data(nlh);
1531 copy_to_user_policy(xp, p, dir);
1532 err = copy_to_user_tmpl(xp, skb);
1534 err = copy_to_user_sec_ctx(xp, skb);
1536 err = copy_to_user_policy_type(xp->type, skb);
1538 err = xfrm_mark_put(skb, &xp->mark);
1540 nlmsg_cancel(skb, nlh);
1543 nlmsg_end(skb, nlh);
1547 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1549 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1551 xfrm_policy_walk_done(walk);
1555 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1557 struct net *net = sock_net(skb->sk);
1558 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1559 struct xfrm_dump_info info;
1561 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1562 sizeof(cb->args) - sizeof(cb->args[0]));
1564 info.in_skb = cb->skb;
1566 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1567 info.nlmsg_flags = NLM_F_MULTI;
1571 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1574 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1579 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1580 struct xfrm_policy *xp,
1583 struct xfrm_dump_info info;
1584 struct sk_buff *skb;
1587 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1589 return ERR_PTR(-ENOMEM);
1591 info.in_skb = in_skb;
1593 info.nlmsg_seq = seq;
1594 info.nlmsg_flags = 0;
1596 err = dump_one_policy(xp, dir, 0, &info);
1599 return ERR_PTR(err);
1605 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1606 struct nlattr **attrs)
1608 struct net *net = sock_net(skb->sk);
1609 struct xfrm_policy *xp;
1610 struct xfrm_userpolicy_id *p;
1611 u8 type = XFRM_POLICY_TYPE_MAIN;
1616 u32 mark = xfrm_mark_get(attrs, &m);
1618 p = nlmsg_data(nlh);
1619 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1621 err = copy_from_user_policy_type(&type, attrs);
1625 err = verify_policy_dir(p->dir);
1630 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1632 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1633 struct xfrm_sec_ctx *ctx;
1635 err = verify_sec_ctx_len(attrs);
1641 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1643 err = security_xfrm_policy_alloc(&ctx, uctx);
1647 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1649 security_xfrm_policy_free(ctx);
1655 struct sk_buff *resp_skb;
1657 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1658 if (IS_ERR(resp_skb)) {
1659 err = PTR_ERR(resp_skb);
1661 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1662 NETLINK_CB(skb).portid);
1665 kuid_t loginuid = audit_get_loginuid(current);
1666 unsigned int sessionid = audit_get_sessionid(current);
1669 security_task_getsecid(current, &sid);
1670 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1676 c.data.byid = p->index;
1677 c.event = nlh->nlmsg_type;
1678 c.seq = nlh->nlmsg_seq;
1679 c.portid = nlh->nlmsg_pid;
1680 km_policy_notify(xp, p->dir, &c);
1685 if (delete && err == 0)
1686 xfrm_garbage_collect(net);
1690 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1691 struct nlattr **attrs)
1693 struct net *net = sock_net(skb->sk);
1695 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1696 struct xfrm_audit audit_info;
1699 audit_info.loginuid = audit_get_loginuid(current);
1700 audit_info.sessionid = audit_get_sessionid(current);
1701 security_task_getsecid(current, &audit_info.secid);
1702 err = xfrm_state_flush(net, p->proto, &audit_info);
1704 if (err == -ESRCH) /* empty table */
1708 c.data.proto = p->proto;
1709 c.event = nlh->nlmsg_type;
1710 c.seq = nlh->nlmsg_seq;
1711 c.portid = nlh->nlmsg_pid;
1713 km_state_notify(NULL, &c);
1718 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1720 size_t replay_size = x->replay_esn ?
1721 xfrm_replay_state_esn_len(x->replay_esn) :
1722 sizeof(struct xfrm_replay_state);
1724 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1725 + nla_total_size(replay_size)
1726 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1727 + nla_total_size(sizeof(struct xfrm_mark))
1728 + nla_total_size(4) /* XFRM_AE_RTHR */
1729 + nla_total_size(4); /* XFRM_AE_ETHR */
1732 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1734 struct xfrm_aevent_id *id;
1735 struct nlmsghdr *nlh;
1738 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1742 id = nlmsg_data(nlh);
1743 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1744 id->sa_id.spi = x->id.spi;
1745 id->sa_id.family = x->props.family;
1746 id->sa_id.proto = x->id.proto;
1747 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1748 id->reqid = x->props.reqid;
1749 id->flags = c->data.aevent;
1751 if (x->replay_esn) {
1752 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1753 xfrm_replay_state_esn_len(x->replay_esn),
1756 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1761 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1765 if (id->flags & XFRM_AE_RTHR) {
1766 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1770 if (id->flags & XFRM_AE_ETHR) {
1771 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1772 x->replay_maxage * 10 / HZ);
1776 err = xfrm_mark_put(skb, &x->mark);
1780 return nlmsg_end(skb, nlh);
1783 nlmsg_cancel(skb, nlh);
1787 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1788 struct nlattr **attrs)
1790 struct net *net = sock_net(skb->sk);
1791 struct xfrm_state *x;
1792 struct sk_buff *r_skb;
1797 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1798 struct xfrm_usersa_id *id = &p->sa_id;
1800 mark = xfrm_mark_get(attrs, &m);
1802 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1806 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1807 if (r_skb == NULL) {
1813 * XXX: is this lock really needed - none of the other
1814 * gets lock (the concern is things getting updated
1815 * while we are still reading) - jhs
1817 spin_lock_bh(&x->lock);
1818 c.data.aevent = p->flags;
1819 c.seq = nlh->nlmsg_seq;
1820 c.portid = nlh->nlmsg_pid;
1822 if (build_aevent(r_skb, x, &c) < 0)
1824 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1825 spin_unlock_bh(&x->lock);
1830 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1831 struct nlattr **attrs)
1833 struct net *net = sock_net(skb->sk);
1834 struct xfrm_state *x;
1839 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1840 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1841 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1842 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1844 if (!lt && !rp && !re)
1847 /* pedantic mode - thou shalt sayeth replaceth */
1848 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1851 mark = xfrm_mark_get(attrs, &m);
1853 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1857 if (x->km.state != XFRM_STATE_VALID)
1860 err = xfrm_replay_verify_len(x->replay_esn, re);
1864 spin_lock_bh(&x->lock);
1865 xfrm_update_ae_params(x, attrs, 1);
1866 spin_unlock_bh(&x->lock);
1868 c.event = nlh->nlmsg_type;
1869 c.seq = nlh->nlmsg_seq;
1870 c.portid = nlh->nlmsg_pid;
1871 c.data.aevent = XFRM_AE_CU;
1872 km_state_notify(x, &c);
1879 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1880 struct nlattr **attrs)
1882 struct net *net = sock_net(skb->sk);
1884 u8 type = XFRM_POLICY_TYPE_MAIN;
1886 struct xfrm_audit audit_info;
1888 err = copy_from_user_policy_type(&type, attrs);
1892 audit_info.loginuid = audit_get_loginuid(current);
1893 audit_info.sessionid = audit_get_sessionid(current);
1894 security_task_getsecid(current, &audit_info.secid);
1895 err = xfrm_policy_flush(net, type, &audit_info);
1897 if (err == -ESRCH) /* empty table */
1903 c.event = nlh->nlmsg_type;
1904 c.seq = nlh->nlmsg_seq;
1905 c.portid = nlh->nlmsg_pid;
1907 km_policy_notify(NULL, 0, &c);
1911 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1912 struct nlattr **attrs)
1914 struct net *net = sock_net(skb->sk);
1915 struct xfrm_policy *xp;
1916 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1917 struct xfrm_userpolicy_info *p = &up->pol;
1918 u8 type = XFRM_POLICY_TYPE_MAIN;
1921 u32 mark = xfrm_mark_get(attrs, &m);
1923 err = copy_from_user_policy_type(&type, attrs);
1927 err = verify_policy_dir(p->dir);
1932 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1934 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1935 struct xfrm_sec_ctx *ctx;
1937 err = verify_sec_ctx_len(attrs);
1943 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1945 err = security_xfrm_policy_alloc(&ctx, uctx);
1949 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1950 &p->sel, ctx, 0, &err);
1951 security_xfrm_policy_free(ctx);
1956 if (unlikely(xp->walk.dead))
1961 kuid_t loginuid = audit_get_loginuid(current);
1962 unsigned int sessionid = audit_get_sessionid(current);
1965 security_task_getsecid(current, &sid);
1966 xfrm_policy_delete(xp, p->dir);
1967 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1970 // reset the timers here?
1971 WARN(1, "Dont know what to do with soft policy expire\n");
1973 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
1980 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1981 struct nlattr **attrs)
1983 struct net *net = sock_net(skb->sk);
1984 struct xfrm_state *x;
1986 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1987 struct xfrm_usersa_info *p = &ue->state;
1989 u32 mark = xfrm_mark_get(attrs, &m);
1991 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1997 spin_lock_bh(&x->lock);
1999 if (x->km.state != XFRM_STATE_VALID)
2001 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2004 kuid_t loginuid = audit_get_loginuid(current);
2005 unsigned int sessionid = audit_get_sessionid(current);
2008 security_task_getsecid(current, &sid);
2009 __xfrm_state_delete(x);
2010 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
2014 spin_unlock_bh(&x->lock);
2019 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2020 struct nlattr **attrs)
2022 struct net *net = sock_net(skb->sk);
2023 struct xfrm_policy *xp;
2024 struct xfrm_user_tmpl *ut;
2026 struct nlattr *rt = attrs[XFRMA_TMPL];
2027 struct xfrm_mark mark;
2029 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2030 struct xfrm_state *x = xfrm_state_alloc(net);
2036 xfrm_mark_get(attrs, &mark);
2038 err = verify_newpolicy_info(&ua->policy);
2043 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2047 memcpy(&x->id, &ua->id, sizeof(ua->id));
2048 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2049 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2050 xp->mark.m = x->mark.m = mark.m;
2051 xp->mark.v = x->mark.v = mark.v;
2053 /* extract the templates and for each call km_key */
2054 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2055 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2056 memcpy(&x->id, &t->id, sizeof(x->id));
2057 x->props.mode = t->mode;
2058 x->props.reqid = t->reqid;
2059 x->props.family = ut->family;
2060 t->aalgos = ua->aalgos;
2061 t->ealgos = ua->ealgos;
2062 t->calgos = ua->calgos;
2063 err = km_query(x, t, xp);
2073 WARN(1, "BAD policy passed\n");
2080 #ifdef CONFIG_XFRM_MIGRATE
2081 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2082 struct xfrm_kmaddress *k,
2083 struct nlattr **attrs, int *num)
2085 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2086 struct xfrm_user_migrate *um;
2090 struct xfrm_user_kmaddress *uk;
2092 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2093 memcpy(&k->local, &uk->local, sizeof(k->local));
2094 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2095 k->family = uk->family;
2096 k->reserved = uk->reserved;
2100 num_migrate = nla_len(rt) / sizeof(*um);
2102 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2105 for (i = 0; i < num_migrate; i++, um++, ma++) {
2106 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2107 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2108 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2109 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2111 ma->proto = um->proto;
2112 ma->mode = um->mode;
2113 ma->reqid = um->reqid;
2115 ma->old_family = um->old_family;
2116 ma->new_family = um->new_family;
2123 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2124 struct nlattr **attrs)
2126 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2127 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2128 struct xfrm_kmaddress km, *kmp;
2133 if (attrs[XFRMA_MIGRATE] == NULL)
2136 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2138 err = copy_from_user_policy_type(&type, attrs);
2142 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2149 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2154 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2155 struct nlattr **attrs)
2157 return -ENOPROTOOPT;
2161 #ifdef CONFIG_XFRM_MIGRATE
2162 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2164 struct xfrm_user_migrate um;
2166 memset(&um, 0, sizeof(um));
2167 um.proto = m->proto;
2169 um.reqid = m->reqid;
2170 um.old_family = m->old_family;
2171 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2172 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2173 um.new_family = m->new_family;
2174 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2175 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2177 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2180 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2182 struct xfrm_user_kmaddress uk;
2184 memset(&uk, 0, sizeof(uk));
2185 uk.family = k->family;
2186 uk.reserved = k->reserved;
2187 memcpy(&uk.local, &k->local, sizeof(uk.local));
2188 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2190 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2193 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2195 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2196 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2197 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2198 + userpolicy_type_attrsize();
2201 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2202 int num_migrate, const struct xfrm_kmaddress *k,
2203 const struct xfrm_selector *sel, u8 dir, u8 type)
2205 const struct xfrm_migrate *mp;
2206 struct xfrm_userpolicy_id *pol_id;
2207 struct nlmsghdr *nlh;
2210 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2214 pol_id = nlmsg_data(nlh);
2215 /* copy data from selector, dir, and type to the pol_id */
2216 memset(pol_id, 0, sizeof(*pol_id));
2217 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2221 err = copy_to_user_kmaddress(k, skb);
2225 err = copy_to_user_policy_type(type, skb);
2228 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2229 err = copy_to_user_migrate(mp, skb);
2234 return nlmsg_end(skb, nlh);
2237 nlmsg_cancel(skb, nlh);
2241 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2242 const struct xfrm_migrate *m, int num_migrate,
2243 const struct xfrm_kmaddress *k)
2245 struct net *net = &init_net;
2246 struct sk_buff *skb;
2248 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2253 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2256 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2259 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2260 const struct xfrm_migrate *m, int num_migrate,
2261 const struct xfrm_kmaddress *k)
2263 return -ENOPROTOOPT;
2267 #define XMSGSIZE(type) sizeof(struct type)
2269 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2270 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2271 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2272 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2273 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2274 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2275 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2276 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2277 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2278 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2279 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2280 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2281 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2282 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2283 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2284 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2285 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2286 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2287 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2288 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2289 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2294 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2295 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2296 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2297 [XFRMA_LASTUSED] = { .type = NLA_U64},
2298 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2299 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2300 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2301 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2302 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2303 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2304 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2305 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2306 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2307 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2308 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2309 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2310 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2311 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2312 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2313 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2314 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2315 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2316 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2317 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2318 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2321 static const struct xfrm_link {
2322 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2323 int (*dump)(struct sk_buff *, struct netlink_callback *);
2324 int (*done)(struct netlink_callback *);
2325 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2326 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2327 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2328 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2329 .dump = xfrm_dump_sa,
2330 .done = xfrm_dump_sa_done },
2331 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2332 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2333 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2334 .dump = xfrm_dump_policy,
2335 .done = xfrm_dump_policy_done },
2336 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2337 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2338 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2339 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2340 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2341 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2342 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2343 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2344 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2345 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2346 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2347 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2348 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2351 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2353 struct net *net = sock_net(skb->sk);
2354 struct nlattr *attrs[XFRMA_MAX+1];
2355 const struct xfrm_link *link;
2358 type = nlh->nlmsg_type;
2359 if (type > XFRM_MSG_MAX)
2362 type -= XFRM_MSG_BASE;
2363 link = &xfrm_dispatch[type];
2365 /* All operations require privileges, even GET */
2366 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2369 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2370 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2371 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2372 if (link->dump == NULL)
2376 struct netlink_dump_control c = {
2380 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2384 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2389 if (link->doit == NULL)
2392 return link->doit(skb, nlh, attrs);
2395 static void xfrm_netlink_rcv(struct sk_buff *skb)
2397 mutex_lock(&xfrm_cfg_mutex);
2398 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2399 mutex_unlock(&xfrm_cfg_mutex);
2402 static inline size_t xfrm_expire_msgsize(void)
2404 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2405 + nla_total_size(sizeof(struct xfrm_mark));
2408 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2410 struct xfrm_user_expire *ue;
2411 struct nlmsghdr *nlh;
2414 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2418 ue = nlmsg_data(nlh);
2419 copy_to_user_state(x, &ue->state);
2420 ue->hard = (c->data.hard != 0) ? 1 : 0;
2422 err = xfrm_mark_put(skb, &x->mark);
2426 return nlmsg_end(skb, nlh);
2429 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2431 struct net *net = xs_net(x);
2432 struct sk_buff *skb;
2434 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2438 if (build_expire(skb, x, c) < 0) {
2443 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2446 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2448 struct net *net = xs_net(x);
2449 struct sk_buff *skb;
2451 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2455 if (build_aevent(skb, x, c) < 0)
2458 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2461 static int xfrm_notify_sa_flush(const struct km_event *c)
2463 struct net *net = c->net;
2464 struct xfrm_usersa_flush *p;
2465 struct nlmsghdr *nlh;
2466 struct sk_buff *skb;
2467 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2469 skb = nlmsg_new(len, GFP_ATOMIC);
2473 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2479 p = nlmsg_data(nlh);
2480 p->proto = c->data.proto;
2482 nlmsg_end(skb, nlh);
2484 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2487 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2491 l += nla_total_size(aead_len(x->aead));
2493 l += nla_total_size(sizeof(struct xfrm_algo) +
2494 (x->aalg->alg_key_len + 7) / 8);
2495 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2498 l += nla_total_size(xfrm_alg_len(x->ealg));
2500 l += nla_total_size(sizeof(*x->calg));
2502 l += nla_total_size(sizeof(*x->encap));
2504 l += nla_total_size(sizeof(x->tfcpad));
2506 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2508 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2509 x->security->ctx_len);
2511 l += nla_total_size(sizeof(*x->coaddr));
2512 if (x->props.extra_flags)
2513 l += nla_total_size(sizeof(x->props.extra_flags));
2515 /* Must count x->lastused as it may become non-zero behind our back. */
2516 l += nla_total_size(sizeof(u64));
2521 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2523 struct net *net = xs_net(x);
2524 struct xfrm_usersa_info *p;
2525 struct xfrm_usersa_id *id;
2526 struct nlmsghdr *nlh;
2527 struct sk_buff *skb;
2528 int len = xfrm_sa_len(x);
2531 headlen = sizeof(*p);
2532 if (c->event == XFRM_MSG_DELSA) {
2533 len += nla_total_size(headlen);
2534 headlen = sizeof(*id);
2535 len += nla_total_size(sizeof(struct xfrm_mark));
2537 len += NLMSG_ALIGN(headlen);
2539 skb = nlmsg_new(len, GFP_ATOMIC);
2543 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2548 p = nlmsg_data(nlh);
2549 if (c->event == XFRM_MSG_DELSA) {
2550 struct nlattr *attr;
2552 id = nlmsg_data(nlh);
2553 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2554 id->spi = x->id.spi;
2555 id->family = x->props.family;
2556 id->proto = x->id.proto;
2558 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2565 err = copy_to_user_state_extra(x, p, skb);
2569 nlmsg_end(skb, nlh);
2571 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2578 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2582 case XFRM_MSG_EXPIRE:
2583 return xfrm_exp_state_notify(x, c);
2584 case XFRM_MSG_NEWAE:
2585 return xfrm_aevent_state_notify(x, c);
2586 case XFRM_MSG_DELSA:
2587 case XFRM_MSG_UPDSA:
2588 case XFRM_MSG_NEWSA:
2589 return xfrm_notify_sa(x, c);
2590 case XFRM_MSG_FLUSHSA:
2591 return xfrm_notify_sa_flush(c);
2593 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2602 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2603 struct xfrm_policy *xp)
2605 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2606 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2607 + nla_total_size(sizeof(struct xfrm_mark))
2608 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2609 + userpolicy_type_attrsize();
2612 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2613 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2615 __u32 seq = xfrm_get_acqseq();
2616 struct xfrm_user_acquire *ua;
2617 struct nlmsghdr *nlh;
2620 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2624 ua = nlmsg_data(nlh);
2625 memcpy(&ua->id, &x->id, sizeof(ua->id));
2626 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2627 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2628 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2629 ua->aalgos = xt->aalgos;
2630 ua->ealgos = xt->ealgos;
2631 ua->calgos = xt->calgos;
2632 ua->seq = x->km.seq = seq;
2634 err = copy_to_user_tmpl(xp, skb);
2636 err = copy_to_user_state_sec_ctx(x, skb);
2638 err = copy_to_user_policy_type(xp->type, skb);
2640 err = xfrm_mark_put(skb, &xp->mark);
2642 nlmsg_cancel(skb, nlh);
2646 return nlmsg_end(skb, nlh);
2649 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2650 struct xfrm_policy *xp)
2652 struct net *net = xs_net(x);
2653 struct sk_buff *skb;
2655 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2659 if (build_acquire(skb, x, xt, xp) < 0)
2662 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2665 /* User gives us xfrm_user_policy_info followed by an array of 0
2666 * or more templates.
2668 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2669 u8 *data, int len, int *dir)
2671 struct net *net = sock_net(sk);
2672 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2673 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2674 struct xfrm_policy *xp;
2677 switch (sk->sk_family) {
2679 if (opt != IP_XFRM_POLICY) {
2684 #if IS_ENABLED(CONFIG_IPV6)
2686 if (opt != IPV6_XFRM_POLICY) {
2699 if (len < sizeof(*p) ||
2700 verify_newpolicy_info(p))
2703 nr = ((len - sizeof(*p)) / sizeof(*ut));
2704 if (validate_tmpl(nr, ut, p->sel.family))
2707 if (p->dir > XFRM_POLICY_OUT)
2710 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2716 copy_from_user_policy(xp, p);
2717 xp->type = XFRM_POLICY_TYPE_MAIN;
2718 copy_templates(xp, ut, nr);
2725 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2727 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2728 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2729 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2730 + nla_total_size(sizeof(struct xfrm_mark))
2731 + userpolicy_type_attrsize();
2734 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2735 int dir, const struct km_event *c)
2737 struct xfrm_user_polexpire *upe;
2738 int hard = c->data.hard;
2739 struct nlmsghdr *nlh;
2742 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2746 upe = nlmsg_data(nlh);
2747 copy_to_user_policy(xp, &upe->pol, dir);
2748 err = copy_to_user_tmpl(xp, skb);
2750 err = copy_to_user_sec_ctx(xp, skb);
2752 err = copy_to_user_policy_type(xp->type, skb);
2754 err = xfrm_mark_put(skb, &xp->mark);
2756 nlmsg_cancel(skb, nlh);
2761 return nlmsg_end(skb, nlh);
2764 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2766 struct net *net = xp_net(xp);
2767 struct sk_buff *skb;
2769 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2773 if (build_polexpire(skb, xp, dir, c) < 0)
2776 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2779 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2781 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2782 struct net *net = xp_net(xp);
2783 struct xfrm_userpolicy_info *p;
2784 struct xfrm_userpolicy_id *id;
2785 struct nlmsghdr *nlh;
2786 struct sk_buff *skb;
2789 headlen = sizeof(*p);
2790 if (c->event == XFRM_MSG_DELPOLICY) {
2791 len += nla_total_size(headlen);
2792 headlen = sizeof(*id);
2794 len += userpolicy_type_attrsize();
2795 len += nla_total_size(sizeof(struct xfrm_mark));
2796 len += NLMSG_ALIGN(headlen);
2798 skb = nlmsg_new(len, GFP_ATOMIC);
2802 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2807 p = nlmsg_data(nlh);
2808 if (c->event == XFRM_MSG_DELPOLICY) {
2809 struct nlattr *attr;
2811 id = nlmsg_data(nlh);
2812 memset(id, 0, sizeof(*id));
2815 id->index = xp->index;
2817 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2819 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2827 copy_to_user_policy(xp, p, dir);
2828 err = copy_to_user_tmpl(xp, skb);
2830 err = copy_to_user_policy_type(xp->type, skb);
2832 err = xfrm_mark_put(skb, &xp->mark);
2836 nlmsg_end(skb, nlh);
2838 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2845 static int xfrm_notify_policy_flush(const struct km_event *c)
2847 struct net *net = c->net;
2848 struct nlmsghdr *nlh;
2849 struct sk_buff *skb;
2852 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2856 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2860 err = copy_to_user_policy_type(c->data.type, skb);
2864 nlmsg_end(skb, nlh);
2866 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2873 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2877 case XFRM_MSG_NEWPOLICY:
2878 case XFRM_MSG_UPDPOLICY:
2879 case XFRM_MSG_DELPOLICY:
2880 return xfrm_notify_policy(xp, dir, c);
2881 case XFRM_MSG_FLUSHPOLICY:
2882 return xfrm_notify_policy_flush(c);
2883 case XFRM_MSG_POLEXPIRE:
2884 return xfrm_exp_policy_notify(xp, dir, c);
2886 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2894 static inline size_t xfrm_report_msgsize(void)
2896 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2899 static int build_report(struct sk_buff *skb, u8 proto,
2900 struct xfrm_selector *sel, xfrm_address_t *addr)
2902 struct xfrm_user_report *ur;
2903 struct nlmsghdr *nlh;
2905 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2909 ur = nlmsg_data(nlh);
2911 memcpy(&ur->sel, sel, sizeof(ur->sel));
2914 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2916 nlmsg_cancel(skb, nlh);
2920 return nlmsg_end(skb, nlh);
2923 static int xfrm_send_report(struct net *net, u8 proto,
2924 struct xfrm_selector *sel, xfrm_address_t *addr)
2926 struct sk_buff *skb;
2928 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2932 if (build_report(skb, proto, sel, addr) < 0)
2935 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2938 static inline size_t xfrm_mapping_msgsize(void)
2940 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2943 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2944 xfrm_address_t *new_saddr, __be16 new_sport)
2946 struct xfrm_user_mapping *um;
2947 struct nlmsghdr *nlh;
2949 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2953 um = nlmsg_data(nlh);
2955 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2956 um->id.spi = x->id.spi;
2957 um->id.family = x->props.family;
2958 um->id.proto = x->id.proto;
2959 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2960 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2961 um->new_sport = new_sport;
2962 um->old_sport = x->encap->encap_sport;
2963 um->reqid = x->props.reqid;
2965 return nlmsg_end(skb, nlh);
2968 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2971 struct net *net = xs_net(x);
2972 struct sk_buff *skb;
2974 if (x->id.proto != IPPROTO_ESP)
2980 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2984 if (build_mapping(skb, x, ipaddr, sport) < 0)
2987 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2990 static struct xfrm_mgr netlink_mgr = {
2992 .notify = xfrm_send_state_notify,
2993 .acquire = xfrm_send_acquire,
2994 .compile_policy = xfrm_compile_policy,
2995 .notify_policy = xfrm_send_policy_notify,
2996 .report = xfrm_send_report,
2997 .migrate = xfrm_send_migrate,
2998 .new_mapping = xfrm_send_mapping,
3001 static int __net_init xfrm_user_net_init(struct net *net)
3004 struct netlink_kernel_cfg cfg = {
3005 .groups = XFRMNLGRP_MAX,
3006 .input = xfrm_netlink_rcv,
3009 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3012 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3013 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3017 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3020 list_for_each_entry(net, net_exit_list, exit_list)
3021 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3023 list_for_each_entry(net, net_exit_list, exit_list)
3024 netlink_kernel_release(net->xfrm.nlsk_stash);
3027 static struct pernet_operations xfrm_user_net_ops = {
3028 .init = xfrm_user_net_init,
3029 .exit_batch = xfrm_user_net_exit,
3032 static int __init xfrm_user_init(void)
3036 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3038 rv = register_pernet_subsys(&xfrm_user_net_ops);
3041 rv = xfrm_register_km(&netlink_mgr);
3043 unregister_pernet_subsys(&xfrm_user_net_ops);
3047 static void __exit xfrm_user_exit(void)
3049 xfrm_unregister_km(&netlink_mgr);
3050 unregister_pernet_subsys(&xfrm_user_net_ops);
3053 module_init(xfrm_user_init);
3054 module_exit(xfrm_user_exit);
3055 MODULE_LICENSE("GPL");
3056 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);