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>
33 #include <linux/audit.h>
35 static inline int alg_len(struct xfrm_algo *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) < alg_len(algp))
54 if (!algp->alg_key_len &&
55 strcmp(algp->alg_name, "digest_null") != 0)
60 if (!algp->alg_key_len &&
61 strcmp(algp->alg_name, "cipher_null") != 0)
66 /* Zero length keys are legal. */
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
77 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
78 xfrm_address_t **addrp)
80 struct nlattr *rt = attrs[type];
83 *addrp = nla_data(rt);
86 static inline int verify_sec_ctx_len(struct nlattr **attrs)
88 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
89 struct xfrm_user_sec_ctx *uctx;
95 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
102 static int verify_newsa_info(struct xfrm_usersa_info *p,
103 struct nlattr **attrs)
113 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
125 switch (p->id.proto) {
127 if (!attrs[XFRMA_ALG_AUTH] ||
128 attrs[XFRMA_ALG_CRYPT] ||
129 attrs[XFRMA_ALG_COMP])
134 if ((!attrs[XFRMA_ALG_AUTH] &&
135 !attrs[XFRMA_ALG_CRYPT]) ||
136 attrs[XFRMA_ALG_COMP])
141 if (!attrs[XFRMA_ALG_COMP] ||
142 attrs[XFRMA_ALG_AUTH] ||
143 attrs[XFRMA_ALG_CRYPT])
147 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
148 case IPPROTO_DSTOPTS:
149 case IPPROTO_ROUTING:
150 if (attrs[XFRMA_ALG_COMP] ||
151 attrs[XFRMA_ALG_AUTH] ||
152 attrs[XFRMA_ALG_CRYPT] ||
153 attrs[XFRMA_ENCAP] ||
154 attrs[XFRMA_SEC_CTX] ||
155 !attrs[XFRMA_COADDR])
164 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
166 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
168 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
170 if ((err = verify_sec_ctx_len(attrs)))
175 case XFRM_MODE_TRANSPORT:
176 case XFRM_MODE_TUNNEL:
177 case XFRM_MODE_ROUTEOPTIMIZATION:
191 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
192 struct xfrm_algo_desc *(*get_byname)(char *, int),
195 struct xfrm_algo *p, *ualg;
196 struct xfrm_algo_desc *algo;
201 ualg = nla_data(rta);
203 algo = get_byname(ualg->alg_name, 1);
206 *props = algo->desc.sadb_alg_id;
208 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
212 strcpy(p->alg_name, algo->name);
217 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct nlattr *rta)
219 struct xfrm_encap_tmpl *p, *uencap;
224 uencap = nla_data(rta);
225 p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
234 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
239 len += sizeof(struct xfrm_user_sec_ctx);
240 len += xfrm_ctx->ctx_len;
245 static int attach_sec_ctx(struct xfrm_state *x, struct nlattr *u_arg)
247 struct xfrm_user_sec_ctx *uctx;
252 uctx = nla_data(u_arg);
253 return security_xfrm_state_alloc(x, uctx);
256 static int attach_one_addr(xfrm_address_t **addrpp, struct nlattr *rta)
258 xfrm_address_t *p, *uaddrp;
263 uaddrp = nla_data(rta);
264 p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
272 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
274 memcpy(&x->id, &p->id, sizeof(x->id));
275 memcpy(&x->sel, &p->sel, sizeof(x->sel));
276 memcpy(&x->lft, &p->lft, sizeof(x->lft));
277 x->props.mode = p->mode;
278 x->props.replay_window = p->replay_window;
279 x->props.reqid = p->reqid;
280 x->props.family = p->family;
281 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
282 x->props.flags = p->flags;
285 * Set inner address family if the KM left it as zero.
286 * See comment in validate_tmpl.
289 x->sel.family = p->family;
293 * someday when pfkey also has support, we could have the code
294 * somehow made shareable and move it to xfrm_state.c - JHS
297 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
299 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
300 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
301 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
302 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
305 struct xfrm_replay_state *replay;
306 replay = nla_data(rp);
307 memcpy(&x->replay, replay, sizeof(*replay));
308 memcpy(&x->preplay, replay, sizeof(*replay));
312 struct xfrm_lifetime_cur *ltime;
313 ltime = nla_data(lt);
314 x->curlft.bytes = ltime->bytes;
315 x->curlft.packets = ltime->packets;
316 x->curlft.add_time = ltime->add_time;
317 x->curlft.use_time = ltime->use_time;
321 x->replay_maxage = nla_get_u32(et);
324 x->replay_maxdiff = nla_get_u32(rt);
327 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
328 struct nlattr **attrs,
331 struct xfrm_state *x = xfrm_state_alloc();
337 copy_from_user_state(x, p);
339 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
340 xfrm_aalg_get_byname,
341 attrs[XFRMA_ALG_AUTH])))
343 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
344 xfrm_ealg_get_byname,
345 attrs[XFRMA_ALG_CRYPT])))
347 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
348 xfrm_calg_get_byname,
349 attrs[XFRMA_ALG_COMP])))
351 if ((err = attach_encap_tmpl(&x->encap, attrs[XFRMA_ENCAP])))
353 if ((err = attach_one_addr(&x->coaddr, attrs[XFRMA_COADDR])))
355 err = xfrm_init_state(x);
359 if ((err = attach_sec_ctx(x, attrs[XFRMA_SEC_CTX])))
363 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
364 /* sysctl_xfrm_aevent_etime is in 100ms units */
365 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
366 x->preplay.bitmap = 0;
367 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
368 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
370 /* override default values from above */
372 xfrm_update_ae_params(x, attrs);
377 x->km.state = XFRM_STATE_DEAD;
384 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
385 struct nlattr **attrs)
387 struct xfrm_usersa_info *p = nlmsg_data(nlh);
388 struct xfrm_state *x;
392 err = verify_newsa_info(p, attrs);
396 x = xfrm_state_construct(p, attrs, &err);
401 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
402 err = xfrm_state_add(x);
404 err = xfrm_state_update(x);
406 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
407 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
410 x->km.state = XFRM_STATE_DEAD;
415 c.seq = nlh->nlmsg_seq;
416 c.pid = nlh->nlmsg_pid;
417 c.event = nlh->nlmsg_type;
419 km_state_notify(x, &c);
425 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
426 struct nlattr **attrs,
429 struct xfrm_state *x = NULL;
432 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
434 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
436 xfrm_address_t *saddr = NULL;
438 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
445 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
455 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
456 struct nlattr **attrs)
458 struct xfrm_state *x;
461 struct xfrm_usersa_id *p = nlmsg_data(nlh);
463 x = xfrm_user_state_lookup(p, attrs, &err);
467 if ((err = security_xfrm_state_delete(x)) != 0)
470 if (xfrm_state_kern(x)) {
475 err = xfrm_state_delete(x);
480 c.seq = nlh->nlmsg_seq;
481 c.pid = nlh->nlmsg_pid;
482 c.event = nlh->nlmsg_type;
483 km_state_notify(x, &c);
486 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
487 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
492 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
494 memcpy(&p->id, &x->id, sizeof(p->id));
495 memcpy(&p->sel, &x->sel, sizeof(p->sel));
496 memcpy(&p->lft, &x->lft, sizeof(p->lft));
497 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
498 memcpy(&p->stats, &x->stats, sizeof(p->stats));
499 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
500 p->mode = x->props.mode;
501 p->replay_window = x->props.replay_window;
502 p->reqid = x->props.reqid;
503 p->family = x->props.family;
504 p->flags = x->props.flags;
508 struct xfrm_dump_info {
509 struct sk_buff *in_skb;
510 struct sk_buff *out_skb;
517 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
519 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
520 struct xfrm_user_sec_ctx *uctx;
523 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
527 uctx = nla_data(attr);
528 uctx->exttype = XFRMA_SEC_CTX;
529 uctx->len = ctx_size;
530 uctx->ctx_doi = s->ctx_doi;
531 uctx->ctx_alg = s->ctx_alg;
532 uctx->ctx_len = s->ctx_len;
533 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
538 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
540 struct xfrm_dump_info *sp = ptr;
541 struct sk_buff *in_skb = sp->in_skb;
542 struct sk_buff *skb = sp->out_skb;
543 struct xfrm_usersa_info *p;
544 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);
555 copy_to_user_state(x, p);
558 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
560 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
562 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
565 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
567 if (x->security && copy_sec_ctx(x->security, skb) < 0)
568 goto nla_put_failure;
571 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
574 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
582 nlmsg_cancel(skb, nlh);
586 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
588 struct xfrm_dump_info info;
590 info.in_skb = cb->skb;
592 info.nlmsg_seq = cb->nlh->nlmsg_seq;
593 info.nlmsg_flags = NLM_F_MULTI;
595 info.start_idx = cb->args[0];
596 (void) xfrm_state_walk(0, dump_one_state, &info);
597 cb->args[0] = info.this_idx;
602 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
603 struct xfrm_state *x, u32 seq)
605 struct xfrm_dump_info info;
608 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
610 return ERR_PTR(-ENOMEM);
612 info.in_skb = in_skb;
614 info.nlmsg_seq = seq;
615 info.nlmsg_flags = 0;
616 info.this_idx = info.start_idx = 0;
618 if (dump_one_state(x, 0, &info)) {
626 static inline size_t xfrm_spdinfo_msgsize(void)
628 return NLMSG_ALIGN(4)
629 + nla_total_size(sizeof(struct xfrmu_spdinfo))
630 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
633 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
635 struct xfrmk_spdinfo si;
636 struct xfrmu_spdinfo spc;
637 struct xfrmu_spdhinfo sph;
638 struct nlmsghdr *nlh;
641 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
642 if (nlh == NULL) /* shouldnt really happen ... */
647 xfrm_spd_getinfo(&si);
648 spc.incnt = si.incnt;
649 spc.outcnt = si.outcnt;
650 spc.fwdcnt = si.fwdcnt;
651 spc.inscnt = si.inscnt;
652 spc.outscnt = si.outscnt;
653 spc.fwdscnt = si.fwdscnt;
654 sph.spdhcnt = si.spdhcnt;
655 sph.spdhmcnt = si.spdhmcnt;
657 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
658 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
660 return nlmsg_end(skb, nlh);
663 nlmsg_cancel(skb, nlh);
667 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
668 struct nlattr **attrs)
670 struct sk_buff *r_skb;
671 u32 *flags = nlmsg_data(nlh);
672 u32 spid = NETLINK_CB(skb).pid;
673 u32 seq = nlh->nlmsg_seq;
675 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
679 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
682 return nlmsg_unicast(xfrm_nl, r_skb, spid);
685 static inline size_t xfrm_sadinfo_msgsize(void)
687 return NLMSG_ALIGN(4)
688 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
689 + nla_total_size(4); /* XFRMA_SAD_CNT */
692 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
694 struct xfrmk_sadinfo si;
695 struct xfrmu_sadhinfo sh;
696 struct nlmsghdr *nlh;
699 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
700 if (nlh == NULL) /* shouldnt really happen ... */
705 xfrm_sad_getinfo(&si);
707 sh.sadhmcnt = si.sadhmcnt;
708 sh.sadhcnt = si.sadhcnt;
710 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
711 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
713 return nlmsg_end(skb, nlh);
716 nlmsg_cancel(skb, nlh);
720 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
721 struct nlattr **attrs)
723 struct sk_buff *r_skb;
724 u32 *flags = nlmsg_data(nlh);
725 u32 spid = NETLINK_CB(skb).pid;
726 u32 seq = nlh->nlmsg_seq;
728 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
732 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
735 return nlmsg_unicast(xfrm_nl, r_skb, spid);
738 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
739 struct nlattr **attrs)
741 struct xfrm_usersa_id *p = nlmsg_data(nlh);
742 struct xfrm_state *x;
743 struct sk_buff *resp_skb;
746 x = xfrm_user_state_lookup(p, attrs, &err);
750 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
751 if (IS_ERR(resp_skb)) {
752 err = PTR_ERR(resp_skb);
754 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
761 static int verify_userspi_info(struct xfrm_userspi_info *p)
763 switch (p->info.id.proto) {
769 /* IPCOMP spi is 16-bits. */
770 if (p->max >= 0x10000)
784 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
785 struct nlattr **attrs)
787 struct xfrm_state *x;
788 struct xfrm_userspi_info *p;
789 struct sk_buff *resp_skb;
790 xfrm_address_t *daddr;
795 err = verify_userspi_info(p);
799 family = p->info.family;
800 daddr = &p->info.id.daddr;
804 x = xfrm_find_acq_byseq(p->info.seq);
805 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
812 x = xfrm_find_acq(p->info.mode, p->info.reqid,
813 p->info.id.proto, daddr,
820 resp_skb = ERR_PTR(-ENOENT);
822 spin_lock_bh(&x->lock);
823 if (x->km.state != XFRM_STATE_DEAD) {
824 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
826 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
828 spin_unlock_bh(&x->lock);
830 if (IS_ERR(resp_skb)) {
831 err = PTR_ERR(resp_skb);
835 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
843 static int verify_policy_dir(u8 dir)
847 case XFRM_POLICY_OUT:
848 case XFRM_POLICY_FWD:
858 static int verify_policy_type(u8 type)
861 case XFRM_POLICY_TYPE_MAIN:
862 #ifdef CONFIG_XFRM_SUB_POLICY
863 case XFRM_POLICY_TYPE_SUB:
874 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
878 case XFRM_SHARE_SESSION:
879 case XFRM_SHARE_USER:
880 case XFRM_SHARE_UNIQUE:
888 case XFRM_POLICY_ALLOW:
889 case XFRM_POLICY_BLOCK:
896 switch (p->sel.family) {
901 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
904 return -EAFNOSUPPORT;
911 return verify_policy_dir(p->dir);
914 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
916 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
917 struct xfrm_user_sec_ctx *uctx;
923 return security_xfrm_policy_alloc(pol, uctx);
926 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
932 for (i = 0; i < nr; i++, ut++) {
933 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
935 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
936 memcpy(&t->saddr, &ut->saddr,
937 sizeof(xfrm_address_t));
938 t->reqid = ut->reqid;
940 t->share = ut->share;
941 t->optional = ut->optional;
942 t->aalgos = ut->aalgos;
943 t->ealgos = ut->ealgos;
944 t->calgos = ut->calgos;
945 t->encap_family = ut->family;
949 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
953 if (nr > XFRM_MAX_DEPTH)
956 for (i = 0; i < nr; i++) {
957 /* We never validated the ut->family value, so many
958 * applications simply leave it at zero. The check was
959 * never made and ut->family was ignored because all
960 * templates could be assumed to have the same family as
961 * the policy itself. Now that we will have ipv4-in-ipv6
962 * and ipv6-in-ipv4 tunnels, this is no longer true.
965 ut[i].family = family;
967 switch (ut[i].family) {
970 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
982 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
984 struct nlattr *rt = attrs[XFRMA_TMPL];
989 struct xfrm_user_tmpl *utmpl = nla_data(rt);
990 int nr = nla_len(rt) / sizeof(*utmpl);
993 err = validate_tmpl(nr, utmpl, pol->family);
997 copy_templates(pol, utmpl, nr);
1002 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1004 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1005 struct xfrm_userpolicy_type *upt;
1006 u8 type = XFRM_POLICY_TYPE_MAIN;
1014 err = verify_policy_type(type);
1022 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1024 xp->priority = p->priority;
1025 xp->index = p->index;
1026 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1027 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1028 xp->action = p->action;
1029 xp->flags = p->flags;
1030 xp->family = p->sel.family;
1031 /* XXX xp->share = p->share; */
1034 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1036 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1037 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1038 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1039 p->priority = xp->priority;
1040 p->index = xp->index;
1041 p->sel.family = xp->family;
1043 p->action = xp->action;
1044 p->flags = xp->flags;
1045 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1048 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1050 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1058 copy_from_user_policy(xp, p);
1060 err = copy_from_user_policy_type(&xp->type, attrs);
1064 if (!(err = copy_from_user_tmpl(xp, attrs)))
1065 err = copy_from_user_sec_ctx(xp, attrs);
1076 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1077 struct nlattr **attrs)
1079 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1080 struct xfrm_policy *xp;
1085 err = verify_newpolicy_info(p);
1088 err = verify_sec_ctx_len(attrs);
1092 xp = xfrm_policy_construct(p, attrs, &err);
1096 /* shouldnt excl be based on nlh flags??
1097 * Aha! this is anti-netlink really i.e more pfkey derived
1098 * in netlink excl is a flag and you wouldnt need
1099 * a type XFRM_MSG_UPDPOLICY - JHS */
1100 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1101 err = xfrm_policy_insert(p->dir, xp, excl);
1102 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1103 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1106 security_xfrm_policy_free(xp);
1111 c.event = nlh->nlmsg_type;
1112 c.seq = nlh->nlmsg_seq;
1113 c.pid = nlh->nlmsg_pid;
1114 km_policy_notify(xp, p->dir, &c);
1121 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1123 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1126 if (xp->xfrm_nr == 0)
1129 for (i = 0; i < xp->xfrm_nr; i++) {
1130 struct xfrm_user_tmpl *up = &vec[i];
1131 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1133 memcpy(&up->id, &kp->id, sizeof(up->id));
1134 up->family = kp->encap_family;
1135 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1136 up->reqid = kp->reqid;
1137 up->mode = kp->mode;
1138 up->share = kp->share;
1139 up->optional = kp->optional;
1140 up->aalgos = kp->aalgos;
1141 up->ealgos = kp->ealgos;
1142 up->calgos = kp->calgos;
1145 return nla_put(skb, XFRMA_TMPL,
1146 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1149 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1152 return copy_sec_ctx(x->security, skb);
1157 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1160 return copy_sec_ctx(xp->security, skb);
1164 static inline size_t userpolicy_type_attrsize(void)
1166 #ifdef CONFIG_XFRM_SUB_POLICY
1167 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1173 #ifdef CONFIG_XFRM_SUB_POLICY
1174 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1176 struct xfrm_userpolicy_type upt = {
1180 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1184 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1190 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1192 struct xfrm_dump_info *sp = ptr;
1193 struct xfrm_userpolicy_info *p;
1194 struct sk_buff *in_skb = sp->in_skb;
1195 struct sk_buff *skb = sp->out_skb;
1196 struct nlmsghdr *nlh;
1198 if (sp->this_idx < sp->start_idx)
1201 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1202 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1206 p = nlmsg_data(nlh);
1207 copy_to_user_policy(xp, p, dir);
1208 if (copy_to_user_tmpl(xp, skb) < 0)
1210 if (copy_to_user_sec_ctx(xp, skb))
1212 if (copy_to_user_policy_type(xp->type, skb) < 0)
1215 nlmsg_end(skb, nlh);
1221 nlmsg_cancel(skb, nlh);
1225 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1227 struct xfrm_dump_info info;
1229 info.in_skb = cb->skb;
1231 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1232 info.nlmsg_flags = NLM_F_MULTI;
1234 info.start_idx = cb->args[0];
1235 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1236 #ifdef CONFIG_XFRM_SUB_POLICY
1237 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1239 cb->args[0] = info.this_idx;
1244 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1245 struct xfrm_policy *xp,
1248 struct xfrm_dump_info info;
1249 struct sk_buff *skb;
1251 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1253 return ERR_PTR(-ENOMEM);
1255 info.in_skb = in_skb;
1257 info.nlmsg_seq = seq;
1258 info.nlmsg_flags = 0;
1259 info.this_idx = info.start_idx = 0;
1261 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1269 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1270 struct nlattr **attrs)
1272 struct xfrm_policy *xp;
1273 struct xfrm_userpolicy_id *p;
1274 u8 type = XFRM_POLICY_TYPE_MAIN;
1279 p = nlmsg_data(nlh);
1280 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1282 err = copy_from_user_policy_type(&type, attrs);
1286 err = verify_policy_dir(p->dir);
1291 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1293 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1294 struct xfrm_policy tmp;
1296 err = verify_sec_ctx_len(attrs);
1300 memset(&tmp, 0, sizeof(struct xfrm_policy));
1302 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1304 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1307 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1309 security_xfrm_policy_free(&tmp);
1315 struct sk_buff *resp_skb;
1317 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1318 if (IS_ERR(resp_skb)) {
1319 err = PTR_ERR(resp_skb);
1321 err = nlmsg_unicast(xfrm_nl, resp_skb,
1322 NETLINK_CB(skb).pid);
1325 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1326 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
1331 c.data.byid = p->index;
1332 c.event = nlh->nlmsg_type;
1333 c.seq = nlh->nlmsg_seq;
1334 c.pid = nlh->nlmsg_pid;
1335 km_policy_notify(xp, p->dir, &c);
1343 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1344 struct nlattr **attrs)
1347 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1348 struct xfrm_audit audit_info;
1351 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1352 audit_info.secid = NETLINK_CB(skb).sid;
1353 err = xfrm_state_flush(p->proto, &audit_info);
1356 c.data.proto = p->proto;
1357 c.event = nlh->nlmsg_type;
1358 c.seq = nlh->nlmsg_seq;
1359 c.pid = nlh->nlmsg_pid;
1360 km_state_notify(NULL, &c);
1365 static inline size_t xfrm_aevent_msgsize(void)
1367 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1368 + nla_total_size(sizeof(struct xfrm_replay_state))
1369 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1370 + nla_total_size(4) /* XFRM_AE_RTHR */
1371 + nla_total_size(4); /* XFRM_AE_ETHR */
1374 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1376 struct xfrm_aevent_id *id;
1377 struct nlmsghdr *nlh;
1379 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1383 id = nlmsg_data(nlh);
1384 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1385 id->sa_id.spi = x->id.spi;
1386 id->sa_id.family = x->props.family;
1387 id->sa_id.proto = x->id.proto;
1388 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1389 id->reqid = x->props.reqid;
1390 id->flags = c->data.aevent;
1392 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1393 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1395 if (id->flags & XFRM_AE_RTHR)
1396 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1398 if (id->flags & XFRM_AE_ETHR)
1399 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1400 x->replay_maxage * 10 / HZ);
1402 return nlmsg_end(skb, nlh);
1405 nlmsg_cancel(skb, nlh);
1409 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1410 struct nlattr **attrs)
1412 struct xfrm_state *x;
1413 struct sk_buff *r_skb;
1416 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1417 struct xfrm_usersa_id *id = &p->sa_id;
1419 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1423 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1430 * XXX: is this lock really needed - none of the other
1431 * gets lock (the concern is things getting updated
1432 * while we are still reading) - jhs
1434 spin_lock_bh(&x->lock);
1435 c.data.aevent = p->flags;
1436 c.seq = nlh->nlmsg_seq;
1437 c.pid = nlh->nlmsg_pid;
1439 if (build_aevent(r_skb, x, &c) < 0)
1441 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1442 spin_unlock_bh(&x->lock);
1447 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1448 struct nlattr **attrs)
1450 struct xfrm_state *x;
1453 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1454 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1455 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1460 /* pedantic mode - thou shalt sayeth replaceth */
1461 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1464 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1468 if (x->km.state != XFRM_STATE_VALID)
1471 spin_lock_bh(&x->lock);
1472 xfrm_update_ae_params(x, attrs);
1473 spin_unlock_bh(&x->lock);
1475 c.event = nlh->nlmsg_type;
1476 c.seq = nlh->nlmsg_seq;
1477 c.pid = nlh->nlmsg_pid;
1478 c.data.aevent = XFRM_AE_CU;
1479 km_state_notify(x, &c);
1486 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1487 struct nlattr **attrs)
1490 u8 type = XFRM_POLICY_TYPE_MAIN;
1492 struct xfrm_audit audit_info;
1494 err = copy_from_user_policy_type(&type, attrs);
1498 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1499 audit_info.secid = NETLINK_CB(skb).sid;
1500 err = xfrm_policy_flush(type, &audit_info);
1504 c.event = nlh->nlmsg_type;
1505 c.seq = nlh->nlmsg_seq;
1506 c.pid = nlh->nlmsg_pid;
1507 km_policy_notify(NULL, 0, &c);
1511 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1512 struct nlattr **attrs)
1514 struct xfrm_policy *xp;
1515 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1516 struct xfrm_userpolicy_info *p = &up->pol;
1517 u8 type = XFRM_POLICY_TYPE_MAIN;
1520 err = copy_from_user_policy_type(&type, attrs);
1525 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1527 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1528 struct xfrm_policy tmp;
1530 err = verify_sec_ctx_len(attrs);
1534 memset(&tmp, 0, sizeof(struct xfrm_policy));
1536 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1538 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1541 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1543 security_xfrm_policy_free(&tmp);
1548 read_lock(&xp->lock);
1550 read_unlock(&xp->lock);
1554 read_unlock(&xp->lock);
1557 xfrm_policy_delete(xp, p->dir);
1558 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1559 AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
1562 // reset the timers here?
1563 printk("Dont know what to do with soft policy expire\n");
1565 km_policy_expired(xp, p->dir, up->hard, current->pid);
1572 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1573 struct nlattr **attrs)
1575 struct xfrm_state *x;
1577 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1578 struct xfrm_usersa_info *p = &ue->state;
1580 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1586 spin_lock_bh(&x->lock);
1588 if (x->km.state != XFRM_STATE_VALID)
1590 km_state_expired(x, ue->hard, current->pid);
1593 __xfrm_state_delete(x);
1594 xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
1595 AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
1599 spin_unlock_bh(&x->lock);
1604 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1605 struct nlattr **attrs)
1607 struct xfrm_policy *xp;
1608 struct xfrm_user_tmpl *ut;
1610 struct nlattr *rt = attrs[XFRMA_TMPL];
1612 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1613 struct xfrm_state *x = xfrm_state_alloc();
1619 err = verify_newpolicy_info(&ua->policy);
1621 printk("BAD policy passed\n");
1627 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1633 memcpy(&x->id, &ua->id, sizeof(ua->id));
1634 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1635 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1638 /* extract the templates and for each call km_key */
1639 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1640 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1641 memcpy(&x->id, &t->id, sizeof(x->id));
1642 x->props.mode = t->mode;
1643 x->props.reqid = t->reqid;
1644 x->props.family = ut->family;
1645 t->aalgos = ua->aalgos;
1646 t->ealgos = ua->ealgos;
1647 t->calgos = ua->calgos;
1648 err = km_query(x, t, xp);
1658 #ifdef CONFIG_XFRM_MIGRATE
1659 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1660 struct nlattr **attrs, int *num)
1662 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1663 struct xfrm_user_migrate *um;
1667 num_migrate = nla_len(rt) / sizeof(*um);
1669 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1672 for (i = 0; i < num_migrate; i++, um++, ma++) {
1673 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1674 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1675 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1676 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1678 ma->proto = um->proto;
1679 ma->mode = um->mode;
1680 ma->reqid = um->reqid;
1682 ma->old_family = um->old_family;
1683 ma->new_family = um->new_family;
1690 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1691 struct nlattr **attrs)
1693 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1694 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1699 if (attrs[XFRMA_MIGRATE] == NULL)
1702 err = copy_from_user_policy_type(&type, attrs);
1706 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1714 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1719 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1720 struct nlattr **attrs)
1722 return -ENOPROTOOPT;
1726 #ifdef CONFIG_XFRM_MIGRATE
1727 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1729 struct xfrm_user_migrate um;
1731 memset(&um, 0, sizeof(um));
1732 um.proto = m->proto;
1734 um.reqid = m->reqid;
1735 um.old_family = m->old_family;
1736 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1737 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1738 um.new_family = m->new_family;
1739 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1740 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1742 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1745 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1747 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1748 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1749 + userpolicy_type_attrsize();
1752 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1753 int num_migrate, struct xfrm_selector *sel,
1756 struct xfrm_migrate *mp;
1757 struct xfrm_userpolicy_id *pol_id;
1758 struct nlmsghdr *nlh;
1761 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1765 pol_id = nlmsg_data(nlh);
1766 /* copy data from selector, dir, and type to the pol_id */
1767 memset(pol_id, 0, sizeof(*pol_id));
1768 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1771 if (copy_to_user_policy_type(type, skb) < 0)
1774 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1775 if (copy_to_user_migrate(mp, skb) < 0)
1779 return nlmsg_end(skb, nlh);
1781 nlmsg_cancel(skb, nlh);
1785 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1786 struct xfrm_migrate *m, int num_migrate)
1788 struct sk_buff *skb;
1790 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1795 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1798 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1801 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1802 struct xfrm_migrate *m, int num_migrate)
1804 return -ENOPROTOOPT;
1808 #define XMSGSIZE(type) sizeof(struct type)
1810 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1811 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1812 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1813 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1814 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1815 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1816 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1817 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1818 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1819 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1820 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1821 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1822 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1823 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1824 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1825 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1826 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1827 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1828 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1829 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1830 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
1835 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1836 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1837 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1838 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1839 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1840 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1841 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1842 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1843 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1844 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1845 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1846 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1847 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1848 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1849 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1852 static struct xfrm_link {
1853 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1854 int (*dump)(struct sk_buff *, struct netlink_callback *);
1855 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1856 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1857 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1858 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1859 .dump = xfrm_dump_sa },
1860 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1861 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1862 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1863 .dump = xfrm_dump_policy },
1864 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1865 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1866 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1867 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1868 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1869 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1870 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1871 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1872 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1873 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1874 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1875 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
1876 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1879 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1881 struct nlattr *attrs[XFRMA_MAX+1];
1882 struct xfrm_link *link;
1885 type = nlh->nlmsg_type;
1886 if (type > XFRM_MSG_MAX)
1889 type -= XFRM_MSG_BASE;
1890 link = &xfrm_dispatch[type];
1892 /* All operations require privileges, even GET */
1893 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1896 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1897 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1898 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1899 if (link->dump == NULL)
1902 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
1905 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1910 if (link->doit == NULL)
1913 return link->doit(skb, nlh, attrs);
1916 static void xfrm_netlink_rcv(struct sock *sk, int len)
1918 unsigned int qlen = 0;
1921 mutex_lock(&xfrm_cfg_mutex);
1922 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1923 mutex_unlock(&xfrm_cfg_mutex);
1928 static inline size_t xfrm_expire_msgsize(void)
1930 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1933 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1935 struct xfrm_user_expire *ue;
1936 struct nlmsghdr *nlh;
1938 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1942 ue = nlmsg_data(nlh);
1943 copy_to_user_state(x, &ue->state);
1944 ue->hard = (c->data.hard != 0) ? 1 : 0;
1946 return nlmsg_end(skb, nlh);
1949 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1951 struct sk_buff *skb;
1953 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
1957 if (build_expire(skb, x, c) < 0)
1960 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1963 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1965 struct sk_buff *skb;
1967 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1971 if (build_aevent(skb, x, c) < 0)
1974 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1977 static int xfrm_notify_sa_flush(struct km_event *c)
1979 struct xfrm_usersa_flush *p;
1980 struct nlmsghdr *nlh;
1981 struct sk_buff *skb;
1982 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
1984 skb = nlmsg_new(len, GFP_ATOMIC);
1988 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1994 p = nlmsg_data(nlh);
1995 p->proto = c->data.proto;
1997 nlmsg_end(skb, nlh);
1999 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2002 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2006 l += nla_total_size(alg_len(x->aalg));
2008 l += nla_total_size(alg_len(x->ealg));
2010 l += nla_total_size(sizeof(*x->calg));
2012 l += nla_total_size(sizeof(*x->encap));
2017 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2019 struct xfrm_usersa_info *p;
2020 struct xfrm_usersa_id *id;
2021 struct nlmsghdr *nlh;
2022 struct sk_buff *skb;
2023 int len = xfrm_sa_len(x);
2026 headlen = sizeof(*p);
2027 if (c->event == XFRM_MSG_DELSA) {
2028 len += nla_total_size(headlen);
2029 headlen = sizeof(*id);
2031 len += NLMSG_ALIGN(headlen);
2033 skb = nlmsg_new(len, GFP_ATOMIC);
2037 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2039 goto nla_put_failure;
2041 p = nlmsg_data(nlh);
2042 if (c->event == XFRM_MSG_DELSA) {
2043 struct nlattr *attr;
2045 id = nlmsg_data(nlh);
2046 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2047 id->spi = x->id.spi;
2048 id->family = x->props.family;
2049 id->proto = x->id.proto;
2051 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2053 goto nla_put_failure;
2058 copy_to_user_state(x, p);
2061 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
2063 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
2065 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2068 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2070 nlmsg_end(skb, nlh);
2072 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2079 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2083 case XFRM_MSG_EXPIRE:
2084 return xfrm_exp_state_notify(x, c);
2085 case XFRM_MSG_NEWAE:
2086 return xfrm_aevent_state_notify(x, c);
2087 case XFRM_MSG_DELSA:
2088 case XFRM_MSG_UPDSA:
2089 case XFRM_MSG_NEWSA:
2090 return xfrm_notify_sa(x, c);
2091 case XFRM_MSG_FLUSHSA:
2092 return xfrm_notify_sa_flush(c);
2094 printk("xfrm_user: Unknown SA event %d\n", c->event);
2102 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2103 struct xfrm_policy *xp)
2105 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2106 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2107 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2108 + userpolicy_type_attrsize();
2111 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2112 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2115 struct xfrm_user_acquire *ua;
2116 struct nlmsghdr *nlh;
2117 __u32 seq = xfrm_get_acqseq();
2119 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2123 ua = nlmsg_data(nlh);
2124 memcpy(&ua->id, &x->id, sizeof(ua->id));
2125 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2126 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2127 copy_to_user_policy(xp, &ua->policy, dir);
2128 ua->aalgos = xt->aalgos;
2129 ua->ealgos = xt->ealgos;
2130 ua->calgos = xt->calgos;
2131 ua->seq = x->km.seq = seq;
2133 if (copy_to_user_tmpl(xp, skb) < 0)
2135 if (copy_to_user_state_sec_ctx(x, skb))
2137 if (copy_to_user_policy_type(xp->type, skb) < 0)
2140 return nlmsg_end(skb, nlh);
2143 nlmsg_cancel(skb, nlh);
2147 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2148 struct xfrm_policy *xp, int dir)
2150 struct sk_buff *skb;
2152 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2156 if (build_acquire(skb, x, xt, xp, dir) < 0)
2159 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2162 /* User gives us xfrm_user_policy_info followed by an array of 0
2163 * or more templates.
2165 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2166 u8 *data, int len, int *dir)
2168 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2169 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2170 struct xfrm_policy *xp;
2173 switch (sk->sk_family) {
2175 if (opt != IP_XFRM_POLICY) {
2180 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2182 if (opt != IPV6_XFRM_POLICY) {
2195 if (len < sizeof(*p) ||
2196 verify_newpolicy_info(p))
2199 nr = ((len - sizeof(*p)) / sizeof(*ut));
2200 if (validate_tmpl(nr, ut, p->sel.family))
2203 if (p->dir > XFRM_POLICY_OUT)
2206 xp = xfrm_policy_alloc(GFP_KERNEL);
2212 copy_from_user_policy(xp, p);
2213 xp->type = XFRM_POLICY_TYPE_MAIN;
2214 copy_templates(xp, ut, nr);
2221 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2223 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2224 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2225 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2226 + userpolicy_type_attrsize();
2229 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2230 int dir, struct km_event *c)
2232 struct xfrm_user_polexpire *upe;
2233 struct nlmsghdr *nlh;
2234 int hard = c->data.hard;
2236 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2240 upe = nlmsg_data(nlh);
2241 copy_to_user_policy(xp, &upe->pol, dir);
2242 if (copy_to_user_tmpl(xp, skb) < 0)
2244 if (copy_to_user_sec_ctx(xp, skb))
2246 if (copy_to_user_policy_type(xp->type, skb) < 0)
2250 return nlmsg_end(skb, nlh);
2253 nlmsg_cancel(skb, nlh);
2257 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2259 struct sk_buff *skb;
2261 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2265 if (build_polexpire(skb, xp, dir, c) < 0)
2268 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2271 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2273 struct xfrm_userpolicy_info *p;
2274 struct xfrm_userpolicy_id *id;
2275 struct nlmsghdr *nlh;
2276 struct sk_buff *skb;
2277 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2280 headlen = sizeof(*p);
2281 if (c->event == XFRM_MSG_DELPOLICY) {
2282 len += nla_total_size(headlen);
2283 headlen = sizeof(*id);
2285 len += userpolicy_type_attrsize();
2286 len += NLMSG_ALIGN(headlen);
2288 skb = nlmsg_new(len, GFP_ATOMIC);
2292 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2296 p = nlmsg_data(nlh);
2297 if (c->event == XFRM_MSG_DELPOLICY) {
2298 struct nlattr *attr;
2300 id = nlmsg_data(nlh);
2301 memset(id, 0, sizeof(*id));
2304 id->index = xp->index;
2306 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2308 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2315 copy_to_user_policy(xp, p, dir);
2316 if (copy_to_user_tmpl(xp, skb) < 0)
2318 if (copy_to_user_policy_type(xp->type, skb) < 0)
2321 nlmsg_end(skb, nlh);
2323 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2330 static int xfrm_notify_policy_flush(struct km_event *c)
2332 struct nlmsghdr *nlh;
2333 struct sk_buff *skb;
2335 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2339 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2342 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2345 nlmsg_end(skb, nlh);
2347 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2354 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2358 case XFRM_MSG_NEWPOLICY:
2359 case XFRM_MSG_UPDPOLICY:
2360 case XFRM_MSG_DELPOLICY:
2361 return xfrm_notify_policy(xp, dir, c);
2362 case XFRM_MSG_FLUSHPOLICY:
2363 return xfrm_notify_policy_flush(c);
2364 case XFRM_MSG_POLEXPIRE:
2365 return xfrm_exp_policy_notify(xp, dir, c);
2367 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2374 static inline size_t xfrm_report_msgsize(void)
2376 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2379 static int build_report(struct sk_buff *skb, u8 proto,
2380 struct xfrm_selector *sel, xfrm_address_t *addr)
2382 struct xfrm_user_report *ur;
2383 struct nlmsghdr *nlh;
2385 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2389 ur = nlmsg_data(nlh);
2391 memcpy(&ur->sel, sel, sizeof(ur->sel));
2394 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2396 return nlmsg_end(skb, nlh);
2399 nlmsg_cancel(skb, nlh);
2403 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2404 xfrm_address_t *addr)
2406 struct sk_buff *skb;
2408 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2412 if (build_report(skb, proto, sel, addr) < 0)
2415 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2418 static struct xfrm_mgr netlink_mgr = {
2420 .notify = xfrm_send_state_notify,
2421 .acquire = xfrm_send_acquire,
2422 .compile_policy = xfrm_compile_policy,
2423 .notify_policy = xfrm_send_policy_notify,
2424 .report = xfrm_send_report,
2425 .migrate = xfrm_send_migrate,
2428 static int __init xfrm_user_init(void)
2432 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2434 nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
2435 xfrm_netlink_rcv, NULL, THIS_MODULE);
2438 rcu_assign_pointer(xfrm_nl, nlsk);
2440 xfrm_register_km(&netlink_mgr);
2445 static void __exit xfrm_user_exit(void)
2447 struct sock *nlsk = xfrm_nl;
2449 xfrm_unregister_km(&netlink_mgr);
2450 rcu_assign_pointer(xfrm_nl, NULL);
2452 sock_release(nlsk->sk_socket);
2455 module_init(xfrm_user_init);
2456 module_exit(xfrm_user_exit);
2457 MODULE_LICENSE("GPL");
2458 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);