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] ||
184 attrs[XFRMA_TFCPAD] ||
185 (ntohl(p->id.spi) >= 0x10000))
191 if (attrs[XFRMA_ALG_COMP])
193 if (!attrs[XFRMA_ALG_AUTH] &&
194 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
195 !attrs[XFRMA_ALG_CRYPT] &&
196 !attrs[XFRMA_ALG_AEAD])
198 if ((attrs[XFRMA_ALG_AUTH] ||
199 attrs[XFRMA_ALG_AUTH_TRUNC] ||
200 attrs[XFRMA_ALG_CRYPT]) &&
201 attrs[XFRMA_ALG_AEAD])
203 if (attrs[XFRMA_TFCPAD] &&
204 p->mode != XFRM_MODE_TUNNEL)
209 if (!attrs[XFRMA_ALG_COMP] ||
210 attrs[XFRMA_ALG_AEAD] ||
211 attrs[XFRMA_ALG_AUTH] ||
212 attrs[XFRMA_ALG_AUTH_TRUNC] ||
213 attrs[XFRMA_ALG_CRYPT] ||
218 #if IS_ENABLED(CONFIG_IPV6)
219 case IPPROTO_DSTOPTS:
220 case IPPROTO_ROUTING:
221 if (attrs[XFRMA_ALG_COMP] ||
222 attrs[XFRMA_ALG_AUTH] ||
223 attrs[XFRMA_ALG_AUTH_TRUNC] ||
224 attrs[XFRMA_ALG_AEAD] ||
225 attrs[XFRMA_ALG_CRYPT] ||
226 attrs[XFRMA_ENCAP] ||
227 attrs[XFRMA_SEC_CTX] ||
228 attrs[XFRMA_TFCPAD] ||
229 !attrs[XFRMA_COADDR])
238 if ((err = verify_aead(attrs)))
240 if ((err = verify_auth_trunc(attrs)))
242 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
244 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
246 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
248 if ((err = verify_sec_ctx_len(attrs)))
250 if ((err = verify_replay(p, attrs)))
255 case XFRM_MODE_TRANSPORT:
256 case XFRM_MODE_TUNNEL:
257 case XFRM_MODE_ROUTEOPTIMIZATION:
271 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
272 struct xfrm_algo_desc *(*get_byname)(const char *, int),
275 struct xfrm_algo *p, *ualg;
276 struct xfrm_algo_desc *algo;
281 ualg = nla_data(rta);
283 algo = get_byname(ualg->alg_name, 1);
286 *props = algo->desc.sadb_alg_id;
288 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
292 strcpy(p->alg_name, algo->name);
297 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
300 struct xfrm_algo *ualg;
301 struct xfrm_algo_auth *p;
302 struct xfrm_algo_desc *algo;
307 ualg = nla_data(rta);
309 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
312 *props = algo->desc.sadb_alg_id;
314 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
318 strcpy(p->alg_name, algo->name);
319 p->alg_key_len = ualg->alg_key_len;
320 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
321 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
327 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
330 struct xfrm_algo_auth *p, *ualg;
331 struct xfrm_algo_desc *algo;
336 ualg = nla_data(rta);
338 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
341 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
342 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
344 *props = algo->desc.sadb_alg_id;
346 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
350 strcpy(p->alg_name, algo->name);
351 if (!p->alg_trunc_len)
352 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
358 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
361 struct xfrm_algo_aead *p, *ualg;
362 struct xfrm_algo_desc *algo;
367 ualg = nla_data(rta);
369 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
372 *props = algo->desc.sadb_alg_id;
374 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
378 strcpy(p->alg_name, algo->name);
383 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
386 struct xfrm_replay_state_esn *up;
389 if (!replay_esn || !rp)
393 ulen = xfrm_replay_state_esn_len(up);
395 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
401 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
402 struct xfrm_replay_state_esn **preplay_esn,
405 struct xfrm_replay_state_esn *p, *pp, *up;
412 klen = xfrm_replay_state_esn_len(up);
413 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
415 p = kzalloc(klen, GFP_KERNEL);
419 pp = kzalloc(klen, GFP_KERNEL);
426 memcpy(pp, up, ulen);
434 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
439 len += sizeof(struct xfrm_user_sec_ctx);
440 len += xfrm_ctx->ctx_len;
445 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
447 memcpy(&x->id, &p->id, sizeof(x->id));
448 memcpy(&x->sel, &p->sel, sizeof(x->sel));
449 memcpy(&x->lft, &p->lft, sizeof(x->lft));
450 x->props.mode = p->mode;
451 x->props.replay_window = min_t(unsigned int, p->replay_window,
452 sizeof(x->replay.bitmap) * 8);
453 x->props.reqid = p->reqid;
454 x->props.family = p->family;
455 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
456 x->props.flags = p->flags;
458 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
459 x->sel.family = p->family;
463 * someday when pfkey also has support, we could have the code
464 * somehow made shareable and move it to xfrm_state.c - JHS
467 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
470 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
471 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
472 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
473 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
474 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
477 struct xfrm_replay_state_esn *replay_esn;
478 replay_esn = nla_data(re);
479 memcpy(x->replay_esn, replay_esn,
480 xfrm_replay_state_esn_len(replay_esn));
481 memcpy(x->preplay_esn, replay_esn,
482 xfrm_replay_state_esn_len(replay_esn));
486 struct xfrm_replay_state *replay;
487 replay = nla_data(rp);
488 memcpy(&x->replay, replay, sizeof(*replay));
489 memcpy(&x->preplay, replay, sizeof(*replay));
493 struct xfrm_lifetime_cur *ltime;
494 ltime = nla_data(lt);
495 x->curlft.bytes = ltime->bytes;
496 x->curlft.packets = ltime->packets;
497 x->curlft.add_time = ltime->add_time;
498 x->curlft.use_time = ltime->use_time;
502 x->replay_maxage = nla_get_u32(et);
505 x->replay_maxdiff = nla_get_u32(rt);
508 static struct xfrm_state *xfrm_state_construct(struct net *net,
509 struct xfrm_usersa_info *p,
510 struct nlattr **attrs,
513 struct xfrm_state *x = xfrm_state_alloc(net);
519 copy_from_user_state(x, p);
521 if (attrs[XFRMA_SA_EXTRA_FLAGS])
522 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
524 if ((err = attach_aead(&x->aead, &x->props.ealgo,
525 attrs[XFRMA_ALG_AEAD])))
527 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
528 attrs[XFRMA_ALG_AUTH_TRUNC])))
530 if (!x->props.aalgo) {
531 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
532 attrs[XFRMA_ALG_AUTH])))
535 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
536 xfrm_ealg_get_byname,
537 attrs[XFRMA_ALG_CRYPT])))
539 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
540 xfrm_calg_get_byname,
541 attrs[XFRMA_ALG_COMP])))
544 if (attrs[XFRMA_ENCAP]) {
545 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
546 sizeof(*x->encap), GFP_KERNEL);
547 if (x->encap == NULL)
551 if (attrs[XFRMA_TFCPAD])
552 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
554 if (attrs[XFRMA_COADDR]) {
555 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
556 sizeof(*x->coaddr), GFP_KERNEL);
557 if (x->coaddr == NULL)
561 xfrm_mark_get(attrs, &x->mark);
563 err = __xfrm_init_state(x, false);
567 if (attrs[XFRMA_SEC_CTX] &&
568 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
571 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
572 attrs[XFRMA_REPLAY_ESN_VAL])))
576 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
577 /* sysctl_xfrm_aevent_etime is in 100ms units */
578 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
580 if ((err = xfrm_init_replay(x)))
583 /* override default values from above */
584 xfrm_update_ae_params(x, attrs, 0);
589 x->km.state = XFRM_STATE_DEAD;
596 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
597 struct nlattr **attrs)
599 struct net *net = sock_net(skb->sk);
600 struct xfrm_usersa_info *p = nlmsg_data(nlh);
601 struct xfrm_state *x;
604 kuid_t loginuid = audit_get_loginuid(current);
605 u32 sessionid = audit_get_sessionid(current);
608 err = verify_newsa_info(p, attrs);
612 x = xfrm_state_construct(net, p, attrs, &err);
617 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
618 err = xfrm_state_add(x);
620 err = xfrm_state_update(x);
622 security_task_getsecid(current, &sid);
623 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
626 x->km.state = XFRM_STATE_DEAD;
631 c.seq = nlh->nlmsg_seq;
632 c.portid = nlh->nlmsg_pid;
633 c.event = nlh->nlmsg_type;
635 km_state_notify(x, &c);
641 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
642 struct xfrm_usersa_id *p,
643 struct nlattr **attrs,
646 struct xfrm_state *x = NULL;
649 u32 mark = xfrm_mark_get(attrs, &m);
651 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
653 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
655 xfrm_address_t *saddr = NULL;
657 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
664 x = xfrm_state_lookup_byaddr(net, mark,
666 p->proto, p->family);
675 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
676 struct nlattr **attrs)
678 struct net *net = sock_net(skb->sk);
679 struct xfrm_state *x;
682 struct xfrm_usersa_id *p = nlmsg_data(nlh);
683 kuid_t loginuid = audit_get_loginuid(current);
684 u32 sessionid = audit_get_sessionid(current);
687 x = xfrm_user_state_lookup(net, p, attrs, &err);
691 if ((err = security_xfrm_state_delete(x)) != 0)
694 if (xfrm_state_kern(x)) {
699 err = xfrm_state_delete(x);
704 c.seq = nlh->nlmsg_seq;
705 c.portid = nlh->nlmsg_pid;
706 c.event = nlh->nlmsg_type;
707 km_state_notify(x, &c);
710 security_task_getsecid(current, &sid);
711 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
716 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
718 memset(p, 0, sizeof(*p));
719 memcpy(&p->id, &x->id, sizeof(p->id));
720 memcpy(&p->sel, &x->sel, sizeof(p->sel));
721 memcpy(&p->lft, &x->lft, sizeof(p->lft));
722 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
723 memcpy(&p->stats, &x->stats, sizeof(p->stats));
724 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
725 p->mode = x->props.mode;
726 p->replay_window = x->props.replay_window;
727 p->reqid = x->props.reqid;
728 p->family = x->props.family;
729 p->flags = x->props.flags;
733 struct xfrm_dump_info {
734 struct sk_buff *in_skb;
735 struct sk_buff *out_skb;
740 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
742 struct xfrm_user_sec_ctx *uctx;
744 int ctx_size = sizeof(*uctx) + s->ctx_len;
746 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
750 uctx = nla_data(attr);
751 uctx->exttype = XFRMA_SEC_CTX;
752 uctx->len = ctx_size;
753 uctx->ctx_doi = s->ctx_doi;
754 uctx->ctx_alg = s->ctx_alg;
755 uctx->ctx_len = s->ctx_len;
756 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
761 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
763 struct xfrm_algo *algo;
766 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
767 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
771 algo = nla_data(nla);
772 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
773 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
774 algo->alg_key_len = auth->alg_key_len;
779 /* Don't change this without updating xfrm_sa_len! */
780 static int copy_to_user_state_extra(struct xfrm_state *x,
781 struct xfrm_usersa_info *p,
786 copy_to_user_state(x, p);
788 if (x->props.extra_flags) {
789 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
790 x->props.extra_flags);
796 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
801 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
806 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
811 ret = copy_to_user_auth(x->aalg, skb);
813 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
814 xfrm_alg_auth_len(x->aalg), x->aalg);
819 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
824 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
829 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
834 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
838 ret = xfrm_mark_put(skb, &x->mark);
842 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
843 xfrm_replay_state_esn_len(x->replay_esn),
849 ret = copy_sec_ctx(x->security, skb);
854 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
856 struct xfrm_dump_info *sp = ptr;
857 struct sk_buff *in_skb = sp->in_skb;
858 struct sk_buff *skb = sp->out_skb;
859 struct xfrm_usersa_info *p;
860 struct nlmsghdr *nlh;
863 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
864 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
870 err = copy_to_user_state_extra(x, p, skb);
872 nlmsg_cancel(skb, nlh);
879 static int xfrm_dump_sa_done(struct netlink_callback *cb)
881 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
882 struct sock *sk = cb->skb->sk;
883 struct net *net = sock_net(sk);
885 xfrm_state_walk_done(walk, net);
889 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
891 struct net *net = sock_net(skb->sk);
892 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
893 struct xfrm_dump_info info;
895 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
896 sizeof(cb->args) - sizeof(cb->args[0]));
898 info.in_skb = cb->skb;
900 info.nlmsg_seq = cb->nlh->nlmsg_seq;
901 info.nlmsg_flags = NLM_F_MULTI;
905 xfrm_state_walk_init(walk, 0);
908 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
913 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
914 struct xfrm_state *x, u32 seq)
916 struct xfrm_dump_info info;
920 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
922 return ERR_PTR(-ENOMEM);
924 info.in_skb = in_skb;
926 info.nlmsg_seq = seq;
927 info.nlmsg_flags = 0;
929 err = dump_one_state(x, 0, &info);
938 static inline size_t xfrm_spdinfo_msgsize(void)
940 return NLMSG_ALIGN(4)
941 + nla_total_size(sizeof(struct xfrmu_spdinfo))
942 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
945 static int build_spdinfo(struct sk_buff *skb, struct net *net,
946 u32 portid, u32 seq, u32 flags)
948 struct xfrmk_spdinfo si;
949 struct xfrmu_spdinfo spc;
950 struct xfrmu_spdhinfo sph;
951 struct nlmsghdr *nlh;
955 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
956 if (nlh == NULL) /* shouldn't really happen ... */
961 xfrm_spd_getinfo(net, &si);
962 spc.incnt = si.incnt;
963 spc.outcnt = si.outcnt;
964 spc.fwdcnt = si.fwdcnt;
965 spc.inscnt = si.inscnt;
966 spc.outscnt = si.outscnt;
967 spc.fwdscnt = si.fwdscnt;
968 sph.spdhcnt = si.spdhcnt;
969 sph.spdhmcnt = si.spdhmcnt;
971 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
973 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
975 nlmsg_cancel(skb, nlh);
979 return nlmsg_end(skb, nlh);
982 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
983 struct nlattr **attrs)
985 struct net *net = sock_net(skb->sk);
986 struct sk_buff *r_skb;
987 u32 *flags = nlmsg_data(nlh);
988 u32 sportid = NETLINK_CB(skb).portid;
989 u32 seq = nlh->nlmsg_seq;
991 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
995 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
998 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1001 static inline size_t xfrm_sadinfo_msgsize(void)
1003 return NLMSG_ALIGN(4)
1004 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1005 + nla_total_size(4); /* XFRMA_SAD_CNT */
1008 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1009 u32 portid, u32 seq, u32 flags)
1011 struct xfrmk_sadinfo si;
1012 struct xfrmu_sadhinfo sh;
1013 struct nlmsghdr *nlh;
1017 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1018 if (nlh == NULL) /* shouldn't really happen ... */
1021 f = nlmsg_data(nlh);
1023 xfrm_sad_getinfo(net, &si);
1025 sh.sadhmcnt = si.sadhmcnt;
1026 sh.sadhcnt = si.sadhcnt;
1028 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1030 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1032 nlmsg_cancel(skb, nlh);
1036 return nlmsg_end(skb, nlh);
1039 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1040 struct nlattr **attrs)
1042 struct net *net = sock_net(skb->sk);
1043 struct sk_buff *r_skb;
1044 u32 *flags = nlmsg_data(nlh);
1045 u32 sportid = NETLINK_CB(skb).portid;
1046 u32 seq = nlh->nlmsg_seq;
1048 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1052 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1055 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1058 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1059 struct nlattr **attrs)
1061 struct net *net = sock_net(skb->sk);
1062 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1063 struct xfrm_state *x;
1064 struct sk_buff *resp_skb;
1067 x = xfrm_user_state_lookup(net, p, attrs, &err);
1071 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1072 if (IS_ERR(resp_skb)) {
1073 err = PTR_ERR(resp_skb);
1075 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1082 static int verify_userspi_info(struct xfrm_userspi_info *p)
1084 switch (p->info.id.proto) {
1090 /* IPCOMP spi is 16-bits. */
1091 if (p->max >= 0x10000)
1099 if (p->min > p->max)
1105 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1106 struct nlattr **attrs)
1108 struct net *net = sock_net(skb->sk);
1109 struct xfrm_state *x;
1110 struct xfrm_userspi_info *p;
1111 struct sk_buff *resp_skb;
1112 xfrm_address_t *daddr;
1118 p = nlmsg_data(nlh);
1119 err = verify_userspi_info(p);
1123 family = p->info.family;
1124 daddr = &p->info.id.daddr;
1128 mark = xfrm_mark_get(attrs, &m);
1130 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1131 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1138 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1139 p->info.id.proto, daddr,
1146 err = xfrm_alloc_spi(x, p->min, p->max);
1150 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1151 if (IS_ERR(resp_skb)) {
1152 err = PTR_ERR(resp_skb);
1156 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1164 static int verify_policy_dir(u8 dir)
1167 case XFRM_POLICY_IN:
1168 case XFRM_POLICY_OUT:
1169 case XFRM_POLICY_FWD:
1179 static int verify_policy_type(u8 type)
1182 case XFRM_POLICY_TYPE_MAIN:
1183 #ifdef CONFIG_XFRM_SUB_POLICY
1184 case XFRM_POLICY_TYPE_SUB:
1195 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1200 case XFRM_SHARE_ANY:
1201 case XFRM_SHARE_SESSION:
1202 case XFRM_SHARE_USER:
1203 case XFRM_SHARE_UNIQUE:
1210 switch (p->action) {
1211 case XFRM_POLICY_ALLOW:
1212 case XFRM_POLICY_BLOCK:
1219 switch (p->sel.family) {
1224 #if IS_ENABLED(CONFIG_IPV6)
1227 return -EAFNOSUPPORT;
1234 ret = verify_policy_dir(p->dir);
1237 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1243 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1245 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1246 struct xfrm_user_sec_ctx *uctx;
1251 uctx = nla_data(rt);
1252 return security_xfrm_policy_alloc(&pol->security, uctx);
1255 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1261 for (i = 0; i < nr; i++, ut++) {
1262 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1264 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1265 memcpy(&t->saddr, &ut->saddr,
1266 sizeof(xfrm_address_t));
1267 t->reqid = ut->reqid;
1269 t->share = ut->share;
1270 t->optional = ut->optional;
1271 t->aalgos = ut->aalgos;
1272 t->ealgos = ut->ealgos;
1273 t->calgos = ut->calgos;
1274 /* If all masks are ~0, then we allow all algorithms. */
1275 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1276 t->encap_family = ut->family;
1280 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1284 if (nr > XFRM_MAX_DEPTH)
1287 for (i = 0; i < nr; i++) {
1288 /* We never validated the ut->family value, so many
1289 * applications simply leave it at zero. The check was
1290 * never made and ut->family was ignored because all
1291 * templates could be assumed to have the same family as
1292 * the policy itself. Now that we will have ipv4-in-ipv6
1293 * and ipv6-in-ipv4 tunnels, this is no longer true.
1296 ut[i].family = family;
1298 switch (ut[i].family) {
1301 #if IS_ENABLED(CONFIG_IPV6)
1313 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1315 struct nlattr *rt = attrs[XFRMA_TMPL];
1320 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1321 int nr = nla_len(rt) / sizeof(*utmpl);
1324 err = validate_tmpl(nr, utmpl, pol->family);
1328 copy_templates(pol, utmpl, nr);
1333 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1335 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1336 struct xfrm_userpolicy_type *upt;
1337 u8 type = XFRM_POLICY_TYPE_MAIN;
1345 err = verify_policy_type(type);
1353 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1355 xp->priority = p->priority;
1356 xp->index = p->index;
1357 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1358 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1359 xp->action = p->action;
1360 xp->flags = p->flags;
1361 xp->family = p->sel.family;
1362 /* XXX xp->share = p->share; */
1365 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1367 memset(p, 0, sizeof(*p));
1368 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1369 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1370 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1371 p->priority = xp->priority;
1372 p->index = xp->index;
1373 p->sel.family = xp->family;
1375 p->action = xp->action;
1376 p->flags = xp->flags;
1377 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1380 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1382 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1390 copy_from_user_policy(xp, p);
1392 err = copy_from_user_policy_type(&xp->type, attrs);
1396 if (!(err = copy_from_user_tmpl(xp, attrs)))
1397 err = copy_from_user_sec_ctx(xp, attrs);
1401 xfrm_mark_get(attrs, &xp->mark);
1407 xfrm_policy_destroy(xp);
1411 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1412 struct nlattr **attrs)
1414 struct net *net = sock_net(skb->sk);
1415 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1416 struct xfrm_policy *xp;
1420 kuid_t loginuid = audit_get_loginuid(current);
1421 u32 sessionid = audit_get_sessionid(current);
1424 err = verify_newpolicy_info(p);
1427 err = verify_sec_ctx_len(attrs);
1431 xp = xfrm_policy_construct(net, p, attrs, &err);
1435 /* shouldn't excl be based on nlh flags??
1436 * Aha! this is anti-netlink really i.e more pfkey derived
1437 * in netlink excl is a flag and you wouldnt need
1438 * a type XFRM_MSG_UPDPOLICY - JHS */
1439 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1440 err = xfrm_policy_insert(p->dir, xp, excl);
1441 security_task_getsecid(current, &sid);
1442 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1445 security_xfrm_policy_free(xp->security);
1450 c.event = nlh->nlmsg_type;
1451 c.seq = nlh->nlmsg_seq;
1452 c.portid = nlh->nlmsg_pid;
1453 km_policy_notify(xp, p->dir, &c);
1460 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1462 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1465 if (xp->xfrm_nr == 0)
1468 for (i = 0; i < xp->xfrm_nr; i++) {
1469 struct xfrm_user_tmpl *up = &vec[i];
1470 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1472 memset(up, 0, sizeof(*up));
1473 memcpy(&up->id, &kp->id, sizeof(up->id));
1474 up->family = kp->encap_family;
1475 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1476 up->reqid = kp->reqid;
1477 up->mode = kp->mode;
1478 up->share = kp->share;
1479 up->optional = kp->optional;
1480 up->aalgos = kp->aalgos;
1481 up->ealgos = kp->ealgos;
1482 up->calgos = kp->calgos;
1485 return nla_put(skb, XFRMA_TMPL,
1486 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1489 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1492 return copy_sec_ctx(x->security, skb);
1497 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1500 return copy_sec_ctx(xp->security, skb);
1503 static inline size_t userpolicy_type_attrsize(void)
1505 #ifdef CONFIG_XFRM_SUB_POLICY
1506 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1512 #ifdef CONFIG_XFRM_SUB_POLICY
1513 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1515 struct xfrm_userpolicy_type upt = {
1519 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1523 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1529 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1531 struct xfrm_dump_info *sp = ptr;
1532 struct xfrm_userpolicy_info *p;
1533 struct sk_buff *in_skb = sp->in_skb;
1534 struct sk_buff *skb = sp->out_skb;
1535 struct nlmsghdr *nlh;
1538 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1539 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1543 p = nlmsg_data(nlh);
1544 copy_to_user_policy(xp, p, dir);
1545 err = copy_to_user_tmpl(xp, skb);
1547 err = copy_to_user_sec_ctx(xp, skb);
1549 err = copy_to_user_policy_type(xp->type, skb);
1551 err = xfrm_mark_put(skb, &xp->mark);
1553 nlmsg_cancel(skb, nlh);
1556 nlmsg_end(skb, nlh);
1560 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1562 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1563 struct net *net = sock_net(cb->skb->sk);
1565 xfrm_policy_walk_done(walk, net);
1569 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1571 struct net *net = sock_net(skb->sk);
1572 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1573 struct xfrm_dump_info info;
1575 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1576 sizeof(cb->args) - sizeof(cb->args[0]));
1578 info.in_skb = cb->skb;
1580 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1581 info.nlmsg_flags = NLM_F_MULTI;
1585 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1588 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1593 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1594 struct xfrm_policy *xp,
1597 struct xfrm_dump_info info;
1598 struct sk_buff *skb;
1601 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1603 return ERR_PTR(-ENOMEM);
1605 info.in_skb = in_skb;
1607 info.nlmsg_seq = seq;
1608 info.nlmsg_flags = 0;
1610 err = dump_one_policy(xp, dir, 0, &info);
1613 return ERR_PTR(err);
1619 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1620 struct nlattr **attrs)
1622 struct net *net = sock_net(skb->sk);
1623 struct xfrm_policy *xp;
1624 struct xfrm_userpolicy_id *p;
1625 u8 type = XFRM_POLICY_TYPE_MAIN;
1630 u32 mark = xfrm_mark_get(attrs, &m);
1632 p = nlmsg_data(nlh);
1633 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1635 err = copy_from_user_policy_type(&type, attrs);
1639 err = verify_policy_dir(p->dir);
1644 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1646 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1647 struct xfrm_sec_ctx *ctx;
1649 err = verify_sec_ctx_len(attrs);
1655 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1657 err = security_xfrm_policy_alloc(&ctx, uctx);
1661 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1663 security_xfrm_policy_free(ctx);
1669 struct sk_buff *resp_skb;
1671 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1672 if (IS_ERR(resp_skb)) {
1673 err = PTR_ERR(resp_skb);
1675 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1676 NETLINK_CB(skb).portid);
1679 kuid_t loginuid = audit_get_loginuid(current);
1680 u32 sessionid = audit_get_sessionid(current);
1683 security_task_getsecid(current, &sid);
1684 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1690 c.data.byid = p->index;
1691 c.event = nlh->nlmsg_type;
1692 c.seq = nlh->nlmsg_seq;
1693 c.portid = nlh->nlmsg_pid;
1694 km_policy_notify(xp, p->dir, &c);
1699 if (delete && err == 0)
1700 xfrm_garbage_collect(net);
1704 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1705 struct nlattr **attrs)
1707 struct net *net = sock_net(skb->sk);
1709 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1710 struct xfrm_audit audit_info;
1713 audit_info.loginuid = audit_get_loginuid(current);
1714 audit_info.sessionid = audit_get_sessionid(current);
1715 security_task_getsecid(current, &audit_info.secid);
1716 err = xfrm_state_flush(net, p->proto, &audit_info);
1718 if (err == -ESRCH) /* empty table */
1722 c.data.proto = p->proto;
1723 c.event = nlh->nlmsg_type;
1724 c.seq = nlh->nlmsg_seq;
1725 c.portid = nlh->nlmsg_pid;
1727 km_state_notify(NULL, &c);
1732 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1734 size_t replay_size = x->replay_esn ?
1735 xfrm_replay_state_esn_len(x->replay_esn) :
1736 sizeof(struct xfrm_replay_state);
1738 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1739 + nla_total_size(replay_size)
1740 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1741 + nla_total_size(sizeof(struct xfrm_mark))
1742 + nla_total_size(4) /* XFRM_AE_RTHR */
1743 + nla_total_size(4); /* XFRM_AE_ETHR */
1746 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1748 struct xfrm_aevent_id *id;
1749 struct nlmsghdr *nlh;
1752 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1756 id = nlmsg_data(nlh);
1757 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1758 id->sa_id.spi = x->id.spi;
1759 id->sa_id.family = x->props.family;
1760 id->sa_id.proto = x->id.proto;
1761 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1762 id->reqid = x->props.reqid;
1763 id->flags = c->data.aevent;
1765 if (x->replay_esn) {
1766 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1767 xfrm_replay_state_esn_len(x->replay_esn),
1770 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1775 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1779 if (id->flags & XFRM_AE_RTHR) {
1780 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1784 if (id->flags & XFRM_AE_ETHR) {
1785 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1786 x->replay_maxage * 10 / HZ);
1790 err = xfrm_mark_put(skb, &x->mark);
1794 return nlmsg_end(skb, nlh);
1797 nlmsg_cancel(skb, nlh);
1801 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1802 struct nlattr **attrs)
1804 struct net *net = sock_net(skb->sk);
1805 struct xfrm_state *x;
1806 struct sk_buff *r_skb;
1811 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1812 struct xfrm_usersa_id *id = &p->sa_id;
1814 mark = xfrm_mark_get(attrs, &m);
1816 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1820 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1821 if (r_skb == NULL) {
1827 * XXX: is this lock really needed - none of the other
1828 * gets lock (the concern is things getting updated
1829 * while we are still reading) - jhs
1831 spin_lock_bh(&x->lock);
1832 c.data.aevent = p->flags;
1833 c.seq = nlh->nlmsg_seq;
1834 c.portid = nlh->nlmsg_pid;
1836 if (build_aevent(r_skb, x, &c) < 0)
1838 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1839 spin_unlock_bh(&x->lock);
1844 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1845 struct nlattr **attrs)
1847 struct net *net = sock_net(skb->sk);
1848 struct xfrm_state *x;
1853 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1854 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1855 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1856 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1858 if (!lt && !rp && !re)
1861 /* pedantic mode - thou shalt sayeth replaceth */
1862 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1865 mark = xfrm_mark_get(attrs, &m);
1867 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1871 if (x->km.state != XFRM_STATE_VALID)
1874 err = xfrm_replay_verify_len(x->replay_esn, re);
1878 spin_lock_bh(&x->lock);
1879 xfrm_update_ae_params(x, attrs, 1);
1880 spin_unlock_bh(&x->lock);
1882 c.event = nlh->nlmsg_type;
1883 c.seq = nlh->nlmsg_seq;
1884 c.portid = nlh->nlmsg_pid;
1885 c.data.aevent = XFRM_AE_CU;
1886 km_state_notify(x, &c);
1893 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1894 struct nlattr **attrs)
1896 struct net *net = sock_net(skb->sk);
1898 u8 type = XFRM_POLICY_TYPE_MAIN;
1900 struct xfrm_audit audit_info;
1902 err = copy_from_user_policy_type(&type, attrs);
1906 audit_info.loginuid = audit_get_loginuid(current);
1907 audit_info.sessionid = audit_get_sessionid(current);
1908 security_task_getsecid(current, &audit_info.secid);
1909 err = xfrm_policy_flush(net, type, &audit_info);
1911 if (err == -ESRCH) /* empty table */
1917 c.event = nlh->nlmsg_type;
1918 c.seq = nlh->nlmsg_seq;
1919 c.portid = nlh->nlmsg_pid;
1921 km_policy_notify(NULL, 0, &c);
1925 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1926 struct nlattr **attrs)
1928 struct net *net = sock_net(skb->sk);
1929 struct xfrm_policy *xp;
1930 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1931 struct xfrm_userpolicy_info *p = &up->pol;
1932 u8 type = XFRM_POLICY_TYPE_MAIN;
1935 u32 mark = xfrm_mark_get(attrs, &m);
1937 err = copy_from_user_policy_type(&type, attrs);
1941 err = verify_policy_dir(p->dir);
1946 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1948 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1949 struct xfrm_sec_ctx *ctx;
1951 err = verify_sec_ctx_len(attrs);
1957 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1959 err = security_xfrm_policy_alloc(&ctx, uctx);
1963 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1964 &p->sel, ctx, 0, &err);
1965 security_xfrm_policy_free(ctx);
1970 if (unlikely(xp->walk.dead))
1975 kuid_t loginuid = audit_get_loginuid(current);
1976 u32 sessionid = audit_get_sessionid(current);
1979 security_task_getsecid(current, &sid);
1980 xfrm_policy_delete(xp, p->dir);
1981 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1984 // reset the timers here?
1985 WARN(1, "Dont know what to do with soft policy expire\n");
1987 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
1994 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1995 struct nlattr **attrs)
1997 struct net *net = sock_net(skb->sk);
1998 struct xfrm_state *x;
2000 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2001 struct xfrm_usersa_info *p = &ue->state;
2003 u32 mark = xfrm_mark_get(attrs, &m);
2005 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2011 spin_lock_bh(&x->lock);
2013 if (x->km.state != XFRM_STATE_VALID)
2015 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2018 kuid_t loginuid = audit_get_loginuid(current);
2019 u32 sessionid = audit_get_sessionid(current);
2022 security_task_getsecid(current, &sid);
2023 __xfrm_state_delete(x);
2024 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
2028 spin_unlock_bh(&x->lock);
2033 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2034 struct nlattr **attrs)
2036 struct net *net = sock_net(skb->sk);
2037 struct xfrm_policy *xp;
2038 struct xfrm_user_tmpl *ut;
2040 struct nlattr *rt = attrs[XFRMA_TMPL];
2041 struct xfrm_mark mark;
2043 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2044 struct xfrm_state *x = xfrm_state_alloc(net);
2050 xfrm_mark_get(attrs, &mark);
2052 err = verify_newpolicy_info(&ua->policy);
2057 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2061 memcpy(&x->id, &ua->id, sizeof(ua->id));
2062 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2063 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2064 xp->mark.m = x->mark.m = mark.m;
2065 xp->mark.v = x->mark.v = mark.v;
2067 /* extract the templates and for each call km_key */
2068 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2069 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2070 memcpy(&x->id, &t->id, sizeof(x->id));
2071 x->props.mode = t->mode;
2072 x->props.reqid = t->reqid;
2073 x->props.family = ut->family;
2074 t->aalgos = ua->aalgos;
2075 t->ealgos = ua->ealgos;
2076 t->calgos = ua->calgos;
2077 err = km_query(x, t, xp);
2087 WARN(1, "BAD policy passed\n");
2094 #ifdef CONFIG_XFRM_MIGRATE
2095 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2096 struct xfrm_kmaddress *k,
2097 struct nlattr **attrs, int *num)
2099 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2100 struct xfrm_user_migrate *um;
2104 struct xfrm_user_kmaddress *uk;
2106 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2107 memcpy(&k->local, &uk->local, sizeof(k->local));
2108 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2109 k->family = uk->family;
2110 k->reserved = uk->reserved;
2114 num_migrate = nla_len(rt) / sizeof(*um);
2116 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2119 for (i = 0; i < num_migrate; i++, um++, ma++) {
2120 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2121 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2122 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2123 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2125 ma->proto = um->proto;
2126 ma->mode = um->mode;
2127 ma->reqid = um->reqid;
2129 ma->old_family = um->old_family;
2130 ma->new_family = um->new_family;
2137 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2138 struct nlattr **attrs)
2140 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2141 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2142 struct xfrm_kmaddress km, *kmp;
2146 struct net *net = sock_net(skb->sk);
2148 if (attrs[XFRMA_MIGRATE] == NULL)
2151 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2153 err = copy_from_user_policy_type(&type, attrs);
2157 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2164 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2169 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2170 struct nlattr **attrs)
2172 return -ENOPROTOOPT;
2176 #ifdef CONFIG_XFRM_MIGRATE
2177 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2179 struct xfrm_user_migrate um;
2181 memset(&um, 0, sizeof(um));
2182 um.proto = m->proto;
2184 um.reqid = m->reqid;
2185 um.old_family = m->old_family;
2186 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2187 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2188 um.new_family = m->new_family;
2189 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2190 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2192 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2195 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2197 struct xfrm_user_kmaddress uk;
2199 memset(&uk, 0, sizeof(uk));
2200 uk.family = k->family;
2201 uk.reserved = k->reserved;
2202 memcpy(&uk.local, &k->local, sizeof(uk.local));
2203 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2205 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2208 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2210 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2211 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2212 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2213 + userpolicy_type_attrsize();
2216 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2217 int num_migrate, const struct xfrm_kmaddress *k,
2218 const struct xfrm_selector *sel, u8 dir, u8 type)
2220 const struct xfrm_migrate *mp;
2221 struct xfrm_userpolicy_id *pol_id;
2222 struct nlmsghdr *nlh;
2225 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2229 pol_id = nlmsg_data(nlh);
2230 /* copy data from selector, dir, and type to the pol_id */
2231 memset(pol_id, 0, sizeof(*pol_id));
2232 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2236 err = copy_to_user_kmaddress(k, skb);
2240 err = copy_to_user_policy_type(type, skb);
2243 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2244 err = copy_to_user_migrate(mp, skb);
2249 return nlmsg_end(skb, nlh);
2252 nlmsg_cancel(skb, nlh);
2256 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2257 const struct xfrm_migrate *m, int num_migrate,
2258 const struct xfrm_kmaddress *k)
2260 struct net *net = &init_net;
2261 struct sk_buff *skb;
2263 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2268 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2271 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2274 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2275 const struct xfrm_migrate *m, int num_migrate,
2276 const struct xfrm_kmaddress *k)
2278 return -ENOPROTOOPT;
2282 #define XMSGSIZE(type) sizeof(struct type)
2284 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2285 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2286 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2287 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2288 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2289 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2290 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2291 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2292 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2293 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2294 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2295 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2296 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2297 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2298 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2299 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2300 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2301 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2302 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2303 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2304 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2309 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2310 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2311 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2312 [XFRMA_LASTUSED] = { .type = NLA_U64},
2313 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2314 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2315 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2316 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2317 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2318 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2319 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2320 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2321 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2322 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2323 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2324 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2325 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2326 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2327 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2328 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2329 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2330 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2331 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2332 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2333 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2336 static const struct xfrm_link {
2337 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2338 int (*dump)(struct sk_buff *, struct netlink_callback *);
2339 int (*done)(struct netlink_callback *);
2340 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2341 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2342 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2343 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2344 .dump = xfrm_dump_sa,
2345 .done = xfrm_dump_sa_done },
2346 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2347 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2348 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2349 .dump = xfrm_dump_policy,
2350 .done = xfrm_dump_policy_done },
2351 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2352 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2353 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2354 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2355 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2356 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2357 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2358 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2359 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2360 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2361 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2362 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2363 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2366 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2368 struct net *net = sock_net(skb->sk);
2369 struct nlattr *attrs[XFRMA_MAX+1];
2370 const struct xfrm_link *link;
2373 type = nlh->nlmsg_type;
2374 if (type > XFRM_MSG_MAX)
2377 type -= XFRM_MSG_BASE;
2378 link = &xfrm_dispatch[type];
2380 /* All operations require privileges, even GET */
2381 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2384 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2385 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2386 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2387 if (link->dump == NULL)
2391 struct netlink_dump_control c = {
2395 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2399 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2404 if (link->doit == NULL)
2407 return link->doit(skb, nlh, attrs);
2410 static void xfrm_netlink_rcv(struct sk_buff *skb)
2412 struct net *net = sock_net(skb->sk);
2414 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2415 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2416 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2419 static inline size_t xfrm_expire_msgsize(void)
2421 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2422 + nla_total_size(sizeof(struct xfrm_mark));
2425 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2427 struct xfrm_user_expire *ue;
2428 struct nlmsghdr *nlh;
2431 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2435 ue = nlmsg_data(nlh);
2436 copy_to_user_state(x, &ue->state);
2437 ue->hard = (c->data.hard != 0) ? 1 : 0;
2439 err = xfrm_mark_put(skb, &x->mark);
2443 return nlmsg_end(skb, nlh);
2446 static int xfrm_exp_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_expire_msgsize(), GFP_ATOMIC);
2455 if (build_expire(skb, x, c) < 0) {
2460 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2463 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2465 struct net *net = xs_net(x);
2466 struct sk_buff *skb;
2468 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2472 if (build_aevent(skb, x, c) < 0)
2475 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2478 static int xfrm_notify_sa_flush(const struct km_event *c)
2480 struct net *net = c->net;
2481 struct xfrm_usersa_flush *p;
2482 struct nlmsghdr *nlh;
2483 struct sk_buff *skb;
2484 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2486 skb = nlmsg_new(len, GFP_ATOMIC);
2490 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2496 p = nlmsg_data(nlh);
2497 p->proto = c->data.proto;
2499 nlmsg_end(skb, nlh);
2501 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2504 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2508 l += nla_total_size(aead_len(x->aead));
2510 l += nla_total_size(sizeof(struct xfrm_algo) +
2511 (x->aalg->alg_key_len + 7) / 8);
2512 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2515 l += nla_total_size(xfrm_alg_len(x->ealg));
2517 l += nla_total_size(sizeof(*x->calg));
2519 l += nla_total_size(sizeof(*x->encap));
2521 l += nla_total_size(sizeof(x->tfcpad));
2523 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2525 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2526 x->security->ctx_len);
2528 l += nla_total_size(sizeof(*x->coaddr));
2529 if (x->props.extra_flags)
2530 l += nla_total_size(sizeof(x->props.extra_flags));
2532 /* Must count x->lastused as it may become non-zero behind our back. */
2533 l += nla_total_size(sizeof(u64));
2538 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2540 struct net *net = xs_net(x);
2541 struct xfrm_usersa_info *p;
2542 struct xfrm_usersa_id *id;
2543 struct nlmsghdr *nlh;
2544 struct sk_buff *skb;
2545 int len = xfrm_sa_len(x);
2548 headlen = sizeof(*p);
2549 if (c->event == XFRM_MSG_DELSA) {
2550 len += nla_total_size(headlen);
2551 headlen = sizeof(*id);
2552 len += nla_total_size(sizeof(struct xfrm_mark));
2554 len += NLMSG_ALIGN(headlen);
2556 skb = nlmsg_new(len, GFP_ATOMIC);
2560 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2565 p = nlmsg_data(nlh);
2566 if (c->event == XFRM_MSG_DELSA) {
2567 struct nlattr *attr;
2569 id = nlmsg_data(nlh);
2570 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2571 id->spi = x->id.spi;
2572 id->family = x->props.family;
2573 id->proto = x->id.proto;
2575 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2582 err = copy_to_user_state_extra(x, p, skb);
2586 nlmsg_end(skb, nlh);
2588 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2595 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2599 case XFRM_MSG_EXPIRE:
2600 return xfrm_exp_state_notify(x, c);
2601 case XFRM_MSG_NEWAE:
2602 return xfrm_aevent_state_notify(x, c);
2603 case XFRM_MSG_DELSA:
2604 case XFRM_MSG_UPDSA:
2605 case XFRM_MSG_NEWSA:
2606 return xfrm_notify_sa(x, c);
2607 case XFRM_MSG_FLUSHSA:
2608 return xfrm_notify_sa_flush(c);
2610 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2619 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2620 struct xfrm_policy *xp)
2622 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2623 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2624 + nla_total_size(sizeof(struct xfrm_mark))
2625 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2626 + userpolicy_type_attrsize();
2629 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2630 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2632 __u32 seq = xfrm_get_acqseq();
2633 struct xfrm_user_acquire *ua;
2634 struct nlmsghdr *nlh;
2637 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2641 ua = nlmsg_data(nlh);
2642 memcpy(&ua->id, &x->id, sizeof(ua->id));
2643 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2644 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2645 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2646 ua->aalgos = xt->aalgos;
2647 ua->ealgos = xt->ealgos;
2648 ua->calgos = xt->calgos;
2649 ua->seq = x->km.seq = seq;
2651 err = copy_to_user_tmpl(xp, skb);
2653 err = copy_to_user_state_sec_ctx(x, skb);
2655 err = copy_to_user_policy_type(xp->type, skb);
2657 err = xfrm_mark_put(skb, &xp->mark);
2659 nlmsg_cancel(skb, nlh);
2663 return nlmsg_end(skb, nlh);
2666 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2667 struct xfrm_policy *xp)
2669 struct net *net = xs_net(x);
2670 struct sk_buff *skb;
2672 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2676 if (build_acquire(skb, x, xt, xp) < 0)
2679 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2682 /* User gives us xfrm_user_policy_info followed by an array of 0
2683 * or more templates.
2685 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2686 u8 *data, int len, int *dir)
2688 struct net *net = sock_net(sk);
2689 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2690 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2691 struct xfrm_policy *xp;
2694 switch (sk->sk_family) {
2696 if (opt != IP_XFRM_POLICY) {
2701 #if IS_ENABLED(CONFIG_IPV6)
2703 if (opt != IPV6_XFRM_POLICY) {
2716 if (len < sizeof(*p) ||
2717 verify_newpolicy_info(p))
2720 nr = ((len - sizeof(*p)) / sizeof(*ut));
2721 if (validate_tmpl(nr, ut, p->sel.family))
2724 if (p->dir > XFRM_POLICY_OUT)
2727 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2733 copy_from_user_policy(xp, p);
2734 xp->type = XFRM_POLICY_TYPE_MAIN;
2735 copy_templates(xp, ut, nr);
2742 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2744 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2745 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2746 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2747 + nla_total_size(sizeof(struct xfrm_mark))
2748 + userpolicy_type_attrsize();
2751 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2752 int dir, const struct km_event *c)
2754 struct xfrm_user_polexpire *upe;
2755 int hard = c->data.hard;
2756 struct nlmsghdr *nlh;
2759 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2763 upe = nlmsg_data(nlh);
2764 copy_to_user_policy(xp, &upe->pol, dir);
2765 err = copy_to_user_tmpl(xp, skb);
2767 err = copy_to_user_sec_ctx(xp, skb);
2769 err = copy_to_user_policy_type(xp->type, skb);
2771 err = xfrm_mark_put(skb, &xp->mark);
2773 nlmsg_cancel(skb, nlh);
2778 return nlmsg_end(skb, nlh);
2781 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2783 struct net *net = xp_net(xp);
2784 struct sk_buff *skb;
2786 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2790 if (build_polexpire(skb, xp, dir, c) < 0)
2793 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2796 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2798 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2799 struct net *net = xp_net(xp);
2800 struct xfrm_userpolicy_info *p;
2801 struct xfrm_userpolicy_id *id;
2802 struct nlmsghdr *nlh;
2803 struct sk_buff *skb;
2806 headlen = sizeof(*p);
2807 if (c->event == XFRM_MSG_DELPOLICY) {
2808 len += nla_total_size(headlen);
2809 headlen = sizeof(*id);
2811 len += userpolicy_type_attrsize();
2812 len += nla_total_size(sizeof(struct xfrm_mark));
2813 len += NLMSG_ALIGN(headlen);
2815 skb = nlmsg_new(len, GFP_ATOMIC);
2819 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2824 p = nlmsg_data(nlh);
2825 if (c->event == XFRM_MSG_DELPOLICY) {
2826 struct nlattr *attr;
2828 id = nlmsg_data(nlh);
2829 memset(id, 0, sizeof(*id));
2832 id->index = xp->index;
2834 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2836 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2844 copy_to_user_policy(xp, p, dir);
2845 err = copy_to_user_tmpl(xp, skb);
2847 err = copy_to_user_policy_type(xp->type, skb);
2849 err = xfrm_mark_put(skb, &xp->mark);
2853 nlmsg_end(skb, nlh);
2855 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2862 static int xfrm_notify_policy_flush(const struct km_event *c)
2864 struct net *net = c->net;
2865 struct nlmsghdr *nlh;
2866 struct sk_buff *skb;
2869 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2873 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2877 err = copy_to_user_policy_type(c->data.type, skb);
2881 nlmsg_end(skb, nlh);
2883 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2890 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2894 case XFRM_MSG_NEWPOLICY:
2895 case XFRM_MSG_UPDPOLICY:
2896 case XFRM_MSG_DELPOLICY:
2897 return xfrm_notify_policy(xp, dir, c);
2898 case XFRM_MSG_FLUSHPOLICY:
2899 return xfrm_notify_policy_flush(c);
2900 case XFRM_MSG_POLEXPIRE:
2901 return xfrm_exp_policy_notify(xp, dir, c);
2903 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2911 static inline size_t xfrm_report_msgsize(void)
2913 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2916 static int build_report(struct sk_buff *skb, u8 proto,
2917 struct xfrm_selector *sel, xfrm_address_t *addr)
2919 struct xfrm_user_report *ur;
2920 struct nlmsghdr *nlh;
2922 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2926 ur = nlmsg_data(nlh);
2928 memcpy(&ur->sel, sel, sizeof(ur->sel));
2931 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2933 nlmsg_cancel(skb, nlh);
2937 return nlmsg_end(skb, nlh);
2940 static int xfrm_send_report(struct net *net, u8 proto,
2941 struct xfrm_selector *sel, xfrm_address_t *addr)
2943 struct sk_buff *skb;
2945 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2949 if (build_report(skb, proto, sel, addr) < 0)
2952 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2955 static inline size_t xfrm_mapping_msgsize(void)
2957 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2960 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2961 xfrm_address_t *new_saddr, __be16 new_sport)
2963 struct xfrm_user_mapping *um;
2964 struct nlmsghdr *nlh;
2966 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2970 um = nlmsg_data(nlh);
2972 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2973 um->id.spi = x->id.spi;
2974 um->id.family = x->props.family;
2975 um->id.proto = x->id.proto;
2976 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2977 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2978 um->new_sport = new_sport;
2979 um->old_sport = x->encap->encap_sport;
2980 um->reqid = x->props.reqid;
2982 return nlmsg_end(skb, nlh);
2985 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2988 struct net *net = xs_net(x);
2989 struct sk_buff *skb;
2991 if (x->id.proto != IPPROTO_ESP)
2997 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3001 if (build_mapping(skb, x, ipaddr, sport) < 0)
3004 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
3007 static struct xfrm_mgr netlink_mgr = {
3009 .notify = xfrm_send_state_notify,
3010 .acquire = xfrm_send_acquire,
3011 .compile_policy = xfrm_compile_policy,
3012 .notify_policy = xfrm_send_policy_notify,
3013 .report = xfrm_send_report,
3014 .migrate = xfrm_send_migrate,
3015 .new_mapping = xfrm_send_mapping,
3018 static int __net_init xfrm_user_net_init(struct net *net)
3021 struct netlink_kernel_cfg cfg = {
3022 .groups = XFRMNLGRP_MAX,
3023 .input = xfrm_netlink_rcv,
3026 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3029 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3030 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3034 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3037 list_for_each_entry(net, net_exit_list, exit_list)
3038 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3040 list_for_each_entry(net, net_exit_list, exit_list)
3041 netlink_kernel_release(net->xfrm.nlsk_stash);
3044 static struct pernet_operations xfrm_user_net_ops = {
3045 .init = xfrm_user_net_init,
3046 .exit_batch = xfrm_user_net_exit,
3049 static int __init xfrm_user_init(void)
3053 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3055 rv = register_pernet_subsys(&xfrm_user_net_ops);
3058 rv = xfrm_register_km(&netlink_mgr);
3060 unregister_pernet_subsys(&xfrm_user_net_ops);
3064 static void __exit xfrm_user_exit(void)
3066 xfrm_unregister_km(&netlink_mgr);
3067 unregister_pernet_subsys(&xfrm_user_net_ops);
3070 module_init(xfrm_user_init);
3071 module_exit(xfrm_user_exit);
3072 MODULE_LICENSE("GPL");
3073 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);