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];
130 if (p->replay_window != 0)
136 static int verify_newsa_info(struct xfrm_usersa_info *p,
137 struct nlattr **attrs)
147 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
159 switch (p->id.proto) {
161 if ((!attrs[XFRMA_ALG_AUTH] &&
162 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
163 attrs[XFRMA_ALG_AEAD] ||
164 attrs[XFRMA_ALG_CRYPT] ||
165 attrs[XFRMA_ALG_COMP] ||
171 if (attrs[XFRMA_ALG_COMP])
173 if (!attrs[XFRMA_ALG_AUTH] &&
174 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
175 !attrs[XFRMA_ALG_CRYPT] &&
176 !attrs[XFRMA_ALG_AEAD])
178 if ((attrs[XFRMA_ALG_AUTH] ||
179 attrs[XFRMA_ALG_AUTH_TRUNC] ||
180 attrs[XFRMA_ALG_CRYPT]) &&
181 attrs[XFRMA_ALG_AEAD])
183 if (attrs[XFRMA_TFCPAD] &&
184 p->mode != XFRM_MODE_TUNNEL)
189 if (!attrs[XFRMA_ALG_COMP] ||
190 attrs[XFRMA_ALG_AEAD] ||
191 attrs[XFRMA_ALG_AUTH] ||
192 attrs[XFRMA_ALG_AUTH_TRUNC] ||
193 attrs[XFRMA_ALG_CRYPT] ||
198 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
199 case IPPROTO_DSTOPTS:
200 case IPPROTO_ROUTING:
201 if (attrs[XFRMA_ALG_COMP] ||
202 attrs[XFRMA_ALG_AUTH] ||
203 attrs[XFRMA_ALG_AUTH_TRUNC] ||
204 attrs[XFRMA_ALG_AEAD] ||
205 attrs[XFRMA_ALG_CRYPT] ||
206 attrs[XFRMA_ENCAP] ||
207 attrs[XFRMA_SEC_CTX] ||
208 attrs[XFRMA_TFCPAD] ||
209 !attrs[XFRMA_COADDR])
218 if ((err = verify_aead(attrs)))
220 if ((err = verify_auth_trunc(attrs)))
222 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
224 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
226 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
228 if ((err = verify_sec_ctx_len(attrs)))
230 if ((err = verify_replay(p, attrs)))
235 case XFRM_MODE_TRANSPORT:
236 case XFRM_MODE_TUNNEL:
237 case XFRM_MODE_ROUTEOPTIMIZATION:
251 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
252 struct xfrm_algo_desc *(*get_byname)(const char *, int),
255 struct xfrm_algo *p, *ualg;
256 struct xfrm_algo_desc *algo;
261 ualg = nla_data(rta);
263 algo = get_byname(ualg->alg_name, 1);
266 *props = algo->desc.sadb_alg_id;
268 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
272 strcpy(p->alg_name, algo->name);
277 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
280 struct xfrm_algo *ualg;
281 struct xfrm_algo_auth *p;
282 struct xfrm_algo_desc *algo;
287 ualg = nla_data(rta);
289 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
292 *props = algo->desc.sadb_alg_id;
294 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
298 strcpy(p->alg_name, algo->name);
299 p->alg_key_len = ualg->alg_key_len;
300 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
301 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
307 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
310 struct xfrm_algo_auth *p, *ualg;
311 struct xfrm_algo_desc *algo;
316 ualg = nla_data(rta);
318 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
321 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
322 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
324 *props = algo->desc.sadb_alg_id;
326 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
330 strcpy(p->alg_name, algo->name);
331 if (!p->alg_trunc_len)
332 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
338 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
341 struct xfrm_algo_aead *p, *ualg;
342 struct xfrm_algo_desc *algo;
347 ualg = nla_data(rta);
349 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
352 *props = algo->desc.sadb_alg_id;
354 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
358 strcpy(p->alg_name, algo->name);
363 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
366 struct xfrm_replay_state_esn *up;
368 if (!replay_esn || !rp)
373 if (xfrm_replay_state_esn_len(replay_esn) !=
374 xfrm_replay_state_esn_len(up))
380 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
381 struct xfrm_replay_state_esn **preplay_esn,
384 struct xfrm_replay_state_esn *p, *pp, *up;
391 p = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
395 pp = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
407 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
412 len += sizeof(struct xfrm_user_sec_ctx);
413 len += xfrm_ctx->ctx_len;
418 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
420 memcpy(&x->id, &p->id, sizeof(x->id));
421 memcpy(&x->sel, &p->sel, sizeof(x->sel));
422 memcpy(&x->lft, &p->lft, sizeof(x->lft));
423 x->props.mode = p->mode;
424 x->props.replay_window = p->replay_window;
425 x->props.reqid = p->reqid;
426 x->props.family = p->family;
427 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
428 x->props.flags = p->flags;
430 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
431 x->sel.family = p->family;
435 * someday when pfkey also has support, we could have the code
436 * somehow made shareable and move it to xfrm_state.c - JHS
439 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
441 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
442 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
443 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
444 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
445 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
448 struct xfrm_replay_state_esn *replay_esn;
449 replay_esn = nla_data(re);
450 memcpy(x->replay_esn, replay_esn,
451 xfrm_replay_state_esn_len(replay_esn));
452 memcpy(x->preplay_esn, replay_esn,
453 xfrm_replay_state_esn_len(replay_esn));
457 struct xfrm_replay_state *replay;
458 replay = nla_data(rp);
459 memcpy(&x->replay, replay, sizeof(*replay));
460 memcpy(&x->preplay, replay, sizeof(*replay));
464 struct xfrm_lifetime_cur *ltime;
465 ltime = nla_data(lt);
466 x->curlft.bytes = ltime->bytes;
467 x->curlft.packets = ltime->packets;
468 x->curlft.add_time = ltime->add_time;
469 x->curlft.use_time = ltime->use_time;
473 x->replay_maxage = nla_get_u32(et);
476 x->replay_maxdiff = nla_get_u32(rt);
479 static struct xfrm_state *xfrm_state_construct(struct net *net,
480 struct xfrm_usersa_info *p,
481 struct nlattr **attrs,
484 struct xfrm_state *x = xfrm_state_alloc(net);
490 copy_from_user_state(x, p);
492 if ((err = attach_aead(&x->aead, &x->props.ealgo,
493 attrs[XFRMA_ALG_AEAD])))
495 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
496 attrs[XFRMA_ALG_AUTH_TRUNC])))
498 if (!x->props.aalgo) {
499 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
500 attrs[XFRMA_ALG_AUTH])))
503 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
504 xfrm_ealg_get_byname,
505 attrs[XFRMA_ALG_CRYPT])))
507 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
508 xfrm_calg_get_byname,
509 attrs[XFRMA_ALG_COMP])))
512 if (attrs[XFRMA_ENCAP]) {
513 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
514 sizeof(*x->encap), GFP_KERNEL);
515 if (x->encap == NULL)
519 if (attrs[XFRMA_TFCPAD])
520 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
522 if (attrs[XFRMA_COADDR]) {
523 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
524 sizeof(*x->coaddr), GFP_KERNEL);
525 if (x->coaddr == NULL)
529 xfrm_mark_get(attrs, &x->mark);
531 err = __xfrm_init_state(x, false);
535 if (attrs[XFRMA_SEC_CTX] &&
536 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
539 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
540 attrs[XFRMA_REPLAY_ESN_VAL])))
544 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
545 /* sysctl_xfrm_aevent_etime is in 100ms units */
546 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
548 if ((err = xfrm_init_replay(x)))
551 /* override default values from above */
552 xfrm_update_ae_params(x, attrs);
557 x->km.state = XFRM_STATE_DEAD;
564 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
565 struct nlattr **attrs)
567 struct net *net = sock_net(skb->sk);
568 struct xfrm_usersa_info *p = nlmsg_data(nlh);
569 struct xfrm_state *x;
572 uid_t loginuid = audit_get_loginuid(current);
573 u32 sessionid = audit_get_sessionid(current);
576 err = verify_newsa_info(p, attrs);
580 x = xfrm_state_construct(net, p, attrs, &err);
585 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
586 err = xfrm_state_add(x);
588 err = xfrm_state_update(x);
590 security_task_getsecid(current, &sid);
591 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
594 x->km.state = XFRM_STATE_DEAD;
599 c.seq = nlh->nlmsg_seq;
600 c.pid = nlh->nlmsg_pid;
601 c.event = nlh->nlmsg_type;
603 km_state_notify(x, &c);
609 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
610 struct xfrm_usersa_id *p,
611 struct nlattr **attrs,
614 struct xfrm_state *x = NULL;
617 u32 mark = xfrm_mark_get(attrs, &m);
619 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
621 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
623 xfrm_address_t *saddr = NULL;
625 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
632 x = xfrm_state_lookup_byaddr(net, mark,
634 p->proto, p->family);
643 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
644 struct nlattr **attrs)
646 struct net *net = sock_net(skb->sk);
647 struct xfrm_state *x;
650 struct xfrm_usersa_id *p = nlmsg_data(nlh);
651 uid_t loginuid = audit_get_loginuid(current);
652 u32 sessionid = audit_get_sessionid(current);
655 x = xfrm_user_state_lookup(net, p, attrs, &err);
659 if ((err = security_xfrm_state_delete(x)) != 0)
662 if (xfrm_state_kern(x)) {
667 err = xfrm_state_delete(x);
672 c.seq = nlh->nlmsg_seq;
673 c.pid = nlh->nlmsg_pid;
674 c.event = nlh->nlmsg_type;
675 km_state_notify(x, &c);
678 security_task_getsecid(current, &sid);
679 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
684 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
686 memcpy(&p->id, &x->id, sizeof(p->id));
687 memcpy(&p->sel, &x->sel, sizeof(p->sel));
688 memcpy(&p->lft, &x->lft, sizeof(p->lft));
689 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
690 memcpy(&p->stats, &x->stats, sizeof(p->stats));
691 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
692 p->mode = x->props.mode;
693 p->replay_window = x->props.replay_window;
694 p->reqid = x->props.reqid;
695 p->family = x->props.family;
696 p->flags = x->props.flags;
700 struct xfrm_dump_info {
701 struct sk_buff *in_skb;
702 struct sk_buff *out_skb;
707 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
709 struct xfrm_user_sec_ctx *uctx;
711 int ctx_size = sizeof(*uctx) + s->ctx_len;
713 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
717 uctx = nla_data(attr);
718 uctx->exttype = XFRMA_SEC_CTX;
719 uctx->len = ctx_size;
720 uctx->ctx_doi = s->ctx_doi;
721 uctx->ctx_alg = s->ctx_alg;
722 uctx->ctx_len = s->ctx_len;
723 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
728 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
730 struct xfrm_algo *algo;
733 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
734 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
738 algo = nla_data(nla);
739 strcpy(algo->alg_name, auth->alg_name);
740 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
741 algo->alg_key_len = auth->alg_key_len;
746 /* Don't change this without updating xfrm_sa_len! */
747 static int copy_to_user_state_extra(struct xfrm_state *x,
748 struct xfrm_usersa_info *p,
751 copy_to_user_state(x, p);
754 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
757 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
760 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
762 if (copy_to_user_auth(x->aalg, skb))
763 goto nla_put_failure;
765 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
766 xfrm_alg_auth_len(x->aalg), x->aalg);
769 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
771 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
774 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
777 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
779 if (xfrm_mark_put(skb, &x->mark))
780 goto nla_put_failure;
783 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
784 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
786 if (x->security && copy_sec_ctx(x->security, skb) < 0)
787 goto nla_put_failure;
795 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
797 struct xfrm_dump_info *sp = ptr;
798 struct sk_buff *in_skb = sp->in_skb;
799 struct sk_buff *skb = sp->out_skb;
800 struct xfrm_usersa_info *p;
801 struct nlmsghdr *nlh;
804 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
805 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
811 err = copy_to_user_state_extra(x, p, skb);
813 goto nla_put_failure;
819 nlmsg_cancel(skb, nlh);
823 static int xfrm_dump_sa_done(struct netlink_callback *cb)
825 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
826 xfrm_state_walk_done(walk);
830 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
832 struct net *net = sock_net(skb->sk);
833 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
834 struct xfrm_dump_info info;
836 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
837 sizeof(cb->args) - sizeof(cb->args[0]));
839 info.in_skb = cb->skb;
841 info.nlmsg_seq = cb->nlh->nlmsg_seq;
842 info.nlmsg_flags = NLM_F_MULTI;
846 xfrm_state_walk_init(walk, 0);
849 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
854 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
855 struct xfrm_state *x, u32 seq)
857 struct xfrm_dump_info info;
860 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
862 return ERR_PTR(-ENOMEM);
864 info.in_skb = in_skb;
866 info.nlmsg_seq = seq;
867 info.nlmsg_flags = 0;
869 if (dump_one_state(x, 0, &info)) {
877 static inline size_t xfrm_spdinfo_msgsize(void)
879 return NLMSG_ALIGN(4)
880 + nla_total_size(sizeof(struct xfrmu_spdinfo))
881 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
884 static int build_spdinfo(struct sk_buff *skb, struct net *net,
885 u32 pid, u32 seq, u32 flags)
887 struct xfrmk_spdinfo si;
888 struct xfrmu_spdinfo spc;
889 struct xfrmu_spdhinfo sph;
890 struct nlmsghdr *nlh;
893 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
894 if (nlh == NULL) /* shouldnt really happen ... */
899 xfrm_spd_getinfo(net, &si);
900 spc.incnt = si.incnt;
901 spc.outcnt = si.outcnt;
902 spc.fwdcnt = si.fwdcnt;
903 spc.inscnt = si.inscnt;
904 spc.outscnt = si.outscnt;
905 spc.fwdscnt = si.fwdscnt;
906 sph.spdhcnt = si.spdhcnt;
907 sph.spdhmcnt = si.spdhmcnt;
909 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
910 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
912 return nlmsg_end(skb, nlh);
915 nlmsg_cancel(skb, nlh);
919 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
920 struct nlattr **attrs)
922 struct net *net = sock_net(skb->sk);
923 struct sk_buff *r_skb;
924 u32 *flags = nlmsg_data(nlh);
925 u32 spid = NETLINK_CB(skb).pid;
926 u32 seq = nlh->nlmsg_seq;
928 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
932 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
935 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
938 static inline size_t xfrm_sadinfo_msgsize(void)
940 return NLMSG_ALIGN(4)
941 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
942 + nla_total_size(4); /* XFRMA_SAD_CNT */
945 static int build_sadinfo(struct sk_buff *skb, struct net *net,
946 u32 pid, u32 seq, u32 flags)
948 struct xfrmk_sadinfo si;
949 struct xfrmu_sadhinfo sh;
950 struct nlmsghdr *nlh;
953 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
954 if (nlh == NULL) /* shouldnt really happen ... */
959 xfrm_sad_getinfo(net, &si);
961 sh.sadhmcnt = si.sadhmcnt;
962 sh.sadhcnt = si.sadhcnt;
964 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
965 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
967 return nlmsg_end(skb, nlh);
970 nlmsg_cancel(skb, nlh);
974 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
975 struct nlattr **attrs)
977 struct net *net = sock_net(skb->sk);
978 struct sk_buff *r_skb;
979 u32 *flags = nlmsg_data(nlh);
980 u32 spid = NETLINK_CB(skb).pid;
981 u32 seq = nlh->nlmsg_seq;
983 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
987 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
990 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
993 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
994 struct nlattr **attrs)
996 struct net *net = sock_net(skb->sk);
997 struct xfrm_usersa_id *p = nlmsg_data(nlh);
998 struct xfrm_state *x;
999 struct sk_buff *resp_skb;
1002 x = xfrm_user_state_lookup(net, p, attrs, &err);
1006 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1007 if (IS_ERR(resp_skb)) {
1008 err = PTR_ERR(resp_skb);
1010 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1017 static int verify_userspi_info(struct xfrm_userspi_info *p)
1019 switch (p->info.id.proto) {
1025 /* IPCOMP spi is 16-bits. */
1026 if (p->max >= 0x10000)
1034 if (p->min > p->max)
1040 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1041 struct nlattr **attrs)
1043 struct net *net = sock_net(skb->sk);
1044 struct xfrm_state *x;
1045 struct xfrm_userspi_info *p;
1046 struct sk_buff *resp_skb;
1047 xfrm_address_t *daddr;
1053 p = nlmsg_data(nlh);
1054 err = verify_userspi_info(p);
1058 family = p->info.family;
1059 daddr = &p->info.id.daddr;
1063 mark = xfrm_mark_get(attrs, &m);
1065 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1066 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1073 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1074 p->info.id.proto, daddr,
1081 err = xfrm_alloc_spi(x, p->min, p->max);
1085 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1086 if (IS_ERR(resp_skb)) {
1087 err = PTR_ERR(resp_skb);
1091 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1099 static int verify_policy_dir(u8 dir)
1102 case XFRM_POLICY_IN:
1103 case XFRM_POLICY_OUT:
1104 case XFRM_POLICY_FWD:
1114 static int verify_policy_type(u8 type)
1117 case XFRM_POLICY_TYPE_MAIN:
1118 #ifdef CONFIG_XFRM_SUB_POLICY
1119 case XFRM_POLICY_TYPE_SUB:
1130 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1133 case XFRM_SHARE_ANY:
1134 case XFRM_SHARE_SESSION:
1135 case XFRM_SHARE_USER:
1136 case XFRM_SHARE_UNIQUE:
1143 switch (p->action) {
1144 case XFRM_POLICY_ALLOW:
1145 case XFRM_POLICY_BLOCK:
1152 switch (p->sel.family) {
1157 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1160 return -EAFNOSUPPORT;
1167 return verify_policy_dir(p->dir);
1170 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1172 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1173 struct xfrm_user_sec_ctx *uctx;
1178 uctx = nla_data(rt);
1179 return security_xfrm_policy_alloc(&pol->security, uctx);
1182 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1188 for (i = 0; i < nr; i++, ut++) {
1189 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1191 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1192 memcpy(&t->saddr, &ut->saddr,
1193 sizeof(xfrm_address_t));
1194 t->reqid = ut->reqid;
1196 t->share = ut->share;
1197 t->optional = ut->optional;
1198 t->aalgos = ut->aalgos;
1199 t->ealgos = ut->ealgos;
1200 t->calgos = ut->calgos;
1201 /* If all masks are ~0, then we allow all algorithms. */
1202 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1203 t->encap_family = ut->family;
1207 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1211 if (nr > XFRM_MAX_DEPTH)
1214 for (i = 0; i < nr; i++) {
1215 /* We never validated the ut->family value, so many
1216 * applications simply leave it at zero. The check was
1217 * never made and ut->family was ignored because all
1218 * templates could be assumed to have the same family as
1219 * the policy itself. Now that we will have ipv4-in-ipv6
1220 * and ipv6-in-ipv4 tunnels, this is no longer true.
1223 ut[i].family = family;
1225 switch (ut[i].family) {
1228 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1240 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1242 struct nlattr *rt = attrs[XFRMA_TMPL];
1247 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1248 int nr = nla_len(rt) / sizeof(*utmpl);
1251 err = validate_tmpl(nr, utmpl, pol->family);
1255 copy_templates(pol, utmpl, nr);
1260 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1262 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1263 struct xfrm_userpolicy_type *upt;
1264 u8 type = XFRM_POLICY_TYPE_MAIN;
1272 err = verify_policy_type(type);
1280 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1282 xp->priority = p->priority;
1283 xp->index = p->index;
1284 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1285 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1286 xp->action = p->action;
1287 xp->flags = p->flags;
1288 xp->family = p->sel.family;
1289 /* XXX xp->share = p->share; */
1292 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1294 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1295 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1296 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1297 p->priority = xp->priority;
1298 p->index = xp->index;
1299 p->sel.family = xp->family;
1301 p->action = xp->action;
1302 p->flags = xp->flags;
1303 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1306 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1308 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1316 copy_from_user_policy(xp, p);
1318 err = copy_from_user_policy_type(&xp->type, attrs);
1322 if (!(err = copy_from_user_tmpl(xp, attrs)))
1323 err = copy_from_user_sec_ctx(xp, attrs);
1327 xfrm_mark_get(attrs, &xp->mark);
1333 xfrm_policy_destroy(xp);
1337 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1338 struct nlattr **attrs)
1340 struct net *net = sock_net(skb->sk);
1341 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1342 struct xfrm_policy *xp;
1346 uid_t loginuid = audit_get_loginuid(current);
1347 u32 sessionid = audit_get_sessionid(current);
1350 err = verify_newpolicy_info(p);
1353 err = verify_sec_ctx_len(attrs);
1357 xp = xfrm_policy_construct(net, p, attrs, &err);
1361 /* shouldnt excl be based on nlh flags??
1362 * Aha! this is anti-netlink really i.e more pfkey derived
1363 * in netlink excl is a flag and you wouldnt need
1364 * a type XFRM_MSG_UPDPOLICY - JHS */
1365 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1366 err = xfrm_policy_insert(p->dir, xp, excl);
1367 security_task_getsecid(current, &sid);
1368 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1371 security_xfrm_policy_free(xp->security);
1376 c.event = nlh->nlmsg_type;
1377 c.seq = nlh->nlmsg_seq;
1378 c.pid = nlh->nlmsg_pid;
1379 km_policy_notify(xp, p->dir, &c);
1386 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1388 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1391 if (xp->xfrm_nr == 0)
1394 for (i = 0; i < xp->xfrm_nr; i++) {
1395 struct xfrm_user_tmpl *up = &vec[i];
1396 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1398 memcpy(&up->id, &kp->id, sizeof(up->id));
1399 up->family = kp->encap_family;
1400 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1401 up->reqid = kp->reqid;
1402 up->mode = kp->mode;
1403 up->share = kp->share;
1404 up->optional = kp->optional;
1405 up->aalgos = kp->aalgos;
1406 up->ealgos = kp->ealgos;
1407 up->calgos = kp->calgos;
1410 return nla_put(skb, XFRMA_TMPL,
1411 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1414 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1417 return copy_sec_ctx(x->security, skb);
1422 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1425 return copy_sec_ctx(xp->security, skb);
1429 static inline size_t userpolicy_type_attrsize(void)
1431 #ifdef CONFIG_XFRM_SUB_POLICY
1432 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1438 #ifdef CONFIG_XFRM_SUB_POLICY
1439 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1441 struct xfrm_userpolicy_type upt = {
1445 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1449 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1455 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1457 struct xfrm_dump_info *sp = ptr;
1458 struct xfrm_userpolicy_info *p;
1459 struct sk_buff *in_skb = sp->in_skb;
1460 struct sk_buff *skb = sp->out_skb;
1461 struct nlmsghdr *nlh;
1463 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1464 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1468 p = nlmsg_data(nlh);
1469 copy_to_user_policy(xp, p, dir);
1470 if (copy_to_user_tmpl(xp, skb) < 0)
1472 if (copy_to_user_sec_ctx(xp, skb))
1474 if (copy_to_user_policy_type(xp->type, skb) < 0)
1476 if (xfrm_mark_put(skb, &xp->mark))
1477 goto nla_put_failure;
1479 nlmsg_end(skb, nlh);
1484 nlmsg_cancel(skb, nlh);
1488 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1490 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1492 xfrm_policy_walk_done(walk);
1496 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1498 struct net *net = sock_net(skb->sk);
1499 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1500 struct xfrm_dump_info info;
1502 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1503 sizeof(cb->args) - sizeof(cb->args[0]));
1505 info.in_skb = cb->skb;
1507 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1508 info.nlmsg_flags = NLM_F_MULTI;
1512 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1515 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1520 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1521 struct xfrm_policy *xp,
1524 struct xfrm_dump_info info;
1525 struct sk_buff *skb;
1527 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1529 return ERR_PTR(-ENOMEM);
1531 info.in_skb = in_skb;
1533 info.nlmsg_seq = seq;
1534 info.nlmsg_flags = 0;
1536 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1544 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1545 struct nlattr **attrs)
1547 struct net *net = sock_net(skb->sk);
1548 struct xfrm_policy *xp;
1549 struct xfrm_userpolicy_id *p;
1550 u8 type = XFRM_POLICY_TYPE_MAIN;
1555 u32 mark = xfrm_mark_get(attrs, &m);
1557 p = nlmsg_data(nlh);
1558 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1560 err = copy_from_user_policy_type(&type, attrs);
1564 err = verify_policy_dir(p->dir);
1569 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1571 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1572 struct xfrm_sec_ctx *ctx;
1574 err = verify_sec_ctx_len(attrs);
1580 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1582 err = security_xfrm_policy_alloc(&ctx, uctx);
1586 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1588 security_xfrm_policy_free(ctx);
1594 struct sk_buff *resp_skb;
1596 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1597 if (IS_ERR(resp_skb)) {
1598 err = PTR_ERR(resp_skb);
1600 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1601 NETLINK_CB(skb).pid);
1604 uid_t loginuid = audit_get_loginuid(current);
1605 u32 sessionid = audit_get_sessionid(current);
1608 security_task_getsecid(current, &sid);
1609 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1615 c.data.byid = p->index;
1616 c.event = nlh->nlmsg_type;
1617 c.seq = nlh->nlmsg_seq;
1618 c.pid = nlh->nlmsg_pid;
1619 km_policy_notify(xp, p->dir, &c);
1627 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1628 struct nlattr **attrs)
1630 struct net *net = sock_net(skb->sk);
1632 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1633 struct xfrm_audit audit_info;
1636 audit_info.loginuid = audit_get_loginuid(current);
1637 audit_info.sessionid = audit_get_sessionid(current);
1638 security_task_getsecid(current, &audit_info.secid);
1639 err = xfrm_state_flush(net, p->proto, &audit_info);
1641 if (err == -ESRCH) /* empty table */
1645 c.data.proto = p->proto;
1646 c.event = nlh->nlmsg_type;
1647 c.seq = nlh->nlmsg_seq;
1648 c.pid = nlh->nlmsg_pid;
1650 km_state_notify(NULL, &c);
1655 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1657 size_t replay_size = x->replay_esn ?
1658 xfrm_replay_state_esn_len(x->replay_esn) :
1659 sizeof(struct xfrm_replay_state);
1661 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1662 + nla_total_size(replay_size)
1663 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1664 + nla_total_size(sizeof(struct xfrm_mark))
1665 + nla_total_size(4) /* XFRM_AE_RTHR */
1666 + nla_total_size(4); /* XFRM_AE_ETHR */
1669 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1671 struct xfrm_aevent_id *id;
1672 struct nlmsghdr *nlh;
1674 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1678 id = nlmsg_data(nlh);
1679 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1680 id->sa_id.spi = x->id.spi;
1681 id->sa_id.family = x->props.family;
1682 id->sa_id.proto = x->id.proto;
1683 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1684 id->reqid = x->props.reqid;
1685 id->flags = c->data.aevent;
1688 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1689 xfrm_replay_state_esn_len(x->replay_esn),
1692 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1694 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1696 if (id->flags & XFRM_AE_RTHR)
1697 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1699 if (id->flags & XFRM_AE_ETHR)
1700 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1701 x->replay_maxage * 10 / HZ);
1703 if (xfrm_mark_put(skb, &x->mark))
1704 goto nla_put_failure;
1706 return nlmsg_end(skb, nlh);
1709 nlmsg_cancel(skb, nlh);
1713 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1714 struct nlattr **attrs)
1716 struct net *net = sock_net(skb->sk);
1717 struct xfrm_state *x;
1718 struct sk_buff *r_skb;
1723 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1724 struct xfrm_usersa_id *id = &p->sa_id;
1726 mark = xfrm_mark_get(attrs, &m);
1728 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1732 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1733 if (r_skb == NULL) {
1739 * XXX: is this lock really needed - none of the other
1740 * gets lock (the concern is things getting updated
1741 * while we are still reading) - jhs
1743 spin_lock_bh(&x->lock);
1744 c.data.aevent = p->flags;
1745 c.seq = nlh->nlmsg_seq;
1746 c.pid = nlh->nlmsg_pid;
1748 if (build_aevent(r_skb, x, &c) < 0)
1750 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1751 spin_unlock_bh(&x->lock);
1756 static int xfrm_new_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;
1765 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1766 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1767 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1768 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1770 if (!lt && !rp && !re)
1773 /* pedantic mode - thou shalt sayeth replaceth */
1774 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1777 mark = xfrm_mark_get(attrs, &m);
1779 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1783 if (x->km.state != XFRM_STATE_VALID)
1786 err = xfrm_replay_verify_len(x->replay_esn, rp);
1790 spin_lock_bh(&x->lock);
1791 xfrm_update_ae_params(x, attrs);
1792 spin_unlock_bh(&x->lock);
1794 c.event = nlh->nlmsg_type;
1795 c.seq = nlh->nlmsg_seq;
1796 c.pid = nlh->nlmsg_pid;
1797 c.data.aevent = XFRM_AE_CU;
1798 km_state_notify(x, &c);
1805 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1806 struct nlattr **attrs)
1808 struct net *net = sock_net(skb->sk);
1810 u8 type = XFRM_POLICY_TYPE_MAIN;
1812 struct xfrm_audit audit_info;
1814 err = copy_from_user_policy_type(&type, attrs);
1818 audit_info.loginuid = audit_get_loginuid(current);
1819 audit_info.sessionid = audit_get_sessionid(current);
1820 security_task_getsecid(current, &audit_info.secid);
1821 err = xfrm_policy_flush(net, type, &audit_info);
1823 if (err == -ESRCH) /* empty table */
1829 c.event = nlh->nlmsg_type;
1830 c.seq = nlh->nlmsg_seq;
1831 c.pid = nlh->nlmsg_pid;
1833 km_policy_notify(NULL, 0, &c);
1837 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1838 struct nlattr **attrs)
1840 struct net *net = sock_net(skb->sk);
1841 struct xfrm_policy *xp;
1842 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1843 struct xfrm_userpolicy_info *p = &up->pol;
1844 u8 type = XFRM_POLICY_TYPE_MAIN;
1847 u32 mark = xfrm_mark_get(attrs, &m);
1849 err = copy_from_user_policy_type(&type, attrs);
1853 err = verify_policy_dir(p->dir);
1858 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1860 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1861 struct xfrm_sec_ctx *ctx;
1863 err = verify_sec_ctx_len(attrs);
1869 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1871 err = security_xfrm_policy_alloc(&ctx, uctx);
1875 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1876 &p->sel, ctx, 0, &err);
1877 security_xfrm_policy_free(ctx);
1882 if (unlikely(xp->walk.dead))
1887 uid_t loginuid = audit_get_loginuid(current);
1888 u32 sessionid = audit_get_sessionid(current);
1891 security_task_getsecid(current, &sid);
1892 xfrm_policy_delete(xp, p->dir);
1893 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1896 // reset the timers here?
1897 WARN(1, "Dont know what to do with soft policy expire\n");
1899 km_policy_expired(xp, p->dir, up->hard, current->pid);
1906 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1907 struct nlattr **attrs)
1909 struct net *net = sock_net(skb->sk);
1910 struct xfrm_state *x;
1912 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1913 struct xfrm_usersa_info *p = &ue->state;
1915 u32 mark = xfrm_mark_get(attrs, &m);
1917 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1923 spin_lock_bh(&x->lock);
1925 if (x->km.state != XFRM_STATE_VALID)
1927 km_state_expired(x, ue->hard, current->pid);
1930 uid_t loginuid = audit_get_loginuid(current);
1931 u32 sessionid = audit_get_sessionid(current);
1934 security_task_getsecid(current, &sid);
1935 __xfrm_state_delete(x);
1936 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1940 spin_unlock_bh(&x->lock);
1945 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1946 struct nlattr **attrs)
1948 struct net *net = sock_net(skb->sk);
1949 struct xfrm_policy *xp;
1950 struct xfrm_user_tmpl *ut;
1952 struct nlattr *rt = attrs[XFRMA_TMPL];
1953 struct xfrm_mark mark;
1955 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1956 struct xfrm_state *x = xfrm_state_alloc(net);
1962 xfrm_mark_get(attrs, &mark);
1964 err = verify_newpolicy_info(&ua->policy);
1969 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1973 memcpy(&x->id, &ua->id, sizeof(ua->id));
1974 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1975 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1976 xp->mark.m = x->mark.m = mark.m;
1977 xp->mark.v = x->mark.v = mark.v;
1979 /* extract the templates and for each call km_key */
1980 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1981 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1982 memcpy(&x->id, &t->id, sizeof(x->id));
1983 x->props.mode = t->mode;
1984 x->props.reqid = t->reqid;
1985 x->props.family = ut->family;
1986 t->aalgos = ua->aalgos;
1987 t->ealgos = ua->ealgos;
1988 t->calgos = ua->calgos;
1989 err = km_query(x, t, xp);
1999 WARN(1, "BAD policy passed\n");
2006 #ifdef CONFIG_XFRM_MIGRATE
2007 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2008 struct xfrm_kmaddress *k,
2009 struct nlattr **attrs, int *num)
2011 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2012 struct xfrm_user_migrate *um;
2016 struct xfrm_user_kmaddress *uk;
2018 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2019 memcpy(&k->local, &uk->local, sizeof(k->local));
2020 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2021 k->family = uk->family;
2022 k->reserved = uk->reserved;
2026 num_migrate = nla_len(rt) / sizeof(*um);
2028 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2031 for (i = 0; i < num_migrate; i++, um++, ma++) {
2032 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2033 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2034 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2035 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2037 ma->proto = um->proto;
2038 ma->mode = um->mode;
2039 ma->reqid = um->reqid;
2041 ma->old_family = um->old_family;
2042 ma->new_family = um->new_family;
2049 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2050 struct nlattr **attrs)
2052 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2053 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2054 struct xfrm_kmaddress km, *kmp;
2059 if (attrs[XFRMA_MIGRATE] == NULL)
2062 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2064 err = copy_from_user_policy_type(&type, attrs);
2068 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2075 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2080 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2081 struct nlattr **attrs)
2083 return -ENOPROTOOPT;
2087 #ifdef CONFIG_XFRM_MIGRATE
2088 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2090 struct xfrm_user_migrate um;
2092 memset(&um, 0, sizeof(um));
2093 um.proto = m->proto;
2095 um.reqid = m->reqid;
2096 um.old_family = m->old_family;
2097 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2098 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2099 um.new_family = m->new_family;
2100 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2101 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2103 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2106 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2108 struct xfrm_user_kmaddress uk;
2110 memset(&uk, 0, sizeof(uk));
2111 uk.family = k->family;
2112 uk.reserved = k->reserved;
2113 memcpy(&uk.local, &k->local, sizeof(uk.local));
2114 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2116 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2119 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2121 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2122 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2123 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2124 + userpolicy_type_attrsize();
2127 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2128 int num_migrate, const struct xfrm_kmaddress *k,
2129 const struct xfrm_selector *sel, u8 dir, u8 type)
2131 const struct xfrm_migrate *mp;
2132 struct xfrm_userpolicy_id *pol_id;
2133 struct nlmsghdr *nlh;
2136 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2140 pol_id = nlmsg_data(nlh);
2141 /* copy data from selector, dir, and type to the pol_id */
2142 memset(pol_id, 0, sizeof(*pol_id));
2143 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2146 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2149 if (copy_to_user_policy_type(type, skb) < 0)
2152 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2153 if (copy_to_user_migrate(mp, skb) < 0)
2157 return nlmsg_end(skb, nlh);
2159 nlmsg_cancel(skb, nlh);
2163 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2164 const struct xfrm_migrate *m, int num_migrate,
2165 const struct xfrm_kmaddress *k)
2167 struct net *net = &init_net;
2168 struct sk_buff *skb;
2170 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2175 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2178 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2181 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2182 const struct xfrm_migrate *m, int num_migrate,
2183 const struct xfrm_kmaddress *k)
2185 return -ENOPROTOOPT;
2189 #define XMSGSIZE(type) sizeof(struct type)
2191 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2192 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2193 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2194 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2195 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2196 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2197 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2198 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2199 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2200 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2201 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2202 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2203 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2204 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2205 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2206 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2207 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2208 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2209 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2210 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2211 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2216 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2217 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2218 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2219 [XFRMA_LASTUSED] = { .type = NLA_U64},
2220 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2221 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2222 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2223 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2224 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2225 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2226 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2227 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2228 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2229 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2230 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2231 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2232 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2233 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2234 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2235 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2236 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2237 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2238 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2239 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2242 static struct xfrm_link {
2243 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2244 int (*dump)(struct sk_buff *, struct netlink_callback *);
2245 int (*done)(struct netlink_callback *);
2246 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2247 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2248 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2249 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2250 .dump = xfrm_dump_sa,
2251 .done = xfrm_dump_sa_done },
2252 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2253 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2254 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2255 .dump = xfrm_dump_policy,
2256 .done = xfrm_dump_policy_done },
2257 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2258 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2259 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2260 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2261 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2262 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2263 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2264 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2265 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2266 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2267 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2268 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2269 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2272 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2274 struct net *net = sock_net(skb->sk);
2275 struct nlattr *attrs[XFRMA_MAX+1];
2276 struct xfrm_link *link;
2279 type = nlh->nlmsg_type;
2280 if (type > XFRM_MSG_MAX)
2283 type -= XFRM_MSG_BASE;
2284 link = &xfrm_dispatch[type];
2286 /* All operations require privileges, even GET */
2287 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2290 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2291 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2292 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2293 if (link->dump == NULL)
2296 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
2299 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2304 if (link->doit == NULL)
2307 return link->doit(skb, nlh, attrs);
2310 static void xfrm_netlink_rcv(struct sk_buff *skb)
2312 mutex_lock(&xfrm_cfg_mutex);
2313 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2314 mutex_unlock(&xfrm_cfg_mutex);
2317 static inline size_t xfrm_expire_msgsize(void)
2319 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2320 + nla_total_size(sizeof(struct xfrm_mark));
2323 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2325 struct xfrm_user_expire *ue;
2326 struct nlmsghdr *nlh;
2328 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2332 ue = nlmsg_data(nlh);
2333 copy_to_user_state(x, &ue->state);
2334 ue->hard = (c->data.hard != 0) ? 1 : 0;
2336 if (xfrm_mark_put(skb, &x->mark))
2337 goto nla_put_failure;
2339 return nlmsg_end(skb, nlh);
2345 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2347 struct net *net = xs_net(x);
2348 struct sk_buff *skb;
2350 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2354 if (build_expire(skb, x, c) < 0) {
2359 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2362 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2364 struct net *net = xs_net(x);
2365 struct sk_buff *skb;
2367 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2371 if (build_aevent(skb, x, c) < 0)
2374 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2377 static int xfrm_notify_sa_flush(const struct km_event *c)
2379 struct net *net = c->net;
2380 struct xfrm_usersa_flush *p;
2381 struct nlmsghdr *nlh;
2382 struct sk_buff *skb;
2383 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2385 skb = nlmsg_new(len, GFP_ATOMIC);
2389 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2395 p = nlmsg_data(nlh);
2396 p->proto = c->data.proto;
2398 nlmsg_end(skb, nlh);
2400 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2403 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2407 l += nla_total_size(aead_len(x->aead));
2409 l += nla_total_size(sizeof(struct xfrm_algo) +
2410 (x->aalg->alg_key_len + 7) / 8);
2411 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2414 l += nla_total_size(xfrm_alg_len(x->ealg));
2416 l += nla_total_size(sizeof(*x->calg));
2418 l += nla_total_size(sizeof(*x->encap));
2420 l += nla_total_size(sizeof(x->tfcpad));
2422 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2424 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2425 x->security->ctx_len);
2427 l += nla_total_size(sizeof(*x->coaddr));
2429 /* Must count x->lastused as it may become non-zero behind our back. */
2430 l += nla_total_size(sizeof(u64));
2435 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2437 struct net *net = xs_net(x);
2438 struct xfrm_usersa_info *p;
2439 struct xfrm_usersa_id *id;
2440 struct nlmsghdr *nlh;
2441 struct sk_buff *skb;
2442 int len = xfrm_sa_len(x);
2445 headlen = sizeof(*p);
2446 if (c->event == XFRM_MSG_DELSA) {
2447 len += nla_total_size(headlen);
2448 headlen = sizeof(*id);
2449 len += nla_total_size(sizeof(struct xfrm_mark));
2451 len += NLMSG_ALIGN(headlen);
2453 skb = nlmsg_new(len, GFP_ATOMIC);
2457 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2459 goto nla_put_failure;
2461 p = nlmsg_data(nlh);
2462 if (c->event == XFRM_MSG_DELSA) {
2463 struct nlattr *attr;
2465 id = nlmsg_data(nlh);
2466 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2467 id->spi = x->id.spi;
2468 id->family = x->props.family;
2469 id->proto = x->id.proto;
2471 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2473 goto nla_put_failure;
2478 if (copy_to_user_state_extra(x, p, skb))
2479 goto nla_put_failure;
2481 nlmsg_end(skb, nlh);
2483 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2486 /* Somebody screwed up with xfrm_sa_len! */
2492 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2496 case XFRM_MSG_EXPIRE:
2497 return xfrm_exp_state_notify(x, c);
2498 case XFRM_MSG_NEWAE:
2499 return xfrm_aevent_state_notify(x, c);
2500 case XFRM_MSG_DELSA:
2501 case XFRM_MSG_UPDSA:
2502 case XFRM_MSG_NEWSA:
2503 return xfrm_notify_sa(x, c);
2504 case XFRM_MSG_FLUSHSA:
2505 return xfrm_notify_sa_flush(c);
2507 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2516 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2517 struct xfrm_policy *xp)
2519 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2520 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2521 + nla_total_size(sizeof(struct xfrm_mark))
2522 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2523 + userpolicy_type_attrsize();
2526 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2527 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2530 struct xfrm_user_acquire *ua;
2531 struct nlmsghdr *nlh;
2532 __u32 seq = xfrm_get_acqseq();
2534 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2538 ua = nlmsg_data(nlh);
2539 memcpy(&ua->id, &x->id, sizeof(ua->id));
2540 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2541 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2542 copy_to_user_policy(xp, &ua->policy, dir);
2543 ua->aalgos = xt->aalgos;
2544 ua->ealgos = xt->ealgos;
2545 ua->calgos = xt->calgos;
2546 ua->seq = x->km.seq = seq;
2548 if (copy_to_user_tmpl(xp, skb) < 0)
2550 if (copy_to_user_state_sec_ctx(x, skb))
2552 if (copy_to_user_policy_type(xp->type, skb) < 0)
2554 if (xfrm_mark_put(skb, &xp->mark))
2555 goto nla_put_failure;
2557 return nlmsg_end(skb, nlh);
2561 nlmsg_cancel(skb, nlh);
2565 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2566 struct xfrm_policy *xp, int dir)
2568 struct net *net = xs_net(x);
2569 struct sk_buff *skb;
2571 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2575 if (build_acquire(skb, x, xt, xp, dir) < 0)
2578 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2581 /* User gives us xfrm_user_policy_info followed by an array of 0
2582 * or more templates.
2584 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2585 u8 *data, int len, int *dir)
2587 struct net *net = sock_net(sk);
2588 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2589 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2590 struct xfrm_policy *xp;
2593 switch (sk->sk_family) {
2595 if (opt != IP_XFRM_POLICY) {
2600 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2602 if (opt != IPV6_XFRM_POLICY) {
2615 if (len < sizeof(*p) ||
2616 verify_newpolicy_info(p))
2619 nr = ((len - sizeof(*p)) / sizeof(*ut));
2620 if (validate_tmpl(nr, ut, p->sel.family))
2623 if (p->dir > XFRM_POLICY_OUT)
2626 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2632 copy_from_user_policy(xp, p);
2633 xp->type = XFRM_POLICY_TYPE_MAIN;
2634 copy_templates(xp, ut, nr);
2641 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2643 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2644 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2645 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2646 + nla_total_size(sizeof(struct xfrm_mark))
2647 + userpolicy_type_attrsize();
2650 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2651 int dir, const struct km_event *c)
2653 struct xfrm_user_polexpire *upe;
2654 struct nlmsghdr *nlh;
2655 int hard = c->data.hard;
2657 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2661 upe = nlmsg_data(nlh);
2662 copy_to_user_policy(xp, &upe->pol, dir);
2663 if (copy_to_user_tmpl(xp, skb) < 0)
2665 if (copy_to_user_sec_ctx(xp, skb))
2667 if (copy_to_user_policy_type(xp->type, skb) < 0)
2669 if (xfrm_mark_put(skb, &xp->mark))
2670 goto nla_put_failure;
2673 return nlmsg_end(skb, nlh);
2677 nlmsg_cancel(skb, nlh);
2681 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2683 struct net *net = xp_net(xp);
2684 struct sk_buff *skb;
2686 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2690 if (build_polexpire(skb, xp, dir, c) < 0)
2693 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2696 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2698 struct net *net = xp_net(xp);
2699 struct xfrm_userpolicy_info *p;
2700 struct xfrm_userpolicy_id *id;
2701 struct nlmsghdr *nlh;
2702 struct sk_buff *skb;
2703 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2706 headlen = sizeof(*p);
2707 if (c->event == XFRM_MSG_DELPOLICY) {
2708 len += nla_total_size(headlen);
2709 headlen = sizeof(*id);
2711 len += userpolicy_type_attrsize();
2712 len += nla_total_size(sizeof(struct xfrm_mark));
2713 len += NLMSG_ALIGN(headlen);
2715 skb = nlmsg_new(len, GFP_ATOMIC);
2719 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2723 p = nlmsg_data(nlh);
2724 if (c->event == XFRM_MSG_DELPOLICY) {
2725 struct nlattr *attr;
2727 id = nlmsg_data(nlh);
2728 memset(id, 0, sizeof(*id));
2731 id->index = xp->index;
2733 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2735 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2742 copy_to_user_policy(xp, p, dir);
2743 if (copy_to_user_tmpl(xp, skb) < 0)
2745 if (copy_to_user_policy_type(xp->type, skb) < 0)
2748 if (xfrm_mark_put(skb, &xp->mark))
2749 goto nla_put_failure;
2751 nlmsg_end(skb, nlh);
2753 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2761 static int xfrm_notify_policy_flush(const struct km_event *c)
2763 struct net *net = c->net;
2764 struct nlmsghdr *nlh;
2765 struct sk_buff *skb;
2767 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2771 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2774 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2777 nlmsg_end(skb, nlh);
2779 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2786 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2790 case XFRM_MSG_NEWPOLICY:
2791 case XFRM_MSG_UPDPOLICY:
2792 case XFRM_MSG_DELPOLICY:
2793 return xfrm_notify_policy(xp, dir, c);
2794 case XFRM_MSG_FLUSHPOLICY:
2795 return xfrm_notify_policy_flush(c);
2796 case XFRM_MSG_POLEXPIRE:
2797 return xfrm_exp_policy_notify(xp, dir, c);
2799 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2807 static inline size_t xfrm_report_msgsize(void)
2809 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2812 static int build_report(struct sk_buff *skb, u8 proto,
2813 struct xfrm_selector *sel, xfrm_address_t *addr)
2815 struct xfrm_user_report *ur;
2816 struct nlmsghdr *nlh;
2818 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2822 ur = nlmsg_data(nlh);
2824 memcpy(&ur->sel, sel, sizeof(ur->sel));
2827 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2829 return nlmsg_end(skb, nlh);
2832 nlmsg_cancel(skb, nlh);
2836 static int xfrm_send_report(struct net *net, u8 proto,
2837 struct xfrm_selector *sel, xfrm_address_t *addr)
2839 struct sk_buff *skb;
2841 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2845 if (build_report(skb, proto, sel, addr) < 0)
2848 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2851 static inline size_t xfrm_mapping_msgsize(void)
2853 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2856 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2857 xfrm_address_t *new_saddr, __be16 new_sport)
2859 struct xfrm_user_mapping *um;
2860 struct nlmsghdr *nlh;
2862 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2866 um = nlmsg_data(nlh);
2868 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2869 um->id.spi = x->id.spi;
2870 um->id.family = x->props.family;
2871 um->id.proto = x->id.proto;
2872 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2873 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2874 um->new_sport = new_sport;
2875 um->old_sport = x->encap->encap_sport;
2876 um->reqid = x->props.reqid;
2878 return nlmsg_end(skb, nlh);
2881 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2884 struct net *net = xs_net(x);
2885 struct sk_buff *skb;
2887 if (x->id.proto != IPPROTO_ESP)
2893 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2897 if (build_mapping(skb, x, ipaddr, sport) < 0)
2900 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2903 static struct xfrm_mgr netlink_mgr = {
2905 .notify = xfrm_send_state_notify,
2906 .acquire = xfrm_send_acquire,
2907 .compile_policy = xfrm_compile_policy,
2908 .notify_policy = xfrm_send_policy_notify,
2909 .report = xfrm_send_report,
2910 .migrate = xfrm_send_migrate,
2911 .new_mapping = xfrm_send_mapping,
2914 static int __net_init xfrm_user_net_init(struct net *net)
2918 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2919 xfrm_netlink_rcv, NULL, THIS_MODULE);
2922 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2923 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2927 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2930 list_for_each_entry(net, net_exit_list, exit_list)
2931 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2933 list_for_each_entry(net, net_exit_list, exit_list)
2934 netlink_kernel_release(net->xfrm.nlsk_stash);
2937 static struct pernet_operations xfrm_user_net_ops = {
2938 .init = xfrm_user_net_init,
2939 .exit_batch = xfrm_user_net_exit,
2942 static int __init xfrm_user_init(void)
2946 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2948 rv = register_pernet_subsys(&xfrm_user_net_ops);
2951 rv = xfrm_register_km(&netlink_mgr);
2953 unregister_pernet_subsys(&xfrm_user_net_ops);
2957 static void __exit xfrm_user_exit(void)
2959 xfrm_unregister_km(&netlink_mgr);
2960 unregister_pernet_subsys(&xfrm_user_net_ops);
2963 module_init(xfrm_user_init);
2964 module_exit(xfrm_user_exit);
2965 MODULE_LICENSE("GPL");
2966 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);