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 aead_len(struct xfrm_algo_aead *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) < xfrm_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 int verify_aead(struct nlattr **attrs)
78 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
79 struct xfrm_algo_aead *algp;
85 if (nla_len(rt) < aead_len(algp))
88 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
92 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
93 xfrm_address_t **addrp)
95 struct nlattr *rt = attrs[type];
98 *addrp = nla_data(rt);
101 static inline int verify_sec_ctx_len(struct nlattr **attrs)
103 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
104 struct xfrm_user_sec_ctx *uctx;
110 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
117 static int verify_newsa_info(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
128 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
140 switch (p->id.proto) {
142 if (!attrs[XFRMA_ALG_AUTH] ||
143 attrs[XFRMA_ALG_AEAD] ||
144 attrs[XFRMA_ALG_CRYPT] ||
145 attrs[XFRMA_ALG_COMP])
150 if (attrs[XFRMA_ALG_COMP])
152 if (!attrs[XFRMA_ALG_AUTH] &&
153 !attrs[XFRMA_ALG_CRYPT] &&
154 !attrs[XFRMA_ALG_AEAD])
156 if ((attrs[XFRMA_ALG_AUTH] ||
157 attrs[XFRMA_ALG_CRYPT]) &&
158 attrs[XFRMA_ALG_AEAD])
163 if (!attrs[XFRMA_ALG_COMP] ||
164 attrs[XFRMA_ALG_AEAD] ||
165 attrs[XFRMA_ALG_AUTH] ||
166 attrs[XFRMA_ALG_CRYPT])
170 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171 case IPPROTO_DSTOPTS:
172 case IPPROTO_ROUTING:
173 if (attrs[XFRMA_ALG_COMP] ||
174 attrs[XFRMA_ALG_AUTH] ||
175 attrs[XFRMA_ALG_AEAD] ||
176 attrs[XFRMA_ALG_CRYPT] ||
177 attrs[XFRMA_ENCAP] ||
178 attrs[XFRMA_SEC_CTX] ||
179 !attrs[XFRMA_COADDR])
188 if ((err = verify_aead(attrs)))
190 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
192 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
194 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
196 if ((err = verify_sec_ctx_len(attrs)))
201 case XFRM_MODE_TRANSPORT:
202 case XFRM_MODE_TUNNEL:
203 case XFRM_MODE_ROUTEOPTIMIZATION:
217 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
218 struct xfrm_algo_desc *(*get_byname)(char *, int),
221 struct xfrm_algo *p, *ualg;
222 struct xfrm_algo_desc *algo;
227 ualg = nla_data(rta);
229 algo = get_byname(ualg->alg_name, 1);
232 *props = algo->desc.sadb_alg_id;
234 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
238 strcpy(p->alg_name, algo->name);
243 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
246 struct xfrm_algo_aead *p, *ualg;
247 struct xfrm_algo_desc *algo;
252 ualg = nla_data(rta);
254 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
257 *props = algo->desc.sadb_alg_id;
259 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
263 strcpy(p->alg_name, algo->name);
268 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
273 len += sizeof(struct xfrm_user_sec_ctx);
274 len += xfrm_ctx->ctx_len;
279 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
281 memcpy(&x->id, &p->id, sizeof(x->id));
282 memcpy(&x->sel, &p->sel, sizeof(x->sel));
283 memcpy(&x->lft, &p->lft, sizeof(x->lft));
284 x->props.mode = p->mode;
285 x->props.replay_window = p->replay_window;
286 x->props.reqid = p->reqid;
287 x->props.family = p->family;
288 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
289 x->props.flags = p->flags;
292 x->sel.family = p->family;
297 * someday when pfkey also has support, we could have the code
298 * somehow made shareable and move it to xfrm_state.c - JHS
301 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
303 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
304 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
305 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
306 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
309 struct xfrm_replay_state *replay;
310 replay = nla_data(rp);
311 memcpy(&x->replay, replay, sizeof(*replay));
312 memcpy(&x->preplay, replay, sizeof(*replay));
316 struct xfrm_lifetime_cur *ltime;
317 ltime = nla_data(lt);
318 x->curlft.bytes = ltime->bytes;
319 x->curlft.packets = ltime->packets;
320 x->curlft.add_time = ltime->add_time;
321 x->curlft.use_time = ltime->use_time;
325 x->replay_maxage = nla_get_u32(et);
328 x->replay_maxdiff = nla_get_u32(rt);
331 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
332 struct nlattr **attrs,
335 struct xfrm_state *x = xfrm_state_alloc();
341 copy_from_user_state(x, p);
343 if ((err = attach_aead(&x->aead, &x->props.ealgo,
344 attrs[XFRMA_ALG_AEAD])))
346 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
347 xfrm_aalg_get_byname,
348 attrs[XFRMA_ALG_AUTH])))
350 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
351 xfrm_ealg_get_byname,
352 attrs[XFRMA_ALG_CRYPT])))
354 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
355 xfrm_calg_get_byname,
356 attrs[XFRMA_ALG_COMP])))
359 if (attrs[XFRMA_ENCAP]) {
360 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
361 sizeof(*x->encap), GFP_KERNEL);
362 if (x->encap == NULL)
366 if (attrs[XFRMA_COADDR]) {
367 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
368 sizeof(*x->coaddr), GFP_KERNEL);
369 if (x->coaddr == NULL)
373 err = xfrm_init_state(x);
377 if (attrs[XFRMA_SEC_CTX] &&
378 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
382 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
383 /* sysctl_xfrm_aevent_etime is in 100ms units */
384 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
385 x->preplay.bitmap = 0;
386 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
387 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
389 /* override default values from above */
391 xfrm_update_ae_params(x, attrs);
396 x->km.state = XFRM_STATE_DEAD;
403 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
404 struct nlattr **attrs)
406 struct xfrm_usersa_info *p = nlmsg_data(nlh);
407 struct xfrm_state *x;
411 err = verify_newsa_info(p, attrs);
415 x = xfrm_state_construct(p, attrs, &err);
420 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
421 err = xfrm_state_add(x);
423 err = xfrm_state_update(x);
425 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
426 NETLINK_CB(skb).sid);
429 x->km.state = XFRM_STATE_DEAD;
434 c.seq = nlh->nlmsg_seq;
435 c.pid = nlh->nlmsg_pid;
436 c.event = nlh->nlmsg_type;
438 km_state_notify(x, &c);
444 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
445 struct nlattr **attrs,
448 struct xfrm_state *x = NULL;
451 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
453 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
455 xfrm_address_t *saddr = NULL;
457 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
464 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
474 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
475 struct nlattr **attrs)
477 struct xfrm_state *x;
480 struct xfrm_usersa_id *p = nlmsg_data(nlh);
482 x = xfrm_user_state_lookup(p, attrs, &err);
486 if ((err = security_xfrm_state_delete(x)) != 0)
489 if (xfrm_state_kern(x)) {
494 err = xfrm_state_delete(x);
499 c.seq = nlh->nlmsg_seq;
500 c.pid = nlh->nlmsg_pid;
501 c.event = nlh->nlmsg_type;
502 km_state_notify(x, &c);
505 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
506 NETLINK_CB(skb).sid);
511 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
513 memcpy(&p->id, &x->id, sizeof(p->id));
514 memcpy(&p->sel, &x->sel, sizeof(p->sel));
515 memcpy(&p->lft, &x->lft, sizeof(p->lft));
516 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
517 memcpy(&p->stats, &x->stats, sizeof(p->stats));
518 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
519 p->mode = x->props.mode;
520 p->replay_window = x->props.replay_window;
521 p->reqid = x->props.reqid;
522 p->family = x->props.family;
523 p->flags = x->props.flags;
527 struct xfrm_dump_info {
528 struct sk_buff *in_skb;
529 struct sk_buff *out_skb;
534 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
536 struct xfrm_user_sec_ctx *uctx;
538 int ctx_size = sizeof(*uctx) + s->ctx_len;
540 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
544 uctx = nla_data(attr);
545 uctx->exttype = XFRMA_SEC_CTX;
546 uctx->len = ctx_size;
547 uctx->ctx_doi = s->ctx_doi;
548 uctx->ctx_alg = s->ctx_alg;
549 uctx->ctx_len = s->ctx_len;
550 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
555 /* Don't change this without updating xfrm_sa_len! */
556 static int copy_to_user_state_extra(struct xfrm_state *x,
557 struct xfrm_usersa_info *p,
560 copy_to_user_state(x, p);
563 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
566 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
569 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
571 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
573 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
575 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
578 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
580 if (x->security && copy_sec_ctx(x->security, skb) < 0)
581 goto nla_put_failure;
589 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
591 struct xfrm_dump_info *sp = ptr;
592 struct sk_buff *in_skb = sp->in_skb;
593 struct sk_buff *skb = sp->out_skb;
594 struct xfrm_usersa_info *p;
595 struct nlmsghdr *nlh;
598 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
599 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
605 err = copy_to_user_state_extra(x, p, skb);
607 goto nla_put_failure;
613 nlmsg_cancel(skb, nlh);
617 static int xfrm_dump_sa_done(struct netlink_callback *cb)
619 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
620 xfrm_state_walk_done(walk);
624 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
626 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
627 struct xfrm_dump_info info;
629 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
630 sizeof(cb->args) - sizeof(cb->args[0]));
632 info.in_skb = cb->skb;
634 info.nlmsg_seq = cb->nlh->nlmsg_seq;
635 info.nlmsg_flags = NLM_F_MULTI;
639 xfrm_state_walk_init(walk, 0);
642 (void) xfrm_state_walk(walk, dump_one_state, &info);
647 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
648 struct xfrm_state *x, u32 seq)
650 struct xfrm_dump_info info;
653 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
655 return ERR_PTR(-ENOMEM);
657 info.in_skb = in_skb;
659 info.nlmsg_seq = seq;
660 info.nlmsg_flags = 0;
662 if (dump_one_state(x, 0, &info)) {
670 static inline size_t xfrm_spdinfo_msgsize(void)
672 return NLMSG_ALIGN(4)
673 + nla_total_size(sizeof(struct xfrmu_spdinfo))
674 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
677 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
679 struct xfrmk_spdinfo si;
680 struct xfrmu_spdinfo spc;
681 struct xfrmu_spdhinfo sph;
682 struct nlmsghdr *nlh;
685 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
686 if (nlh == NULL) /* shouldnt really happen ... */
691 xfrm_spd_getinfo(&si);
692 spc.incnt = si.incnt;
693 spc.outcnt = si.outcnt;
694 spc.fwdcnt = si.fwdcnt;
695 spc.inscnt = si.inscnt;
696 spc.outscnt = si.outscnt;
697 spc.fwdscnt = si.fwdscnt;
698 sph.spdhcnt = si.spdhcnt;
699 sph.spdhmcnt = si.spdhmcnt;
701 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
702 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
704 return nlmsg_end(skb, nlh);
707 nlmsg_cancel(skb, nlh);
711 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
712 struct nlattr **attrs)
714 struct sk_buff *r_skb;
715 u32 *flags = nlmsg_data(nlh);
716 u32 spid = NETLINK_CB(skb).pid;
717 u32 seq = nlh->nlmsg_seq;
719 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
723 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
726 return nlmsg_unicast(xfrm_nl, r_skb, spid);
729 static inline size_t xfrm_sadinfo_msgsize(void)
731 return NLMSG_ALIGN(4)
732 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
733 + nla_total_size(4); /* XFRMA_SAD_CNT */
736 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
738 struct xfrmk_sadinfo si;
739 struct xfrmu_sadhinfo sh;
740 struct nlmsghdr *nlh;
743 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
744 if (nlh == NULL) /* shouldnt really happen ... */
749 xfrm_sad_getinfo(&si);
751 sh.sadhmcnt = si.sadhmcnt;
752 sh.sadhcnt = si.sadhcnt;
754 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
755 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
757 return nlmsg_end(skb, nlh);
760 nlmsg_cancel(skb, nlh);
764 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
765 struct nlattr **attrs)
767 struct sk_buff *r_skb;
768 u32 *flags = nlmsg_data(nlh);
769 u32 spid = NETLINK_CB(skb).pid;
770 u32 seq = nlh->nlmsg_seq;
772 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
776 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
779 return nlmsg_unicast(xfrm_nl, r_skb, spid);
782 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
783 struct nlattr **attrs)
785 struct xfrm_usersa_id *p = nlmsg_data(nlh);
786 struct xfrm_state *x;
787 struct sk_buff *resp_skb;
790 x = xfrm_user_state_lookup(p, attrs, &err);
794 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
795 if (IS_ERR(resp_skb)) {
796 err = PTR_ERR(resp_skb);
798 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
805 static int verify_userspi_info(struct xfrm_userspi_info *p)
807 switch (p->info.id.proto) {
813 /* IPCOMP spi is 16-bits. */
814 if (p->max >= 0x10000)
828 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
829 struct nlattr **attrs)
831 struct xfrm_state *x;
832 struct xfrm_userspi_info *p;
833 struct sk_buff *resp_skb;
834 xfrm_address_t *daddr;
839 err = verify_userspi_info(p);
843 family = p->info.family;
844 daddr = &p->info.id.daddr;
848 x = xfrm_find_acq_byseq(p->info.seq);
849 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
856 x = xfrm_find_acq(p->info.mode, p->info.reqid,
857 p->info.id.proto, daddr,
864 err = xfrm_alloc_spi(x, p->min, p->max);
868 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
869 if (IS_ERR(resp_skb)) {
870 err = PTR_ERR(resp_skb);
874 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
882 static int verify_policy_dir(u8 dir)
886 case XFRM_POLICY_OUT:
887 case XFRM_POLICY_FWD:
897 static int verify_policy_type(u8 type)
900 case XFRM_POLICY_TYPE_MAIN:
901 #ifdef CONFIG_XFRM_SUB_POLICY
902 case XFRM_POLICY_TYPE_SUB:
913 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
917 case XFRM_SHARE_SESSION:
918 case XFRM_SHARE_USER:
919 case XFRM_SHARE_UNIQUE:
927 case XFRM_POLICY_ALLOW:
928 case XFRM_POLICY_BLOCK:
935 switch (p->sel.family) {
940 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
943 return -EAFNOSUPPORT;
950 return verify_policy_dir(p->dir);
953 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
955 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
956 struct xfrm_user_sec_ctx *uctx;
962 return security_xfrm_policy_alloc(&pol->security, uctx);
965 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
971 for (i = 0; i < nr; i++, ut++) {
972 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
974 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
975 memcpy(&t->saddr, &ut->saddr,
976 sizeof(xfrm_address_t));
977 t->reqid = ut->reqid;
979 t->share = ut->share;
980 t->optional = ut->optional;
981 t->aalgos = ut->aalgos;
982 t->ealgos = ut->ealgos;
983 t->calgos = ut->calgos;
984 /* If all masks are ~0, then we allow all algorithms. */
985 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
986 t->encap_family = ut->family;
990 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
994 if (nr > XFRM_MAX_DEPTH)
997 for (i = 0; i < nr; i++) {
998 /* We never validated the ut->family value, so many
999 * applications simply leave it at zero. The check was
1000 * never made and ut->family was ignored because all
1001 * templates could be assumed to have the same family as
1002 * the policy itself. Now that we will have ipv4-in-ipv6
1003 * and ipv6-in-ipv4 tunnels, this is no longer true.
1006 ut[i].family = family;
1008 switch (ut[i].family) {
1011 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1023 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1025 struct nlattr *rt = attrs[XFRMA_TMPL];
1030 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1031 int nr = nla_len(rt) / sizeof(*utmpl);
1034 err = validate_tmpl(nr, utmpl, pol->family);
1038 copy_templates(pol, utmpl, nr);
1043 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1045 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1046 struct xfrm_userpolicy_type *upt;
1047 u8 type = XFRM_POLICY_TYPE_MAIN;
1055 err = verify_policy_type(type);
1063 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1065 xp->priority = p->priority;
1066 xp->index = p->index;
1067 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1068 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1069 xp->action = p->action;
1070 xp->flags = p->flags;
1071 xp->family = p->sel.family;
1072 /* XXX xp->share = p->share; */
1075 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1077 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1078 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1079 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1080 p->priority = xp->priority;
1081 p->index = xp->index;
1082 p->sel.family = xp->family;
1084 p->action = xp->action;
1085 p->flags = xp->flags;
1086 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1089 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1091 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1099 copy_from_user_policy(xp, p);
1101 err = copy_from_user_policy_type(&xp->type, attrs);
1105 if (!(err = copy_from_user_tmpl(xp, attrs)))
1106 err = copy_from_user_sec_ctx(xp, attrs);
1114 xfrm_policy_destroy(xp);
1118 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1119 struct nlattr **attrs)
1121 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1122 struct xfrm_policy *xp;
1127 err = verify_newpolicy_info(p);
1130 err = verify_sec_ctx_len(attrs);
1134 xp = xfrm_policy_construct(p, attrs, &err);
1138 /* shouldnt excl be based on nlh flags??
1139 * Aha! this is anti-netlink really i.e more pfkey derived
1140 * in netlink excl is a flag and you wouldnt need
1141 * a type XFRM_MSG_UPDPOLICY - JHS */
1142 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1143 err = xfrm_policy_insert(p->dir, xp, excl);
1144 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1145 NETLINK_CB(skb).sid);
1148 security_xfrm_policy_free(xp->security);
1153 c.event = nlh->nlmsg_type;
1154 c.seq = nlh->nlmsg_seq;
1155 c.pid = nlh->nlmsg_pid;
1156 km_policy_notify(xp, p->dir, &c);
1163 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1165 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1168 if (xp->xfrm_nr == 0)
1171 for (i = 0; i < xp->xfrm_nr; i++) {
1172 struct xfrm_user_tmpl *up = &vec[i];
1173 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1175 memcpy(&up->id, &kp->id, sizeof(up->id));
1176 up->family = kp->encap_family;
1177 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1178 up->reqid = kp->reqid;
1179 up->mode = kp->mode;
1180 up->share = kp->share;
1181 up->optional = kp->optional;
1182 up->aalgos = kp->aalgos;
1183 up->ealgos = kp->ealgos;
1184 up->calgos = kp->calgos;
1187 return nla_put(skb, XFRMA_TMPL,
1188 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1191 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1194 return copy_sec_ctx(x->security, skb);
1199 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1202 return copy_sec_ctx(xp->security, skb);
1206 static inline size_t userpolicy_type_attrsize(void)
1208 #ifdef CONFIG_XFRM_SUB_POLICY
1209 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1215 #ifdef CONFIG_XFRM_SUB_POLICY
1216 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1218 struct xfrm_userpolicy_type upt = {
1222 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1226 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1232 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1234 struct xfrm_dump_info *sp = ptr;
1235 struct xfrm_userpolicy_info *p;
1236 struct sk_buff *in_skb = sp->in_skb;
1237 struct sk_buff *skb = sp->out_skb;
1238 struct nlmsghdr *nlh;
1240 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1241 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1245 p = nlmsg_data(nlh);
1246 copy_to_user_policy(xp, p, dir);
1247 if (copy_to_user_tmpl(xp, skb) < 0)
1249 if (copy_to_user_sec_ctx(xp, skb))
1251 if (copy_to_user_policy_type(xp->type, skb) < 0)
1254 nlmsg_end(skb, nlh);
1258 nlmsg_cancel(skb, nlh);
1262 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1264 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1266 xfrm_policy_walk_done(walk);
1270 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1272 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1273 struct xfrm_dump_info info;
1275 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1276 sizeof(cb->args) - sizeof(cb->args[0]));
1278 info.in_skb = cb->skb;
1280 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1281 info.nlmsg_flags = NLM_F_MULTI;
1285 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1288 (void) xfrm_policy_walk(walk, dump_one_policy, &info);
1293 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1294 struct xfrm_policy *xp,
1297 struct xfrm_dump_info info;
1298 struct sk_buff *skb;
1300 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1302 return ERR_PTR(-ENOMEM);
1304 info.in_skb = in_skb;
1306 info.nlmsg_seq = seq;
1307 info.nlmsg_flags = 0;
1309 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1317 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1318 struct nlattr **attrs)
1320 struct xfrm_policy *xp;
1321 struct xfrm_userpolicy_id *p;
1322 u8 type = XFRM_POLICY_TYPE_MAIN;
1327 p = nlmsg_data(nlh);
1328 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1330 err = copy_from_user_policy_type(&type, attrs);
1334 err = verify_policy_dir(p->dir);
1339 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1341 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1342 struct xfrm_sec_ctx *ctx;
1344 err = verify_sec_ctx_len(attrs);
1350 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1352 err = security_xfrm_policy_alloc(&ctx, uctx);
1356 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx,
1358 security_xfrm_policy_free(ctx);
1364 struct sk_buff *resp_skb;
1366 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1367 if (IS_ERR(resp_skb)) {
1368 err = PTR_ERR(resp_skb);
1370 err = nlmsg_unicast(xfrm_nl, resp_skb,
1371 NETLINK_CB(skb).pid);
1374 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1375 NETLINK_CB(skb).loginuid,
1376 NETLINK_CB(skb).sid);
1381 c.data.byid = p->index;
1382 c.event = nlh->nlmsg_type;
1383 c.seq = nlh->nlmsg_seq;
1384 c.pid = nlh->nlmsg_pid;
1385 km_policy_notify(xp, p->dir, &c);
1393 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1394 struct nlattr **attrs)
1397 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1398 struct xfrm_audit audit_info;
1401 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1402 audit_info.secid = NETLINK_CB(skb).sid;
1403 err = xfrm_state_flush(p->proto, &audit_info);
1406 c.data.proto = p->proto;
1407 c.event = nlh->nlmsg_type;
1408 c.seq = nlh->nlmsg_seq;
1409 c.pid = nlh->nlmsg_pid;
1410 km_state_notify(NULL, &c);
1415 static inline size_t xfrm_aevent_msgsize(void)
1417 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1418 + nla_total_size(sizeof(struct xfrm_replay_state))
1419 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1420 + nla_total_size(4) /* XFRM_AE_RTHR */
1421 + nla_total_size(4); /* XFRM_AE_ETHR */
1424 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1426 struct xfrm_aevent_id *id;
1427 struct nlmsghdr *nlh;
1429 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1433 id = nlmsg_data(nlh);
1434 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1435 id->sa_id.spi = x->id.spi;
1436 id->sa_id.family = x->props.family;
1437 id->sa_id.proto = x->id.proto;
1438 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1439 id->reqid = x->props.reqid;
1440 id->flags = c->data.aevent;
1442 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1443 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1445 if (id->flags & XFRM_AE_RTHR)
1446 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1448 if (id->flags & XFRM_AE_ETHR)
1449 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1450 x->replay_maxage * 10 / HZ);
1452 return nlmsg_end(skb, nlh);
1455 nlmsg_cancel(skb, nlh);
1459 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1460 struct nlattr **attrs)
1462 struct xfrm_state *x;
1463 struct sk_buff *r_skb;
1466 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1467 struct xfrm_usersa_id *id = &p->sa_id;
1469 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1473 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1480 * XXX: is this lock really needed - none of the other
1481 * gets lock (the concern is things getting updated
1482 * while we are still reading) - jhs
1484 spin_lock_bh(&x->lock);
1485 c.data.aevent = p->flags;
1486 c.seq = nlh->nlmsg_seq;
1487 c.pid = nlh->nlmsg_pid;
1489 if (build_aevent(r_skb, x, &c) < 0)
1491 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1492 spin_unlock_bh(&x->lock);
1497 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1498 struct nlattr **attrs)
1500 struct xfrm_state *x;
1503 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1504 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1505 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1510 /* pedantic mode - thou shalt sayeth replaceth */
1511 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1514 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1518 if (x->km.state != XFRM_STATE_VALID)
1521 spin_lock_bh(&x->lock);
1522 xfrm_update_ae_params(x, attrs);
1523 spin_unlock_bh(&x->lock);
1525 c.event = nlh->nlmsg_type;
1526 c.seq = nlh->nlmsg_seq;
1527 c.pid = nlh->nlmsg_pid;
1528 c.data.aevent = XFRM_AE_CU;
1529 km_state_notify(x, &c);
1536 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1537 struct nlattr **attrs)
1540 u8 type = XFRM_POLICY_TYPE_MAIN;
1542 struct xfrm_audit audit_info;
1544 err = copy_from_user_policy_type(&type, attrs);
1548 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1549 audit_info.secid = NETLINK_CB(skb).sid;
1550 err = xfrm_policy_flush(type, &audit_info);
1554 c.event = nlh->nlmsg_type;
1555 c.seq = nlh->nlmsg_seq;
1556 c.pid = nlh->nlmsg_pid;
1557 km_policy_notify(NULL, 0, &c);
1561 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1562 struct nlattr **attrs)
1564 struct xfrm_policy *xp;
1565 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1566 struct xfrm_userpolicy_info *p = &up->pol;
1567 u8 type = XFRM_POLICY_TYPE_MAIN;
1570 err = copy_from_user_policy_type(&type, attrs);
1575 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1577 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1578 struct xfrm_sec_ctx *ctx;
1580 err = verify_sec_ctx_len(attrs);
1586 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1588 err = security_xfrm_policy_alloc(&ctx, uctx);
1592 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, 0, &err);
1593 security_xfrm_policy_free(ctx);
1598 read_lock(&xp->lock);
1600 read_unlock(&xp->lock);
1604 read_unlock(&xp->lock);
1607 xfrm_policy_delete(xp, p->dir);
1608 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1609 NETLINK_CB(skb).sid);
1612 // reset the timers here?
1613 printk("Dont know what to do with soft policy expire\n");
1615 km_policy_expired(xp, p->dir, up->hard, current->pid);
1622 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1623 struct nlattr **attrs)
1625 struct xfrm_state *x;
1627 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1628 struct xfrm_usersa_info *p = &ue->state;
1630 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1636 spin_lock_bh(&x->lock);
1638 if (x->km.state != XFRM_STATE_VALID)
1640 km_state_expired(x, ue->hard, current->pid);
1643 __xfrm_state_delete(x);
1644 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1645 NETLINK_CB(skb).sid);
1649 spin_unlock_bh(&x->lock);
1654 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1655 struct nlattr **attrs)
1657 struct xfrm_policy *xp;
1658 struct xfrm_user_tmpl *ut;
1660 struct nlattr *rt = attrs[XFRMA_TMPL];
1662 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1663 struct xfrm_state *x = xfrm_state_alloc();
1669 err = verify_newpolicy_info(&ua->policy);
1671 printk("BAD policy passed\n");
1677 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1683 memcpy(&x->id, &ua->id, sizeof(ua->id));
1684 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1685 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1688 /* extract the templates and for each call km_key */
1689 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1690 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1691 memcpy(&x->id, &t->id, sizeof(x->id));
1692 x->props.mode = t->mode;
1693 x->props.reqid = t->reqid;
1694 x->props.family = ut->family;
1695 t->aalgos = ua->aalgos;
1696 t->ealgos = ua->ealgos;
1697 t->calgos = ua->calgos;
1698 err = km_query(x, t, xp);
1708 #ifdef CONFIG_XFRM_MIGRATE
1709 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1710 struct nlattr **attrs, int *num)
1712 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1713 struct xfrm_user_migrate *um;
1717 num_migrate = nla_len(rt) / sizeof(*um);
1719 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1722 for (i = 0; i < num_migrate; i++, um++, ma++) {
1723 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1724 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1725 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1726 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1728 ma->proto = um->proto;
1729 ma->mode = um->mode;
1730 ma->reqid = um->reqid;
1732 ma->old_family = um->old_family;
1733 ma->new_family = um->new_family;
1740 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1741 struct nlattr **attrs)
1743 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1744 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1749 if (attrs[XFRMA_MIGRATE] == NULL)
1752 err = copy_from_user_policy_type(&type, attrs);
1756 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1764 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1769 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1770 struct nlattr **attrs)
1772 return -ENOPROTOOPT;
1776 #ifdef CONFIG_XFRM_MIGRATE
1777 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1779 struct xfrm_user_migrate um;
1781 memset(&um, 0, sizeof(um));
1782 um.proto = m->proto;
1784 um.reqid = m->reqid;
1785 um.old_family = m->old_family;
1786 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1787 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1788 um.new_family = m->new_family;
1789 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1790 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1792 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1795 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1797 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1798 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1799 + userpolicy_type_attrsize();
1802 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1803 int num_migrate, struct xfrm_selector *sel,
1806 struct xfrm_migrate *mp;
1807 struct xfrm_userpolicy_id *pol_id;
1808 struct nlmsghdr *nlh;
1811 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1815 pol_id = nlmsg_data(nlh);
1816 /* copy data from selector, dir, and type to the pol_id */
1817 memset(pol_id, 0, sizeof(*pol_id));
1818 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1821 if (copy_to_user_policy_type(type, skb) < 0)
1824 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1825 if (copy_to_user_migrate(mp, skb) < 0)
1829 return nlmsg_end(skb, nlh);
1831 nlmsg_cancel(skb, nlh);
1835 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1836 struct xfrm_migrate *m, int num_migrate)
1838 struct sk_buff *skb;
1840 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1845 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1848 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1851 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1852 struct xfrm_migrate *m, int num_migrate)
1854 return -ENOPROTOOPT;
1858 #define XMSGSIZE(type) sizeof(struct type)
1860 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1861 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1862 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1863 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1864 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1865 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1866 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1867 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1868 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1869 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1870 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1871 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1872 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1873 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1874 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1875 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1876 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1877 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1878 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1879 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1880 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
1885 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1886 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
1887 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1888 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1889 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1890 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1891 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1892 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1893 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1894 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1895 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1896 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1897 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1898 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1899 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1900 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1903 static struct xfrm_link {
1904 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1905 int (*dump)(struct sk_buff *, struct netlink_callback *);
1906 int (*done)(struct netlink_callback *);
1907 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1908 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1909 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1910 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1911 .dump = xfrm_dump_sa,
1912 .done = xfrm_dump_sa_done },
1913 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1914 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1915 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1916 .dump = xfrm_dump_policy,
1917 .done = xfrm_dump_policy_done },
1918 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1919 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1920 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1921 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1922 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1923 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1924 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1925 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1926 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1927 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1928 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1929 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
1930 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1933 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1935 struct nlattr *attrs[XFRMA_MAX+1];
1936 struct xfrm_link *link;
1939 type = nlh->nlmsg_type;
1940 if (type > XFRM_MSG_MAX)
1943 type -= XFRM_MSG_BASE;
1944 link = &xfrm_dispatch[type];
1946 /* All operations require privileges, even GET */
1947 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1950 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1951 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1952 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1953 if (link->dump == NULL)
1956 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
1959 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1964 if (link->doit == NULL)
1967 return link->doit(skb, nlh, attrs);
1970 static void xfrm_netlink_rcv(struct sk_buff *skb)
1972 mutex_lock(&xfrm_cfg_mutex);
1973 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1974 mutex_unlock(&xfrm_cfg_mutex);
1977 static inline size_t xfrm_expire_msgsize(void)
1979 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1982 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1984 struct xfrm_user_expire *ue;
1985 struct nlmsghdr *nlh;
1987 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1991 ue = nlmsg_data(nlh);
1992 copy_to_user_state(x, &ue->state);
1993 ue->hard = (c->data.hard != 0) ? 1 : 0;
1995 return nlmsg_end(skb, nlh);
1998 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
2000 struct sk_buff *skb;
2002 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2006 if (build_expire(skb, x, c) < 0)
2009 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2012 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2014 struct sk_buff *skb;
2016 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2020 if (build_aevent(skb, x, c) < 0)
2023 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2026 static int xfrm_notify_sa_flush(struct km_event *c)
2028 struct xfrm_usersa_flush *p;
2029 struct nlmsghdr *nlh;
2030 struct sk_buff *skb;
2031 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2033 skb = nlmsg_new(len, GFP_ATOMIC);
2037 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2043 p = nlmsg_data(nlh);
2044 p->proto = c->data.proto;
2046 nlmsg_end(skb, nlh);
2048 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2051 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2055 l += nla_total_size(aead_len(x->aead));
2057 l += nla_total_size(xfrm_alg_len(x->aalg));
2059 l += nla_total_size(xfrm_alg_len(x->ealg));
2061 l += nla_total_size(sizeof(*x->calg));
2063 l += nla_total_size(sizeof(*x->encap));
2065 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2066 x->security->ctx_len);
2068 l += nla_total_size(sizeof(*x->coaddr));
2070 /* Must count x->lastused as it may become non-zero behind our back. */
2071 l += nla_total_size(sizeof(u64));
2076 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2078 struct xfrm_usersa_info *p;
2079 struct xfrm_usersa_id *id;
2080 struct nlmsghdr *nlh;
2081 struct sk_buff *skb;
2082 int len = xfrm_sa_len(x);
2085 headlen = sizeof(*p);
2086 if (c->event == XFRM_MSG_DELSA) {
2087 len += nla_total_size(headlen);
2088 headlen = sizeof(*id);
2090 len += NLMSG_ALIGN(headlen);
2092 skb = nlmsg_new(len, GFP_ATOMIC);
2096 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2098 goto nla_put_failure;
2100 p = nlmsg_data(nlh);
2101 if (c->event == XFRM_MSG_DELSA) {
2102 struct nlattr *attr;
2104 id = nlmsg_data(nlh);
2105 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2106 id->spi = x->id.spi;
2107 id->family = x->props.family;
2108 id->proto = x->id.proto;
2110 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2112 goto nla_put_failure;
2117 if (copy_to_user_state_extra(x, p, skb))
2118 goto nla_put_failure;
2120 nlmsg_end(skb, nlh);
2122 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2125 /* Somebody screwed up with xfrm_sa_len! */
2131 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2135 case XFRM_MSG_EXPIRE:
2136 return xfrm_exp_state_notify(x, c);
2137 case XFRM_MSG_NEWAE:
2138 return xfrm_aevent_state_notify(x, c);
2139 case XFRM_MSG_DELSA:
2140 case XFRM_MSG_UPDSA:
2141 case XFRM_MSG_NEWSA:
2142 return xfrm_notify_sa(x, c);
2143 case XFRM_MSG_FLUSHSA:
2144 return xfrm_notify_sa_flush(c);
2146 printk("xfrm_user: Unknown SA event %d\n", c->event);
2154 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2155 struct xfrm_policy *xp)
2157 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2158 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2159 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2160 + userpolicy_type_attrsize();
2163 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2164 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2167 struct xfrm_user_acquire *ua;
2168 struct nlmsghdr *nlh;
2169 __u32 seq = xfrm_get_acqseq();
2171 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2175 ua = nlmsg_data(nlh);
2176 memcpy(&ua->id, &x->id, sizeof(ua->id));
2177 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2178 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2179 copy_to_user_policy(xp, &ua->policy, dir);
2180 ua->aalgos = xt->aalgos;
2181 ua->ealgos = xt->ealgos;
2182 ua->calgos = xt->calgos;
2183 ua->seq = x->km.seq = seq;
2185 if (copy_to_user_tmpl(xp, skb) < 0)
2187 if (copy_to_user_state_sec_ctx(x, skb))
2189 if (copy_to_user_policy_type(xp->type, skb) < 0)
2192 return nlmsg_end(skb, nlh);
2195 nlmsg_cancel(skb, nlh);
2199 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2200 struct xfrm_policy *xp, int dir)
2202 struct sk_buff *skb;
2204 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2208 if (build_acquire(skb, x, xt, xp, dir) < 0)
2211 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2214 /* User gives us xfrm_user_policy_info followed by an array of 0
2215 * or more templates.
2217 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2218 u8 *data, int len, int *dir)
2220 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2221 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2222 struct xfrm_policy *xp;
2225 switch (sk->sk_family) {
2227 if (opt != IP_XFRM_POLICY) {
2232 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2234 if (opt != IPV6_XFRM_POLICY) {
2247 if (len < sizeof(*p) ||
2248 verify_newpolicy_info(p))
2251 nr = ((len - sizeof(*p)) / sizeof(*ut));
2252 if (validate_tmpl(nr, ut, p->sel.family))
2255 if (p->dir > XFRM_POLICY_OUT)
2258 xp = xfrm_policy_alloc(GFP_KERNEL);
2264 copy_from_user_policy(xp, p);
2265 xp->type = XFRM_POLICY_TYPE_MAIN;
2266 copy_templates(xp, ut, nr);
2273 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2275 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2276 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2277 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2278 + userpolicy_type_attrsize();
2281 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2282 int dir, struct km_event *c)
2284 struct xfrm_user_polexpire *upe;
2285 struct nlmsghdr *nlh;
2286 int hard = c->data.hard;
2288 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2292 upe = nlmsg_data(nlh);
2293 copy_to_user_policy(xp, &upe->pol, dir);
2294 if (copy_to_user_tmpl(xp, skb) < 0)
2296 if (copy_to_user_sec_ctx(xp, skb))
2298 if (copy_to_user_policy_type(xp->type, skb) < 0)
2302 return nlmsg_end(skb, nlh);
2305 nlmsg_cancel(skb, nlh);
2309 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2311 struct sk_buff *skb;
2313 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2317 if (build_polexpire(skb, xp, dir, c) < 0)
2320 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2323 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2325 struct xfrm_userpolicy_info *p;
2326 struct xfrm_userpolicy_id *id;
2327 struct nlmsghdr *nlh;
2328 struct sk_buff *skb;
2329 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2332 headlen = sizeof(*p);
2333 if (c->event == XFRM_MSG_DELPOLICY) {
2334 len += nla_total_size(headlen);
2335 headlen = sizeof(*id);
2337 len += userpolicy_type_attrsize();
2338 len += NLMSG_ALIGN(headlen);
2340 skb = nlmsg_new(len, GFP_ATOMIC);
2344 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2348 p = nlmsg_data(nlh);
2349 if (c->event == XFRM_MSG_DELPOLICY) {
2350 struct nlattr *attr;
2352 id = nlmsg_data(nlh);
2353 memset(id, 0, sizeof(*id));
2356 id->index = xp->index;
2358 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2360 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2367 copy_to_user_policy(xp, p, dir);
2368 if (copy_to_user_tmpl(xp, skb) < 0)
2370 if (copy_to_user_policy_type(xp->type, skb) < 0)
2373 nlmsg_end(skb, nlh);
2375 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2382 static int xfrm_notify_policy_flush(struct km_event *c)
2384 struct nlmsghdr *nlh;
2385 struct sk_buff *skb;
2387 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2391 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2394 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2397 nlmsg_end(skb, nlh);
2399 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2406 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2410 case XFRM_MSG_NEWPOLICY:
2411 case XFRM_MSG_UPDPOLICY:
2412 case XFRM_MSG_DELPOLICY:
2413 return xfrm_notify_policy(xp, dir, c);
2414 case XFRM_MSG_FLUSHPOLICY:
2415 return xfrm_notify_policy_flush(c);
2416 case XFRM_MSG_POLEXPIRE:
2417 return xfrm_exp_policy_notify(xp, dir, c);
2419 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2426 static inline size_t xfrm_report_msgsize(void)
2428 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2431 static int build_report(struct sk_buff *skb, u8 proto,
2432 struct xfrm_selector *sel, xfrm_address_t *addr)
2434 struct xfrm_user_report *ur;
2435 struct nlmsghdr *nlh;
2437 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2441 ur = nlmsg_data(nlh);
2443 memcpy(&ur->sel, sel, sizeof(ur->sel));
2446 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2448 return nlmsg_end(skb, nlh);
2451 nlmsg_cancel(skb, nlh);
2455 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2456 xfrm_address_t *addr)
2458 struct sk_buff *skb;
2460 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2464 if (build_report(skb, proto, sel, addr) < 0)
2467 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2470 static struct xfrm_mgr netlink_mgr = {
2472 .notify = xfrm_send_state_notify,
2473 .acquire = xfrm_send_acquire,
2474 .compile_policy = xfrm_compile_policy,
2475 .notify_policy = xfrm_send_policy_notify,
2476 .report = xfrm_send_report,
2477 .migrate = xfrm_send_migrate,
2480 static int __init xfrm_user_init(void)
2484 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2486 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
2487 xfrm_netlink_rcv, NULL, THIS_MODULE);
2490 rcu_assign_pointer(xfrm_nl, nlsk);
2492 xfrm_register_km(&netlink_mgr);
2497 static void __exit xfrm_user_exit(void)
2499 struct sock *nlsk = xfrm_nl;
2501 xfrm_unregister_km(&netlink_mgr);
2502 rcu_assign_pointer(xfrm_nl, NULL);
2504 netlink_kernel_release(nlsk);
2507 module_init(xfrm_user_init);
2508 module_exit(xfrm_user_exit);
2509 MODULE_LICENSE("GPL");
2510 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);