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 /* Check the overall length and the internal bitmap length to avoid
394 * potential overflow. */
395 if (nla_len(rp) < ulen ||
396 xfrm_replay_state_esn_len(replay_esn) != ulen ||
397 replay_esn->bmp_len != up->bmp_len)
400 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
406 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
407 struct xfrm_replay_state_esn **preplay_esn,
410 struct xfrm_replay_state_esn *p, *pp, *up;
417 klen = xfrm_replay_state_esn_len(up);
418 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
420 p = kzalloc(klen, GFP_KERNEL);
424 pp = kzalloc(klen, GFP_KERNEL);
431 memcpy(pp, up, ulen);
439 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
444 len += sizeof(struct xfrm_user_sec_ctx);
445 len += xfrm_ctx->ctx_len;
450 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
452 memcpy(&x->id, &p->id, sizeof(x->id));
453 memcpy(&x->sel, &p->sel, sizeof(x->sel));
454 memcpy(&x->lft, &p->lft, sizeof(x->lft));
455 x->props.mode = p->mode;
456 x->props.replay_window = p->replay_window;
457 x->props.reqid = p->reqid;
458 x->props.family = p->family;
459 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
460 x->props.flags = p->flags;
462 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
463 x->sel.family = p->family;
467 * someday when pfkey also has support, we could have the code
468 * somehow made shareable and move it to xfrm_state.c - JHS
471 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
474 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
475 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
476 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
477 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
478 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
481 struct xfrm_replay_state_esn *replay_esn;
482 replay_esn = nla_data(re);
483 memcpy(x->replay_esn, replay_esn,
484 xfrm_replay_state_esn_len(replay_esn));
485 memcpy(x->preplay_esn, replay_esn,
486 xfrm_replay_state_esn_len(replay_esn));
490 struct xfrm_replay_state *replay;
491 replay = nla_data(rp);
492 memcpy(&x->replay, replay, sizeof(*replay));
493 memcpy(&x->preplay, replay, sizeof(*replay));
497 struct xfrm_lifetime_cur *ltime;
498 ltime = nla_data(lt);
499 x->curlft.bytes = ltime->bytes;
500 x->curlft.packets = ltime->packets;
501 x->curlft.add_time = ltime->add_time;
502 x->curlft.use_time = ltime->use_time;
506 x->replay_maxage = nla_get_u32(et);
509 x->replay_maxdiff = nla_get_u32(rt);
512 static struct xfrm_state *xfrm_state_construct(struct net *net,
513 struct xfrm_usersa_info *p,
514 struct nlattr **attrs,
517 struct xfrm_state *x = xfrm_state_alloc(net);
523 copy_from_user_state(x, p);
525 if ((err = attach_aead(&x->aead, &x->props.ealgo,
526 attrs[XFRMA_ALG_AEAD])))
528 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
529 attrs[XFRMA_ALG_AUTH_TRUNC])))
531 if (!x->props.aalgo) {
532 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
533 attrs[XFRMA_ALG_AUTH])))
536 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
537 xfrm_ealg_get_byname,
538 attrs[XFRMA_ALG_CRYPT])))
540 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
541 xfrm_calg_get_byname,
542 attrs[XFRMA_ALG_COMP])))
545 if (attrs[XFRMA_ENCAP]) {
546 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
547 sizeof(*x->encap), GFP_KERNEL);
548 if (x->encap == NULL)
552 if (attrs[XFRMA_TFCPAD])
553 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
555 if (attrs[XFRMA_COADDR]) {
556 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
557 sizeof(*x->coaddr), GFP_KERNEL);
558 if (x->coaddr == NULL)
562 xfrm_mark_get(attrs, &x->mark);
564 err = __xfrm_init_state(x, false);
568 if (attrs[XFRMA_SEC_CTX]) {
569 err = security_xfrm_state_alloc(x,
570 nla_data(attrs[XFRMA_SEC_CTX]));
575 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
576 attrs[XFRMA_REPLAY_ESN_VAL])))
580 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
581 /* sysctl_xfrm_aevent_etime is in 100ms units */
582 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
584 if ((err = xfrm_init_replay(x)))
587 /* override default values from above */
588 xfrm_update_ae_params(x, attrs, 0);
593 x->km.state = XFRM_STATE_DEAD;
600 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
601 struct nlattr **attrs)
603 struct net *net = sock_net(skb->sk);
604 struct xfrm_usersa_info *p = nlmsg_data(nlh);
605 struct xfrm_state *x;
608 uid_t loginuid = audit_get_loginuid(current);
609 u32 sessionid = audit_get_sessionid(current);
612 err = verify_newsa_info(p, attrs);
616 x = xfrm_state_construct(net, p, attrs, &err);
621 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
622 err = xfrm_state_add(x);
624 err = xfrm_state_update(x);
626 security_task_getsecid(current, &sid);
627 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
630 x->km.state = XFRM_STATE_DEAD;
635 c.seq = nlh->nlmsg_seq;
636 c.pid = nlh->nlmsg_pid;
637 c.event = nlh->nlmsg_type;
639 km_state_notify(x, &c);
645 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
646 struct xfrm_usersa_id *p,
647 struct nlattr **attrs,
650 struct xfrm_state *x = NULL;
653 u32 mark = xfrm_mark_get(attrs, &m);
655 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
657 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
659 xfrm_address_t *saddr = NULL;
661 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
668 x = xfrm_state_lookup_byaddr(net, mark,
670 p->proto, p->family);
679 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
680 struct nlattr **attrs)
682 struct net *net = sock_net(skb->sk);
683 struct xfrm_state *x;
686 struct xfrm_usersa_id *p = nlmsg_data(nlh);
687 uid_t loginuid = audit_get_loginuid(current);
688 u32 sessionid = audit_get_sessionid(current);
691 x = xfrm_user_state_lookup(net, p, attrs, &err);
695 if ((err = security_xfrm_state_delete(x)) != 0)
698 if (xfrm_state_kern(x)) {
703 err = xfrm_state_delete(x);
708 c.seq = nlh->nlmsg_seq;
709 c.pid = nlh->nlmsg_pid;
710 c.event = nlh->nlmsg_type;
711 km_state_notify(x, &c);
714 security_task_getsecid(current, &sid);
715 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
720 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
722 memset(p, 0, sizeof(*p));
723 memcpy(&p->id, &x->id, sizeof(p->id));
724 memcpy(&p->sel, &x->sel, sizeof(p->sel));
725 memcpy(&p->lft, &x->lft, sizeof(p->lft));
726 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
727 memcpy(&p->stats, &x->stats, sizeof(p->stats));
728 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
729 p->mode = x->props.mode;
730 p->replay_window = x->props.replay_window;
731 p->reqid = x->props.reqid;
732 p->family = x->props.family;
733 p->flags = x->props.flags;
737 struct xfrm_dump_info {
738 struct sk_buff *in_skb;
739 struct sk_buff *out_skb;
744 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
746 struct xfrm_user_sec_ctx *uctx;
748 int ctx_size = sizeof(*uctx) + s->ctx_len;
750 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
754 uctx = nla_data(attr);
755 uctx->exttype = XFRMA_SEC_CTX;
756 uctx->len = ctx_size;
757 uctx->ctx_doi = s->ctx_doi;
758 uctx->ctx_alg = s->ctx_alg;
759 uctx->ctx_len = s->ctx_len;
760 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
765 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
767 struct xfrm_algo *algo;
770 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
771 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
775 algo = nla_data(nla);
776 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
777 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
778 algo->alg_key_len = auth->alg_key_len;
783 /* Don't change this without updating xfrm_sa_len! */
784 static int copy_to_user_state_extra(struct xfrm_state *x,
785 struct xfrm_usersa_info *p,
788 copy_to_user_state(x, p);
791 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
794 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
797 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
799 if (copy_to_user_auth(x->aalg, skb))
800 goto nla_put_failure;
802 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
803 xfrm_alg_auth_len(x->aalg), x->aalg);
806 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
808 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
811 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
814 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
816 if (xfrm_mark_put(skb, &x->mark))
817 goto nla_put_failure;
820 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
821 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
823 if (x->security && copy_sec_ctx(x->security, skb) < 0)
824 goto nla_put_failure;
832 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
834 struct xfrm_dump_info *sp = ptr;
835 struct sk_buff *in_skb = sp->in_skb;
836 struct sk_buff *skb = sp->out_skb;
837 struct xfrm_usersa_info *p;
838 struct nlmsghdr *nlh;
841 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
842 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
848 err = copy_to_user_state_extra(x, p, skb);
850 goto nla_put_failure;
856 nlmsg_cancel(skb, nlh);
860 static int xfrm_dump_sa_done(struct netlink_callback *cb)
862 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
863 xfrm_state_walk_done(walk);
867 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
869 struct net *net = sock_net(skb->sk);
870 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
871 struct xfrm_dump_info info;
873 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
874 sizeof(cb->args) - sizeof(cb->args[0]));
876 info.in_skb = cb->skb;
878 info.nlmsg_seq = cb->nlh->nlmsg_seq;
879 info.nlmsg_flags = NLM_F_MULTI;
883 xfrm_state_walk_init(walk, 0);
886 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
891 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
892 struct xfrm_state *x, u32 seq)
894 struct xfrm_dump_info info;
898 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
900 return ERR_PTR(-ENOMEM);
902 info.in_skb = in_skb;
904 info.nlmsg_seq = seq;
905 info.nlmsg_flags = 0;
907 err = dump_one_state(x, 0, &info);
916 static inline size_t xfrm_spdinfo_msgsize(void)
918 return NLMSG_ALIGN(4)
919 + nla_total_size(sizeof(struct xfrmu_spdinfo))
920 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
923 static int build_spdinfo(struct sk_buff *skb, struct net *net,
924 u32 pid, u32 seq, u32 flags)
926 struct xfrmk_spdinfo si;
927 struct xfrmu_spdinfo spc;
928 struct xfrmu_spdhinfo sph;
929 struct nlmsghdr *nlh;
932 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
933 if (nlh == NULL) /* shouldn't really happen ... */
938 xfrm_spd_getinfo(net, &si);
939 spc.incnt = si.incnt;
940 spc.outcnt = si.outcnt;
941 spc.fwdcnt = si.fwdcnt;
942 spc.inscnt = si.inscnt;
943 spc.outscnt = si.outscnt;
944 spc.fwdscnt = si.fwdscnt;
945 sph.spdhcnt = si.spdhcnt;
946 sph.spdhmcnt = si.spdhmcnt;
948 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
949 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
951 return nlmsg_end(skb, nlh);
954 nlmsg_cancel(skb, nlh);
958 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
959 struct nlattr **attrs)
961 struct net *net = sock_net(skb->sk);
962 struct sk_buff *r_skb;
963 u32 *flags = nlmsg_data(nlh);
964 u32 spid = NETLINK_CB(skb).pid;
965 u32 seq = nlh->nlmsg_seq;
967 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
971 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
974 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
977 static inline size_t xfrm_sadinfo_msgsize(void)
979 return NLMSG_ALIGN(4)
980 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
981 + nla_total_size(4); /* XFRMA_SAD_CNT */
984 static int build_sadinfo(struct sk_buff *skb, struct net *net,
985 u32 pid, u32 seq, u32 flags)
987 struct xfrmk_sadinfo si;
988 struct xfrmu_sadhinfo sh;
989 struct nlmsghdr *nlh;
992 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
993 if (nlh == NULL) /* shouldn't really happen ... */
998 xfrm_sad_getinfo(net, &si);
1000 sh.sadhmcnt = si.sadhmcnt;
1001 sh.sadhcnt = si.sadhcnt;
1003 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
1004 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1006 return nlmsg_end(skb, nlh);
1009 nlmsg_cancel(skb, nlh);
1013 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1014 struct nlattr **attrs)
1016 struct net *net = sock_net(skb->sk);
1017 struct sk_buff *r_skb;
1018 u32 *flags = nlmsg_data(nlh);
1019 u32 spid = NETLINK_CB(skb).pid;
1020 u32 seq = nlh->nlmsg_seq;
1022 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1026 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
1029 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
1032 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1033 struct nlattr **attrs)
1035 struct net *net = sock_net(skb->sk);
1036 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1037 struct xfrm_state *x;
1038 struct sk_buff *resp_skb;
1041 x = xfrm_user_state_lookup(net, p, attrs, &err);
1045 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1046 if (IS_ERR(resp_skb)) {
1047 err = PTR_ERR(resp_skb);
1049 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1056 static int verify_userspi_info(struct xfrm_userspi_info *p)
1058 switch (p->info.id.proto) {
1064 /* IPCOMP spi is 16-bits. */
1065 if (p->max >= 0x10000)
1073 if (p->min > p->max)
1079 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1080 struct nlattr **attrs)
1082 struct net *net = sock_net(skb->sk);
1083 struct xfrm_state *x;
1084 struct xfrm_userspi_info *p;
1085 struct sk_buff *resp_skb;
1086 xfrm_address_t *daddr;
1092 p = nlmsg_data(nlh);
1093 err = verify_userspi_info(p);
1097 family = p->info.family;
1098 daddr = &p->info.id.daddr;
1102 mark = xfrm_mark_get(attrs, &m);
1104 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1105 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1112 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1113 p->info.id.proto, daddr,
1120 err = xfrm_alloc_spi(x, p->min, p->max);
1124 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1125 if (IS_ERR(resp_skb)) {
1126 err = PTR_ERR(resp_skb);
1130 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1138 static int verify_policy_dir(u8 dir)
1141 case XFRM_POLICY_IN:
1142 case XFRM_POLICY_OUT:
1143 case XFRM_POLICY_FWD:
1153 static int verify_policy_type(u8 type)
1156 case XFRM_POLICY_TYPE_MAIN:
1157 #ifdef CONFIG_XFRM_SUB_POLICY
1158 case XFRM_POLICY_TYPE_SUB:
1169 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1172 case XFRM_SHARE_ANY:
1173 case XFRM_SHARE_SESSION:
1174 case XFRM_SHARE_USER:
1175 case XFRM_SHARE_UNIQUE:
1182 switch (p->action) {
1183 case XFRM_POLICY_ALLOW:
1184 case XFRM_POLICY_BLOCK:
1191 switch (p->sel.family) {
1196 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1199 return -EAFNOSUPPORT;
1206 return verify_policy_dir(p->dir);
1209 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1211 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1212 struct xfrm_user_sec_ctx *uctx;
1217 uctx = nla_data(rt);
1218 return security_xfrm_policy_alloc(&pol->security, uctx);
1221 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1227 for (i = 0; i < nr; i++, ut++) {
1228 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1230 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1231 memcpy(&t->saddr, &ut->saddr,
1232 sizeof(xfrm_address_t));
1233 t->reqid = ut->reqid;
1235 t->share = ut->share;
1236 t->optional = ut->optional;
1237 t->aalgos = ut->aalgos;
1238 t->ealgos = ut->ealgos;
1239 t->calgos = ut->calgos;
1240 /* If all masks are ~0, then we allow all algorithms. */
1241 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1242 t->encap_family = ut->family;
1246 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1250 if (nr > XFRM_MAX_DEPTH)
1253 for (i = 0; i < nr; i++) {
1254 /* We never validated the ut->family value, so many
1255 * applications simply leave it at zero. The check was
1256 * never made and ut->family was ignored because all
1257 * templates could be assumed to have the same family as
1258 * the policy itself. Now that we will have ipv4-in-ipv6
1259 * and ipv6-in-ipv4 tunnels, this is no longer true.
1262 ut[i].family = family;
1264 switch (ut[i].family) {
1267 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1279 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1281 struct nlattr *rt = attrs[XFRMA_TMPL];
1286 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1287 int nr = nla_len(rt) / sizeof(*utmpl);
1290 err = validate_tmpl(nr, utmpl, pol->family);
1294 copy_templates(pol, utmpl, nr);
1299 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1301 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1302 struct xfrm_userpolicy_type *upt;
1303 u8 type = XFRM_POLICY_TYPE_MAIN;
1311 err = verify_policy_type(type);
1319 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1321 xp->priority = p->priority;
1322 xp->index = p->index;
1323 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1324 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1325 xp->action = p->action;
1326 xp->flags = p->flags;
1327 xp->family = p->sel.family;
1328 /* XXX xp->share = p->share; */
1331 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1333 memset(p, 0, sizeof(*p));
1334 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1335 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1336 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1337 p->priority = xp->priority;
1338 p->index = xp->index;
1339 p->sel.family = xp->family;
1341 p->action = xp->action;
1342 p->flags = xp->flags;
1343 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1346 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1348 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1356 copy_from_user_policy(xp, p);
1358 err = copy_from_user_policy_type(&xp->type, attrs);
1362 if (!(err = copy_from_user_tmpl(xp, attrs)))
1363 err = copy_from_user_sec_ctx(xp, attrs);
1367 xfrm_mark_get(attrs, &xp->mark);
1373 xfrm_policy_destroy(xp);
1377 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1378 struct nlattr **attrs)
1380 struct net *net = sock_net(skb->sk);
1381 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1382 struct xfrm_policy *xp;
1386 uid_t loginuid = audit_get_loginuid(current);
1387 u32 sessionid = audit_get_sessionid(current);
1390 err = verify_newpolicy_info(p);
1393 err = verify_sec_ctx_len(attrs);
1397 xp = xfrm_policy_construct(net, p, attrs, &err);
1401 /* shouldn't excl be based on nlh flags??
1402 * Aha! this is anti-netlink really i.e more pfkey derived
1403 * in netlink excl is a flag and you wouldnt need
1404 * a type XFRM_MSG_UPDPOLICY - JHS */
1405 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1406 err = xfrm_policy_insert(p->dir, xp, excl);
1407 security_task_getsecid(current, &sid);
1408 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1411 security_xfrm_policy_free(xp->security);
1416 c.event = nlh->nlmsg_type;
1417 c.seq = nlh->nlmsg_seq;
1418 c.pid = nlh->nlmsg_pid;
1419 km_policy_notify(xp, p->dir, &c);
1426 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1428 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1431 if (xp->xfrm_nr == 0)
1434 for (i = 0; i < xp->xfrm_nr; i++) {
1435 struct xfrm_user_tmpl *up = &vec[i];
1436 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1438 memset(up, 0, sizeof(*up));
1439 memcpy(&up->id, &kp->id, sizeof(up->id));
1440 up->family = kp->encap_family;
1441 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1442 up->reqid = kp->reqid;
1443 up->mode = kp->mode;
1444 up->share = kp->share;
1445 up->optional = kp->optional;
1446 up->aalgos = kp->aalgos;
1447 up->ealgos = kp->ealgos;
1448 up->calgos = kp->calgos;
1451 return nla_put(skb, XFRMA_TMPL,
1452 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1455 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1458 return copy_sec_ctx(x->security, skb);
1463 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1466 return copy_sec_ctx(xp->security, skb);
1470 static inline size_t userpolicy_type_attrsize(void)
1472 #ifdef CONFIG_XFRM_SUB_POLICY
1473 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1479 #ifdef CONFIG_XFRM_SUB_POLICY
1480 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1482 struct xfrm_userpolicy_type upt = {
1486 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1490 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1496 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1498 struct xfrm_dump_info *sp = ptr;
1499 struct xfrm_userpolicy_info *p;
1500 struct sk_buff *in_skb = sp->in_skb;
1501 struct sk_buff *skb = sp->out_skb;
1502 struct nlmsghdr *nlh;
1504 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1505 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1509 p = nlmsg_data(nlh);
1510 copy_to_user_policy(xp, p, dir);
1511 if (copy_to_user_tmpl(xp, skb) < 0)
1513 if (copy_to_user_sec_ctx(xp, skb))
1515 if (copy_to_user_policy_type(xp->type, skb) < 0)
1517 if (xfrm_mark_put(skb, &xp->mark))
1518 goto nla_put_failure;
1520 nlmsg_end(skb, nlh);
1525 nlmsg_cancel(skb, nlh);
1529 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1531 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1533 xfrm_policy_walk_done(walk);
1537 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1539 struct net *net = sock_net(skb->sk);
1540 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1541 struct xfrm_dump_info info;
1543 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1544 sizeof(cb->args) - sizeof(cb->args[0]));
1546 info.in_skb = cb->skb;
1548 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1549 info.nlmsg_flags = NLM_F_MULTI;
1553 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1556 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1561 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1562 struct xfrm_policy *xp,
1565 struct xfrm_dump_info info;
1566 struct sk_buff *skb;
1569 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1571 return ERR_PTR(-ENOMEM);
1573 info.in_skb = in_skb;
1575 info.nlmsg_seq = seq;
1576 info.nlmsg_flags = 0;
1578 err = dump_one_policy(xp, dir, 0, &info);
1581 return ERR_PTR(err);
1587 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1588 struct nlattr **attrs)
1590 struct net *net = sock_net(skb->sk);
1591 struct xfrm_policy *xp;
1592 struct xfrm_userpolicy_id *p;
1593 u8 type = XFRM_POLICY_TYPE_MAIN;
1598 u32 mark = xfrm_mark_get(attrs, &m);
1600 p = nlmsg_data(nlh);
1601 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1603 err = copy_from_user_policy_type(&type, attrs);
1607 err = verify_policy_dir(p->dir);
1612 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1614 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1615 struct xfrm_sec_ctx *ctx;
1617 err = verify_sec_ctx_len(attrs);
1623 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1625 err = security_xfrm_policy_alloc(&ctx, uctx);
1629 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1631 security_xfrm_policy_free(ctx);
1637 struct sk_buff *resp_skb;
1639 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1640 if (IS_ERR(resp_skb)) {
1641 err = PTR_ERR(resp_skb);
1643 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1644 NETLINK_CB(skb).pid);
1647 uid_t loginuid = audit_get_loginuid(current);
1648 u32 sessionid = audit_get_sessionid(current);
1651 security_task_getsecid(current, &sid);
1652 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1658 c.data.byid = p->index;
1659 c.event = nlh->nlmsg_type;
1660 c.seq = nlh->nlmsg_seq;
1661 c.pid = nlh->nlmsg_pid;
1662 km_policy_notify(xp, p->dir, &c);
1670 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1671 struct nlattr **attrs)
1673 struct net *net = sock_net(skb->sk);
1675 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1676 struct xfrm_audit audit_info;
1679 audit_info.loginuid = audit_get_loginuid(current);
1680 audit_info.sessionid = audit_get_sessionid(current);
1681 security_task_getsecid(current, &audit_info.secid);
1682 err = xfrm_state_flush(net, p->proto, &audit_info);
1684 if (err == -ESRCH) /* empty table */
1688 c.data.proto = p->proto;
1689 c.event = nlh->nlmsg_type;
1690 c.seq = nlh->nlmsg_seq;
1691 c.pid = nlh->nlmsg_pid;
1693 km_state_notify(NULL, &c);
1698 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1700 size_t replay_size = x->replay_esn ?
1701 xfrm_replay_state_esn_len(x->replay_esn) :
1702 sizeof(struct xfrm_replay_state);
1704 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1705 + nla_total_size(replay_size)
1706 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1707 + nla_total_size(sizeof(struct xfrm_mark))
1708 + nla_total_size(4) /* XFRM_AE_RTHR */
1709 + nla_total_size(4); /* XFRM_AE_ETHR */
1712 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1714 struct xfrm_aevent_id *id;
1715 struct nlmsghdr *nlh;
1717 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1721 id = nlmsg_data(nlh);
1722 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1723 id->sa_id.spi = x->id.spi;
1724 id->sa_id.family = x->props.family;
1725 id->sa_id.proto = x->id.proto;
1726 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1727 id->reqid = x->props.reqid;
1728 id->flags = c->data.aevent;
1731 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1732 xfrm_replay_state_esn_len(x->replay_esn),
1735 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1737 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1739 if (id->flags & XFRM_AE_RTHR)
1740 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1742 if (id->flags & XFRM_AE_ETHR)
1743 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1744 x->replay_maxage * 10 / HZ);
1746 if (xfrm_mark_put(skb, &x->mark))
1747 goto nla_put_failure;
1749 return nlmsg_end(skb, nlh);
1752 nlmsg_cancel(skb, nlh);
1756 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1757 struct nlattr **attrs)
1759 struct net *net = sock_net(skb->sk);
1760 struct xfrm_state *x;
1761 struct sk_buff *r_skb;
1766 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1767 struct xfrm_usersa_id *id = &p->sa_id;
1769 mark = xfrm_mark_get(attrs, &m);
1771 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1775 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1776 if (r_skb == NULL) {
1782 * XXX: is this lock really needed - none of the other
1783 * gets lock (the concern is things getting updated
1784 * while we are still reading) - jhs
1786 spin_lock_bh(&x->lock);
1787 c.data.aevent = p->flags;
1788 c.seq = nlh->nlmsg_seq;
1789 c.pid = nlh->nlmsg_pid;
1791 if (build_aevent(r_skb, x, &c) < 0)
1793 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1794 spin_unlock_bh(&x->lock);
1799 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1800 struct nlattr **attrs)
1802 struct net *net = sock_net(skb->sk);
1803 struct xfrm_state *x;
1808 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1809 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1810 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1811 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1813 if (!lt && !rp && !re)
1816 /* pedantic mode - thou shalt sayeth replaceth */
1817 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1820 mark = xfrm_mark_get(attrs, &m);
1822 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1826 if (x->km.state != XFRM_STATE_VALID)
1829 err = xfrm_replay_verify_len(x->replay_esn, rp);
1833 spin_lock_bh(&x->lock);
1834 xfrm_update_ae_params(x, attrs, 1);
1835 spin_unlock_bh(&x->lock);
1837 c.event = nlh->nlmsg_type;
1838 c.seq = nlh->nlmsg_seq;
1839 c.pid = nlh->nlmsg_pid;
1840 c.data.aevent = XFRM_AE_CU;
1841 km_state_notify(x, &c);
1848 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1849 struct nlattr **attrs)
1851 struct net *net = sock_net(skb->sk);
1853 u8 type = XFRM_POLICY_TYPE_MAIN;
1855 struct xfrm_audit audit_info;
1857 err = copy_from_user_policy_type(&type, attrs);
1861 audit_info.loginuid = audit_get_loginuid(current);
1862 audit_info.sessionid = audit_get_sessionid(current);
1863 security_task_getsecid(current, &audit_info.secid);
1864 err = xfrm_policy_flush(net, type, &audit_info);
1866 if (err == -ESRCH) /* empty table */
1872 c.event = nlh->nlmsg_type;
1873 c.seq = nlh->nlmsg_seq;
1874 c.pid = nlh->nlmsg_pid;
1876 km_policy_notify(NULL, 0, &c);
1880 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1881 struct nlattr **attrs)
1883 struct net *net = sock_net(skb->sk);
1884 struct xfrm_policy *xp;
1885 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1886 struct xfrm_userpolicy_info *p = &up->pol;
1887 u8 type = XFRM_POLICY_TYPE_MAIN;
1890 u32 mark = xfrm_mark_get(attrs, &m);
1892 err = copy_from_user_policy_type(&type, attrs);
1896 err = verify_policy_dir(p->dir);
1901 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1903 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1904 struct xfrm_sec_ctx *ctx;
1906 err = verify_sec_ctx_len(attrs);
1912 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1914 err = security_xfrm_policy_alloc(&ctx, uctx);
1918 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1919 &p->sel, ctx, 0, &err);
1920 security_xfrm_policy_free(ctx);
1925 if (unlikely(xp->walk.dead))
1930 uid_t loginuid = audit_get_loginuid(current);
1931 u32 sessionid = audit_get_sessionid(current);
1934 security_task_getsecid(current, &sid);
1935 xfrm_policy_delete(xp, p->dir);
1936 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1939 // reset the timers here?
1940 WARN(1, "Dont know what to do with soft policy expire\n");
1942 km_policy_expired(xp, p->dir, up->hard, current->pid);
1949 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1950 struct nlattr **attrs)
1952 struct net *net = sock_net(skb->sk);
1953 struct xfrm_state *x;
1955 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1956 struct xfrm_usersa_info *p = &ue->state;
1958 u32 mark = xfrm_mark_get(attrs, &m);
1960 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1966 spin_lock_bh(&x->lock);
1968 if (x->km.state != XFRM_STATE_VALID)
1970 km_state_expired(x, ue->hard, current->pid);
1973 uid_t loginuid = audit_get_loginuid(current);
1974 u32 sessionid = audit_get_sessionid(current);
1977 security_task_getsecid(current, &sid);
1978 __xfrm_state_delete(x);
1979 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1983 spin_unlock_bh(&x->lock);
1988 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1989 struct nlattr **attrs)
1991 struct net *net = sock_net(skb->sk);
1992 struct xfrm_policy *xp;
1993 struct xfrm_user_tmpl *ut;
1995 struct nlattr *rt = attrs[XFRMA_TMPL];
1996 struct xfrm_mark mark;
1998 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1999 struct xfrm_state *x = xfrm_state_alloc(net);
2005 xfrm_mark_get(attrs, &mark);
2007 err = verify_newpolicy_info(&ua->policy);
2012 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2016 memcpy(&x->id, &ua->id, sizeof(ua->id));
2017 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2018 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2019 xp->mark.m = x->mark.m = mark.m;
2020 xp->mark.v = x->mark.v = mark.v;
2022 /* extract the templates and for each call km_key */
2023 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2024 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2025 memcpy(&x->id, &t->id, sizeof(x->id));
2026 x->props.mode = t->mode;
2027 x->props.reqid = t->reqid;
2028 x->props.family = ut->family;
2029 t->aalgos = ua->aalgos;
2030 t->ealgos = ua->ealgos;
2031 t->calgos = ua->calgos;
2032 err = km_query(x, t, xp);
2042 WARN(1, "BAD policy passed\n");
2049 #ifdef CONFIG_XFRM_MIGRATE
2050 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2051 struct xfrm_kmaddress *k,
2052 struct nlattr **attrs, int *num)
2054 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2055 struct xfrm_user_migrate *um;
2059 struct xfrm_user_kmaddress *uk;
2061 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2062 memcpy(&k->local, &uk->local, sizeof(k->local));
2063 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2064 k->family = uk->family;
2065 k->reserved = uk->reserved;
2069 num_migrate = nla_len(rt) / sizeof(*um);
2071 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2074 for (i = 0; i < num_migrate; i++, um++, ma++) {
2075 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2076 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2077 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2078 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2080 ma->proto = um->proto;
2081 ma->mode = um->mode;
2082 ma->reqid = um->reqid;
2084 ma->old_family = um->old_family;
2085 ma->new_family = um->new_family;
2092 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2093 struct nlattr **attrs)
2095 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2096 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2097 struct xfrm_kmaddress km, *kmp;
2102 if (attrs[XFRMA_MIGRATE] == NULL)
2105 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2107 err = copy_from_user_policy_type(&type, attrs);
2111 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2118 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2123 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2124 struct nlattr **attrs)
2126 return -ENOPROTOOPT;
2130 #ifdef CONFIG_XFRM_MIGRATE
2131 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2133 struct xfrm_user_migrate um;
2135 memset(&um, 0, sizeof(um));
2136 um.proto = m->proto;
2138 um.reqid = m->reqid;
2139 um.old_family = m->old_family;
2140 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2141 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2142 um.new_family = m->new_family;
2143 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2144 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2146 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2149 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2151 struct xfrm_user_kmaddress uk;
2153 memset(&uk, 0, sizeof(uk));
2154 uk.family = k->family;
2155 uk.reserved = k->reserved;
2156 memcpy(&uk.local, &k->local, sizeof(uk.local));
2157 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2159 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2162 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2164 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2165 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2166 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2167 + userpolicy_type_attrsize();
2170 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2171 int num_migrate, const struct xfrm_kmaddress *k,
2172 const struct xfrm_selector *sel, u8 dir, u8 type)
2174 const struct xfrm_migrate *mp;
2175 struct xfrm_userpolicy_id *pol_id;
2176 struct nlmsghdr *nlh;
2179 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2183 pol_id = nlmsg_data(nlh);
2184 /* copy data from selector, dir, and type to the pol_id */
2185 memset(pol_id, 0, sizeof(*pol_id));
2186 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2189 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2192 if (copy_to_user_policy_type(type, skb) < 0)
2195 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2196 if (copy_to_user_migrate(mp, skb) < 0)
2200 return nlmsg_end(skb, nlh);
2202 nlmsg_cancel(skb, nlh);
2206 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2207 const struct xfrm_migrate *m, int num_migrate,
2208 const struct xfrm_kmaddress *k)
2210 struct net *net = &init_net;
2211 struct sk_buff *skb;
2213 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2218 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2221 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2224 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2225 const struct xfrm_migrate *m, int num_migrate,
2226 const struct xfrm_kmaddress *k)
2228 return -ENOPROTOOPT;
2232 #define XMSGSIZE(type) sizeof(struct type)
2234 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2235 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2236 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2237 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2238 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2239 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2240 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2241 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2242 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2243 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2244 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2245 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2246 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2247 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2248 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2249 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2250 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2251 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2252 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2253 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2254 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2259 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2260 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2261 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2262 [XFRMA_LASTUSED] = { .type = NLA_U64},
2263 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2264 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2265 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2266 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2267 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2268 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2269 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2270 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2271 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2272 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2273 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2274 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2275 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2276 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2277 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2278 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2279 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2280 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2281 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2282 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2285 static struct xfrm_link {
2286 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2287 int (*dump)(struct sk_buff *, struct netlink_callback *);
2288 int (*done)(struct netlink_callback *);
2289 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2290 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2291 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2292 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2293 .dump = xfrm_dump_sa,
2294 .done = xfrm_dump_sa_done },
2295 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2296 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2297 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2298 .dump = xfrm_dump_policy,
2299 .done = xfrm_dump_policy_done },
2300 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2301 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2302 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2303 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2304 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2305 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2306 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2307 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2308 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2309 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2310 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2311 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2312 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2315 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2317 struct net *net = sock_net(skb->sk);
2318 struct nlattr *attrs[XFRMA_MAX+1];
2319 struct xfrm_link *link;
2322 type = nlh->nlmsg_type;
2323 if (type > XFRM_MSG_MAX)
2326 type -= XFRM_MSG_BASE;
2327 link = &xfrm_dispatch[type];
2329 /* All operations require privileges, even GET */
2330 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2333 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2334 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2335 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2336 if (link->dump == NULL)
2339 return netlink_dump_start(net->xfrm.nlsk, skb, nlh,
2340 link->dump, link->done, 0);
2343 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2348 if (link->doit == NULL)
2351 return link->doit(skb, nlh, attrs);
2354 static void xfrm_netlink_rcv(struct sk_buff *skb)
2356 mutex_lock(&xfrm_cfg_mutex);
2357 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2358 mutex_unlock(&xfrm_cfg_mutex);
2361 static inline size_t xfrm_expire_msgsize(void)
2363 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2364 + nla_total_size(sizeof(struct xfrm_mark));
2367 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2369 struct xfrm_user_expire *ue;
2370 struct nlmsghdr *nlh;
2372 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2376 ue = nlmsg_data(nlh);
2377 copy_to_user_state(x, &ue->state);
2378 ue->hard = (c->data.hard != 0) ? 1 : 0;
2380 if (xfrm_mark_put(skb, &x->mark))
2381 goto nla_put_failure;
2383 return nlmsg_end(skb, nlh);
2389 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2391 struct net *net = xs_net(x);
2392 struct sk_buff *skb;
2394 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2398 if (build_expire(skb, x, c) < 0) {
2403 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2406 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2408 struct net *net = xs_net(x);
2409 struct sk_buff *skb;
2411 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2415 if (build_aevent(skb, x, c) < 0)
2418 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2421 static int xfrm_notify_sa_flush(const struct km_event *c)
2423 struct net *net = c->net;
2424 struct xfrm_usersa_flush *p;
2425 struct nlmsghdr *nlh;
2426 struct sk_buff *skb;
2427 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2429 skb = nlmsg_new(len, GFP_ATOMIC);
2433 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2439 p = nlmsg_data(nlh);
2440 p->proto = c->data.proto;
2442 nlmsg_end(skb, nlh);
2444 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2447 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2451 l += nla_total_size(aead_len(x->aead));
2453 l += nla_total_size(sizeof(struct xfrm_algo) +
2454 (x->aalg->alg_key_len + 7) / 8);
2455 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2458 l += nla_total_size(xfrm_alg_len(x->ealg));
2460 l += nla_total_size(sizeof(*x->calg));
2462 l += nla_total_size(sizeof(*x->encap));
2464 l += nla_total_size(sizeof(x->tfcpad));
2466 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2468 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2469 x->security->ctx_len);
2471 l += nla_total_size(sizeof(*x->coaddr));
2473 /* Must count x->lastused as it may become non-zero behind our back. */
2474 l += nla_total_size(sizeof(u64));
2479 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2481 struct net *net = xs_net(x);
2482 struct xfrm_usersa_info *p;
2483 struct xfrm_usersa_id *id;
2484 struct nlmsghdr *nlh;
2485 struct sk_buff *skb;
2486 int len = xfrm_sa_len(x);
2489 headlen = sizeof(*p);
2490 if (c->event == XFRM_MSG_DELSA) {
2491 len += nla_total_size(headlen);
2492 headlen = sizeof(*id);
2493 len += nla_total_size(sizeof(struct xfrm_mark));
2495 len += NLMSG_ALIGN(headlen);
2497 skb = nlmsg_new(len, GFP_ATOMIC);
2501 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2503 goto nla_put_failure;
2505 p = nlmsg_data(nlh);
2506 if (c->event == XFRM_MSG_DELSA) {
2507 struct nlattr *attr;
2509 id = nlmsg_data(nlh);
2510 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2511 id->spi = x->id.spi;
2512 id->family = x->props.family;
2513 id->proto = x->id.proto;
2515 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2517 goto nla_put_failure;
2522 if (copy_to_user_state_extra(x, p, skb))
2523 goto nla_put_failure;
2525 nlmsg_end(skb, nlh);
2527 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2530 /* Somebody screwed up with xfrm_sa_len! */
2536 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2540 case XFRM_MSG_EXPIRE:
2541 return xfrm_exp_state_notify(x, c);
2542 case XFRM_MSG_NEWAE:
2543 return xfrm_aevent_state_notify(x, c);
2544 case XFRM_MSG_DELSA:
2545 case XFRM_MSG_UPDSA:
2546 case XFRM_MSG_NEWSA:
2547 return xfrm_notify_sa(x, c);
2548 case XFRM_MSG_FLUSHSA:
2549 return xfrm_notify_sa_flush(c);
2551 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2560 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2561 struct xfrm_policy *xp)
2563 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2564 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2565 + nla_total_size(sizeof(struct xfrm_mark))
2566 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2567 + userpolicy_type_attrsize();
2570 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2571 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2574 struct xfrm_user_acquire *ua;
2575 struct nlmsghdr *nlh;
2576 __u32 seq = xfrm_get_acqseq();
2578 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2582 ua = nlmsg_data(nlh);
2583 memcpy(&ua->id, &x->id, sizeof(ua->id));
2584 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2585 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2586 copy_to_user_policy(xp, &ua->policy, dir);
2587 ua->aalgos = xt->aalgos;
2588 ua->ealgos = xt->ealgos;
2589 ua->calgos = xt->calgos;
2590 ua->seq = x->km.seq = seq;
2592 if (copy_to_user_tmpl(xp, skb) < 0)
2594 if (copy_to_user_state_sec_ctx(x, skb))
2596 if (copy_to_user_policy_type(xp->type, skb) < 0)
2598 if (xfrm_mark_put(skb, &xp->mark))
2599 goto nla_put_failure;
2601 return nlmsg_end(skb, nlh);
2605 nlmsg_cancel(skb, nlh);
2609 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2610 struct xfrm_policy *xp, int dir)
2612 struct net *net = xs_net(x);
2613 struct sk_buff *skb;
2615 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2619 if (build_acquire(skb, x, xt, xp, dir) < 0)
2622 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2625 /* User gives us xfrm_user_policy_info followed by an array of 0
2626 * or more templates.
2628 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2629 u8 *data, int len, int *dir)
2631 struct net *net = sock_net(sk);
2632 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2633 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2634 struct xfrm_policy *xp;
2637 switch (sk->sk_family) {
2639 if (opt != IP_XFRM_POLICY) {
2644 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2646 if (opt != IPV6_XFRM_POLICY) {
2659 if (len < sizeof(*p) ||
2660 verify_newpolicy_info(p))
2663 nr = ((len - sizeof(*p)) / sizeof(*ut));
2664 if (validate_tmpl(nr, ut, p->sel.family))
2667 if (p->dir > XFRM_POLICY_OUT)
2670 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2676 copy_from_user_policy(xp, p);
2677 xp->type = XFRM_POLICY_TYPE_MAIN;
2678 copy_templates(xp, ut, nr);
2685 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2687 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2688 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2689 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2690 + nla_total_size(sizeof(struct xfrm_mark))
2691 + userpolicy_type_attrsize();
2694 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2695 int dir, const struct km_event *c)
2697 struct xfrm_user_polexpire *upe;
2698 struct nlmsghdr *nlh;
2699 int hard = c->data.hard;
2701 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2705 upe = nlmsg_data(nlh);
2706 copy_to_user_policy(xp, &upe->pol, dir);
2707 if (copy_to_user_tmpl(xp, skb) < 0)
2709 if (copy_to_user_sec_ctx(xp, skb))
2711 if (copy_to_user_policy_type(xp->type, skb) < 0)
2713 if (xfrm_mark_put(skb, &xp->mark))
2714 goto nla_put_failure;
2717 return nlmsg_end(skb, nlh);
2721 nlmsg_cancel(skb, nlh);
2725 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2727 struct net *net = xp_net(xp);
2728 struct sk_buff *skb;
2730 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2734 if (build_polexpire(skb, xp, dir, c) < 0)
2737 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2740 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2742 struct net *net = xp_net(xp);
2743 struct xfrm_userpolicy_info *p;
2744 struct xfrm_userpolicy_id *id;
2745 struct nlmsghdr *nlh;
2746 struct sk_buff *skb;
2747 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2750 headlen = sizeof(*p);
2751 if (c->event == XFRM_MSG_DELPOLICY) {
2752 len += nla_total_size(headlen);
2753 headlen = sizeof(*id);
2755 len += userpolicy_type_attrsize();
2756 len += nla_total_size(sizeof(struct xfrm_mark));
2757 len += NLMSG_ALIGN(headlen);
2759 skb = nlmsg_new(len, GFP_ATOMIC);
2763 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2767 p = nlmsg_data(nlh);
2768 if (c->event == XFRM_MSG_DELPOLICY) {
2769 struct nlattr *attr;
2771 id = nlmsg_data(nlh);
2772 memset(id, 0, sizeof(*id));
2775 id->index = xp->index;
2777 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2779 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2786 copy_to_user_policy(xp, p, dir);
2787 if (copy_to_user_tmpl(xp, skb) < 0)
2789 if (copy_to_user_policy_type(xp->type, skb) < 0)
2792 if (xfrm_mark_put(skb, &xp->mark))
2793 goto nla_put_failure;
2795 nlmsg_end(skb, nlh);
2797 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2805 static int xfrm_notify_policy_flush(const struct km_event *c)
2807 struct net *net = c->net;
2808 struct nlmsghdr *nlh;
2809 struct sk_buff *skb;
2811 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2815 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2818 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2821 nlmsg_end(skb, nlh);
2823 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2830 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2834 case XFRM_MSG_NEWPOLICY:
2835 case XFRM_MSG_UPDPOLICY:
2836 case XFRM_MSG_DELPOLICY:
2837 return xfrm_notify_policy(xp, dir, c);
2838 case XFRM_MSG_FLUSHPOLICY:
2839 return xfrm_notify_policy_flush(c);
2840 case XFRM_MSG_POLEXPIRE:
2841 return xfrm_exp_policy_notify(xp, dir, c);
2843 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2851 static inline size_t xfrm_report_msgsize(void)
2853 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2856 static int build_report(struct sk_buff *skb, u8 proto,
2857 struct xfrm_selector *sel, xfrm_address_t *addr)
2859 struct xfrm_user_report *ur;
2860 struct nlmsghdr *nlh;
2862 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2866 ur = nlmsg_data(nlh);
2868 memcpy(&ur->sel, sel, sizeof(ur->sel));
2871 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2873 return nlmsg_end(skb, nlh);
2876 nlmsg_cancel(skb, nlh);
2880 static int xfrm_send_report(struct net *net, u8 proto,
2881 struct xfrm_selector *sel, xfrm_address_t *addr)
2883 struct sk_buff *skb;
2885 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2889 if (build_report(skb, proto, sel, addr) < 0)
2892 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2895 static inline size_t xfrm_mapping_msgsize(void)
2897 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2900 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2901 xfrm_address_t *new_saddr, __be16 new_sport)
2903 struct xfrm_user_mapping *um;
2904 struct nlmsghdr *nlh;
2906 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2910 um = nlmsg_data(nlh);
2912 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2913 um->id.spi = x->id.spi;
2914 um->id.family = x->props.family;
2915 um->id.proto = x->id.proto;
2916 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2917 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2918 um->new_sport = new_sport;
2919 um->old_sport = x->encap->encap_sport;
2920 um->reqid = x->props.reqid;
2922 return nlmsg_end(skb, nlh);
2925 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2928 struct net *net = xs_net(x);
2929 struct sk_buff *skb;
2931 if (x->id.proto != IPPROTO_ESP)
2937 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2941 if (build_mapping(skb, x, ipaddr, sport) < 0)
2944 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2947 static struct xfrm_mgr netlink_mgr = {
2949 .notify = xfrm_send_state_notify,
2950 .acquire = xfrm_send_acquire,
2951 .compile_policy = xfrm_compile_policy,
2952 .notify_policy = xfrm_send_policy_notify,
2953 .report = xfrm_send_report,
2954 .migrate = xfrm_send_migrate,
2955 .new_mapping = xfrm_send_mapping,
2958 static int __net_init xfrm_user_net_init(struct net *net)
2962 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2963 xfrm_netlink_rcv, NULL, THIS_MODULE);
2966 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2967 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2971 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2974 list_for_each_entry(net, net_exit_list, exit_list)
2975 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
2977 list_for_each_entry(net, net_exit_list, exit_list)
2978 netlink_kernel_release(net->xfrm.nlsk_stash);
2981 static struct pernet_operations xfrm_user_net_ops = {
2982 .init = xfrm_user_net_init,
2983 .exit_batch = xfrm_user_net_exit,
2986 static int __init xfrm_user_init(void)
2990 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2992 rv = register_pernet_subsys(&xfrm_user_net_ops);
2995 rv = xfrm_register_km(&netlink_mgr);
2997 unregister_pernet_subsys(&xfrm_user_net_ops);
3001 static void __exit xfrm_user_exit(void)
3003 xfrm_unregister_km(&netlink_mgr);
3004 unregister_pernet_subsys(&xfrm_user_net_ops);
3007 module_init(xfrm_user_init);
3008 module_exit(xfrm_user_exit);
3009 MODULE_LICENSE("GPL");
3010 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);