1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
30 #include <asm/uaccess.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
35 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
37 struct nlattr *rt = attrs[type];
38 struct xfrm_algo *algp;
44 if (nla_len(rt) < xfrm_alg_len(algp))
57 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
61 static int verify_auth_trunc(struct nlattr **attrs)
63 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
64 struct xfrm_algo_auth *algp;
70 if (nla_len(rt) < xfrm_alg_auth_len(algp))
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
77 static int verify_aead(struct nlattr **attrs)
79 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
80 struct xfrm_algo_aead *algp;
86 if (nla_len(rt) < aead_len(algp))
89 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
93 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
94 xfrm_address_t **addrp)
96 struct nlattr *rt = attrs[type];
99 *addrp = nla_data(rt);
102 static inline int verify_sec_ctx_len(struct nlattr **attrs)
104 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
105 struct xfrm_user_sec_ctx *uctx;
111 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
117 static inline int verify_replay(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
120 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
121 struct xfrm_replay_state_esn *rs;
123 if (p->flags & XFRM_STATE_ESN) {
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
140 if (p->id.proto != IPPROTO_ESP)
143 if (p->replay_window != 0)
149 static int verify_newsa_info(struct xfrm_usersa_info *p,
150 struct nlattr **attrs)
160 #if IS_ENABLED(CONFIG_IPV6)
172 switch (p->id.proto) {
174 if ((!attrs[XFRMA_ALG_AUTH] &&
175 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
176 attrs[XFRMA_ALG_AEAD] ||
177 attrs[XFRMA_ALG_CRYPT] ||
178 attrs[XFRMA_ALG_COMP] ||
179 attrs[XFRMA_TFCPAD] ||
180 (ntohl(p->id.spi) >= 0x10000))
186 if (attrs[XFRMA_ALG_COMP])
188 if (!attrs[XFRMA_ALG_AUTH] &&
189 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
190 !attrs[XFRMA_ALG_CRYPT] &&
191 !attrs[XFRMA_ALG_AEAD])
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_TFCPAD] &&
199 p->mode != XFRM_MODE_TUNNEL)
204 if (!attrs[XFRMA_ALG_COMP] ||
205 attrs[XFRMA_ALG_AEAD] ||
206 attrs[XFRMA_ALG_AUTH] ||
207 attrs[XFRMA_ALG_AUTH_TRUNC] ||
208 attrs[XFRMA_ALG_CRYPT] ||
213 #if IS_ENABLED(CONFIG_IPV6)
214 case IPPROTO_DSTOPTS:
215 case IPPROTO_ROUTING:
216 if (attrs[XFRMA_ALG_COMP] ||
217 attrs[XFRMA_ALG_AUTH] ||
218 attrs[XFRMA_ALG_AUTH_TRUNC] ||
219 attrs[XFRMA_ALG_AEAD] ||
220 attrs[XFRMA_ALG_CRYPT] ||
221 attrs[XFRMA_ENCAP] ||
222 attrs[XFRMA_SEC_CTX] ||
223 attrs[XFRMA_TFCPAD] ||
224 !attrs[XFRMA_COADDR])
233 if ((err = verify_aead(attrs)))
235 if ((err = verify_auth_trunc(attrs)))
237 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
239 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
241 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
243 if ((err = verify_sec_ctx_len(attrs)))
245 if ((err = verify_replay(p, attrs)))
250 case XFRM_MODE_TRANSPORT:
251 case XFRM_MODE_TUNNEL:
252 case XFRM_MODE_ROUTEOPTIMIZATION:
266 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
267 struct xfrm_algo_desc *(*get_byname)(const char *, int),
270 struct xfrm_algo *p, *ualg;
271 struct xfrm_algo_desc *algo;
276 ualg = nla_data(rta);
278 algo = get_byname(ualg->alg_name, 1);
281 *props = algo->desc.sadb_alg_id;
283 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
287 strcpy(p->alg_name, algo->name);
292 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
295 struct xfrm_algo *ualg;
296 struct xfrm_algo_auth *p;
297 struct xfrm_algo_desc *algo;
302 ualg = nla_data(rta);
304 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
307 *props = algo->desc.sadb_alg_id;
309 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
313 strcpy(p->alg_name, algo->name);
314 p->alg_key_len = ualg->alg_key_len;
315 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
316 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
322 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
325 struct xfrm_algo_auth *p, *ualg;
326 struct xfrm_algo_desc *algo;
331 ualg = nla_data(rta);
333 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
336 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
337 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
339 *props = algo->desc.sadb_alg_id;
341 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
345 strcpy(p->alg_name, algo->name);
346 if (!p->alg_trunc_len)
347 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
353 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
356 struct xfrm_algo_aead *p, *ualg;
357 struct xfrm_algo_desc *algo;
362 ualg = nla_data(rta);
364 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
367 *props = algo->desc.sadb_alg_id;
369 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
373 strcpy(p->alg_name, algo->name);
378 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
381 struct xfrm_replay_state_esn *up;
384 if (!replay_esn || !rp)
388 ulen = xfrm_replay_state_esn_len(up);
390 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
396 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
397 struct xfrm_replay_state_esn **preplay_esn,
400 struct xfrm_replay_state_esn *p, *pp, *up;
407 klen = xfrm_replay_state_esn_len(up);
408 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
410 p = kzalloc(klen, GFP_KERNEL);
414 pp = kzalloc(klen, GFP_KERNEL);
421 memcpy(pp, up, ulen);
429 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
434 len += sizeof(struct xfrm_user_sec_ctx);
435 len += xfrm_ctx->ctx_len;
440 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
442 memcpy(&x->id, &p->id, sizeof(x->id));
443 memcpy(&x->sel, &p->sel, sizeof(x->sel));
444 memcpy(&x->lft, &p->lft, sizeof(x->lft));
445 x->props.mode = p->mode;
446 x->props.replay_window = min_t(unsigned int, p->replay_window,
447 sizeof(x->replay.bitmap) * 8);
448 x->props.reqid = p->reqid;
449 x->props.family = p->family;
450 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
451 x->props.flags = p->flags;
453 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
454 x->sel.family = p->family;
458 * someday when pfkey also has support, we could have the code
459 * somehow made shareable and move it to xfrm_state.c - JHS
462 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
465 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
466 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
467 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
468 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
469 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
472 struct xfrm_replay_state_esn *replay_esn;
473 replay_esn = nla_data(re);
474 memcpy(x->replay_esn, replay_esn,
475 xfrm_replay_state_esn_len(replay_esn));
476 memcpy(x->preplay_esn, replay_esn,
477 xfrm_replay_state_esn_len(replay_esn));
481 struct xfrm_replay_state *replay;
482 replay = nla_data(rp);
483 memcpy(&x->replay, replay, sizeof(*replay));
484 memcpy(&x->preplay, replay, sizeof(*replay));
488 struct xfrm_lifetime_cur *ltime;
489 ltime = nla_data(lt);
490 x->curlft.bytes = ltime->bytes;
491 x->curlft.packets = ltime->packets;
492 x->curlft.add_time = ltime->add_time;
493 x->curlft.use_time = ltime->use_time;
497 x->replay_maxage = nla_get_u32(et);
500 x->replay_maxdiff = nla_get_u32(rt);
503 static struct xfrm_state *xfrm_state_construct(struct net *net,
504 struct xfrm_usersa_info *p,
505 struct nlattr **attrs,
508 struct xfrm_state *x = xfrm_state_alloc(net);
514 copy_from_user_state(x, p);
516 if (attrs[XFRMA_SA_EXTRA_FLAGS])
517 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
519 if ((err = attach_aead(&x->aead, &x->props.ealgo,
520 attrs[XFRMA_ALG_AEAD])))
522 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
523 attrs[XFRMA_ALG_AUTH_TRUNC])))
525 if (!x->props.aalgo) {
526 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
527 attrs[XFRMA_ALG_AUTH])))
530 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
531 xfrm_ealg_get_byname,
532 attrs[XFRMA_ALG_CRYPT])))
534 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
535 xfrm_calg_get_byname,
536 attrs[XFRMA_ALG_COMP])))
539 if (attrs[XFRMA_ENCAP]) {
540 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
541 sizeof(*x->encap), GFP_KERNEL);
542 if (x->encap == NULL)
546 if (attrs[XFRMA_TFCPAD])
547 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
549 if (attrs[XFRMA_COADDR]) {
550 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
551 sizeof(*x->coaddr), GFP_KERNEL);
552 if (x->coaddr == NULL)
556 xfrm_mark_get(attrs, &x->mark);
558 err = __xfrm_init_state(x, false);
562 if (attrs[XFRMA_SEC_CTX] &&
563 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
566 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
567 attrs[XFRMA_REPLAY_ESN_VAL])))
571 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
572 /* sysctl_xfrm_aevent_etime is in 100ms units */
573 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
575 if ((err = xfrm_init_replay(x)))
578 /* override default values from above */
579 xfrm_update_ae_params(x, attrs, 0);
584 x->km.state = XFRM_STATE_DEAD;
591 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
592 struct nlattr **attrs)
594 struct net *net = sock_net(skb->sk);
595 struct xfrm_usersa_info *p = nlmsg_data(nlh);
596 struct xfrm_state *x;
599 kuid_t loginuid = audit_get_loginuid(current);
600 unsigned int sessionid = audit_get_sessionid(current);
603 err = verify_newsa_info(p, attrs);
607 x = xfrm_state_construct(net, p, attrs, &err);
612 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
613 err = xfrm_state_add(x);
615 err = xfrm_state_update(x);
617 security_task_getsecid(current, &sid);
618 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
621 x->km.state = XFRM_STATE_DEAD;
626 c.seq = nlh->nlmsg_seq;
627 c.portid = nlh->nlmsg_pid;
628 c.event = nlh->nlmsg_type;
630 km_state_notify(x, &c);
636 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
637 struct xfrm_usersa_id *p,
638 struct nlattr **attrs,
641 struct xfrm_state *x = NULL;
644 u32 mark = xfrm_mark_get(attrs, &m);
646 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
648 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
650 xfrm_address_t *saddr = NULL;
652 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
659 x = xfrm_state_lookup_byaddr(net, mark,
661 p->proto, p->family);
670 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
671 struct nlattr **attrs)
673 struct net *net = sock_net(skb->sk);
674 struct xfrm_state *x;
677 struct xfrm_usersa_id *p = nlmsg_data(nlh);
678 kuid_t loginuid = audit_get_loginuid(current);
679 unsigned int sessionid = audit_get_sessionid(current);
682 x = xfrm_user_state_lookup(net, p, attrs, &err);
686 if ((err = security_xfrm_state_delete(x)) != 0)
689 if (xfrm_state_kern(x)) {
694 err = xfrm_state_delete(x);
699 c.seq = nlh->nlmsg_seq;
700 c.portid = nlh->nlmsg_pid;
701 c.event = nlh->nlmsg_type;
702 km_state_notify(x, &c);
705 security_task_getsecid(current, &sid);
706 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
711 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
713 memset(p, 0, sizeof(*p));
714 memcpy(&p->id, &x->id, sizeof(p->id));
715 memcpy(&p->sel, &x->sel, sizeof(p->sel));
716 memcpy(&p->lft, &x->lft, sizeof(p->lft));
717 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
718 memcpy(&p->stats, &x->stats, sizeof(p->stats));
719 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
720 p->mode = x->props.mode;
721 p->replay_window = x->props.replay_window;
722 p->reqid = x->props.reqid;
723 p->family = x->props.family;
724 p->flags = x->props.flags;
728 struct xfrm_dump_info {
729 struct sk_buff *in_skb;
730 struct sk_buff *out_skb;
735 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
737 struct xfrm_user_sec_ctx *uctx;
739 int ctx_size = sizeof(*uctx) + s->ctx_len;
741 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
745 uctx = nla_data(attr);
746 uctx->exttype = XFRMA_SEC_CTX;
747 uctx->len = ctx_size;
748 uctx->ctx_doi = s->ctx_doi;
749 uctx->ctx_alg = s->ctx_alg;
750 uctx->ctx_len = s->ctx_len;
751 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
756 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
758 struct xfrm_algo *algo;
761 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
762 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
766 algo = nla_data(nla);
767 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
768 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
769 algo->alg_key_len = auth->alg_key_len;
774 /* Don't change this without updating xfrm_sa_len! */
775 static int copy_to_user_state_extra(struct xfrm_state *x,
776 struct xfrm_usersa_info *p,
781 copy_to_user_state(x, p);
783 if (x->props.extra_flags) {
784 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
785 x->props.extra_flags);
791 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
796 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
801 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
806 ret = copy_to_user_auth(x->aalg, skb);
808 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
809 xfrm_alg_auth_len(x->aalg), x->aalg);
814 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
819 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
824 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
829 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
833 ret = xfrm_mark_put(skb, &x->mark);
837 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
838 xfrm_replay_state_esn_len(x->replay_esn),
844 ret = copy_sec_ctx(x->security, skb);
849 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
851 struct xfrm_dump_info *sp = ptr;
852 struct sk_buff *in_skb = sp->in_skb;
853 struct sk_buff *skb = sp->out_skb;
854 struct xfrm_usersa_info *p;
855 struct nlmsghdr *nlh;
858 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
859 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
865 err = copy_to_user_state_extra(x, p, skb);
867 nlmsg_cancel(skb, nlh);
874 static int xfrm_dump_sa_done(struct netlink_callback *cb)
876 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
877 struct sock *sk = cb->skb->sk;
878 struct net *net = sock_net(sk);
880 xfrm_state_walk_done(walk, net);
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 xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1078 struct nlattr **attrs)
1080 struct net *net = sock_net(skb->sk);
1081 struct xfrm_state *x;
1082 struct xfrm_userspi_info *p;
1083 struct sk_buff *resp_skb;
1084 xfrm_address_t *daddr;
1090 p = nlmsg_data(nlh);
1091 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1095 family = p->info.family;
1096 daddr = &p->info.id.daddr;
1100 mark = xfrm_mark_get(attrs, &m);
1102 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1103 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1110 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1111 p->info.id.proto, daddr,
1118 err = xfrm_alloc_spi(x, p->min, p->max);
1122 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1123 if (IS_ERR(resp_skb)) {
1124 err = PTR_ERR(resp_skb);
1128 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1136 static int verify_policy_dir(u8 dir)
1139 case XFRM_POLICY_IN:
1140 case XFRM_POLICY_OUT:
1141 case XFRM_POLICY_FWD:
1151 static int verify_policy_type(u8 type)
1154 case XFRM_POLICY_TYPE_MAIN:
1155 #ifdef CONFIG_XFRM_SUB_POLICY
1156 case XFRM_POLICY_TYPE_SUB:
1167 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1172 case XFRM_SHARE_ANY:
1173 case XFRM_SHARE_SESSION:
1174 case XFRM_SHARE_USER:
1175 case XFRM_SHARE_UNIQUE:
1182 switch (p->action) {
1183 case XFRM_POLICY_ALLOW:
1184 case XFRM_POLICY_BLOCK:
1191 switch (p->sel.family) {
1196 #if IS_ENABLED(CONFIG_IPV6)
1199 return -EAFNOSUPPORT;
1206 ret = verify_policy_dir(p->dir);
1209 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1215 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1217 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1218 struct xfrm_user_sec_ctx *uctx;
1223 uctx = nla_data(rt);
1224 return security_xfrm_policy_alloc(&pol->security, uctx);
1227 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1233 for (i = 0; i < nr; i++, ut++) {
1234 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1236 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1237 memcpy(&t->saddr, &ut->saddr,
1238 sizeof(xfrm_address_t));
1239 t->reqid = ut->reqid;
1241 t->share = ut->share;
1242 t->optional = ut->optional;
1243 t->aalgos = ut->aalgos;
1244 t->ealgos = ut->ealgos;
1245 t->calgos = ut->calgos;
1246 /* If all masks are ~0, then we allow all algorithms. */
1247 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1248 t->encap_family = ut->family;
1252 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1256 if (nr > XFRM_MAX_DEPTH)
1259 for (i = 0; i < nr; i++) {
1260 /* We never validated the ut->family value, so many
1261 * applications simply leave it at zero. The check was
1262 * never made and ut->family was ignored because all
1263 * templates could be assumed to have the same family as
1264 * the policy itself. Now that we will have ipv4-in-ipv6
1265 * and ipv6-in-ipv4 tunnels, this is no longer true.
1268 ut[i].family = family;
1270 switch (ut[i].family) {
1273 #if IS_ENABLED(CONFIG_IPV6)
1285 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1287 struct nlattr *rt = attrs[XFRMA_TMPL];
1292 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1293 int nr = nla_len(rt) / sizeof(*utmpl);
1296 err = validate_tmpl(nr, utmpl, pol->family);
1300 copy_templates(pol, utmpl, nr);
1305 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1307 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1308 struct xfrm_userpolicy_type *upt;
1309 u8 type = XFRM_POLICY_TYPE_MAIN;
1317 err = verify_policy_type(type);
1325 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1327 xp->priority = p->priority;
1328 xp->index = p->index;
1329 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1330 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1331 xp->action = p->action;
1332 xp->flags = p->flags;
1333 xp->family = p->sel.family;
1334 /* XXX xp->share = p->share; */
1337 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1339 memset(p, 0, sizeof(*p));
1340 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1341 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1342 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1343 p->priority = xp->priority;
1344 p->index = xp->index;
1345 p->sel.family = xp->family;
1347 p->action = xp->action;
1348 p->flags = xp->flags;
1349 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1352 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1354 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1362 copy_from_user_policy(xp, p);
1364 err = copy_from_user_policy_type(&xp->type, attrs);
1368 if (!(err = copy_from_user_tmpl(xp, attrs)))
1369 err = copy_from_user_sec_ctx(xp, attrs);
1373 xfrm_mark_get(attrs, &xp->mark);
1379 xfrm_policy_destroy(xp);
1383 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1384 struct nlattr **attrs)
1386 struct net *net = sock_net(skb->sk);
1387 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1388 struct xfrm_policy *xp;
1392 kuid_t loginuid = audit_get_loginuid(current);
1393 unsigned int sessionid = audit_get_sessionid(current);
1396 err = verify_newpolicy_info(p);
1399 err = verify_sec_ctx_len(attrs);
1403 xp = xfrm_policy_construct(net, p, attrs, &err);
1407 /* shouldn't excl be based on nlh flags??
1408 * Aha! this is anti-netlink really i.e more pfkey derived
1409 * in netlink excl is a flag and you wouldnt need
1410 * a type XFRM_MSG_UPDPOLICY - JHS */
1411 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1412 err = xfrm_policy_insert(p->dir, xp, excl);
1413 security_task_getsecid(current, &sid);
1414 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1417 security_xfrm_policy_free(xp->security);
1422 c.event = nlh->nlmsg_type;
1423 c.seq = nlh->nlmsg_seq;
1424 c.portid = nlh->nlmsg_pid;
1425 km_policy_notify(xp, p->dir, &c);
1432 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1434 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1437 if (xp->xfrm_nr == 0)
1440 for (i = 0; i < xp->xfrm_nr; i++) {
1441 struct xfrm_user_tmpl *up = &vec[i];
1442 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1444 memset(up, 0, sizeof(*up));
1445 memcpy(&up->id, &kp->id, sizeof(up->id));
1446 up->family = kp->encap_family;
1447 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1448 up->reqid = kp->reqid;
1449 up->mode = kp->mode;
1450 up->share = kp->share;
1451 up->optional = kp->optional;
1452 up->aalgos = kp->aalgos;
1453 up->ealgos = kp->ealgos;
1454 up->calgos = kp->calgos;
1457 return nla_put(skb, XFRMA_TMPL,
1458 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1461 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1464 return copy_sec_ctx(x->security, skb);
1469 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1472 return copy_sec_ctx(xp->security, skb);
1475 static inline size_t userpolicy_type_attrsize(void)
1477 #ifdef CONFIG_XFRM_SUB_POLICY
1478 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1484 #ifdef CONFIG_XFRM_SUB_POLICY
1485 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1487 struct xfrm_userpolicy_type upt = {
1491 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1495 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1501 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1503 struct xfrm_dump_info *sp = ptr;
1504 struct xfrm_userpolicy_info *p;
1505 struct sk_buff *in_skb = sp->in_skb;
1506 struct sk_buff *skb = sp->out_skb;
1507 struct nlmsghdr *nlh;
1510 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1511 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1515 p = nlmsg_data(nlh);
1516 copy_to_user_policy(xp, p, dir);
1517 err = copy_to_user_tmpl(xp, skb);
1519 err = copy_to_user_sec_ctx(xp, skb);
1521 err = copy_to_user_policy_type(xp->type, skb);
1523 err = xfrm_mark_put(skb, &xp->mark);
1525 nlmsg_cancel(skb, nlh);
1528 nlmsg_end(skb, nlh);
1532 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1534 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1535 struct net *net = sock_net(cb->skb->sk);
1537 xfrm_policy_walk_done(walk, net);
1541 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1543 struct net *net = sock_net(skb->sk);
1544 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1545 struct xfrm_dump_info info;
1547 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1548 sizeof(cb->args) - sizeof(cb->args[0]));
1550 info.in_skb = cb->skb;
1552 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1553 info.nlmsg_flags = NLM_F_MULTI;
1557 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1560 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1565 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1566 struct xfrm_policy *xp,
1569 struct xfrm_dump_info info;
1570 struct sk_buff *skb;
1573 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1575 return ERR_PTR(-ENOMEM);
1577 info.in_skb = in_skb;
1579 info.nlmsg_seq = seq;
1580 info.nlmsg_flags = 0;
1582 err = dump_one_policy(xp, dir, 0, &info);
1585 return ERR_PTR(err);
1591 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1592 struct nlattr **attrs)
1594 struct net *net = sock_net(skb->sk);
1595 struct xfrm_policy *xp;
1596 struct xfrm_userpolicy_id *p;
1597 u8 type = XFRM_POLICY_TYPE_MAIN;
1602 u32 mark = xfrm_mark_get(attrs, &m);
1604 p = nlmsg_data(nlh);
1605 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1607 err = copy_from_user_policy_type(&type, attrs);
1611 err = verify_policy_dir(p->dir);
1616 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1618 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1619 struct xfrm_sec_ctx *ctx;
1621 err = verify_sec_ctx_len(attrs);
1627 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1629 err = security_xfrm_policy_alloc(&ctx, uctx);
1633 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1635 security_xfrm_policy_free(ctx);
1641 struct sk_buff *resp_skb;
1643 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1644 if (IS_ERR(resp_skb)) {
1645 err = PTR_ERR(resp_skb);
1647 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1648 NETLINK_CB(skb).portid);
1651 kuid_t loginuid = audit_get_loginuid(current);
1652 unsigned int sessionid = audit_get_sessionid(current);
1655 security_task_getsecid(current, &sid);
1656 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1662 c.data.byid = p->index;
1663 c.event = nlh->nlmsg_type;
1664 c.seq = nlh->nlmsg_seq;
1665 c.portid = nlh->nlmsg_pid;
1666 km_policy_notify(xp, p->dir, &c);
1671 if (delete && err == 0)
1672 xfrm_garbage_collect(net);
1676 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1677 struct nlattr **attrs)
1679 struct net *net = sock_net(skb->sk);
1681 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1682 struct xfrm_audit audit_info;
1685 audit_info.loginuid = audit_get_loginuid(current);
1686 audit_info.sessionid = audit_get_sessionid(current);
1687 security_task_getsecid(current, &audit_info.secid);
1688 err = xfrm_state_flush(net, p->proto, &audit_info);
1690 if (err == -ESRCH) /* empty table */
1694 c.data.proto = p->proto;
1695 c.event = nlh->nlmsg_type;
1696 c.seq = nlh->nlmsg_seq;
1697 c.portid = nlh->nlmsg_pid;
1699 km_state_notify(NULL, &c);
1704 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1706 size_t replay_size = x->replay_esn ?
1707 xfrm_replay_state_esn_len(x->replay_esn) :
1708 sizeof(struct xfrm_replay_state);
1710 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1711 + nla_total_size(replay_size)
1712 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1713 + nla_total_size(sizeof(struct xfrm_mark))
1714 + nla_total_size(4) /* XFRM_AE_RTHR */
1715 + nla_total_size(4); /* XFRM_AE_ETHR */
1718 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1720 struct xfrm_aevent_id *id;
1721 struct nlmsghdr *nlh;
1724 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1728 id = nlmsg_data(nlh);
1729 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1730 id->sa_id.spi = x->id.spi;
1731 id->sa_id.family = x->props.family;
1732 id->sa_id.proto = x->id.proto;
1733 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1734 id->reqid = x->props.reqid;
1735 id->flags = c->data.aevent;
1737 if (x->replay_esn) {
1738 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1739 xfrm_replay_state_esn_len(x->replay_esn),
1742 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1747 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1751 if (id->flags & XFRM_AE_RTHR) {
1752 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1756 if (id->flags & XFRM_AE_ETHR) {
1757 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1758 x->replay_maxage * 10 / HZ);
1762 err = xfrm_mark_put(skb, &x->mark);
1766 return nlmsg_end(skb, nlh);
1769 nlmsg_cancel(skb, nlh);
1773 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1774 struct nlattr **attrs)
1776 struct net *net = sock_net(skb->sk);
1777 struct xfrm_state *x;
1778 struct sk_buff *r_skb;
1783 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1784 struct xfrm_usersa_id *id = &p->sa_id;
1786 mark = xfrm_mark_get(attrs, &m);
1788 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1792 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1793 if (r_skb == NULL) {
1799 * XXX: is this lock really needed - none of the other
1800 * gets lock (the concern is things getting updated
1801 * while we are still reading) - jhs
1803 spin_lock_bh(&x->lock);
1804 c.data.aevent = p->flags;
1805 c.seq = nlh->nlmsg_seq;
1806 c.portid = nlh->nlmsg_pid;
1808 if (build_aevent(r_skb, x, &c) < 0)
1810 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1811 spin_unlock_bh(&x->lock);
1816 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1817 struct nlattr **attrs)
1819 struct net *net = sock_net(skb->sk);
1820 struct xfrm_state *x;
1825 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1826 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1827 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1828 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1830 if (!lt && !rp && !re)
1833 /* pedantic mode - thou shalt sayeth replaceth */
1834 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1837 mark = xfrm_mark_get(attrs, &m);
1839 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1843 if (x->km.state != XFRM_STATE_VALID)
1846 err = xfrm_replay_verify_len(x->replay_esn, re);
1850 spin_lock_bh(&x->lock);
1851 xfrm_update_ae_params(x, attrs, 1);
1852 spin_unlock_bh(&x->lock);
1854 c.event = nlh->nlmsg_type;
1855 c.seq = nlh->nlmsg_seq;
1856 c.portid = nlh->nlmsg_pid;
1857 c.data.aevent = XFRM_AE_CU;
1858 km_state_notify(x, &c);
1865 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1866 struct nlattr **attrs)
1868 struct net *net = sock_net(skb->sk);
1870 u8 type = XFRM_POLICY_TYPE_MAIN;
1872 struct xfrm_audit audit_info;
1874 err = copy_from_user_policy_type(&type, attrs);
1878 audit_info.loginuid = audit_get_loginuid(current);
1879 audit_info.sessionid = audit_get_sessionid(current);
1880 security_task_getsecid(current, &audit_info.secid);
1881 err = xfrm_policy_flush(net, type, &audit_info);
1883 if (err == -ESRCH) /* empty table */
1889 c.event = nlh->nlmsg_type;
1890 c.seq = nlh->nlmsg_seq;
1891 c.portid = nlh->nlmsg_pid;
1893 km_policy_notify(NULL, 0, &c);
1897 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1898 struct nlattr **attrs)
1900 struct net *net = sock_net(skb->sk);
1901 struct xfrm_policy *xp;
1902 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1903 struct xfrm_userpolicy_info *p = &up->pol;
1904 u8 type = XFRM_POLICY_TYPE_MAIN;
1907 u32 mark = xfrm_mark_get(attrs, &m);
1909 err = copy_from_user_policy_type(&type, attrs);
1913 err = verify_policy_dir(p->dir);
1918 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1920 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1921 struct xfrm_sec_ctx *ctx;
1923 err = verify_sec_ctx_len(attrs);
1929 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1931 err = security_xfrm_policy_alloc(&ctx, uctx);
1935 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1936 &p->sel, ctx, 0, &err);
1937 security_xfrm_policy_free(ctx);
1942 if (unlikely(xp->walk.dead))
1947 kuid_t loginuid = audit_get_loginuid(current);
1948 unsigned int sessionid = audit_get_sessionid(current);
1951 security_task_getsecid(current, &sid);
1952 xfrm_policy_delete(xp, p->dir);
1953 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1956 // reset the timers here?
1957 WARN(1, "Dont know what to do with soft policy expire\n");
1959 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
1966 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1967 struct nlattr **attrs)
1969 struct net *net = sock_net(skb->sk);
1970 struct xfrm_state *x;
1972 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1973 struct xfrm_usersa_info *p = &ue->state;
1975 u32 mark = xfrm_mark_get(attrs, &m);
1977 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1983 spin_lock_bh(&x->lock);
1985 if (x->km.state != XFRM_STATE_VALID)
1987 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
1990 kuid_t loginuid = audit_get_loginuid(current);
1991 unsigned int sessionid = audit_get_sessionid(current);
1994 security_task_getsecid(current, &sid);
1995 __xfrm_state_delete(x);
1996 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
2000 spin_unlock_bh(&x->lock);
2005 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2006 struct nlattr **attrs)
2008 struct net *net = sock_net(skb->sk);
2009 struct xfrm_policy *xp;
2010 struct xfrm_user_tmpl *ut;
2012 struct nlattr *rt = attrs[XFRMA_TMPL];
2013 struct xfrm_mark mark;
2015 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2016 struct xfrm_state *x = xfrm_state_alloc(net);
2022 xfrm_mark_get(attrs, &mark);
2024 err = verify_newpolicy_info(&ua->policy);
2029 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2033 memcpy(&x->id, &ua->id, sizeof(ua->id));
2034 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2035 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2036 xp->mark.m = x->mark.m = mark.m;
2037 xp->mark.v = x->mark.v = mark.v;
2039 /* extract the templates and for each call km_key */
2040 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2041 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2042 memcpy(&x->id, &t->id, sizeof(x->id));
2043 x->props.mode = t->mode;
2044 x->props.reqid = t->reqid;
2045 x->props.family = ut->family;
2046 t->aalgos = ua->aalgos;
2047 t->ealgos = ua->ealgos;
2048 t->calgos = ua->calgos;
2049 err = km_query(x, t, xp);
2059 WARN(1, "BAD policy passed\n");
2066 #ifdef CONFIG_XFRM_MIGRATE
2067 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2068 struct xfrm_kmaddress *k,
2069 struct nlattr **attrs, int *num)
2071 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2072 struct xfrm_user_migrate *um;
2076 struct xfrm_user_kmaddress *uk;
2078 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2079 memcpy(&k->local, &uk->local, sizeof(k->local));
2080 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2081 k->family = uk->family;
2082 k->reserved = uk->reserved;
2086 num_migrate = nla_len(rt) / sizeof(*um);
2088 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2091 for (i = 0; i < num_migrate; i++, um++, ma++) {
2092 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2093 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2094 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2095 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2097 ma->proto = um->proto;
2098 ma->mode = um->mode;
2099 ma->reqid = um->reqid;
2101 ma->old_family = um->old_family;
2102 ma->new_family = um->new_family;
2109 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2110 struct nlattr **attrs)
2112 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2113 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2114 struct xfrm_kmaddress km, *kmp;
2118 struct net *net = sock_net(skb->sk);
2120 if (attrs[XFRMA_MIGRATE] == NULL)
2123 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2125 err = copy_from_user_policy_type(&type, attrs);
2129 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2136 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2141 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2142 struct nlattr **attrs)
2144 return -ENOPROTOOPT;
2148 #ifdef CONFIG_XFRM_MIGRATE
2149 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2151 struct xfrm_user_migrate um;
2153 memset(&um, 0, sizeof(um));
2154 um.proto = m->proto;
2156 um.reqid = m->reqid;
2157 um.old_family = m->old_family;
2158 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2159 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2160 um.new_family = m->new_family;
2161 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2162 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2164 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2167 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2169 struct xfrm_user_kmaddress uk;
2171 memset(&uk, 0, sizeof(uk));
2172 uk.family = k->family;
2173 uk.reserved = k->reserved;
2174 memcpy(&uk.local, &k->local, sizeof(uk.local));
2175 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2177 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2180 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2182 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2183 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2184 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2185 + userpolicy_type_attrsize();
2188 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2189 int num_migrate, const struct xfrm_kmaddress *k,
2190 const struct xfrm_selector *sel, u8 dir, u8 type)
2192 const struct xfrm_migrate *mp;
2193 struct xfrm_userpolicy_id *pol_id;
2194 struct nlmsghdr *nlh;
2197 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2201 pol_id = nlmsg_data(nlh);
2202 /* copy data from selector, dir, and type to the pol_id */
2203 memset(pol_id, 0, sizeof(*pol_id));
2204 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2208 err = copy_to_user_kmaddress(k, skb);
2212 err = copy_to_user_policy_type(type, skb);
2215 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2216 err = copy_to_user_migrate(mp, skb);
2221 return nlmsg_end(skb, nlh);
2224 nlmsg_cancel(skb, nlh);
2228 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2229 const struct xfrm_migrate *m, int num_migrate,
2230 const struct xfrm_kmaddress *k)
2232 struct net *net = &init_net;
2233 struct sk_buff *skb;
2235 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2240 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2243 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2246 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2247 const struct xfrm_migrate *m, int num_migrate,
2248 const struct xfrm_kmaddress *k)
2250 return -ENOPROTOOPT;
2254 #define XMSGSIZE(type) sizeof(struct type)
2256 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2257 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2258 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2259 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2260 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2261 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2262 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2263 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2264 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2265 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2266 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2267 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2268 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2269 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2270 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2271 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2272 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2273 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2274 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2275 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2276 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2281 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2282 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2283 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2284 [XFRMA_LASTUSED] = { .type = NLA_U64},
2285 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2286 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2287 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2288 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2289 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2290 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2291 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2292 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2293 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2294 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2295 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2296 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2297 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2298 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2299 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2300 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2301 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2302 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2303 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2304 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2305 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2308 static const struct xfrm_link {
2309 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2310 int (*dump)(struct sk_buff *, struct netlink_callback *);
2311 int (*done)(struct netlink_callback *);
2312 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2313 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2314 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2315 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2316 .dump = xfrm_dump_sa,
2317 .done = xfrm_dump_sa_done },
2318 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2319 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2320 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2321 .dump = xfrm_dump_policy,
2322 .done = xfrm_dump_policy_done },
2323 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2324 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2325 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2326 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2327 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2328 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2329 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2330 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2331 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2332 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2333 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2334 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2335 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2338 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2340 struct net *net = sock_net(skb->sk);
2341 struct nlattr *attrs[XFRMA_MAX+1];
2342 const struct xfrm_link *link;
2345 type = nlh->nlmsg_type;
2346 if (type > XFRM_MSG_MAX)
2349 type -= XFRM_MSG_BASE;
2350 link = &xfrm_dispatch[type];
2352 /* All operations require privileges, even GET */
2353 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2356 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2357 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2358 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2359 if (link->dump == NULL)
2363 struct netlink_dump_control c = {
2367 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2371 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2376 if (link->doit == NULL)
2379 return link->doit(skb, nlh, attrs);
2382 static void xfrm_netlink_rcv(struct sk_buff *skb)
2384 struct net *net = sock_net(skb->sk);
2386 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2387 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2388 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2391 static inline size_t xfrm_expire_msgsize(void)
2393 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2394 + nla_total_size(sizeof(struct xfrm_mark));
2397 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2399 struct xfrm_user_expire *ue;
2400 struct nlmsghdr *nlh;
2403 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2407 ue = nlmsg_data(nlh);
2408 copy_to_user_state(x, &ue->state);
2409 ue->hard = (c->data.hard != 0) ? 1 : 0;
2411 err = xfrm_mark_put(skb, &x->mark);
2415 return nlmsg_end(skb, nlh);
2418 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2420 struct net *net = xs_net(x);
2421 struct sk_buff *skb;
2423 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2427 if (build_expire(skb, x, c) < 0) {
2432 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2435 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2437 struct net *net = xs_net(x);
2438 struct sk_buff *skb;
2440 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2444 if (build_aevent(skb, x, c) < 0)
2447 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2450 static int xfrm_notify_sa_flush(const struct km_event *c)
2452 struct net *net = c->net;
2453 struct xfrm_usersa_flush *p;
2454 struct nlmsghdr *nlh;
2455 struct sk_buff *skb;
2456 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2458 skb = nlmsg_new(len, GFP_ATOMIC);
2462 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2468 p = nlmsg_data(nlh);
2469 p->proto = c->data.proto;
2471 nlmsg_end(skb, nlh);
2473 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2476 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2480 l += nla_total_size(aead_len(x->aead));
2482 l += nla_total_size(sizeof(struct xfrm_algo) +
2483 (x->aalg->alg_key_len + 7) / 8);
2484 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2487 l += nla_total_size(xfrm_alg_len(x->ealg));
2489 l += nla_total_size(sizeof(*x->calg));
2491 l += nla_total_size(sizeof(*x->encap));
2493 l += nla_total_size(sizeof(x->tfcpad));
2495 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2497 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2498 x->security->ctx_len);
2500 l += nla_total_size(sizeof(*x->coaddr));
2501 if (x->props.extra_flags)
2502 l += nla_total_size(sizeof(x->props.extra_flags));
2504 /* Must count x->lastused as it may become non-zero behind our back. */
2505 l += nla_total_size(sizeof(u64));
2510 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2512 struct net *net = xs_net(x);
2513 struct xfrm_usersa_info *p;
2514 struct xfrm_usersa_id *id;
2515 struct nlmsghdr *nlh;
2516 struct sk_buff *skb;
2517 int len = xfrm_sa_len(x);
2520 headlen = sizeof(*p);
2521 if (c->event == XFRM_MSG_DELSA) {
2522 len += nla_total_size(headlen);
2523 headlen = sizeof(*id);
2524 len += nla_total_size(sizeof(struct xfrm_mark));
2526 len += NLMSG_ALIGN(headlen);
2528 skb = nlmsg_new(len, GFP_ATOMIC);
2532 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2537 p = nlmsg_data(nlh);
2538 if (c->event == XFRM_MSG_DELSA) {
2539 struct nlattr *attr;
2541 id = nlmsg_data(nlh);
2542 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2543 id->spi = x->id.spi;
2544 id->family = x->props.family;
2545 id->proto = x->id.proto;
2547 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2554 err = copy_to_user_state_extra(x, p, skb);
2558 nlmsg_end(skb, nlh);
2560 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2567 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2571 case XFRM_MSG_EXPIRE:
2572 return xfrm_exp_state_notify(x, c);
2573 case XFRM_MSG_NEWAE:
2574 return xfrm_aevent_state_notify(x, c);
2575 case XFRM_MSG_DELSA:
2576 case XFRM_MSG_UPDSA:
2577 case XFRM_MSG_NEWSA:
2578 return xfrm_notify_sa(x, c);
2579 case XFRM_MSG_FLUSHSA:
2580 return xfrm_notify_sa_flush(c);
2582 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2591 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2592 struct xfrm_policy *xp)
2594 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2595 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2596 + nla_total_size(sizeof(struct xfrm_mark))
2597 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2598 + userpolicy_type_attrsize();
2601 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2602 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2604 __u32 seq = xfrm_get_acqseq();
2605 struct xfrm_user_acquire *ua;
2606 struct nlmsghdr *nlh;
2609 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2613 ua = nlmsg_data(nlh);
2614 memcpy(&ua->id, &x->id, sizeof(ua->id));
2615 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2616 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2617 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2618 ua->aalgos = xt->aalgos;
2619 ua->ealgos = xt->ealgos;
2620 ua->calgos = xt->calgos;
2621 ua->seq = x->km.seq = seq;
2623 err = copy_to_user_tmpl(xp, skb);
2625 err = copy_to_user_state_sec_ctx(x, skb);
2627 err = copy_to_user_policy_type(xp->type, skb);
2629 err = xfrm_mark_put(skb, &xp->mark);
2631 nlmsg_cancel(skb, nlh);
2635 return nlmsg_end(skb, nlh);
2638 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2639 struct xfrm_policy *xp)
2641 struct net *net = xs_net(x);
2642 struct sk_buff *skb;
2644 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2648 if (build_acquire(skb, x, xt, xp) < 0)
2651 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2654 /* User gives us xfrm_user_policy_info followed by an array of 0
2655 * or more templates.
2657 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2658 u8 *data, int len, int *dir)
2660 struct net *net = sock_net(sk);
2661 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2662 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2663 struct xfrm_policy *xp;
2666 switch (sk->sk_family) {
2668 if (opt != IP_XFRM_POLICY) {
2673 #if IS_ENABLED(CONFIG_IPV6)
2675 if (opt != IPV6_XFRM_POLICY) {
2688 if (len < sizeof(*p) ||
2689 verify_newpolicy_info(p))
2692 nr = ((len - sizeof(*p)) / sizeof(*ut));
2693 if (validate_tmpl(nr, ut, p->sel.family))
2696 if (p->dir > XFRM_POLICY_OUT)
2699 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2705 copy_from_user_policy(xp, p);
2706 xp->type = XFRM_POLICY_TYPE_MAIN;
2707 copy_templates(xp, ut, nr);
2714 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2716 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2717 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2718 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2719 + nla_total_size(sizeof(struct xfrm_mark))
2720 + userpolicy_type_attrsize();
2723 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2724 int dir, const struct km_event *c)
2726 struct xfrm_user_polexpire *upe;
2727 int hard = c->data.hard;
2728 struct nlmsghdr *nlh;
2731 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2735 upe = nlmsg_data(nlh);
2736 copy_to_user_policy(xp, &upe->pol, dir);
2737 err = copy_to_user_tmpl(xp, skb);
2739 err = copy_to_user_sec_ctx(xp, skb);
2741 err = copy_to_user_policy_type(xp->type, skb);
2743 err = xfrm_mark_put(skb, &xp->mark);
2745 nlmsg_cancel(skb, nlh);
2750 return nlmsg_end(skb, nlh);
2753 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2755 struct net *net = xp_net(xp);
2756 struct sk_buff *skb;
2758 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2762 if (build_polexpire(skb, xp, dir, c) < 0)
2765 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2768 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2770 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2771 struct net *net = xp_net(xp);
2772 struct xfrm_userpolicy_info *p;
2773 struct xfrm_userpolicy_id *id;
2774 struct nlmsghdr *nlh;
2775 struct sk_buff *skb;
2778 headlen = sizeof(*p);
2779 if (c->event == XFRM_MSG_DELPOLICY) {
2780 len += nla_total_size(headlen);
2781 headlen = sizeof(*id);
2783 len += userpolicy_type_attrsize();
2784 len += nla_total_size(sizeof(struct xfrm_mark));
2785 len += NLMSG_ALIGN(headlen);
2787 skb = nlmsg_new(len, GFP_ATOMIC);
2791 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2796 p = nlmsg_data(nlh);
2797 if (c->event == XFRM_MSG_DELPOLICY) {
2798 struct nlattr *attr;
2800 id = nlmsg_data(nlh);
2801 memset(id, 0, sizeof(*id));
2804 id->index = xp->index;
2806 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2808 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2816 copy_to_user_policy(xp, p, dir);
2817 err = copy_to_user_tmpl(xp, skb);
2819 err = copy_to_user_policy_type(xp->type, skb);
2821 err = xfrm_mark_put(skb, &xp->mark);
2825 nlmsg_end(skb, nlh);
2827 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2834 static int xfrm_notify_policy_flush(const struct km_event *c)
2836 struct net *net = c->net;
2837 struct nlmsghdr *nlh;
2838 struct sk_buff *skb;
2841 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2845 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2849 err = copy_to_user_policy_type(c->data.type, skb);
2853 nlmsg_end(skb, nlh);
2855 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2862 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2866 case XFRM_MSG_NEWPOLICY:
2867 case XFRM_MSG_UPDPOLICY:
2868 case XFRM_MSG_DELPOLICY:
2869 return xfrm_notify_policy(xp, dir, c);
2870 case XFRM_MSG_FLUSHPOLICY:
2871 return xfrm_notify_policy_flush(c);
2872 case XFRM_MSG_POLEXPIRE:
2873 return xfrm_exp_policy_notify(xp, dir, c);
2875 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2883 static inline size_t xfrm_report_msgsize(void)
2885 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2888 static int build_report(struct sk_buff *skb, u8 proto,
2889 struct xfrm_selector *sel, xfrm_address_t *addr)
2891 struct xfrm_user_report *ur;
2892 struct nlmsghdr *nlh;
2894 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2898 ur = nlmsg_data(nlh);
2900 memcpy(&ur->sel, sel, sizeof(ur->sel));
2903 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2905 nlmsg_cancel(skb, nlh);
2909 return nlmsg_end(skb, nlh);
2912 static int xfrm_send_report(struct net *net, u8 proto,
2913 struct xfrm_selector *sel, xfrm_address_t *addr)
2915 struct sk_buff *skb;
2917 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2921 if (build_report(skb, proto, sel, addr) < 0)
2924 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2927 static inline size_t xfrm_mapping_msgsize(void)
2929 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2932 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2933 xfrm_address_t *new_saddr, __be16 new_sport)
2935 struct xfrm_user_mapping *um;
2936 struct nlmsghdr *nlh;
2938 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2942 um = nlmsg_data(nlh);
2944 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2945 um->id.spi = x->id.spi;
2946 um->id.family = x->props.family;
2947 um->id.proto = x->id.proto;
2948 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2949 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2950 um->new_sport = new_sport;
2951 um->old_sport = x->encap->encap_sport;
2952 um->reqid = x->props.reqid;
2954 return nlmsg_end(skb, nlh);
2957 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2960 struct net *net = xs_net(x);
2961 struct sk_buff *skb;
2963 if (x->id.proto != IPPROTO_ESP)
2969 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2973 if (build_mapping(skb, x, ipaddr, sport) < 0)
2976 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2979 static struct xfrm_mgr netlink_mgr = {
2981 .notify = xfrm_send_state_notify,
2982 .acquire = xfrm_send_acquire,
2983 .compile_policy = xfrm_compile_policy,
2984 .notify_policy = xfrm_send_policy_notify,
2985 .report = xfrm_send_report,
2986 .migrate = xfrm_send_migrate,
2987 .new_mapping = xfrm_send_mapping,
2990 static int __net_init xfrm_user_net_init(struct net *net)
2993 struct netlink_kernel_cfg cfg = {
2994 .groups = XFRMNLGRP_MAX,
2995 .input = xfrm_netlink_rcv,
2998 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3001 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3002 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3006 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3009 list_for_each_entry(net, net_exit_list, exit_list)
3010 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3012 list_for_each_entry(net, net_exit_list, exit_list)
3013 netlink_kernel_release(net->xfrm.nlsk_stash);
3016 static struct pernet_operations xfrm_user_net_ops = {
3017 .init = xfrm_user_net_init,
3018 .exit_batch = xfrm_user_net_exit,
3021 static int __init xfrm_user_init(void)
3025 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3027 rv = register_pernet_subsys(&xfrm_user_net_ops);
3030 rv = xfrm_register_km(&netlink_mgr);
3032 unregister_pernet_subsys(&xfrm_user_net_ops);
3036 static void __exit xfrm_user_exit(void)
3038 xfrm_unregister_km(&netlink_mgr);
3039 unregister_pernet_subsys(&xfrm_user_net_ops);
3042 module_init(xfrm_user_init);
3043 module_exit(xfrm_user_exit);
3044 MODULE_LICENSE("GPL");
3045 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);