1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
29 #include <asm/uaccess.h>
30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31 #include <linux/in6.h>
34 static inline int aead_len(struct xfrm_algo_aead *alg)
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
41 struct nlattr *rt = attrs[type];
42 struct xfrm_algo *algp;
48 if (nla_len(rt) < xfrm_alg_len(algp))
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
65 /* Zero length keys are legal. */
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
76 static int verify_aead(struct nlattr **attrs)
78 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
79 struct xfrm_algo_aead *algp;
85 if (nla_len(rt) < aead_len(algp))
88 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
92 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
93 xfrm_address_t **addrp)
95 struct nlattr *rt = attrs[type];
98 *addrp = nla_data(rt);
101 static inline int verify_sec_ctx_len(struct nlattr **attrs)
103 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
104 struct xfrm_user_sec_ctx *uctx;
110 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
117 static int verify_newsa_info(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
128 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
140 switch (p->id.proto) {
142 if (!attrs[XFRMA_ALG_AUTH] ||
143 attrs[XFRMA_ALG_AEAD] ||
144 attrs[XFRMA_ALG_CRYPT] ||
145 attrs[XFRMA_ALG_COMP])
150 if (attrs[XFRMA_ALG_COMP])
152 if (!attrs[XFRMA_ALG_AUTH] &&
153 !attrs[XFRMA_ALG_CRYPT] &&
154 !attrs[XFRMA_ALG_AEAD])
156 if ((attrs[XFRMA_ALG_AUTH] ||
157 attrs[XFRMA_ALG_CRYPT]) &&
158 attrs[XFRMA_ALG_AEAD])
163 if (!attrs[XFRMA_ALG_COMP] ||
164 attrs[XFRMA_ALG_AEAD] ||
165 attrs[XFRMA_ALG_AUTH] ||
166 attrs[XFRMA_ALG_CRYPT])
170 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171 case IPPROTO_DSTOPTS:
172 case IPPROTO_ROUTING:
173 if (attrs[XFRMA_ALG_COMP] ||
174 attrs[XFRMA_ALG_AUTH] ||
175 attrs[XFRMA_ALG_AEAD] ||
176 attrs[XFRMA_ALG_CRYPT] ||
177 attrs[XFRMA_ENCAP] ||
178 attrs[XFRMA_SEC_CTX] ||
179 !attrs[XFRMA_COADDR])
188 if ((err = verify_aead(attrs)))
190 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
192 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
194 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
196 if ((err = verify_sec_ctx_len(attrs)))
201 case XFRM_MODE_TRANSPORT:
202 case XFRM_MODE_TUNNEL:
203 case XFRM_MODE_ROUTEOPTIMIZATION:
217 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
218 struct xfrm_algo_desc *(*get_byname)(char *, int),
221 struct xfrm_algo *p, *ualg;
222 struct xfrm_algo_desc *algo;
227 ualg = nla_data(rta);
229 algo = get_byname(ualg->alg_name, 1);
232 *props = algo->desc.sadb_alg_id;
234 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
238 strcpy(p->alg_name, algo->name);
243 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
246 struct xfrm_algo_aead *p, *ualg;
247 struct xfrm_algo_desc *algo;
252 ualg = nla_data(rta);
254 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
257 *props = algo->desc.sadb_alg_id;
259 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
263 strcpy(p->alg_name, algo->name);
268 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
273 len += sizeof(struct xfrm_user_sec_ctx);
274 len += xfrm_ctx->ctx_len;
279 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
281 memcpy(&x->id, &p->id, sizeof(x->id));
282 memcpy(&x->sel, &p->sel, sizeof(x->sel));
283 memcpy(&x->lft, &p->lft, sizeof(x->lft));
284 x->props.mode = p->mode;
285 x->props.replay_window = p->replay_window;
286 x->props.reqid = p->reqid;
287 x->props.family = p->family;
288 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
289 x->props.flags = p->flags;
291 if (x->props.mode == XFRM_MODE_TRANSPORT)
292 x->sel.family = p->family;
297 * someday when pfkey also has support, we could have the code
298 * somehow made shareable and move it to xfrm_state.c - JHS
301 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
303 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
304 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
305 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
306 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
309 struct xfrm_replay_state *replay;
310 replay = nla_data(rp);
311 memcpy(&x->replay, replay, sizeof(*replay));
312 memcpy(&x->preplay, replay, sizeof(*replay));
316 struct xfrm_lifetime_cur *ltime;
317 ltime = nla_data(lt);
318 x->curlft.bytes = ltime->bytes;
319 x->curlft.packets = ltime->packets;
320 x->curlft.add_time = ltime->add_time;
321 x->curlft.use_time = ltime->use_time;
325 x->replay_maxage = nla_get_u32(et);
328 x->replay_maxdiff = nla_get_u32(rt);
331 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
332 struct nlattr **attrs,
335 struct xfrm_state *x = xfrm_state_alloc();
341 copy_from_user_state(x, p);
343 if ((err = attach_aead(&x->aead, &x->props.ealgo,
344 attrs[XFRMA_ALG_AEAD])))
346 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
347 xfrm_aalg_get_byname,
348 attrs[XFRMA_ALG_AUTH])))
350 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
351 xfrm_ealg_get_byname,
352 attrs[XFRMA_ALG_CRYPT])))
354 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
355 xfrm_calg_get_byname,
356 attrs[XFRMA_ALG_COMP])))
359 if (attrs[XFRMA_ENCAP]) {
360 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
361 sizeof(*x->encap), GFP_KERNEL);
362 if (x->encap == NULL)
366 if (attrs[XFRMA_COADDR]) {
367 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
368 sizeof(*x->coaddr), GFP_KERNEL);
369 if (x->coaddr == NULL)
373 err = xfrm_init_state(x);
377 if (attrs[XFRMA_SEC_CTX] &&
378 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
382 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
383 /* sysctl_xfrm_aevent_etime is in 100ms units */
384 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
385 x->preplay.bitmap = 0;
386 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
387 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
389 /* override default values from above */
391 xfrm_update_ae_params(x, attrs);
396 x->km.state = XFRM_STATE_DEAD;
403 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
404 struct nlattr **attrs)
406 struct xfrm_usersa_info *p = nlmsg_data(nlh);
407 struct xfrm_state *x;
411 err = verify_newsa_info(p, attrs);
415 x = xfrm_state_construct(p, attrs, &err);
420 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
421 err = xfrm_state_add(x);
423 err = xfrm_state_update(x);
425 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
426 NETLINK_CB(skb).sid);
429 x->km.state = XFRM_STATE_DEAD;
434 c.seq = nlh->nlmsg_seq;
435 c.pid = nlh->nlmsg_pid;
436 c.event = nlh->nlmsg_type;
438 km_state_notify(x, &c);
444 static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
445 struct nlattr **attrs,
448 struct xfrm_state *x = NULL;
451 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
453 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
455 xfrm_address_t *saddr = NULL;
457 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
464 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
474 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
475 struct nlattr **attrs)
477 struct xfrm_state *x;
480 struct xfrm_usersa_id *p = nlmsg_data(nlh);
482 x = xfrm_user_state_lookup(p, attrs, &err);
486 if ((err = security_xfrm_state_delete(x)) != 0)
489 if (xfrm_state_kern(x)) {
494 err = xfrm_state_delete(x);
499 c.seq = nlh->nlmsg_seq;
500 c.pid = nlh->nlmsg_pid;
501 c.event = nlh->nlmsg_type;
502 km_state_notify(x, &c);
505 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
506 NETLINK_CB(skb).sid);
511 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
513 memcpy(&p->id, &x->id, sizeof(p->id));
514 memcpy(&p->sel, &x->sel, sizeof(p->sel));
515 memcpy(&p->lft, &x->lft, sizeof(p->lft));
516 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
517 memcpy(&p->stats, &x->stats, sizeof(p->stats));
518 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
519 p->mode = x->props.mode;
520 p->replay_window = x->props.replay_window;
521 p->reqid = x->props.reqid;
522 p->family = x->props.family;
523 p->flags = x->props.flags;
527 struct xfrm_dump_info {
528 struct sk_buff *in_skb;
529 struct sk_buff *out_skb;
536 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
538 struct xfrm_user_sec_ctx *uctx;
540 int ctx_size = sizeof(*uctx) + s->ctx_len;
542 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
546 uctx = nla_data(attr);
547 uctx->exttype = XFRMA_SEC_CTX;
548 uctx->len = ctx_size;
549 uctx->ctx_doi = s->ctx_doi;
550 uctx->ctx_alg = s->ctx_alg;
551 uctx->ctx_len = s->ctx_len;
552 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
557 /* Don't change this without updating xfrm_sa_len! */
558 static int copy_to_user_state_extra(struct xfrm_state *x,
559 struct xfrm_usersa_info *p,
562 copy_to_user_state(x, p);
565 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
568 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
571 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
573 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
575 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
577 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
580 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
582 if (x->security && copy_sec_ctx(x->security, skb) < 0)
583 goto nla_put_failure;
591 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
593 struct xfrm_dump_info *sp = ptr;
594 struct sk_buff *in_skb = sp->in_skb;
595 struct sk_buff *skb = sp->out_skb;
596 struct xfrm_usersa_info *p;
597 struct nlmsghdr *nlh;
600 if (sp->this_idx < sp->start_idx)
603 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
604 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
610 err = copy_to_user_state_extra(x, p, skb);
612 goto nla_put_failure;
620 nlmsg_cancel(skb, nlh);
624 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
626 struct xfrm_dump_info info;
628 info.in_skb = cb->skb;
630 info.nlmsg_seq = cb->nlh->nlmsg_seq;
631 info.nlmsg_flags = NLM_F_MULTI;
633 info.start_idx = cb->args[0];
634 (void) xfrm_state_walk(0, dump_one_state, &info);
635 cb->args[0] = info.this_idx;
640 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
641 struct xfrm_state *x, u32 seq)
643 struct xfrm_dump_info info;
646 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
648 return ERR_PTR(-ENOMEM);
650 info.in_skb = in_skb;
652 info.nlmsg_seq = seq;
653 info.nlmsg_flags = 0;
654 info.this_idx = info.start_idx = 0;
656 if (dump_one_state(x, 0, &info)) {
664 static inline size_t xfrm_spdinfo_msgsize(void)
666 return NLMSG_ALIGN(4)
667 + nla_total_size(sizeof(struct xfrmu_spdinfo))
668 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
671 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
673 struct xfrmk_spdinfo si;
674 struct xfrmu_spdinfo spc;
675 struct xfrmu_spdhinfo sph;
676 struct nlmsghdr *nlh;
679 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
680 if (nlh == NULL) /* shouldnt really happen ... */
685 xfrm_spd_getinfo(&si);
686 spc.incnt = si.incnt;
687 spc.outcnt = si.outcnt;
688 spc.fwdcnt = si.fwdcnt;
689 spc.inscnt = si.inscnt;
690 spc.outscnt = si.outscnt;
691 spc.fwdscnt = si.fwdscnt;
692 sph.spdhcnt = si.spdhcnt;
693 sph.spdhmcnt = si.spdhmcnt;
695 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
696 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
698 return nlmsg_end(skb, nlh);
701 nlmsg_cancel(skb, nlh);
705 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
706 struct nlattr **attrs)
708 struct sk_buff *r_skb;
709 u32 *flags = nlmsg_data(nlh);
710 u32 spid = NETLINK_CB(skb).pid;
711 u32 seq = nlh->nlmsg_seq;
713 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
717 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
720 return nlmsg_unicast(xfrm_nl, r_skb, spid);
723 static inline size_t xfrm_sadinfo_msgsize(void)
725 return NLMSG_ALIGN(4)
726 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
727 + nla_total_size(4); /* XFRMA_SAD_CNT */
730 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
732 struct xfrmk_sadinfo si;
733 struct xfrmu_sadhinfo sh;
734 struct nlmsghdr *nlh;
737 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
738 if (nlh == NULL) /* shouldnt really happen ... */
743 xfrm_sad_getinfo(&si);
745 sh.sadhmcnt = si.sadhmcnt;
746 sh.sadhcnt = si.sadhcnt;
748 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
749 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
751 return nlmsg_end(skb, nlh);
754 nlmsg_cancel(skb, nlh);
758 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
759 struct nlattr **attrs)
761 struct sk_buff *r_skb;
762 u32 *flags = nlmsg_data(nlh);
763 u32 spid = NETLINK_CB(skb).pid;
764 u32 seq = nlh->nlmsg_seq;
766 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
770 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
773 return nlmsg_unicast(xfrm_nl, r_skb, spid);
776 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
777 struct nlattr **attrs)
779 struct xfrm_usersa_id *p = nlmsg_data(nlh);
780 struct xfrm_state *x;
781 struct sk_buff *resp_skb;
784 x = xfrm_user_state_lookup(p, attrs, &err);
788 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
789 if (IS_ERR(resp_skb)) {
790 err = PTR_ERR(resp_skb);
792 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
799 static int verify_userspi_info(struct xfrm_userspi_info *p)
801 switch (p->info.id.proto) {
807 /* IPCOMP spi is 16-bits. */
808 if (p->max >= 0x10000)
822 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
823 struct nlattr **attrs)
825 struct xfrm_state *x;
826 struct xfrm_userspi_info *p;
827 struct sk_buff *resp_skb;
828 xfrm_address_t *daddr;
833 err = verify_userspi_info(p);
837 family = p->info.family;
838 daddr = &p->info.id.daddr;
842 x = xfrm_find_acq_byseq(p->info.seq);
843 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
850 x = xfrm_find_acq(p->info.mode, p->info.reqid,
851 p->info.id.proto, daddr,
858 err = xfrm_alloc_spi(x, p->min, p->max);
862 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
863 if (IS_ERR(resp_skb)) {
864 err = PTR_ERR(resp_skb);
868 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
876 static int verify_policy_dir(u8 dir)
880 case XFRM_POLICY_OUT:
881 case XFRM_POLICY_FWD:
891 static int verify_policy_type(u8 type)
894 case XFRM_POLICY_TYPE_MAIN:
895 #ifdef CONFIG_XFRM_SUB_POLICY
896 case XFRM_POLICY_TYPE_SUB:
907 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
911 case XFRM_SHARE_SESSION:
912 case XFRM_SHARE_USER:
913 case XFRM_SHARE_UNIQUE:
921 case XFRM_POLICY_ALLOW:
922 case XFRM_POLICY_BLOCK:
929 switch (p->sel.family) {
934 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
937 return -EAFNOSUPPORT;
944 return verify_policy_dir(p->dir);
947 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
949 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
950 struct xfrm_user_sec_ctx *uctx;
956 return security_xfrm_policy_alloc(pol, uctx);
959 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
965 for (i = 0; i < nr; i++, ut++) {
966 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
968 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
969 memcpy(&t->saddr, &ut->saddr,
970 sizeof(xfrm_address_t));
971 t->reqid = ut->reqid;
973 t->share = ut->share;
974 t->optional = ut->optional;
975 t->aalgos = ut->aalgos;
976 t->ealgos = ut->ealgos;
977 t->calgos = ut->calgos;
978 t->encap_family = ut->family;
982 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
986 if (nr > XFRM_MAX_DEPTH)
989 for (i = 0; i < nr; i++) {
990 /* We never validated the ut->family value, so many
991 * applications simply leave it at zero. The check was
992 * never made and ut->family was ignored because all
993 * templates could be assumed to have the same family as
994 * the policy itself. Now that we will have ipv4-in-ipv6
995 * and ipv6-in-ipv4 tunnels, this is no longer true.
998 ut[i].family = family;
1000 switch (ut[i].family) {
1003 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1015 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1017 struct nlattr *rt = attrs[XFRMA_TMPL];
1022 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1023 int nr = nla_len(rt) / sizeof(*utmpl);
1026 err = validate_tmpl(nr, utmpl, pol->family);
1030 copy_templates(pol, utmpl, nr);
1035 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1037 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1038 struct xfrm_userpolicy_type *upt;
1039 u8 type = XFRM_POLICY_TYPE_MAIN;
1047 err = verify_policy_type(type);
1055 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1057 xp->priority = p->priority;
1058 xp->index = p->index;
1059 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1060 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1061 xp->action = p->action;
1062 xp->flags = p->flags;
1063 xp->family = p->sel.family;
1064 /* XXX xp->share = p->share; */
1067 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1069 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1070 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1071 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1072 p->priority = xp->priority;
1073 p->index = xp->index;
1074 p->sel.family = xp->family;
1076 p->action = xp->action;
1077 p->flags = xp->flags;
1078 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1081 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1083 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1091 copy_from_user_policy(xp, p);
1093 err = copy_from_user_policy_type(&xp->type, attrs);
1097 if (!(err = copy_from_user_tmpl(xp, attrs)))
1098 err = copy_from_user_sec_ctx(xp, attrs);
1106 xfrm_policy_destroy(xp);
1110 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1111 struct nlattr **attrs)
1113 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1114 struct xfrm_policy *xp;
1119 err = verify_newpolicy_info(p);
1122 err = verify_sec_ctx_len(attrs);
1126 xp = xfrm_policy_construct(p, attrs, &err);
1130 /* shouldnt excl be based on nlh flags??
1131 * Aha! this is anti-netlink really i.e more pfkey derived
1132 * in netlink excl is a flag and you wouldnt need
1133 * a type XFRM_MSG_UPDPOLICY - JHS */
1134 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1135 err = xfrm_policy_insert(p->dir, xp, excl);
1136 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1137 NETLINK_CB(skb).sid);
1140 security_xfrm_policy_free(xp);
1145 c.event = nlh->nlmsg_type;
1146 c.seq = nlh->nlmsg_seq;
1147 c.pid = nlh->nlmsg_pid;
1148 km_policy_notify(xp, p->dir, &c);
1155 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1157 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1160 if (xp->xfrm_nr == 0)
1163 for (i = 0; i < xp->xfrm_nr; i++) {
1164 struct xfrm_user_tmpl *up = &vec[i];
1165 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1167 memcpy(&up->id, &kp->id, sizeof(up->id));
1168 up->family = kp->encap_family;
1169 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1170 up->reqid = kp->reqid;
1171 up->mode = kp->mode;
1172 up->share = kp->share;
1173 up->optional = kp->optional;
1174 up->aalgos = kp->aalgos;
1175 up->ealgos = kp->ealgos;
1176 up->calgos = kp->calgos;
1179 return nla_put(skb, XFRMA_TMPL,
1180 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1183 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1186 return copy_sec_ctx(x->security, skb);
1191 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1194 return copy_sec_ctx(xp->security, skb);
1198 static inline size_t userpolicy_type_attrsize(void)
1200 #ifdef CONFIG_XFRM_SUB_POLICY
1201 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1207 #ifdef CONFIG_XFRM_SUB_POLICY
1208 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1210 struct xfrm_userpolicy_type upt = {
1214 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1218 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1224 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1226 struct xfrm_dump_info *sp = ptr;
1227 struct xfrm_userpolicy_info *p;
1228 struct sk_buff *in_skb = sp->in_skb;
1229 struct sk_buff *skb = sp->out_skb;
1230 struct nlmsghdr *nlh;
1232 if (sp->this_idx < sp->start_idx)
1235 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1236 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1240 p = nlmsg_data(nlh);
1241 copy_to_user_policy(xp, p, dir);
1242 if (copy_to_user_tmpl(xp, skb) < 0)
1244 if (copy_to_user_sec_ctx(xp, skb))
1246 if (copy_to_user_policy_type(xp->type, skb) < 0)
1249 nlmsg_end(skb, nlh);
1255 nlmsg_cancel(skb, nlh);
1259 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1261 struct xfrm_dump_info info;
1263 info.in_skb = cb->skb;
1265 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1266 info.nlmsg_flags = NLM_F_MULTI;
1268 info.start_idx = cb->args[0];
1269 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1270 #ifdef CONFIG_XFRM_SUB_POLICY
1271 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1273 cb->args[0] = info.this_idx;
1278 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1279 struct xfrm_policy *xp,
1282 struct xfrm_dump_info info;
1283 struct sk_buff *skb;
1285 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1287 return ERR_PTR(-ENOMEM);
1289 info.in_skb = in_skb;
1291 info.nlmsg_seq = seq;
1292 info.nlmsg_flags = 0;
1293 info.this_idx = info.start_idx = 0;
1295 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1303 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1304 struct nlattr **attrs)
1306 struct xfrm_policy *xp;
1307 struct xfrm_userpolicy_id *p;
1308 u8 type = XFRM_POLICY_TYPE_MAIN;
1313 p = nlmsg_data(nlh);
1314 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1316 err = copy_from_user_policy_type(&type, attrs);
1320 err = verify_policy_dir(p->dir);
1325 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
1327 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1328 struct xfrm_policy tmp;
1330 err = verify_sec_ctx_len(attrs);
1334 memset(&tmp, 0, sizeof(struct xfrm_policy));
1336 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1338 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1341 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1343 security_xfrm_policy_free(&tmp);
1349 struct sk_buff *resp_skb;
1351 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1352 if (IS_ERR(resp_skb)) {
1353 err = PTR_ERR(resp_skb);
1355 err = nlmsg_unicast(xfrm_nl, resp_skb,
1356 NETLINK_CB(skb).pid);
1359 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1360 NETLINK_CB(skb).loginuid,
1361 NETLINK_CB(skb).sid);
1366 c.data.byid = p->index;
1367 c.event = nlh->nlmsg_type;
1368 c.seq = nlh->nlmsg_seq;
1369 c.pid = nlh->nlmsg_pid;
1370 km_policy_notify(xp, p->dir, &c);
1378 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1379 struct nlattr **attrs)
1382 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1383 struct xfrm_audit audit_info;
1386 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1387 audit_info.secid = NETLINK_CB(skb).sid;
1388 err = xfrm_state_flush(p->proto, &audit_info);
1391 c.data.proto = p->proto;
1392 c.event = nlh->nlmsg_type;
1393 c.seq = nlh->nlmsg_seq;
1394 c.pid = nlh->nlmsg_pid;
1395 km_state_notify(NULL, &c);
1400 static inline size_t xfrm_aevent_msgsize(void)
1402 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1403 + nla_total_size(sizeof(struct xfrm_replay_state))
1404 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1405 + nla_total_size(4) /* XFRM_AE_RTHR */
1406 + nla_total_size(4); /* XFRM_AE_ETHR */
1409 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1411 struct xfrm_aevent_id *id;
1412 struct nlmsghdr *nlh;
1414 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1418 id = nlmsg_data(nlh);
1419 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1420 id->sa_id.spi = x->id.spi;
1421 id->sa_id.family = x->props.family;
1422 id->sa_id.proto = x->id.proto;
1423 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1424 id->reqid = x->props.reqid;
1425 id->flags = c->data.aevent;
1427 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1428 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1430 if (id->flags & XFRM_AE_RTHR)
1431 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1433 if (id->flags & XFRM_AE_ETHR)
1434 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1435 x->replay_maxage * 10 / HZ);
1437 return nlmsg_end(skb, nlh);
1440 nlmsg_cancel(skb, nlh);
1444 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1445 struct nlattr **attrs)
1447 struct xfrm_state *x;
1448 struct sk_buff *r_skb;
1451 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1452 struct xfrm_usersa_id *id = &p->sa_id;
1454 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1458 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1465 * XXX: is this lock really needed - none of the other
1466 * gets lock (the concern is things getting updated
1467 * while we are still reading) - jhs
1469 spin_lock_bh(&x->lock);
1470 c.data.aevent = p->flags;
1471 c.seq = nlh->nlmsg_seq;
1472 c.pid = nlh->nlmsg_pid;
1474 if (build_aevent(r_skb, x, &c) < 0)
1476 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
1477 spin_unlock_bh(&x->lock);
1482 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1483 struct nlattr **attrs)
1485 struct xfrm_state *x;
1488 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1489 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1490 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1495 /* pedantic mode - thou shalt sayeth replaceth */
1496 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1499 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1503 if (x->km.state != XFRM_STATE_VALID)
1506 spin_lock_bh(&x->lock);
1507 xfrm_update_ae_params(x, attrs);
1508 spin_unlock_bh(&x->lock);
1510 c.event = nlh->nlmsg_type;
1511 c.seq = nlh->nlmsg_seq;
1512 c.pid = nlh->nlmsg_pid;
1513 c.data.aevent = XFRM_AE_CU;
1514 km_state_notify(x, &c);
1521 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1522 struct nlattr **attrs)
1525 u8 type = XFRM_POLICY_TYPE_MAIN;
1527 struct xfrm_audit audit_info;
1529 err = copy_from_user_policy_type(&type, attrs);
1533 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1534 audit_info.secid = NETLINK_CB(skb).sid;
1535 err = xfrm_policy_flush(type, &audit_info);
1539 c.event = nlh->nlmsg_type;
1540 c.seq = nlh->nlmsg_seq;
1541 c.pid = nlh->nlmsg_pid;
1542 km_policy_notify(NULL, 0, &c);
1546 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1547 struct nlattr **attrs)
1549 struct xfrm_policy *xp;
1550 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1551 struct xfrm_userpolicy_info *p = &up->pol;
1552 u8 type = XFRM_POLICY_TYPE_MAIN;
1555 err = copy_from_user_policy_type(&type, attrs);
1560 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
1562 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1563 struct xfrm_policy tmp;
1565 err = verify_sec_ctx_len(attrs);
1569 memset(&tmp, 0, sizeof(struct xfrm_policy));
1571 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1573 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1576 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1578 security_xfrm_policy_free(&tmp);
1583 read_lock(&xp->lock);
1585 read_unlock(&xp->lock);
1589 read_unlock(&xp->lock);
1592 xfrm_policy_delete(xp, p->dir);
1593 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1594 NETLINK_CB(skb).sid);
1597 // reset the timers here?
1598 printk("Dont know what to do with soft policy expire\n");
1600 km_policy_expired(xp, p->dir, up->hard, current->pid);
1607 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1608 struct nlattr **attrs)
1610 struct xfrm_state *x;
1612 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1613 struct xfrm_usersa_info *p = &ue->state;
1615 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
1621 spin_lock_bh(&x->lock);
1623 if (x->km.state != XFRM_STATE_VALID)
1625 km_state_expired(x, ue->hard, current->pid);
1628 __xfrm_state_delete(x);
1629 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1630 NETLINK_CB(skb).sid);
1634 spin_unlock_bh(&x->lock);
1639 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1640 struct nlattr **attrs)
1642 struct xfrm_policy *xp;
1643 struct xfrm_user_tmpl *ut;
1645 struct nlattr *rt = attrs[XFRMA_TMPL];
1647 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1648 struct xfrm_state *x = xfrm_state_alloc();
1654 err = verify_newpolicy_info(&ua->policy);
1656 printk("BAD policy passed\n");
1662 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
1668 memcpy(&x->id, &ua->id, sizeof(ua->id));
1669 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1670 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1673 /* extract the templates and for each call km_key */
1674 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1675 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1676 memcpy(&x->id, &t->id, sizeof(x->id));
1677 x->props.mode = t->mode;
1678 x->props.reqid = t->reqid;
1679 x->props.family = ut->family;
1680 t->aalgos = ua->aalgos;
1681 t->ealgos = ua->ealgos;
1682 t->calgos = ua->calgos;
1683 err = km_query(x, t, xp);
1693 #ifdef CONFIG_XFRM_MIGRATE
1694 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1695 struct nlattr **attrs, int *num)
1697 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1698 struct xfrm_user_migrate *um;
1702 num_migrate = nla_len(rt) / sizeof(*um);
1704 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1707 for (i = 0; i < num_migrate; i++, um++, ma++) {
1708 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1709 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1710 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1711 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1713 ma->proto = um->proto;
1714 ma->mode = um->mode;
1715 ma->reqid = um->reqid;
1717 ma->old_family = um->old_family;
1718 ma->new_family = um->new_family;
1725 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1726 struct nlattr **attrs)
1728 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1729 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1734 if (attrs[XFRMA_MIGRATE] == NULL)
1737 err = copy_from_user_policy_type(&type, attrs);
1741 err = copy_from_user_migrate((struct xfrm_migrate *)m,
1749 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1754 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1755 struct nlattr **attrs)
1757 return -ENOPROTOOPT;
1761 #ifdef CONFIG_XFRM_MIGRATE
1762 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1764 struct xfrm_user_migrate um;
1766 memset(&um, 0, sizeof(um));
1767 um.proto = m->proto;
1769 um.reqid = m->reqid;
1770 um.old_family = m->old_family;
1771 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1772 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1773 um.new_family = m->new_family;
1774 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1775 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1777 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1780 static inline size_t xfrm_migrate_msgsize(int num_migrate)
1782 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1783 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1784 + userpolicy_type_attrsize();
1787 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1788 int num_migrate, struct xfrm_selector *sel,
1791 struct xfrm_migrate *mp;
1792 struct xfrm_userpolicy_id *pol_id;
1793 struct nlmsghdr *nlh;
1796 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1800 pol_id = nlmsg_data(nlh);
1801 /* copy data from selector, dir, and type to the pol_id */
1802 memset(pol_id, 0, sizeof(*pol_id));
1803 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1806 if (copy_to_user_policy_type(type, skb) < 0)
1809 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1810 if (copy_to_user_migrate(mp, skb) < 0)
1814 return nlmsg_end(skb, nlh);
1816 nlmsg_cancel(skb, nlh);
1820 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1821 struct xfrm_migrate *m, int num_migrate)
1823 struct sk_buff *skb;
1825 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
1830 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1833 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
1836 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1837 struct xfrm_migrate *m, int num_migrate)
1839 return -ENOPROTOOPT;
1843 #define XMSGSIZE(type) sizeof(struct type)
1845 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1846 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1847 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1848 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1849 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1850 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1851 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1852 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1853 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1854 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1855 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1856 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1857 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1858 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1859 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
1860 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1861 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1862 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
1863 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1864 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1865 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
1870 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
1871 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
1872 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1873 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1874 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1875 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1876 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1877 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1878 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1879 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1880 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1881 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1882 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1883 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1884 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1885 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1888 static struct xfrm_link {
1889 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
1890 int (*dump)(struct sk_buff *, struct netlink_callback *);
1891 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1892 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1893 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1894 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1895 .dump = xfrm_dump_sa },
1896 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1897 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1898 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1899 .dump = xfrm_dump_policy },
1900 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1901 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
1902 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
1903 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1904 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1905 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
1906 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1907 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
1908 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1909 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1910 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
1911 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
1912 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
1915 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1917 struct nlattr *attrs[XFRMA_MAX+1];
1918 struct xfrm_link *link;
1921 type = nlh->nlmsg_type;
1922 if (type > XFRM_MSG_MAX)
1925 type -= XFRM_MSG_BASE;
1926 link = &xfrm_dispatch[type];
1928 /* All operations require privileges, even GET */
1929 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1932 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1933 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1934 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1935 if (link->dump == NULL)
1938 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
1941 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
1946 if (link->doit == NULL)
1949 return link->doit(skb, nlh, attrs);
1952 static void xfrm_netlink_rcv(struct sk_buff *skb)
1954 mutex_lock(&xfrm_cfg_mutex);
1955 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1956 mutex_unlock(&xfrm_cfg_mutex);
1959 static inline size_t xfrm_expire_msgsize(void)
1961 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1964 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1966 struct xfrm_user_expire *ue;
1967 struct nlmsghdr *nlh;
1969 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1973 ue = nlmsg_data(nlh);
1974 copy_to_user_state(x, &ue->state);
1975 ue->hard = (c->data.hard != 0) ? 1 : 0;
1977 return nlmsg_end(skb, nlh);
1980 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1982 struct sk_buff *skb;
1984 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
1988 if (build_expire(skb, x, c) < 0)
1991 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1994 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1996 struct sk_buff *skb;
1998 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2002 if (build_aevent(skb, x, c) < 0)
2005 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2008 static int xfrm_notify_sa_flush(struct km_event *c)
2010 struct xfrm_usersa_flush *p;
2011 struct nlmsghdr *nlh;
2012 struct sk_buff *skb;
2013 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2015 skb = nlmsg_new(len, GFP_ATOMIC);
2019 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2025 p = nlmsg_data(nlh);
2026 p->proto = c->data.proto;
2028 nlmsg_end(skb, nlh);
2030 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2033 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2037 l += nla_total_size(aead_len(x->aead));
2039 l += nla_total_size(xfrm_alg_len(x->aalg));
2041 l += nla_total_size(xfrm_alg_len(x->ealg));
2043 l += nla_total_size(sizeof(*x->calg));
2045 l += nla_total_size(sizeof(*x->encap));
2047 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2048 x->security->ctx_len);
2050 l += nla_total_size(sizeof(*x->coaddr));
2052 /* Must count x->lastused as it may become non-zero behind our back. */
2053 l += nla_total_size(sizeof(u64));
2058 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2060 struct xfrm_usersa_info *p;
2061 struct xfrm_usersa_id *id;
2062 struct nlmsghdr *nlh;
2063 struct sk_buff *skb;
2064 int len = xfrm_sa_len(x);
2067 headlen = sizeof(*p);
2068 if (c->event == XFRM_MSG_DELSA) {
2069 len += nla_total_size(headlen);
2070 headlen = sizeof(*id);
2072 len += NLMSG_ALIGN(headlen);
2074 skb = nlmsg_new(len, GFP_ATOMIC);
2078 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2080 goto nla_put_failure;
2082 p = nlmsg_data(nlh);
2083 if (c->event == XFRM_MSG_DELSA) {
2084 struct nlattr *attr;
2086 id = nlmsg_data(nlh);
2087 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2088 id->spi = x->id.spi;
2089 id->family = x->props.family;
2090 id->proto = x->id.proto;
2092 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2094 goto nla_put_failure;
2099 if (copy_to_user_state_extra(x, p, skb))
2100 goto nla_put_failure;
2102 nlmsg_end(skb, nlh);
2104 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2107 /* Somebody screwed up with xfrm_sa_len! */
2113 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2117 case XFRM_MSG_EXPIRE:
2118 return xfrm_exp_state_notify(x, c);
2119 case XFRM_MSG_NEWAE:
2120 return xfrm_aevent_state_notify(x, c);
2121 case XFRM_MSG_DELSA:
2122 case XFRM_MSG_UPDSA:
2123 case XFRM_MSG_NEWSA:
2124 return xfrm_notify_sa(x, c);
2125 case XFRM_MSG_FLUSHSA:
2126 return xfrm_notify_sa_flush(c);
2128 printk("xfrm_user: Unknown SA event %d\n", c->event);
2136 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2137 struct xfrm_policy *xp)
2139 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2140 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2141 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2142 + userpolicy_type_attrsize();
2145 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2146 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2149 struct xfrm_user_acquire *ua;
2150 struct nlmsghdr *nlh;
2151 __u32 seq = xfrm_get_acqseq();
2153 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2157 ua = nlmsg_data(nlh);
2158 memcpy(&ua->id, &x->id, sizeof(ua->id));
2159 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2160 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2161 copy_to_user_policy(xp, &ua->policy, dir);
2162 ua->aalgos = xt->aalgos;
2163 ua->ealgos = xt->ealgos;
2164 ua->calgos = xt->calgos;
2165 ua->seq = x->km.seq = seq;
2167 if (copy_to_user_tmpl(xp, skb) < 0)
2169 if (copy_to_user_state_sec_ctx(x, skb))
2171 if (copy_to_user_policy_type(xp->type, skb) < 0)
2174 return nlmsg_end(skb, nlh);
2177 nlmsg_cancel(skb, nlh);
2181 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2182 struct xfrm_policy *xp, int dir)
2184 struct sk_buff *skb;
2186 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2190 if (build_acquire(skb, x, xt, xp, dir) < 0)
2193 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2196 /* User gives us xfrm_user_policy_info followed by an array of 0
2197 * or more templates.
2199 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2200 u8 *data, int len, int *dir)
2202 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2203 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2204 struct xfrm_policy *xp;
2207 switch (sk->sk_family) {
2209 if (opt != IP_XFRM_POLICY) {
2214 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2216 if (opt != IPV6_XFRM_POLICY) {
2229 if (len < sizeof(*p) ||
2230 verify_newpolicy_info(p))
2233 nr = ((len - sizeof(*p)) / sizeof(*ut));
2234 if (validate_tmpl(nr, ut, p->sel.family))
2237 if (p->dir > XFRM_POLICY_OUT)
2240 xp = xfrm_policy_alloc(GFP_KERNEL);
2246 copy_from_user_policy(xp, p);
2247 xp->type = XFRM_POLICY_TYPE_MAIN;
2248 copy_templates(xp, ut, nr);
2255 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2257 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2258 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2259 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2260 + userpolicy_type_attrsize();
2263 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2264 int dir, struct km_event *c)
2266 struct xfrm_user_polexpire *upe;
2267 struct nlmsghdr *nlh;
2268 int hard = c->data.hard;
2270 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2274 upe = nlmsg_data(nlh);
2275 copy_to_user_policy(xp, &upe->pol, dir);
2276 if (copy_to_user_tmpl(xp, skb) < 0)
2278 if (copy_to_user_sec_ctx(xp, skb))
2280 if (copy_to_user_policy_type(xp->type, skb) < 0)
2284 return nlmsg_end(skb, nlh);
2287 nlmsg_cancel(skb, nlh);
2291 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2293 struct sk_buff *skb;
2295 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2299 if (build_polexpire(skb, xp, dir, c) < 0)
2302 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2305 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2307 struct xfrm_userpolicy_info *p;
2308 struct xfrm_userpolicy_id *id;
2309 struct nlmsghdr *nlh;
2310 struct sk_buff *skb;
2311 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2314 headlen = sizeof(*p);
2315 if (c->event == XFRM_MSG_DELPOLICY) {
2316 len += nla_total_size(headlen);
2317 headlen = sizeof(*id);
2319 len += userpolicy_type_attrsize();
2320 len += NLMSG_ALIGN(headlen);
2322 skb = nlmsg_new(len, GFP_ATOMIC);
2326 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2330 p = nlmsg_data(nlh);
2331 if (c->event == XFRM_MSG_DELPOLICY) {
2332 struct nlattr *attr;
2334 id = nlmsg_data(nlh);
2335 memset(id, 0, sizeof(*id));
2338 id->index = xp->index;
2340 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2342 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2349 copy_to_user_policy(xp, p, dir);
2350 if (copy_to_user_tmpl(xp, skb) < 0)
2352 if (copy_to_user_policy_type(xp->type, skb) < 0)
2355 nlmsg_end(skb, nlh);
2357 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2364 static int xfrm_notify_policy_flush(struct km_event *c)
2366 struct nlmsghdr *nlh;
2367 struct sk_buff *skb;
2369 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2373 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2376 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2379 nlmsg_end(skb, nlh);
2381 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2388 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2392 case XFRM_MSG_NEWPOLICY:
2393 case XFRM_MSG_UPDPOLICY:
2394 case XFRM_MSG_DELPOLICY:
2395 return xfrm_notify_policy(xp, dir, c);
2396 case XFRM_MSG_FLUSHPOLICY:
2397 return xfrm_notify_policy_flush(c);
2398 case XFRM_MSG_POLEXPIRE:
2399 return xfrm_exp_policy_notify(xp, dir, c);
2401 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2408 static inline size_t xfrm_report_msgsize(void)
2410 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2413 static int build_report(struct sk_buff *skb, u8 proto,
2414 struct xfrm_selector *sel, xfrm_address_t *addr)
2416 struct xfrm_user_report *ur;
2417 struct nlmsghdr *nlh;
2419 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2423 ur = nlmsg_data(nlh);
2425 memcpy(&ur->sel, sel, sizeof(ur->sel));
2428 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2430 return nlmsg_end(skb, nlh);
2433 nlmsg_cancel(skb, nlh);
2437 static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2438 xfrm_address_t *addr)
2440 struct sk_buff *skb;
2442 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2446 if (build_report(skb, proto, sel, addr) < 0)
2449 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2452 static struct xfrm_mgr netlink_mgr = {
2454 .notify = xfrm_send_state_notify,
2455 .acquire = xfrm_send_acquire,
2456 .compile_policy = xfrm_compile_policy,
2457 .notify_policy = xfrm_send_policy_notify,
2458 .report = xfrm_send_report,
2459 .migrate = xfrm_send_migrate,
2462 static int __init xfrm_user_init(void)
2466 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2468 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
2469 xfrm_netlink_rcv, NULL, THIS_MODULE);
2472 rcu_assign_pointer(xfrm_nl, nlsk);
2474 xfrm_register_km(&netlink_mgr);
2479 static void __exit xfrm_user_exit(void)
2481 struct sock *nlsk = xfrm_nl;
2483 xfrm_unregister_km(&netlink_mgr);
2484 rcu_assign_pointer(xfrm_nl, NULL);
2486 netlink_kernel_release(nlsk);
2489 module_init(xfrm_user_init);
2490 module_exit(xfrm_user_exit);
2491 MODULE_LICENSE("GPL");
2492 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);