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 defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
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 defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
177 switch (p->id.proto) {
179 if ((!attrs[XFRMA_ALG_AUTH] &&
180 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
181 attrs[XFRMA_ALG_AEAD] ||
182 attrs[XFRMA_ALG_CRYPT] ||
183 attrs[XFRMA_ALG_COMP] ||
189 if (attrs[XFRMA_ALG_COMP])
191 if (!attrs[XFRMA_ALG_AUTH] &&
192 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
193 !attrs[XFRMA_ALG_CRYPT] &&
194 !attrs[XFRMA_ALG_AEAD])
196 if ((attrs[XFRMA_ALG_AUTH] ||
197 attrs[XFRMA_ALG_AUTH_TRUNC] ||
198 attrs[XFRMA_ALG_CRYPT]) &&
199 attrs[XFRMA_ALG_AEAD])
201 if (attrs[XFRMA_TFCPAD] &&
202 p->mode != XFRM_MODE_TUNNEL)
207 if (!attrs[XFRMA_ALG_COMP] ||
208 attrs[XFRMA_ALG_AEAD] ||
209 attrs[XFRMA_ALG_AUTH] ||
210 attrs[XFRMA_ALG_AUTH_TRUNC] ||
211 attrs[XFRMA_ALG_CRYPT] ||
216 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
217 case IPPROTO_DSTOPTS:
218 case IPPROTO_ROUTING:
219 if (attrs[XFRMA_ALG_COMP] ||
220 attrs[XFRMA_ALG_AUTH] ||
221 attrs[XFRMA_ALG_AUTH_TRUNC] ||
222 attrs[XFRMA_ALG_AEAD] ||
223 attrs[XFRMA_ALG_CRYPT] ||
224 attrs[XFRMA_ENCAP] ||
225 attrs[XFRMA_SEC_CTX] ||
226 attrs[XFRMA_TFCPAD] ||
227 !attrs[XFRMA_COADDR])
236 if ((err = verify_aead(attrs)))
238 if ((err = verify_auth_trunc(attrs)))
240 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
242 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
244 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
246 if ((err = verify_sec_ctx_len(attrs)))
248 if ((err = verify_replay(p, attrs)))
253 case XFRM_MODE_TRANSPORT:
254 case XFRM_MODE_TUNNEL:
255 case XFRM_MODE_ROUTEOPTIMIZATION:
269 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
270 struct xfrm_algo_desc *(*get_byname)(const char *, int),
273 struct xfrm_algo *p, *ualg;
274 struct xfrm_algo_desc *algo;
279 ualg = nla_data(rta);
281 algo = get_byname(ualg->alg_name, 1);
284 *props = algo->desc.sadb_alg_id;
286 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
290 strcpy(p->alg_name, algo->name);
295 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
298 struct xfrm_algo *ualg;
299 struct xfrm_algo_auth *p;
300 struct xfrm_algo_desc *algo;
305 ualg = nla_data(rta);
307 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
310 *props = algo->desc.sadb_alg_id;
312 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
316 strcpy(p->alg_name, algo->name);
317 p->alg_key_len = ualg->alg_key_len;
318 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
319 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
325 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
328 struct xfrm_algo_auth *p, *ualg;
329 struct xfrm_algo_desc *algo;
334 ualg = nla_data(rta);
336 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
339 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
342 *props = algo->desc.sadb_alg_id;
344 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
348 strcpy(p->alg_name, algo->name);
349 if (!p->alg_trunc_len)
350 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
356 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
359 struct xfrm_algo_aead *p, *ualg;
360 struct xfrm_algo_desc *algo;
365 ualg = nla_data(rta);
367 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
370 *props = algo->desc.sadb_alg_id;
372 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
376 strcpy(p->alg_name, algo->name);
381 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
384 struct xfrm_replay_state_esn *up;
387 if (!replay_esn || !rp)
391 ulen = xfrm_replay_state_esn_len(up);
393 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
399 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
400 struct xfrm_replay_state_esn **preplay_esn,
403 struct xfrm_replay_state_esn *p, *pp, *up;
410 klen = xfrm_replay_state_esn_len(up);
411 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
413 p = kzalloc(klen, GFP_KERNEL);
417 pp = kzalloc(klen, GFP_KERNEL);
424 memcpy(pp, up, ulen);
432 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
437 len += sizeof(struct xfrm_user_sec_ctx);
438 len += xfrm_ctx->ctx_len;
443 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
445 memcpy(&x->id, &p->id, sizeof(x->id));
446 memcpy(&x->sel, &p->sel, sizeof(x->sel));
447 memcpy(&x->lft, &p->lft, sizeof(x->lft));
448 x->props.mode = p->mode;
449 x->props.replay_window = p->replay_window;
450 x->props.reqid = p->reqid;
451 x->props.family = p->family;
452 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
453 x->props.flags = p->flags;
455 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
456 x->sel.family = p->family;
460 * someday when pfkey also has support, we could have the code
461 * somehow made shareable and move it to xfrm_state.c - JHS
464 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
467 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
468 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
469 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
470 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
471 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
474 struct xfrm_replay_state_esn *replay_esn;
475 replay_esn = nla_data(re);
476 memcpy(x->replay_esn, replay_esn,
477 xfrm_replay_state_esn_len(replay_esn));
478 memcpy(x->preplay_esn, replay_esn,
479 xfrm_replay_state_esn_len(replay_esn));
483 struct xfrm_replay_state *replay;
484 replay = nla_data(rp);
485 memcpy(&x->replay, replay, sizeof(*replay));
486 memcpy(&x->preplay, replay, sizeof(*replay));
490 struct xfrm_lifetime_cur *ltime;
491 ltime = nla_data(lt);
492 x->curlft.bytes = ltime->bytes;
493 x->curlft.packets = ltime->packets;
494 x->curlft.add_time = ltime->add_time;
495 x->curlft.use_time = ltime->use_time;
499 x->replay_maxage = nla_get_u32(et);
502 x->replay_maxdiff = nla_get_u32(rt);
505 static struct xfrm_state *xfrm_state_construct(struct net *net,
506 struct xfrm_usersa_info *p,
507 struct nlattr **attrs,
510 struct xfrm_state *x = xfrm_state_alloc(net);
516 copy_from_user_state(x, p);
518 if ((err = attach_aead(&x->aead, &x->props.ealgo,
519 attrs[XFRMA_ALG_AEAD])))
521 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
522 attrs[XFRMA_ALG_AUTH_TRUNC])))
524 if (!x->props.aalgo) {
525 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
526 attrs[XFRMA_ALG_AUTH])))
529 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
530 xfrm_ealg_get_byname,
531 attrs[XFRMA_ALG_CRYPT])))
533 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
534 xfrm_calg_get_byname,
535 attrs[XFRMA_ALG_COMP])))
538 if (attrs[XFRMA_ENCAP]) {
539 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
540 sizeof(*x->encap), GFP_KERNEL);
541 if (x->encap == NULL)
545 if (attrs[XFRMA_TFCPAD])
546 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
548 if (attrs[XFRMA_COADDR]) {
549 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
550 sizeof(*x->coaddr), GFP_KERNEL);
551 if (x->coaddr == NULL)
555 xfrm_mark_get(attrs, &x->mark);
557 err = __xfrm_init_state(x, false);
561 if (attrs[XFRMA_SEC_CTX]) {
562 err = security_xfrm_state_alloc(x,
563 nla_data(attrs[XFRMA_SEC_CTX]));
568 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
569 attrs[XFRMA_REPLAY_ESN_VAL])))
573 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
574 /* sysctl_xfrm_aevent_etime is in 100ms units */
575 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
577 if ((err = xfrm_init_replay(x)))
580 /* override default values from above */
581 xfrm_update_ae_params(x, attrs, 0);
586 x->km.state = XFRM_STATE_DEAD;
593 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
594 struct nlattr **attrs)
596 struct net *net = sock_net(skb->sk);
597 struct xfrm_usersa_info *p = nlmsg_data(nlh);
598 struct xfrm_state *x;
601 uid_t loginuid = audit_get_loginuid(current);
602 u32 sessionid = audit_get_sessionid(current);
605 err = verify_newsa_info(p, attrs);
609 x = xfrm_state_construct(net, p, attrs, &err);
614 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
615 err = xfrm_state_add(x);
617 err = xfrm_state_update(x);
619 security_task_getsecid(current, &sid);
620 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
623 x->km.state = XFRM_STATE_DEAD;
628 c.seq = nlh->nlmsg_seq;
629 c.pid = nlh->nlmsg_pid;
630 c.event = nlh->nlmsg_type;
632 km_state_notify(x, &c);
638 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
639 struct xfrm_usersa_id *p,
640 struct nlattr **attrs,
643 struct xfrm_state *x = NULL;
646 u32 mark = xfrm_mark_get(attrs, &m);
648 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
650 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
652 xfrm_address_t *saddr = NULL;
654 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
661 x = xfrm_state_lookup_byaddr(net, mark,
663 p->proto, p->family);
672 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
673 struct nlattr **attrs)
675 struct net *net = sock_net(skb->sk);
676 struct xfrm_state *x;
679 struct xfrm_usersa_id *p = nlmsg_data(nlh);
680 uid_t loginuid = audit_get_loginuid(current);
681 u32 sessionid = audit_get_sessionid(current);
684 x = xfrm_user_state_lookup(net, p, attrs, &err);
688 if ((err = security_xfrm_state_delete(x)) != 0)
691 if (xfrm_state_kern(x)) {
696 err = xfrm_state_delete(x);
701 c.seq = nlh->nlmsg_seq;
702 c.pid = nlh->nlmsg_pid;
703 c.event = nlh->nlmsg_type;
704 km_state_notify(x, &c);
707 security_task_getsecid(current, &sid);
708 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
713 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
715 memset(p, 0, sizeof(*p));
716 memcpy(&p->id, &x->id, sizeof(p->id));
717 memcpy(&p->sel, &x->sel, sizeof(p->sel));
718 memcpy(&p->lft, &x->lft, sizeof(p->lft));
719 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
720 memcpy(&p->stats, &x->stats, sizeof(p->stats));
721 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
722 p->mode = x->props.mode;
723 p->replay_window = x->props.replay_window;
724 p->reqid = x->props.reqid;
725 p->family = x->props.family;
726 p->flags = x->props.flags;
730 struct xfrm_dump_info {
731 struct sk_buff *in_skb;
732 struct sk_buff *out_skb;
737 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
739 struct xfrm_user_sec_ctx *uctx;
741 int ctx_size = sizeof(*uctx) + s->ctx_len;
743 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
747 uctx = nla_data(attr);
748 uctx->exttype = XFRMA_SEC_CTX;
749 uctx->len = ctx_size;
750 uctx->ctx_doi = s->ctx_doi;
751 uctx->ctx_alg = s->ctx_alg;
752 uctx->ctx_len = s->ctx_len;
753 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
758 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
760 struct xfrm_algo *algo;
763 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
764 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
768 algo = nla_data(nla);
769 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
770 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
771 algo->alg_key_len = auth->alg_key_len;
776 /* Don't change this without updating xfrm_sa_len! */
777 static int copy_to_user_state_extra(struct xfrm_state *x,
778 struct xfrm_usersa_info *p,
781 copy_to_user_state(x, p);
784 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
787 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
790 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
792 if (copy_to_user_auth(x->aalg, skb))
793 goto nla_put_failure;
795 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
796 xfrm_alg_auth_len(x->aalg), x->aalg);
799 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
801 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
804 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
807 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
809 if (xfrm_mark_put(skb, &x->mark))
810 goto nla_put_failure;
813 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
814 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
816 if (x->security && copy_sec_ctx(x->security, skb) < 0)
817 goto nla_put_failure;
825 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
827 struct xfrm_dump_info *sp = ptr;
828 struct sk_buff *in_skb = sp->in_skb;
829 struct sk_buff *skb = sp->out_skb;
830 struct xfrm_usersa_info *p;
831 struct nlmsghdr *nlh;
834 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
835 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
841 err = copy_to_user_state_extra(x, p, skb);
843 goto nla_put_failure;
849 nlmsg_cancel(skb, nlh);
853 static int xfrm_dump_sa_done(struct netlink_callback *cb)
855 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
856 xfrm_state_walk_done(walk);
860 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
862 struct net *net = sock_net(skb->sk);
863 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
864 struct xfrm_dump_info info;
866 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
867 sizeof(cb->args) - sizeof(cb->args[0]));
869 info.in_skb = cb->skb;
871 info.nlmsg_seq = cb->nlh->nlmsg_seq;
872 info.nlmsg_flags = NLM_F_MULTI;
876 xfrm_state_walk_init(walk, 0);
879 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
884 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
885 struct xfrm_state *x, u32 seq)
887 struct xfrm_dump_info info;
891 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
893 return ERR_PTR(-ENOMEM);
895 info.in_skb = in_skb;
897 info.nlmsg_seq = seq;
898 info.nlmsg_flags = 0;
900 err = dump_one_state(x, 0, &info);
909 static inline size_t xfrm_spdinfo_msgsize(void)
911 return NLMSG_ALIGN(4)
912 + nla_total_size(sizeof(struct xfrmu_spdinfo))
913 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
916 static int build_spdinfo(struct sk_buff *skb, struct net *net,
917 u32 pid, u32 seq, u32 flags)
919 struct xfrmk_spdinfo si;
920 struct xfrmu_spdinfo spc;
921 struct xfrmu_spdhinfo sph;
922 struct nlmsghdr *nlh;
925 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
926 if (nlh == NULL) /* shouldn't really happen ... */
931 xfrm_spd_getinfo(net, &si);
932 spc.incnt = si.incnt;
933 spc.outcnt = si.outcnt;
934 spc.fwdcnt = si.fwdcnt;
935 spc.inscnt = si.inscnt;
936 spc.outscnt = si.outscnt;
937 spc.fwdscnt = si.fwdscnt;
938 sph.spdhcnt = si.spdhcnt;
939 sph.spdhmcnt = si.spdhmcnt;
941 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
942 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
944 return nlmsg_end(skb, nlh);
947 nlmsg_cancel(skb, nlh);
951 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
952 struct nlattr **attrs)
954 struct net *net = sock_net(skb->sk);
955 struct sk_buff *r_skb;
956 u32 *flags = nlmsg_data(nlh);
957 u32 spid = NETLINK_CB(skb).pid;
958 u32 seq = nlh->nlmsg_seq;
960 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
964 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
967 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
970 static inline size_t xfrm_sadinfo_msgsize(void)
972 return NLMSG_ALIGN(4)
973 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
974 + nla_total_size(4); /* XFRMA_SAD_CNT */
977 static int build_sadinfo(struct sk_buff *skb, struct net *net,
978 u32 pid, u32 seq, u32 flags)
980 struct xfrmk_sadinfo si;
981 struct xfrmu_sadhinfo sh;
982 struct nlmsghdr *nlh;
985 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
986 if (nlh == NULL) /* shouldn't really happen ... */
991 xfrm_sad_getinfo(net, &si);
993 sh.sadhmcnt = si.sadhmcnt;
994 sh.sadhcnt = si.sadhcnt;
996 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
997 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
999 return nlmsg_end(skb, nlh);
1002 nlmsg_cancel(skb, nlh);
1006 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1007 struct nlattr **attrs)
1009 struct net *net = sock_net(skb->sk);
1010 struct sk_buff *r_skb;
1011 u32 *flags = nlmsg_data(nlh);
1012 u32 spid = NETLINK_CB(skb).pid;
1013 u32 seq = nlh->nlmsg_seq;
1015 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1019 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
1022 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
1025 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1026 struct nlattr **attrs)
1028 struct net *net = sock_net(skb->sk);
1029 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1030 struct xfrm_state *x;
1031 struct sk_buff *resp_skb;
1034 x = xfrm_user_state_lookup(net, p, attrs, &err);
1038 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1039 if (IS_ERR(resp_skb)) {
1040 err = PTR_ERR(resp_skb);
1042 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1049 static int verify_userspi_info(struct xfrm_userspi_info *p)
1051 switch (p->info.id.proto) {
1057 /* IPCOMP spi is 16-bits. */
1058 if (p->max >= 0x10000)
1066 if (p->min > p->max)
1072 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1073 struct nlattr **attrs)
1075 struct net *net = sock_net(skb->sk);
1076 struct xfrm_state *x;
1077 struct xfrm_userspi_info *p;
1078 struct sk_buff *resp_skb;
1079 xfrm_address_t *daddr;
1085 p = nlmsg_data(nlh);
1086 err = verify_userspi_info(p);
1090 family = p->info.family;
1091 daddr = &p->info.id.daddr;
1095 mark = xfrm_mark_get(attrs, &m);
1097 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1098 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1105 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1106 p->info.id.proto, daddr,
1113 err = xfrm_alloc_spi(x, p->min, p->max);
1117 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1118 if (IS_ERR(resp_skb)) {
1119 err = PTR_ERR(resp_skb);
1123 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1131 static int verify_policy_dir(u8 dir)
1134 case XFRM_POLICY_IN:
1135 case XFRM_POLICY_OUT:
1136 case XFRM_POLICY_FWD:
1146 static int verify_policy_type(u8 type)
1149 case XFRM_POLICY_TYPE_MAIN:
1150 #ifdef CONFIG_XFRM_SUB_POLICY
1151 case XFRM_POLICY_TYPE_SUB:
1162 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1165 case XFRM_SHARE_ANY:
1166 case XFRM_SHARE_SESSION:
1167 case XFRM_SHARE_USER:
1168 case XFRM_SHARE_UNIQUE:
1175 switch (p->action) {
1176 case XFRM_POLICY_ALLOW:
1177 case XFRM_POLICY_BLOCK:
1184 switch (p->sel.family) {
1189 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1192 return -EAFNOSUPPORT;
1199 return verify_policy_dir(p->dir);
1202 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1204 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1205 struct xfrm_user_sec_ctx *uctx;
1210 uctx = nla_data(rt);
1211 return security_xfrm_policy_alloc(&pol->security, uctx);
1214 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1220 for (i = 0; i < nr; i++, ut++) {
1221 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1223 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1224 memcpy(&t->saddr, &ut->saddr,
1225 sizeof(xfrm_address_t));
1226 t->reqid = ut->reqid;
1228 t->share = ut->share;
1229 t->optional = ut->optional;
1230 t->aalgos = ut->aalgos;
1231 t->ealgos = ut->ealgos;
1232 t->calgos = ut->calgos;
1233 /* If all masks are ~0, then we allow all algorithms. */
1234 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1235 t->encap_family = ut->family;
1239 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1243 if (nr > XFRM_MAX_DEPTH)
1246 for (i = 0; i < nr; i++) {
1247 /* We never validated the ut->family value, so many
1248 * applications simply leave it at zero. The check was
1249 * never made and ut->family was ignored because all
1250 * templates could be assumed to have the same family as
1251 * the policy itself. Now that we will have ipv4-in-ipv6
1252 * and ipv6-in-ipv4 tunnels, this is no longer true.
1255 ut[i].family = family;
1257 switch (ut[i].family) {
1260 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1272 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1274 struct nlattr *rt = attrs[XFRMA_TMPL];
1279 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1280 int nr = nla_len(rt) / sizeof(*utmpl);
1283 err = validate_tmpl(nr, utmpl, pol->family);
1287 copy_templates(pol, utmpl, nr);
1292 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1294 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1295 struct xfrm_userpolicy_type *upt;
1296 u8 type = XFRM_POLICY_TYPE_MAIN;
1304 err = verify_policy_type(type);
1312 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1314 xp->priority = p->priority;
1315 xp->index = p->index;
1316 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1317 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1318 xp->action = p->action;
1319 xp->flags = p->flags;
1320 xp->family = p->sel.family;
1321 /* XXX xp->share = p->share; */
1324 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1326 memset(p, 0, sizeof(*p));
1327 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1328 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1329 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1330 p->priority = xp->priority;
1331 p->index = xp->index;
1332 p->sel.family = xp->family;
1334 p->action = xp->action;
1335 p->flags = xp->flags;
1336 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1339 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1341 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1349 copy_from_user_policy(xp, p);
1351 err = copy_from_user_policy_type(&xp->type, attrs);
1355 if (!(err = copy_from_user_tmpl(xp, attrs)))
1356 err = copy_from_user_sec_ctx(xp, attrs);
1360 xfrm_mark_get(attrs, &xp->mark);
1366 xfrm_policy_destroy(xp);
1370 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1371 struct nlattr **attrs)
1373 struct net *net = sock_net(skb->sk);
1374 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1375 struct xfrm_policy *xp;
1379 uid_t loginuid = audit_get_loginuid(current);
1380 u32 sessionid = audit_get_sessionid(current);
1383 err = verify_newpolicy_info(p);
1386 err = verify_sec_ctx_len(attrs);
1390 xp = xfrm_policy_construct(net, p, attrs, &err);
1394 /* shouldn't excl be based on nlh flags??
1395 * Aha! this is anti-netlink really i.e more pfkey derived
1396 * in netlink excl is a flag and you wouldnt need
1397 * a type XFRM_MSG_UPDPOLICY - JHS */
1398 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1399 err = xfrm_policy_insert(p->dir, xp, excl);
1400 security_task_getsecid(current, &sid);
1401 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1404 security_xfrm_policy_free(xp->security);
1409 c.event = nlh->nlmsg_type;
1410 c.seq = nlh->nlmsg_seq;
1411 c.pid = nlh->nlmsg_pid;
1412 km_policy_notify(xp, p->dir, &c);
1419 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1421 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1424 if (xp->xfrm_nr == 0)
1427 for (i = 0; i < xp->xfrm_nr; i++) {
1428 struct xfrm_user_tmpl *up = &vec[i];
1429 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1431 memset(up, 0, sizeof(*up));
1432 memcpy(&up->id, &kp->id, sizeof(up->id));
1433 up->family = kp->encap_family;
1434 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1435 up->reqid = kp->reqid;
1436 up->mode = kp->mode;
1437 up->share = kp->share;
1438 up->optional = kp->optional;
1439 up->aalgos = kp->aalgos;
1440 up->ealgos = kp->ealgos;
1441 up->calgos = kp->calgos;
1444 return nla_put(skb, XFRMA_TMPL,
1445 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1448 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1451 return copy_sec_ctx(x->security, skb);
1456 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1459 return copy_sec_ctx(xp->security, skb);
1463 static inline size_t userpolicy_type_attrsize(void)
1465 #ifdef CONFIG_XFRM_SUB_POLICY
1466 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1472 #ifdef CONFIG_XFRM_SUB_POLICY
1473 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1475 struct xfrm_userpolicy_type upt = {
1479 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1483 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1489 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1491 struct xfrm_dump_info *sp = ptr;
1492 struct xfrm_userpolicy_info *p;
1493 struct sk_buff *in_skb = sp->in_skb;
1494 struct sk_buff *skb = sp->out_skb;
1495 struct nlmsghdr *nlh;
1497 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1498 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1502 p = nlmsg_data(nlh);
1503 copy_to_user_policy(xp, p, dir);
1504 if (copy_to_user_tmpl(xp, skb) < 0)
1506 if (copy_to_user_sec_ctx(xp, skb))
1508 if (copy_to_user_policy_type(xp->type, skb) < 0)
1510 if (xfrm_mark_put(skb, &xp->mark))
1511 goto nla_put_failure;
1513 nlmsg_end(skb, nlh);
1518 nlmsg_cancel(skb, nlh);
1522 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1524 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1526 xfrm_policy_walk_done(walk);
1530 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1532 struct net *net = sock_net(skb->sk);
1533 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1534 struct xfrm_dump_info info;
1536 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1537 sizeof(cb->args) - sizeof(cb->args[0]));
1539 info.in_skb = cb->skb;
1541 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1542 info.nlmsg_flags = NLM_F_MULTI;
1546 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1549 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1554 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1555 struct xfrm_policy *xp,
1558 struct xfrm_dump_info info;
1559 struct sk_buff *skb;
1562 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1564 return ERR_PTR(-ENOMEM);
1566 info.in_skb = in_skb;
1568 info.nlmsg_seq = seq;
1569 info.nlmsg_flags = 0;
1571 err = dump_one_policy(xp, dir, 0, &info);
1574 return ERR_PTR(err);
1580 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1581 struct nlattr **attrs)
1583 struct net *net = sock_net(skb->sk);
1584 struct xfrm_policy *xp;
1585 struct xfrm_userpolicy_id *p;
1586 u8 type = XFRM_POLICY_TYPE_MAIN;
1591 u32 mark = xfrm_mark_get(attrs, &m);
1593 p = nlmsg_data(nlh);
1594 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1596 err = copy_from_user_policy_type(&type, attrs);
1600 err = verify_policy_dir(p->dir);
1605 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1607 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1608 struct xfrm_sec_ctx *ctx;
1610 err = verify_sec_ctx_len(attrs);
1616 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1618 err = security_xfrm_policy_alloc(&ctx, uctx);
1622 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1624 security_xfrm_policy_free(ctx);
1630 struct sk_buff *resp_skb;
1632 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1633 if (IS_ERR(resp_skb)) {
1634 err = PTR_ERR(resp_skb);
1636 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1637 NETLINK_CB(skb).pid);
1640 uid_t loginuid = audit_get_loginuid(current);
1641 u32 sessionid = audit_get_sessionid(current);
1644 security_task_getsecid(current, &sid);
1645 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1651 c.data.byid = p->index;
1652 c.event = nlh->nlmsg_type;
1653 c.seq = nlh->nlmsg_seq;
1654 c.pid = nlh->nlmsg_pid;
1655 km_policy_notify(xp, p->dir, &c);
1663 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1664 struct nlattr **attrs)
1666 struct net *net = sock_net(skb->sk);
1668 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1669 struct xfrm_audit audit_info;
1672 audit_info.loginuid = audit_get_loginuid(current);
1673 audit_info.sessionid = audit_get_sessionid(current);
1674 security_task_getsecid(current, &audit_info.secid);
1675 err = xfrm_state_flush(net, p->proto, &audit_info);
1677 if (err == -ESRCH) /* empty table */
1681 c.data.proto = p->proto;
1682 c.event = nlh->nlmsg_type;
1683 c.seq = nlh->nlmsg_seq;
1684 c.pid = nlh->nlmsg_pid;
1686 km_state_notify(NULL, &c);
1691 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1693 size_t replay_size = x->replay_esn ?
1694 xfrm_replay_state_esn_len(x->replay_esn) :
1695 sizeof(struct xfrm_replay_state);
1697 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1698 + nla_total_size(replay_size)
1699 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1700 + nla_total_size(sizeof(struct xfrm_mark))
1701 + nla_total_size(4) /* XFRM_AE_RTHR */
1702 + nla_total_size(4); /* XFRM_AE_ETHR */
1705 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1707 struct xfrm_aevent_id *id;
1708 struct nlmsghdr *nlh;
1710 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1714 id = nlmsg_data(nlh);
1715 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1716 id->sa_id.spi = x->id.spi;
1717 id->sa_id.family = x->props.family;
1718 id->sa_id.proto = x->id.proto;
1719 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1720 id->reqid = x->props.reqid;
1721 id->flags = c->data.aevent;
1724 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1725 xfrm_replay_state_esn_len(x->replay_esn),
1728 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1730 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1732 if (id->flags & XFRM_AE_RTHR)
1733 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1735 if (id->flags & XFRM_AE_ETHR)
1736 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1737 x->replay_maxage * 10 / HZ);
1739 if (xfrm_mark_put(skb, &x->mark))
1740 goto nla_put_failure;
1742 return nlmsg_end(skb, nlh);
1745 nlmsg_cancel(skb, nlh);
1749 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1750 struct nlattr **attrs)
1752 struct net *net = sock_net(skb->sk);
1753 struct xfrm_state *x;
1754 struct sk_buff *r_skb;
1759 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1760 struct xfrm_usersa_id *id = &p->sa_id;
1762 mark = xfrm_mark_get(attrs, &m);
1764 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1768 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1769 if (r_skb == NULL) {
1775 * XXX: is this lock really needed - none of the other
1776 * gets lock (the concern is things getting updated
1777 * while we are still reading) - jhs
1779 spin_lock_bh(&x->lock);
1780 c.data.aevent = p->flags;
1781 c.seq = nlh->nlmsg_seq;
1782 c.pid = nlh->nlmsg_pid;
1784 if (build_aevent(r_skb, x, &c) < 0)
1786 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1787 spin_unlock_bh(&x->lock);
1792 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1793 struct nlattr **attrs)
1795 struct net *net = sock_net(skb->sk);
1796 struct xfrm_state *x;
1801 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1802 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1803 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1804 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1806 if (!lt && !rp && !re)
1809 /* pedantic mode - thou shalt sayeth replaceth */
1810 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1813 mark = xfrm_mark_get(attrs, &m);
1815 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1819 if (x->km.state != XFRM_STATE_VALID)
1822 err = xfrm_replay_verify_len(x->replay_esn, rp);
1826 spin_lock_bh(&x->lock);
1827 xfrm_update_ae_params(x, attrs, 1);
1828 spin_unlock_bh(&x->lock);
1830 c.event = nlh->nlmsg_type;
1831 c.seq = nlh->nlmsg_seq;
1832 c.pid = nlh->nlmsg_pid;
1833 c.data.aevent = XFRM_AE_CU;
1834 km_state_notify(x, &c);
1841 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1842 struct nlattr **attrs)
1844 struct net *net = sock_net(skb->sk);
1846 u8 type = XFRM_POLICY_TYPE_MAIN;
1848 struct xfrm_audit audit_info;
1850 err = copy_from_user_policy_type(&type, attrs);
1854 audit_info.loginuid = audit_get_loginuid(current);
1855 audit_info.sessionid = audit_get_sessionid(current);
1856 security_task_getsecid(current, &audit_info.secid);
1857 err = xfrm_policy_flush(net, type, &audit_info);
1859 if (err == -ESRCH) /* empty table */
1865 c.event = nlh->nlmsg_type;
1866 c.seq = nlh->nlmsg_seq;
1867 c.pid = nlh->nlmsg_pid;
1869 km_policy_notify(NULL, 0, &c);
1873 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1874 struct nlattr **attrs)
1876 struct net *net = sock_net(skb->sk);
1877 struct xfrm_policy *xp;
1878 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1879 struct xfrm_userpolicy_info *p = &up->pol;
1880 u8 type = XFRM_POLICY_TYPE_MAIN;
1883 u32 mark = xfrm_mark_get(attrs, &m);
1885 err = copy_from_user_policy_type(&type, attrs);
1889 err = verify_policy_dir(p->dir);
1894 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1896 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1897 struct xfrm_sec_ctx *ctx;
1899 err = verify_sec_ctx_len(attrs);
1905 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1907 err = security_xfrm_policy_alloc(&ctx, uctx);
1911 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1912 &p->sel, ctx, 0, &err);
1913 security_xfrm_policy_free(ctx);
1918 if (unlikely(xp->walk.dead))
1923 uid_t loginuid = audit_get_loginuid(current);
1924 u32 sessionid = audit_get_sessionid(current);
1927 security_task_getsecid(current, &sid);
1928 xfrm_policy_delete(xp, p->dir);
1929 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1932 // reset the timers here?
1933 WARN(1, "Dont know what to do with soft policy expire\n");
1935 km_policy_expired(xp, p->dir, up->hard, current->pid);
1942 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1943 struct nlattr **attrs)
1945 struct net *net = sock_net(skb->sk);
1946 struct xfrm_state *x;
1948 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1949 struct xfrm_usersa_info *p = &ue->state;
1951 u32 mark = xfrm_mark_get(attrs, &m);
1953 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1959 spin_lock_bh(&x->lock);
1961 if (x->km.state != XFRM_STATE_VALID)
1963 km_state_expired(x, ue->hard, current->pid);
1966 uid_t loginuid = audit_get_loginuid(current);
1967 u32 sessionid = audit_get_sessionid(current);
1970 security_task_getsecid(current, &sid);
1971 __xfrm_state_delete(x);
1972 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1976 spin_unlock_bh(&x->lock);
1981 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1982 struct nlattr **attrs)
1984 struct net *net = sock_net(skb->sk);
1985 struct xfrm_policy *xp;
1986 struct xfrm_user_tmpl *ut;
1988 struct nlattr *rt = attrs[XFRMA_TMPL];
1989 struct xfrm_mark mark;
1991 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1992 struct xfrm_state *x = xfrm_state_alloc(net);
1998 xfrm_mark_get(attrs, &mark);
2000 err = verify_newpolicy_info(&ua->policy);
2005 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2009 memcpy(&x->id, &ua->id, sizeof(ua->id));
2010 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2011 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2012 xp->mark.m = x->mark.m = mark.m;
2013 xp->mark.v = x->mark.v = mark.v;
2015 /* extract the templates and for each call km_key */
2016 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2017 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2018 memcpy(&x->id, &t->id, sizeof(x->id));
2019 x->props.mode = t->mode;
2020 x->props.reqid = t->reqid;
2021 x->props.family = ut->family;
2022 t->aalgos = ua->aalgos;
2023 t->ealgos = ua->ealgos;
2024 t->calgos = ua->calgos;
2025 err = km_query(x, t, xp);
2035 WARN(1, "BAD policy passed\n");
2042 #ifdef CONFIG_XFRM_MIGRATE
2043 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2044 struct xfrm_kmaddress *k,
2045 struct nlattr **attrs, int *num)
2047 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2048 struct xfrm_user_migrate *um;
2052 struct xfrm_user_kmaddress *uk;
2054 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2055 memcpy(&k->local, &uk->local, sizeof(k->local));
2056 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2057 k->family = uk->family;
2058 k->reserved = uk->reserved;
2062 num_migrate = nla_len(rt) / sizeof(*um);
2064 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2067 for (i = 0; i < num_migrate; i++, um++, ma++) {
2068 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2069 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2070 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2071 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2073 ma->proto = um->proto;
2074 ma->mode = um->mode;
2075 ma->reqid = um->reqid;
2077 ma->old_family = um->old_family;
2078 ma->new_family = um->new_family;
2085 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2086 struct nlattr **attrs)
2088 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2089 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2090 struct xfrm_kmaddress km, *kmp;
2095 if (attrs[XFRMA_MIGRATE] == NULL)
2098 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2100 err = copy_from_user_policy_type(&type, attrs);
2104 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2111 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2116 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2117 struct nlattr **attrs)
2119 return -ENOPROTOOPT;
2123 #ifdef CONFIG_XFRM_MIGRATE
2124 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2126 struct xfrm_user_migrate um;
2128 memset(&um, 0, sizeof(um));
2129 um.proto = m->proto;
2131 um.reqid = m->reqid;
2132 um.old_family = m->old_family;
2133 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2134 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2135 um.new_family = m->new_family;
2136 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2137 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2139 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2142 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2144 struct xfrm_user_kmaddress uk;
2146 memset(&uk, 0, sizeof(uk));
2147 uk.family = k->family;
2148 uk.reserved = k->reserved;
2149 memcpy(&uk.local, &k->local, sizeof(uk.local));
2150 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2152 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2155 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2157 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2158 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2159 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2160 + userpolicy_type_attrsize();
2163 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2164 int num_migrate, const struct xfrm_kmaddress *k,
2165 const struct xfrm_selector *sel, u8 dir, u8 type)
2167 const struct xfrm_migrate *mp;
2168 struct xfrm_userpolicy_id *pol_id;
2169 struct nlmsghdr *nlh;
2172 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2176 pol_id = nlmsg_data(nlh);
2177 /* copy data from selector, dir, and type to the pol_id */
2178 memset(pol_id, 0, sizeof(*pol_id));
2179 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2182 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2185 if (copy_to_user_policy_type(type, skb) < 0)
2188 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2189 if (copy_to_user_migrate(mp, skb) < 0)
2193 return nlmsg_end(skb, nlh);
2195 nlmsg_cancel(skb, nlh);
2199 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2200 const struct xfrm_migrate *m, int num_migrate,
2201 const struct xfrm_kmaddress *k)
2203 struct net *net = &init_net;
2204 struct sk_buff *skb;
2206 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2211 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2214 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2217 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2218 const struct xfrm_migrate *m, int num_migrate,
2219 const struct xfrm_kmaddress *k)
2221 return -ENOPROTOOPT;
2225 #define XMSGSIZE(type) sizeof(struct type)
2227 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2228 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2229 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2230 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2231 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2232 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2233 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2234 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2235 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2236 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2237 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2238 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2239 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2240 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2241 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2242 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2243 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2244 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2245 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2246 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2247 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2252 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2253 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2254 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2255 [XFRMA_LASTUSED] = { .type = NLA_U64},
2256 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2257 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2258 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2259 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2260 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2261 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2262 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2263 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2264 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2265 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2266 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2267 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2268 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2269 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2270 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2271 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2272 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2273 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2274 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2275 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2278 static struct xfrm_link {
2279 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2280 int (*dump)(struct sk_buff *, struct netlink_callback *);
2281 int (*done)(struct netlink_callback *);
2282 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2283 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2284 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2285 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2286 .dump = xfrm_dump_sa,
2287 .done = xfrm_dump_sa_done },
2288 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2289 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2290 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2291 .dump = xfrm_dump_policy,
2292 .done = xfrm_dump_policy_done },
2293 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2294 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2295 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2296 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2297 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2298 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2299 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2300 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2301 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2302 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2303 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2304 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2305 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2308 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2310 struct net *net = sock_net(skb->sk);
2311 struct nlattr *attrs[XFRMA_MAX+1];
2312 struct xfrm_link *link;
2315 type = nlh->nlmsg_type;
2316 if (type > XFRM_MSG_MAX)
2319 type -= XFRM_MSG_BASE;
2320 link = &xfrm_dispatch[type];
2322 /* All operations require privileges, even GET */
2323 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2326 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2327 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2328 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2329 if (link->dump == NULL)
2332 return netlink_dump_start(net->xfrm.nlsk, skb, nlh,
2333 link->dump, link->done, 0);
2336 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2341 if (link->doit == NULL)
2344 return link->doit(skb, nlh, attrs);
2347 static void xfrm_netlink_rcv(struct sk_buff *skb)
2349 mutex_lock(&xfrm_cfg_mutex);
2350 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2351 mutex_unlock(&xfrm_cfg_mutex);
2354 static inline size_t xfrm_expire_msgsize(void)
2356 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2357 + nla_total_size(sizeof(struct xfrm_mark));
2360 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2362 struct xfrm_user_expire *ue;
2363 struct nlmsghdr *nlh;
2365 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2369 ue = nlmsg_data(nlh);
2370 copy_to_user_state(x, &ue->state);
2371 ue->hard = (c->data.hard != 0) ? 1 : 0;
2373 if (xfrm_mark_put(skb, &x->mark))
2374 goto nla_put_failure;
2376 return nlmsg_end(skb, nlh);
2382 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2384 struct net *net = xs_net(x);
2385 struct sk_buff *skb;
2387 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2391 if (build_expire(skb, x, c) < 0) {
2396 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2399 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2401 struct net *net = xs_net(x);
2402 struct sk_buff *skb;
2404 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2408 if (build_aevent(skb, x, c) < 0)
2411 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2414 static int xfrm_notify_sa_flush(const struct km_event *c)
2416 struct net *net = c->net;
2417 struct xfrm_usersa_flush *p;
2418 struct nlmsghdr *nlh;
2419 struct sk_buff *skb;
2420 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2422 skb = nlmsg_new(len, GFP_ATOMIC);
2426 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2432 p = nlmsg_data(nlh);
2433 p->proto = c->data.proto;
2435 nlmsg_end(skb, nlh);
2437 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2440 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2444 l += nla_total_size(aead_len(x->aead));
2446 l += nla_total_size(sizeof(struct xfrm_algo) +
2447 (x->aalg->alg_key_len + 7) / 8);
2448 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2451 l += nla_total_size(xfrm_alg_len(x->ealg));
2453 l += nla_total_size(sizeof(*x->calg));
2455 l += nla_total_size(sizeof(*x->encap));
2457 l += nla_total_size(sizeof(x->tfcpad));
2459 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2461 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2462 x->security->ctx_len);
2464 l += nla_total_size(sizeof(*x->coaddr));
2466 /* Must count x->lastused as it may become non-zero behind our back. */
2467 l += nla_total_size(sizeof(u64));
2472 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2474 struct net *net = xs_net(x);
2475 struct xfrm_usersa_info *p;
2476 struct xfrm_usersa_id *id;
2477 struct nlmsghdr *nlh;
2478 struct sk_buff *skb;
2479 int len = xfrm_sa_len(x);
2482 headlen = sizeof(*p);
2483 if (c->event == XFRM_MSG_DELSA) {
2484 len += nla_total_size(headlen);
2485 headlen = sizeof(*id);
2486 len += nla_total_size(sizeof(struct xfrm_mark));
2488 len += NLMSG_ALIGN(headlen);
2490 skb = nlmsg_new(len, GFP_ATOMIC);
2494 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2496 goto nla_put_failure;
2498 p = nlmsg_data(nlh);
2499 if (c->event == XFRM_MSG_DELSA) {
2500 struct nlattr *attr;
2502 id = nlmsg_data(nlh);
2503 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2504 id->spi = x->id.spi;
2505 id->family = x->props.family;
2506 id->proto = x->id.proto;
2508 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2510 goto nla_put_failure;
2515 if (copy_to_user_state_extra(x, p, skb))
2516 goto nla_put_failure;
2518 nlmsg_end(skb, nlh);
2520 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2523 /* Somebody screwed up with xfrm_sa_len! */
2529 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2533 case XFRM_MSG_EXPIRE:
2534 return xfrm_exp_state_notify(x, c);
2535 case XFRM_MSG_NEWAE:
2536 return xfrm_aevent_state_notify(x, c);
2537 case XFRM_MSG_DELSA:
2538 case XFRM_MSG_UPDSA:
2539 case XFRM_MSG_NEWSA:
2540 return xfrm_notify_sa(x, c);
2541 case XFRM_MSG_FLUSHSA:
2542 return xfrm_notify_sa_flush(c);
2544 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2553 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2554 struct xfrm_policy *xp)
2556 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2557 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2558 + nla_total_size(sizeof(struct xfrm_mark))
2559 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2560 + userpolicy_type_attrsize();
2563 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2564 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2567 struct xfrm_user_acquire *ua;
2568 struct nlmsghdr *nlh;
2569 __u32 seq = xfrm_get_acqseq();
2571 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2575 ua = nlmsg_data(nlh);
2576 memcpy(&ua->id, &x->id, sizeof(ua->id));
2577 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2578 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2579 copy_to_user_policy(xp, &ua->policy, dir);
2580 ua->aalgos = xt->aalgos;
2581 ua->ealgos = xt->ealgos;
2582 ua->calgos = xt->calgos;
2583 ua->seq = x->km.seq = seq;
2585 if (copy_to_user_tmpl(xp, skb) < 0)
2587 if (copy_to_user_state_sec_ctx(x, skb))
2589 if (copy_to_user_policy_type(xp->type, skb) < 0)
2591 if (xfrm_mark_put(skb, &xp->mark))
2592 goto nla_put_failure;
2594 return nlmsg_end(skb, nlh);
2598 nlmsg_cancel(skb, nlh);
2602 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2603 struct xfrm_policy *xp, int dir)
2605 struct net *net = xs_net(x);
2606 struct sk_buff *skb;
2608 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2612 if (build_acquire(skb, x, xt, xp, dir) < 0)
2615 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2618 /* User gives us xfrm_user_policy_info followed by an array of 0
2619 * or more templates.
2621 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2622 u8 *data, int len, int *dir)
2624 struct net *net = sock_net(sk);
2625 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2626 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2627 struct xfrm_policy *xp;
2630 switch (sk->sk_family) {
2632 if (opt != IP_XFRM_POLICY) {
2637 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2639 if (opt != IPV6_XFRM_POLICY) {
2652 if (len < sizeof(*p) ||
2653 verify_newpolicy_info(p))
2656 nr = ((len - sizeof(*p)) / sizeof(*ut));
2657 if (validate_tmpl(nr, ut, p->sel.family))
2660 if (p->dir > XFRM_POLICY_OUT)
2663 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2669 copy_from_user_policy(xp, p);
2670 xp->type = XFRM_POLICY_TYPE_MAIN;
2671 copy_templates(xp, ut, nr);
2678 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2680 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2681 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2682 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2683 + nla_total_size(sizeof(struct xfrm_mark))
2684 + userpolicy_type_attrsize();
2687 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2688 int dir, const struct km_event *c)
2690 struct xfrm_user_polexpire *upe;
2691 struct nlmsghdr *nlh;
2692 int hard = c->data.hard;
2694 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2698 upe = nlmsg_data(nlh);
2699 copy_to_user_policy(xp, &upe->pol, dir);
2700 if (copy_to_user_tmpl(xp, skb) < 0)
2702 if (copy_to_user_sec_ctx(xp, skb))
2704 if (copy_to_user_policy_type(xp->type, skb) < 0)
2706 if (xfrm_mark_put(skb, &xp->mark))
2707 goto nla_put_failure;
2710 return nlmsg_end(skb, nlh);
2714 nlmsg_cancel(skb, nlh);
2718 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2720 struct net *net = xp_net(xp);
2721 struct sk_buff *skb;
2723 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2727 if (build_polexpire(skb, xp, dir, c) < 0)
2730 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2733 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2735 struct net *net = xp_net(xp);
2736 struct xfrm_userpolicy_info *p;
2737 struct xfrm_userpolicy_id *id;
2738 struct nlmsghdr *nlh;
2739 struct sk_buff *skb;
2740 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2743 headlen = sizeof(*p);
2744 if (c->event == XFRM_MSG_DELPOLICY) {
2745 len += nla_total_size(headlen);
2746 headlen = sizeof(*id);
2748 len += userpolicy_type_attrsize();
2749 len += nla_total_size(sizeof(struct xfrm_mark));
2750 len += NLMSG_ALIGN(headlen);
2752 skb = nlmsg_new(len, GFP_ATOMIC);
2756 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2760 p = nlmsg_data(nlh);
2761 if (c->event == XFRM_MSG_DELPOLICY) {
2762 struct nlattr *attr;
2764 id = nlmsg_data(nlh);
2765 memset(id, 0, sizeof(*id));
2768 id->index = xp->index;
2770 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2772 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2779 copy_to_user_policy(xp, p, dir);
2780 if (copy_to_user_tmpl(xp, skb) < 0)
2782 if (copy_to_user_policy_type(xp->type, skb) < 0)
2785 if (xfrm_mark_put(skb, &xp->mark))
2786 goto nla_put_failure;
2788 nlmsg_end(skb, nlh);
2790 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2798 static int xfrm_notify_policy_flush(const struct km_event *c)
2800 struct net *net = c->net;
2801 struct nlmsghdr *nlh;
2802 struct sk_buff *skb;
2804 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2808 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2811 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2814 nlmsg_end(skb, nlh);
2816 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2823 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2827 case XFRM_MSG_NEWPOLICY:
2828 case XFRM_MSG_UPDPOLICY:
2829 case XFRM_MSG_DELPOLICY:
2830 return xfrm_notify_policy(xp, dir, c);
2831 case XFRM_MSG_FLUSHPOLICY:
2832 return xfrm_notify_policy_flush(c);
2833 case XFRM_MSG_POLEXPIRE:
2834 return xfrm_exp_policy_notify(xp, dir, c);
2836 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2844 static inline size_t xfrm_report_msgsize(void)
2846 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2849 static int build_report(struct sk_buff *skb, u8 proto,
2850 struct xfrm_selector *sel, xfrm_address_t *addr)
2852 struct xfrm_user_report *ur;
2853 struct nlmsghdr *nlh;
2855 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2859 ur = nlmsg_data(nlh);
2861 memcpy(&ur->sel, sel, sizeof(ur->sel));
2864 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2866 return nlmsg_end(skb, nlh);
2869 nlmsg_cancel(skb, nlh);
2873 static int xfrm_send_report(struct net *net, u8 proto,
2874 struct xfrm_selector *sel, xfrm_address_t *addr)
2876 struct sk_buff *skb;
2878 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2882 if (build_report(skb, proto, sel, addr) < 0)
2885 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2888 static inline size_t xfrm_mapping_msgsize(void)
2890 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2893 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2894 xfrm_address_t *new_saddr, __be16 new_sport)
2896 struct xfrm_user_mapping *um;
2897 struct nlmsghdr *nlh;
2899 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2903 um = nlmsg_data(nlh);
2905 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2906 um->id.spi = x->id.spi;
2907 um->id.family = x->props.family;
2908 um->id.proto = x->id.proto;
2909 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2910 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2911 um->new_sport = new_sport;
2912 um->old_sport = x->encap->encap_sport;
2913 um->reqid = x->props.reqid;
2915 return nlmsg_end(skb, nlh);
2918 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2921 struct net *net = xs_net(x);
2922 struct sk_buff *skb;
2924 if (x->id.proto != IPPROTO_ESP)
2930 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2934 if (build_mapping(skb, x, ipaddr, sport) < 0)
2937 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2940 static struct xfrm_mgr netlink_mgr = {
2942 .notify = xfrm_send_state_notify,
2943 .acquire = xfrm_send_acquire,
2944 .compile_policy = xfrm_compile_policy,
2945 .notify_policy = xfrm_send_policy_notify,
2946 .report = xfrm_send_report,
2947 .migrate = xfrm_send_migrate,
2948 .new_mapping = xfrm_send_mapping,
2951 static int __net_init xfrm_user_net_init(struct net *net)
2955 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2956 xfrm_netlink_rcv, NULL, THIS_MODULE);
2959 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2960 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2964 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2967 list_for_each_entry(net, net_exit_list, exit_list)
2968 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
2970 list_for_each_entry(net, net_exit_list, exit_list)
2971 netlink_kernel_release(net->xfrm.nlsk_stash);
2974 static struct pernet_operations xfrm_user_net_ops = {
2975 .init = xfrm_user_net_init,
2976 .exit_batch = xfrm_user_net_exit,
2979 static int __init xfrm_user_init(void)
2983 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2985 rv = register_pernet_subsys(&xfrm_user_net_ops);
2988 rv = xfrm_register_km(&netlink_mgr);
2990 unregister_pernet_subsys(&xfrm_user_net_ops);
2994 static void __exit xfrm_user_exit(void)
2996 xfrm_unregister_km(&netlink_mgr);
2997 unregister_pernet_subsys(&xfrm_user_net_ops);
3000 module_init(xfrm_user_init);
3001 module_exit(xfrm_user_exit);
3002 MODULE_LICENSE("GPL");
3003 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);