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>
29 #include <asm/uaccess.h>
30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31 #include <linux/in6.h>
34 static inline int alg_len(struct xfrm_algo *alg)
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
41 struct nlattr *rt = attrs[type];
42 struct xfrm_algo *algp;
48 if (nla_len(rt) < alg_len(algp))
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
65 /* Zero length keys are legal. */
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
76 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
77 xfrm_address_t **addrp)
79 struct nlattr *rt = attrs[type];
82 *addrp = nla_data(rt);
85 static inline int verify_sec_ctx_len(struct nlattr **attrs)
87 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
88 struct xfrm_user_sec_ctx *uctx;
94 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
101 static int verify_newsa_info(struct xfrm_usersa_info *p,
102 struct nlattr **attrs)
112 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
124 switch (p->id.proto) {
126 if (!attrs[XFRMA_ALG_AUTH] ||
127 attrs[XFRMA_ALG_CRYPT] ||
128 attrs[XFRMA_ALG_COMP])
133 if ((!attrs[XFRMA_ALG_AUTH] &&
134 !attrs[XFRMA_ALG_CRYPT]) ||
135 attrs[XFRMA_ALG_COMP])
140 if (!attrs[XFRMA_ALG_COMP] ||
141 attrs[XFRMA_ALG_AUTH] ||
142 attrs[XFRMA_ALG_CRYPT])
146 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 case IPPROTO_DSTOPTS:
148 case IPPROTO_ROUTING:
149 if (attrs[XFRMA_ALG_COMP] ||
150 attrs[XFRMA_ALG_AUTH] ||
151 attrs[XFRMA_ALG_CRYPT] ||
152 attrs[XFRMA_ENCAP] ||
153 attrs[XFRMA_SEC_CTX] ||
154 !attrs[XFRMA_COADDR])
163 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
165 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
167 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
169 if ((err = verify_sec_ctx_len(attrs)))
174 case XFRM_MODE_TRANSPORT:
175 case XFRM_MODE_TUNNEL:
176 case XFRM_MODE_ROUTEOPTIMIZATION:
190 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
191 struct xfrm_algo_desc *(*get_byname)(char *, int),
194 struct xfrm_algo *p, *ualg;
195 struct xfrm_algo_desc *algo;
200 ualg = nla_data(rta);
202 algo = get_byname(ualg->alg_name, 1);
205 *props = algo->desc.sadb_alg_id;
207 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
211 strcpy(p->alg_name, algo->name);
216 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
221 len += sizeof(struct xfrm_user_sec_ctx);
222 len += xfrm_ctx->ctx_len;
227 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
229 memcpy(&x->id, &p->id, sizeof(x->id));
230 memcpy(&x->sel, &p->sel, sizeof(x->sel));
231 memcpy(&x->lft, &p->lft, sizeof(x->lft));
232 x->props.mode = p->mode;
233 x->props.replay_window = p->replay_window;
234 x->props.reqid = p->reqid;
235 x->props.family = p->family;
236 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
237 x->props.flags = p->flags;
240 * Set inner address family if the KM left it as zero.
241 * See comment in validate_tmpl.
244 x->sel.family = p->family;
248 * someday when pfkey also has support, we could have the code
249 * somehow made shareable and move it to xfrm_state.c - JHS
252 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
254 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
255 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
256 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
257 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
260 struct xfrm_replay_state *replay;
261 replay = nla_data(rp);
262 memcpy(&x->replay, replay, sizeof(*replay));
263 memcpy(&x->preplay, replay, sizeof(*replay));
267 struct xfrm_lifetime_cur *ltime;
268 ltime = nla_data(lt);
269 x->curlft.bytes = ltime->bytes;
270 x->curlft.packets = ltime->packets;
271 x->curlft.add_time = ltime->add_time;
272 x->curlft.use_time = ltime->use_time;
276 x->replay_maxage = nla_get_u32(et);
279 x->replay_maxdiff = nla_get_u32(rt);
282 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
283 struct nlattr **attrs,
286 struct xfrm_state *x = xfrm_state_alloc();
292 copy_from_user_state(x, p);
294 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
295 xfrm_aalg_get_byname,
296 attrs[XFRMA_ALG_AUTH])))
298 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
299 xfrm_ealg_get_byname,
300 attrs[XFRMA_ALG_CRYPT])))
302 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
303 xfrm_calg_get_byname,
304 attrs[XFRMA_ALG_COMP])))
307 if (attrs[XFRMA_ENCAP]) {
308 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
309 sizeof(*x->encap), GFP_KERNEL);
310 if (x->encap == NULL)
314 if (attrs[XFRMA_COADDR]) {
315 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
316 sizeof(*x->coaddr), GFP_KERNEL);
317 if (x->coaddr == NULL)
321 err = xfrm_init_state(x);
325 if (attrs[XFRMA_SEC_CTX] &&
326 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
330 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
331 /* sysctl_xfrm_aevent_etime is in 100ms units */
332 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
333 x->preplay.bitmap = 0;
334 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
335 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
337 /* override default values from above */
339 xfrm_update_ae_params(x, attrs);
344 x->km.state = XFRM_STATE_DEAD;
351 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
352 struct nlattr **attrs)
354 struct xfrm_usersa_info *p = nlmsg_data(nlh);
355 struct xfrm_state *x;
359 err = verify_newsa_info(p, attrs);
363 x = xfrm_state_construct(p, attrs, &err);
368 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
369 err = xfrm_state_add(x);
371 err = xfrm_state_update(x);
373 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
374 NETLINK_CB(skb).sid);
377 x->km.state = XFRM_STATE_DEAD;
382 c.seq = nlh->nlmsg_seq;
383 c.pid = nlh->nlmsg_pid;
384 c.event = nlh->nlmsg_type;
386 km_state_notify(x, &c);
392 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
393 struct nlattr **attrs,
396 struct xfrm_state *x = NULL;
399 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
401 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
403 xfrm_address_t *saddr = NULL;
405 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
412 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
422 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
423 struct nlattr **attrs)
425 struct xfrm_state *x;
428 struct xfrm_usersa_id *p = nlmsg_data(nlh);
430 x = xfrm_user_state_lookup(p, attrs, &err);
434 if ((err = security_xfrm_state_delete(x)) != 0)
437 if (xfrm_state_kern(x)) {
442 err = xfrm_state_delete(x);
447 c.seq = nlh->nlmsg_seq;
448 c.pid = nlh->nlmsg_pid;
449 c.event = nlh->nlmsg_type;
450 km_state_notify(x, &c);
453 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
454 NETLINK_CB(skb).sid);
459 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
461 memcpy(&p->id, &x->id, sizeof(p->id));
462 memcpy(&p->sel, &x->sel, sizeof(p->sel));
463 memcpy(&p->lft, &x->lft, sizeof(p->lft));
464 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
465 memcpy(&p->stats, &x->stats, sizeof(p->stats));
466 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
467 p->mode = x->props.mode;
468 p->replay_window = x->props.replay_window;
469 p->reqid = x->props.reqid;
470 p->family = x->props.family;
471 p->flags = x->props.flags;
475 struct xfrm_dump_info {
476 struct sk_buff *in_skb;
477 struct sk_buff *out_skb;
484 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
486 struct xfrm_user_sec_ctx *uctx;
488 int ctx_size = sizeof(*uctx) + s->ctx_len;
490 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
494 uctx = nla_data(attr);
495 uctx->exttype = XFRMA_SEC_CTX;
496 uctx->len = ctx_size;
497 uctx->ctx_doi = s->ctx_doi;
498 uctx->ctx_alg = s->ctx_alg;
499 uctx->ctx_len = s->ctx_len;
500 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
505 /* Don't change this without updating xfrm_sa_len! */
506 static int copy_to_user_state_extra(struct xfrm_state *x,
507 struct xfrm_usersa_info *p,
510 copy_to_user_state(x, p);
513 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
516 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
519 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
521 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
523 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
526 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
528 if (x->security && copy_sec_ctx(x->security, skb) < 0)
529 goto nla_put_failure;
537 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
539 struct xfrm_dump_info *sp = ptr;
540 struct sk_buff *in_skb = sp->in_skb;
541 struct sk_buff *skb = sp->out_skb;
542 struct xfrm_usersa_info *p;
543 struct nlmsghdr *nlh;
546 if (sp->this_idx < sp->start_idx)
549 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
550 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
556 err = copy_to_user_state_extra(x, p, skb);
558 goto nla_put_failure;
566 nlmsg_cancel(skb, nlh);
570 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
572 struct xfrm_dump_info info;
574 info.in_skb = cb->skb;
576 info.nlmsg_seq = cb->nlh->nlmsg_seq;
577 info.nlmsg_flags = NLM_F_MULTI;
579 info.start_idx = cb->args[0];
580 (void) xfrm_state_walk(0, dump_one_state, &info);
581 cb->args[0] = info.this_idx;
586 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
587 struct xfrm_state *x, u32 seq)
589 struct xfrm_dump_info info;
592 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
594 return ERR_PTR(-ENOMEM);
596 info.in_skb = in_skb;
598 info.nlmsg_seq = seq;
599 info.nlmsg_flags = 0;
600 info.this_idx = info.start_idx = 0;
602 if (dump_one_state(x, 0, &info)) {
610 static inline size_t xfrm_spdinfo_msgsize(void)
612 return NLMSG_ALIGN(4)
613 + nla_total_size(sizeof(struct xfrmu_spdinfo))
614 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
617 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
619 struct xfrmk_spdinfo si;
620 struct xfrmu_spdinfo spc;
621 struct xfrmu_spdhinfo sph;
622 struct nlmsghdr *nlh;
625 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
626 if (nlh == NULL) /* shouldnt really happen ... */
631 xfrm_spd_getinfo(&si);
632 spc.incnt = si.incnt;
633 spc.outcnt = si.outcnt;
634 spc.fwdcnt = si.fwdcnt;
635 spc.inscnt = si.inscnt;
636 spc.outscnt = si.outscnt;
637 spc.fwdscnt = si.fwdscnt;
638 sph.spdhcnt = si.spdhcnt;
639 sph.spdhmcnt = si.spdhmcnt;
641 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
642 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
644 return nlmsg_end(skb, nlh);
647 nlmsg_cancel(skb, nlh);
651 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
652 struct nlattr **attrs)
654 struct sk_buff *r_skb;
655 u32 *flags = nlmsg_data(nlh);
656 u32 spid = NETLINK_CB(skb).pid;
657 u32 seq = nlh->nlmsg_seq;
659 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
663 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
666 return nlmsg_unicast(xfrm_nl, r_skb, spid);
669 static inline size_t xfrm_sadinfo_msgsize(void)
671 return NLMSG_ALIGN(4)
672 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
673 + nla_total_size(4); /* XFRMA_SAD_CNT */
676 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
678 struct xfrmk_sadinfo si;
679 struct xfrmu_sadhinfo sh;
680 struct nlmsghdr *nlh;
683 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
684 if (nlh == NULL) /* shouldnt really happen ... */
689 xfrm_sad_getinfo(&si);
691 sh.sadhmcnt = si.sadhmcnt;
692 sh.sadhcnt = si.sadhcnt;
694 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
695 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
697 return nlmsg_end(skb, nlh);
700 nlmsg_cancel(skb, nlh);
704 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
705 struct nlattr **attrs)
707 struct sk_buff *r_skb;
708 u32 *flags = nlmsg_data(nlh);
709 u32 spid = NETLINK_CB(skb).pid;
710 u32 seq = nlh->nlmsg_seq;
712 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
716 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
719 return nlmsg_unicast(xfrm_nl, r_skb, spid);
722 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
723 struct nlattr **attrs)
725 struct xfrm_usersa_id *p = nlmsg_data(nlh);
726 struct xfrm_state *x;
727 struct sk_buff *resp_skb;
730 x = xfrm_user_state_lookup(p, attrs, &err);
734 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
735 if (IS_ERR(resp_skb)) {
736 err = PTR_ERR(resp_skb);
738 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
745 static int verify_userspi_info(struct xfrm_userspi_info *p)
747 switch (p->info.id.proto) {
753 /* IPCOMP spi is 16-bits. */
754 if (p->max >= 0x10000)
768 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
769 struct nlattr **attrs)
771 struct xfrm_state *x;
772 struct xfrm_userspi_info *p;
773 struct sk_buff *resp_skb;
774 xfrm_address_t *daddr;
779 err = verify_userspi_info(p);
783 family = p->info.family;
784 daddr = &p->info.id.daddr;
788 x = xfrm_find_acq_byseq(p->info.seq);
789 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
796 x = xfrm_find_acq(p->info.mode, p->info.reqid,
797 p->info.id.proto, daddr,
804 err = xfrm_alloc_spi(x, p->min, p->max);
808 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
809 if (IS_ERR(resp_skb)) {
810 err = PTR_ERR(resp_skb);
814 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
822 static int verify_policy_dir(u8 dir)
826 case XFRM_POLICY_OUT:
827 case XFRM_POLICY_FWD:
837 static int verify_policy_type(u8 type)
840 case XFRM_POLICY_TYPE_MAIN:
841 #ifdef CONFIG_XFRM_SUB_POLICY
842 case XFRM_POLICY_TYPE_SUB:
853 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
857 case XFRM_SHARE_SESSION:
858 case XFRM_SHARE_USER:
859 case XFRM_SHARE_UNIQUE:
867 case XFRM_POLICY_ALLOW:
868 case XFRM_POLICY_BLOCK:
875 switch (p->sel.family) {
880 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
883 return -EAFNOSUPPORT;
890 return verify_policy_dir(p->dir);
893 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
895 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
896 struct xfrm_user_sec_ctx *uctx;
902 return security_xfrm_policy_alloc(pol, uctx);
905 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
911 for (i = 0; i < nr; i++, ut++) {
912 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
914 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
915 memcpy(&t->saddr, &ut->saddr,
916 sizeof(xfrm_address_t));
917 t->reqid = ut->reqid;
919 t->share = ut->share;
920 t->optional = ut->optional;
921 t->aalgos = ut->aalgos;
922 t->ealgos = ut->ealgos;
923 t->calgos = ut->calgos;
924 t->encap_family = ut->family;
928 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
932 if (nr > XFRM_MAX_DEPTH)
935 for (i = 0; i < nr; i++) {
936 /* We never validated the ut->family value, so many
937 * applications simply leave it at zero. The check was
938 * never made and ut->family was ignored because all
939 * templates could be assumed to have the same family as
940 * the policy itself. Now that we will have ipv4-in-ipv6
941 * and ipv6-in-ipv4 tunnels, this is no longer true.
944 ut[i].family = family;
946 switch (ut[i].family) {
949 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
961 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
963 struct nlattr *rt = attrs[XFRMA_TMPL];
968 struct xfrm_user_tmpl *utmpl = nla_data(rt);
969 int nr = nla_len(rt) / sizeof(*utmpl);
972 err = validate_tmpl(nr, utmpl, pol->family);
976 copy_templates(pol, utmpl, nr);
981 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
983 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
984 struct xfrm_userpolicy_type *upt;
985 u8 type = XFRM_POLICY_TYPE_MAIN;
993 err = verify_policy_type(type);
1001 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1003 xp->priority = p->priority;
1004 xp->index = p->index;
1005 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1006 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1007 xp->action = p->action;
1008 xp->flags = p->flags;
1009 xp->family = p->sel.family;
1010 /* XXX xp->share = p->share; */
1013 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1015 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1016 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1017 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1018 p->priority = xp->priority;
1019 p->index = xp->index;
1020 p->sel.family = xp->family;
1022 p->action = xp->action;
1023 p->flags = xp->flags;
1024 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1027 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1029 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1037 copy_from_user_policy(xp, p);
1039 err = copy_from_user_policy_type(&xp->type, attrs);
1043 if (!(err = copy_from_user_tmpl(xp, attrs)))
1044 err = copy_from_user_sec_ctx(xp, attrs);
1055 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1056 struct nlattr **attrs)
1058 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1059 struct xfrm_policy *xp;
1064 err = verify_newpolicy_info(p);
1067 err = verify_sec_ctx_len(attrs);
1071 xp = xfrm_policy_construct(p, attrs, &err);
1075 /* shouldnt excl be based on nlh flags??
1076 * Aha! this is anti-netlink really i.e more pfkey derived
1077 * in netlink excl is a flag and you wouldnt need
1078 * a type XFRM_MSG_UPDPOLICY - JHS */
1079 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1080 err = xfrm_policy_insert(p->dir, xp, excl);
1081 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1082 NETLINK_CB(skb).sid);
1085 security_xfrm_policy_free(xp);
1090 c.event = nlh->nlmsg_type;
1091 c.seq = nlh->nlmsg_seq;
1092 c.pid = nlh->nlmsg_pid;
1093 km_policy_notify(xp, p->dir, &c);
1100 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1102 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1105 if (xp->xfrm_nr == 0)
1108 for (i = 0; i < xp->xfrm_nr; i++) {
1109 struct xfrm_user_tmpl *up = &vec[i];
1110 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1112 memcpy(&up->id, &kp->id, sizeof(up->id));
1113 up->family = kp->encap_family;
1114 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1115 up->reqid = kp->reqid;
1116 up->mode = kp->mode;
1117 up->share = kp->share;
1118 up->optional = kp->optional;
1119 up->aalgos = kp->aalgos;
1120 up->ealgos = kp->ealgos;
1121 up->calgos = kp->calgos;
1124 return nla_put(skb, XFRMA_TMPL,
1125 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1128 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1131 return copy_sec_ctx(x->security, skb);
1136 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1139 return copy_sec_ctx(xp->security, skb);
1143 static inline size_t userpolicy_type_attrsize(void)
1145 #ifdef CONFIG_XFRM_SUB_POLICY
1146 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1152 #ifdef CONFIG_XFRM_SUB_POLICY
1153 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1155 struct xfrm_userpolicy_type upt = {
1159 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1163 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1169 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1171 struct xfrm_dump_info *sp = ptr;
1172 struct xfrm_userpolicy_info *p;
1173 struct sk_buff *in_skb = sp->in_skb;
1174 struct sk_buff *skb = sp->out_skb;
1175 struct nlmsghdr *nlh;
1177 if (sp->this_idx < sp->start_idx)
1180 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1181 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1185 p = nlmsg_data(nlh);
1186 copy_to_user_policy(xp, p, dir);
1187 if (copy_to_user_tmpl(xp, skb) < 0)
1189 if (copy_to_user_sec_ctx(xp, skb))
1191 if (copy_to_user_policy_type(xp->type, skb) < 0)
1194 nlmsg_end(skb, nlh);
1200 nlmsg_cancel(skb, nlh);
1204 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1206 struct xfrm_dump_info info;
1208 info.in_skb = cb->skb;
1210 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1211 info.nlmsg_flags = NLM_F_MULTI;
1213 info.start_idx = cb->args[0];
1214 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1215 #ifdef CONFIG_XFRM_SUB_POLICY
1216 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1218 cb->args[0] = info.this_idx;
1223 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1224 struct xfrm_policy *xp,
1227 struct xfrm_dump_info info;
1228 struct sk_buff *skb;
1230 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1232 return ERR_PTR(-ENOMEM);
1234 info.in_skb = in_skb;
1236 info.nlmsg_seq = seq;
1237 info.nlmsg_flags = 0;
1238 info.this_idx = info.start_idx = 0;
1240 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1248 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1249 struct nlattr **attrs)
1251 struct xfrm_policy *xp;
1252 struct xfrm_userpolicy_id *p;
1253 u8 type = XFRM_POLICY_TYPE_MAIN;
1258 p = nlmsg_data(nlh);
1259 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1261 err = copy_from_user_policy_type(&type, attrs);
1265 err = verify_policy_dir(p->dir);
1270 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1272 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1273 struct xfrm_policy tmp;
1275 err = verify_sec_ctx_len(attrs);
1279 memset(&tmp, 0, sizeof(struct xfrm_policy));
1281 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1283 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1286 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1288 security_xfrm_policy_free(&tmp);
1294 struct sk_buff *resp_skb;
1296 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1297 if (IS_ERR(resp_skb)) {
1298 err = PTR_ERR(resp_skb);
1300 err = nlmsg_unicast(xfrm_nl, resp_skb,
1301 NETLINK_CB(skb).pid);
1304 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1305 NETLINK_CB(skb).loginuid,
1306 NETLINK_CB(skb).sid);
1311 c.data.byid = p->index;
1312 c.event = nlh->nlmsg_type;
1313 c.seq = nlh->nlmsg_seq;
1314 c.pid = nlh->nlmsg_pid;
1315 km_policy_notify(xp, p->dir, &c);
1323 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1324 struct nlattr **attrs)
1327 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1328 struct xfrm_audit audit_info;
1331 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1332 audit_info.secid = NETLINK_CB(skb).sid;
1333 err = xfrm_state_flush(p->proto, &audit_info);
1336 c.data.proto = p->proto;
1337 c.event = nlh->nlmsg_type;
1338 c.seq = nlh->nlmsg_seq;
1339 c.pid = nlh->nlmsg_pid;
1340 km_state_notify(NULL, &c);
1345 static inline size_t xfrm_aevent_msgsize(void)
1347 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1348 + nla_total_size(sizeof(struct xfrm_replay_state))
1349 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1350 + nla_total_size(4) /* XFRM_AE_RTHR */
1351 + nla_total_size(4); /* XFRM_AE_ETHR */
1354 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1356 struct xfrm_aevent_id *id;
1357 struct nlmsghdr *nlh;
1359 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1363 id = nlmsg_data(nlh);
1364 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1365 id->sa_id.spi = x->id.spi;
1366 id->sa_id.family = x->props.family;
1367 id->sa_id.proto = x->id.proto;
1368 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1369 id->reqid = x->props.reqid;
1370 id->flags = c->data.aevent;
1372 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1373 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1375 if (id->flags & XFRM_AE_RTHR)
1376 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1378 if (id->flags & XFRM_AE_ETHR)
1379 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1380 x->replay_maxage * 10 / HZ);
1382 return nlmsg_end(skb, nlh);
1385 nlmsg_cancel(skb, nlh);
1389 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1390 struct nlattr **attrs)
1392 struct xfrm_state *x;
1393 struct sk_buff *r_skb;
1396 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1397 struct xfrm_usersa_id *id = &p->sa_id;
1399 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1403 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1410 * XXX: is this lock really needed - none of the other
1411 * gets lock (the concern is things getting updated
1412 * while we are still reading) - jhs
1414 spin_lock_bh(&x->lock);
1415 c.data.aevent = p->flags;
1416 c.seq = nlh->nlmsg_seq;
1417 c.pid = nlh->nlmsg_pid;
1419 if (build_aevent(r_skb, x, &c) < 0)
1421 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1422 spin_unlock_bh(&x->lock);
1427 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1428 struct nlattr **attrs)
1430 struct xfrm_state *x;
1433 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1434 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1435 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1440 /* pedantic mode - thou shalt sayeth replaceth */
1441 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1444 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1448 if (x->km.state != XFRM_STATE_VALID)
1451 spin_lock_bh(&x->lock);
1452 xfrm_update_ae_params(x, attrs);
1453 spin_unlock_bh(&x->lock);
1455 c.event = nlh->nlmsg_type;
1456 c.seq = nlh->nlmsg_seq;
1457 c.pid = nlh->nlmsg_pid;
1458 c.data.aevent = XFRM_AE_CU;
1459 km_state_notify(x, &c);
1466 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1467 struct nlattr **attrs)
1470 u8 type = XFRM_POLICY_TYPE_MAIN;
1472 struct xfrm_audit audit_info;
1474 err = copy_from_user_policy_type(&type, attrs);
1478 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1479 audit_info.secid = NETLINK_CB(skb).sid;
1480 err = xfrm_policy_flush(type, &audit_info);
1484 c.event = nlh->nlmsg_type;
1485 c.seq = nlh->nlmsg_seq;
1486 c.pid = nlh->nlmsg_pid;
1487 km_policy_notify(NULL, 0, &c);
1491 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1492 struct nlattr **attrs)
1494 struct xfrm_policy *xp;
1495 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1496 struct xfrm_userpolicy_info *p = &up->pol;
1497 u8 type = XFRM_POLICY_TYPE_MAIN;
1500 err = copy_from_user_policy_type(&type, attrs);
1505 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1507 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1508 struct xfrm_policy tmp;
1510 err = verify_sec_ctx_len(attrs);
1514 memset(&tmp, 0, sizeof(struct xfrm_policy));
1516 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1518 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1521 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1523 security_xfrm_policy_free(&tmp);
1528 read_lock(&xp->lock);
1530 read_unlock(&xp->lock);
1534 read_unlock(&xp->lock);
1537 xfrm_policy_delete(xp, p->dir);
1538 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1539 NETLINK_CB(skb).sid);
1542 // reset the timers here?
1543 printk("Dont know what to do with soft policy expire\n");
1545 km_policy_expired(xp, p->dir, up->hard, current->pid);
1552 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1553 struct nlattr **attrs)
1555 struct xfrm_state *x;
1557 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1558 struct xfrm_usersa_info *p = &ue->state;
1560 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1566 spin_lock_bh(&x->lock);
1568 if (x->km.state != XFRM_STATE_VALID)
1570 km_state_expired(x, ue->hard, current->pid);
1573 __xfrm_state_delete(x);
1574 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1575 NETLINK_CB(skb).sid);
1579 spin_unlock_bh(&x->lock);
1584 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1585 struct nlattr **attrs)
1587 struct xfrm_policy *xp;
1588 struct xfrm_user_tmpl *ut;
1590 struct nlattr *rt = attrs[XFRMA_TMPL];
1592 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1593 struct xfrm_state *x = xfrm_state_alloc();
1599 err = verify_newpolicy_info(&ua->policy);
1601 printk("BAD policy passed\n");
1607 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1613 memcpy(&x->id, &ua->id, sizeof(ua->id));
1614 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1615 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1618 /* extract the templates and for each call km_key */
1619 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1620 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1621 memcpy(&x->id, &t->id, sizeof(x->id));
1622 x->props.mode = t->mode;
1623 x->props.reqid = t->reqid;
1624 x->props.family = ut->family;
1625 t->aalgos = ua->aalgos;
1626 t->ealgos = ua->ealgos;
1627 t->calgos = ua->calgos;
1628 err = km_query(x, t, xp);
1638 #ifdef CONFIG_XFRM_MIGRATE
1639 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1640 struct nlattr **attrs, int *num)
1642 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1643 struct xfrm_user_migrate *um;
1647 num_migrate = nla_len(rt) / sizeof(*um);
1649 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1652 for (i = 0; i < num_migrate; i++, um++, ma++) {
1653 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1654 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1655 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1656 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1658 ma->proto = um->proto;
1659 ma->mode = um->mode;
1660 ma->reqid = um->reqid;
1662 ma->old_family = um->old_family;
1663 ma->new_family = um->new_family;
1670 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1671 struct nlattr **attrs)
1673 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1674 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1679 if (attrs[XFRMA_MIGRATE] == NULL)
1682 err = copy_from_user_policy_type(&type, attrs);
1686 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1694 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1699 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1700 struct nlattr **attrs)
1702 return -ENOPROTOOPT;
1706 #ifdef CONFIG_XFRM_MIGRATE
1707 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1709 struct xfrm_user_migrate um;
1711 memset(&um, 0, sizeof(um));
1712 um.proto = m->proto;
1714 um.reqid = m->reqid;
1715 um.old_family = m->old_family;
1716 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1717 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1718 um.new_family = m->new_family;
1719 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1720 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1722 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1725 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1727 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1728 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1729 + userpolicy_type_attrsize();
1732 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1733 int num_migrate, struct xfrm_selector *sel,
1736 struct xfrm_migrate *mp;
1737 struct xfrm_userpolicy_id *pol_id;
1738 struct nlmsghdr *nlh;
1741 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1745 pol_id = nlmsg_data(nlh);
1746 /* copy data from selector, dir, and type to the pol_id */
1747 memset(pol_id, 0, sizeof(*pol_id));
1748 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1751 if (copy_to_user_policy_type(type, skb) < 0)
1754 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1755 if (copy_to_user_migrate(mp, skb) < 0)
1759 return nlmsg_end(skb, nlh);
1761 nlmsg_cancel(skb, nlh);
1765 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1766 struct xfrm_migrate *m, int num_migrate)
1768 struct sk_buff *skb;
1770 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1775 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1778 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1781 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1782 struct xfrm_migrate *m, int num_migrate)
1784 return -ENOPROTOOPT;
1788 #define XMSGSIZE(type) sizeof(struct type)
1790 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1791 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1792 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1793 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1794 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1795 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1796 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1797 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1798 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1799 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1800 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1801 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1802 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1803 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1804 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1805 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1806 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1807 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1808 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1809 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1810 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
1815 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1816 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1817 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1818 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1819 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1820 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1821 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1822 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1823 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1824 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1825 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1826 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1827 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1828 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1829 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1832 static struct xfrm_link {
1833 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1834 int (*dump)(struct sk_buff *, struct netlink_callback *);
1835 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1836 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1837 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1838 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1839 .dump = xfrm_dump_sa },
1840 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1841 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1842 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1843 .dump = xfrm_dump_policy },
1844 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1845 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1846 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1847 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1848 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1849 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1850 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1851 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1852 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1853 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1854 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1855 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
1856 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1859 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1861 struct nlattr *attrs[XFRMA_MAX+1];
1862 struct xfrm_link *link;
1865 type = nlh->nlmsg_type;
1866 if (type > XFRM_MSG_MAX)
1869 type -= XFRM_MSG_BASE;
1870 link = &xfrm_dispatch[type];
1872 /* All operations require privileges, even GET */
1873 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1876 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1877 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1878 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1879 if (link->dump == NULL)
1882 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
1885 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1890 if (link->doit == NULL)
1893 return link->doit(skb, nlh, attrs);
1896 static void xfrm_netlink_rcv(struct sk_buff *skb)
1898 mutex_lock(&xfrm_cfg_mutex);
1899 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1900 mutex_unlock(&xfrm_cfg_mutex);
1903 static inline size_t xfrm_expire_msgsize(void)
1905 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1908 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1910 struct xfrm_user_expire *ue;
1911 struct nlmsghdr *nlh;
1913 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1917 ue = nlmsg_data(nlh);
1918 copy_to_user_state(x, &ue->state);
1919 ue->hard = (c->data.hard != 0) ? 1 : 0;
1921 return nlmsg_end(skb, nlh);
1924 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1926 struct sk_buff *skb;
1928 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
1932 if (build_expire(skb, x, c) < 0)
1935 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1938 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1940 struct sk_buff *skb;
1942 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1946 if (build_aevent(skb, x, c) < 0)
1949 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1952 static int xfrm_notify_sa_flush(struct km_event *c)
1954 struct xfrm_usersa_flush *p;
1955 struct nlmsghdr *nlh;
1956 struct sk_buff *skb;
1957 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
1959 skb = nlmsg_new(len, GFP_ATOMIC);
1963 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1969 p = nlmsg_data(nlh);
1970 p->proto = c->data.proto;
1972 nlmsg_end(skb, nlh);
1974 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1977 static inline size_t xfrm_sa_len(struct xfrm_state *x)
1981 l += nla_total_size(alg_len(x->aalg));
1983 l += nla_total_size(alg_len(x->ealg));
1985 l += nla_total_size(sizeof(*x->calg));
1987 l += nla_total_size(sizeof(*x->encap));
1989 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
1990 x->security->ctx_len);
1992 l += nla_total_size(sizeof(*x->coaddr));
1994 /* Must count this as this may become non-zero behind our back. */
1995 l += nla_total_size(sizeof(x->lastused));
2000 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2002 struct xfrm_usersa_info *p;
2003 struct xfrm_usersa_id *id;
2004 struct nlmsghdr *nlh;
2005 struct sk_buff *skb;
2006 int len = xfrm_sa_len(x);
2009 headlen = sizeof(*p);
2010 if (c->event == XFRM_MSG_DELSA) {
2011 len += nla_total_size(headlen);
2012 headlen = sizeof(*id);
2014 len += NLMSG_ALIGN(headlen);
2016 skb = nlmsg_new(len, GFP_ATOMIC);
2020 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2022 goto nla_put_failure;
2024 p = nlmsg_data(nlh);
2025 if (c->event == XFRM_MSG_DELSA) {
2026 struct nlattr *attr;
2028 id = nlmsg_data(nlh);
2029 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2030 id->spi = x->id.spi;
2031 id->family = x->props.family;
2032 id->proto = x->id.proto;
2034 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2036 goto nla_put_failure;
2041 if (copy_to_user_state_extra(x, p, skb))
2042 goto nla_put_failure;
2044 nlmsg_end(skb, nlh);
2046 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2049 /* Somebody screwed up with xfrm_sa_len! */
2055 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2059 case XFRM_MSG_EXPIRE:
2060 return xfrm_exp_state_notify(x, c);
2061 case XFRM_MSG_NEWAE:
2062 return xfrm_aevent_state_notify(x, c);
2063 case XFRM_MSG_DELSA:
2064 case XFRM_MSG_UPDSA:
2065 case XFRM_MSG_NEWSA:
2066 return xfrm_notify_sa(x, c);
2067 case XFRM_MSG_FLUSHSA:
2068 return xfrm_notify_sa_flush(c);
2070 printk("xfrm_user: Unknown SA event %d\n", c->event);
2078 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2079 struct xfrm_policy *xp)
2081 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2082 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2083 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2084 + userpolicy_type_attrsize();
2087 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2088 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2091 struct xfrm_user_acquire *ua;
2092 struct nlmsghdr *nlh;
2093 __u32 seq = xfrm_get_acqseq();
2095 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2099 ua = nlmsg_data(nlh);
2100 memcpy(&ua->id, &x->id, sizeof(ua->id));
2101 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2102 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2103 copy_to_user_policy(xp, &ua->policy, dir);
2104 ua->aalgos = xt->aalgos;
2105 ua->ealgos = xt->ealgos;
2106 ua->calgos = xt->calgos;
2107 ua->seq = x->km.seq = seq;
2109 if (copy_to_user_tmpl(xp, skb) < 0)
2111 if (copy_to_user_state_sec_ctx(x, skb))
2113 if (copy_to_user_policy_type(xp->type, skb) < 0)
2116 return nlmsg_end(skb, nlh);
2119 nlmsg_cancel(skb, nlh);
2123 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2124 struct xfrm_policy *xp, int dir)
2126 struct sk_buff *skb;
2128 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2132 if (build_acquire(skb, x, xt, xp, dir) < 0)
2135 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2138 /* User gives us xfrm_user_policy_info followed by an array of 0
2139 * or more templates.
2141 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2142 u8 *data, int len, int *dir)
2144 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2145 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2146 struct xfrm_policy *xp;
2149 switch (sk->sk_family) {
2151 if (opt != IP_XFRM_POLICY) {
2156 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2158 if (opt != IPV6_XFRM_POLICY) {
2171 if (len < sizeof(*p) ||
2172 verify_newpolicy_info(p))
2175 nr = ((len - sizeof(*p)) / sizeof(*ut));
2176 if (validate_tmpl(nr, ut, p->sel.family))
2179 if (p->dir > XFRM_POLICY_OUT)
2182 xp = xfrm_policy_alloc(GFP_KERNEL);
2188 copy_from_user_policy(xp, p);
2189 xp->type = XFRM_POLICY_TYPE_MAIN;
2190 copy_templates(xp, ut, nr);
2197 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2199 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2200 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2201 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2202 + userpolicy_type_attrsize();
2205 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2206 int dir, struct km_event *c)
2208 struct xfrm_user_polexpire *upe;
2209 struct nlmsghdr *nlh;
2210 int hard = c->data.hard;
2212 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2216 upe = nlmsg_data(nlh);
2217 copy_to_user_policy(xp, &upe->pol, dir);
2218 if (copy_to_user_tmpl(xp, skb) < 0)
2220 if (copy_to_user_sec_ctx(xp, skb))
2222 if (copy_to_user_policy_type(xp->type, skb) < 0)
2226 return nlmsg_end(skb, nlh);
2229 nlmsg_cancel(skb, nlh);
2233 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2235 struct sk_buff *skb;
2237 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2241 if (build_polexpire(skb, xp, dir, c) < 0)
2244 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2247 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2249 struct xfrm_userpolicy_info *p;
2250 struct xfrm_userpolicy_id *id;
2251 struct nlmsghdr *nlh;
2252 struct sk_buff *skb;
2253 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2256 headlen = sizeof(*p);
2257 if (c->event == XFRM_MSG_DELPOLICY) {
2258 len += nla_total_size(headlen);
2259 headlen = sizeof(*id);
2261 len += userpolicy_type_attrsize();
2262 len += NLMSG_ALIGN(headlen);
2264 skb = nlmsg_new(len, GFP_ATOMIC);
2268 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2272 p = nlmsg_data(nlh);
2273 if (c->event == XFRM_MSG_DELPOLICY) {
2274 struct nlattr *attr;
2276 id = nlmsg_data(nlh);
2277 memset(id, 0, sizeof(*id));
2280 id->index = xp->index;
2282 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2284 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2291 copy_to_user_policy(xp, p, dir);
2292 if (copy_to_user_tmpl(xp, skb) < 0)
2294 if (copy_to_user_policy_type(xp->type, skb) < 0)
2297 nlmsg_end(skb, nlh);
2299 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2306 static int xfrm_notify_policy_flush(struct km_event *c)
2308 struct nlmsghdr *nlh;
2309 struct sk_buff *skb;
2311 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2315 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2318 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2321 nlmsg_end(skb, nlh);
2323 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2330 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2334 case XFRM_MSG_NEWPOLICY:
2335 case XFRM_MSG_UPDPOLICY:
2336 case XFRM_MSG_DELPOLICY:
2337 return xfrm_notify_policy(xp, dir, c);
2338 case XFRM_MSG_FLUSHPOLICY:
2339 return xfrm_notify_policy_flush(c);
2340 case XFRM_MSG_POLEXPIRE:
2341 return xfrm_exp_policy_notify(xp, dir, c);
2343 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2350 static inline size_t xfrm_report_msgsize(void)
2352 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2355 static int build_report(struct sk_buff *skb, u8 proto,
2356 struct xfrm_selector *sel, xfrm_address_t *addr)
2358 struct xfrm_user_report *ur;
2359 struct nlmsghdr *nlh;
2361 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2365 ur = nlmsg_data(nlh);
2367 memcpy(&ur->sel, sel, sizeof(ur->sel));
2370 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2372 return nlmsg_end(skb, nlh);
2375 nlmsg_cancel(skb, nlh);
2379 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2380 xfrm_address_t *addr)
2382 struct sk_buff *skb;
2384 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2388 if (build_report(skb, proto, sel, addr) < 0)
2391 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2394 static struct xfrm_mgr netlink_mgr = {
2396 .notify = xfrm_send_state_notify,
2397 .acquire = xfrm_send_acquire,
2398 .compile_policy = xfrm_compile_policy,
2399 .notify_policy = xfrm_send_policy_notify,
2400 .report = xfrm_send_report,
2401 .migrate = xfrm_send_migrate,
2404 static int __init xfrm_user_init(void)
2408 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2410 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
2411 xfrm_netlink_rcv, NULL, THIS_MODULE);
2414 rcu_assign_pointer(xfrm_nl, nlsk);
2416 xfrm_register_km(&netlink_mgr);
2421 static void __exit xfrm_user_exit(void)
2423 struct sock *nlsk = xfrm_nl;
2425 xfrm_unregister_km(&netlink_mgr);
2426 rcu_assign_pointer(xfrm_nl, NULL);
2428 sock_release(nlsk->sk_socket);
2431 module_init(xfrm_user_init);
2432 module_exit(xfrm_user_exit);
2433 MODULE_LICENSE("GPL");
2434 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);