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))
61 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
65 static int verify_auth_trunc(struct nlattr **attrs)
67 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
68 struct xfrm_algo_auth *algp;
74 if (nla_len(rt) < xfrm_alg_auth_len(algp))
77 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
81 static int verify_aead(struct nlattr **attrs)
83 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
84 struct xfrm_algo_aead *algp;
90 if (nla_len(rt) < aead_len(algp))
93 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
97 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
98 xfrm_address_t **addrp)
100 struct nlattr *rt = attrs[type];
103 *addrp = nla_data(rt);
106 static inline int verify_sec_ctx_len(struct nlattr **attrs)
108 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
109 struct xfrm_user_sec_ctx *uctx;
115 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
122 static int verify_newsa_info(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
133 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
145 switch (p->id.proto) {
147 if ((!attrs[XFRMA_ALG_AUTH] &&
148 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
149 attrs[XFRMA_ALG_AEAD] ||
150 attrs[XFRMA_ALG_CRYPT] ||
151 attrs[XFRMA_ALG_COMP])
156 if (attrs[XFRMA_ALG_COMP])
158 if (!attrs[XFRMA_ALG_AUTH] &&
159 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
160 !attrs[XFRMA_ALG_CRYPT] &&
161 !attrs[XFRMA_ALG_AEAD])
163 if ((attrs[XFRMA_ALG_AUTH] ||
164 attrs[XFRMA_ALG_AUTH_TRUNC] ||
165 attrs[XFRMA_ALG_CRYPT]) &&
166 attrs[XFRMA_ALG_AEAD])
171 if (!attrs[XFRMA_ALG_COMP] ||
172 attrs[XFRMA_ALG_AEAD] ||
173 attrs[XFRMA_ALG_AUTH] ||
174 attrs[XFRMA_ALG_AUTH_TRUNC] ||
175 attrs[XFRMA_ALG_CRYPT])
179 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS:
181 case IPPROTO_ROUTING:
182 if (attrs[XFRMA_ALG_COMP] ||
183 attrs[XFRMA_ALG_AUTH] ||
184 attrs[XFRMA_ALG_AUTH_TRUNC] ||
185 attrs[XFRMA_ALG_AEAD] ||
186 attrs[XFRMA_ALG_CRYPT] ||
187 attrs[XFRMA_ENCAP] ||
188 attrs[XFRMA_SEC_CTX] ||
189 !attrs[XFRMA_COADDR])
198 if ((err = verify_aead(attrs)))
200 if ((err = verify_auth_trunc(attrs)))
202 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
204 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
206 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
208 if ((err = verify_sec_ctx_len(attrs)))
213 case XFRM_MODE_TRANSPORT:
214 case XFRM_MODE_TUNNEL:
215 case XFRM_MODE_ROUTEOPTIMIZATION:
229 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
230 struct xfrm_algo_desc *(*get_byname)(char *, int),
233 struct xfrm_algo *p, *ualg;
234 struct xfrm_algo_desc *algo;
239 ualg = nla_data(rta);
241 algo = get_byname(ualg->alg_name, 1);
244 *props = algo->desc.sadb_alg_id;
246 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
250 strcpy(p->alg_name, algo->name);
255 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
258 struct xfrm_algo *ualg;
259 struct xfrm_algo_auth *p;
260 struct xfrm_algo_desc *algo;
265 ualg = nla_data(rta);
267 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
270 *props = algo->desc.sadb_alg_id;
272 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
276 strcpy(p->alg_name, algo->name);
277 p->alg_key_len = ualg->alg_key_len;
278 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
279 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
285 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
288 struct xfrm_algo_auth *p, *ualg;
289 struct xfrm_algo_desc *algo;
294 ualg = nla_data(rta);
296 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
299 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
301 *props = algo->desc.sadb_alg_id;
303 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
307 strcpy(p->alg_name, algo->name);
308 if (!p->alg_trunc_len)
309 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
315 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
318 struct xfrm_algo_aead *p, *ualg;
319 struct xfrm_algo_desc *algo;
324 ualg = nla_data(rta);
326 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
329 *props = algo->desc.sadb_alg_id;
331 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
335 strcpy(p->alg_name, algo->name);
340 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
345 len += sizeof(struct xfrm_user_sec_ctx);
346 len += xfrm_ctx->ctx_len;
351 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
353 memcpy(&x->id, &p->id, sizeof(x->id));
354 memcpy(&x->sel, &p->sel, sizeof(x->sel));
355 memcpy(&x->lft, &p->lft, sizeof(x->lft));
356 x->props.mode = p->mode;
357 x->props.replay_window = p->replay_window;
358 x->props.reqid = p->reqid;
359 x->props.family = p->family;
360 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
361 x->props.flags = p->flags;
363 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
364 x->sel.family = p->family;
368 * someday when pfkey also has support, we could have the code
369 * somehow made shareable and move it to xfrm_state.c - JHS
372 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
374 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
375 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
376 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
377 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
380 struct xfrm_replay_state *replay;
381 replay = nla_data(rp);
382 memcpy(&x->replay, replay, sizeof(*replay));
383 memcpy(&x->preplay, replay, sizeof(*replay));
387 struct xfrm_lifetime_cur *ltime;
388 ltime = nla_data(lt);
389 x->curlft.bytes = ltime->bytes;
390 x->curlft.packets = ltime->packets;
391 x->curlft.add_time = ltime->add_time;
392 x->curlft.use_time = ltime->use_time;
396 x->replay_maxage = nla_get_u32(et);
399 x->replay_maxdiff = nla_get_u32(rt);
402 static struct xfrm_state *xfrm_state_construct(struct net *net,
403 struct xfrm_usersa_info *p,
404 struct nlattr **attrs,
407 struct xfrm_state *x = xfrm_state_alloc(net);
413 copy_from_user_state(x, p);
415 if ((err = attach_aead(&x->aead, &x->props.ealgo,
416 attrs[XFRMA_ALG_AEAD])))
418 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
419 attrs[XFRMA_ALG_AUTH_TRUNC])))
421 if (!x->props.aalgo) {
422 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
423 attrs[XFRMA_ALG_AUTH])))
426 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
427 xfrm_ealg_get_byname,
428 attrs[XFRMA_ALG_CRYPT])))
430 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
431 xfrm_calg_get_byname,
432 attrs[XFRMA_ALG_COMP])))
435 if (attrs[XFRMA_ENCAP]) {
436 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
437 sizeof(*x->encap), GFP_KERNEL);
438 if (x->encap == NULL)
442 if (attrs[XFRMA_COADDR]) {
443 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
444 sizeof(*x->coaddr), GFP_KERNEL);
445 if (x->coaddr == NULL)
449 err = xfrm_init_state(x);
453 if (attrs[XFRMA_SEC_CTX] &&
454 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
458 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
459 /* sysctl_xfrm_aevent_etime is in 100ms units */
460 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
461 x->preplay.bitmap = 0;
462 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
463 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
465 /* override default values from above */
467 xfrm_update_ae_params(x, attrs);
472 x->km.state = XFRM_STATE_DEAD;
479 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
480 struct nlattr **attrs)
482 struct net *net = sock_net(skb->sk);
483 struct xfrm_usersa_info *p = nlmsg_data(nlh);
484 struct xfrm_state *x;
487 uid_t loginuid = NETLINK_CB(skb).loginuid;
488 u32 sessionid = NETLINK_CB(skb).sessionid;
489 u32 sid = NETLINK_CB(skb).sid;
491 err = verify_newsa_info(p, attrs);
495 x = xfrm_state_construct(net, p, attrs, &err);
500 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
501 err = xfrm_state_add(x);
503 err = xfrm_state_update(x);
505 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
508 x->km.state = XFRM_STATE_DEAD;
513 c.seq = nlh->nlmsg_seq;
514 c.pid = nlh->nlmsg_pid;
515 c.event = nlh->nlmsg_type;
517 km_state_notify(x, &c);
523 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
524 struct xfrm_usersa_id *p,
525 struct nlattr **attrs,
528 struct xfrm_state *x = NULL;
531 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
533 x = xfrm_state_lookup(net, &p->daddr, p->spi, p->proto, p->family);
535 xfrm_address_t *saddr = NULL;
537 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
544 x = xfrm_state_lookup_byaddr(net, &p->daddr, saddr,
545 p->proto, p->family);
554 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
555 struct nlattr **attrs)
557 struct net *net = sock_net(skb->sk);
558 struct xfrm_state *x;
561 struct xfrm_usersa_id *p = nlmsg_data(nlh);
562 uid_t loginuid = NETLINK_CB(skb).loginuid;
563 u32 sessionid = NETLINK_CB(skb).sessionid;
564 u32 sid = NETLINK_CB(skb).sid;
566 x = xfrm_user_state_lookup(net, p, attrs, &err);
570 if ((err = security_xfrm_state_delete(x)) != 0)
573 if (xfrm_state_kern(x)) {
578 err = xfrm_state_delete(x);
583 c.seq = nlh->nlmsg_seq;
584 c.pid = nlh->nlmsg_pid;
585 c.event = nlh->nlmsg_type;
586 km_state_notify(x, &c);
589 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
594 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
596 memcpy(&p->id, &x->id, sizeof(p->id));
597 memcpy(&p->sel, &x->sel, sizeof(p->sel));
598 memcpy(&p->lft, &x->lft, sizeof(p->lft));
599 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
600 memcpy(&p->stats, &x->stats, sizeof(p->stats));
601 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
602 p->mode = x->props.mode;
603 p->replay_window = x->props.replay_window;
604 p->reqid = x->props.reqid;
605 p->family = x->props.family;
606 p->flags = x->props.flags;
610 struct xfrm_dump_info {
611 struct sk_buff *in_skb;
612 struct sk_buff *out_skb;
617 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
619 struct xfrm_user_sec_ctx *uctx;
621 int ctx_size = sizeof(*uctx) + s->ctx_len;
623 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
627 uctx = nla_data(attr);
628 uctx->exttype = XFRMA_SEC_CTX;
629 uctx->len = ctx_size;
630 uctx->ctx_doi = s->ctx_doi;
631 uctx->ctx_alg = s->ctx_alg;
632 uctx->ctx_len = s->ctx_len;
633 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
638 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
640 struct xfrm_algo *algo;
643 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
644 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
648 algo = nla_data(nla);
649 strcpy(algo->alg_name, auth->alg_name);
650 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
651 algo->alg_key_len = auth->alg_key_len;
656 /* Don't change this without updating xfrm_sa_len! */
657 static int copy_to_user_state_extra(struct xfrm_state *x,
658 struct xfrm_usersa_info *p,
661 copy_to_user_state(x, p);
664 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
667 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
670 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
672 if (copy_to_user_auth(x->aalg, skb))
673 goto nla_put_failure;
675 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
676 xfrm_alg_auth_len(x->aalg), x->aalg);
679 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
681 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
684 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
686 if (x->security && copy_sec_ctx(x->security, skb) < 0)
687 goto nla_put_failure;
695 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
697 struct xfrm_dump_info *sp = ptr;
698 struct sk_buff *in_skb = sp->in_skb;
699 struct sk_buff *skb = sp->out_skb;
700 struct xfrm_usersa_info *p;
701 struct nlmsghdr *nlh;
704 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
705 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
711 err = copy_to_user_state_extra(x, p, skb);
713 goto nla_put_failure;
719 nlmsg_cancel(skb, nlh);
723 static int xfrm_dump_sa_done(struct netlink_callback *cb)
725 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
726 xfrm_state_walk_done(walk);
730 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
732 struct net *net = sock_net(skb->sk);
733 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
734 struct xfrm_dump_info info;
736 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
737 sizeof(cb->args) - sizeof(cb->args[0]));
739 info.in_skb = cb->skb;
741 info.nlmsg_seq = cb->nlh->nlmsg_seq;
742 info.nlmsg_flags = NLM_F_MULTI;
746 xfrm_state_walk_init(walk, 0);
749 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
754 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
755 struct xfrm_state *x, u32 seq)
757 struct xfrm_dump_info info;
760 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
762 return ERR_PTR(-ENOMEM);
764 info.in_skb = in_skb;
766 info.nlmsg_seq = seq;
767 info.nlmsg_flags = 0;
769 if (dump_one_state(x, 0, &info)) {
777 static inline size_t xfrm_spdinfo_msgsize(void)
779 return NLMSG_ALIGN(4)
780 + nla_total_size(sizeof(struct xfrmu_spdinfo))
781 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
784 static int build_spdinfo(struct sk_buff *skb, struct net *net,
785 u32 pid, u32 seq, u32 flags)
787 struct xfrmk_spdinfo si;
788 struct xfrmu_spdinfo spc;
789 struct xfrmu_spdhinfo sph;
790 struct nlmsghdr *nlh;
793 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
794 if (nlh == NULL) /* shouldnt really happen ... */
799 xfrm_spd_getinfo(net, &si);
800 spc.incnt = si.incnt;
801 spc.outcnt = si.outcnt;
802 spc.fwdcnt = si.fwdcnt;
803 spc.inscnt = si.inscnt;
804 spc.outscnt = si.outscnt;
805 spc.fwdscnt = si.fwdscnt;
806 sph.spdhcnt = si.spdhcnt;
807 sph.spdhmcnt = si.spdhmcnt;
809 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
810 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
812 return nlmsg_end(skb, nlh);
815 nlmsg_cancel(skb, nlh);
819 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
820 struct nlattr **attrs)
822 struct net *net = sock_net(skb->sk);
823 struct sk_buff *r_skb;
824 u32 *flags = nlmsg_data(nlh);
825 u32 spid = NETLINK_CB(skb).pid;
826 u32 seq = nlh->nlmsg_seq;
828 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
832 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
835 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
838 static inline size_t xfrm_sadinfo_msgsize(void)
840 return NLMSG_ALIGN(4)
841 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
842 + nla_total_size(4); /* XFRMA_SAD_CNT */
845 static int build_sadinfo(struct sk_buff *skb, struct net *net,
846 u32 pid, u32 seq, u32 flags)
848 struct xfrmk_sadinfo si;
849 struct xfrmu_sadhinfo sh;
850 struct nlmsghdr *nlh;
853 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
854 if (nlh == NULL) /* shouldnt really happen ... */
859 xfrm_sad_getinfo(net, &si);
861 sh.sadhmcnt = si.sadhmcnt;
862 sh.sadhcnt = si.sadhcnt;
864 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
865 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
867 return nlmsg_end(skb, nlh);
870 nlmsg_cancel(skb, nlh);
874 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
875 struct nlattr **attrs)
877 struct net *net = sock_net(skb->sk);
878 struct sk_buff *r_skb;
879 u32 *flags = nlmsg_data(nlh);
880 u32 spid = NETLINK_CB(skb).pid;
881 u32 seq = nlh->nlmsg_seq;
883 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
887 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
890 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
893 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
894 struct nlattr **attrs)
896 struct net *net = sock_net(skb->sk);
897 struct xfrm_usersa_id *p = nlmsg_data(nlh);
898 struct xfrm_state *x;
899 struct sk_buff *resp_skb;
902 x = xfrm_user_state_lookup(net, p, attrs, &err);
906 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
907 if (IS_ERR(resp_skb)) {
908 err = PTR_ERR(resp_skb);
910 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
917 static int verify_userspi_info(struct xfrm_userspi_info *p)
919 switch (p->info.id.proto) {
925 /* IPCOMP spi is 16-bits. */
926 if (p->max >= 0x10000)
940 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
941 struct nlattr **attrs)
943 struct net *net = sock_net(skb->sk);
944 struct xfrm_state *x;
945 struct xfrm_userspi_info *p;
946 struct sk_buff *resp_skb;
947 xfrm_address_t *daddr;
952 err = verify_userspi_info(p);
956 family = p->info.family;
957 daddr = &p->info.id.daddr;
961 x = xfrm_find_acq_byseq(net, p->info.seq);
962 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
969 x = xfrm_find_acq(net, p->info.mode, p->info.reqid,
970 p->info.id.proto, daddr,
977 err = xfrm_alloc_spi(x, p->min, p->max);
981 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
982 if (IS_ERR(resp_skb)) {
983 err = PTR_ERR(resp_skb);
987 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
995 static int verify_policy_dir(u8 dir)
999 case XFRM_POLICY_OUT:
1000 case XFRM_POLICY_FWD:
1010 static int verify_policy_type(u8 type)
1013 case XFRM_POLICY_TYPE_MAIN:
1014 #ifdef CONFIG_XFRM_SUB_POLICY
1015 case XFRM_POLICY_TYPE_SUB:
1026 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1029 case XFRM_SHARE_ANY:
1030 case XFRM_SHARE_SESSION:
1031 case XFRM_SHARE_USER:
1032 case XFRM_SHARE_UNIQUE:
1039 switch (p->action) {
1040 case XFRM_POLICY_ALLOW:
1041 case XFRM_POLICY_BLOCK:
1048 switch (p->sel.family) {
1053 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1056 return -EAFNOSUPPORT;
1063 return verify_policy_dir(p->dir);
1066 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1068 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1069 struct xfrm_user_sec_ctx *uctx;
1074 uctx = nla_data(rt);
1075 return security_xfrm_policy_alloc(&pol->security, uctx);
1078 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1084 for (i = 0; i < nr; i++, ut++) {
1085 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1087 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1088 memcpy(&t->saddr, &ut->saddr,
1089 sizeof(xfrm_address_t));
1090 t->reqid = ut->reqid;
1092 t->share = ut->share;
1093 t->optional = ut->optional;
1094 t->aalgos = ut->aalgos;
1095 t->ealgos = ut->ealgos;
1096 t->calgos = ut->calgos;
1097 /* If all masks are ~0, then we allow all algorithms. */
1098 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1099 t->encap_family = ut->family;
1103 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1107 if (nr > XFRM_MAX_DEPTH)
1110 for (i = 0; i < nr; i++) {
1111 /* We never validated the ut->family value, so many
1112 * applications simply leave it at zero. The check was
1113 * never made and ut->family was ignored because all
1114 * templates could be assumed to have the same family as
1115 * the policy itself. Now that we will have ipv4-in-ipv6
1116 * and ipv6-in-ipv4 tunnels, this is no longer true.
1119 ut[i].family = family;
1121 switch (ut[i].family) {
1124 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1136 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1138 struct nlattr *rt = attrs[XFRMA_TMPL];
1143 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1144 int nr = nla_len(rt) / sizeof(*utmpl);
1147 err = validate_tmpl(nr, utmpl, pol->family);
1151 copy_templates(pol, utmpl, nr);
1156 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1158 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1159 struct xfrm_userpolicy_type *upt;
1160 u8 type = XFRM_POLICY_TYPE_MAIN;
1168 err = verify_policy_type(type);
1176 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1178 xp->priority = p->priority;
1179 xp->index = p->index;
1180 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1181 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1182 xp->action = p->action;
1183 xp->flags = p->flags;
1184 xp->family = p->sel.family;
1185 /* XXX xp->share = p->share; */
1188 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1190 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1191 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1192 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1193 p->priority = xp->priority;
1194 p->index = xp->index;
1195 p->sel.family = xp->family;
1197 p->action = xp->action;
1198 p->flags = xp->flags;
1199 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1202 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1204 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1212 copy_from_user_policy(xp, p);
1214 err = copy_from_user_policy_type(&xp->type, attrs);
1218 if (!(err = copy_from_user_tmpl(xp, attrs)))
1219 err = copy_from_user_sec_ctx(xp, attrs);
1227 xfrm_policy_destroy(xp);
1231 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1232 struct nlattr **attrs)
1234 struct net *net = sock_net(skb->sk);
1235 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1236 struct xfrm_policy *xp;
1240 uid_t loginuid = NETLINK_CB(skb).loginuid;
1241 u32 sessionid = NETLINK_CB(skb).sessionid;
1242 u32 sid = NETLINK_CB(skb).sid;
1244 err = verify_newpolicy_info(p);
1247 err = verify_sec_ctx_len(attrs);
1251 xp = xfrm_policy_construct(net, p, attrs, &err);
1255 /* shouldnt excl be based on nlh flags??
1256 * Aha! this is anti-netlink really i.e more pfkey derived
1257 * in netlink excl is a flag and you wouldnt need
1258 * a type XFRM_MSG_UPDPOLICY - JHS */
1259 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1260 err = xfrm_policy_insert(p->dir, xp, excl);
1261 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1264 security_xfrm_policy_free(xp->security);
1269 c.event = nlh->nlmsg_type;
1270 c.seq = nlh->nlmsg_seq;
1271 c.pid = nlh->nlmsg_pid;
1272 km_policy_notify(xp, p->dir, &c);
1279 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1281 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1284 if (xp->xfrm_nr == 0)
1287 for (i = 0; i < xp->xfrm_nr; i++) {
1288 struct xfrm_user_tmpl *up = &vec[i];
1289 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1291 memcpy(&up->id, &kp->id, sizeof(up->id));
1292 up->family = kp->encap_family;
1293 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1294 up->reqid = kp->reqid;
1295 up->mode = kp->mode;
1296 up->share = kp->share;
1297 up->optional = kp->optional;
1298 up->aalgos = kp->aalgos;
1299 up->ealgos = kp->ealgos;
1300 up->calgos = kp->calgos;
1303 return nla_put(skb, XFRMA_TMPL,
1304 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1307 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1310 return copy_sec_ctx(x->security, skb);
1315 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1318 return copy_sec_ctx(xp->security, skb);
1322 static inline size_t userpolicy_type_attrsize(void)
1324 #ifdef CONFIG_XFRM_SUB_POLICY
1325 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1331 #ifdef CONFIG_XFRM_SUB_POLICY
1332 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1334 struct xfrm_userpolicy_type upt = {
1338 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1342 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1348 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1350 struct xfrm_dump_info *sp = ptr;
1351 struct xfrm_userpolicy_info *p;
1352 struct sk_buff *in_skb = sp->in_skb;
1353 struct sk_buff *skb = sp->out_skb;
1354 struct nlmsghdr *nlh;
1356 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1357 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1361 p = nlmsg_data(nlh);
1362 copy_to_user_policy(xp, p, dir);
1363 if (copy_to_user_tmpl(xp, skb) < 0)
1365 if (copy_to_user_sec_ctx(xp, skb))
1367 if (copy_to_user_policy_type(xp->type, skb) < 0)
1370 nlmsg_end(skb, nlh);
1374 nlmsg_cancel(skb, nlh);
1378 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1380 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1382 xfrm_policy_walk_done(walk);
1386 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1388 struct net *net = sock_net(skb->sk);
1389 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1390 struct xfrm_dump_info info;
1392 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1393 sizeof(cb->args) - sizeof(cb->args[0]));
1395 info.in_skb = cb->skb;
1397 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1398 info.nlmsg_flags = NLM_F_MULTI;
1402 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1405 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1410 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1411 struct xfrm_policy *xp,
1414 struct xfrm_dump_info info;
1415 struct sk_buff *skb;
1417 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1419 return ERR_PTR(-ENOMEM);
1421 info.in_skb = in_skb;
1423 info.nlmsg_seq = seq;
1424 info.nlmsg_flags = 0;
1426 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1434 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1435 struct nlattr **attrs)
1437 struct net *net = sock_net(skb->sk);
1438 struct xfrm_policy *xp;
1439 struct xfrm_userpolicy_id *p;
1440 u8 type = XFRM_POLICY_TYPE_MAIN;
1445 p = nlmsg_data(nlh);
1446 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1448 err = copy_from_user_policy_type(&type, attrs);
1452 err = verify_policy_dir(p->dir);
1457 xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
1459 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1460 struct xfrm_sec_ctx *ctx;
1462 err = verify_sec_ctx_len(attrs);
1468 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1470 err = security_xfrm_policy_alloc(&ctx, uctx);
1474 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
1476 security_xfrm_policy_free(ctx);
1482 struct sk_buff *resp_skb;
1484 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1485 if (IS_ERR(resp_skb)) {
1486 err = PTR_ERR(resp_skb);
1488 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1489 NETLINK_CB(skb).pid);
1492 uid_t loginuid = NETLINK_CB(skb).loginuid;
1493 u32 sessionid = NETLINK_CB(skb).sessionid;
1494 u32 sid = NETLINK_CB(skb).sid;
1496 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1502 c.data.byid = p->index;
1503 c.event = nlh->nlmsg_type;
1504 c.seq = nlh->nlmsg_seq;
1505 c.pid = nlh->nlmsg_pid;
1506 km_policy_notify(xp, p->dir, &c);
1514 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1515 struct nlattr **attrs)
1517 struct net *net = sock_net(skb->sk);
1519 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1520 struct xfrm_audit audit_info;
1523 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1524 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1525 audit_info.secid = NETLINK_CB(skb).sid;
1526 err = xfrm_state_flush(net, p->proto, &audit_info);
1528 if (err == -ESRCH) /* empty table */
1532 c.data.proto = p->proto;
1533 c.event = nlh->nlmsg_type;
1534 c.seq = nlh->nlmsg_seq;
1535 c.pid = nlh->nlmsg_pid;
1537 km_state_notify(NULL, &c);
1542 static inline size_t xfrm_aevent_msgsize(void)
1544 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1545 + nla_total_size(sizeof(struct xfrm_replay_state))
1546 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1547 + nla_total_size(4) /* XFRM_AE_RTHR */
1548 + nla_total_size(4); /* XFRM_AE_ETHR */
1551 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1553 struct xfrm_aevent_id *id;
1554 struct nlmsghdr *nlh;
1556 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1560 id = nlmsg_data(nlh);
1561 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1562 id->sa_id.spi = x->id.spi;
1563 id->sa_id.family = x->props.family;
1564 id->sa_id.proto = x->id.proto;
1565 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1566 id->reqid = x->props.reqid;
1567 id->flags = c->data.aevent;
1569 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1570 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1572 if (id->flags & XFRM_AE_RTHR)
1573 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1575 if (id->flags & XFRM_AE_ETHR)
1576 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1577 x->replay_maxage * 10 / HZ);
1579 return nlmsg_end(skb, nlh);
1582 nlmsg_cancel(skb, nlh);
1586 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1587 struct nlattr **attrs)
1589 struct net *net = sock_net(skb->sk);
1590 struct xfrm_state *x;
1591 struct sk_buff *r_skb;
1594 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1595 struct xfrm_usersa_id *id = &p->sa_id;
1597 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1601 x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family);
1608 * XXX: is this lock really needed - none of the other
1609 * gets lock (the concern is things getting updated
1610 * while we are still reading) - jhs
1612 spin_lock_bh(&x->lock);
1613 c.data.aevent = p->flags;
1614 c.seq = nlh->nlmsg_seq;
1615 c.pid = nlh->nlmsg_pid;
1617 if (build_aevent(r_skb, x, &c) < 0)
1619 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1620 spin_unlock_bh(&x->lock);
1625 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1626 struct nlattr **attrs)
1628 struct net *net = sock_net(skb->sk);
1629 struct xfrm_state *x;
1632 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1633 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1634 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1639 /* pedantic mode - thou shalt sayeth replaceth */
1640 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1643 x = xfrm_state_lookup(net, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1647 if (x->km.state != XFRM_STATE_VALID)
1650 spin_lock_bh(&x->lock);
1651 xfrm_update_ae_params(x, attrs);
1652 spin_unlock_bh(&x->lock);
1654 c.event = nlh->nlmsg_type;
1655 c.seq = nlh->nlmsg_seq;
1656 c.pid = nlh->nlmsg_pid;
1657 c.data.aevent = XFRM_AE_CU;
1658 km_state_notify(x, &c);
1665 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1666 struct nlattr **attrs)
1668 struct net *net = sock_net(skb->sk);
1670 u8 type = XFRM_POLICY_TYPE_MAIN;
1672 struct xfrm_audit audit_info;
1674 err = copy_from_user_policy_type(&type, attrs);
1678 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1679 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1680 audit_info.secid = NETLINK_CB(skb).sid;
1681 err = xfrm_policy_flush(net, type, &audit_info);
1685 c.event = nlh->nlmsg_type;
1686 c.seq = nlh->nlmsg_seq;
1687 c.pid = nlh->nlmsg_pid;
1689 km_policy_notify(NULL, 0, &c);
1693 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1694 struct nlattr **attrs)
1696 struct net *net = sock_net(skb->sk);
1697 struct xfrm_policy *xp;
1698 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1699 struct xfrm_userpolicy_info *p = &up->pol;
1700 u8 type = XFRM_POLICY_TYPE_MAIN;
1703 err = copy_from_user_policy_type(&type, attrs);
1708 xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err);
1710 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1711 struct xfrm_sec_ctx *ctx;
1713 err = verify_sec_ctx_len(attrs);
1719 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1721 err = security_xfrm_policy_alloc(&ctx, uctx);
1725 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err);
1726 security_xfrm_policy_free(ctx);
1731 read_lock(&xp->lock);
1732 if (xp->walk.dead) {
1733 read_unlock(&xp->lock);
1737 read_unlock(&xp->lock);
1740 uid_t loginuid = NETLINK_CB(skb).loginuid;
1741 uid_t sessionid = NETLINK_CB(skb).sessionid;
1742 u32 sid = NETLINK_CB(skb).sid;
1743 xfrm_policy_delete(xp, p->dir);
1744 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1747 // reset the timers here?
1748 printk("Dont know what to do with soft policy expire\n");
1750 km_policy_expired(xp, p->dir, up->hard, current->pid);
1757 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1758 struct nlattr **attrs)
1760 struct net *net = sock_net(skb->sk);
1761 struct xfrm_state *x;
1763 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1764 struct xfrm_usersa_info *p = &ue->state;
1766 x = xfrm_state_lookup(net, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1772 spin_lock_bh(&x->lock);
1774 if (x->km.state != XFRM_STATE_VALID)
1776 km_state_expired(x, ue->hard, current->pid);
1779 uid_t loginuid = NETLINK_CB(skb).loginuid;
1780 uid_t sessionid = NETLINK_CB(skb).sessionid;
1781 u32 sid = NETLINK_CB(skb).sid;
1782 __xfrm_state_delete(x);
1783 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1787 spin_unlock_bh(&x->lock);
1792 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1793 struct nlattr **attrs)
1795 struct net *net = sock_net(skb->sk);
1796 struct xfrm_policy *xp;
1797 struct xfrm_user_tmpl *ut;
1799 struct nlattr *rt = attrs[XFRMA_TMPL];
1801 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1802 struct xfrm_state *x = xfrm_state_alloc(net);
1808 err = verify_newpolicy_info(&ua->policy);
1813 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1817 memcpy(&x->id, &ua->id, sizeof(ua->id));
1818 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1819 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1822 /* extract the templates and for each call km_key */
1823 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1824 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1825 memcpy(&x->id, &t->id, sizeof(x->id));
1826 x->props.mode = t->mode;
1827 x->props.reqid = t->reqid;
1828 x->props.family = ut->family;
1829 t->aalgos = ua->aalgos;
1830 t->ealgos = ua->ealgos;
1831 t->calgos = ua->calgos;
1832 err = km_query(x, t, xp);
1842 printk("BAD policy passed\n");
1849 #ifdef CONFIG_XFRM_MIGRATE
1850 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1851 struct xfrm_kmaddress *k,
1852 struct nlattr **attrs, int *num)
1854 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1855 struct xfrm_user_migrate *um;
1859 struct xfrm_user_kmaddress *uk;
1861 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1862 memcpy(&k->local, &uk->local, sizeof(k->local));
1863 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1864 k->family = uk->family;
1865 k->reserved = uk->reserved;
1869 num_migrate = nla_len(rt) / sizeof(*um);
1871 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1874 for (i = 0; i < num_migrate; i++, um++, ma++) {
1875 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1876 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1877 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1878 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1880 ma->proto = um->proto;
1881 ma->mode = um->mode;
1882 ma->reqid = um->reqid;
1884 ma->old_family = um->old_family;
1885 ma->new_family = um->new_family;
1892 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1893 struct nlattr **attrs)
1895 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1896 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1897 struct xfrm_kmaddress km, *kmp;
1902 if (attrs[XFRMA_MIGRATE] == NULL)
1905 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1907 err = copy_from_user_policy_type(&type, attrs);
1911 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
1918 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
1923 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1924 struct nlattr **attrs)
1926 return -ENOPROTOOPT;
1930 #ifdef CONFIG_XFRM_MIGRATE
1931 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1933 struct xfrm_user_migrate um;
1935 memset(&um, 0, sizeof(um));
1936 um.proto = m->proto;
1938 um.reqid = m->reqid;
1939 um.old_family = m->old_family;
1940 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1941 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1942 um.new_family = m->new_family;
1943 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1944 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1946 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1949 static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1951 struct xfrm_user_kmaddress uk;
1953 memset(&uk, 0, sizeof(uk));
1954 uk.family = k->family;
1955 uk.reserved = k->reserved;
1956 memcpy(&uk.local, &k->local, sizeof(uk.local));
1957 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
1959 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1962 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
1964 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1965 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
1966 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1967 + userpolicy_type_attrsize();
1970 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1971 int num_migrate, struct xfrm_kmaddress *k,
1972 struct xfrm_selector *sel, u8 dir, u8 type)
1974 struct xfrm_migrate *mp;
1975 struct xfrm_userpolicy_id *pol_id;
1976 struct nlmsghdr *nlh;
1979 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1983 pol_id = nlmsg_data(nlh);
1984 /* copy data from selector, dir, and type to the pol_id */
1985 memset(pol_id, 0, sizeof(*pol_id));
1986 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1989 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
1992 if (copy_to_user_policy_type(type, skb) < 0)
1995 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1996 if (copy_to_user_migrate(mp, skb) < 0)
2000 return nlmsg_end(skb, nlh);
2002 nlmsg_cancel(skb, nlh);
2006 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2007 struct xfrm_migrate *m, int num_migrate,
2008 struct xfrm_kmaddress *k)
2010 struct net *net = &init_net;
2011 struct sk_buff *skb;
2013 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2018 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2021 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2024 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2025 struct xfrm_migrate *m, int num_migrate,
2026 struct xfrm_kmaddress *k)
2028 return -ENOPROTOOPT;
2032 #define XMSGSIZE(type) sizeof(struct type)
2034 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2035 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2036 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2037 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2038 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2039 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2040 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2041 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2042 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2043 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2044 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2045 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2046 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2047 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2048 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2049 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2050 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2051 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2052 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2053 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2054 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2059 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2060 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2061 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2062 [XFRMA_LASTUSED] = { .type = NLA_U64},
2063 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2064 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2065 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2066 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2067 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2068 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2069 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2070 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2071 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2072 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2073 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2074 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2075 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2076 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2077 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2078 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2079 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2082 static struct xfrm_link {
2083 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2084 int (*dump)(struct sk_buff *, struct netlink_callback *);
2085 int (*done)(struct netlink_callback *);
2086 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2087 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2088 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2089 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2090 .dump = xfrm_dump_sa,
2091 .done = xfrm_dump_sa_done },
2092 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2093 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2094 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2095 .dump = xfrm_dump_policy,
2096 .done = xfrm_dump_policy_done },
2097 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2098 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2099 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2100 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2101 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2102 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2103 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2104 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2105 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2106 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2107 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2108 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2109 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2112 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2114 struct net *net = sock_net(skb->sk);
2115 struct nlattr *attrs[XFRMA_MAX+1];
2116 struct xfrm_link *link;
2119 type = nlh->nlmsg_type;
2120 if (type > XFRM_MSG_MAX)
2123 type -= XFRM_MSG_BASE;
2124 link = &xfrm_dispatch[type];
2126 /* All operations require privileges, even GET */
2127 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2130 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2131 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2132 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2133 if (link->dump == NULL)
2136 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
2139 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2144 if (link->doit == NULL)
2147 return link->doit(skb, nlh, attrs);
2150 static void xfrm_netlink_rcv(struct sk_buff *skb)
2152 mutex_lock(&xfrm_cfg_mutex);
2153 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2154 mutex_unlock(&xfrm_cfg_mutex);
2157 static inline size_t xfrm_expire_msgsize(void)
2159 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
2162 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
2164 struct xfrm_user_expire *ue;
2165 struct nlmsghdr *nlh;
2167 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2171 ue = nlmsg_data(nlh);
2172 copy_to_user_state(x, &ue->state);
2173 ue->hard = (c->data.hard != 0) ? 1 : 0;
2175 return nlmsg_end(skb, nlh);
2178 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
2180 struct net *net = xs_net(x);
2181 struct sk_buff *skb;
2183 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2187 if (build_expire(skb, x, c) < 0)
2190 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2193 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2195 struct net *net = xs_net(x);
2196 struct sk_buff *skb;
2198 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2202 if (build_aevent(skb, x, c) < 0)
2205 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2208 static int xfrm_notify_sa_flush(struct km_event *c)
2210 struct net *net = c->net;
2211 struct xfrm_usersa_flush *p;
2212 struct nlmsghdr *nlh;
2213 struct sk_buff *skb;
2214 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2216 skb = nlmsg_new(len, GFP_ATOMIC);
2220 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2226 p = nlmsg_data(nlh);
2227 p->proto = c->data.proto;
2229 nlmsg_end(skb, nlh);
2231 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2234 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2238 l += nla_total_size(aead_len(x->aead));
2240 l += nla_total_size(sizeof(struct xfrm_algo) +
2241 (x->aalg->alg_key_len + 7) / 8);
2242 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2245 l += nla_total_size(xfrm_alg_len(x->ealg));
2247 l += nla_total_size(sizeof(*x->calg));
2249 l += nla_total_size(sizeof(*x->encap));
2251 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2252 x->security->ctx_len);
2254 l += nla_total_size(sizeof(*x->coaddr));
2256 /* Must count x->lastused as it may become non-zero behind our back. */
2257 l += nla_total_size(sizeof(u64));
2262 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2264 struct net *net = xs_net(x);
2265 struct xfrm_usersa_info *p;
2266 struct xfrm_usersa_id *id;
2267 struct nlmsghdr *nlh;
2268 struct sk_buff *skb;
2269 int len = xfrm_sa_len(x);
2272 headlen = sizeof(*p);
2273 if (c->event == XFRM_MSG_DELSA) {
2274 len += nla_total_size(headlen);
2275 headlen = sizeof(*id);
2277 len += NLMSG_ALIGN(headlen);
2279 skb = nlmsg_new(len, GFP_ATOMIC);
2283 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2285 goto nla_put_failure;
2287 p = nlmsg_data(nlh);
2288 if (c->event == XFRM_MSG_DELSA) {
2289 struct nlattr *attr;
2291 id = nlmsg_data(nlh);
2292 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2293 id->spi = x->id.spi;
2294 id->family = x->props.family;
2295 id->proto = x->id.proto;
2297 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2299 goto nla_put_failure;
2304 if (copy_to_user_state_extra(x, p, skb))
2305 goto nla_put_failure;
2307 nlmsg_end(skb, nlh);
2309 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2312 /* Somebody screwed up with xfrm_sa_len! */
2318 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2322 case XFRM_MSG_EXPIRE:
2323 return xfrm_exp_state_notify(x, c);
2324 case XFRM_MSG_NEWAE:
2325 return xfrm_aevent_state_notify(x, c);
2326 case XFRM_MSG_DELSA:
2327 case XFRM_MSG_UPDSA:
2328 case XFRM_MSG_NEWSA:
2329 return xfrm_notify_sa(x, c);
2330 case XFRM_MSG_FLUSHSA:
2331 return xfrm_notify_sa_flush(c);
2333 printk("xfrm_user: Unknown SA event %d\n", c->event);
2341 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2342 struct xfrm_policy *xp)
2344 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2345 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2346 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2347 + userpolicy_type_attrsize();
2350 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2351 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2354 struct xfrm_user_acquire *ua;
2355 struct nlmsghdr *nlh;
2356 __u32 seq = xfrm_get_acqseq();
2358 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2362 ua = nlmsg_data(nlh);
2363 memcpy(&ua->id, &x->id, sizeof(ua->id));
2364 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2365 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2366 copy_to_user_policy(xp, &ua->policy, dir);
2367 ua->aalgos = xt->aalgos;
2368 ua->ealgos = xt->ealgos;
2369 ua->calgos = xt->calgos;
2370 ua->seq = x->km.seq = seq;
2372 if (copy_to_user_tmpl(xp, skb) < 0)
2374 if (copy_to_user_state_sec_ctx(x, skb))
2376 if (copy_to_user_policy_type(xp->type, skb) < 0)
2379 return nlmsg_end(skb, nlh);
2382 nlmsg_cancel(skb, nlh);
2386 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2387 struct xfrm_policy *xp, int dir)
2389 struct net *net = xs_net(x);
2390 struct sk_buff *skb;
2392 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2396 if (build_acquire(skb, x, xt, xp, dir) < 0)
2399 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2402 /* User gives us xfrm_user_policy_info followed by an array of 0
2403 * or more templates.
2405 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2406 u8 *data, int len, int *dir)
2408 struct net *net = sock_net(sk);
2409 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2410 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2411 struct xfrm_policy *xp;
2414 switch (sk->sk_family) {
2416 if (opt != IP_XFRM_POLICY) {
2421 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2423 if (opt != IPV6_XFRM_POLICY) {
2436 if (len < sizeof(*p) ||
2437 verify_newpolicy_info(p))
2440 nr = ((len - sizeof(*p)) / sizeof(*ut));
2441 if (validate_tmpl(nr, ut, p->sel.family))
2444 if (p->dir > XFRM_POLICY_OUT)
2447 xp = xfrm_policy_alloc(net, GFP_KERNEL);
2453 copy_from_user_policy(xp, p);
2454 xp->type = XFRM_POLICY_TYPE_MAIN;
2455 copy_templates(xp, ut, nr);
2462 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2464 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2465 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2466 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2467 + userpolicy_type_attrsize();
2470 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2471 int dir, struct km_event *c)
2473 struct xfrm_user_polexpire *upe;
2474 struct nlmsghdr *nlh;
2475 int hard = c->data.hard;
2477 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2481 upe = nlmsg_data(nlh);
2482 copy_to_user_policy(xp, &upe->pol, dir);
2483 if (copy_to_user_tmpl(xp, skb) < 0)
2485 if (copy_to_user_sec_ctx(xp, skb))
2487 if (copy_to_user_policy_type(xp->type, skb) < 0)
2491 return nlmsg_end(skb, nlh);
2494 nlmsg_cancel(skb, nlh);
2498 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2500 struct net *net = xp_net(xp);
2501 struct sk_buff *skb;
2503 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2507 if (build_polexpire(skb, xp, dir, c) < 0)
2510 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2513 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2515 struct net *net = xp_net(xp);
2516 struct xfrm_userpolicy_info *p;
2517 struct xfrm_userpolicy_id *id;
2518 struct nlmsghdr *nlh;
2519 struct sk_buff *skb;
2520 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2523 headlen = sizeof(*p);
2524 if (c->event == XFRM_MSG_DELPOLICY) {
2525 len += nla_total_size(headlen);
2526 headlen = sizeof(*id);
2528 len += userpolicy_type_attrsize();
2529 len += NLMSG_ALIGN(headlen);
2531 skb = nlmsg_new(len, GFP_ATOMIC);
2535 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2539 p = nlmsg_data(nlh);
2540 if (c->event == XFRM_MSG_DELPOLICY) {
2541 struct nlattr *attr;
2543 id = nlmsg_data(nlh);
2544 memset(id, 0, sizeof(*id));
2547 id->index = xp->index;
2549 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2551 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2558 copy_to_user_policy(xp, p, dir);
2559 if (copy_to_user_tmpl(xp, skb) < 0)
2561 if (copy_to_user_policy_type(xp->type, skb) < 0)
2564 nlmsg_end(skb, nlh);
2566 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2573 static int xfrm_notify_policy_flush(struct km_event *c)
2575 struct net *net = c->net;
2576 struct nlmsghdr *nlh;
2577 struct sk_buff *skb;
2579 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2583 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2586 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2589 nlmsg_end(skb, nlh);
2591 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2598 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2602 case XFRM_MSG_NEWPOLICY:
2603 case XFRM_MSG_UPDPOLICY:
2604 case XFRM_MSG_DELPOLICY:
2605 return xfrm_notify_policy(xp, dir, c);
2606 case XFRM_MSG_FLUSHPOLICY:
2607 return xfrm_notify_policy_flush(c);
2608 case XFRM_MSG_POLEXPIRE:
2609 return xfrm_exp_policy_notify(xp, dir, c);
2611 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2618 static inline size_t xfrm_report_msgsize(void)
2620 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2623 static int build_report(struct sk_buff *skb, u8 proto,
2624 struct xfrm_selector *sel, xfrm_address_t *addr)
2626 struct xfrm_user_report *ur;
2627 struct nlmsghdr *nlh;
2629 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2633 ur = nlmsg_data(nlh);
2635 memcpy(&ur->sel, sel, sizeof(ur->sel));
2638 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2640 return nlmsg_end(skb, nlh);
2643 nlmsg_cancel(skb, nlh);
2647 static int xfrm_send_report(struct net *net, u8 proto,
2648 struct xfrm_selector *sel, xfrm_address_t *addr)
2650 struct sk_buff *skb;
2652 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2656 if (build_report(skb, proto, sel, addr) < 0)
2659 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2662 static inline size_t xfrm_mapping_msgsize(void)
2664 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2667 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2668 xfrm_address_t *new_saddr, __be16 new_sport)
2670 struct xfrm_user_mapping *um;
2671 struct nlmsghdr *nlh;
2673 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2677 um = nlmsg_data(nlh);
2679 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2680 um->id.spi = x->id.spi;
2681 um->id.family = x->props.family;
2682 um->id.proto = x->id.proto;
2683 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2684 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2685 um->new_sport = new_sport;
2686 um->old_sport = x->encap->encap_sport;
2687 um->reqid = x->props.reqid;
2689 return nlmsg_end(skb, nlh);
2692 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2695 struct net *net = xs_net(x);
2696 struct sk_buff *skb;
2698 if (x->id.proto != IPPROTO_ESP)
2704 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2708 if (build_mapping(skb, x, ipaddr, sport) < 0)
2711 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2714 static struct xfrm_mgr netlink_mgr = {
2716 .notify = xfrm_send_state_notify,
2717 .acquire = xfrm_send_acquire,
2718 .compile_policy = xfrm_compile_policy,
2719 .notify_policy = xfrm_send_policy_notify,
2720 .report = xfrm_send_report,
2721 .migrate = xfrm_send_migrate,
2722 .new_mapping = xfrm_send_mapping,
2725 static int __net_init xfrm_user_net_init(struct net *net)
2729 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2730 xfrm_netlink_rcv, NULL, THIS_MODULE);
2733 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2734 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2738 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2741 list_for_each_entry(net, net_exit_list, exit_list)
2742 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2744 list_for_each_entry(net, net_exit_list, exit_list)
2745 netlink_kernel_release(net->xfrm.nlsk_stash);
2748 static struct pernet_operations xfrm_user_net_ops = {
2749 .init = xfrm_user_net_init,
2750 .exit_batch = xfrm_user_net_exit,
2753 static int __init xfrm_user_init(void)
2757 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2759 rv = register_pernet_subsys(&xfrm_user_net_ops);
2762 rv = xfrm_register_km(&netlink_mgr);
2764 unregister_pernet_subsys(&xfrm_user_net_ops);
2768 static void __exit xfrm_user_exit(void)
2770 xfrm_unregister_km(&netlink_mgr);
2771 unregister_pernet_subsys(&xfrm_user_net_ops);
2774 module_init(xfrm_user_init);
2775 module_exit(xfrm_user_exit);
2776 MODULE_LICENSE("GPL");
2777 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);