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];
1534 xfrm_policy_walk_done(walk);
1538 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1540 struct net *net = sock_net(skb->sk);
1541 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1542 struct xfrm_dump_info info;
1544 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1545 sizeof(cb->args) - sizeof(cb->args[0]));
1547 info.in_skb = cb->skb;
1549 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1550 info.nlmsg_flags = NLM_F_MULTI;
1554 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1557 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1562 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1563 struct xfrm_policy *xp,
1566 struct xfrm_dump_info info;
1567 struct sk_buff *skb;
1570 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1572 return ERR_PTR(-ENOMEM);
1574 info.in_skb = in_skb;
1576 info.nlmsg_seq = seq;
1577 info.nlmsg_flags = 0;
1579 err = dump_one_policy(xp, dir, 0, &info);
1582 return ERR_PTR(err);
1588 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1589 struct nlattr **attrs)
1591 struct net *net = sock_net(skb->sk);
1592 struct xfrm_policy *xp;
1593 struct xfrm_userpolicy_id *p;
1594 u8 type = XFRM_POLICY_TYPE_MAIN;
1599 u32 mark = xfrm_mark_get(attrs, &m);
1601 p = nlmsg_data(nlh);
1602 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1604 err = copy_from_user_policy_type(&type, attrs);
1608 err = verify_policy_dir(p->dir);
1613 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1615 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1616 struct xfrm_sec_ctx *ctx;
1618 err = verify_sec_ctx_len(attrs);
1624 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1626 err = security_xfrm_policy_alloc(&ctx, uctx);
1630 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1632 security_xfrm_policy_free(ctx);
1638 struct sk_buff *resp_skb;
1640 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1641 if (IS_ERR(resp_skb)) {
1642 err = PTR_ERR(resp_skb);
1644 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1645 NETLINK_CB(skb).pid);
1648 uid_t loginuid = audit_get_loginuid(current);
1649 u32 sessionid = audit_get_sessionid(current);
1652 security_task_getsecid(current, &sid);
1653 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1659 c.data.byid = p->index;
1660 c.event = nlh->nlmsg_type;
1661 c.seq = nlh->nlmsg_seq;
1662 c.pid = nlh->nlmsg_pid;
1663 km_policy_notify(xp, p->dir, &c);
1671 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1672 struct nlattr **attrs)
1674 struct net *net = sock_net(skb->sk);
1676 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1677 struct xfrm_audit audit_info;
1680 audit_info.loginuid = audit_get_loginuid(current);
1681 audit_info.sessionid = audit_get_sessionid(current);
1682 security_task_getsecid(current, &audit_info.secid);
1683 err = xfrm_state_flush(net, p->proto, &audit_info);
1685 if (err == -ESRCH) /* empty table */
1689 c.data.proto = p->proto;
1690 c.event = nlh->nlmsg_type;
1691 c.seq = nlh->nlmsg_seq;
1692 c.pid = nlh->nlmsg_pid;
1694 km_state_notify(NULL, &c);
1699 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1701 size_t replay_size = x->replay_esn ?
1702 xfrm_replay_state_esn_len(x->replay_esn) :
1703 sizeof(struct xfrm_replay_state);
1705 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1706 + nla_total_size(replay_size)
1707 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1708 + nla_total_size(sizeof(struct xfrm_mark))
1709 + nla_total_size(4) /* XFRM_AE_RTHR */
1710 + nla_total_size(4); /* XFRM_AE_ETHR */
1713 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1715 struct xfrm_aevent_id *id;
1716 struct nlmsghdr *nlh;
1718 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1722 id = nlmsg_data(nlh);
1723 memset(&id->sa_id, 0, sizeof(id->sa_id));
1724 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1725 id->sa_id.spi = x->id.spi;
1726 id->sa_id.family = x->props.family;
1727 id->sa_id.proto = x->id.proto;
1728 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1729 id->reqid = x->props.reqid;
1730 id->flags = c->data.aevent;
1733 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1734 xfrm_replay_state_esn_len(x->replay_esn),
1737 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1739 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1741 if (id->flags & XFRM_AE_RTHR)
1742 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1744 if (id->flags & XFRM_AE_ETHR)
1745 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1746 x->replay_maxage * 10 / HZ);
1748 if (xfrm_mark_put(skb, &x->mark))
1749 goto nla_put_failure;
1751 return nlmsg_end(skb, nlh);
1754 nlmsg_cancel(skb, nlh);
1758 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1759 struct nlattr **attrs)
1761 struct net *net = sock_net(skb->sk);
1762 struct xfrm_state *x;
1763 struct sk_buff *r_skb;
1768 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1769 struct xfrm_usersa_id *id = &p->sa_id;
1771 mark = xfrm_mark_get(attrs, &m);
1773 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1777 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1778 if (r_skb == NULL) {
1784 * XXX: is this lock really needed - none of the other
1785 * gets lock (the concern is things getting updated
1786 * while we are still reading) - jhs
1788 spin_lock_bh(&x->lock);
1789 c.data.aevent = p->flags;
1790 c.seq = nlh->nlmsg_seq;
1791 c.pid = nlh->nlmsg_pid;
1793 if (build_aevent(r_skb, x, &c) < 0)
1795 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1796 spin_unlock_bh(&x->lock);
1801 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1802 struct nlattr **attrs)
1804 struct net *net = sock_net(skb->sk);
1805 struct xfrm_state *x;
1810 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1811 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1812 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1813 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1815 if (!lt && !rp && !re)
1818 /* pedantic mode - thou shalt sayeth replaceth */
1819 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1822 mark = xfrm_mark_get(attrs, &m);
1824 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1828 if (x->km.state != XFRM_STATE_VALID)
1831 err = xfrm_replay_verify_len(x->replay_esn, rp);
1835 spin_lock_bh(&x->lock);
1836 xfrm_update_ae_params(x, attrs, 1);
1837 spin_unlock_bh(&x->lock);
1839 c.event = nlh->nlmsg_type;
1840 c.seq = nlh->nlmsg_seq;
1841 c.pid = nlh->nlmsg_pid;
1842 c.data.aevent = XFRM_AE_CU;
1843 km_state_notify(x, &c);
1850 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1851 struct nlattr **attrs)
1853 struct net *net = sock_net(skb->sk);
1855 u8 type = XFRM_POLICY_TYPE_MAIN;
1857 struct xfrm_audit audit_info;
1859 err = copy_from_user_policy_type(&type, attrs);
1863 audit_info.loginuid = audit_get_loginuid(current);
1864 audit_info.sessionid = audit_get_sessionid(current);
1865 security_task_getsecid(current, &audit_info.secid);
1866 err = xfrm_policy_flush(net, type, &audit_info);
1868 if (err == -ESRCH) /* empty table */
1874 c.event = nlh->nlmsg_type;
1875 c.seq = nlh->nlmsg_seq;
1876 c.pid = nlh->nlmsg_pid;
1878 km_policy_notify(NULL, 0, &c);
1882 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1883 struct nlattr **attrs)
1885 struct net *net = sock_net(skb->sk);
1886 struct xfrm_policy *xp;
1887 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1888 struct xfrm_userpolicy_info *p = &up->pol;
1889 u8 type = XFRM_POLICY_TYPE_MAIN;
1892 u32 mark = xfrm_mark_get(attrs, &m);
1894 err = copy_from_user_policy_type(&type, attrs);
1898 err = verify_policy_dir(p->dir);
1903 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1905 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1906 struct xfrm_sec_ctx *ctx;
1908 err = verify_sec_ctx_len(attrs);
1914 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1916 err = security_xfrm_policy_alloc(&ctx, uctx);
1920 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1921 &p->sel, ctx, 0, &err);
1922 security_xfrm_policy_free(ctx);
1927 if (unlikely(xp->walk.dead))
1932 uid_t loginuid = audit_get_loginuid(current);
1933 u32 sessionid = audit_get_sessionid(current);
1936 security_task_getsecid(current, &sid);
1937 xfrm_policy_delete(xp, p->dir);
1938 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1941 // reset the timers here?
1942 WARN(1, "Dont know what to do with soft policy expire\n");
1944 km_policy_expired(xp, p->dir, up->hard, current->pid);
1951 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1952 struct nlattr **attrs)
1954 struct net *net = sock_net(skb->sk);
1955 struct xfrm_state *x;
1957 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1958 struct xfrm_usersa_info *p = &ue->state;
1960 u32 mark = xfrm_mark_get(attrs, &m);
1962 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1968 spin_lock_bh(&x->lock);
1970 if (x->km.state != XFRM_STATE_VALID)
1972 km_state_expired(x, ue->hard, current->pid);
1975 uid_t loginuid = audit_get_loginuid(current);
1976 u32 sessionid = audit_get_sessionid(current);
1979 security_task_getsecid(current, &sid);
1980 __xfrm_state_delete(x);
1981 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1985 spin_unlock_bh(&x->lock);
1990 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1991 struct nlattr **attrs)
1993 struct net *net = sock_net(skb->sk);
1994 struct xfrm_policy *xp;
1995 struct xfrm_user_tmpl *ut;
1997 struct nlattr *rt = attrs[XFRMA_TMPL];
1998 struct xfrm_mark mark;
2000 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2001 struct xfrm_state *x = xfrm_state_alloc(net);
2007 xfrm_mark_get(attrs, &mark);
2009 err = verify_newpolicy_info(&ua->policy);
2014 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2018 memcpy(&x->id, &ua->id, sizeof(ua->id));
2019 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2020 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2021 xp->mark.m = x->mark.m = mark.m;
2022 xp->mark.v = x->mark.v = mark.v;
2024 /* extract the templates and for each call km_key */
2025 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2026 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2027 memcpy(&x->id, &t->id, sizeof(x->id));
2028 x->props.mode = t->mode;
2029 x->props.reqid = t->reqid;
2030 x->props.family = ut->family;
2031 t->aalgos = ua->aalgos;
2032 t->ealgos = ua->ealgos;
2033 t->calgos = ua->calgos;
2034 err = km_query(x, t, xp);
2044 WARN(1, "BAD policy passed\n");
2051 #ifdef CONFIG_XFRM_MIGRATE
2052 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2053 struct xfrm_kmaddress *k,
2054 struct nlattr **attrs, int *num)
2056 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2057 struct xfrm_user_migrate *um;
2061 struct xfrm_user_kmaddress *uk;
2063 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2064 memcpy(&k->local, &uk->local, sizeof(k->local));
2065 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2066 k->family = uk->family;
2067 k->reserved = uk->reserved;
2071 num_migrate = nla_len(rt) / sizeof(*um);
2073 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2076 for (i = 0; i < num_migrate; i++, um++, ma++) {
2077 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2078 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2079 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2080 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2082 ma->proto = um->proto;
2083 ma->mode = um->mode;
2084 ma->reqid = um->reqid;
2086 ma->old_family = um->old_family;
2087 ma->new_family = um->new_family;
2094 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2095 struct nlattr **attrs)
2097 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2098 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2099 struct xfrm_kmaddress km, *kmp;
2104 if (attrs[XFRMA_MIGRATE] == NULL)
2107 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2109 err = copy_from_user_policy_type(&type, attrs);
2113 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2120 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2125 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2126 struct nlattr **attrs)
2128 return -ENOPROTOOPT;
2132 #ifdef CONFIG_XFRM_MIGRATE
2133 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2135 struct xfrm_user_migrate um;
2137 memset(&um, 0, sizeof(um));
2138 um.proto = m->proto;
2140 um.reqid = m->reqid;
2141 um.old_family = m->old_family;
2142 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2143 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2144 um.new_family = m->new_family;
2145 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2146 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2148 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2151 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2153 struct xfrm_user_kmaddress uk;
2155 memset(&uk, 0, sizeof(uk));
2156 uk.family = k->family;
2157 uk.reserved = k->reserved;
2158 memcpy(&uk.local, &k->local, sizeof(uk.local));
2159 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2161 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2164 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2166 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2167 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2168 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2169 + userpolicy_type_attrsize();
2172 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2173 int num_migrate, const struct xfrm_kmaddress *k,
2174 const struct xfrm_selector *sel, u8 dir, u8 type)
2176 const struct xfrm_migrate *mp;
2177 struct xfrm_userpolicy_id *pol_id;
2178 struct nlmsghdr *nlh;
2181 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2185 pol_id = nlmsg_data(nlh);
2186 /* copy data from selector, dir, and type to the pol_id */
2187 memset(pol_id, 0, sizeof(*pol_id));
2188 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2191 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2194 if (copy_to_user_policy_type(type, skb) < 0)
2197 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2198 if (copy_to_user_migrate(mp, skb) < 0)
2202 return nlmsg_end(skb, nlh);
2204 nlmsg_cancel(skb, nlh);
2208 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2209 const struct xfrm_migrate *m, int num_migrate,
2210 const struct xfrm_kmaddress *k)
2212 struct net *net = &init_net;
2213 struct sk_buff *skb;
2215 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2220 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2223 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2226 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2227 const struct xfrm_migrate *m, int num_migrate,
2228 const struct xfrm_kmaddress *k)
2230 return -ENOPROTOOPT;
2234 #define XMSGSIZE(type) sizeof(struct type)
2236 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2237 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2238 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2239 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2240 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2241 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2242 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2243 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2244 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2245 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2246 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2247 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2248 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2249 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2250 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2251 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2252 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2253 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2254 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2255 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2256 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2261 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2262 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2263 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2264 [XFRMA_LASTUSED] = { .type = NLA_U64},
2265 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2266 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2267 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2268 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2269 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2270 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2271 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2272 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2273 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2274 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2275 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2276 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2277 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2278 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2279 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2280 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2281 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2282 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2283 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2284 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2287 static struct xfrm_link {
2288 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2289 int (*dump)(struct sk_buff *, struct netlink_callback *);
2290 int (*done)(struct netlink_callback *);
2291 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2292 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2293 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2294 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2295 .dump = xfrm_dump_sa,
2296 .done = xfrm_dump_sa_done },
2297 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2298 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2299 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2300 .dump = xfrm_dump_policy,
2301 .done = xfrm_dump_policy_done },
2302 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2303 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2304 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2305 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2306 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2307 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2308 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2309 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2310 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2311 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2312 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2313 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2314 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2317 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2319 struct net *net = sock_net(skb->sk);
2320 struct nlattr *attrs[XFRMA_MAX+1];
2321 struct xfrm_link *link;
2324 type = nlh->nlmsg_type;
2325 if (type > XFRM_MSG_MAX)
2328 type -= XFRM_MSG_BASE;
2329 link = &xfrm_dispatch[type];
2331 /* All operations require privileges, even GET */
2332 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2335 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2336 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2337 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2338 if (link->dump == NULL)
2341 return netlink_dump_start(net->xfrm.nlsk, skb, nlh,
2342 link->dump, link->done, 0);
2345 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2350 if (link->doit == NULL)
2353 return link->doit(skb, nlh, attrs);
2356 static void xfrm_netlink_rcv(struct sk_buff *skb)
2358 mutex_lock(&xfrm_cfg_mutex);
2359 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2360 mutex_unlock(&xfrm_cfg_mutex);
2363 static inline size_t xfrm_expire_msgsize(void)
2365 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2366 + nla_total_size(sizeof(struct xfrm_mark));
2369 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2371 struct xfrm_user_expire *ue;
2372 struct nlmsghdr *nlh;
2374 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2378 ue = nlmsg_data(nlh);
2379 copy_to_user_state(x, &ue->state);
2380 ue->hard = (c->data.hard != 0) ? 1 : 0;
2382 if (xfrm_mark_put(skb, &x->mark))
2383 goto nla_put_failure;
2385 return nlmsg_end(skb, nlh);
2391 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2393 struct net *net = xs_net(x);
2394 struct sk_buff *skb;
2396 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2400 if (build_expire(skb, x, c) < 0) {
2405 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2408 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2410 struct net *net = xs_net(x);
2411 struct sk_buff *skb;
2413 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2417 if (build_aevent(skb, x, c) < 0)
2420 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2423 static int xfrm_notify_sa_flush(const struct km_event *c)
2425 struct net *net = c->net;
2426 struct xfrm_usersa_flush *p;
2427 struct nlmsghdr *nlh;
2428 struct sk_buff *skb;
2429 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2431 skb = nlmsg_new(len, GFP_ATOMIC);
2435 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2441 p = nlmsg_data(nlh);
2442 p->proto = c->data.proto;
2444 nlmsg_end(skb, nlh);
2446 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2449 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2453 l += nla_total_size(aead_len(x->aead));
2455 l += nla_total_size(sizeof(struct xfrm_algo) +
2456 (x->aalg->alg_key_len + 7) / 8);
2457 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2460 l += nla_total_size(xfrm_alg_len(x->ealg));
2462 l += nla_total_size(sizeof(*x->calg));
2464 l += nla_total_size(sizeof(*x->encap));
2466 l += nla_total_size(sizeof(x->tfcpad));
2468 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2470 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2471 x->security->ctx_len);
2473 l += nla_total_size(sizeof(*x->coaddr));
2475 /* Must count x->lastused as it may become non-zero behind our back. */
2476 l += nla_total_size(sizeof(u64));
2481 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2483 struct net *net = xs_net(x);
2484 struct xfrm_usersa_info *p;
2485 struct xfrm_usersa_id *id;
2486 struct nlmsghdr *nlh;
2487 struct sk_buff *skb;
2488 int len = xfrm_sa_len(x);
2491 headlen = sizeof(*p);
2492 if (c->event == XFRM_MSG_DELSA) {
2493 len += nla_total_size(headlen);
2494 headlen = sizeof(*id);
2495 len += nla_total_size(sizeof(struct xfrm_mark));
2497 len += NLMSG_ALIGN(headlen);
2499 skb = nlmsg_new(len, GFP_ATOMIC);
2503 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2505 goto nla_put_failure;
2507 p = nlmsg_data(nlh);
2508 if (c->event == XFRM_MSG_DELSA) {
2509 struct nlattr *attr;
2511 id = nlmsg_data(nlh);
2512 memset(id, 0, sizeof(*id));
2513 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2514 id->spi = x->id.spi;
2515 id->family = x->props.family;
2516 id->proto = x->id.proto;
2518 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2520 goto nla_put_failure;
2525 if (copy_to_user_state_extra(x, p, skb))
2526 goto nla_put_failure;
2528 nlmsg_end(skb, nlh);
2530 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2533 /* Somebody screwed up with xfrm_sa_len! */
2539 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2543 case XFRM_MSG_EXPIRE:
2544 return xfrm_exp_state_notify(x, c);
2545 case XFRM_MSG_NEWAE:
2546 return xfrm_aevent_state_notify(x, c);
2547 case XFRM_MSG_DELSA:
2548 case XFRM_MSG_UPDSA:
2549 case XFRM_MSG_NEWSA:
2550 return xfrm_notify_sa(x, c);
2551 case XFRM_MSG_FLUSHSA:
2552 return xfrm_notify_sa_flush(c);
2554 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2563 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2564 struct xfrm_policy *xp)
2566 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2567 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2568 + nla_total_size(sizeof(struct xfrm_mark))
2569 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2570 + userpolicy_type_attrsize();
2573 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2574 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2577 struct xfrm_user_acquire *ua;
2578 struct nlmsghdr *nlh;
2579 __u32 seq = xfrm_get_acqseq();
2581 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2585 ua = nlmsg_data(nlh);
2586 memcpy(&ua->id, &x->id, sizeof(ua->id));
2587 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2588 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2589 copy_to_user_policy(xp, &ua->policy, dir);
2590 ua->aalgos = xt->aalgos;
2591 ua->ealgos = xt->ealgos;
2592 ua->calgos = xt->calgos;
2593 ua->seq = x->km.seq = seq;
2595 if (copy_to_user_tmpl(xp, skb) < 0)
2597 if (copy_to_user_state_sec_ctx(x, skb))
2599 if (copy_to_user_policy_type(xp->type, skb) < 0)
2601 if (xfrm_mark_put(skb, &xp->mark))
2602 goto nla_put_failure;
2604 return nlmsg_end(skb, nlh);
2608 nlmsg_cancel(skb, nlh);
2612 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2613 struct xfrm_policy *xp, int dir)
2615 struct net *net = xs_net(x);
2616 struct sk_buff *skb;
2618 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2622 if (build_acquire(skb, x, xt, xp, dir) < 0)
2625 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2628 /* User gives us xfrm_user_policy_info followed by an array of 0
2629 * or more templates.
2631 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2632 u8 *data, int len, int *dir)
2634 struct net *net = sock_net(sk);
2635 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2636 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2637 struct xfrm_policy *xp;
2640 switch (sk->sk_family) {
2642 if (opt != IP_XFRM_POLICY) {
2647 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2649 if (opt != IPV6_XFRM_POLICY) {
2662 if (len < sizeof(*p) ||
2663 verify_newpolicy_info(p))
2666 nr = ((len - sizeof(*p)) / sizeof(*ut));
2667 if (validate_tmpl(nr, ut, p->sel.family))
2670 if (p->dir > XFRM_POLICY_OUT)
2673 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2679 copy_from_user_policy(xp, p);
2680 xp->type = XFRM_POLICY_TYPE_MAIN;
2681 copy_templates(xp, ut, nr);
2688 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2690 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2691 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2692 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2693 + nla_total_size(sizeof(struct xfrm_mark))
2694 + userpolicy_type_attrsize();
2697 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2698 int dir, const struct km_event *c)
2700 struct xfrm_user_polexpire *upe;
2701 struct nlmsghdr *nlh;
2702 int hard = c->data.hard;
2704 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2708 upe = nlmsg_data(nlh);
2709 copy_to_user_policy(xp, &upe->pol, dir);
2710 if (copy_to_user_tmpl(xp, skb) < 0)
2712 if (copy_to_user_sec_ctx(xp, skb))
2714 if (copy_to_user_policy_type(xp->type, skb) < 0)
2716 if (xfrm_mark_put(skb, &xp->mark))
2717 goto nla_put_failure;
2720 return nlmsg_end(skb, nlh);
2724 nlmsg_cancel(skb, nlh);
2728 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2730 struct net *net = xp_net(xp);
2731 struct sk_buff *skb;
2733 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2737 if (build_polexpire(skb, xp, dir, c) < 0)
2740 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2743 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2745 struct net *net = xp_net(xp);
2746 struct xfrm_userpolicy_info *p;
2747 struct xfrm_userpolicy_id *id;
2748 struct nlmsghdr *nlh;
2749 struct sk_buff *skb;
2750 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2753 headlen = sizeof(*p);
2754 if (c->event == XFRM_MSG_DELPOLICY) {
2755 len += nla_total_size(headlen);
2756 headlen = sizeof(*id);
2758 len += userpolicy_type_attrsize();
2759 len += nla_total_size(sizeof(struct xfrm_mark));
2760 len += NLMSG_ALIGN(headlen);
2762 skb = nlmsg_new(len, GFP_ATOMIC);
2766 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2770 p = nlmsg_data(nlh);
2771 if (c->event == XFRM_MSG_DELPOLICY) {
2772 struct nlattr *attr;
2774 id = nlmsg_data(nlh);
2775 memset(id, 0, sizeof(*id));
2778 id->index = xp->index;
2780 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2782 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2789 copy_to_user_policy(xp, p, dir);
2790 if (copy_to_user_tmpl(xp, skb) < 0)
2792 if (copy_to_user_policy_type(xp->type, skb) < 0)
2795 if (xfrm_mark_put(skb, &xp->mark))
2796 goto nla_put_failure;
2798 nlmsg_end(skb, nlh);
2800 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2808 static int xfrm_notify_policy_flush(const struct km_event *c)
2810 struct net *net = c->net;
2811 struct nlmsghdr *nlh;
2812 struct sk_buff *skb;
2814 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2818 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2821 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2824 nlmsg_end(skb, nlh);
2826 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2833 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2837 case XFRM_MSG_NEWPOLICY:
2838 case XFRM_MSG_UPDPOLICY:
2839 case XFRM_MSG_DELPOLICY:
2840 return xfrm_notify_policy(xp, dir, c);
2841 case XFRM_MSG_FLUSHPOLICY:
2842 return xfrm_notify_policy_flush(c);
2843 case XFRM_MSG_POLEXPIRE:
2844 return xfrm_exp_policy_notify(xp, dir, c);
2846 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2854 static inline size_t xfrm_report_msgsize(void)
2856 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2859 static int build_report(struct sk_buff *skb, u8 proto,
2860 struct xfrm_selector *sel, xfrm_address_t *addr)
2862 struct xfrm_user_report *ur;
2863 struct nlmsghdr *nlh;
2865 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2869 ur = nlmsg_data(nlh);
2871 memcpy(&ur->sel, sel, sizeof(ur->sel));
2874 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2876 return nlmsg_end(skb, nlh);
2879 nlmsg_cancel(skb, nlh);
2883 static int xfrm_send_report(struct net *net, u8 proto,
2884 struct xfrm_selector *sel, xfrm_address_t *addr)
2886 struct sk_buff *skb;
2888 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2892 if (build_report(skb, proto, sel, addr) < 0)
2895 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2898 static inline size_t xfrm_mapping_msgsize(void)
2900 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2903 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2904 xfrm_address_t *new_saddr, __be16 new_sport)
2906 struct xfrm_user_mapping *um;
2907 struct nlmsghdr *nlh;
2909 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2913 um = nlmsg_data(nlh);
2915 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2916 um->id.spi = x->id.spi;
2917 um->id.family = x->props.family;
2918 um->id.proto = x->id.proto;
2919 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2920 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2921 um->new_sport = new_sport;
2922 um->old_sport = x->encap->encap_sport;
2923 um->reqid = x->props.reqid;
2925 return nlmsg_end(skb, nlh);
2928 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2931 struct net *net = xs_net(x);
2932 struct sk_buff *skb;
2934 if (x->id.proto != IPPROTO_ESP)
2940 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2944 if (build_mapping(skb, x, ipaddr, sport) < 0)
2947 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2950 static struct xfrm_mgr netlink_mgr = {
2952 .notify = xfrm_send_state_notify,
2953 .acquire = xfrm_send_acquire,
2954 .compile_policy = xfrm_compile_policy,
2955 .notify_policy = xfrm_send_policy_notify,
2956 .report = xfrm_send_report,
2957 .migrate = xfrm_send_migrate,
2958 .new_mapping = xfrm_send_mapping,
2961 static int __net_init xfrm_user_net_init(struct net *net)
2965 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2966 xfrm_netlink_rcv, NULL, THIS_MODULE);
2969 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2970 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2974 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2977 list_for_each_entry(net, net_exit_list, exit_list)
2978 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
2980 list_for_each_entry(net, net_exit_list, exit_list)
2981 netlink_kernel_release(net->xfrm.nlsk_stash);
2984 static struct pernet_operations xfrm_user_net_ops = {
2985 .init = xfrm_user_net_init,
2986 .exit_batch = xfrm_user_net_exit,
2989 static int __init xfrm_user_init(void)
2993 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2995 rv = register_pernet_subsys(&xfrm_user_net_ops);
2998 rv = xfrm_register_km(&netlink_mgr);
3000 unregister_pernet_subsys(&xfrm_user_net_ops);
3004 static void __exit xfrm_user_exit(void)
3006 xfrm_unregister_km(&netlink_mgr);
3007 unregister_pernet_subsys(&xfrm_user_net_ops);
3010 module_init(xfrm_user_init);
3011 module_exit(xfrm_user_exit);
3012 MODULE_LICENSE("GPL");
3013 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);