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>
36 static inline int aead_len(struct xfrm_algo_aead *alg)
38 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
41 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
43 struct nlattr *rt = attrs[type];
44 struct xfrm_algo *algp;
50 if (nla_len(rt) < xfrm_alg_len(algp))
63 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
67 static int verify_auth_trunc(struct nlattr **attrs)
69 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
70 struct xfrm_algo_auth *algp;
76 if (nla_len(rt) < xfrm_alg_auth_len(algp))
79 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
83 static int verify_aead(struct nlattr **attrs)
85 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
86 struct xfrm_algo_aead *algp;
92 if (nla_len(rt) < aead_len(algp))
95 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
99 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
100 xfrm_address_t **addrp)
102 struct nlattr *rt = attrs[type];
105 *addrp = nla_data(rt);
108 static inline int verify_sec_ctx_len(struct nlattr **attrs)
110 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
111 struct xfrm_user_sec_ctx *uctx;
117 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
124 static int verify_newsa_info(struct xfrm_usersa_info *p,
125 struct nlattr **attrs)
135 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 switch (p->id.proto) {
149 if ((!attrs[XFRMA_ALG_AUTH] &&
150 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
151 attrs[XFRMA_ALG_AEAD] ||
152 attrs[XFRMA_ALG_CRYPT] ||
153 attrs[XFRMA_ALG_COMP])
158 if (attrs[XFRMA_ALG_COMP])
160 if (!attrs[XFRMA_ALG_AUTH] &&
161 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
162 !attrs[XFRMA_ALG_CRYPT] &&
163 !attrs[XFRMA_ALG_AEAD])
165 if ((attrs[XFRMA_ALG_AUTH] ||
166 attrs[XFRMA_ALG_AUTH_TRUNC] ||
167 attrs[XFRMA_ALG_CRYPT]) &&
168 attrs[XFRMA_ALG_AEAD])
173 if (!attrs[XFRMA_ALG_COMP] ||
174 attrs[XFRMA_ALG_AEAD] ||
175 attrs[XFRMA_ALG_AUTH] ||
176 attrs[XFRMA_ALG_AUTH_TRUNC] ||
177 attrs[XFRMA_ALG_CRYPT])
181 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
182 case IPPROTO_DSTOPTS:
183 case IPPROTO_ROUTING:
184 if (attrs[XFRMA_ALG_COMP] ||
185 attrs[XFRMA_ALG_AUTH] ||
186 attrs[XFRMA_ALG_AUTH_TRUNC] ||
187 attrs[XFRMA_ALG_AEAD] ||
188 attrs[XFRMA_ALG_CRYPT] ||
189 attrs[XFRMA_ENCAP] ||
190 attrs[XFRMA_SEC_CTX] ||
191 !attrs[XFRMA_COADDR])
200 if ((err = verify_aead(attrs)))
202 if ((err = verify_auth_trunc(attrs)))
204 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
206 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
208 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
210 if ((err = verify_sec_ctx_len(attrs)))
215 case XFRM_MODE_TRANSPORT:
216 case XFRM_MODE_TUNNEL:
217 case XFRM_MODE_ROUTEOPTIMIZATION:
231 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
232 struct xfrm_algo_desc *(*get_byname)(char *, int),
235 struct xfrm_algo *p, *ualg;
236 struct xfrm_algo_desc *algo;
241 ualg = nla_data(rta);
243 algo = get_byname(ualg->alg_name, 1);
246 *props = algo->desc.sadb_alg_id;
248 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
252 strcpy(p->alg_name, algo->name);
257 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
260 struct xfrm_algo *ualg;
261 struct xfrm_algo_auth *p;
262 struct xfrm_algo_desc *algo;
267 ualg = nla_data(rta);
269 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
272 *props = algo->desc.sadb_alg_id;
274 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
278 strcpy(p->alg_name, algo->name);
279 p->alg_key_len = ualg->alg_key_len;
280 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
281 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
287 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
290 struct xfrm_algo_auth *p, *ualg;
291 struct xfrm_algo_desc *algo;
296 ualg = nla_data(rta);
298 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
301 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
303 *props = algo->desc.sadb_alg_id;
305 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
309 strcpy(p->alg_name, algo->name);
310 if (!p->alg_trunc_len)
311 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
317 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
320 struct xfrm_algo_aead *p, *ualg;
321 struct xfrm_algo_desc *algo;
326 ualg = nla_data(rta);
328 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
331 *props = algo->desc.sadb_alg_id;
333 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
337 strcpy(p->alg_name, algo->name);
342 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
347 len += sizeof(struct xfrm_user_sec_ctx);
348 len += xfrm_ctx->ctx_len;
353 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
355 memcpy(&x->id, &p->id, sizeof(x->id));
356 memcpy(&x->sel, &p->sel, sizeof(x->sel));
357 memcpy(&x->lft, &p->lft, sizeof(x->lft));
358 x->props.mode = p->mode;
359 x->props.replay_window = p->replay_window;
360 x->props.reqid = p->reqid;
361 x->props.family = p->family;
362 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
363 x->props.flags = p->flags;
365 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
366 x->sel.family = p->family;
370 * someday when pfkey also has support, we could have the code
371 * somehow made shareable and move it to xfrm_state.c - JHS
374 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
376 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
377 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
378 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
379 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
382 struct xfrm_replay_state *replay;
383 replay = nla_data(rp);
384 memcpy(&x->replay, replay, sizeof(*replay));
385 memcpy(&x->preplay, replay, sizeof(*replay));
389 struct xfrm_lifetime_cur *ltime;
390 ltime = nla_data(lt);
391 x->curlft.bytes = ltime->bytes;
392 x->curlft.packets = ltime->packets;
393 x->curlft.add_time = ltime->add_time;
394 x->curlft.use_time = ltime->use_time;
398 x->replay_maxage = nla_get_u32(et);
401 x->replay_maxdiff = nla_get_u32(rt);
404 static struct xfrm_state *xfrm_state_construct(struct net *net,
405 struct xfrm_usersa_info *p,
406 struct nlattr **attrs,
409 struct xfrm_state *x = xfrm_state_alloc(net);
415 copy_from_user_state(x, p);
417 if ((err = attach_aead(&x->aead, &x->props.ealgo,
418 attrs[XFRMA_ALG_AEAD])))
420 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
421 attrs[XFRMA_ALG_AUTH_TRUNC])))
423 if (!x->props.aalgo) {
424 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
425 attrs[XFRMA_ALG_AUTH])))
428 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
429 xfrm_ealg_get_byname,
430 attrs[XFRMA_ALG_CRYPT])))
432 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
433 xfrm_calg_get_byname,
434 attrs[XFRMA_ALG_COMP])))
437 if (attrs[XFRMA_ENCAP]) {
438 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
439 sizeof(*x->encap), GFP_KERNEL);
440 if (x->encap == NULL)
444 if (attrs[XFRMA_COADDR]) {
445 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
446 sizeof(*x->coaddr), GFP_KERNEL);
447 if (x->coaddr == NULL)
451 xfrm_mark_get(attrs, &x->mark);
453 err = xfrm_init_state(x);
457 if (attrs[XFRMA_SEC_CTX] &&
458 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
462 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
463 /* sysctl_xfrm_aevent_etime is in 100ms units */
464 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
465 x->preplay.bitmap = 0;
466 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
467 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
469 /* override default values from above */
471 xfrm_update_ae_params(x, attrs);
476 x->km.state = XFRM_STATE_DEAD;
483 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
484 struct nlattr **attrs)
486 struct net *net = sock_net(skb->sk);
487 struct xfrm_usersa_info *p = nlmsg_data(nlh);
488 struct xfrm_state *x;
491 uid_t loginuid = NETLINK_CB(skb).loginuid;
492 u32 sessionid = NETLINK_CB(skb).sessionid;
493 u32 sid = NETLINK_CB(skb).sid;
495 err = verify_newsa_info(p, attrs);
499 x = xfrm_state_construct(net, p, attrs, &err);
504 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
505 err = xfrm_state_add(x);
507 err = xfrm_state_update(x);
509 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
512 x->km.state = XFRM_STATE_DEAD;
517 c.seq = nlh->nlmsg_seq;
518 c.pid = nlh->nlmsg_pid;
519 c.event = nlh->nlmsg_type;
521 km_state_notify(x, &c);
527 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
528 struct xfrm_usersa_id *p,
529 struct nlattr **attrs,
532 struct xfrm_state *x = NULL;
535 u32 mark = xfrm_mark_get(attrs, &m);
537 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
539 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
541 xfrm_address_t *saddr = NULL;
543 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
550 x = xfrm_state_lookup_byaddr(net, mark,
552 p->proto, p->family);
561 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
562 struct nlattr **attrs)
564 struct net *net = sock_net(skb->sk);
565 struct xfrm_state *x;
568 struct xfrm_usersa_id *p = nlmsg_data(nlh);
569 uid_t loginuid = NETLINK_CB(skb).loginuid;
570 u32 sessionid = NETLINK_CB(skb).sessionid;
571 u32 sid = NETLINK_CB(skb).sid;
573 x = xfrm_user_state_lookup(net, p, attrs, &err);
577 if ((err = security_xfrm_state_delete(x)) != 0)
580 if (xfrm_state_kern(x)) {
585 err = xfrm_state_delete(x);
590 c.seq = nlh->nlmsg_seq;
591 c.pid = nlh->nlmsg_pid;
592 c.event = nlh->nlmsg_type;
593 km_state_notify(x, &c);
596 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
601 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
603 memcpy(&p->id, &x->id, sizeof(p->id));
604 memcpy(&p->sel, &x->sel, sizeof(p->sel));
605 memcpy(&p->lft, &x->lft, sizeof(p->lft));
606 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
607 memcpy(&p->stats, &x->stats, sizeof(p->stats));
608 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
609 p->mode = x->props.mode;
610 p->replay_window = x->props.replay_window;
611 p->reqid = x->props.reqid;
612 p->family = x->props.family;
613 p->flags = x->props.flags;
617 struct xfrm_dump_info {
618 struct sk_buff *in_skb;
619 struct sk_buff *out_skb;
624 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
626 struct xfrm_user_sec_ctx *uctx;
628 int ctx_size = sizeof(*uctx) + s->ctx_len;
630 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
634 uctx = nla_data(attr);
635 uctx->exttype = XFRMA_SEC_CTX;
636 uctx->len = ctx_size;
637 uctx->ctx_doi = s->ctx_doi;
638 uctx->ctx_alg = s->ctx_alg;
639 uctx->ctx_len = s->ctx_len;
640 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
645 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
647 struct xfrm_algo *algo;
650 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
651 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
655 algo = nla_data(nla);
656 strcpy(algo->alg_name, auth->alg_name);
657 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
658 algo->alg_key_len = auth->alg_key_len;
663 /* Don't change this without updating xfrm_sa_len! */
664 static int copy_to_user_state_extra(struct xfrm_state *x,
665 struct xfrm_usersa_info *p,
668 copy_to_user_state(x, p);
671 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
674 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
677 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
679 if (copy_to_user_auth(x->aalg, skb))
680 goto nla_put_failure;
682 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
683 xfrm_alg_auth_len(x->aalg), x->aalg);
686 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
688 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
691 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
693 if (xfrm_mark_put(skb, &x->mark))
694 goto nla_put_failure;
696 if (x->security && copy_sec_ctx(x->security, skb) < 0)
697 goto nla_put_failure;
705 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
707 struct xfrm_dump_info *sp = ptr;
708 struct sk_buff *in_skb = sp->in_skb;
709 struct sk_buff *skb = sp->out_skb;
710 struct xfrm_usersa_info *p;
711 struct nlmsghdr *nlh;
714 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
715 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
721 err = copy_to_user_state_extra(x, p, skb);
723 goto nla_put_failure;
729 nlmsg_cancel(skb, nlh);
733 static int xfrm_dump_sa_done(struct netlink_callback *cb)
735 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
736 xfrm_state_walk_done(walk);
740 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
742 struct net *net = sock_net(skb->sk);
743 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
744 struct xfrm_dump_info info;
746 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
747 sizeof(cb->args) - sizeof(cb->args[0]));
749 info.in_skb = cb->skb;
751 info.nlmsg_seq = cb->nlh->nlmsg_seq;
752 info.nlmsg_flags = NLM_F_MULTI;
756 xfrm_state_walk_init(walk, 0);
759 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
764 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
765 struct xfrm_state *x, u32 seq)
767 struct xfrm_dump_info info;
770 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
772 return ERR_PTR(-ENOMEM);
774 info.in_skb = in_skb;
776 info.nlmsg_seq = seq;
777 info.nlmsg_flags = 0;
779 if (dump_one_state(x, 0, &info)) {
787 static inline size_t xfrm_spdinfo_msgsize(void)
789 return NLMSG_ALIGN(4)
790 + nla_total_size(sizeof(struct xfrmu_spdinfo))
791 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
794 static int build_spdinfo(struct sk_buff *skb, struct net *net,
795 u32 pid, u32 seq, u32 flags)
797 struct xfrmk_spdinfo si;
798 struct xfrmu_spdinfo spc;
799 struct xfrmu_spdhinfo sph;
800 struct nlmsghdr *nlh;
803 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
804 if (nlh == NULL) /* shouldnt really happen ... */
809 xfrm_spd_getinfo(net, &si);
810 spc.incnt = si.incnt;
811 spc.outcnt = si.outcnt;
812 spc.fwdcnt = si.fwdcnt;
813 spc.inscnt = si.inscnt;
814 spc.outscnt = si.outscnt;
815 spc.fwdscnt = si.fwdscnt;
816 sph.spdhcnt = si.spdhcnt;
817 sph.spdhmcnt = si.spdhmcnt;
819 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
820 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
822 return nlmsg_end(skb, nlh);
825 nlmsg_cancel(skb, nlh);
829 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
830 struct nlattr **attrs)
832 struct net *net = sock_net(skb->sk);
833 struct sk_buff *r_skb;
834 u32 *flags = nlmsg_data(nlh);
835 u32 spid = NETLINK_CB(skb).pid;
836 u32 seq = nlh->nlmsg_seq;
838 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
842 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
845 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
848 static inline size_t xfrm_sadinfo_msgsize(void)
850 return NLMSG_ALIGN(4)
851 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
852 + nla_total_size(4); /* XFRMA_SAD_CNT */
855 static int build_sadinfo(struct sk_buff *skb, struct net *net,
856 u32 pid, u32 seq, u32 flags)
858 struct xfrmk_sadinfo si;
859 struct xfrmu_sadhinfo sh;
860 struct nlmsghdr *nlh;
863 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
864 if (nlh == NULL) /* shouldnt really happen ... */
869 xfrm_sad_getinfo(net, &si);
871 sh.sadhmcnt = si.sadhmcnt;
872 sh.sadhcnt = si.sadhcnt;
874 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
875 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
877 return nlmsg_end(skb, nlh);
880 nlmsg_cancel(skb, nlh);
884 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
885 struct nlattr **attrs)
887 struct net *net = sock_net(skb->sk);
888 struct sk_buff *r_skb;
889 u32 *flags = nlmsg_data(nlh);
890 u32 spid = NETLINK_CB(skb).pid;
891 u32 seq = nlh->nlmsg_seq;
893 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
897 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
900 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
903 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
904 struct nlattr **attrs)
906 struct net *net = sock_net(skb->sk);
907 struct xfrm_usersa_id *p = nlmsg_data(nlh);
908 struct xfrm_state *x;
909 struct sk_buff *resp_skb;
912 x = xfrm_user_state_lookup(net, p, attrs, &err);
916 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
917 if (IS_ERR(resp_skb)) {
918 err = PTR_ERR(resp_skb);
920 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
927 static int verify_userspi_info(struct xfrm_userspi_info *p)
929 switch (p->info.id.proto) {
935 /* IPCOMP spi is 16-bits. */
936 if (p->max >= 0x10000)
950 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
951 struct nlattr **attrs)
953 struct net *net = sock_net(skb->sk);
954 struct xfrm_state *x;
955 struct xfrm_userspi_info *p;
956 struct sk_buff *resp_skb;
957 xfrm_address_t *daddr;
964 err = verify_userspi_info(p);
968 family = p->info.family;
969 daddr = &p->info.id.daddr;
973 mark = xfrm_mark_get(attrs, &m);
975 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
976 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
983 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
984 p->info.id.proto, daddr,
991 err = xfrm_alloc_spi(x, p->min, p->max);
995 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
996 if (IS_ERR(resp_skb)) {
997 err = PTR_ERR(resp_skb);
1001 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1009 static int verify_policy_dir(u8 dir)
1012 case XFRM_POLICY_IN:
1013 case XFRM_POLICY_OUT:
1014 case XFRM_POLICY_FWD:
1024 static int verify_policy_type(u8 type)
1027 case XFRM_POLICY_TYPE_MAIN:
1028 #ifdef CONFIG_XFRM_SUB_POLICY
1029 case XFRM_POLICY_TYPE_SUB:
1040 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1043 case XFRM_SHARE_ANY:
1044 case XFRM_SHARE_SESSION:
1045 case XFRM_SHARE_USER:
1046 case XFRM_SHARE_UNIQUE:
1053 switch (p->action) {
1054 case XFRM_POLICY_ALLOW:
1055 case XFRM_POLICY_BLOCK:
1062 switch (p->sel.family) {
1067 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1070 return -EAFNOSUPPORT;
1077 return verify_policy_dir(p->dir);
1080 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1082 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1083 struct xfrm_user_sec_ctx *uctx;
1088 uctx = nla_data(rt);
1089 return security_xfrm_policy_alloc(&pol->security, uctx);
1092 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1098 for (i = 0; i < nr; i++, ut++) {
1099 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1101 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1102 memcpy(&t->saddr, &ut->saddr,
1103 sizeof(xfrm_address_t));
1104 t->reqid = ut->reqid;
1106 t->share = ut->share;
1107 t->optional = ut->optional;
1108 t->aalgos = ut->aalgos;
1109 t->ealgos = ut->ealgos;
1110 t->calgos = ut->calgos;
1111 /* If all masks are ~0, then we allow all algorithms. */
1112 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1113 t->encap_family = ut->family;
1117 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1121 if (nr > XFRM_MAX_DEPTH)
1124 for (i = 0; i < nr; i++) {
1125 /* We never validated the ut->family value, so many
1126 * applications simply leave it at zero. The check was
1127 * never made and ut->family was ignored because all
1128 * templates could be assumed to have the same family as
1129 * the policy itself. Now that we will have ipv4-in-ipv6
1130 * and ipv6-in-ipv4 tunnels, this is no longer true.
1133 ut[i].family = family;
1135 switch (ut[i].family) {
1138 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1150 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1152 struct nlattr *rt = attrs[XFRMA_TMPL];
1157 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1158 int nr = nla_len(rt) / sizeof(*utmpl);
1161 err = validate_tmpl(nr, utmpl, pol->family);
1165 copy_templates(pol, utmpl, nr);
1170 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1172 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1173 struct xfrm_userpolicy_type *upt;
1174 u8 type = XFRM_POLICY_TYPE_MAIN;
1182 err = verify_policy_type(type);
1190 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1192 xp->priority = p->priority;
1193 xp->index = p->index;
1194 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1195 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1196 xp->action = p->action;
1197 xp->flags = p->flags;
1198 xp->family = p->sel.family;
1199 /* XXX xp->share = p->share; */
1202 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1204 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1205 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1206 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1207 p->priority = xp->priority;
1208 p->index = xp->index;
1209 p->sel.family = xp->family;
1211 p->action = xp->action;
1212 p->flags = xp->flags;
1213 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1216 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1218 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1226 copy_from_user_policy(xp, p);
1228 err = copy_from_user_policy_type(&xp->type, attrs);
1232 if (!(err = copy_from_user_tmpl(xp, attrs)))
1233 err = copy_from_user_sec_ctx(xp, attrs);
1241 xfrm_policy_destroy(xp);
1245 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1246 struct nlattr **attrs)
1248 struct net *net = sock_net(skb->sk);
1249 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1250 struct xfrm_policy *xp;
1254 uid_t loginuid = NETLINK_CB(skb).loginuid;
1255 u32 sessionid = NETLINK_CB(skb).sessionid;
1256 u32 sid = NETLINK_CB(skb).sid;
1258 err = verify_newpolicy_info(p);
1261 err = verify_sec_ctx_len(attrs);
1265 xp = xfrm_policy_construct(net, p, attrs, &err);
1269 /* shouldnt excl be based on nlh flags??
1270 * Aha! this is anti-netlink really i.e more pfkey derived
1271 * in netlink excl is a flag and you wouldnt need
1272 * a type XFRM_MSG_UPDPOLICY - JHS */
1273 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1274 err = xfrm_policy_insert(p->dir, xp, excl);
1275 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1278 security_xfrm_policy_free(xp->security);
1283 c.event = nlh->nlmsg_type;
1284 c.seq = nlh->nlmsg_seq;
1285 c.pid = nlh->nlmsg_pid;
1286 km_policy_notify(xp, p->dir, &c);
1293 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1295 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1298 if (xp->xfrm_nr == 0)
1301 for (i = 0; i < xp->xfrm_nr; i++) {
1302 struct xfrm_user_tmpl *up = &vec[i];
1303 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1305 memcpy(&up->id, &kp->id, sizeof(up->id));
1306 up->family = kp->encap_family;
1307 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1308 up->reqid = kp->reqid;
1309 up->mode = kp->mode;
1310 up->share = kp->share;
1311 up->optional = kp->optional;
1312 up->aalgos = kp->aalgos;
1313 up->ealgos = kp->ealgos;
1314 up->calgos = kp->calgos;
1317 return nla_put(skb, XFRMA_TMPL,
1318 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1321 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1324 return copy_sec_ctx(x->security, skb);
1329 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1332 return copy_sec_ctx(xp->security, skb);
1336 static inline size_t userpolicy_type_attrsize(void)
1338 #ifdef CONFIG_XFRM_SUB_POLICY
1339 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1345 #ifdef CONFIG_XFRM_SUB_POLICY
1346 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1348 struct xfrm_userpolicy_type upt = {
1352 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1356 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1362 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1364 struct xfrm_dump_info *sp = ptr;
1365 struct xfrm_userpolicy_info *p;
1366 struct sk_buff *in_skb = sp->in_skb;
1367 struct sk_buff *skb = sp->out_skb;
1368 struct nlmsghdr *nlh;
1370 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1371 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1375 p = nlmsg_data(nlh);
1376 copy_to_user_policy(xp, p, dir);
1377 if (copy_to_user_tmpl(xp, skb) < 0)
1379 if (copy_to_user_sec_ctx(xp, skb))
1381 if (copy_to_user_policy_type(xp->type, skb) < 0)
1384 nlmsg_end(skb, nlh);
1388 nlmsg_cancel(skb, nlh);
1392 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1394 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1396 xfrm_policy_walk_done(walk);
1400 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1402 struct net *net = sock_net(skb->sk);
1403 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1404 struct xfrm_dump_info info;
1406 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1407 sizeof(cb->args) - sizeof(cb->args[0]));
1409 info.in_skb = cb->skb;
1411 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1412 info.nlmsg_flags = NLM_F_MULTI;
1416 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1419 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1424 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1425 struct xfrm_policy *xp,
1428 struct xfrm_dump_info info;
1429 struct sk_buff *skb;
1431 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1433 return ERR_PTR(-ENOMEM);
1435 info.in_skb = in_skb;
1437 info.nlmsg_seq = seq;
1438 info.nlmsg_flags = 0;
1440 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1448 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1449 struct nlattr **attrs)
1451 struct net *net = sock_net(skb->sk);
1452 struct xfrm_policy *xp;
1453 struct xfrm_userpolicy_id *p;
1454 u8 type = XFRM_POLICY_TYPE_MAIN;
1459 p = nlmsg_data(nlh);
1460 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1462 err = copy_from_user_policy_type(&type, attrs);
1466 err = verify_policy_dir(p->dir);
1471 xp = xfrm_policy_byid(net, DUMMY_MARK, type, p->dir, p->index, delete, &err);
1473 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1474 struct xfrm_sec_ctx *ctx;
1476 err = verify_sec_ctx_len(attrs);
1482 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1484 err = security_xfrm_policy_alloc(&ctx, uctx);
1488 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, type, p->dir, &p->sel,
1490 security_xfrm_policy_free(ctx);
1496 struct sk_buff *resp_skb;
1498 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1499 if (IS_ERR(resp_skb)) {
1500 err = PTR_ERR(resp_skb);
1502 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1503 NETLINK_CB(skb).pid);
1506 uid_t loginuid = NETLINK_CB(skb).loginuid;
1507 u32 sessionid = NETLINK_CB(skb).sessionid;
1508 u32 sid = NETLINK_CB(skb).sid;
1510 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1516 c.data.byid = p->index;
1517 c.event = nlh->nlmsg_type;
1518 c.seq = nlh->nlmsg_seq;
1519 c.pid = nlh->nlmsg_pid;
1520 km_policy_notify(xp, p->dir, &c);
1528 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1529 struct nlattr **attrs)
1531 struct net *net = sock_net(skb->sk);
1533 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1534 struct xfrm_audit audit_info;
1537 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1538 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1539 audit_info.secid = NETLINK_CB(skb).sid;
1540 err = xfrm_state_flush(net, p->proto, &audit_info);
1542 if (err == -ESRCH) /* empty table */
1546 c.data.proto = p->proto;
1547 c.event = nlh->nlmsg_type;
1548 c.seq = nlh->nlmsg_seq;
1549 c.pid = nlh->nlmsg_pid;
1551 km_state_notify(NULL, &c);
1556 static inline size_t xfrm_aevent_msgsize(void)
1558 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1559 + nla_total_size(sizeof(struct xfrm_replay_state))
1560 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1561 + nla_total_size(sizeof(struct xfrm_mark))
1562 + nla_total_size(4) /* XFRM_AE_RTHR */
1563 + nla_total_size(4); /* XFRM_AE_ETHR */
1566 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1568 struct xfrm_aevent_id *id;
1569 struct nlmsghdr *nlh;
1571 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1575 id = nlmsg_data(nlh);
1576 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1577 id->sa_id.spi = x->id.spi;
1578 id->sa_id.family = x->props.family;
1579 id->sa_id.proto = x->id.proto;
1580 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1581 id->reqid = x->props.reqid;
1582 id->flags = c->data.aevent;
1584 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1585 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1587 if (id->flags & XFRM_AE_RTHR)
1588 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1590 if (id->flags & XFRM_AE_ETHR)
1591 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1592 x->replay_maxage * 10 / HZ);
1594 if (xfrm_mark_put(skb, &x->mark))
1595 goto nla_put_failure;
1597 return nlmsg_end(skb, nlh);
1600 nlmsg_cancel(skb, nlh);
1604 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1605 struct nlattr **attrs)
1607 struct net *net = sock_net(skb->sk);
1608 struct xfrm_state *x;
1609 struct sk_buff *r_skb;
1614 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1615 struct xfrm_usersa_id *id = &p->sa_id;
1617 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1621 mark = xfrm_mark_get(attrs, &m);
1623 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1630 * XXX: is this lock really needed - none of the other
1631 * gets lock (the concern is things getting updated
1632 * while we are still reading) - jhs
1634 spin_lock_bh(&x->lock);
1635 c.data.aevent = p->flags;
1636 c.seq = nlh->nlmsg_seq;
1637 c.pid = nlh->nlmsg_pid;
1639 if (build_aevent(r_skb, x, &c) < 0)
1641 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1642 spin_unlock_bh(&x->lock);
1647 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1648 struct nlattr **attrs)
1650 struct net *net = sock_net(skb->sk);
1651 struct xfrm_state *x;
1656 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1657 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1658 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1663 /* pedantic mode - thou shalt sayeth replaceth */
1664 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1667 mark = xfrm_mark_get(attrs, &m);
1669 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1673 if (x->km.state != XFRM_STATE_VALID)
1676 spin_lock_bh(&x->lock);
1677 xfrm_update_ae_params(x, attrs);
1678 spin_unlock_bh(&x->lock);
1680 c.event = nlh->nlmsg_type;
1681 c.seq = nlh->nlmsg_seq;
1682 c.pid = nlh->nlmsg_pid;
1683 c.data.aevent = XFRM_AE_CU;
1684 km_state_notify(x, &c);
1691 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1692 struct nlattr **attrs)
1694 struct net *net = sock_net(skb->sk);
1696 u8 type = XFRM_POLICY_TYPE_MAIN;
1698 struct xfrm_audit audit_info;
1700 err = copy_from_user_policy_type(&type, attrs);
1704 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1705 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1706 audit_info.secid = NETLINK_CB(skb).sid;
1707 err = xfrm_policy_flush(net, type, &audit_info);
1709 if (err == -ESRCH) /* empty table */
1715 c.event = nlh->nlmsg_type;
1716 c.seq = nlh->nlmsg_seq;
1717 c.pid = nlh->nlmsg_pid;
1719 km_policy_notify(NULL, 0, &c);
1723 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1724 struct nlattr **attrs)
1726 struct net *net = sock_net(skb->sk);
1727 struct xfrm_policy *xp;
1728 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1729 struct xfrm_userpolicy_info *p = &up->pol;
1730 u8 type = XFRM_POLICY_TYPE_MAIN;
1733 err = copy_from_user_policy_type(&type, attrs);
1738 xp = xfrm_policy_byid(net, DUMMY_MARK, type, p->dir, p->index, 0, &err);
1740 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1741 struct xfrm_sec_ctx *ctx;
1743 err = verify_sec_ctx_len(attrs);
1749 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1751 err = security_xfrm_policy_alloc(&ctx, uctx);
1755 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, type, p->dir,
1756 &p->sel, ctx, 0, &err);
1757 security_xfrm_policy_free(ctx);
1762 read_lock(&xp->lock);
1763 if (xp->walk.dead) {
1764 read_unlock(&xp->lock);
1768 read_unlock(&xp->lock);
1771 uid_t loginuid = NETLINK_CB(skb).loginuid;
1772 uid_t sessionid = NETLINK_CB(skb).sessionid;
1773 u32 sid = NETLINK_CB(skb).sid;
1774 xfrm_policy_delete(xp, p->dir);
1775 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1778 // reset the timers here?
1779 printk("Dont know what to do with soft policy expire\n");
1781 km_policy_expired(xp, p->dir, up->hard, current->pid);
1788 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1789 struct nlattr **attrs)
1791 struct net *net = sock_net(skb->sk);
1792 struct xfrm_state *x;
1794 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1795 struct xfrm_usersa_info *p = &ue->state;
1797 u32 mark = xfrm_mark_get(attrs, &m);;
1799 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1805 spin_lock_bh(&x->lock);
1807 if (x->km.state != XFRM_STATE_VALID)
1809 km_state_expired(x, ue->hard, current->pid);
1812 uid_t loginuid = NETLINK_CB(skb).loginuid;
1813 uid_t sessionid = NETLINK_CB(skb).sessionid;
1814 u32 sid = NETLINK_CB(skb).sid;
1815 __xfrm_state_delete(x);
1816 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1820 spin_unlock_bh(&x->lock);
1825 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1826 struct nlattr **attrs)
1828 struct net *net = sock_net(skb->sk);
1829 struct xfrm_policy *xp;
1830 struct xfrm_user_tmpl *ut;
1832 struct nlattr *rt = attrs[XFRMA_TMPL];
1833 struct xfrm_mark mark;
1835 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1836 struct xfrm_state *x = xfrm_state_alloc(net);
1842 xfrm_mark_get(attrs, &mark);
1844 err = verify_newpolicy_info(&ua->policy);
1849 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1853 memcpy(&x->id, &ua->id, sizeof(ua->id));
1854 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1855 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1856 xp->mark.m = x->mark.m = mark.m;
1857 xp->mark.v = x->mark.v = mark.v;
1859 /* extract the templates and for each call km_key */
1860 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1861 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1862 memcpy(&x->id, &t->id, sizeof(x->id));
1863 x->props.mode = t->mode;
1864 x->props.reqid = t->reqid;
1865 x->props.family = ut->family;
1866 t->aalgos = ua->aalgos;
1867 t->ealgos = ua->ealgos;
1868 t->calgos = ua->calgos;
1869 err = km_query(x, t, xp);
1879 printk("BAD policy passed\n");
1886 #ifdef CONFIG_XFRM_MIGRATE
1887 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1888 struct xfrm_kmaddress *k,
1889 struct nlattr **attrs, int *num)
1891 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1892 struct xfrm_user_migrate *um;
1896 struct xfrm_user_kmaddress *uk;
1898 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1899 memcpy(&k->local, &uk->local, sizeof(k->local));
1900 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1901 k->family = uk->family;
1902 k->reserved = uk->reserved;
1906 num_migrate = nla_len(rt) / sizeof(*um);
1908 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1911 for (i = 0; i < num_migrate; i++, um++, ma++) {
1912 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1913 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1914 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1915 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1917 ma->proto = um->proto;
1918 ma->mode = um->mode;
1919 ma->reqid = um->reqid;
1921 ma->old_family = um->old_family;
1922 ma->new_family = um->new_family;
1929 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1930 struct nlattr **attrs)
1932 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1933 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1934 struct xfrm_kmaddress km, *kmp;
1939 if (attrs[XFRMA_MIGRATE] == NULL)
1942 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1944 err = copy_from_user_policy_type(&type, attrs);
1948 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
1955 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
1960 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1961 struct nlattr **attrs)
1963 return -ENOPROTOOPT;
1967 #ifdef CONFIG_XFRM_MIGRATE
1968 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1970 struct xfrm_user_migrate um;
1972 memset(&um, 0, sizeof(um));
1973 um.proto = m->proto;
1975 um.reqid = m->reqid;
1976 um.old_family = m->old_family;
1977 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1978 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1979 um.new_family = m->new_family;
1980 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1981 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1983 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1986 static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1988 struct xfrm_user_kmaddress uk;
1990 memset(&uk, 0, sizeof(uk));
1991 uk.family = k->family;
1992 uk.reserved = k->reserved;
1993 memcpy(&uk.local, &k->local, sizeof(uk.local));
1994 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
1996 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1999 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2001 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2002 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2003 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2004 + userpolicy_type_attrsize();
2007 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
2008 int num_migrate, struct xfrm_kmaddress *k,
2009 struct xfrm_selector *sel, u8 dir, u8 type)
2011 struct xfrm_migrate *mp;
2012 struct xfrm_userpolicy_id *pol_id;
2013 struct nlmsghdr *nlh;
2016 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2020 pol_id = nlmsg_data(nlh);
2021 /* copy data from selector, dir, and type to the pol_id */
2022 memset(pol_id, 0, sizeof(*pol_id));
2023 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2026 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2029 if (copy_to_user_policy_type(type, skb) < 0)
2032 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2033 if (copy_to_user_migrate(mp, skb) < 0)
2037 return nlmsg_end(skb, nlh);
2039 nlmsg_cancel(skb, nlh);
2043 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2044 struct xfrm_migrate *m, int num_migrate,
2045 struct xfrm_kmaddress *k)
2047 struct net *net = &init_net;
2048 struct sk_buff *skb;
2050 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2055 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2058 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2061 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2062 struct xfrm_migrate *m, int num_migrate,
2063 struct xfrm_kmaddress *k)
2065 return -ENOPROTOOPT;
2069 #define XMSGSIZE(type) sizeof(struct type)
2071 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2072 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2073 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2074 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2075 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2076 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2077 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2078 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2079 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2080 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2081 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2082 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2083 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2084 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2085 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2086 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2087 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2088 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2089 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2090 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2091 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2096 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2097 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2098 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2099 [XFRMA_LASTUSED] = { .type = NLA_U64},
2100 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2101 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2102 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2103 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2104 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2105 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2106 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2107 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2108 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2109 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2110 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2111 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2112 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2113 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2114 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2115 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2116 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2117 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2120 static struct xfrm_link {
2121 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2122 int (*dump)(struct sk_buff *, struct netlink_callback *);
2123 int (*done)(struct netlink_callback *);
2124 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2125 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2126 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2127 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2128 .dump = xfrm_dump_sa,
2129 .done = xfrm_dump_sa_done },
2130 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2131 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2132 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2133 .dump = xfrm_dump_policy,
2134 .done = xfrm_dump_policy_done },
2135 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2136 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2137 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2138 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2139 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2140 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2141 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2142 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2143 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2144 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2145 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2146 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2147 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2150 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2152 struct net *net = sock_net(skb->sk);
2153 struct nlattr *attrs[XFRMA_MAX+1];
2154 struct xfrm_link *link;
2157 type = nlh->nlmsg_type;
2158 if (type > XFRM_MSG_MAX)
2161 type -= XFRM_MSG_BASE;
2162 link = &xfrm_dispatch[type];
2164 /* All operations require privileges, even GET */
2165 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2168 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2169 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2170 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2171 if (link->dump == NULL)
2174 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
2177 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2182 if (link->doit == NULL)
2185 return link->doit(skb, nlh, attrs);
2188 static void xfrm_netlink_rcv(struct sk_buff *skb)
2190 mutex_lock(&xfrm_cfg_mutex);
2191 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2192 mutex_unlock(&xfrm_cfg_mutex);
2195 static inline size_t xfrm_expire_msgsize(void)
2197 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2198 + nla_total_size(sizeof(struct xfrm_mark));
2201 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
2203 struct xfrm_user_expire *ue;
2204 struct nlmsghdr *nlh;
2206 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2210 ue = nlmsg_data(nlh);
2211 copy_to_user_state(x, &ue->state);
2212 ue->hard = (c->data.hard != 0) ? 1 : 0;
2214 if (xfrm_mark_put(skb, &x->mark))
2215 goto nla_put_failure;
2217 return nlmsg_end(skb, nlh);
2223 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
2225 struct net *net = xs_net(x);
2226 struct sk_buff *skb;
2228 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2232 if (build_expire(skb, x, c) < 0) {
2237 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2240 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2242 struct net *net = xs_net(x);
2243 struct sk_buff *skb;
2245 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2249 if (build_aevent(skb, x, c) < 0)
2252 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2255 static int xfrm_notify_sa_flush(struct km_event *c)
2257 struct net *net = c->net;
2258 struct xfrm_usersa_flush *p;
2259 struct nlmsghdr *nlh;
2260 struct sk_buff *skb;
2261 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2263 skb = nlmsg_new(len, GFP_ATOMIC);
2267 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2273 p = nlmsg_data(nlh);
2274 p->proto = c->data.proto;
2276 nlmsg_end(skb, nlh);
2278 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2281 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2285 l += nla_total_size(aead_len(x->aead));
2287 l += nla_total_size(sizeof(struct xfrm_algo) +
2288 (x->aalg->alg_key_len + 7) / 8);
2289 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2292 l += nla_total_size(xfrm_alg_len(x->ealg));
2294 l += nla_total_size(sizeof(*x->calg));
2296 l += nla_total_size(sizeof(*x->encap));
2298 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2299 x->security->ctx_len);
2301 l += nla_total_size(sizeof(*x->coaddr));
2303 /* Must count x->lastused as it may become non-zero behind our back. */
2304 l += nla_total_size(sizeof(u64));
2309 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2311 struct net *net = xs_net(x);
2312 struct xfrm_usersa_info *p;
2313 struct xfrm_usersa_id *id;
2314 struct nlmsghdr *nlh;
2315 struct sk_buff *skb;
2316 int len = xfrm_sa_len(x);
2319 headlen = sizeof(*p);
2320 if (c->event == XFRM_MSG_DELSA) {
2321 len += nla_total_size(headlen);
2322 headlen = sizeof(*id);
2323 len += nla_total_size(sizeof(struct xfrm_mark));
2325 len += NLMSG_ALIGN(headlen);
2327 skb = nlmsg_new(len, GFP_ATOMIC);
2331 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2333 goto nla_put_failure;
2335 p = nlmsg_data(nlh);
2336 if (c->event == XFRM_MSG_DELSA) {
2337 struct nlattr *attr;
2339 id = nlmsg_data(nlh);
2340 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2341 id->spi = x->id.spi;
2342 id->family = x->props.family;
2343 id->proto = x->id.proto;
2345 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2347 goto nla_put_failure;
2352 if (copy_to_user_state_extra(x, p, skb))
2353 goto nla_put_failure;
2355 nlmsg_end(skb, nlh);
2357 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2360 /* Somebody screwed up with xfrm_sa_len! */
2366 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2370 case XFRM_MSG_EXPIRE:
2371 return xfrm_exp_state_notify(x, c);
2372 case XFRM_MSG_NEWAE:
2373 return xfrm_aevent_state_notify(x, c);
2374 case XFRM_MSG_DELSA:
2375 case XFRM_MSG_UPDSA:
2376 case XFRM_MSG_NEWSA:
2377 return xfrm_notify_sa(x, c);
2378 case XFRM_MSG_FLUSHSA:
2379 return xfrm_notify_sa_flush(c);
2381 printk("xfrm_user: Unknown SA event %d\n", c->event);
2389 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2390 struct xfrm_policy *xp)
2392 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2393 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2394 + nla_total_size(sizeof(struct xfrm_mark))
2395 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2396 + userpolicy_type_attrsize();
2399 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2400 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2403 struct xfrm_user_acquire *ua;
2404 struct nlmsghdr *nlh;
2405 __u32 seq = xfrm_get_acqseq();
2407 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2411 ua = nlmsg_data(nlh);
2412 memcpy(&ua->id, &x->id, sizeof(ua->id));
2413 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2414 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2415 copy_to_user_policy(xp, &ua->policy, dir);
2416 ua->aalgos = xt->aalgos;
2417 ua->ealgos = xt->ealgos;
2418 ua->calgos = xt->calgos;
2419 ua->seq = x->km.seq = seq;
2421 if (copy_to_user_tmpl(xp, skb) < 0)
2423 if (copy_to_user_state_sec_ctx(x, skb))
2425 if (copy_to_user_policy_type(xp->type, skb) < 0)
2428 return nlmsg_end(skb, nlh);
2431 nlmsg_cancel(skb, nlh);
2435 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2436 struct xfrm_policy *xp, int dir)
2438 struct net *net = xs_net(x);
2439 struct sk_buff *skb;
2441 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2445 if (build_acquire(skb, x, xt, xp, dir) < 0)
2448 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2451 /* User gives us xfrm_user_policy_info followed by an array of 0
2452 * or more templates.
2454 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2455 u8 *data, int len, int *dir)
2457 struct net *net = sock_net(sk);
2458 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2459 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2460 struct xfrm_policy *xp;
2463 switch (sk->sk_family) {
2465 if (opt != IP_XFRM_POLICY) {
2470 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2472 if (opt != IPV6_XFRM_POLICY) {
2485 if (len < sizeof(*p) ||
2486 verify_newpolicy_info(p))
2489 nr = ((len - sizeof(*p)) / sizeof(*ut));
2490 if (validate_tmpl(nr, ut, p->sel.family))
2493 if (p->dir > XFRM_POLICY_OUT)
2496 xp = xfrm_policy_alloc(net, GFP_KERNEL);
2502 copy_from_user_policy(xp, p);
2503 xp->type = XFRM_POLICY_TYPE_MAIN;
2504 copy_templates(xp, ut, nr);
2511 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2513 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2514 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2515 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2516 + userpolicy_type_attrsize();
2519 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2520 int dir, struct km_event *c)
2522 struct xfrm_user_polexpire *upe;
2523 struct nlmsghdr *nlh;
2524 int hard = c->data.hard;
2526 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2530 upe = nlmsg_data(nlh);
2531 copy_to_user_policy(xp, &upe->pol, dir);
2532 if (copy_to_user_tmpl(xp, skb) < 0)
2534 if (copy_to_user_sec_ctx(xp, skb))
2536 if (copy_to_user_policy_type(xp->type, skb) < 0)
2540 return nlmsg_end(skb, nlh);
2543 nlmsg_cancel(skb, nlh);
2547 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2549 struct net *net = xp_net(xp);
2550 struct sk_buff *skb;
2552 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2556 if (build_polexpire(skb, xp, dir, c) < 0)
2559 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2562 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2564 struct net *net = xp_net(xp);
2565 struct xfrm_userpolicy_info *p;
2566 struct xfrm_userpolicy_id *id;
2567 struct nlmsghdr *nlh;
2568 struct sk_buff *skb;
2569 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2572 headlen = sizeof(*p);
2573 if (c->event == XFRM_MSG_DELPOLICY) {
2574 len += nla_total_size(headlen);
2575 headlen = sizeof(*id);
2577 len += userpolicy_type_attrsize();
2578 len += NLMSG_ALIGN(headlen);
2580 skb = nlmsg_new(len, GFP_ATOMIC);
2584 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2588 p = nlmsg_data(nlh);
2589 if (c->event == XFRM_MSG_DELPOLICY) {
2590 struct nlattr *attr;
2592 id = nlmsg_data(nlh);
2593 memset(id, 0, sizeof(*id));
2596 id->index = xp->index;
2598 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2600 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2607 copy_to_user_policy(xp, p, dir);
2608 if (copy_to_user_tmpl(xp, skb) < 0)
2610 if (copy_to_user_policy_type(xp->type, skb) < 0)
2613 nlmsg_end(skb, nlh);
2615 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2622 static int xfrm_notify_policy_flush(struct km_event *c)
2624 struct net *net = c->net;
2625 struct nlmsghdr *nlh;
2626 struct sk_buff *skb;
2628 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2632 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2635 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2638 nlmsg_end(skb, nlh);
2640 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2647 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2651 case XFRM_MSG_NEWPOLICY:
2652 case XFRM_MSG_UPDPOLICY:
2653 case XFRM_MSG_DELPOLICY:
2654 return xfrm_notify_policy(xp, dir, c);
2655 case XFRM_MSG_FLUSHPOLICY:
2656 return xfrm_notify_policy_flush(c);
2657 case XFRM_MSG_POLEXPIRE:
2658 return xfrm_exp_policy_notify(xp, dir, c);
2660 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2667 static inline size_t xfrm_report_msgsize(void)
2669 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2672 static int build_report(struct sk_buff *skb, u8 proto,
2673 struct xfrm_selector *sel, xfrm_address_t *addr)
2675 struct xfrm_user_report *ur;
2676 struct nlmsghdr *nlh;
2678 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2682 ur = nlmsg_data(nlh);
2684 memcpy(&ur->sel, sel, sizeof(ur->sel));
2687 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2689 return nlmsg_end(skb, nlh);
2692 nlmsg_cancel(skb, nlh);
2696 static int xfrm_send_report(struct net *net, u8 proto,
2697 struct xfrm_selector *sel, xfrm_address_t *addr)
2699 struct sk_buff *skb;
2701 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2705 if (build_report(skb, proto, sel, addr) < 0)
2708 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2711 static inline size_t xfrm_mapping_msgsize(void)
2713 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2716 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2717 xfrm_address_t *new_saddr, __be16 new_sport)
2719 struct xfrm_user_mapping *um;
2720 struct nlmsghdr *nlh;
2722 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2726 um = nlmsg_data(nlh);
2728 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2729 um->id.spi = x->id.spi;
2730 um->id.family = x->props.family;
2731 um->id.proto = x->id.proto;
2732 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2733 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2734 um->new_sport = new_sport;
2735 um->old_sport = x->encap->encap_sport;
2736 um->reqid = x->props.reqid;
2738 return nlmsg_end(skb, nlh);
2741 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2744 struct net *net = xs_net(x);
2745 struct sk_buff *skb;
2747 if (x->id.proto != IPPROTO_ESP)
2753 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2757 if (build_mapping(skb, x, ipaddr, sport) < 0)
2760 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2763 static struct xfrm_mgr netlink_mgr = {
2765 .notify = xfrm_send_state_notify,
2766 .acquire = xfrm_send_acquire,
2767 .compile_policy = xfrm_compile_policy,
2768 .notify_policy = xfrm_send_policy_notify,
2769 .report = xfrm_send_report,
2770 .migrate = xfrm_send_migrate,
2771 .new_mapping = xfrm_send_mapping,
2774 static int __net_init xfrm_user_net_init(struct net *net)
2778 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2779 xfrm_netlink_rcv, NULL, THIS_MODULE);
2782 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2783 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2787 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2790 list_for_each_entry(net, net_exit_list, exit_list)
2791 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2793 list_for_each_entry(net, net_exit_list, exit_list)
2794 netlink_kernel_release(net->xfrm.nlsk_stash);
2797 static struct pernet_operations xfrm_user_net_ops = {
2798 .init = xfrm_user_net_init,
2799 .exit_batch = xfrm_user_net_exit,
2802 static int __init xfrm_user_init(void)
2806 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2808 rv = register_pernet_subsys(&xfrm_user_net_ops);
2811 rv = xfrm_register_km(&netlink_mgr);
2813 unregister_pernet_subsys(&xfrm_user_net_ops);
2817 static void __exit xfrm_user_exit(void)
2819 xfrm_unregister_km(&netlink_mgr);
2820 unregister_pernet_subsys(&xfrm_user_net_ops);
2823 module_init(xfrm_user_init);
2824 module_exit(xfrm_user_exit);
2825 MODULE_LICENSE("GPL");
2826 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);