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 int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
36 struct nlattr *rt = attrs[type];
37 struct xfrm_algo *algp;
43 if (nla_len(rt) < xfrm_alg_len(algp))
48 if (!algp->alg_key_len &&
49 strcmp(algp->alg_name, "digest_null") != 0)
54 if (!algp->alg_key_len &&
55 strcmp(algp->alg_name, "cipher_null") != 0)
60 /* Zero length keys are legal. */
67 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
71 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
72 xfrm_address_t **addrp)
74 struct nlattr *rt = attrs[type];
77 *addrp = nla_data(rt);
80 static inline int verify_sec_ctx_len(struct nlattr **attrs)
82 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
83 struct xfrm_user_sec_ctx *uctx;
89 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
96 static int verify_newsa_info(struct xfrm_usersa_info *p,
97 struct nlattr **attrs)
107 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
119 switch (p->id.proto) {
121 if (!attrs[XFRMA_ALG_AUTH] ||
122 attrs[XFRMA_ALG_CRYPT] ||
123 attrs[XFRMA_ALG_COMP])
128 if ((!attrs[XFRMA_ALG_AUTH] &&
129 !attrs[XFRMA_ALG_CRYPT]) ||
130 attrs[XFRMA_ALG_COMP])
135 if (!attrs[XFRMA_ALG_COMP] ||
136 attrs[XFRMA_ALG_AUTH] ||
137 attrs[XFRMA_ALG_CRYPT])
141 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
142 case IPPROTO_DSTOPTS:
143 case IPPROTO_ROUTING:
144 if (attrs[XFRMA_ALG_COMP] ||
145 attrs[XFRMA_ALG_AUTH] ||
146 attrs[XFRMA_ALG_CRYPT] ||
147 attrs[XFRMA_ENCAP] ||
148 attrs[XFRMA_SEC_CTX] ||
149 !attrs[XFRMA_COADDR])
158 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
160 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
162 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
164 if ((err = verify_sec_ctx_len(attrs)))
169 case XFRM_MODE_TRANSPORT:
170 case XFRM_MODE_TUNNEL:
171 case XFRM_MODE_ROUTEOPTIMIZATION:
185 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
186 struct xfrm_algo_desc *(*get_byname)(char *, int),
189 struct xfrm_algo *p, *ualg;
190 struct xfrm_algo_desc *algo;
195 ualg = nla_data(rta);
197 algo = get_byname(ualg->alg_name, 1);
200 *props = algo->desc.sadb_alg_id;
202 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
206 strcpy(p->alg_name, algo->name);
211 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
216 len += sizeof(struct xfrm_user_sec_ctx);
217 len += xfrm_ctx->ctx_len;
222 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
224 memcpy(&x->id, &p->id, sizeof(x->id));
225 memcpy(&x->sel, &p->sel, sizeof(x->sel));
226 memcpy(&x->lft, &p->lft, sizeof(x->lft));
227 x->props.mode = p->mode;
228 x->props.replay_window = p->replay_window;
229 x->props.reqid = p->reqid;
230 x->props.family = p->family;
231 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
232 x->props.flags = p->flags;
235 * Set inner address family if the KM left it as zero.
236 * See comment in validate_tmpl.
239 x->sel.family = p->family;
243 * someday when pfkey also has support, we could have the code
244 * somehow made shareable and move it to xfrm_state.c - JHS
247 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
249 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
250 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
251 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
252 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
255 struct xfrm_replay_state *replay;
256 replay = nla_data(rp);
257 memcpy(&x->replay, replay, sizeof(*replay));
258 memcpy(&x->preplay, replay, sizeof(*replay));
262 struct xfrm_lifetime_cur *ltime;
263 ltime = nla_data(lt);
264 x->curlft.bytes = ltime->bytes;
265 x->curlft.packets = ltime->packets;
266 x->curlft.add_time = ltime->add_time;
267 x->curlft.use_time = ltime->use_time;
271 x->replay_maxage = nla_get_u32(et);
274 x->replay_maxdiff = nla_get_u32(rt);
277 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
278 struct nlattr **attrs,
281 struct xfrm_state *x = xfrm_state_alloc();
287 copy_from_user_state(x, p);
289 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
290 xfrm_aalg_get_byname,
291 attrs[XFRMA_ALG_AUTH])))
293 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
294 xfrm_ealg_get_byname,
295 attrs[XFRMA_ALG_CRYPT])))
297 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
298 xfrm_calg_get_byname,
299 attrs[XFRMA_ALG_COMP])))
302 if (attrs[XFRMA_ENCAP]) {
303 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
304 sizeof(*x->encap), GFP_KERNEL);
305 if (x->encap == NULL)
309 if (attrs[XFRMA_COADDR]) {
310 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
311 sizeof(*x->coaddr), GFP_KERNEL);
312 if (x->coaddr == NULL)
316 err = xfrm_init_state(x);
320 if (attrs[XFRMA_SEC_CTX] &&
321 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
325 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
326 /* sysctl_xfrm_aevent_etime is in 100ms units */
327 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
328 x->preplay.bitmap = 0;
329 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
330 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
332 /* override default values from above */
334 xfrm_update_ae_params(x, attrs);
339 x->km.state = XFRM_STATE_DEAD;
346 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
347 struct nlattr **attrs)
349 struct xfrm_usersa_info *p = nlmsg_data(nlh);
350 struct xfrm_state *x;
354 err = verify_newsa_info(p, attrs);
358 x = xfrm_state_construct(p, attrs, &err);
363 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
364 err = xfrm_state_add(x);
366 err = xfrm_state_update(x);
368 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
369 NETLINK_CB(skb).sid);
372 x->km.state = XFRM_STATE_DEAD;
377 c.seq = nlh->nlmsg_seq;
378 c.pid = nlh->nlmsg_pid;
379 c.event = nlh->nlmsg_type;
381 km_state_notify(x, &c);
387 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
388 struct nlattr **attrs,
391 struct xfrm_state *x = NULL;
394 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
396 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
398 xfrm_address_t *saddr = NULL;
400 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
407 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
417 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
418 struct nlattr **attrs)
420 struct xfrm_state *x;
423 struct xfrm_usersa_id *p = nlmsg_data(nlh);
425 x = xfrm_user_state_lookup(p, attrs, &err);
429 if ((err = security_xfrm_state_delete(x)) != 0)
432 if (xfrm_state_kern(x)) {
437 err = xfrm_state_delete(x);
442 c.seq = nlh->nlmsg_seq;
443 c.pid = nlh->nlmsg_pid;
444 c.event = nlh->nlmsg_type;
445 km_state_notify(x, &c);
448 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
449 NETLINK_CB(skb).sid);
454 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
456 memcpy(&p->id, &x->id, sizeof(p->id));
457 memcpy(&p->sel, &x->sel, sizeof(p->sel));
458 memcpy(&p->lft, &x->lft, sizeof(p->lft));
459 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
460 memcpy(&p->stats, &x->stats, sizeof(p->stats));
461 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
462 p->mode = x->props.mode;
463 p->replay_window = x->props.replay_window;
464 p->reqid = x->props.reqid;
465 p->family = x->props.family;
466 p->flags = x->props.flags;
470 struct xfrm_dump_info {
471 struct sk_buff *in_skb;
472 struct sk_buff *out_skb;
479 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
481 struct xfrm_user_sec_ctx *uctx;
483 int ctx_size = sizeof(*uctx) + s->ctx_len;
485 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
489 uctx = nla_data(attr);
490 uctx->exttype = XFRMA_SEC_CTX;
491 uctx->len = ctx_size;
492 uctx->ctx_doi = s->ctx_doi;
493 uctx->ctx_alg = s->ctx_alg;
494 uctx->ctx_len = s->ctx_len;
495 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
500 /* Don't change this without updating xfrm_sa_len! */
501 static int copy_to_user_state_extra(struct xfrm_state *x,
502 struct xfrm_usersa_info *p,
505 copy_to_user_state(x, p);
508 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
511 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
514 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
516 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
518 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
521 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
523 if (x->security && copy_sec_ctx(x->security, skb) < 0)
524 goto nla_put_failure;
532 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
534 struct xfrm_dump_info *sp = ptr;
535 struct sk_buff *in_skb = sp->in_skb;
536 struct sk_buff *skb = sp->out_skb;
537 struct xfrm_usersa_info *p;
538 struct nlmsghdr *nlh;
541 if (sp->this_idx < sp->start_idx)
544 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
545 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
551 err = copy_to_user_state_extra(x, p, skb);
553 goto nla_put_failure;
561 nlmsg_cancel(skb, nlh);
565 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
567 struct xfrm_dump_info info;
569 info.in_skb = cb->skb;
571 info.nlmsg_seq = cb->nlh->nlmsg_seq;
572 info.nlmsg_flags = NLM_F_MULTI;
574 info.start_idx = cb->args[0];
575 (void) xfrm_state_walk(0, dump_one_state, &info);
576 cb->args[0] = info.this_idx;
581 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
582 struct xfrm_state *x, u32 seq)
584 struct xfrm_dump_info info;
587 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
589 return ERR_PTR(-ENOMEM);
591 info.in_skb = in_skb;
593 info.nlmsg_seq = seq;
594 info.nlmsg_flags = 0;
595 info.this_idx = info.start_idx = 0;
597 if (dump_one_state(x, 0, &info)) {
605 static inline size_t xfrm_spdinfo_msgsize(void)
607 return NLMSG_ALIGN(4)
608 + nla_total_size(sizeof(struct xfrmu_spdinfo))
609 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
612 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
614 struct xfrmk_spdinfo si;
615 struct xfrmu_spdinfo spc;
616 struct xfrmu_spdhinfo sph;
617 struct nlmsghdr *nlh;
620 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
621 if (nlh == NULL) /* shouldnt really happen ... */
626 xfrm_spd_getinfo(&si);
627 spc.incnt = si.incnt;
628 spc.outcnt = si.outcnt;
629 spc.fwdcnt = si.fwdcnt;
630 spc.inscnt = si.inscnt;
631 spc.outscnt = si.outscnt;
632 spc.fwdscnt = si.fwdscnt;
633 sph.spdhcnt = si.spdhcnt;
634 sph.spdhmcnt = si.spdhmcnt;
636 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
637 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
639 return nlmsg_end(skb, nlh);
642 nlmsg_cancel(skb, nlh);
646 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
647 struct nlattr **attrs)
649 struct sk_buff *r_skb;
650 u32 *flags = nlmsg_data(nlh);
651 u32 spid = NETLINK_CB(skb).pid;
652 u32 seq = nlh->nlmsg_seq;
654 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
658 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
661 return nlmsg_unicast(xfrm_nl, r_skb, spid);
664 static inline size_t xfrm_sadinfo_msgsize(void)
666 return NLMSG_ALIGN(4)
667 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
668 + nla_total_size(4); /* XFRMA_SAD_CNT */
671 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
673 struct xfrmk_sadinfo si;
674 struct xfrmu_sadhinfo sh;
675 struct nlmsghdr *nlh;
678 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
679 if (nlh == NULL) /* shouldnt really happen ... */
684 xfrm_sad_getinfo(&si);
686 sh.sadhmcnt = si.sadhmcnt;
687 sh.sadhcnt = si.sadhcnt;
689 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
690 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
692 return nlmsg_end(skb, nlh);
695 nlmsg_cancel(skb, nlh);
699 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
700 struct nlattr **attrs)
702 struct sk_buff *r_skb;
703 u32 *flags = nlmsg_data(nlh);
704 u32 spid = NETLINK_CB(skb).pid;
705 u32 seq = nlh->nlmsg_seq;
707 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
711 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
714 return nlmsg_unicast(xfrm_nl, r_skb, spid);
717 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
718 struct nlattr **attrs)
720 struct xfrm_usersa_id *p = nlmsg_data(nlh);
721 struct xfrm_state *x;
722 struct sk_buff *resp_skb;
725 x = xfrm_user_state_lookup(p, attrs, &err);
729 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
730 if (IS_ERR(resp_skb)) {
731 err = PTR_ERR(resp_skb);
733 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
740 static int verify_userspi_info(struct xfrm_userspi_info *p)
742 switch (p->info.id.proto) {
748 /* IPCOMP spi is 16-bits. */
749 if (p->max >= 0x10000)
763 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
764 struct nlattr **attrs)
766 struct xfrm_state *x;
767 struct xfrm_userspi_info *p;
768 struct sk_buff *resp_skb;
769 xfrm_address_t *daddr;
774 err = verify_userspi_info(p);
778 family = p->info.family;
779 daddr = &p->info.id.daddr;
783 x = xfrm_find_acq_byseq(p->info.seq);
784 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
791 x = xfrm_find_acq(p->info.mode, p->info.reqid,
792 p->info.id.proto, daddr,
799 err = xfrm_alloc_spi(x, p->min, p->max);
803 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
804 if (IS_ERR(resp_skb)) {
805 err = PTR_ERR(resp_skb);
809 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
817 static int verify_policy_dir(u8 dir)
821 case XFRM_POLICY_OUT:
822 case XFRM_POLICY_FWD:
832 static int verify_policy_type(u8 type)
835 case XFRM_POLICY_TYPE_MAIN:
836 #ifdef CONFIG_XFRM_SUB_POLICY
837 case XFRM_POLICY_TYPE_SUB:
848 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
852 case XFRM_SHARE_SESSION:
853 case XFRM_SHARE_USER:
854 case XFRM_SHARE_UNIQUE:
862 case XFRM_POLICY_ALLOW:
863 case XFRM_POLICY_BLOCK:
870 switch (p->sel.family) {
875 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
878 return -EAFNOSUPPORT;
885 return verify_policy_dir(p->dir);
888 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
890 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
891 struct xfrm_user_sec_ctx *uctx;
897 return security_xfrm_policy_alloc(pol, uctx);
900 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
906 for (i = 0; i < nr; i++, ut++) {
907 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
909 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
910 memcpy(&t->saddr, &ut->saddr,
911 sizeof(xfrm_address_t));
912 t->reqid = ut->reqid;
914 t->share = ut->share;
915 t->optional = ut->optional;
916 t->aalgos = ut->aalgos;
917 t->ealgos = ut->ealgos;
918 t->calgos = ut->calgos;
919 t->encap_family = ut->family;
923 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
927 if (nr > XFRM_MAX_DEPTH)
930 for (i = 0; i < nr; i++) {
931 /* We never validated the ut->family value, so many
932 * applications simply leave it at zero. The check was
933 * never made and ut->family was ignored because all
934 * templates could be assumed to have the same family as
935 * the policy itself. Now that we will have ipv4-in-ipv6
936 * and ipv6-in-ipv4 tunnels, this is no longer true.
939 ut[i].family = family;
941 switch (ut[i].family) {
944 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
956 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
958 struct nlattr *rt = attrs[XFRMA_TMPL];
963 struct xfrm_user_tmpl *utmpl = nla_data(rt);
964 int nr = nla_len(rt) / sizeof(*utmpl);
967 err = validate_tmpl(nr, utmpl, pol->family);
971 copy_templates(pol, utmpl, nr);
976 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
978 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
979 struct xfrm_userpolicy_type *upt;
980 u8 type = XFRM_POLICY_TYPE_MAIN;
988 err = verify_policy_type(type);
996 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
998 xp->priority = p->priority;
999 xp->index = p->index;
1000 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1001 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1002 xp->action = p->action;
1003 xp->flags = p->flags;
1004 xp->family = p->sel.family;
1005 /* XXX xp->share = p->share; */
1008 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1010 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1011 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1012 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1013 p->priority = xp->priority;
1014 p->index = xp->index;
1015 p->sel.family = xp->family;
1017 p->action = xp->action;
1018 p->flags = xp->flags;
1019 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1022 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1024 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1032 copy_from_user_policy(xp, p);
1034 err = copy_from_user_policy_type(&xp->type, attrs);
1038 if (!(err = copy_from_user_tmpl(xp, attrs)))
1039 err = copy_from_user_sec_ctx(xp, attrs);
1050 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1051 struct nlattr **attrs)
1053 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1054 struct xfrm_policy *xp;
1059 err = verify_newpolicy_info(p);
1062 err = verify_sec_ctx_len(attrs);
1066 xp = xfrm_policy_construct(p, attrs, &err);
1070 /* shouldnt excl be based on nlh flags??
1071 * Aha! this is anti-netlink really i.e more pfkey derived
1072 * in netlink excl is a flag and you wouldnt need
1073 * a type XFRM_MSG_UPDPOLICY - JHS */
1074 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1075 err = xfrm_policy_insert(p->dir, xp, excl);
1076 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1077 NETLINK_CB(skb).sid);
1080 security_xfrm_policy_free(xp);
1085 c.event = nlh->nlmsg_type;
1086 c.seq = nlh->nlmsg_seq;
1087 c.pid = nlh->nlmsg_pid;
1088 km_policy_notify(xp, p->dir, &c);
1095 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1097 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1100 if (xp->xfrm_nr == 0)
1103 for (i = 0; i < xp->xfrm_nr; i++) {
1104 struct xfrm_user_tmpl *up = &vec[i];
1105 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1107 memcpy(&up->id, &kp->id, sizeof(up->id));
1108 up->family = kp->encap_family;
1109 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1110 up->reqid = kp->reqid;
1111 up->mode = kp->mode;
1112 up->share = kp->share;
1113 up->optional = kp->optional;
1114 up->aalgos = kp->aalgos;
1115 up->ealgos = kp->ealgos;
1116 up->calgos = kp->calgos;
1119 return nla_put(skb, XFRMA_TMPL,
1120 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1123 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1126 return copy_sec_ctx(x->security, skb);
1131 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1134 return copy_sec_ctx(xp->security, skb);
1138 static inline size_t userpolicy_type_attrsize(void)
1140 #ifdef CONFIG_XFRM_SUB_POLICY
1141 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1147 #ifdef CONFIG_XFRM_SUB_POLICY
1148 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1150 struct xfrm_userpolicy_type upt = {
1154 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1158 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1164 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1166 struct xfrm_dump_info *sp = ptr;
1167 struct xfrm_userpolicy_info *p;
1168 struct sk_buff *in_skb = sp->in_skb;
1169 struct sk_buff *skb = sp->out_skb;
1170 struct nlmsghdr *nlh;
1172 if (sp->this_idx < sp->start_idx)
1175 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1176 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1180 p = nlmsg_data(nlh);
1181 copy_to_user_policy(xp, p, dir);
1182 if (copy_to_user_tmpl(xp, skb) < 0)
1184 if (copy_to_user_sec_ctx(xp, skb))
1186 if (copy_to_user_policy_type(xp->type, skb) < 0)
1189 nlmsg_end(skb, nlh);
1195 nlmsg_cancel(skb, nlh);
1199 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1201 struct xfrm_dump_info info;
1203 info.in_skb = cb->skb;
1205 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1206 info.nlmsg_flags = NLM_F_MULTI;
1208 info.start_idx = cb->args[0];
1209 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1210 #ifdef CONFIG_XFRM_SUB_POLICY
1211 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1213 cb->args[0] = info.this_idx;
1218 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1219 struct xfrm_policy *xp,
1222 struct xfrm_dump_info info;
1223 struct sk_buff *skb;
1225 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1227 return ERR_PTR(-ENOMEM);
1229 info.in_skb = in_skb;
1231 info.nlmsg_seq = seq;
1232 info.nlmsg_flags = 0;
1233 info.this_idx = info.start_idx = 0;
1235 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1243 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1244 struct nlattr **attrs)
1246 struct xfrm_policy *xp;
1247 struct xfrm_userpolicy_id *p;
1248 u8 type = XFRM_POLICY_TYPE_MAIN;
1253 p = nlmsg_data(nlh);
1254 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1256 err = copy_from_user_policy_type(&type, attrs);
1260 err = verify_policy_dir(p->dir);
1265 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1267 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1268 struct xfrm_policy tmp;
1270 err = verify_sec_ctx_len(attrs);
1274 memset(&tmp, 0, sizeof(struct xfrm_policy));
1276 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1278 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1281 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1283 security_xfrm_policy_free(&tmp);
1289 struct sk_buff *resp_skb;
1291 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1292 if (IS_ERR(resp_skb)) {
1293 err = PTR_ERR(resp_skb);
1295 err = nlmsg_unicast(xfrm_nl, resp_skb,
1296 NETLINK_CB(skb).pid);
1299 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1300 NETLINK_CB(skb).loginuid,
1301 NETLINK_CB(skb).sid);
1306 c.data.byid = p->index;
1307 c.event = nlh->nlmsg_type;
1308 c.seq = nlh->nlmsg_seq;
1309 c.pid = nlh->nlmsg_pid;
1310 km_policy_notify(xp, p->dir, &c);
1318 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1319 struct nlattr **attrs)
1322 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1323 struct xfrm_audit audit_info;
1326 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1327 audit_info.secid = NETLINK_CB(skb).sid;
1328 err = xfrm_state_flush(p->proto, &audit_info);
1331 c.data.proto = p->proto;
1332 c.event = nlh->nlmsg_type;
1333 c.seq = nlh->nlmsg_seq;
1334 c.pid = nlh->nlmsg_pid;
1335 km_state_notify(NULL, &c);
1340 static inline size_t xfrm_aevent_msgsize(void)
1342 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1343 + nla_total_size(sizeof(struct xfrm_replay_state))
1344 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1345 + nla_total_size(4) /* XFRM_AE_RTHR */
1346 + nla_total_size(4); /* XFRM_AE_ETHR */
1349 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1351 struct xfrm_aevent_id *id;
1352 struct nlmsghdr *nlh;
1354 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1358 id = nlmsg_data(nlh);
1359 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1360 id->sa_id.spi = x->id.spi;
1361 id->sa_id.family = x->props.family;
1362 id->sa_id.proto = x->id.proto;
1363 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1364 id->reqid = x->props.reqid;
1365 id->flags = c->data.aevent;
1367 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1368 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1370 if (id->flags & XFRM_AE_RTHR)
1371 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1373 if (id->flags & XFRM_AE_ETHR)
1374 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1375 x->replay_maxage * 10 / HZ);
1377 return nlmsg_end(skb, nlh);
1380 nlmsg_cancel(skb, nlh);
1384 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1385 struct nlattr **attrs)
1387 struct xfrm_state *x;
1388 struct sk_buff *r_skb;
1391 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1392 struct xfrm_usersa_id *id = &p->sa_id;
1394 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1398 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1405 * XXX: is this lock really needed - none of the other
1406 * gets lock (the concern is things getting updated
1407 * while we are still reading) - jhs
1409 spin_lock_bh(&x->lock);
1410 c.data.aevent = p->flags;
1411 c.seq = nlh->nlmsg_seq;
1412 c.pid = nlh->nlmsg_pid;
1414 if (build_aevent(r_skb, x, &c) < 0)
1416 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1417 spin_unlock_bh(&x->lock);
1422 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1423 struct nlattr **attrs)
1425 struct xfrm_state *x;
1428 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1429 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1430 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1435 /* pedantic mode - thou shalt sayeth replaceth */
1436 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1439 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1443 if (x->km.state != XFRM_STATE_VALID)
1446 spin_lock_bh(&x->lock);
1447 xfrm_update_ae_params(x, attrs);
1448 spin_unlock_bh(&x->lock);
1450 c.event = nlh->nlmsg_type;
1451 c.seq = nlh->nlmsg_seq;
1452 c.pid = nlh->nlmsg_pid;
1453 c.data.aevent = XFRM_AE_CU;
1454 km_state_notify(x, &c);
1461 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1462 struct nlattr **attrs)
1465 u8 type = XFRM_POLICY_TYPE_MAIN;
1467 struct xfrm_audit audit_info;
1469 err = copy_from_user_policy_type(&type, attrs);
1473 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1474 audit_info.secid = NETLINK_CB(skb).sid;
1475 err = xfrm_policy_flush(type, &audit_info);
1479 c.event = nlh->nlmsg_type;
1480 c.seq = nlh->nlmsg_seq;
1481 c.pid = nlh->nlmsg_pid;
1482 km_policy_notify(NULL, 0, &c);
1486 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1487 struct nlattr **attrs)
1489 struct xfrm_policy *xp;
1490 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1491 struct xfrm_userpolicy_info *p = &up->pol;
1492 u8 type = XFRM_POLICY_TYPE_MAIN;
1495 err = copy_from_user_policy_type(&type, attrs);
1500 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1502 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1503 struct xfrm_policy tmp;
1505 err = verify_sec_ctx_len(attrs);
1509 memset(&tmp, 0, sizeof(struct xfrm_policy));
1511 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1513 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1516 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1518 security_xfrm_policy_free(&tmp);
1523 read_lock(&xp->lock);
1525 read_unlock(&xp->lock);
1529 read_unlock(&xp->lock);
1532 xfrm_policy_delete(xp, p->dir);
1533 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1534 NETLINK_CB(skb).sid);
1537 // reset the timers here?
1538 printk("Dont know what to do with soft policy expire\n");
1540 km_policy_expired(xp, p->dir, up->hard, current->pid);
1547 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1548 struct nlattr **attrs)
1550 struct xfrm_state *x;
1552 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1553 struct xfrm_usersa_info *p = &ue->state;
1555 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1561 spin_lock_bh(&x->lock);
1563 if (x->km.state != XFRM_STATE_VALID)
1565 km_state_expired(x, ue->hard, current->pid);
1568 __xfrm_state_delete(x);
1569 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1570 NETLINK_CB(skb).sid);
1574 spin_unlock_bh(&x->lock);
1579 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1580 struct nlattr **attrs)
1582 struct xfrm_policy *xp;
1583 struct xfrm_user_tmpl *ut;
1585 struct nlattr *rt = attrs[XFRMA_TMPL];
1587 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1588 struct xfrm_state *x = xfrm_state_alloc();
1594 err = verify_newpolicy_info(&ua->policy);
1596 printk("BAD policy passed\n");
1602 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1608 memcpy(&x->id, &ua->id, sizeof(ua->id));
1609 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1610 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1613 /* extract the templates and for each call km_key */
1614 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1615 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1616 memcpy(&x->id, &t->id, sizeof(x->id));
1617 x->props.mode = t->mode;
1618 x->props.reqid = t->reqid;
1619 x->props.family = ut->family;
1620 t->aalgos = ua->aalgos;
1621 t->ealgos = ua->ealgos;
1622 t->calgos = ua->calgos;
1623 err = km_query(x, t, xp);
1633 #ifdef CONFIG_XFRM_MIGRATE
1634 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1635 struct nlattr **attrs, int *num)
1637 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1638 struct xfrm_user_migrate *um;
1642 num_migrate = nla_len(rt) / sizeof(*um);
1644 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1647 for (i = 0; i < num_migrate; i++, um++, ma++) {
1648 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1649 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1650 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1651 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1653 ma->proto = um->proto;
1654 ma->mode = um->mode;
1655 ma->reqid = um->reqid;
1657 ma->old_family = um->old_family;
1658 ma->new_family = um->new_family;
1665 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1666 struct nlattr **attrs)
1668 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1669 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1674 if (attrs[XFRMA_MIGRATE] == NULL)
1677 err = copy_from_user_policy_type(&type, attrs);
1681 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1689 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1694 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1695 struct nlattr **attrs)
1697 return -ENOPROTOOPT;
1701 #ifdef CONFIG_XFRM_MIGRATE
1702 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1704 struct xfrm_user_migrate um;
1706 memset(&um, 0, sizeof(um));
1707 um.proto = m->proto;
1709 um.reqid = m->reqid;
1710 um.old_family = m->old_family;
1711 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1712 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1713 um.new_family = m->new_family;
1714 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1715 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1717 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1720 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1722 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1723 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1724 + userpolicy_type_attrsize();
1727 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1728 int num_migrate, struct xfrm_selector *sel,
1731 struct xfrm_migrate *mp;
1732 struct xfrm_userpolicy_id *pol_id;
1733 struct nlmsghdr *nlh;
1736 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1740 pol_id = nlmsg_data(nlh);
1741 /* copy data from selector, dir, and type to the pol_id */
1742 memset(pol_id, 0, sizeof(*pol_id));
1743 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1746 if (copy_to_user_policy_type(type, skb) < 0)
1749 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1750 if (copy_to_user_migrate(mp, skb) < 0)
1754 return nlmsg_end(skb, nlh);
1756 nlmsg_cancel(skb, nlh);
1760 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1761 struct xfrm_migrate *m, int num_migrate)
1763 struct sk_buff *skb;
1765 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1770 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1773 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1776 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1777 struct xfrm_migrate *m, int num_migrate)
1779 return -ENOPROTOOPT;
1783 #define XMSGSIZE(type) sizeof(struct type)
1785 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1786 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1787 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1788 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1789 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1790 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1791 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1792 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1793 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1794 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1795 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1796 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1797 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1798 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1799 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1800 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1801 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1802 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1803 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1804 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1805 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
1810 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1811 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1812 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1813 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1814 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1815 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1816 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1817 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1818 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1819 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1820 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1821 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1822 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1823 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1824 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1827 static struct xfrm_link {
1828 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1829 int (*dump)(struct sk_buff *, struct netlink_callback *);
1830 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1831 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1832 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1833 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1834 .dump = xfrm_dump_sa },
1835 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1836 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1837 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1838 .dump = xfrm_dump_policy },
1839 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1840 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1841 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1842 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1843 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1844 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1845 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1846 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1847 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1848 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1849 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1850 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
1851 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1854 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1856 struct nlattr *attrs[XFRMA_MAX+1];
1857 struct xfrm_link *link;
1860 type = nlh->nlmsg_type;
1861 if (type > XFRM_MSG_MAX)
1864 type -= XFRM_MSG_BASE;
1865 link = &xfrm_dispatch[type];
1867 /* All operations require privileges, even GET */
1868 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1871 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1872 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1873 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1874 if (link->dump == NULL)
1877 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
1880 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1885 if (link->doit == NULL)
1888 return link->doit(skb, nlh, attrs);
1891 static void xfrm_netlink_rcv(struct sk_buff *skb)
1893 mutex_lock(&xfrm_cfg_mutex);
1894 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1895 mutex_unlock(&xfrm_cfg_mutex);
1898 static inline size_t xfrm_expire_msgsize(void)
1900 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1903 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1905 struct xfrm_user_expire *ue;
1906 struct nlmsghdr *nlh;
1908 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1912 ue = nlmsg_data(nlh);
1913 copy_to_user_state(x, &ue->state);
1914 ue->hard = (c->data.hard != 0) ? 1 : 0;
1916 return nlmsg_end(skb, nlh);
1919 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1921 struct sk_buff *skb;
1923 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
1927 if (build_expire(skb, x, c) < 0)
1930 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1933 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1935 struct sk_buff *skb;
1937 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1941 if (build_aevent(skb, x, c) < 0)
1944 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1947 static int xfrm_notify_sa_flush(struct km_event *c)
1949 struct xfrm_usersa_flush *p;
1950 struct nlmsghdr *nlh;
1951 struct sk_buff *skb;
1952 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
1954 skb = nlmsg_new(len, GFP_ATOMIC);
1958 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1964 p = nlmsg_data(nlh);
1965 p->proto = c->data.proto;
1967 nlmsg_end(skb, nlh);
1969 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1972 static inline size_t xfrm_sa_len(struct xfrm_state *x)
1976 l += nla_total_size(xfrm_alg_len(x->aalg));
1978 l += nla_total_size(xfrm_alg_len(x->ealg));
1980 l += nla_total_size(sizeof(*x->calg));
1982 l += nla_total_size(sizeof(*x->encap));
1984 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
1985 x->security->ctx_len);
1987 l += nla_total_size(sizeof(*x->coaddr));
1989 /* Must count this as this may become non-zero behind our back. */
1990 l += nla_total_size(sizeof(x->lastused));
1995 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1997 struct xfrm_usersa_info *p;
1998 struct xfrm_usersa_id *id;
1999 struct nlmsghdr *nlh;
2000 struct sk_buff *skb;
2001 int len = xfrm_sa_len(x);
2004 headlen = sizeof(*p);
2005 if (c->event == XFRM_MSG_DELSA) {
2006 len += nla_total_size(headlen);
2007 headlen = sizeof(*id);
2009 len += NLMSG_ALIGN(headlen);
2011 skb = nlmsg_new(len, GFP_ATOMIC);
2015 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2017 goto nla_put_failure;
2019 p = nlmsg_data(nlh);
2020 if (c->event == XFRM_MSG_DELSA) {
2021 struct nlattr *attr;
2023 id = nlmsg_data(nlh);
2024 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2025 id->spi = x->id.spi;
2026 id->family = x->props.family;
2027 id->proto = x->id.proto;
2029 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2031 goto nla_put_failure;
2036 if (copy_to_user_state_extra(x, p, skb))
2037 goto nla_put_failure;
2039 nlmsg_end(skb, nlh);
2041 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2044 /* Somebody screwed up with xfrm_sa_len! */
2050 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2054 case XFRM_MSG_EXPIRE:
2055 return xfrm_exp_state_notify(x, c);
2056 case XFRM_MSG_NEWAE:
2057 return xfrm_aevent_state_notify(x, c);
2058 case XFRM_MSG_DELSA:
2059 case XFRM_MSG_UPDSA:
2060 case XFRM_MSG_NEWSA:
2061 return xfrm_notify_sa(x, c);
2062 case XFRM_MSG_FLUSHSA:
2063 return xfrm_notify_sa_flush(c);
2065 printk("xfrm_user: Unknown SA event %d\n", c->event);
2073 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2074 struct xfrm_policy *xp)
2076 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2077 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2078 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2079 + userpolicy_type_attrsize();
2082 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2083 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2086 struct xfrm_user_acquire *ua;
2087 struct nlmsghdr *nlh;
2088 __u32 seq = xfrm_get_acqseq();
2090 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2094 ua = nlmsg_data(nlh);
2095 memcpy(&ua->id, &x->id, sizeof(ua->id));
2096 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2097 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2098 copy_to_user_policy(xp, &ua->policy, dir);
2099 ua->aalgos = xt->aalgos;
2100 ua->ealgos = xt->ealgos;
2101 ua->calgos = xt->calgos;
2102 ua->seq = x->km.seq = seq;
2104 if (copy_to_user_tmpl(xp, skb) < 0)
2106 if (copy_to_user_state_sec_ctx(x, skb))
2108 if (copy_to_user_policy_type(xp->type, skb) < 0)
2111 return nlmsg_end(skb, nlh);
2114 nlmsg_cancel(skb, nlh);
2118 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2119 struct xfrm_policy *xp, int dir)
2121 struct sk_buff *skb;
2123 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2127 if (build_acquire(skb, x, xt, xp, dir) < 0)
2130 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2133 /* User gives us xfrm_user_policy_info followed by an array of 0
2134 * or more templates.
2136 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2137 u8 *data, int len, int *dir)
2139 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2140 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2141 struct xfrm_policy *xp;
2144 switch (sk->sk_family) {
2146 if (opt != IP_XFRM_POLICY) {
2151 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2153 if (opt != IPV6_XFRM_POLICY) {
2166 if (len < sizeof(*p) ||
2167 verify_newpolicy_info(p))
2170 nr = ((len - sizeof(*p)) / sizeof(*ut));
2171 if (validate_tmpl(nr, ut, p->sel.family))
2174 if (p->dir > XFRM_POLICY_OUT)
2177 xp = xfrm_policy_alloc(GFP_KERNEL);
2183 copy_from_user_policy(xp, p);
2184 xp->type = XFRM_POLICY_TYPE_MAIN;
2185 copy_templates(xp, ut, nr);
2192 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2194 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2195 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2196 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2197 + userpolicy_type_attrsize();
2200 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2201 int dir, struct km_event *c)
2203 struct xfrm_user_polexpire *upe;
2204 struct nlmsghdr *nlh;
2205 int hard = c->data.hard;
2207 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2211 upe = nlmsg_data(nlh);
2212 copy_to_user_policy(xp, &upe->pol, dir);
2213 if (copy_to_user_tmpl(xp, skb) < 0)
2215 if (copy_to_user_sec_ctx(xp, skb))
2217 if (copy_to_user_policy_type(xp->type, skb) < 0)
2221 return nlmsg_end(skb, nlh);
2224 nlmsg_cancel(skb, nlh);
2228 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2230 struct sk_buff *skb;
2232 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2236 if (build_polexpire(skb, xp, dir, c) < 0)
2239 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2242 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2244 struct xfrm_userpolicy_info *p;
2245 struct xfrm_userpolicy_id *id;
2246 struct nlmsghdr *nlh;
2247 struct sk_buff *skb;
2248 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2251 headlen = sizeof(*p);
2252 if (c->event == XFRM_MSG_DELPOLICY) {
2253 len += nla_total_size(headlen);
2254 headlen = sizeof(*id);
2256 len += userpolicy_type_attrsize();
2257 len += NLMSG_ALIGN(headlen);
2259 skb = nlmsg_new(len, GFP_ATOMIC);
2263 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2267 p = nlmsg_data(nlh);
2268 if (c->event == XFRM_MSG_DELPOLICY) {
2269 struct nlattr *attr;
2271 id = nlmsg_data(nlh);
2272 memset(id, 0, sizeof(*id));
2275 id->index = xp->index;
2277 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2279 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2286 copy_to_user_policy(xp, p, dir);
2287 if (copy_to_user_tmpl(xp, skb) < 0)
2289 if (copy_to_user_policy_type(xp->type, skb) < 0)
2292 nlmsg_end(skb, nlh);
2294 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2301 static int xfrm_notify_policy_flush(struct km_event *c)
2303 struct nlmsghdr *nlh;
2304 struct sk_buff *skb;
2306 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2310 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2313 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2316 nlmsg_end(skb, nlh);
2318 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2325 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2329 case XFRM_MSG_NEWPOLICY:
2330 case XFRM_MSG_UPDPOLICY:
2331 case XFRM_MSG_DELPOLICY:
2332 return xfrm_notify_policy(xp, dir, c);
2333 case XFRM_MSG_FLUSHPOLICY:
2334 return xfrm_notify_policy_flush(c);
2335 case XFRM_MSG_POLEXPIRE:
2336 return xfrm_exp_policy_notify(xp, dir, c);
2338 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2345 static inline size_t xfrm_report_msgsize(void)
2347 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2350 static int build_report(struct sk_buff *skb, u8 proto,
2351 struct xfrm_selector *sel, xfrm_address_t *addr)
2353 struct xfrm_user_report *ur;
2354 struct nlmsghdr *nlh;
2356 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2360 ur = nlmsg_data(nlh);
2362 memcpy(&ur->sel, sel, sizeof(ur->sel));
2365 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2367 return nlmsg_end(skb, nlh);
2370 nlmsg_cancel(skb, nlh);
2374 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2375 xfrm_address_t *addr)
2377 struct sk_buff *skb;
2379 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2383 if (build_report(skb, proto, sel, addr) < 0)
2386 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2389 static struct xfrm_mgr netlink_mgr = {
2391 .notify = xfrm_send_state_notify,
2392 .acquire = xfrm_send_acquire,
2393 .compile_policy = xfrm_compile_policy,
2394 .notify_policy = xfrm_send_policy_notify,
2395 .report = xfrm_send_report,
2396 .migrate = xfrm_send_migrate,
2399 static int __init xfrm_user_init(void)
2403 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2405 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
2406 xfrm_netlink_rcv, NULL, THIS_MODULE);
2409 rcu_assign_pointer(xfrm_nl, nlsk);
2411 xfrm_register_km(&netlink_mgr);
2416 static void __exit xfrm_user_exit(void)
2418 struct sock *nlsk = xfrm_nl;
2420 xfrm_unregister_km(&netlink_mgr);
2421 rcu_assign_pointer(xfrm_nl, NULL);
2423 sock_release(nlsk->sk_socket);
2426 module_init(xfrm_user_init);
2427 module_exit(xfrm_user_exit);
2428 MODULE_LICENSE("GPL");
2429 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);