1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
29 #include <asm/uaccess.h>
30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31 #include <linux/in6.h>
34 static inline int alg_len(struct xfrm_algo *alg)
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
41 struct nlattr *rt = attrs[type];
42 struct xfrm_algo *algp;
48 if (nla_len(rt) < alg_len(algp))
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
65 /* Zero length keys are legal. */
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
76 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
77 xfrm_address_t **addrp)
79 struct nlattr *rt = attrs[type];
82 *addrp = nla_data(rt);
85 static inline int verify_sec_ctx_len(struct nlattr **attrs)
87 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
88 struct xfrm_user_sec_ctx *uctx;
94 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
101 static int verify_newsa_info(struct xfrm_usersa_info *p,
102 struct nlattr **attrs)
112 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
124 switch (p->id.proto) {
126 if (!attrs[XFRMA_ALG_AUTH] ||
127 attrs[XFRMA_ALG_CRYPT] ||
128 attrs[XFRMA_ALG_COMP])
133 if ((!attrs[XFRMA_ALG_AUTH] &&
134 !attrs[XFRMA_ALG_CRYPT]) ||
135 attrs[XFRMA_ALG_COMP])
140 if (!attrs[XFRMA_ALG_COMP] ||
141 attrs[XFRMA_ALG_AUTH] ||
142 attrs[XFRMA_ALG_CRYPT])
146 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 case IPPROTO_DSTOPTS:
148 case IPPROTO_ROUTING:
149 if (attrs[XFRMA_ALG_COMP] ||
150 attrs[XFRMA_ALG_AUTH] ||
151 attrs[XFRMA_ALG_CRYPT] ||
152 attrs[XFRMA_ENCAP] ||
153 attrs[XFRMA_SEC_CTX] ||
154 !attrs[XFRMA_COADDR])
163 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
165 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
167 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
169 if ((err = verify_sec_ctx_len(attrs)))
174 case XFRM_MODE_TRANSPORT:
175 case XFRM_MODE_TUNNEL:
176 case XFRM_MODE_ROUTEOPTIMIZATION:
190 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
191 struct xfrm_algo_desc *(*get_byname)(char *, int),
194 struct xfrm_algo *p, *ualg;
195 struct xfrm_algo_desc *algo;
200 ualg = nla_data(rta);
202 algo = get_byname(ualg->alg_name, 1);
205 *props = algo->desc.sadb_alg_id;
207 p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
211 strcpy(p->alg_name, algo->name);
216 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
221 len += sizeof(struct xfrm_user_sec_ctx);
222 len += xfrm_ctx->ctx_len;
227 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
229 memcpy(&x->id, &p->id, sizeof(x->id));
230 memcpy(&x->sel, &p->sel, sizeof(x->sel));
231 memcpy(&x->lft, &p->lft, sizeof(x->lft));
232 x->props.mode = p->mode;
233 x->props.replay_window = p->replay_window;
234 x->props.reqid = p->reqid;
235 x->props.family = p->family;
236 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
237 x->props.flags = p->flags;
240 * Set inner address family if the KM left it as zero.
241 * See comment in validate_tmpl.
244 x->sel.family = p->family;
248 * someday when pfkey also has support, we could have the code
249 * somehow made shareable and move it to xfrm_state.c - JHS
252 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
254 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
255 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
256 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
257 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
260 struct xfrm_replay_state *replay;
261 replay = nla_data(rp);
262 memcpy(&x->replay, replay, sizeof(*replay));
263 memcpy(&x->preplay, replay, sizeof(*replay));
267 struct xfrm_lifetime_cur *ltime;
268 ltime = nla_data(lt);
269 x->curlft.bytes = ltime->bytes;
270 x->curlft.packets = ltime->packets;
271 x->curlft.add_time = ltime->add_time;
272 x->curlft.use_time = ltime->use_time;
276 x->replay_maxage = nla_get_u32(et);
279 x->replay_maxdiff = nla_get_u32(rt);
282 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
283 struct nlattr **attrs,
286 struct xfrm_state *x = xfrm_state_alloc();
292 copy_from_user_state(x, p);
294 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
295 xfrm_aalg_get_byname,
296 attrs[XFRMA_ALG_AUTH])))
298 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
299 xfrm_ealg_get_byname,
300 attrs[XFRMA_ALG_CRYPT])))
302 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
303 xfrm_calg_get_byname,
304 attrs[XFRMA_ALG_COMP])))
307 if (attrs[XFRMA_ENCAP]) {
308 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
309 sizeof(*x->encap), GFP_KERNEL);
310 if (x->encap == NULL)
314 if (attrs[XFRMA_COADDR]) {
315 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
316 sizeof(*x->coaddr), GFP_KERNEL);
317 if (x->coaddr == NULL)
321 err = xfrm_init_state(x);
325 if (attrs[XFRMA_SEC_CTX] &&
326 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
330 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
331 /* sysctl_xfrm_aevent_etime is in 100ms units */
332 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
333 x->preplay.bitmap = 0;
334 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
335 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
337 /* override default values from above */
339 xfrm_update_ae_params(x, attrs);
344 x->km.state = XFRM_STATE_DEAD;
351 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
352 struct nlattr **attrs)
354 struct xfrm_usersa_info *p = nlmsg_data(nlh);
355 struct xfrm_state *x;
359 err = verify_newsa_info(p, attrs);
363 x = xfrm_state_construct(p, attrs, &err);
368 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
369 err = xfrm_state_add(x);
371 err = xfrm_state_update(x);
373 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
374 NETLINK_CB(skb).sid);
377 x->km.state = XFRM_STATE_DEAD;
382 c.seq = nlh->nlmsg_seq;
383 c.pid = nlh->nlmsg_pid;
384 c.event = nlh->nlmsg_type;
386 km_state_notify(x, &c);
392 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
393 struct nlattr **attrs,
396 struct xfrm_state *x = NULL;
399 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
401 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
403 xfrm_address_t *saddr = NULL;
405 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
412 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
422 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
423 struct nlattr **attrs)
425 struct xfrm_state *x;
428 struct xfrm_usersa_id *p = nlmsg_data(nlh);
430 x = xfrm_user_state_lookup(p, attrs, &err);
434 if ((err = security_xfrm_state_delete(x)) != 0)
437 if (xfrm_state_kern(x)) {
442 err = xfrm_state_delete(x);
447 c.seq = nlh->nlmsg_seq;
448 c.pid = nlh->nlmsg_pid;
449 c.event = nlh->nlmsg_type;
450 km_state_notify(x, &c);
453 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
454 NETLINK_CB(skb).sid);
459 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
461 memcpy(&p->id, &x->id, sizeof(p->id));
462 memcpy(&p->sel, &x->sel, sizeof(p->sel));
463 memcpy(&p->lft, &x->lft, sizeof(p->lft));
464 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
465 memcpy(&p->stats, &x->stats, sizeof(p->stats));
466 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
467 p->mode = x->props.mode;
468 p->replay_window = x->props.replay_window;
469 p->reqid = x->props.reqid;
470 p->family = x->props.family;
471 p->flags = x->props.flags;
475 struct xfrm_dump_info {
476 struct sk_buff *in_skb;
477 struct sk_buff *out_skb;
484 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
486 int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
487 struct xfrm_user_sec_ctx *uctx;
490 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
494 uctx = nla_data(attr);
495 uctx->exttype = XFRMA_SEC_CTX;
496 uctx->len = ctx_size;
497 uctx->ctx_doi = s->ctx_doi;
498 uctx->ctx_alg = s->ctx_alg;
499 uctx->ctx_len = s->ctx_len;
500 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
505 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
507 struct xfrm_dump_info *sp = ptr;
508 struct sk_buff *in_skb = sp->in_skb;
509 struct sk_buff *skb = sp->out_skb;
510 struct xfrm_usersa_info *p;
511 struct nlmsghdr *nlh;
513 if (sp->this_idx < sp->start_idx)
516 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
517 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
522 copy_to_user_state(x, p);
525 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
527 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
529 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
532 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
534 if (x->security && copy_sec_ctx(x->security, skb) < 0)
535 goto nla_put_failure;
538 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
541 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
549 nlmsg_cancel(skb, nlh);
553 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
555 struct xfrm_dump_info info;
557 info.in_skb = cb->skb;
559 info.nlmsg_seq = cb->nlh->nlmsg_seq;
560 info.nlmsg_flags = NLM_F_MULTI;
562 info.start_idx = cb->args[0];
563 (void) xfrm_state_walk(0, dump_one_state, &info);
564 cb->args[0] = info.this_idx;
569 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
570 struct xfrm_state *x, u32 seq)
572 struct xfrm_dump_info info;
575 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
577 return ERR_PTR(-ENOMEM);
579 info.in_skb = in_skb;
581 info.nlmsg_seq = seq;
582 info.nlmsg_flags = 0;
583 info.this_idx = info.start_idx = 0;
585 if (dump_one_state(x, 0, &info)) {
593 static inline size_t xfrm_spdinfo_msgsize(void)
595 return NLMSG_ALIGN(4)
596 + nla_total_size(sizeof(struct xfrmu_spdinfo))
597 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
600 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
602 struct xfrmk_spdinfo si;
603 struct xfrmu_spdinfo spc;
604 struct xfrmu_spdhinfo sph;
605 struct nlmsghdr *nlh;
608 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
609 if (nlh == NULL) /* shouldnt really happen ... */
614 xfrm_spd_getinfo(&si);
615 spc.incnt = si.incnt;
616 spc.outcnt = si.outcnt;
617 spc.fwdcnt = si.fwdcnt;
618 spc.inscnt = si.inscnt;
619 spc.outscnt = si.outscnt;
620 spc.fwdscnt = si.fwdscnt;
621 sph.spdhcnt = si.spdhcnt;
622 sph.spdhmcnt = si.spdhmcnt;
624 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
625 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
627 return nlmsg_end(skb, nlh);
630 nlmsg_cancel(skb, nlh);
634 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
635 struct nlattr **attrs)
637 struct sk_buff *r_skb;
638 u32 *flags = nlmsg_data(nlh);
639 u32 spid = NETLINK_CB(skb).pid;
640 u32 seq = nlh->nlmsg_seq;
642 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
646 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
649 return nlmsg_unicast(xfrm_nl, r_skb, spid);
652 static inline size_t xfrm_sadinfo_msgsize(void)
654 return NLMSG_ALIGN(4)
655 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
656 + nla_total_size(4); /* XFRMA_SAD_CNT */
659 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
661 struct xfrmk_sadinfo si;
662 struct xfrmu_sadhinfo sh;
663 struct nlmsghdr *nlh;
666 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
667 if (nlh == NULL) /* shouldnt really happen ... */
672 xfrm_sad_getinfo(&si);
674 sh.sadhmcnt = si.sadhmcnt;
675 sh.sadhcnt = si.sadhcnt;
677 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
678 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
680 return nlmsg_end(skb, nlh);
683 nlmsg_cancel(skb, nlh);
687 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
688 struct nlattr **attrs)
690 struct sk_buff *r_skb;
691 u32 *flags = nlmsg_data(nlh);
692 u32 spid = NETLINK_CB(skb).pid;
693 u32 seq = nlh->nlmsg_seq;
695 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
699 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
702 return nlmsg_unicast(xfrm_nl, r_skb, spid);
705 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
706 struct nlattr **attrs)
708 struct xfrm_usersa_id *p = nlmsg_data(nlh);
709 struct xfrm_state *x;
710 struct sk_buff *resp_skb;
713 x = xfrm_user_state_lookup(p, attrs, &err);
717 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
718 if (IS_ERR(resp_skb)) {
719 err = PTR_ERR(resp_skb);
721 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
728 static int verify_userspi_info(struct xfrm_userspi_info *p)
730 switch (p->info.id.proto) {
736 /* IPCOMP spi is 16-bits. */
737 if (p->max >= 0x10000)
751 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
752 struct nlattr **attrs)
754 struct xfrm_state *x;
755 struct xfrm_userspi_info *p;
756 struct sk_buff *resp_skb;
757 xfrm_address_t *daddr;
762 err = verify_userspi_info(p);
766 family = p->info.family;
767 daddr = &p->info.id.daddr;
771 x = xfrm_find_acq_byseq(p->info.seq);
772 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
779 x = xfrm_find_acq(p->info.mode, p->info.reqid,
780 p->info.id.proto, daddr,
787 err = xfrm_alloc_spi(x, p->min, p->max);
791 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
792 if (IS_ERR(resp_skb)) {
793 err = PTR_ERR(resp_skb);
797 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
805 static int verify_policy_dir(u8 dir)
809 case XFRM_POLICY_OUT:
810 case XFRM_POLICY_FWD:
820 static int verify_policy_type(u8 type)
823 case XFRM_POLICY_TYPE_MAIN:
824 #ifdef CONFIG_XFRM_SUB_POLICY
825 case XFRM_POLICY_TYPE_SUB:
836 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
840 case XFRM_SHARE_SESSION:
841 case XFRM_SHARE_USER:
842 case XFRM_SHARE_UNIQUE:
850 case XFRM_POLICY_ALLOW:
851 case XFRM_POLICY_BLOCK:
858 switch (p->sel.family) {
863 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
866 return -EAFNOSUPPORT;
873 return verify_policy_dir(p->dir);
876 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
878 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
879 struct xfrm_user_sec_ctx *uctx;
885 return security_xfrm_policy_alloc(pol, uctx);
888 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
894 for (i = 0; i < nr; i++, ut++) {
895 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
897 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
898 memcpy(&t->saddr, &ut->saddr,
899 sizeof(xfrm_address_t));
900 t->reqid = ut->reqid;
902 t->share = ut->share;
903 t->optional = ut->optional;
904 t->aalgos = ut->aalgos;
905 t->ealgos = ut->ealgos;
906 t->calgos = ut->calgos;
907 t->encap_family = ut->family;
911 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
915 if (nr > XFRM_MAX_DEPTH)
918 for (i = 0; i < nr; i++) {
919 /* We never validated the ut->family value, so many
920 * applications simply leave it at zero. The check was
921 * never made and ut->family was ignored because all
922 * templates could be assumed to have the same family as
923 * the policy itself. Now that we will have ipv4-in-ipv6
924 * and ipv6-in-ipv4 tunnels, this is no longer true.
927 ut[i].family = family;
929 switch (ut[i].family) {
932 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
944 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
946 struct nlattr *rt = attrs[XFRMA_TMPL];
951 struct xfrm_user_tmpl *utmpl = nla_data(rt);
952 int nr = nla_len(rt) / sizeof(*utmpl);
955 err = validate_tmpl(nr, utmpl, pol->family);
959 copy_templates(pol, utmpl, nr);
964 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
966 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
967 struct xfrm_userpolicy_type *upt;
968 u8 type = XFRM_POLICY_TYPE_MAIN;
976 err = verify_policy_type(type);
984 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
986 xp->priority = p->priority;
987 xp->index = p->index;
988 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
989 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
990 xp->action = p->action;
991 xp->flags = p->flags;
992 xp->family = p->sel.family;
993 /* XXX xp->share = p->share; */
996 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
998 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
999 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1000 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1001 p->priority = xp->priority;
1002 p->index = xp->index;
1003 p->sel.family = xp->family;
1005 p->action = xp->action;
1006 p->flags = xp->flags;
1007 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1010 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1012 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1020 copy_from_user_policy(xp, p);
1022 err = copy_from_user_policy_type(&xp->type, attrs);
1026 if (!(err = copy_from_user_tmpl(xp, attrs)))
1027 err = copy_from_user_sec_ctx(xp, attrs);
1038 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1039 struct nlattr **attrs)
1041 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1042 struct xfrm_policy *xp;
1047 err = verify_newpolicy_info(p);
1050 err = verify_sec_ctx_len(attrs);
1054 xp = xfrm_policy_construct(p, attrs, &err);
1058 /* shouldnt excl be based on nlh flags??
1059 * Aha! this is anti-netlink really i.e more pfkey derived
1060 * in netlink excl is a flag and you wouldnt need
1061 * a type XFRM_MSG_UPDPOLICY - JHS */
1062 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1063 err = xfrm_policy_insert(p->dir, xp, excl);
1064 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1065 NETLINK_CB(skb).sid);
1068 security_xfrm_policy_free(xp);
1073 c.event = nlh->nlmsg_type;
1074 c.seq = nlh->nlmsg_seq;
1075 c.pid = nlh->nlmsg_pid;
1076 km_policy_notify(xp, p->dir, &c);
1083 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1085 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1088 if (xp->xfrm_nr == 0)
1091 for (i = 0; i < xp->xfrm_nr; i++) {
1092 struct xfrm_user_tmpl *up = &vec[i];
1093 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1095 memcpy(&up->id, &kp->id, sizeof(up->id));
1096 up->family = kp->encap_family;
1097 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1098 up->reqid = kp->reqid;
1099 up->mode = kp->mode;
1100 up->share = kp->share;
1101 up->optional = kp->optional;
1102 up->aalgos = kp->aalgos;
1103 up->ealgos = kp->ealgos;
1104 up->calgos = kp->calgos;
1107 return nla_put(skb, XFRMA_TMPL,
1108 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1111 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1114 return copy_sec_ctx(x->security, skb);
1119 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1122 return copy_sec_ctx(xp->security, skb);
1126 static inline size_t userpolicy_type_attrsize(void)
1128 #ifdef CONFIG_XFRM_SUB_POLICY
1129 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1135 #ifdef CONFIG_XFRM_SUB_POLICY
1136 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1138 struct xfrm_userpolicy_type upt = {
1142 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1146 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1152 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1154 struct xfrm_dump_info *sp = ptr;
1155 struct xfrm_userpolicy_info *p;
1156 struct sk_buff *in_skb = sp->in_skb;
1157 struct sk_buff *skb = sp->out_skb;
1158 struct nlmsghdr *nlh;
1160 if (sp->this_idx < sp->start_idx)
1163 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1164 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1168 p = nlmsg_data(nlh);
1169 copy_to_user_policy(xp, p, dir);
1170 if (copy_to_user_tmpl(xp, skb) < 0)
1172 if (copy_to_user_sec_ctx(xp, skb))
1174 if (copy_to_user_policy_type(xp->type, skb) < 0)
1177 nlmsg_end(skb, nlh);
1183 nlmsg_cancel(skb, nlh);
1187 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1189 struct xfrm_dump_info info;
1191 info.in_skb = cb->skb;
1193 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1194 info.nlmsg_flags = NLM_F_MULTI;
1196 info.start_idx = cb->args[0];
1197 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1198 #ifdef CONFIG_XFRM_SUB_POLICY
1199 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1201 cb->args[0] = info.this_idx;
1206 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1207 struct xfrm_policy *xp,
1210 struct xfrm_dump_info info;
1211 struct sk_buff *skb;
1213 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1215 return ERR_PTR(-ENOMEM);
1217 info.in_skb = in_skb;
1219 info.nlmsg_seq = seq;
1220 info.nlmsg_flags = 0;
1221 info.this_idx = info.start_idx = 0;
1223 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1231 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1232 struct nlattr **attrs)
1234 struct xfrm_policy *xp;
1235 struct xfrm_userpolicy_id *p;
1236 u8 type = XFRM_POLICY_TYPE_MAIN;
1241 p = nlmsg_data(nlh);
1242 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1244 err = copy_from_user_policy_type(&type, attrs);
1248 err = verify_policy_dir(p->dir);
1253 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1255 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1256 struct xfrm_policy tmp;
1258 err = verify_sec_ctx_len(attrs);
1262 memset(&tmp, 0, sizeof(struct xfrm_policy));
1264 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1266 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1269 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1271 security_xfrm_policy_free(&tmp);
1277 struct sk_buff *resp_skb;
1279 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1280 if (IS_ERR(resp_skb)) {
1281 err = PTR_ERR(resp_skb);
1283 err = nlmsg_unicast(xfrm_nl, resp_skb,
1284 NETLINK_CB(skb).pid);
1287 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1288 NETLINK_CB(skb).loginuid,
1289 NETLINK_CB(skb).sid);
1294 c.data.byid = p->index;
1295 c.event = nlh->nlmsg_type;
1296 c.seq = nlh->nlmsg_seq;
1297 c.pid = nlh->nlmsg_pid;
1298 km_policy_notify(xp, p->dir, &c);
1306 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1307 struct nlattr **attrs)
1310 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1311 struct xfrm_audit audit_info;
1314 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1315 audit_info.secid = NETLINK_CB(skb).sid;
1316 err = xfrm_state_flush(p->proto, &audit_info);
1319 c.data.proto = p->proto;
1320 c.event = nlh->nlmsg_type;
1321 c.seq = nlh->nlmsg_seq;
1322 c.pid = nlh->nlmsg_pid;
1323 km_state_notify(NULL, &c);
1328 static inline size_t xfrm_aevent_msgsize(void)
1330 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1331 + nla_total_size(sizeof(struct xfrm_replay_state))
1332 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1333 + nla_total_size(4) /* XFRM_AE_RTHR */
1334 + nla_total_size(4); /* XFRM_AE_ETHR */
1337 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1339 struct xfrm_aevent_id *id;
1340 struct nlmsghdr *nlh;
1342 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1346 id = nlmsg_data(nlh);
1347 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1348 id->sa_id.spi = x->id.spi;
1349 id->sa_id.family = x->props.family;
1350 id->sa_id.proto = x->id.proto;
1351 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1352 id->reqid = x->props.reqid;
1353 id->flags = c->data.aevent;
1355 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1356 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1358 if (id->flags & XFRM_AE_RTHR)
1359 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1361 if (id->flags & XFRM_AE_ETHR)
1362 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1363 x->replay_maxage * 10 / HZ);
1365 return nlmsg_end(skb, nlh);
1368 nlmsg_cancel(skb, nlh);
1372 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1373 struct nlattr **attrs)
1375 struct xfrm_state *x;
1376 struct sk_buff *r_skb;
1379 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1380 struct xfrm_usersa_id *id = &p->sa_id;
1382 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1386 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1393 * XXX: is this lock really needed - none of the other
1394 * gets lock (the concern is things getting updated
1395 * while we are still reading) - jhs
1397 spin_lock_bh(&x->lock);
1398 c.data.aevent = p->flags;
1399 c.seq = nlh->nlmsg_seq;
1400 c.pid = nlh->nlmsg_pid;
1402 if (build_aevent(r_skb, x, &c) < 0)
1404 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1405 spin_unlock_bh(&x->lock);
1410 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1411 struct nlattr **attrs)
1413 struct xfrm_state *x;
1416 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1417 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1418 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1423 /* pedantic mode - thou shalt sayeth replaceth */
1424 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1427 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1431 if (x->km.state != XFRM_STATE_VALID)
1434 spin_lock_bh(&x->lock);
1435 xfrm_update_ae_params(x, attrs);
1436 spin_unlock_bh(&x->lock);
1438 c.event = nlh->nlmsg_type;
1439 c.seq = nlh->nlmsg_seq;
1440 c.pid = nlh->nlmsg_pid;
1441 c.data.aevent = XFRM_AE_CU;
1442 km_state_notify(x, &c);
1449 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1450 struct nlattr **attrs)
1453 u8 type = XFRM_POLICY_TYPE_MAIN;
1455 struct xfrm_audit audit_info;
1457 err = copy_from_user_policy_type(&type, attrs);
1461 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1462 audit_info.secid = NETLINK_CB(skb).sid;
1463 err = xfrm_policy_flush(type, &audit_info);
1467 c.event = nlh->nlmsg_type;
1468 c.seq = nlh->nlmsg_seq;
1469 c.pid = nlh->nlmsg_pid;
1470 km_policy_notify(NULL, 0, &c);
1474 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1475 struct nlattr **attrs)
1477 struct xfrm_policy *xp;
1478 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1479 struct xfrm_userpolicy_info *p = &up->pol;
1480 u8 type = XFRM_POLICY_TYPE_MAIN;
1483 err = copy_from_user_policy_type(&type, attrs);
1488 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1490 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1491 struct xfrm_policy tmp;
1493 err = verify_sec_ctx_len(attrs);
1497 memset(&tmp, 0, sizeof(struct xfrm_policy));
1499 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1501 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1504 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1506 security_xfrm_policy_free(&tmp);
1511 read_lock(&xp->lock);
1513 read_unlock(&xp->lock);
1517 read_unlock(&xp->lock);
1520 xfrm_policy_delete(xp, p->dir);
1521 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1522 NETLINK_CB(skb).sid);
1525 // reset the timers here?
1526 printk("Dont know what to do with soft policy expire\n");
1528 km_policy_expired(xp, p->dir, up->hard, current->pid);
1535 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1536 struct nlattr **attrs)
1538 struct xfrm_state *x;
1540 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1541 struct xfrm_usersa_info *p = &ue->state;
1543 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1549 spin_lock_bh(&x->lock);
1551 if (x->km.state != XFRM_STATE_VALID)
1553 km_state_expired(x, ue->hard, current->pid);
1556 __xfrm_state_delete(x);
1557 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1558 NETLINK_CB(skb).sid);
1562 spin_unlock_bh(&x->lock);
1567 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1568 struct nlattr **attrs)
1570 struct xfrm_policy *xp;
1571 struct xfrm_user_tmpl *ut;
1573 struct nlattr *rt = attrs[XFRMA_TMPL];
1575 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1576 struct xfrm_state *x = xfrm_state_alloc();
1582 err = verify_newpolicy_info(&ua->policy);
1584 printk("BAD policy passed\n");
1590 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1596 memcpy(&x->id, &ua->id, sizeof(ua->id));
1597 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1598 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1601 /* extract the templates and for each call km_key */
1602 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1603 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1604 memcpy(&x->id, &t->id, sizeof(x->id));
1605 x->props.mode = t->mode;
1606 x->props.reqid = t->reqid;
1607 x->props.family = ut->family;
1608 t->aalgos = ua->aalgos;
1609 t->ealgos = ua->ealgos;
1610 t->calgos = ua->calgos;
1611 err = km_query(x, t, xp);
1621 #ifdef CONFIG_XFRM_MIGRATE
1622 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1623 struct nlattr **attrs, int *num)
1625 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1626 struct xfrm_user_migrate *um;
1630 num_migrate = nla_len(rt) / sizeof(*um);
1632 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1635 for (i = 0; i < num_migrate; i++, um++, ma++) {
1636 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1637 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1638 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1639 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1641 ma->proto = um->proto;
1642 ma->mode = um->mode;
1643 ma->reqid = um->reqid;
1645 ma->old_family = um->old_family;
1646 ma->new_family = um->new_family;
1653 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1654 struct nlattr **attrs)
1656 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1657 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1662 if (attrs[XFRMA_MIGRATE] == NULL)
1665 err = copy_from_user_policy_type(&type, attrs);
1669 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1677 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1682 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1683 struct nlattr **attrs)
1685 return -ENOPROTOOPT;
1689 #ifdef CONFIG_XFRM_MIGRATE
1690 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1692 struct xfrm_user_migrate um;
1694 memset(&um, 0, sizeof(um));
1695 um.proto = m->proto;
1697 um.reqid = m->reqid;
1698 um.old_family = m->old_family;
1699 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1700 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1701 um.new_family = m->new_family;
1702 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1703 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1705 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1708 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1710 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1711 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1712 + userpolicy_type_attrsize();
1715 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1716 int num_migrate, struct xfrm_selector *sel,
1719 struct xfrm_migrate *mp;
1720 struct xfrm_userpolicy_id *pol_id;
1721 struct nlmsghdr *nlh;
1724 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1728 pol_id = nlmsg_data(nlh);
1729 /* copy data from selector, dir, and type to the pol_id */
1730 memset(pol_id, 0, sizeof(*pol_id));
1731 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1734 if (copy_to_user_policy_type(type, skb) < 0)
1737 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1738 if (copy_to_user_migrate(mp, skb) < 0)
1742 return nlmsg_end(skb, nlh);
1744 nlmsg_cancel(skb, nlh);
1748 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1749 struct xfrm_migrate *m, int num_migrate)
1751 struct sk_buff *skb;
1753 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1758 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1761 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1764 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1765 struct xfrm_migrate *m, int num_migrate)
1767 return -ENOPROTOOPT;
1771 #define XMSGSIZE(type) sizeof(struct type)
1773 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1774 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1775 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1776 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1777 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1778 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1779 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1780 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1781 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1782 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1783 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1784 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1785 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1786 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1787 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1788 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1789 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1790 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1791 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1792 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1793 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
1798 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1799 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1800 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1801 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1802 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1803 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1804 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1805 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1806 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1807 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1808 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1809 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1810 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1811 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1812 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1815 static struct xfrm_link {
1816 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1817 int (*dump)(struct sk_buff *, struct netlink_callback *);
1818 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1819 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1820 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1821 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1822 .dump = xfrm_dump_sa },
1823 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1824 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1825 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1826 .dump = xfrm_dump_policy },
1827 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1828 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1829 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1830 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1831 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1832 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1833 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1834 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1835 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1836 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1837 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1838 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
1839 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1842 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1844 struct nlattr *attrs[XFRMA_MAX+1];
1845 struct xfrm_link *link;
1848 type = nlh->nlmsg_type;
1849 if (type > XFRM_MSG_MAX)
1852 type -= XFRM_MSG_BASE;
1853 link = &xfrm_dispatch[type];
1855 /* All operations require privileges, even GET */
1856 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1859 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1860 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1861 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1862 if (link->dump == NULL)
1865 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
1868 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1873 if (link->doit == NULL)
1876 return link->doit(skb, nlh, attrs);
1879 static void xfrm_netlink_rcv(struct sock *sk, int len)
1881 unsigned int qlen = 0;
1884 mutex_lock(&xfrm_cfg_mutex);
1885 qlen = netlink_run_queue(sk, qlen, &xfrm_user_rcv_msg);
1886 mutex_unlock(&xfrm_cfg_mutex);
1891 static inline size_t xfrm_expire_msgsize(void)
1893 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1896 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1898 struct xfrm_user_expire *ue;
1899 struct nlmsghdr *nlh;
1901 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1905 ue = nlmsg_data(nlh);
1906 copy_to_user_state(x, &ue->state);
1907 ue->hard = (c->data.hard != 0) ? 1 : 0;
1909 return nlmsg_end(skb, nlh);
1912 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1914 struct sk_buff *skb;
1916 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
1920 if (build_expire(skb, x, c) < 0)
1923 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1926 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1928 struct sk_buff *skb;
1930 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1934 if (build_aevent(skb, x, c) < 0)
1937 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1940 static int xfrm_notify_sa_flush(struct km_event *c)
1942 struct xfrm_usersa_flush *p;
1943 struct nlmsghdr *nlh;
1944 struct sk_buff *skb;
1945 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
1947 skb = nlmsg_new(len, GFP_ATOMIC);
1951 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
1957 p = nlmsg_data(nlh);
1958 p->proto = c->data.proto;
1960 nlmsg_end(skb, nlh);
1962 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1965 static inline size_t xfrm_sa_len(struct xfrm_state *x)
1969 l += nla_total_size(alg_len(x->aalg));
1971 l += nla_total_size(alg_len(x->ealg));
1973 l += nla_total_size(sizeof(*x->calg));
1975 l += nla_total_size(sizeof(*x->encap));
1980 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1982 struct xfrm_usersa_info *p;
1983 struct xfrm_usersa_id *id;
1984 struct nlmsghdr *nlh;
1985 struct sk_buff *skb;
1986 int len = xfrm_sa_len(x);
1989 headlen = sizeof(*p);
1990 if (c->event == XFRM_MSG_DELSA) {
1991 len += nla_total_size(headlen);
1992 headlen = sizeof(*id);
1994 len += NLMSG_ALIGN(headlen);
1996 skb = nlmsg_new(len, GFP_ATOMIC);
2000 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2002 goto nla_put_failure;
2004 p = nlmsg_data(nlh);
2005 if (c->event == XFRM_MSG_DELSA) {
2006 struct nlattr *attr;
2008 id = nlmsg_data(nlh);
2009 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2010 id->spi = x->id.spi;
2011 id->family = x->props.family;
2012 id->proto = x->id.proto;
2014 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2016 goto nla_put_failure;
2021 copy_to_user_state(x, p);
2024 NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
2026 NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
2028 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
2031 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
2033 nlmsg_end(skb, nlh);
2035 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2042 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2046 case XFRM_MSG_EXPIRE:
2047 return xfrm_exp_state_notify(x, c);
2048 case XFRM_MSG_NEWAE:
2049 return xfrm_aevent_state_notify(x, c);
2050 case XFRM_MSG_DELSA:
2051 case XFRM_MSG_UPDSA:
2052 case XFRM_MSG_NEWSA:
2053 return xfrm_notify_sa(x, c);
2054 case XFRM_MSG_FLUSHSA:
2055 return xfrm_notify_sa_flush(c);
2057 printk("xfrm_user: Unknown SA event %d\n", c->event);
2065 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2066 struct xfrm_policy *xp)
2068 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2069 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2070 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2071 + userpolicy_type_attrsize();
2074 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2075 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2078 struct xfrm_user_acquire *ua;
2079 struct nlmsghdr *nlh;
2080 __u32 seq = xfrm_get_acqseq();
2082 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2086 ua = nlmsg_data(nlh);
2087 memcpy(&ua->id, &x->id, sizeof(ua->id));
2088 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2089 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2090 copy_to_user_policy(xp, &ua->policy, dir);
2091 ua->aalgos = xt->aalgos;
2092 ua->ealgos = xt->ealgos;
2093 ua->calgos = xt->calgos;
2094 ua->seq = x->km.seq = seq;
2096 if (copy_to_user_tmpl(xp, skb) < 0)
2098 if (copy_to_user_state_sec_ctx(x, skb))
2100 if (copy_to_user_policy_type(xp->type, skb) < 0)
2103 return nlmsg_end(skb, nlh);
2106 nlmsg_cancel(skb, nlh);
2110 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2111 struct xfrm_policy *xp, int dir)
2113 struct sk_buff *skb;
2115 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2119 if (build_acquire(skb, x, xt, xp, dir) < 0)
2122 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2125 /* User gives us xfrm_user_policy_info followed by an array of 0
2126 * or more templates.
2128 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2129 u8 *data, int len, int *dir)
2131 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2132 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2133 struct xfrm_policy *xp;
2136 switch (sk->sk_family) {
2138 if (opt != IP_XFRM_POLICY) {
2143 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2145 if (opt != IPV6_XFRM_POLICY) {
2158 if (len < sizeof(*p) ||
2159 verify_newpolicy_info(p))
2162 nr = ((len - sizeof(*p)) / sizeof(*ut));
2163 if (validate_tmpl(nr, ut, p->sel.family))
2166 if (p->dir > XFRM_POLICY_OUT)
2169 xp = xfrm_policy_alloc(GFP_KERNEL);
2175 copy_from_user_policy(xp, p);
2176 xp->type = XFRM_POLICY_TYPE_MAIN;
2177 copy_templates(xp, ut, nr);
2184 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2186 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2187 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2188 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2189 + userpolicy_type_attrsize();
2192 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2193 int dir, struct km_event *c)
2195 struct xfrm_user_polexpire *upe;
2196 struct nlmsghdr *nlh;
2197 int hard = c->data.hard;
2199 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2203 upe = nlmsg_data(nlh);
2204 copy_to_user_policy(xp, &upe->pol, dir);
2205 if (copy_to_user_tmpl(xp, skb) < 0)
2207 if (copy_to_user_sec_ctx(xp, skb))
2209 if (copy_to_user_policy_type(xp->type, skb) < 0)
2213 return nlmsg_end(skb, nlh);
2216 nlmsg_cancel(skb, nlh);
2220 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2222 struct sk_buff *skb;
2224 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2228 if (build_polexpire(skb, xp, dir, c) < 0)
2231 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2234 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2236 struct xfrm_userpolicy_info *p;
2237 struct xfrm_userpolicy_id *id;
2238 struct nlmsghdr *nlh;
2239 struct sk_buff *skb;
2240 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2243 headlen = sizeof(*p);
2244 if (c->event == XFRM_MSG_DELPOLICY) {
2245 len += nla_total_size(headlen);
2246 headlen = sizeof(*id);
2248 len += userpolicy_type_attrsize();
2249 len += NLMSG_ALIGN(headlen);
2251 skb = nlmsg_new(len, GFP_ATOMIC);
2255 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2259 p = nlmsg_data(nlh);
2260 if (c->event == XFRM_MSG_DELPOLICY) {
2261 struct nlattr *attr;
2263 id = nlmsg_data(nlh);
2264 memset(id, 0, sizeof(*id));
2267 id->index = xp->index;
2269 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2271 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2278 copy_to_user_policy(xp, p, dir);
2279 if (copy_to_user_tmpl(xp, skb) < 0)
2281 if (copy_to_user_policy_type(xp->type, skb) < 0)
2284 nlmsg_end(skb, nlh);
2286 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2293 static int xfrm_notify_policy_flush(struct km_event *c)
2295 struct nlmsghdr *nlh;
2296 struct sk_buff *skb;
2298 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2302 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2305 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2308 nlmsg_end(skb, nlh);
2310 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2317 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2321 case XFRM_MSG_NEWPOLICY:
2322 case XFRM_MSG_UPDPOLICY:
2323 case XFRM_MSG_DELPOLICY:
2324 return xfrm_notify_policy(xp, dir, c);
2325 case XFRM_MSG_FLUSHPOLICY:
2326 return xfrm_notify_policy_flush(c);
2327 case XFRM_MSG_POLEXPIRE:
2328 return xfrm_exp_policy_notify(xp, dir, c);
2330 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2337 static inline size_t xfrm_report_msgsize(void)
2339 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2342 static int build_report(struct sk_buff *skb, u8 proto,
2343 struct xfrm_selector *sel, xfrm_address_t *addr)
2345 struct xfrm_user_report *ur;
2346 struct nlmsghdr *nlh;
2348 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2352 ur = nlmsg_data(nlh);
2354 memcpy(&ur->sel, sel, sizeof(ur->sel));
2357 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2359 return nlmsg_end(skb, nlh);
2362 nlmsg_cancel(skb, nlh);
2366 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2367 xfrm_address_t *addr)
2369 struct sk_buff *skb;
2371 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2375 if (build_report(skb, proto, sel, addr) < 0)
2378 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2381 static struct xfrm_mgr netlink_mgr = {
2383 .notify = xfrm_send_state_notify,
2384 .acquire = xfrm_send_acquire,
2385 .compile_policy = xfrm_compile_policy,
2386 .notify_policy = xfrm_send_policy_notify,
2387 .report = xfrm_send_report,
2388 .migrate = xfrm_send_migrate,
2391 static int __init xfrm_user_init(void)
2395 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2397 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
2398 xfrm_netlink_rcv, NULL, THIS_MODULE);
2401 rcu_assign_pointer(xfrm_nl, nlsk);
2403 xfrm_register_km(&netlink_mgr);
2408 static void __exit xfrm_user_exit(void)
2410 struct sock *nlsk = xfrm_nl;
2412 xfrm_unregister_km(&netlink_mgr);
2413 rcu_assign_pointer(xfrm_nl, NULL);
2415 sock_release(nlsk->sk_socket);
2418 module_init(xfrm_user_init);
2419 module_exit(xfrm_user_exit);
2420 MODULE_LICENSE("GPL");
2421 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);