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>
30 #include <asm/uaccess.h>
31 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 #include <linux/in6.h>
35 static inline int aead_len(struct xfrm_algo_aead *alg)
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
40 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
42 struct nlattr *rt = attrs[type];
43 struct xfrm_algo *algp;
49 if (nla_len(rt) < xfrm_alg_len(algp))
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
66 static int verify_auth_trunc(struct nlattr **attrs)
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
82 static int verify_aead(struct nlattr **attrs)
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
91 if (nla_len(rt) < aead_len(algp))
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
98 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
99 xfrm_address_t **addrp)
101 struct nlattr *rt = attrs[type];
104 *addrp = nla_data(rt);
107 static inline int verify_sec_ctx_len(struct nlattr **attrs)
109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
110 struct xfrm_user_sec_ctx *uctx;
116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
123 static int verify_newsa_info(struct xfrm_usersa_info *p,
124 struct nlattr **attrs)
134 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
146 switch (p->id.proto) {
148 if ((!attrs[XFRMA_ALG_AUTH] &&
149 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
150 attrs[XFRMA_ALG_AEAD] ||
151 attrs[XFRMA_ALG_CRYPT] ||
152 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])
170 if (attrs[XFRMA_TFCPAD] &&
171 p->mode != XFRM_MODE_TUNNEL)
176 if (!attrs[XFRMA_ALG_COMP] ||
177 attrs[XFRMA_ALG_AEAD] ||
178 attrs[XFRMA_ALG_AUTH] ||
179 attrs[XFRMA_ALG_AUTH_TRUNC] ||
180 attrs[XFRMA_ALG_CRYPT] ||
185 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
186 case IPPROTO_DSTOPTS:
187 case IPPROTO_ROUTING:
188 if (attrs[XFRMA_ALG_COMP] ||
189 attrs[XFRMA_ALG_AUTH] ||
190 attrs[XFRMA_ALG_AUTH_TRUNC] ||
191 attrs[XFRMA_ALG_AEAD] ||
192 attrs[XFRMA_ALG_CRYPT] ||
193 attrs[XFRMA_ENCAP] ||
194 attrs[XFRMA_SEC_CTX] ||
195 attrs[XFRMA_TFCPAD] ||
196 !attrs[XFRMA_COADDR])
205 if ((err = verify_aead(attrs)))
207 if ((err = verify_auth_trunc(attrs)))
209 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
211 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
213 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
215 if ((err = verify_sec_ctx_len(attrs)))
220 case XFRM_MODE_TRANSPORT:
221 case XFRM_MODE_TUNNEL:
222 case XFRM_MODE_ROUTEOPTIMIZATION:
236 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
237 struct xfrm_algo_desc *(*get_byname)(const char *, int),
240 struct xfrm_algo *p, *ualg;
241 struct xfrm_algo_desc *algo;
246 ualg = nla_data(rta);
248 algo = get_byname(ualg->alg_name, 1);
251 *props = algo->desc.sadb_alg_id;
253 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
257 strcpy(p->alg_name, algo->name);
262 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
265 struct xfrm_algo *ualg;
266 struct xfrm_algo_auth *p;
267 struct xfrm_algo_desc *algo;
272 ualg = nla_data(rta);
274 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
277 *props = algo->desc.sadb_alg_id;
279 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
283 strcpy(p->alg_name, algo->name);
284 p->alg_key_len = ualg->alg_key_len;
285 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
286 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
292 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
295 struct xfrm_algo_auth *p, *ualg;
296 struct xfrm_algo_desc *algo;
301 ualg = nla_data(rta);
303 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
306 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
307 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
309 *props = algo->desc.sadb_alg_id;
311 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
315 strcpy(p->alg_name, algo->name);
316 if (!p->alg_trunc_len)
317 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
323 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
326 struct xfrm_algo_aead *p, *ualg;
327 struct xfrm_algo_desc *algo;
332 ualg = nla_data(rta);
334 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
337 *props = algo->desc.sadb_alg_id;
339 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
343 strcpy(p->alg_name, algo->name);
348 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
353 len += sizeof(struct xfrm_user_sec_ctx);
354 len += xfrm_ctx->ctx_len;
359 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
361 memcpy(&x->id, &p->id, sizeof(x->id));
362 memcpy(&x->sel, &p->sel, sizeof(x->sel));
363 memcpy(&x->lft, &p->lft, sizeof(x->lft));
364 x->props.mode = p->mode;
365 x->props.replay_window = p->replay_window;
366 x->props.reqid = p->reqid;
367 x->props.family = p->family;
368 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
369 x->props.flags = p->flags;
371 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
372 x->sel.family = p->family;
376 * someday when pfkey also has support, we could have the code
377 * somehow made shareable and move it to xfrm_state.c - JHS
380 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
382 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
383 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
384 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
385 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
388 struct xfrm_replay_state *replay;
389 replay = nla_data(rp);
390 memcpy(&x->replay, replay, sizeof(*replay));
391 memcpy(&x->preplay, replay, sizeof(*replay));
395 struct xfrm_lifetime_cur *ltime;
396 ltime = nla_data(lt);
397 x->curlft.bytes = ltime->bytes;
398 x->curlft.packets = ltime->packets;
399 x->curlft.add_time = ltime->add_time;
400 x->curlft.use_time = ltime->use_time;
404 x->replay_maxage = nla_get_u32(et);
407 x->replay_maxdiff = nla_get_u32(rt);
410 static struct xfrm_state *xfrm_state_construct(struct net *net,
411 struct xfrm_usersa_info *p,
412 struct nlattr **attrs,
415 struct xfrm_state *x = xfrm_state_alloc(net);
421 copy_from_user_state(x, p);
423 if ((err = attach_aead(&x->aead, &x->props.ealgo,
424 attrs[XFRMA_ALG_AEAD])))
426 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
427 attrs[XFRMA_ALG_AUTH_TRUNC])))
429 if (!x->props.aalgo) {
430 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
431 attrs[XFRMA_ALG_AUTH])))
434 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
435 xfrm_ealg_get_byname,
436 attrs[XFRMA_ALG_CRYPT])))
438 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
439 xfrm_calg_get_byname,
440 attrs[XFRMA_ALG_COMP])))
443 if (attrs[XFRMA_ENCAP]) {
444 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
445 sizeof(*x->encap), GFP_KERNEL);
446 if (x->encap == NULL)
450 if (attrs[XFRMA_TFCPAD])
451 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
453 if (attrs[XFRMA_COADDR]) {
454 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
455 sizeof(*x->coaddr), GFP_KERNEL);
456 if (x->coaddr == NULL)
460 xfrm_mark_get(attrs, &x->mark);
462 err = xfrm_init_state(x);
466 if (attrs[XFRMA_SEC_CTX] &&
467 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
471 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
472 /* sysctl_xfrm_aevent_etime is in 100ms units */
473 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
474 x->preplay.bitmap = 0;
475 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
476 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
478 /* override default values from above */
480 xfrm_update_ae_params(x, attrs);
485 x->km.state = XFRM_STATE_DEAD;
492 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
493 struct nlattr **attrs)
495 struct net *net = sock_net(skb->sk);
496 struct xfrm_usersa_info *p = nlmsg_data(nlh);
497 struct xfrm_state *x;
500 uid_t loginuid = audit_get_loginuid(current);
501 u32 sessionid = audit_get_sessionid(current);
504 err = verify_newsa_info(p, attrs);
508 x = xfrm_state_construct(net, p, attrs, &err);
513 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
514 err = xfrm_state_add(x);
516 err = xfrm_state_update(x);
518 security_task_getsecid(current, &sid);
519 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
522 x->km.state = XFRM_STATE_DEAD;
527 c.seq = nlh->nlmsg_seq;
528 c.pid = nlh->nlmsg_pid;
529 c.event = nlh->nlmsg_type;
531 km_state_notify(x, &c);
537 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
538 struct xfrm_usersa_id *p,
539 struct nlattr **attrs,
542 struct xfrm_state *x = NULL;
545 u32 mark = xfrm_mark_get(attrs, &m);
547 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
549 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
551 xfrm_address_t *saddr = NULL;
553 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
560 x = xfrm_state_lookup_byaddr(net, mark,
562 p->proto, p->family);
571 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
572 struct nlattr **attrs)
574 struct net *net = sock_net(skb->sk);
575 struct xfrm_state *x;
578 struct xfrm_usersa_id *p = nlmsg_data(nlh);
579 uid_t loginuid = audit_get_loginuid(current);
580 u32 sessionid = audit_get_sessionid(current);
583 x = xfrm_user_state_lookup(net, p, attrs, &err);
587 if ((err = security_xfrm_state_delete(x)) != 0)
590 if (xfrm_state_kern(x)) {
595 err = xfrm_state_delete(x);
600 c.seq = nlh->nlmsg_seq;
601 c.pid = nlh->nlmsg_pid;
602 c.event = nlh->nlmsg_type;
603 km_state_notify(x, &c);
606 security_task_getsecid(current, &sid);
607 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
612 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
614 memcpy(&p->id, &x->id, sizeof(p->id));
615 memcpy(&p->sel, &x->sel, sizeof(p->sel));
616 memcpy(&p->lft, &x->lft, sizeof(p->lft));
617 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
618 memcpy(&p->stats, &x->stats, sizeof(p->stats));
619 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
620 p->mode = x->props.mode;
621 p->replay_window = x->props.replay_window;
622 p->reqid = x->props.reqid;
623 p->family = x->props.family;
624 p->flags = x->props.flags;
628 struct xfrm_dump_info {
629 struct sk_buff *in_skb;
630 struct sk_buff *out_skb;
635 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
637 struct xfrm_user_sec_ctx *uctx;
639 int ctx_size = sizeof(*uctx) + s->ctx_len;
641 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
645 uctx = nla_data(attr);
646 uctx->exttype = XFRMA_SEC_CTX;
647 uctx->len = ctx_size;
648 uctx->ctx_doi = s->ctx_doi;
649 uctx->ctx_alg = s->ctx_alg;
650 uctx->ctx_len = s->ctx_len;
651 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
656 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
658 struct xfrm_algo *algo;
661 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
662 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
666 algo = nla_data(nla);
667 strcpy(algo->alg_name, auth->alg_name);
668 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
669 algo->alg_key_len = auth->alg_key_len;
674 /* Don't change this without updating xfrm_sa_len! */
675 static int copy_to_user_state_extra(struct xfrm_state *x,
676 struct xfrm_usersa_info *p,
679 copy_to_user_state(x, p);
682 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
685 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
688 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
690 if (copy_to_user_auth(x->aalg, skb))
691 goto nla_put_failure;
693 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
694 xfrm_alg_auth_len(x->aalg), x->aalg);
697 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
699 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
702 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
705 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
707 if (xfrm_mark_put(skb, &x->mark))
708 goto nla_put_failure;
710 if (x->security && copy_sec_ctx(x->security, skb) < 0)
711 goto nla_put_failure;
719 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
721 struct xfrm_dump_info *sp = ptr;
722 struct sk_buff *in_skb = sp->in_skb;
723 struct sk_buff *skb = sp->out_skb;
724 struct xfrm_usersa_info *p;
725 struct nlmsghdr *nlh;
728 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
729 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
735 err = copy_to_user_state_extra(x, p, skb);
737 goto nla_put_failure;
743 nlmsg_cancel(skb, nlh);
747 static int xfrm_dump_sa_done(struct netlink_callback *cb)
749 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
750 xfrm_state_walk_done(walk);
754 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
756 struct net *net = sock_net(skb->sk);
757 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
758 struct xfrm_dump_info info;
760 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
761 sizeof(cb->args) - sizeof(cb->args[0]));
763 info.in_skb = cb->skb;
765 info.nlmsg_seq = cb->nlh->nlmsg_seq;
766 info.nlmsg_flags = NLM_F_MULTI;
770 xfrm_state_walk_init(walk, 0);
773 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
778 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
779 struct xfrm_state *x, u32 seq)
781 struct xfrm_dump_info info;
784 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
786 return ERR_PTR(-ENOMEM);
788 info.in_skb = in_skb;
790 info.nlmsg_seq = seq;
791 info.nlmsg_flags = 0;
793 if (dump_one_state(x, 0, &info)) {
801 static inline size_t xfrm_spdinfo_msgsize(void)
803 return NLMSG_ALIGN(4)
804 + nla_total_size(sizeof(struct xfrmu_spdinfo))
805 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
808 static int build_spdinfo(struct sk_buff *skb, struct net *net,
809 u32 pid, u32 seq, u32 flags)
811 struct xfrmk_spdinfo si;
812 struct xfrmu_spdinfo spc;
813 struct xfrmu_spdhinfo sph;
814 struct nlmsghdr *nlh;
817 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
818 if (nlh == NULL) /* shouldnt really happen ... */
823 xfrm_spd_getinfo(net, &si);
824 spc.incnt = si.incnt;
825 spc.outcnt = si.outcnt;
826 spc.fwdcnt = si.fwdcnt;
827 spc.inscnt = si.inscnt;
828 spc.outscnt = si.outscnt;
829 spc.fwdscnt = si.fwdscnt;
830 sph.spdhcnt = si.spdhcnt;
831 sph.spdhmcnt = si.spdhmcnt;
833 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
834 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
836 return nlmsg_end(skb, nlh);
839 nlmsg_cancel(skb, nlh);
843 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
844 struct nlattr **attrs)
846 struct net *net = sock_net(skb->sk);
847 struct sk_buff *r_skb;
848 u32 *flags = nlmsg_data(nlh);
849 u32 spid = NETLINK_CB(skb).pid;
850 u32 seq = nlh->nlmsg_seq;
852 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
856 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
859 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
862 static inline size_t xfrm_sadinfo_msgsize(void)
864 return NLMSG_ALIGN(4)
865 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
866 + nla_total_size(4); /* XFRMA_SAD_CNT */
869 static int build_sadinfo(struct sk_buff *skb, struct net *net,
870 u32 pid, u32 seq, u32 flags)
872 struct xfrmk_sadinfo si;
873 struct xfrmu_sadhinfo sh;
874 struct nlmsghdr *nlh;
877 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
878 if (nlh == NULL) /* shouldnt really happen ... */
883 xfrm_sad_getinfo(net, &si);
885 sh.sadhmcnt = si.sadhmcnt;
886 sh.sadhcnt = si.sadhcnt;
888 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
889 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
891 return nlmsg_end(skb, nlh);
894 nlmsg_cancel(skb, nlh);
898 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
899 struct nlattr **attrs)
901 struct net *net = sock_net(skb->sk);
902 struct sk_buff *r_skb;
903 u32 *flags = nlmsg_data(nlh);
904 u32 spid = NETLINK_CB(skb).pid;
905 u32 seq = nlh->nlmsg_seq;
907 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
911 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
914 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
917 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
918 struct nlattr **attrs)
920 struct net *net = sock_net(skb->sk);
921 struct xfrm_usersa_id *p = nlmsg_data(nlh);
922 struct xfrm_state *x;
923 struct sk_buff *resp_skb;
926 x = xfrm_user_state_lookup(net, p, attrs, &err);
930 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
931 if (IS_ERR(resp_skb)) {
932 err = PTR_ERR(resp_skb);
934 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
941 static int verify_userspi_info(struct xfrm_userspi_info *p)
943 switch (p->info.id.proto) {
949 /* IPCOMP spi is 16-bits. */
950 if (p->max >= 0x10000)
964 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
965 struct nlattr **attrs)
967 struct net *net = sock_net(skb->sk);
968 struct xfrm_state *x;
969 struct xfrm_userspi_info *p;
970 struct sk_buff *resp_skb;
971 xfrm_address_t *daddr;
978 err = verify_userspi_info(p);
982 family = p->info.family;
983 daddr = &p->info.id.daddr;
987 mark = xfrm_mark_get(attrs, &m);
989 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
990 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
997 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
998 p->info.id.proto, daddr,
1005 err = xfrm_alloc_spi(x, p->min, p->max);
1009 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1010 if (IS_ERR(resp_skb)) {
1011 err = PTR_ERR(resp_skb);
1015 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1023 static int verify_policy_dir(u8 dir)
1026 case XFRM_POLICY_IN:
1027 case XFRM_POLICY_OUT:
1028 case XFRM_POLICY_FWD:
1038 static int verify_policy_type(u8 type)
1041 case XFRM_POLICY_TYPE_MAIN:
1042 #ifdef CONFIG_XFRM_SUB_POLICY
1043 case XFRM_POLICY_TYPE_SUB:
1054 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1057 case XFRM_SHARE_ANY:
1058 case XFRM_SHARE_SESSION:
1059 case XFRM_SHARE_USER:
1060 case XFRM_SHARE_UNIQUE:
1067 switch (p->action) {
1068 case XFRM_POLICY_ALLOW:
1069 case XFRM_POLICY_BLOCK:
1076 switch (p->sel.family) {
1081 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1084 return -EAFNOSUPPORT;
1091 return verify_policy_dir(p->dir);
1094 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1096 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1097 struct xfrm_user_sec_ctx *uctx;
1102 uctx = nla_data(rt);
1103 return security_xfrm_policy_alloc(&pol->security, uctx);
1106 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1112 for (i = 0; i < nr; i++, ut++) {
1113 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1115 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1116 memcpy(&t->saddr, &ut->saddr,
1117 sizeof(xfrm_address_t));
1118 t->reqid = ut->reqid;
1120 t->share = ut->share;
1121 t->optional = ut->optional;
1122 t->aalgos = ut->aalgos;
1123 t->ealgos = ut->ealgos;
1124 t->calgos = ut->calgos;
1125 /* If all masks are ~0, then we allow all algorithms. */
1126 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1127 t->encap_family = ut->family;
1131 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1135 if (nr > XFRM_MAX_DEPTH)
1138 for (i = 0; i < nr; i++) {
1139 /* We never validated the ut->family value, so many
1140 * applications simply leave it at zero. The check was
1141 * never made and ut->family was ignored because all
1142 * templates could be assumed to have the same family as
1143 * the policy itself. Now that we will have ipv4-in-ipv6
1144 * and ipv6-in-ipv4 tunnels, this is no longer true.
1147 ut[i].family = family;
1149 switch (ut[i].family) {
1152 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1164 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1166 struct nlattr *rt = attrs[XFRMA_TMPL];
1171 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1172 int nr = nla_len(rt) / sizeof(*utmpl);
1175 err = validate_tmpl(nr, utmpl, pol->family);
1179 copy_templates(pol, utmpl, nr);
1184 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1186 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1187 struct xfrm_userpolicy_type *upt;
1188 u8 type = XFRM_POLICY_TYPE_MAIN;
1196 err = verify_policy_type(type);
1204 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1206 xp->priority = p->priority;
1207 xp->index = p->index;
1208 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1209 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1210 xp->action = p->action;
1211 xp->flags = p->flags;
1212 xp->family = p->sel.family;
1213 /* XXX xp->share = p->share; */
1216 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1218 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1219 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1220 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1221 p->priority = xp->priority;
1222 p->index = xp->index;
1223 p->sel.family = xp->family;
1225 p->action = xp->action;
1226 p->flags = xp->flags;
1227 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1230 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1232 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1240 copy_from_user_policy(xp, p);
1242 err = copy_from_user_policy_type(&xp->type, attrs);
1246 if (!(err = copy_from_user_tmpl(xp, attrs)))
1247 err = copy_from_user_sec_ctx(xp, attrs);
1251 xfrm_mark_get(attrs, &xp->mark);
1257 xfrm_policy_destroy(xp);
1261 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1262 struct nlattr **attrs)
1264 struct net *net = sock_net(skb->sk);
1265 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1266 struct xfrm_policy *xp;
1270 uid_t loginuid = audit_get_loginuid(current);
1271 u32 sessionid = audit_get_sessionid(current);
1274 err = verify_newpolicy_info(p);
1277 err = verify_sec_ctx_len(attrs);
1281 xp = xfrm_policy_construct(net, p, attrs, &err);
1285 /* shouldnt excl be based on nlh flags??
1286 * Aha! this is anti-netlink really i.e more pfkey derived
1287 * in netlink excl is a flag and you wouldnt need
1288 * a type XFRM_MSG_UPDPOLICY - JHS */
1289 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1290 err = xfrm_policy_insert(p->dir, xp, excl);
1291 security_task_getsecid(current, &sid);
1292 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1295 security_xfrm_policy_free(xp->security);
1300 c.event = nlh->nlmsg_type;
1301 c.seq = nlh->nlmsg_seq;
1302 c.pid = nlh->nlmsg_pid;
1303 km_policy_notify(xp, p->dir, &c);
1310 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1312 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1315 if (xp->xfrm_nr == 0)
1318 for (i = 0; i < xp->xfrm_nr; i++) {
1319 struct xfrm_user_tmpl *up = &vec[i];
1320 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1322 memcpy(&up->id, &kp->id, sizeof(up->id));
1323 up->family = kp->encap_family;
1324 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1325 up->reqid = kp->reqid;
1326 up->mode = kp->mode;
1327 up->share = kp->share;
1328 up->optional = kp->optional;
1329 up->aalgos = kp->aalgos;
1330 up->ealgos = kp->ealgos;
1331 up->calgos = kp->calgos;
1334 return nla_put(skb, XFRMA_TMPL,
1335 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1338 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1341 return copy_sec_ctx(x->security, skb);
1346 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1349 return copy_sec_ctx(xp->security, skb);
1353 static inline size_t userpolicy_type_attrsize(void)
1355 #ifdef CONFIG_XFRM_SUB_POLICY
1356 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1362 #ifdef CONFIG_XFRM_SUB_POLICY
1363 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1365 struct xfrm_userpolicy_type upt = {
1369 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1373 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1379 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1381 struct xfrm_dump_info *sp = ptr;
1382 struct xfrm_userpolicy_info *p;
1383 struct sk_buff *in_skb = sp->in_skb;
1384 struct sk_buff *skb = sp->out_skb;
1385 struct nlmsghdr *nlh;
1387 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1388 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1392 p = nlmsg_data(nlh);
1393 copy_to_user_policy(xp, p, dir);
1394 if (copy_to_user_tmpl(xp, skb) < 0)
1396 if (copy_to_user_sec_ctx(xp, skb))
1398 if (copy_to_user_policy_type(xp->type, skb) < 0)
1400 if (xfrm_mark_put(skb, &xp->mark))
1401 goto nla_put_failure;
1403 nlmsg_end(skb, nlh);
1408 nlmsg_cancel(skb, nlh);
1412 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1414 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1416 xfrm_policy_walk_done(walk);
1420 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1422 struct net *net = sock_net(skb->sk);
1423 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1424 struct xfrm_dump_info info;
1426 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1427 sizeof(cb->args) - sizeof(cb->args[0]));
1429 info.in_skb = cb->skb;
1431 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1432 info.nlmsg_flags = NLM_F_MULTI;
1436 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1439 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1444 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1445 struct xfrm_policy *xp,
1448 struct xfrm_dump_info info;
1449 struct sk_buff *skb;
1451 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1453 return ERR_PTR(-ENOMEM);
1455 info.in_skb = in_skb;
1457 info.nlmsg_seq = seq;
1458 info.nlmsg_flags = 0;
1460 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1468 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1469 struct nlattr **attrs)
1471 struct net *net = sock_net(skb->sk);
1472 struct xfrm_policy *xp;
1473 struct xfrm_userpolicy_id *p;
1474 u8 type = XFRM_POLICY_TYPE_MAIN;
1479 u32 mark = xfrm_mark_get(attrs, &m);
1481 p = nlmsg_data(nlh);
1482 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1484 err = copy_from_user_policy_type(&type, attrs);
1488 err = verify_policy_dir(p->dir);
1493 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1495 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1496 struct xfrm_sec_ctx *ctx;
1498 err = verify_sec_ctx_len(attrs);
1504 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1506 err = security_xfrm_policy_alloc(&ctx, uctx);
1510 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1512 security_xfrm_policy_free(ctx);
1518 struct sk_buff *resp_skb;
1520 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1521 if (IS_ERR(resp_skb)) {
1522 err = PTR_ERR(resp_skb);
1524 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1525 NETLINK_CB(skb).pid);
1528 uid_t loginuid = audit_get_loginuid(current);
1529 u32 sessionid = audit_get_sessionid(current);
1532 security_task_getsecid(current, &sid);
1533 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1539 c.data.byid = p->index;
1540 c.event = nlh->nlmsg_type;
1541 c.seq = nlh->nlmsg_seq;
1542 c.pid = nlh->nlmsg_pid;
1543 km_policy_notify(xp, p->dir, &c);
1551 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1552 struct nlattr **attrs)
1554 struct net *net = sock_net(skb->sk);
1556 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1557 struct xfrm_audit audit_info;
1560 audit_info.loginuid = audit_get_loginuid(current);
1561 audit_info.sessionid = audit_get_sessionid(current);
1562 security_task_getsecid(current, &audit_info.secid);
1563 err = xfrm_state_flush(net, p->proto, &audit_info);
1565 if (err == -ESRCH) /* empty table */
1569 c.data.proto = p->proto;
1570 c.event = nlh->nlmsg_type;
1571 c.seq = nlh->nlmsg_seq;
1572 c.pid = nlh->nlmsg_pid;
1574 km_state_notify(NULL, &c);
1579 static inline size_t xfrm_aevent_msgsize(void)
1581 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1582 + nla_total_size(sizeof(struct xfrm_replay_state))
1583 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1584 + nla_total_size(sizeof(struct xfrm_mark))
1585 + nla_total_size(4) /* XFRM_AE_RTHR */
1586 + nla_total_size(4); /* XFRM_AE_ETHR */
1589 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1591 struct xfrm_aevent_id *id;
1592 struct nlmsghdr *nlh;
1594 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1598 id = nlmsg_data(nlh);
1599 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1600 id->sa_id.spi = x->id.spi;
1601 id->sa_id.family = x->props.family;
1602 id->sa_id.proto = x->id.proto;
1603 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1604 id->reqid = x->props.reqid;
1605 id->flags = c->data.aevent;
1607 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1608 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1610 if (id->flags & XFRM_AE_RTHR)
1611 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1613 if (id->flags & XFRM_AE_ETHR)
1614 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1615 x->replay_maxage * 10 / HZ);
1617 if (xfrm_mark_put(skb, &x->mark))
1618 goto nla_put_failure;
1620 return nlmsg_end(skb, nlh);
1623 nlmsg_cancel(skb, nlh);
1627 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1628 struct nlattr **attrs)
1630 struct net *net = sock_net(skb->sk);
1631 struct xfrm_state *x;
1632 struct sk_buff *r_skb;
1637 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1638 struct xfrm_usersa_id *id = &p->sa_id;
1640 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1644 mark = xfrm_mark_get(attrs, &m);
1646 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1653 * XXX: is this lock really needed - none of the other
1654 * gets lock (the concern is things getting updated
1655 * while we are still reading) - jhs
1657 spin_lock_bh(&x->lock);
1658 c.data.aevent = p->flags;
1659 c.seq = nlh->nlmsg_seq;
1660 c.pid = nlh->nlmsg_pid;
1662 if (build_aevent(r_skb, x, &c) < 0)
1664 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1665 spin_unlock_bh(&x->lock);
1670 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1671 struct nlattr **attrs)
1673 struct net *net = sock_net(skb->sk);
1674 struct xfrm_state *x;
1679 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1680 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1681 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1686 /* pedantic mode - thou shalt sayeth replaceth */
1687 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1690 mark = xfrm_mark_get(attrs, &m);
1692 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1696 if (x->km.state != XFRM_STATE_VALID)
1699 spin_lock_bh(&x->lock);
1700 xfrm_update_ae_params(x, attrs);
1701 spin_unlock_bh(&x->lock);
1703 c.event = nlh->nlmsg_type;
1704 c.seq = nlh->nlmsg_seq;
1705 c.pid = nlh->nlmsg_pid;
1706 c.data.aevent = XFRM_AE_CU;
1707 km_state_notify(x, &c);
1714 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1715 struct nlattr **attrs)
1717 struct net *net = sock_net(skb->sk);
1719 u8 type = XFRM_POLICY_TYPE_MAIN;
1721 struct xfrm_audit audit_info;
1723 err = copy_from_user_policy_type(&type, attrs);
1727 audit_info.loginuid = audit_get_loginuid(current);
1728 audit_info.sessionid = audit_get_sessionid(current);
1729 security_task_getsecid(current, &audit_info.secid);
1730 err = xfrm_policy_flush(net, type, &audit_info);
1732 if (err == -ESRCH) /* empty table */
1738 c.event = nlh->nlmsg_type;
1739 c.seq = nlh->nlmsg_seq;
1740 c.pid = nlh->nlmsg_pid;
1742 km_policy_notify(NULL, 0, &c);
1746 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1747 struct nlattr **attrs)
1749 struct net *net = sock_net(skb->sk);
1750 struct xfrm_policy *xp;
1751 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1752 struct xfrm_userpolicy_info *p = &up->pol;
1753 u8 type = XFRM_POLICY_TYPE_MAIN;
1756 u32 mark = xfrm_mark_get(attrs, &m);
1758 err = copy_from_user_policy_type(&type, attrs);
1762 err = verify_policy_dir(p->dir);
1767 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1769 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1770 struct xfrm_sec_ctx *ctx;
1772 err = verify_sec_ctx_len(attrs);
1778 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1780 err = security_xfrm_policy_alloc(&ctx, uctx);
1784 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1785 &p->sel, ctx, 0, &err);
1786 security_xfrm_policy_free(ctx);
1791 if (unlikely(xp->walk.dead))
1796 uid_t loginuid = audit_get_loginuid(current);
1797 u32 sessionid = audit_get_sessionid(current);
1800 security_task_getsecid(current, &sid);
1801 xfrm_policy_delete(xp, p->dir);
1802 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1805 // reset the timers here?
1806 WARN(1, "Dont know what to do with soft policy expire\n");
1808 km_policy_expired(xp, p->dir, up->hard, current->pid);
1815 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1816 struct nlattr **attrs)
1818 struct net *net = sock_net(skb->sk);
1819 struct xfrm_state *x;
1821 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1822 struct xfrm_usersa_info *p = &ue->state;
1824 u32 mark = xfrm_mark_get(attrs, &m);
1826 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1832 spin_lock_bh(&x->lock);
1834 if (x->km.state != XFRM_STATE_VALID)
1836 km_state_expired(x, ue->hard, current->pid);
1839 uid_t loginuid = audit_get_loginuid(current);
1840 u32 sessionid = audit_get_sessionid(current);
1843 security_task_getsecid(current, &sid);
1844 __xfrm_state_delete(x);
1845 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1849 spin_unlock_bh(&x->lock);
1854 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1855 struct nlattr **attrs)
1857 struct net *net = sock_net(skb->sk);
1858 struct xfrm_policy *xp;
1859 struct xfrm_user_tmpl *ut;
1861 struct nlattr *rt = attrs[XFRMA_TMPL];
1862 struct xfrm_mark mark;
1864 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1865 struct xfrm_state *x = xfrm_state_alloc(net);
1871 xfrm_mark_get(attrs, &mark);
1873 err = verify_newpolicy_info(&ua->policy);
1878 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1882 memcpy(&x->id, &ua->id, sizeof(ua->id));
1883 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1884 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1885 xp->mark.m = x->mark.m = mark.m;
1886 xp->mark.v = x->mark.v = mark.v;
1888 /* extract the templates and for each call km_key */
1889 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1890 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1891 memcpy(&x->id, &t->id, sizeof(x->id));
1892 x->props.mode = t->mode;
1893 x->props.reqid = t->reqid;
1894 x->props.family = ut->family;
1895 t->aalgos = ua->aalgos;
1896 t->ealgos = ua->ealgos;
1897 t->calgos = ua->calgos;
1898 err = km_query(x, t, xp);
1908 WARN(1, "BAD policy passed\n");
1915 #ifdef CONFIG_XFRM_MIGRATE
1916 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1917 struct xfrm_kmaddress *k,
1918 struct nlattr **attrs, int *num)
1920 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1921 struct xfrm_user_migrate *um;
1925 struct xfrm_user_kmaddress *uk;
1927 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1928 memcpy(&k->local, &uk->local, sizeof(k->local));
1929 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1930 k->family = uk->family;
1931 k->reserved = uk->reserved;
1935 num_migrate = nla_len(rt) / sizeof(*um);
1937 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1940 for (i = 0; i < num_migrate; i++, um++, ma++) {
1941 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1942 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1943 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1944 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1946 ma->proto = um->proto;
1947 ma->mode = um->mode;
1948 ma->reqid = um->reqid;
1950 ma->old_family = um->old_family;
1951 ma->new_family = um->new_family;
1958 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1959 struct nlattr **attrs)
1961 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1962 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1963 struct xfrm_kmaddress km, *kmp;
1968 if (attrs[XFRMA_MIGRATE] == NULL)
1971 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1973 err = copy_from_user_policy_type(&type, attrs);
1977 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
1984 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
1989 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1990 struct nlattr **attrs)
1992 return -ENOPROTOOPT;
1996 #ifdef CONFIG_XFRM_MIGRATE
1997 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
1999 struct xfrm_user_migrate um;
2001 memset(&um, 0, sizeof(um));
2002 um.proto = m->proto;
2004 um.reqid = m->reqid;
2005 um.old_family = m->old_family;
2006 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2007 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2008 um.new_family = m->new_family;
2009 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2010 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2012 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2015 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2017 struct xfrm_user_kmaddress uk;
2019 memset(&uk, 0, sizeof(uk));
2020 uk.family = k->family;
2021 uk.reserved = k->reserved;
2022 memcpy(&uk.local, &k->local, sizeof(uk.local));
2023 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2025 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2028 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2030 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2031 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2032 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2033 + userpolicy_type_attrsize();
2036 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2037 int num_migrate, const struct xfrm_kmaddress *k,
2038 const struct xfrm_selector *sel, u8 dir, u8 type)
2040 const struct xfrm_migrate *mp;
2041 struct xfrm_userpolicy_id *pol_id;
2042 struct nlmsghdr *nlh;
2045 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2049 pol_id = nlmsg_data(nlh);
2050 /* copy data from selector, dir, and type to the pol_id */
2051 memset(pol_id, 0, sizeof(*pol_id));
2052 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2055 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2058 if (copy_to_user_policy_type(type, skb) < 0)
2061 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2062 if (copy_to_user_migrate(mp, skb) < 0)
2066 return nlmsg_end(skb, nlh);
2068 nlmsg_cancel(skb, nlh);
2072 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2073 const struct xfrm_migrate *m, int num_migrate,
2074 const struct xfrm_kmaddress *k)
2076 struct net *net = &init_net;
2077 struct sk_buff *skb;
2079 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2084 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2087 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2090 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2091 const struct xfrm_migrate *m, int num_migrate,
2092 const struct xfrm_kmaddress *k)
2094 return -ENOPROTOOPT;
2098 #define XMSGSIZE(type) sizeof(struct type)
2100 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2101 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2102 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2103 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2104 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2105 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2106 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2107 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2108 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2109 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2110 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2111 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2112 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2113 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2114 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2115 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2116 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2117 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2118 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2119 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2120 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2125 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2126 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2127 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2128 [XFRMA_LASTUSED] = { .type = NLA_U64},
2129 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2130 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2131 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2132 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2133 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2134 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2135 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2136 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2137 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2138 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2139 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2140 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2141 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2142 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2143 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2144 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2145 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2146 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2147 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2150 static struct xfrm_link {
2151 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2152 int (*dump)(struct sk_buff *, struct netlink_callback *);
2153 int (*done)(struct netlink_callback *);
2154 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2155 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2156 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2157 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2158 .dump = xfrm_dump_sa,
2159 .done = xfrm_dump_sa_done },
2160 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2161 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2162 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2163 .dump = xfrm_dump_policy,
2164 .done = xfrm_dump_policy_done },
2165 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2166 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2167 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2168 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2169 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2170 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2171 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2172 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2173 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2174 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2175 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2176 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2177 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2180 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2182 struct net *net = sock_net(skb->sk);
2183 struct nlattr *attrs[XFRMA_MAX+1];
2184 struct xfrm_link *link;
2187 type = nlh->nlmsg_type;
2188 if (type > XFRM_MSG_MAX)
2191 type -= XFRM_MSG_BASE;
2192 link = &xfrm_dispatch[type];
2194 /* All operations require privileges, even GET */
2195 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2198 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2199 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2200 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2201 if (link->dump == NULL)
2204 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
2207 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2212 if (link->doit == NULL)
2215 return link->doit(skb, nlh, attrs);
2218 static void xfrm_netlink_rcv(struct sk_buff *skb)
2220 mutex_lock(&xfrm_cfg_mutex);
2221 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2222 mutex_unlock(&xfrm_cfg_mutex);
2225 static inline size_t xfrm_expire_msgsize(void)
2227 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2228 + nla_total_size(sizeof(struct xfrm_mark));
2231 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2233 struct xfrm_user_expire *ue;
2234 struct nlmsghdr *nlh;
2236 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2240 ue = nlmsg_data(nlh);
2241 copy_to_user_state(x, &ue->state);
2242 ue->hard = (c->data.hard != 0) ? 1 : 0;
2244 if (xfrm_mark_put(skb, &x->mark))
2245 goto nla_put_failure;
2247 return nlmsg_end(skb, nlh);
2253 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2255 struct net *net = xs_net(x);
2256 struct sk_buff *skb;
2258 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2262 if (build_expire(skb, x, c) < 0) {
2267 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2270 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2272 struct net *net = xs_net(x);
2273 struct sk_buff *skb;
2275 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2279 if (build_aevent(skb, x, c) < 0)
2282 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2285 static int xfrm_notify_sa_flush(const struct km_event *c)
2287 struct net *net = c->net;
2288 struct xfrm_usersa_flush *p;
2289 struct nlmsghdr *nlh;
2290 struct sk_buff *skb;
2291 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2293 skb = nlmsg_new(len, GFP_ATOMIC);
2297 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2303 p = nlmsg_data(nlh);
2304 p->proto = c->data.proto;
2306 nlmsg_end(skb, nlh);
2308 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2311 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2315 l += nla_total_size(aead_len(x->aead));
2317 l += nla_total_size(sizeof(struct xfrm_algo) +
2318 (x->aalg->alg_key_len + 7) / 8);
2319 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2322 l += nla_total_size(xfrm_alg_len(x->ealg));
2324 l += nla_total_size(sizeof(*x->calg));
2326 l += nla_total_size(sizeof(*x->encap));
2328 l += nla_total_size(sizeof(x->tfcpad));
2330 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2331 x->security->ctx_len);
2333 l += nla_total_size(sizeof(*x->coaddr));
2335 /* Must count x->lastused as it may become non-zero behind our back. */
2336 l += nla_total_size(sizeof(u64));
2341 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2343 struct net *net = xs_net(x);
2344 struct xfrm_usersa_info *p;
2345 struct xfrm_usersa_id *id;
2346 struct nlmsghdr *nlh;
2347 struct sk_buff *skb;
2348 int len = xfrm_sa_len(x);
2351 headlen = sizeof(*p);
2352 if (c->event == XFRM_MSG_DELSA) {
2353 len += nla_total_size(headlen);
2354 headlen = sizeof(*id);
2355 len += nla_total_size(sizeof(struct xfrm_mark));
2357 len += NLMSG_ALIGN(headlen);
2359 skb = nlmsg_new(len, GFP_ATOMIC);
2363 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2365 goto nla_put_failure;
2367 p = nlmsg_data(nlh);
2368 if (c->event == XFRM_MSG_DELSA) {
2369 struct nlattr *attr;
2371 id = nlmsg_data(nlh);
2372 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2373 id->spi = x->id.spi;
2374 id->family = x->props.family;
2375 id->proto = x->id.proto;
2377 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2379 goto nla_put_failure;
2384 if (copy_to_user_state_extra(x, p, skb))
2385 goto nla_put_failure;
2387 nlmsg_end(skb, nlh);
2389 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2392 /* Somebody screwed up with xfrm_sa_len! */
2398 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2402 case XFRM_MSG_EXPIRE:
2403 return xfrm_exp_state_notify(x, c);
2404 case XFRM_MSG_NEWAE:
2405 return xfrm_aevent_state_notify(x, c);
2406 case XFRM_MSG_DELSA:
2407 case XFRM_MSG_UPDSA:
2408 case XFRM_MSG_NEWSA:
2409 return xfrm_notify_sa(x, c);
2410 case XFRM_MSG_FLUSHSA:
2411 return xfrm_notify_sa_flush(c);
2413 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2422 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2423 struct xfrm_policy *xp)
2425 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2426 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2427 + nla_total_size(sizeof(struct xfrm_mark))
2428 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2429 + userpolicy_type_attrsize();
2432 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2433 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2436 struct xfrm_user_acquire *ua;
2437 struct nlmsghdr *nlh;
2438 __u32 seq = xfrm_get_acqseq();
2440 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2444 ua = nlmsg_data(nlh);
2445 memcpy(&ua->id, &x->id, sizeof(ua->id));
2446 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2447 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2448 copy_to_user_policy(xp, &ua->policy, dir);
2449 ua->aalgos = xt->aalgos;
2450 ua->ealgos = xt->ealgos;
2451 ua->calgos = xt->calgos;
2452 ua->seq = x->km.seq = seq;
2454 if (copy_to_user_tmpl(xp, skb) < 0)
2456 if (copy_to_user_state_sec_ctx(x, skb))
2458 if (copy_to_user_policy_type(xp->type, skb) < 0)
2460 if (xfrm_mark_put(skb, &xp->mark))
2461 goto nla_put_failure;
2463 return nlmsg_end(skb, nlh);
2467 nlmsg_cancel(skb, nlh);
2471 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2472 struct xfrm_policy *xp, int dir)
2474 struct net *net = xs_net(x);
2475 struct sk_buff *skb;
2477 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2481 if (build_acquire(skb, x, xt, xp, dir) < 0)
2484 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2487 /* User gives us xfrm_user_policy_info followed by an array of 0
2488 * or more templates.
2490 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2491 u8 *data, int len, int *dir)
2493 struct net *net = sock_net(sk);
2494 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2495 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2496 struct xfrm_policy *xp;
2499 switch (sk->sk_family) {
2501 if (opt != IP_XFRM_POLICY) {
2506 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2508 if (opt != IPV6_XFRM_POLICY) {
2521 if (len < sizeof(*p) ||
2522 verify_newpolicy_info(p))
2525 nr = ((len - sizeof(*p)) / sizeof(*ut));
2526 if (validate_tmpl(nr, ut, p->sel.family))
2529 if (p->dir > XFRM_POLICY_OUT)
2532 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2538 copy_from_user_policy(xp, p);
2539 xp->type = XFRM_POLICY_TYPE_MAIN;
2540 copy_templates(xp, ut, nr);
2547 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2549 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2550 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2551 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2552 + nla_total_size(sizeof(struct xfrm_mark))
2553 + userpolicy_type_attrsize();
2556 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2557 int dir, const struct km_event *c)
2559 struct xfrm_user_polexpire *upe;
2560 struct nlmsghdr *nlh;
2561 int hard = c->data.hard;
2563 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2567 upe = nlmsg_data(nlh);
2568 copy_to_user_policy(xp, &upe->pol, dir);
2569 if (copy_to_user_tmpl(xp, skb) < 0)
2571 if (copy_to_user_sec_ctx(xp, skb))
2573 if (copy_to_user_policy_type(xp->type, skb) < 0)
2575 if (xfrm_mark_put(skb, &xp->mark))
2576 goto nla_put_failure;
2579 return nlmsg_end(skb, nlh);
2583 nlmsg_cancel(skb, nlh);
2587 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2589 struct net *net = xp_net(xp);
2590 struct sk_buff *skb;
2592 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2596 if (build_polexpire(skb, xp, dir, c) < 0)
2599 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2602 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2604 struct net *net = xp_net(xp);
2605 struct xfrm_userpolicy_info *p;
2606 struct xfrm_userpolicy_id *id;
2607 struct nlmsghdr *nlh;
2608 struct sk_buff *skb;
2609 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2612 headlen = sizeof(*p);
2613 if (c->event == XFRM_MSG_DELPOLICY) {
2614 len += nla_total_size(headlen);
2615 headlen = sizeof(*id);
2617 len += userpolicy_type_attrsize();
2618 len += nla_total_size(sizeof(struct xfrm_mark));
2619 len += NLMSG_ALIGN(headlen);
2621 skb = nlmsg_new(len, GFP_ATOMIC);
2625 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2629 p = nlmsg_data(nlh);
2630 if (c->event == XFRM_MSG_DELPOLICY) {
2631 struct nlattr *attr;
2633 id = nlmsg_data(nlh);
2634 memset(id, 0, sizeof(*id));
2637 id->index = xp->index;
2639 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2641 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2648 copy_to_user_policy(xp, p, dir);
2649 if (copy_to_user_tmpl(xp, skb) < 0)
2651 if (copy_to_user_policy_type(xp->type, skb) < 0)
2654 if (xfrm_mark_put(skb, &xp->mark))
2655 goto nla_put_failure;
2657 nlmsg_end(skb, nlh);
2659 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2667 static int xfrm_notify_policy_flush(const struct km_event *c)
2669 struct net *net = c->net;
2670 struct nlmsghdr *nlh;
2671 struct sk_buff *skb;
2673 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2677 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2680 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2683 nlmsg_end(skb, nlh);
2685 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2692 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2696 case XFRM_MSG_NEWPOLICY:
2697 case XFRM_MSG_UPDPOLICY:
2698 case XFRM_MSG_DELPOLICY:
2699 return xfrm_notify_policy(xp, dir, c);
2700 case XFRM_MSG_FLUSHPOLICY:
2701 return xfrm_notify_policy_flush(c);
2702 case XFRM_MSG_POLEXPIRE:
2703 return xfrm_exp_policy_notify(xp, dir, c);
2705 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2713 static inline size_t xfrm_report_msgsize(void)
2715 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2718 static int build_report(struct sk_buff *skb, u8 proto,
2719 struct xfrm_selector *sel, xfrm_address_t *addr)
2721 struct xfrm_user_report *ur;
2722 struct nlmsghdr *nlh;
2724 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2728 ur = nlmsg_data(nlh);
2730 memcpy(&ur->sel, sel, sizeof(ur->sel));
2733 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2735 return nlmsg_end(skb, nlh);
2738 nlmsg_cancel(skb, nlh);
2742 static int xfrm_send_report(struct net *net, u8 proto,
2743 struct xfrm_selector *sel, xfrm_address_t *addr)
2745 struct sk_buff *skb;
2747 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2751 if (build_report(skb, proto, sel, addr) < 0)
2754 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2757 static inline size_t xfrm_mapping_msgsize(void)
2759 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2762 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2763 xfrm_address_t *new_saddr, __be16 new_sport)
2765 struct xfrm_user_mapping *um;
2766 struct nlmsghdr *nlh;
2768 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2772 um = nlmsg_data(nlh);
2774 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2775 um->id.spi = x->id.spi;
2776 um->id.family = x->props.family;
2777 um->id.proto = x->id.proto;
2778 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2779 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2780 um->new_sport = new_sport;
2781 um->old_sport = x->encap->encap_sport;
2782 um->reqid = x->props.reqid;
2784 return nlmsg_end(skb, nlh);
2787 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2790 struct net *net = xs_net(x);
2791 struct sk_buff *skb;
2793 if (x->id.proto != IPPROTO_ESP)
2799 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2803 if (build_mapping(skb, x, ipaddr, sport) < 0)
2806 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2809 static struct xfrm_mgr netlink_mgr = {
2811 .notify = xfrm_send_state_notify,
2812 .acquire = xfrm_send_acquire,
2813 .compile_policy = xfrm_compile_policy,
2814 .notify_policy = xfrm_send_policy_notify,
2815 .report = xfrm_send_report,
2816 .migrate = xfrm_send_migrate,
2817 .new_mapping = xfrm_send_mapping,
2820 static int __net_init xfrm_user_net_init(struct net *net)
2824 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2825 xfrm_netlink_rcv, NULL, THIS_MODULE);
2828 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2829 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2833 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2836 list_for_each_entry(net, net_exit_list, exit_list)
2837 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2839 list_for_each_entry(net, net_exit_list, exit_list)
2840 netlink_kernel_release(net->xfrm.nlsk_stash);
2843 static struct pernet_operations xfrm_user_net_ops = {
2844 .init = xfrm_user_net_init,
2845 .exit_batch = xfrm_user_net_exit,
2848 static int __init xfrm_user_init(void)
2852 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2854 rv = register_pernet_subsys(&xfrm_user_net_ops);
2857 rv = xfrm_register_km(&netlink_mgr);
2859 unregister_pernet_subsys(&xfrm_user_net_ops);
2863 static void __exit xfrm_user_exit(void)
2865 xfrm_unregister_km(&netlink_mgr);
2866 unregister_pernet_subsys(&xfrm_user_net_ops);
2869 module_init(xfrm_user_init);
2870 module_exit(xfrm_user_exit);
2871 MODULE_LICENSE("GPL");
2872 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);