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))
122 static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
126 struct xfrm_replay_state_esn *rs;
128 if (p->flags & XFRM_STATE_ESN) {
134 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
137 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
138 nla_len(rt) != sizeof(*rs))
145 if (p->id.proto != IPPROTO_ESP)
148 if (p->replay_window != 0)
154 static int verify_newsa_info(struct xfrm_usersa_info *p,
155 struct nlattr **attrs)
165 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
177 switch (p->id.proto) {
179 if ((!attrs[XFRMA_ALG_AUTH] &&
180 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
181 attrs[XFRMA_ALG_AEAD] ||
182 attrs[XFRMA_ALG_CRYPT] ||
183 attrs[XFRMA_ALG_COMP] ||
189 if (attrs[XFRMA_ALG_COMP])
191 if (!attrs[XFRMA_ALG_AUTH] &&
192 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
193 !attrs[XFRMA_ALG_CRYPT] &&
194 !attrs[XFRMA_ALG_AEAD])
196 if ((attrs[XFRMA_ALG_AUTH] ||
197 attrs[XFRMA_ALG_AUTH_TRUNC] ||
198 attrs[XFRMA_ALG_CRYPT]) &&
199 attrs[XFRMA_ALG_AEAD])
201 if (attrs[XFRMA_TFCPAD] &&
202 p->mode != XFRM_MODE_TUNNEL)
207 if (!attrs[XFRMA_ALG_COMP] ||
208 attrs[XFRMA_ALG_AEAD] ||
209 attrs[XFRMA_ALG_AUTH] ||
210 attrs[XFRMA_ALG_AUTH_TRUNC] ||
211 attrs[XFRMA_ALG_CRYPT] ||
216 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
217 case IPPROTO_DSTOPTS:
218 case IPPROTO_ROUTING:
219 if (attrs[XFRMA_ALG_COMP] ||
220 attrs[XFRMA_ALG_AUTH] ||
221 attrs[XFRMA_ALG_AUTH_TRUNC] ||
222 attrs[XFRMA_ALG_AEAD] ||
223 attrs[XFRMA_ALG_CRYPT] ||
224 attrs[XFRMA_ENCAP] ||
225 attrs[XFRMA_SEC_CTX] ||
226 attrs[XFRMA_TFCPAD] ||
227 !attrs[XFRMA_COADDR])
236 if ((err = verify_aead(attrs)))
238 if ((err = verify_auth_trunc(attrs)))
240 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
242 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
244 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
246 if ((err = verify_sec_ctx_len(attrs)))
248 if ((err = verify_replay(p, attrs)))
253 case XFRM_MODE_TRANSPORT:
254 case XFRM_MODE_TUNNEL:
255 case XFRM_MODE_ROUTEOPTIMIZATION:
269 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
270 struct xfrm_algo_desc *(*get_byname)(const char *, int),
273 struct xfrm_algo *p, *ualg;
274 struct xfrm_algo_desc *algo;
279 ualg = nla_data(rta);
281 algo = get_byname(ualg->alg_name, 1);
284 *props = algo->desc.sadb_alg_id;
286 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
290 strcpy(p->alg_name, algo->name);
295 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
298 struct xfrm_algo *ualg;
299 struct xfrm_algo_auth *p;
300 struct xfrm_algo_desc *algo;
305 ualg = nla_data(rta);
307 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
310 *props = algo->desc.sadb_alg_id;
312 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
316 strcpy(p->alg_name, algo->name);
317 p->alg_key_len = ualg->alg_key_len;
318 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
319 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
325 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
328 struct xfrm_algo_auth *p, *ualg;
329 struct xfrm_algo_desc *algo;
334 ualg = nla_data(rta);
336 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
339 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
342 *props = algo->desc.sadb_alg_id;
344 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
348 strcpy(p->alg_name, algo->name);
349 if (!p->alg_trunc_len)
350 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
356 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
359 struct xfrm_algo_aead *p, *ualg;
360 struct xfrm_algo_desc *algo;
365 ualg = nla_data(rta);
367 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
370 *props = algo->desc.sadb_alg_id;
372 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
376 strcpy(p->alg_name, algo->name);
381 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
384 struct xfrm_replay_state_esn *up;
387 if (!replay_esn || !rp)
391 ulen = xfrm_replay_state_esn_len(up);
393 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
396 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
402 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
403 struct xfrm_replay_state_esn **preplay_esn,
406 struct xfrm_replay_state_esn *p, *pp, *up;
413 klen = xfrm_replay_state_esn_len(up);
414 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
416 p = kzalloc(klen, GFP_KERNEL);
420 pp = kzalloc(klen, GFP_KERNEL);
427 memcpy(pp, up, ulen);
435 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
440 len += sizeof(struct xfrm_user_sec_ctx);
441 len += xfrm_ctx->ctx_len;
446 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
448 memcpy(&x->id, &p->id, sizeof(x->id));
449 memcpy(&x->sel, &p->sel, sizeof(x->sel));
450 memcpy(&x->lft, &p->lft, sizeof(x->lft));
451 x->props.mode = p->mode;
452 x->props.replay_window = p->replay_window;
453 x->props.reqid = p->reqid;
454 x->props.family = p->family;
455 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
456 x->props.flags = p->flags;
458 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
459 x->sel.family = p->family;
463 * someday when pfkey also has support, we could have the code
464 * somehow made shareable and move it to xfrm_state.c - JHS
467 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
470 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
471 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
472 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
473 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
474 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
477 struct xfrm_replay_state_esn *replay_esn;
478 replay_esn = nla_data(re);
479 memcpy(x->replay_esn, replay_esn,
480 xfrm_replay_state_esn_len(replay_esn));
481 memcpy(x->preplay_esn, replay_esn,
482 xfrm_replay_state_esn_len(replay_esn));
486 struct xfrm_replay_state *replay;
487 replay = nla_data(rp);
488 memcpy(&x->replay, replay, sizeof(*replay));
489 memcpy(&x->preplay, replay, sizeof(*replay));
493 struct xfrm_lifetime_cur *ltime;
494 ltime = nla_data(lt);
495 x->curlft.bytes = ltime->bytes;
496 x->curlft.packets = ltime->packets;
497 x->curlft.add_time = ltime->add_time;
498 x->curlft.use_time = ltime->use_time;
502 x->replay_maxage = nla_get_u32(et);
505 x->replay_maxdiff = nla_get_u32(rt);
508 static struct xfrm_state *xfrm_state_construct(struct net *net,
509 struct xfrm_usersa_info *p,
510 struct nlattr **attrs,
513 struct xfrm_state *x = xfrm_state_alloc(net);
519 copy_from_user_state(x, p);
521 if ((err = attach_aead(&x->aead, &x->props.ealgo,
522 attrs[XFRMA_ALG_AEAD])))
524 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
525 attrs[XFRMA_ALG_AUTH_TRUNC])))
527 if (!x->props.aalgo) {
528 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
529 attrs[XFRMA_ALG_AUTH])))
532 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
533 xfrm_ealg_get_byname,
534 attrs[XFRMA_ALG_CRYPT])))
536 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
537 xfrm_calg_get_byname,
538 attrs[XFRMA_ALG_COMP])))
541 if (attrs[XFRMA_ENCAP]) {
542 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
543 sizeof(*x->encap), GFP_KERNEL);
544 if (x->encap == NULL)
548 if (attrs[XFRMA_TFCPAD])
549 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
551 if (attrs[XFRMA_COADDR]) {
552 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
553 sizeof(*x->coaddr), GFP_KERNEL);
554 if (x->coaddr == NULL)
558 xfrm_mark_get(attrs, &x->mark);
560 err = __xfrm_init_state(x, false);
564 if (attrs[XFRMA_SEC_CTX]) {
565 err = security_xfrm_state_alloc(x,
566 nla_data(attrs[XFRMA_SEC_CTX]));
571 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
572 attrs[XFRMA_REPLAY_ESN_VAL])))
576 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
577 /* sysctl_xfrm_aevent_etime is in 100ms units */
578 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
580 if ((err = xfrm_init_replay(x)))
583 /* override default values from above */
584 xfrm_update_ae_params(x, attrs, 0);
589 x->km.state = XFRM_STATE_DEAD;
596 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
597 struct nlattr **attrs)
599 struct net *net = sock_net(skb->sk);
600 struct xfrm_usersa_info *p = nlmsg_data(nlh);
601 struct xfrm_state *x;
604 uid_t loginuid = audit_get_loginuid(current);
605 u32 sessionid = audit_get_sessionid(current);
608 err = verify_newsa_info(p, attrs);
612 x = xfrm_state_construct(net, p, attrs, &err);
617 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
618 err = xfrm_state_add(x);
620 err = xfrm_state_update(x);
622 security_task_getsecid(current, &sid);
623 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
626 x->km.state = XFRM_STATE_DEAD;
631 c.seq = nlh->nlmsg_seq;
632 c.pid = nlh->nlmsg_pid;
633 c.event = nlh->nlmsg_type;
635 km_state_notify(x, &c);
641 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
642 struct xfrm_usersa_id *p,
643 struct nlattr **attrs,
646 struct xfrm_state *x = NULL;
649 u32 mark = xfrm_mark_get(attrs, &m);
651 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
653 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
655 xfrm_address_t *saddr = NULL;
657 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
664 x = xfrm_state_lookup_byaddr(net, mark,
666 p->proto, p->family);
675 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
676 struct nlattr **attrs)
678 struct net *net = sock_net(skb->sk);
679 struct xfrm_state *x;
682 struct xfrm_usersa_id *p = nlmsg_data(nlh);
683 uid_t loginuid = audit_get_loginuid(current);
684 u32 sessionid = audit_get_sessionid(current);
687 x = xfrm_user_state_lookup(net, p, attrs, &err);
691 if ((err = security_xfrm_state_delete(x)) != 0)
694 if (xfrm_state_kern(x)) {
699 err = xfrm_state_delete(x);
704 c.seq = nlh->nlmsg_seq;
705 c.pid = nlh->nlmsg_pid;
706 c.event = nlh->nlmsg_type;
707 km_state_notify(x, &c);
710 security_task_getsecid(current, &sid);
711 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
716 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
718 memset(p, 0, sizeof(*p));
719 memcpy(&p->id, &x->id, sizeof(p->id));
720 memcpy(&p->sel, &x->sel, sizeof(p->sel));
721 memcpy(&p->lft, &x->lft, sizeof(p->lft));
722 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
723 memcpy(&p->stats, &x->stats, sizeof(p->stats));
724 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
725 p->mode = x->props.mode;
726 p->replay_window = x->props.replay_window;
727 p->reqid = x->props.reqid;
728 p->family = x->props.family;
729 p->flags = x->props.flags;
733 struct xfrm_dump_info {
734 struct sk_buff *in_skb;
735 struct sk_buff *out_skb;
740 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
742 struct xfrm_user_sec_ctx *uctx;
744 int ctx_size = sizeof(*uctx) + s->ctx_len;
746 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
750 uctx = nla_data(attr);
751 uctx->exttype = XFRMA_SEC_CTX;
752 uctx->len = ctx_size;
753 uctx->ctx_doi = s->ctx_doi;
754 uctx->ctx_alg = s->ctx_alg;
755 uctx->ctx_len = s->ctx_len;
756 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
761 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
763 struct xfrm_algo *algo;
766 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
767 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
771 algo = nla_data(nla);
772 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
773 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
774 algo->alg_key_len = auth->alg_key_len;
779 /* Don't change this without updating xfrm_sa_len! */
780 static int copy_to_user_state_extra(struct xfrm_state *x,
781 struct xfrm_usersa_info *p,
784 copy_to_user_state(x, p);
787 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
790 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
793 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
795 if (copy_to_user_auth(x->aalg, skb))
796 goto nla_put_failure;
798 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
799 xfrm_alg_auth_len(x->aalg), x->aalg);
802 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
804 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
807 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
810 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
812 if (xfrm_mark_put(skb, &x->mark))
813 goto nla_put_failure;
816 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
817 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
819 if (x->security && copy_sec_ctx(x->security, skb) < 0)
820 goto nla_put_failure;
828 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
830 struct xfrm_dump_info *sp = ptr;
831 struct sk_buff *in_skb = sp->in_skb;
832 struct sk_buff *skb = sp->out_skb;
833 struct xfrm_usersa_info *p;
834 struct nlmsghdr *nlh;
837 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
838 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
844 err = copy_to_user_state_extra(x, p, skb);
846 goto nla_put_failure;
852 nlmsg_cancel(skb, nlh);
856 static int xfrm_dump_sa_done(struct netlink_callback *cb)
858 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
859 xfrm_state_walk_done(walk);
863 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
865 struct net *net = sock_net(skb->sk);
866 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
867 struct xfrm_dump_info info;
869 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
870 sizeof(cb->args) - sizeof(cb->args[0]));
872 info.in_skb = cb->skb;
874 info.nlmsg_seq = cb->nlh->nlmsg_seq;
875 info.nlmsg_flags = NLM_F_MULTI;
879 xfrm_state_walk_init(walk, 0);
882 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
887 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
888 struct xfrm_state *x, u32 seq)
890 struct xfrm_dump_info info;
894 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
896 return ERR_PTR(-ENOMEM);
898 info.in_skb = in_skb;
900 info.nlmsg_seq = seq;
901 info.nlmsg_flags = 0;
903 err = dump_one_state(x, 0, &info);
912 static inline size_t xfrm_spdinfo_msgsize(void)
914 return NLMSG_ALIGN(4)
915 + nla_total_size(sizeof(struct xfrmu_spdinfo))
916 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
919 static int build_spdinfo(struct sk_buff *skb, struct net *net,
920 u32 pid, u32 seq, u32 flags)
922 struct xfrmk_spdinfo si;
923 struct xfrmu_spdinfo spc;
924 struct xfrmu_spdhinfo sph;
925 struct nlmsghdr *nlh;
928 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
929 if (nlh == NULL) /* shouldn't really happen ... */
934 xfrm_spd_getinfo(net, &si);
935 spc.incnt = si.incnt;
936 spc.outcnt = si.outcnt;
937 spc.fwdcnt = si.fwdcnt;
938 spc.inscnt = si.inscnt;
939 spc.outscnt = si.outscnt;
940 spc.fwdscnt = si.fwdscnt;
941 sph.spdhcnt = si.spdhcnt;
942 sph.spdhmcnt = si.spdhmcnt;
944 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
945 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
947 return nlmsg_end(skb, nlh);
950 nlmsg_cancel(skb, nlh);
954 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
955 struct nlattr **attrs)
957 struct net *net = sock_net(skb->sk);
958 struct sk_buff *r_skb;
959 u32 *flags = nlmsg_data(nlh);
960 u32 spid = NETLINK_CB(skb).pid;
961 u32 seq = nlh->nlmsg_seq;
963 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
967 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
970 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
973 static inline size_t xfrm_sadinfo_msgsize(void)
975 return NLMSG_ALIGN(4)
976 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
977 + nla_total_size(4); /* XFRMA_SAD_CNT */
980 static int build_sadinfo(struct sk_buff *skb, struct net *net,
981 u32 pid, u32 seq, u32 flags)
983 struct xfrmk_sadinfo si;
984 struct xfrmu_sadhinfo sh;
985 struct nlmsghdr *nlh;
988 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
989 if (nlh == NULL) /* shouldn't really happen ... */
994 xfrm_sad_getinfo(net, &si);
996 sh.sadhmcnt = si.sadhmcnt;
997 sh.sadhcnt = si.sadhcnt;
999 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
1000 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1002 return nlmsg_end(skb, nlh);
1005 nlmsg_cancel(skb, nlh);
1009 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1010 struct nlattr **attrs)
1012 struct net *net = sock_net(skb->sk);
1013 struct sk_buff *r_skb;
1014 u32 *flags = nlmsg_data(nlh);
1015 u32 spid = NETLINK_CB(skb).pid;
1016 u32 seq = nlh->nlmsg_seq;
1018 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1022 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
1025 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
1028 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1029 struct nlattr **attrs)
1031 struct net *net = sock_net(skb->sk);
1032 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1033 struct xfrm_state *x;
1034 struct sk_buff *resp_skb;
1037 x = xfrm_user_state_lookup(net, p, attrs, &err);
1041 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1042 if (IS_ERR(resp_skb)) {
1043 err = PTR_ERR(resp_skb);
1045 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1052 static int verify_userspi_info(struct xfrm_userspi_info *p)
1054 switch (p->info.id.proto) {
1060 /* IPCOMP spi is 16-bits. */
1061 if (p->max >= 0x10000)
1069 if (p->min > p->max)
1075 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1076 struct nlattr **attrs)
1078 struct net *net = sock_net(skb->sk);
1079 struct xfrm_state *x;
1080 struct xfrm_userspi_info *p;
1081 struct sk_buff *resp_skb;
1082 xfrm_address_t *daddr;
1088 p = nlmsg_data(nlh);
1089 err = verify_userspi_info(p);
1093 family = p->info.family;
1094 daddr = &p->info.id.daddr;
1098 mark = xfrm_mark_get(attrs, &m);
1100 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1101 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1108 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1109 p->info.id.proto, daddr,
1116 err = xfrm_alloc_spi(x, p->min, p->max);
1120 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1121 if (IS_ERR(resp_skb)) {
1122 err = PTR_ERR(resp_skb);
1126 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1134 static int verify_policy_dir(u8 dir)
1137 case XFRM_POLICY_IN:
1138 case XFRM_POLICY_OUT:
1139 case XFRM_POLICY_FWD:
1149 static int verify_policy_type(u8 type)
1152 case XFRM_POLICY_TYPE_MAIN:
1153 #ifdef CONFIG_XFRM_SUB_POLICY
1154 case XFRM_POLICY_TYPE_SUB:
1165 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1168 case XFRM_SHARE_ANY:
1169 case XFRM_SHARE_SESSION:
1170 case XFRM_SHARE_USER:
1171 case XFRM_SHARE_UNIQUE:
1178 switch (p->action) {
1179 case XFRM_POLICY_ALLOW:
1180 case XFRM_POLICY_BLOCK:
1187 switch (p->sel.family) {
1192 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1195 return -EAFNOSUPPORT;
1202 return verify_policy_dir(p->dir);
1205 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1207 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1208 struct xfrm_user_sec_ctx *uctx;
1213 uctx = nla_data(rt);
1214 return security_xfrm_policy_alloc(&pol->security, uctx);
1217 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1223 for (i = 0; i < nr; i++, ut++) {
1224 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1226 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1227 memcpy(&t->saddr, &ut->saddr,
1228 sizeof(xfrm_address_t));
1229 t->reqid = ut->reqid;
1231 t->share = ut->share;
1232 t->optional = ut->optional;
1233 t->aalgos = ut->aalgos;
1234 t->ealgos = ut->ealgos;
1235 t->calgos = ut->calgos;
1236 /* If all masks are ~0, then we allow all algorithms. */
1237 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1238 t->encap_family = ut->family;
1242 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1246 if (nr > XFRM_MAX_DEPTH)
1249 for (i = 0; i < nr; i++) {
1250 /* We never validated the ut->family value, so many
1251 * applications simply leave it at zero. The check was
1252 * never made and ut->family was ignored because all
1253 * templates could be assumed to have the same family as
1254 * the policy itself. Now that we will have ipv4-in-ipv6
1255 * and ipv6-in-ipv4 tunnels, this is no longer true.
1258 ut[i].family = family;
1260 switch (ut[i].family) {
1263 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1275 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1277 struct nlattr *rt = attrs[XFRMA_TMPL];
1282 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1283 int nr = nla_len(rt) / sizeof(*utmpl);
1286 err = validate_tmpl(nr, utmpl, pol->family);
1290 copy_templates(pol, utmpl, nr);
1295 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1297 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1298 struct xfrm_userpolicy_type *upt;
1299 u8 type = XFRM_POLICY_TYPE_MAIN;
1307 err = verify_policy_type(type);
1315 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1317 xp->priority = p->priority;
1318 xp->index = p->index;
1319 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1320 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1321 xp->action = p->action;
1322 xp->flags = p->flags;
1323 xp->family = p->sel.family;
1324 /* XXX xp->share = p->share; */
1327 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1329 memset(p, 0, sizeof(*p));
1330 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1331 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1332 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1333 p->priority = xp->priority;
1334 p->index = xp->index;
1335 p->sel.family = xp->family;
1337 p->action = xp->action;
1338 p->flags = xp->flags;
1339 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1342 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1344 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1352 copy_from_user_policy(xp, p);
1354 err = copy_from_user_policy_type(&xp->type, attrs);
1358 if (!(err = copy_from_user_tmpl(xp, attrs)))
1359 err = copy_from_user_sec_ctx(xp, attrs);
1363 xfrm_mark_get(attrs, &xp->mark);
1369 xfrm_policy_destroy(xp);
1373 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1374 struct nlattr **attrs)
1376 struct net *net = sock_net(skb->sk);
1377 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1378 struct xfrm_policy *xp;
1382 uid_t loginuid = audit_get_loginuid(current);
1383 u32 sessionid = audit_get_sessionid(current);
1386 err = verify_newpolicy_info(p);
1389 err = verify_sec_ctx_len(attrs);
1393 xp = xfrm_policy_construct(net, p, attrs, &err);
1397 /* shouldn't excl be based on nlh flags??
1398 * Aha! this is anti-netlink really i.e more pfkey derived
1399 * in netlink excl is a flag and you wouldnt need
1400 * a type XFRM_MSG_UPDPOLICY - JHS */
1401 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1402 err = xfrm_policy_insert(p->dir, xp, excl);
1403 security_task_getsecid(current, &sid);
1404 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1407 security_xfrm_policy_free(xp->security);
1412 c.event = nlh->nlmsg_type;
1413 c.seq = nlh->nlmsg_seq;
1414 c.pid = nlh->nlmsg_pid;
1415 km_policy_notify(xp, p->dir, &c);
1422 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1424 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1427 if (xp->xfrm_nr == 0)
1430 for (i = 0; i < xp->xfrm_nr; i++) {
1431 struct xfrm_user_tmpl *up = &vec[i];
1432 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1434 memset(up, 0, sizeof(*up));
1435 memcpy(&up->id, &kp->id, sizeof(up->id));
1436 up->family = kp->encap_family;
1437 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1438 up->reqid = kp->reqid;
1439 up->mode = kp->mode;
1440 up->share = kp->share;
1441 up->optional = kp->optional;
1442 up->aalgos = kp->aalgos;
1443 up->ealgos = kp->ealgos;
1444 up->calgos = kp->calgos;
1447 return nla_put(skb, XFRMA_TMPL,
1448 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1451 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1454 return copy_sec_ctx(x->security, skb);
1459 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1462 return copy_sec_ctx(xp->security, skb);
1466 static inline size_t userpolicy_type_attrsize(void)
1468 #ifdef CONFIG_XFRM_SUB_POLICY
1469 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1475 #ifdef CONFIG_XFRM_SUB_POLICY
1476 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1478 struct xfrm_userpolicy_type upt = {
1482 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1486 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1492 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1494 struct xfrm_dump_info *sp = ptr;
1495 struct xfrm_userpolicy_info *p;
1496 struct sk_buff *in_skb = sp->in_skb;
1497 struct sk_buff *skb = sp->out_skb;
1498 struct nlmsghdr *nlh;
1500 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1501 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1505 p = nlmsg_data(nlh);
1506 copy_to_user_policy(xp, p, dir);
1507 if (copy_to_user_tmpl(xp, skb) < 0)
1509 if (copy_to_user_sec_ctx(xp, skb))
1511 if (copy_to_user_policy_type(xp->type, skb) < 0)
1513 if (xfrm_mark_put(skb, &xp->mark))
1514 goto nla_put_failure;
1516 nlmsg_end(skb, nlh);
1521 nlmsg_cancel(skb, nlh);
1525 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1527 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1529 xfrm_policy_walk_done(walk);
1533 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1535 struct net *net = sock_net(skb->sk);
1536 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1537 struct xfrm_dump_info info;
1539 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1540 sizeof(cb->args) - sizeof(cb->args[0]));
1542 info.in_skb = cb->skb;
1544 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1545 info.nlmsg_flags = NLM_F_MULTI;
1549 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1552 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1557 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1558 struct xfrm_policy *xp,
1561 struct xfrm_dump_info info;
1562 struct sk_buff *skb;
1565 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1567 return ERR_PTR(-ENOMEM);
1569 info.in_skb = in_skb;
1571 info.nlmsg_seq = seq;
1572 info.nlmsg_flags = 0;
1574 err = dump_one_policy(xp, dir, 0, &info);
1577 return ERR_PTR(err);
1583 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1584 struct nlattr **attrs)
1586 struct net *net = sock_net(skb->sk);
1587 struct xfrm_policy *xp;
1588 struct xfrm_userpolicy_id *p;
1589 u8 type = XFRM_POLICY_TYPE_MAIN;
1594 u32 mark = xfrm_mark_get(attrs, &m);
1596 p = nlmsg_data(nlh);
1597 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1599 err = copy_from_user_policy_type(&type, attrs);
1603 err = verify_policy_dir(p->dir);
1608 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1610 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1611 struct xfrm_sec_ctx *ctx;
1613 err = verify_sec_ctx_len(attrs);
1619 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1621 err = security_xfrm_policy_alloc(&ctx, uctx);
1625 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1627 security_xfrm_policy_free(ctx);
1633 struct sk_buff *resp_skb;
1635 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1636 if (IS_ERR(resp_skb)) {
1637 err = PTR_ERR(resp_skb);
1639 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1640 NETLINK_CB(skb).pid);
1643 uid_t loginuid = audit_get_loginuid(current);
1644 u32 sessionid = audit_get_sessionid(current);
1647 security_task_getsecid(current, &sid);
1648 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1654 c.data.byid = p->index;
1655 c.event = nlh->nlmsg_type;
1656 c.seq = nlh->nlmsg_seq;
1657 c.pid = nlh->nlmsg_pid;
1658 km_policy_notify(xp, p->dir, &c);
1666 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1667 struct nlattr **attrs)
1669 struct net *net = sock_net(skb->sk);
1671 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1672 struct xfrm_audit audit_info;
1675 audit_info.loginuid = audit_get_loginuid(current);
1676 audit_info.sessionid = audit_get_sessionid(current);
1677 security_task_getsecid(current, &audit_info.secid);
1678 err = xfrm_state_flush(net, p->proto, &audit_info);
1680 if (err == -ESRCH) /* empty table */
1684 c.data.proto = p->proto;
1685 c.event = nlh->nlmsg_type;
1686 c.seq = nlh->nlmsg_seq;
1687 c.pid = nlh->nlmsg_pid;
1689 km_state_notify(NULL, &c);
1694 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1696 size_t replay_size = x->replay_esn ?
1697 xfrm_replay_state_esn_len(x->replay_esn) :
1698 sizeof(struct xfrm_replay_state);
1700 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1701 + nla_total_size(replay_size)
1702 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1703 + nla_total_size(sizeof(struct xfrm_mark))
1704 + nla_total_size(4) /* XFRM_AE_RTHR */
1705 + nla_total_size(4); /* XFRM_AE_ETHR */
1708 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1710 struct xfrm_aevent_id *id;
1711 struct nlmsghdr *nlh;
1713 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1717 id = nlmsg_data(nlh);
1718 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1719 id->sa_id.spi = x->id.spi;
1720 id->sa_id.family = x->props.family;
1721 id->sa_id.proto = x->id.proto;
1722 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1723 id->reqid = x->props.reqid;
1724 id->flags = c->data.aevent;
1727 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1728 xfrm_replay_state_esn_len(x->replay_esn),
1731 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1733 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1735 if (id->flags & XFRM_AE_RTHR)
1736 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1738 if (id->flags & XFRM_AE_ETHR)
1739 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1740 x->replay_maxage * 10 / HZ);
1742 if (xfrm_mark_put(skb, &x->mark))
1743 goto nla_put_failure;
1745 return nlmsg_end(skb, nlh);
1748 nlmsg_cancel(skb, nlh);
1752 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1753 struct nlattr **attrs)
1755 struct net *net = sock_net(skb->sk);
1756 struct xfrm_state *x;
1757 struct sk_buff *r_skb;
1762 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1763 struct xfrm_usersa_id *id = &p->sa_id;
1765 mark = xfrm_mark_get(attrs, &m);
1767 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1771 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1772 if (r_skb == NULL) {
1778 * XXX: is this lock really needed - none of the other
1779 * gets lock (the concern is things getting updated
1780 * while we are still reading) - jhs
1782 spin_lock_bh(&x->lock);
1783 c.data.aevent = p->flags;
1784 c.seq = nlh->nlmsg_seq;
1785 c.pid = nlh->nlmsg_pid;
1787 if (build_aevent(r_skb, x, &c) < 0)
1789 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1790 spin_unlock_bh(&x->lock);
1795 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1796 struct nlattr **attrs)
1798 struct net *net = sock_net(skb->sk);
1799 struct xfrm_state *x;
1804 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1805 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1806 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1807 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1809 if (!lt && !rp && !re)
1812 /* pedantic mode - thou shalt sayeth replaceth */
1813 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1816 mark = xfrm_mark_get(attrs, &m);
1818 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1822 if (x->km.state != XFRM_STATE_VALID)
1825 err = xfrm_replay_verify_len(x->replay_esn, rp);
1829 spin_lock_bh(&x->lock);
1830 xfrm_update_ae_params(x, attrs, 1);
1831 spin_unlock_bh(&x->lock);
1833 c.event = nlh->nlmsg_type;
1834 c.seq = nlh->nlmsg_seq;
1835 c.pid = nlh->nlmsg_pid;
1836 c.data.aevent = XFRM_AE_CU;
1837 km_state_notify(x, &c);
1844 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1845 struct nlattr **attrs)
1847 struct net *net = sock_net(skb->sk);
1849 u8 type = XFRM_POLICY_TYPE_MAIN;
1851 struct xfrm_audit audit_info;
1853 err = copy_from_user_policy_type(&type, attrs);
1857 audit_info.loginuid = audit_get_loginuid(current);
1858 audit_info.sessionid = audit_get_sessionid(current);
1859 security_task_getsecid(current, &audit_info.secid);
1860 err = xfrm_policy_flush(net, type, &audit_info);
1862 if (err == -ESRCH) /* empty table */
1868 c.event = nlh->nlmsg_type;
1869 c.seq = nlh->nlmsg_seq;
1870 c.pid = nlh->nlmsg_pid;
1872 km_policy_notify(NULL, 0, &c);
1876 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1877 struct nlattr **attrs)
1879 struct net *net = sock_net(skb->sk);
1880 struct xfrm_policy *xp;
1881 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1882 struct xfrm_userpolicy_info *p = &up->pol;
1883 u8 type = XFRM_POLICY_TYPE_MAIN;
1886 u32 mark = xfrm_mark_get(attrs, &m);
1888 err = copy_from_user_policy_type(&type, attrs);
1892 err = verify_policy_dir(p->dir);
1897 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1899 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1900 struct xfrm_sec_ctx *ctx;
1902 err = verify_sec_ctx_len(attrs);
1908 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1910 err = security_xfrm_policy_alloc(&ctx, uctx);
1914 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1915 &p->sel, ctx, 0, &err);
1916 security_xfrm_policy_free(ctx);
1921 if (unlikely(xp->walk.dead))
1926 uid_t loginuid = audit_get_loginuid(current);
1927 u32 sessionid = audit_get_sessionid(current);
1930 security_task_getsecid(current, &sid);
1931 xfrm_policy_delete(xp, p->dir);
1932 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1935 // reset the timers here?
1936 WARN(1, "Dont know what to do with soft policy expire\n");
1938 km_policy_expired(xp, p->dir, up->hard, current->pid);
1945 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1946 struct nlattr **attrs)
1948 struct net *net = sock_net(skb->sk);
1949 struct xfrm_state *x;
1951 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1952 struct xfrm_usersa_info *p = &ue->state;
1954 u32 mark = xfrm_mark_get(attrs, &m);
1956 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1962 spin_lock_bh(&x->lock);
1964 if (x->km.state != XFRM_STATE_VALID)
1966 km_state_expired(x, ue->hard, current->pid);
1969 uid_t loginuid = audit_get_loginuid(current);
1970 u32 sessionid = audit_get_sessionid(current);
1973 security_task_getsecid(current, &sid);
1974 __xfrm_state_delete(x);
1975 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1979 spin_unlock_bh(&x->lock);
1984 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1985 struct nlattr **attrs)
1987 struct net *net = sock_net(skb->sk);
1988 struct xfrm_policy *xp;
1989 struct xfrm_user_tmpl *ut;
1991 struct nlattr *rt = attrs[XFRMA_TMPL];
1992 struct xfrm_mark mark;
1994 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1995 struct xfrm_state *x = xfrm_state_alloc(net);
2001 xfrm_mark_get(attrs, &mark);
2003 err = verify_newpolicy_info(&ua->policy);
2008 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2012 memcpy(&x->id, &ua->id, sizeof(ua->id));
2013 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2014 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2015 xp->mark.m = x->mark.m = mark.m;
2016 xp->mark.v = x->mark.v = mark.v;
2018 /* extract the templates and for each call km_key */
2019 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2020 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2021 memcpy(&x->id, &t->id, sizeof(x->id));
2022 x->props.mode = t->mode;
2023 x->props.reqid = t->reqid;
2024 x->props.family = ut->family;
2025 t->aalgos = ua->aalgos;
2026 t->ealgos = ua->ealgos;
2027 t->calgos = ua->calgos;
2028 err = km_query(x, t, xp);
2038 WARN(1, "BAD policy passed\n");
2045 #ifdef CONFIG_XFRM_MIGRATE
2046 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2047 struct xfrm_kmaddress *k,
2048 struct nlattr **attrs, int *num)
2050 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2051 struct xfrm_user_migrate *um;
2055 struct xfrm_user_kmaddress *uk;
2057 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2058 memcpy(&k->local, &uk->local, sizeof(k->local));
2059 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2060 k->family = uk->family;
2061 k->reserved = uk->reserved;
2065 num_migrate = nla_len(rt) / sizeof(*um);
2067 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2070 for (i = 0; i < num_migrate; i++, um++, ma++) {
2071 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2072 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2073 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2074 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2076 ma->proto = um->proto;
2077 ma->mode = um->mode;
2078 ma->reqid = um->reqid;
2080 ma->old_family = um->old_family;
2081 ma->new_family = um->new_family;
2088 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2089 struct nlattr **attrs)
2091 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2092 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2093 struct xfrm_kmaddress km, *kmp;
2098 if (attrs[XFRMA_MIGRATE] == NULL)
2101 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2103 err = copy_from_user_policy_type(&type, attrs);
2107 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2114 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2119 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2120 struct nlattr **attrs)
2122 return -ENOPROTOOPT;
2126 #ifdef CONFIG_XFRM_MIGRATE
2127 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2129 struct xfrm_user_migrate um;
2131 memset(&um, 0, sizeof(um));
2132 um.proto = m->proto;
2134 um.reqid = m->reqid;
2135 um.old_family = m->old_family;
2136 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2137 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2138 um.new_family = m->new_family;
2139 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2140 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2142 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2145 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2147 struct xfrm_user_kmaddress uk;
2149 memset(&uk, 0, sizeof(uk));
2150 uk.family = k->family;
2151 uk.reserved = k->reserved;
2152 memcpy(&uk.local, &k->local, sizeof(uk.local));
2153 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2155 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2158 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2160 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2161 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2162 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2163 + userpolicy_type_attrsize();
2166 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2167 int num_migrate, const struct xfrm_kmaddress *k,
2168 const struct xfrm_selector *sel, u8 dir, u8 type)
2170 const struct xfrm_migrate *mp;
2171 struct xfrm_userpolicy_id *pol_id;
2172 struct nlmsghdr *nlh;
2175 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2179 pol_id = nlmsg_data(nlh);
2180 /* copy data from selector, dir, and type to the pol_id */
2181 memset(pol_id, 0, sizeof(*pol_id));
2182 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2185 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2188 if (copy_to_user_policy_type(type, skb) < 0)
2191 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2192 if (copy_to_user_migrate(mp, skb) < 0)
2196 return nlmsg_end(skb, nlh);
2198 nlmsg_cancel(skb, nlh);
2202 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2203 const struct xfrm_migrate *m, int num_migrate,
2204 const struct xfrm_kmaddress *k)
2206 struct net *net = &init_net;
2207 struct sk_buff *skb;
2209 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2214 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2217 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2220 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2221 const struct xfrm_migrate *m, int num_migrate,
2222 const struct xfrm_kmaddress *k)
2224 return -ENOPROTOOPT;
2228 #define XMSGSIZE(type) sizeof(struct type)
2230 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2231 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2232 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2233 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2234 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2235 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2236 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2237 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2238 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2239 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2240 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2241 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2242 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2243 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2244 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2245 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2246 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2247 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2248 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2249 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2250 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2255 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2256 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2257 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2258 [XFRMA_LASTUSED] = { .type = NLA_U64},
2259 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2260 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2261 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2262 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2263 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2264 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2265 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2266 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2267 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2268 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2269 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2270 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2271 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2272 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2273 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2274 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2275 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2276 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2277 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2278 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2281 static struct xfrm_link {
2282 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2283 int (*dump)(struct sk_buff *, struct netlink_callback *);
2284 int (*done)(struct netlink_callback *);
2285 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2286 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2287 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2288 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2289 .dump = xfrm_dump_sa,
2290 .done = xfrm_dump_sa_done },
2291 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2292 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2293 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2294 .dump = xfrm_dump_policy,
2295 .done = xfrm_dump_policy_done },
2296 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2297 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2298 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2299 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2300 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2301 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2302 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2303 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2304 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2305 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2306 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2307 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2308 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2311 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2313 struct net *net = sock_net(skb->sk);
2314 struct nlattr *attrs[XFRMA_MAX+1];
2315 struct xfrm_link *link;
2318 type = nlh->nlmsg_type;
2319 if (type > XFRM_MSG_MAX)
2322 type -= XFRM_MSG_BASE;
2323 link = &xfrm_dispatch[type];
2325 /* All operations require privileges, even GET */
2326 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2329 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2330 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2331 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2332 if (link->dump == NULL)
2335 return netlink_dump_start(net->xfrm.nlsk, skb, nlh,
2336 link->dump, link->done, 0);
2339 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2344 if (link->doit == NULL)
2347 return link->doit(skb, nlh, attrs);
2350 static void xfrm_netlink_rcv(struct sk_buff *skb)
2352 mutex_lock(&xfrm_cfg_mutex);
2353 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2354 mutex_unlock(&xfrm_cfg_mutex);
2357 static inline size_t xfrm_expire_msgsize(void)
2359 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2360 + nla_total_size(sizeof(struct xfrm_mark));
2363 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2365 struct xfrm_user_expire *ue;
2366 struct nlmsghdr *nlh;
2368 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2372 ue = nlmsg_data(nlh);
2373 copy_to_user_state(x, &ue->state);
2374 ue->hard = (c->data.hard != 0) ? 1 : 0;
2376 if (xfrm_mark_put(skb, &x->mark))
2377 goto nla_put_failure;
2379 return nlmsg_end(skb, nlh);
2385 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2387 struct net *net = xs_net(x);
2388 struct sk_buff *skb;
2390 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2394 if (build_expire(skb, x, c) < 0) {
2399 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2402 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2404 struct net *net = xs_net(x);
2405 struct sk_buff *skb;
2407 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2411 if (build_aevent(skb, x, c) < 0)
2414 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2417 static int xfrm_notify_sa_flush(const struct km_event *c)
2419 struct net *net = c->net;
2420 struct xfrm_usersa_flush *p;
2421 struct nlmsghdr *nlh;
2422 struct sk_buff *skb;
2423 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2425 skb = nlmsg_new(len, GFP_ATOMIC);
2429 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2435 p = nlmsg_data(nlh);
2436 p->proto = c->data.proto;
2438 nlmsg_end(skb, nlh);
2440 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2443 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2447 l += nla_total_size(aead_len(x->aead));
2449 l += nla_total_size(sizeof(struct xfrm_algo) +
2450 (x->aalg->alg_key_len + 7) / 8);
2451 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2454 l += nla_total_size(xfrm_alg_len(x->ealg));
2456 l += nla_total_size(sizeof(*x->calg));
2458 l += nla_total_size(sizeof(*x->encap));
2460 l += nla_total_size(sizeof(x->tfcpad));
2462 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2464 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2465 x->security->ctx_len);
2467 l += nla_total_size(sizeof(*x->coaddr));
2469 /* Must count x->lastused as it may become non-zero behind our back. */
2470 l += nla_total_size(sizeof(u64));
2475 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2477 struct net *net = xs_net(x);
2478 struct xfrm_usersa_info *p;
2479 struct xfrm_usersa_id *id;
2480 struct nlmsghdr *nlh;
2481 struct sk_buff *skb;
2482 int len = xfrm_sa_len(x);
2485 headlen = sizeof(*p);
2486 if (c->event == XFRM_MSG_DELSA) {
2487 len += nla_total_size(headlen);
2488 headlen = sizeof(*id);
2489 len += nla_total_size(sizeof(struct xfrm_mark));
2491 len += NLMSG_ALIGN(headlen);
2493 skb = nlmsg_new(len, GFP_ATOMIC);
2497 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2499 goto nla_put_failure;
2501 p = nlmsg_data(nlh);
2502 if (c->event == XFRM_MSG_DELSA) {
2503 struct nlattr *attr;
2505 id = nlmsg_data(nlh);
2506 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2507 id->spi = x->id.spi;
2508 id->family = x->props.family;
2509 id->proto = x->id.proto;
2511 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2513 goto nla_put_failure;
2518 if (copy_to_user_state_extra(x, p, skb))
2519 goto nla_put_failure;
2521 nlmsg_end(skb, nlh);
2523 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2526 /* Somebody screwed up with xfrm_sa_len! */
2532 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2536 case XFRM_MSG_EXPIRE:
2537 return xfrm_exp_state_notify(x, c);
2538 case XFRM_MSG_NEWAE:
2539 return xfrm_aevent_state_notify(x, c);
2540 case XFRM_MSG_DELSA:
2541 case XFRM_MSG_UPDSA:
2542 case XFRM_MSG_NEWSA:
2543 return xfrm_notify_sa(x, c);
2544 case XFRM_MSG_FLUSHSA:
2545 return xfrm_notify_sa_flush(c);
2547 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2556 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2557 struct xfrm_policy *xp)
2559 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2560 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2561 + nla_total_size(sizeof(struct xfrm_mark))
2562 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2563 + userpolicy_type_attrsize();
2566 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2567 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2570 struct xfrm_user_acquire *ua;
2571 struct nlmsghdr *nlh;
2572 __u32 seq = xfrm_get_acqseq();
2574 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2578 ua = nlmsg_data(nlh);
2579 memcpy(&ua->id, &x->id, sizeof(ua->id));
2580 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2581 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2582 copy_to_user_policy(xp, &ua->policy, dir);
2583 ua->aalgos = xt->aalgos;
2584 ua->ealgos = xt->ealgos;
2585 ua->calgos = xt->calgos;
2586 ua->seq = x->km.seq = seq;
2588 if (copy_to_user_tmpl(xp, skb) < 0)
2590 if (copy_to_user_state_sec_ctx(x, skb))
2592 if (copy_to_user_policy_type(xp->type, skb) < 0)
2594 if (xfrm_mark_put(skb, &xp->mark))
2595 goto nla_put_failure;
2597 return nlmsg_end(skb, nlh);
2601 nlmsg_cancel(skb, nlh);
2605 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2606 struct xfrm_policy *xp, int dir)
2608 struct net *net = xs_net(x);
2609 struct sk_buff *skb;
2611 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2615 if (build_acquire(skb, x, xt, xp, dir) < 0)
2618 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2621 /* User gives us xfrm_user_policy_info followed by an array of 0
2622 * or more templates.
2624 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2625 u8 *data, int len, int *dir)
2627 struct net *net = sock_net(sk);
2628 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2629 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2630 struct xfrm_policy *xp;
2633 switch (sk->sk_family) {
2635 if (opt != IP_XFRM_POLICY) {
2640 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2642 if (opt != IPV6_XFRM_POLICY) {
2655 if (len < sizeof(*p) ||
2656 verify_newpolicy_info(p))
2659 nr = ((len - sizeof(*p)) / sizeof(*ut));
2660 if (validate_tmpl(nr, ut, p->sel.family))
2663 if (p->dir > XFRM_POLICY_OUT)
2666 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2672 copy_from_user_policy(xp, p);
2673 xp->type = XFRM_POLICY_TYPE_MAIN;
2674 copy_templates(xp, ut, nr);
2681 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2683 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2684 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2685 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2686 + nla_total_size(sizeof(struct xfrm_mark))
2687 + userpolicy_type_attrsize();
2690 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2691 int dir, const struct km_event *c)
2693 struct xfrm_user_polexpire *upe;
2694 struct nlmsghdr *nlh;
2695 int hard = c->data.hard;
2697 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2701 upe = nlmsg_data(nlh);
2702 copy_to_user_policy(xp, &upe->pol, dir);
2703 if (copy_to_user_tmpl(xp, skb) < 0)
2705 if (copy_to_user_sec_ctx(xp, skb))
2707 if (copy_to_user_policy_type(xp->type, skb) < 0)
2709 if (xfrm_mark_put(skb, &xp->mark))
2710 goto nla_put_failure;
2713 return nlmsg_end(skb, nlh);
2717 nlmsg_cancel(skb, nlh);
2721 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2723 struct net *net = xp_net(xp);
2724 struct sk_buff *skb;
2726 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2730 if (build_polexpire(skb, xp, dir, c) < 0)
2733 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2736 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2738 struct net *net = xp_net(xp);
2739 struct xfrm_userpolicy_info *p;
2740 struct xfrm_userpolicy_id *id;
2741 struct nlmsghdr *nlh;
2742 struct sk_buff *skb;
2743 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2746 headlen = sizeof(*p);
2747 if (c->event == XFRM_MSG_DELPOLICY) {
2748 len += nla_total_size(headlen);
2749 headlen = sizeof(*id);
2751 len += userpolicy_type_attrsize();
2752 len += nla_total_size(sizeof(struct xfrm_mark));
2753 len += NLMSG_ALIGN(headlen);
2755 skb = nlmsg_new(len, GFP_ATOMIC);
2759 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2763 p = nlmsg_data(nlh);
2764 if (c->event == XFRM_MSG_DELPOLICY) {
2765 struct nlattr *attr;
2767 id = nlmsg_data(nlh);
2768 memset(id, 0, sizeof(*id));
2771 id->index = xp->index;
2773 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2775 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2782 copy_to_user_policy(xp, p, dir);
2783 if (copy_to_user_tmpl(xp, skb) < 0)
2785 if (copy_to_user_policy_type(xp->type, skb) < 0)
2788 if (xfrm_mark_put(skb, &xp->mark))
2789 goto nla_put_failure;
2791 nlmsg_end(skb, nlh);
2793 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2801 static int xfrm_notify_policy_flush(const struct km_event *c)
2803 struct net *net = c->net;
2804 struct nlmsghdr *nlh;
2805 struct sk_buff *skb;
2807 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2811 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2814 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2817 nlmsg_end(skb, nlh);
2819 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2826 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2830 case XFRM_MSG_NEWPOLICY:
2831 case XFRM_MSG_UPDPOLICY:
2832 case XFRM_MSG_DELPOLICY:
2833 return xfrm_notify_policy(xp, dir, c);
2834 case XFRM_MSG_FLUSHPOLICY:
2835 return xfrm_notify_policy_flush(c);
2836 case XFRM_MSG_POLEXPIRE:
2837 return xfrm_exp_policy_notify(xp, dir, c);
2839 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2847 static inline size_t xfrm_report_msgsize(void)
2849 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2852 static int build_report(struct sk_buff *skb, u8 proto,
2853 struct xfrm_selector *sel, xfrm_address_t *addr)
2855 struct xfrm_user_report *ur;
2856 struct nlmsghdr *nlh;
2858 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2862 ur = nlmsg_data(nlh);
2864 memcpy(&ur->sel, sel, sizeof(ur->sel));
2867 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2869 return nlmsg_end(skb, nlh);
2872 nlmsg_cancel(skb, nlh);
2876 static int xfrm_send_report(struct net *net, u8 proto,
2877 struct xfrm_selector *sel, xfrm_address_t *addr)
2879 struct sk_buff *skb;
2881 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2885 if (build_report(skb, proto, sel, addr) < 0)
2888 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2891 static inline size_t xfrm_mapping_msgsize(void)
2893 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2896 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2897 xfrm_address_t *new_saddr, __be16 new_sport)
2899 struct xfrm_user_mapping *um;
2900 struct nlmsghdr *nlh;
2902 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2906 um = nlmsg_data(nlh);
2908 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2909 um->id.spi = x->id.spi;
2910 um->id.family = x->props.family;
2911 um->id.proto = x->id.proto;
2912 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2913 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2914 um->new_sport = new_sport;
2915 um->old_sport = x->encap->encap_sport;
2916 um->reqid = x->props.reqid;
2918 return nlmsg_end(skb, nlh);
2921 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2924 struct net *net = xs_net(x);
2925 struct sk_buff *skb;
2927 if (x->id.proto != IPPROTO_ESP)
2933 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2937 if (build_mapping(skb, x, ipaddr, sport) < 0)
2940 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2943 static struct xfrm_mgr netlink_mgr = {
2945 .notify = xfrm_send_state_notify,
2946 .acquire = xfrm_send_acquire,
2947 .compile_policy = xfrm_compile_policy,
2948 .notify_policy = xfrm_send_policy_notify,
2949 .report = xfrm_send_report,
2950 .migrate = xfrm_send_migrate,
2951 .new_mapping = xfrm_send_mapping,
2954 static int __net_init xfrm_user_net_init(struct net *net)
2958 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2959 xfrm_netlink_rcv, NULL, THIS_MODULE);
2962 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2963 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2967 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2970 list_for_each_entry(net, net_exit_list, exit_list)
2971 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
2973 list_for_each_entry(net, net_exit_list, exit_list)
2974 netlink_kernel_release(net->xfrm.nlsk_stash);
2977 static struct pernet_operations xfrm_user_net_ops = {
2978 .init = xfrm_user_net_init,
2979 .exit_batch = xfrm_user_net_exit,
2982 static int __init xfrm_user_init(void)
2986 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2988 rv = register_pernet_subsys(&xfrm_user_net_ops);
2991 rv = xfrm_register_km(&netlink_mgr);
2993 unregister_pernet_subsys(&xfrm_user_net_ops);
2997 static void __exit xfrm_user_exit(void)
2999 xfrm_unregister_km(&netlink_mgr);
3000 unregister_pernet_subsys(&xfrm_user_net_ops);
3003 module_init(xfrm_user_init);
3004 module_exit(xfrm_user_exit);
3005 MODULE_LICENSE("GPL");
3006 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);