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];
127 if ((p->flags & XFRM_STATE_ESN) && !rt)
133 if (p->id.proto != IPPROTO_ESP)
136 if (p->replay_window != 0)
142 static int verify_newsa_info(struct xfrm_usersa_info *p,
143 struct nlattr **attrs)
153 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
165 switch (p->id.proto) {
167 if ((!attrs[XFRMA_ALG_AUTH] &&
168 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
169 attrs[XFRMA_ALG_AEAD] ||
170 attrs[XFRMA_ALG_CRYPT] ||
171 attrs[XFRMA_ALG_COMP] ||
177 if (attrs[XFRMA_ALG_COMP])
179 if (!attrs[XFRMA_ALG_AUTH] &&
180 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
181 !attrs[XFRMA_ALG_CRYPT] &&
182 !attrs[XFRMA_ALG_AEAD])
184 if ((attrs[XFRMA_ALG_AUTH] ||
185 attrs[XFRMA_ALG_AUTH_TRUNC] ||
186 attrs[XFRMA_ALG_CRYPT]) &&
187 attrs[XFRMA_ALG_AEAD])
189 if (attrs[XFRMA_TFCPAD] &&
190 p->mode != XFRM_MODE_TUNNEL)
195 if (!attrs[XFRMA_ALG_COMP] ||
196 attrs[XFRMA_ALG_AEAD] ||
197 attrs[XFRMA_ALG_AUTH] ||
198 attrs[XFRMA_ALG_AUTH_TRUNC] ||
199 attrs[XFRMA_ALG_CRYPT] ||
204 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
205 case IPPROTO_DSTOPTS:
206 case IPPROTO_ROUTING:
207 if (attrs[XFRMA_ALG_COMP] ||
208 attrs[XFRMA_ALG_AUTH] ||
209 attrs[XFRMA_ALG_AUTH_TRUNC] ||
210 attrs[XFRMA_ALG_AEAD] ||
211 attrs[XFRMA_ALG_CRYPT] ||
212 attrs[XFRMA_ENCAP] ||
213 attrs[XFRMA_SEC_CTX] ||
214 attrs[XFRMA_TFCPAD] ||
215 !attrs[XFRMA_COADDR])
224 if ((err = verify_aead(attrs)))
226 if ((err = verify_auth_trunc(attrs)))
228 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
230 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
232 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
234 if ((err = verify_sec_ctx_len(attrs)))
236 if ((err = verify_replay(p, attrs)))
241 case XFRM_MODE_TRANSPORT:
242 case XFRM_MODE_TUNNEL:
243 case XFRM_MODE_ROUTEOPTIMIZATION:
257 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
258 struct xfrm_algo_desc *(*get_byname)(const char *, int),
261 struct xfrm_algo *p, *ualg;
262 struct xfrm_algo_desc *algo;
267 ualg = nla_data(rta);
269 algo = get_byname(ualg->alg_name, 1);
272 *props = algo->desc.sadb_alg_id;
274 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
278 strcpy(p->alg_name, algo->name);
283 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
286 struct xfrm_algo *ualg;
287 struct xfrm_algo_auth *p;
288 struct xfrm_algo_desc *algo;
293 ualg = nla_data(rta);
295 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
298 *props = algo->desc.sadb_alg_id;
300 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
304 strcpy(p->alg_name, algo->name);
305 p->alg_key_len = ualg->alg_key_len;
306 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
307 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
313 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
316 struct xfrm_algo_auth *p, *ualg;
317 struct xfrm_algo_desc *algo;
322 ualg = nla_data(rta);
324 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
327 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
328 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
330 *props = algo->desc.sadb_alg_id;
332 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
336 strcpy(p->alg_name, algo->name);
337 if (!p->alg_trunc_len)
338 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
344 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
347 struct xfrm_algo_aead *p, *ualg;
348 struct xfrm_algo_desc *algo;
353 ualg = nla_data(rta);
355 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
358 *props = algo->desc.sadb_alg_id;
360 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
364 strcpy(p->alg_name, algo->name);
369 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
372 struct xfrm_replay_state_esn *up;
374 if (!replay_esn || !rp)
379 if (xfrm_replay_state_esn_len(replay_esn) !=
380 xfrm_replay_state_esn_len(up))
386 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
387 struct xfrm_replay_state_esn **preplay_esn,
390 struct xfrm_replay_state_esn *p, *pp, *up;
397 p = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
401 pp = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
413 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
418 len += sizeof(struct xfrm_user_sec_ctx);
419 len += xfrm_ctx->ctx_len;
424 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
426 memcpy(&x->id, &p->id, sizeof(x->id));
427 memcpy(&x->sel, &p->sel, sizeof(x->sel));
428 memcpy(&x->lft, &p->lft, sizeof(x->lft));
429 x->props.mode = p->mode;
430 x->props.replay_window = p->replay_window;
431 x->props.reqid = p->reqid;
432 x->props.family = p->family;
433 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
434 x->props.flags = p->flags;
436 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
437 x->sel.family = p->family;
441 * someday when pfkey also has support, we could have the code
442 * somehow made shareable and move it to xfrm_state.c - JHS
445 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
447 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
448 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
449 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
450 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
451 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
454 struct xfrm_replay_state_esn *replay_esn;
455 replay_esn = nla_data(re);
456 memcpy(x->replay_esn, replay_esn,
457 xfrm_replay_state_esn_len(replay_esn));
458 memcpy(x->preplay_esn, replay_esn,
459 xfrm_replay_state_esn_len(replay_esn));
463 struct xfrm_replay_state *replay;
464 replay = nla_data(rp);
465 memcpy(&x->replay, replay, sizeof(*replay));
466 memcpy(&x->preplay, replay, sizeof(*replay));
470 struct xfrm_lifetime_cur *ltime;
471 ltime = nla_data(lt);
472 x->curlft.bytes = ltime->bytes;
473 x->curlft.packets = ltime->packets;
474 x->curlft.add_time = ltime->add_time;
475 x->curlft.use_time = ltime->use_time;
479 x->replay_maxage = nla_get_u32(et);
482 x->replay_maxdiff = nla_get_u32(rt);
485 static struct xfrm_state *xfrm_state_construct(struct net *net,
486 struct xfrm_usersa_info *p,
487 struct nlattr **attrs,
490 struct xfrm_state *x = xfrm_state_alloc(net);
496 copy_from_user_state(x, p);
498 if ((err = attach_aead(&x->aead, &x->props.ealgo,
499 attrs[XFRMA_ALG_AEAD])))
501 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
502 attrs[XFRMA_ALG_AUTH_TRUNC])))
504 if (!x->props.aalgo) {
505 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
506 attrs[XFRMA_ALG_AUTH])))
509 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
510 xfrm_ealg_get_byname,
511 attrs[XFRMA_ALG_CRYPT])))
513 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
514 xfrm_calg_get_byname,
515 attrs[XFRMA_ALG_COMP])))
518 if (attrs[XFRMA_ENCAP]) {
519 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
520 sizeof(*x->encap), GFP_KERNEL);
521 if (x->encap == NULL)
525 if (attrs[XFRMA_TFCPAD])
526 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
528 if (attrs[XFRMA_COADDR]) {
529 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
530 sizeof(*x->coaddr), GFP_KERNEL);
531 if (x->coaddr == NULL)
535 xfrm_mark_get(attrs, &x->mark);
537 err = __xfrm_init_state(x, false);
541 if (attrs[XFRMA_SEC_CTX] &&
542 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
545 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
546 attrs[XFRMA_REPLAY_ESN_VAL])))
550 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
551 /* sysctl_xfrm_aevent_etime is in 100ms units */
552 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
554 if ((err = xfrm_init_replay(x)))
557 /* override default values from above */
558 xfrm_update_ae_params(x, attrs);
563 x->km.state = XFRM_STATE_DEAD;
570 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
571 struct nlattr **attrs)
573 struct net *net = sock_net(skb->sk);
574 struct xfrm_usersa_info *p = nlmsg_data(nlh);
575 struct xfrm_state *x;
578 uid_t loginuid = audit_get_loginuid(current);
579 u32 sessionid = audit_get_sessionid(current);
582 err = verify_newsa_info(p, attrs);
586 x = xfrm_state_construct(net, p, attrs, &err);
591 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
592 err = xfrm_state_add(x);
594 err = xfrm_state_update(x);
596 security_task_getsecid(current, &sid);
597 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
600 x->km.state = XFRM_STATE_DEAD;
605 c.seq = nlh->nlmsg_seq;
606 c.pid = nlh->nlmsg_pid;
607 c.event = nlh->nlmsg_type;
609 km_state_notify(x, &c);
615 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
616 struct xfrm_usersa_id *p,
617 struct nlattr **attrs,
620 struct xfrm_state *x = NULL;
623 u32 mark = xfrm_mark_get(attrs, &m);
625 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
627 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
629 xfrm_address_t *saddr = NULL;
631 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
638 x = xfrm_state_lookup_byaddr(net, mark,
640 p->proto, p->family);
649 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
650 struct nlattr **attrs)
652 struct net *net = sock_net(skb->sk);
653 struct xfrm_state *x;
656 struct xfrm_usersa_id *p = nlmsg_data(nlh);
657 uid_t loginuid = audit_get_loginuid(current);
658 u32 sessionid = audit_get_sessionid(current);
661 x = xfrm_user_state_lookup(net, p, attrs, &err);
665 if ((err = security_xfrm_state_delete(x)) != 0)
668 if (xfrm_state_kern(x)) {
673 err = xfrm_state_delete(x);
678 c.seq = nlh->nlmsg_seq;
679 c.pid = nlh->nlmsg_pid;
680 c.event = nlh->nlmsg_type;
681 km_state_notify(x, &c);
684 security_task_getsecid(current, &sid);
685 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
690 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
692 memset(p, 0, sizeof(*p));
693 memcpy(&p->id, &x->id, sizeof(p->id));
694 memcpy(&p->sel, &x->sel, sizeof(p->sel));
695 memcpy(&p->lft, &x->lft, sizeof(p->lft));
696 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
697 memcpy(&p->stats, &x->stats, sizeof(p->stats));
698 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
699 p->mode = x->props.mode;
700 p->replay_window = x->props.replay_window;
701 p->reqid = x->props.reqid;
702 p->family = x->props.family;
703 p->flags = x->props.flags;
707 struct xfrm_dump_info {
708 struct sk_buff *in_skb;
709 struct sk_buff *out_skb;
714 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
716 struct xfrm_user_sec_ctx *uctx;
718 int ctx_size = sizeof(*uctx) + s->ctx_len;
720 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
724 uctx = nla_data(attr);
725 uctx->exttype = XFRMA_SEC_CTX;
726 uctx->len = ctx_size;
727 uctx->ctx_doi = s->ctx_doi;
728 uctx->ctx_alg = s->ctx_alg;
729 uctx->ctx_len = s->ctx_len;
730 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
735 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
737 struct xfrm_algo *algo;
740 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
741 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
745 algo = nla_data(nla);
746 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
747 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
748 algo->alg_key_len = auth->alg_key_len;
753 /* Don't change this without updating xfrm_sa_len! */
754 static int copy_to_user_state_extra(struct xfrm_state *x,
755 struct xfrm_usersa_info *p,
758 copy_to_user_state(x, p);
761 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
764 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
767 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
769 if (copy_to_user_auth(x->aalg, skb))
770 goto nla_put_failure;
772 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
773 xfrm_alg_auth_len(x->aalg), x->aalg);
776 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
778 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
781 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
784 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
786 if (xfrm_mark_put(skb, &x->mark))
787 goto nla_put_failure;
790 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
791 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
793 if (x->security && copy_sec_ctx(x->security, skb) < 0)
794 goto nla_put_failure;
802 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
804 struct xfrm_dump_info *sp = ptr;
805 struct sk_buff *in_skb = sp->in_skb;
806 struct sk_buff *skb = sp->out_skb;
807 struct xfrm_usersa_info *p;
808 struct nlmsghdr *nlh;
811 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
812 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
818 err = copy_to_user_state_extra(x, p, skb);
820 goto nla_put_failure;
826 nlmsg_cancel(skb, nlh);
830 static int xfrm_dump_sa_done(struct netlink_callback *cb)
832 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
833 xfrm_state_walk_done(walk);
837 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
839 struct net *net = sock_net(skb->sk);
840 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
841 struct xfrm_dump_info info;
843 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
844 sizeof(cb->args) - sizeof(cb->args[0]));
846 info.in_skb = cb->skb;
848 info.nlmsg_seq = cb->nlh->nlmsg_seq;
849 info.nlmsg_flags = NLM_F_MULTI;
853 xfrm_state_walk_init(walk, 0);
856 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
861 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
862 struct xfrm_state *x, u32 seq)
864 struct xfrm_dump_info info;
868 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
870 return ERR_PTR(-ENOMEM);
872 info.in_skb = in_skb;
874 info.nlmsg_seq = seq;
875 info.nlmsg_flags = 0;
877 err = dump_one_state(x, 0, &info);
886 static inline size_t xfrm_spdinfo_msgsize(void)
888 return NLMSG_ALIGN(4)
889 + nla_total_size(sizeof(struct xfrmu_spdinfo))
890 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
893 static int build_spdinfo(struct sk_buff *skb, struct net *net,
894 u32 pid, u32 seq, u32 flags)
896 struct xfrmk_spdinfo si;
897 struct xfrmu_spdinfo spc;
898 struct xfrmu_spdhinfo sph;
899 struct nlmsghdr *nlh;
902 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
903 if (nlh == NULL) /* shouldn't really happen ... */
908 xfrm_spd_getinfo(net, &si);
909 spc.incnt = si.incnt;
910 spc.outcnt = si.outcnt;
911 spc.fwdcnt = si.fwdcnt;
912 spc.inscnt = si.inscnt;
913 spc.outscnt = si.outscnt;
914 spc.fwdscnt = si.fwdscnt;
915 sph.spdhcnt = si.spdhcnt;
916 sph.spdhmcnt = si.spdhmcnt;
918 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
919 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
921 return nlmsg_end(skb, nlh);
924 nlmsg_cancel(skb, nlh);
928 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
929 struct nlattr **attrs)
931 struct net *net = sock_net(skb->sk);
932 struct sk_buff *r_skb;
933 u32 *flags = nlmsg_data(nlh);
934 u32 spid = NETLINK_CB(skb).pid;
935 u32 seq = nlh->nlmsg_seq;
937 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
941 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
944 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
947 static inline size_t xfrm_sadinfo_msgsize(void)
949 return NLMSG_ALIGN(4)
950 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
951 + nla_total_size(4); /* XFRMA_SAD_CNT */
954 static int build_sadinfo(struct sk_buff *skb, struct net *net,
955 u32 pid, u32 seq, u32 flags)
957 struct xfrmk_sadinfo si;
958 struct xfrmu_sadhinfo sh;
959 struct nlmsghdr *nlh;
962 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
963 if (nlh == NULL) /* shouldn't really happen ... */
968 xfrm_sad_getinfo(net, &si);
970 sh.sadhmcnt = si.sadhmcnt;
971 sh.sadhcnt = si.sadhcnt;
973 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
974 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
976 return nlmsg_end(skb, nlh);
979 nlmsg_cancel(skb, nlh);
983 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
984 struct nlattr **attrs)
986 struct net *net = sock_net(skb->sk);
987 struct sk_buff *r_skb;
988 u32 *flags = nlmsg_data(nlh);
989 u32 spid = NETLINK_CB(skb).pid;
990 u32 seq = nlh->nlmsg_seq;
992 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
996 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
999 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
1002 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1003 struct nlattr **attrs)
1005 struct net *net = sock_net(skb->sk);
1006 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1007 struct xfrm_state *x;
1008 struct sk_buff *resp_skb;
1011 x = xfrm_user_state_lookup(net, p, attrs, &err);
1015 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1016 if (IS_ERR(resp_skb)) {
1017 err = PTR_ERR(resp_skb);
1019 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1026 static int verify_userspi_info(struct xfrm_userspi_info *p)
1028 switch (p->info.id.proto) {
1034 /* IPCOMP spi is 16-bits. */
1035 if (p->max >= 0x10000)
1043 if (p->min > p->max)
1049 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1050 struct nlattr **attrs)
1052 struct net *net = sock_net(skb->sk);
1053 struct xfrm_state *x;
1054 struct xfrm_userspi_info *p;
1055 struct sk_buff *resp_skb;
1056 xfrm_address_t *daddr;
1062 p = nlmsg_data(nlh);
1063 err = verify_userspi_info(p);
1067 family = p->info.family;
1068 daddr = &p->info.id.daddr;
1072 mark = xfrm_mark_get(attrs, &m);
1074 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1075 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1082 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1083 p->info.id.proto, daddr,
1090 err = xfrm_alloc_spi(x, p->min, p->max);
1094 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1095 if (IS_ERR(resp_skb)) {
1096 err = PTR_ERR(resp_skb);
1100 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1108 static int verify_policy_dir(u8 dir)
1111 case XFRM_POLICY_IN:
1112 case XFRM_POLICY_OUT:
1113 case XFRM_POLICY_FWD:
1123 static int verify_policy_type(u8 type)
1126 case XFRM_POLICY_TYPE_MAIN:
1127 #ifdef CONFIG_XFRM_SUB_POLICY
1128 case XFRM_POLICY_TYPE_SUB:
1139 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1142 case XFRM_SHARE_ANY:
1143 case XFRM_SHARE_SESSION:
1144 case XFRM_SHARE_USER:
1145 case XFRM_SHARE_UNIQUE:
1152 switch (p->action) {
1153 case XFRM_POLICY_ALLOW:
1154 case XFRM_POLICY_BLOCK:
1161 switch (p->sel.family) {
1166 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1169 return -EAFNOSUPPORT;
1176 return verify_policy_dir(p->dir);
1179 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1181 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1182 struct xfrm_user_sec_ctx *uctx;
1187 uctx = nla_data(rt);
1188 return security_xfrm_policy_alloc(&pol->security, uctx);
1191 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1197 for (i = 0; i < nr; i++, ut++) {
1198 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1200 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1201 memcpy(&t->saddr, &ut->saddr,
1202 sizeof(xfrm_address_t));
1203 t->reqid = ut->reqid;
1205 t->share = ut->share;
1206 t->optional = ut->optional;
1207 t->aalgos = ut->aalgos;
1208 t->ealgos = ut->ealgos;
1209 t->calgos = ut->calgos;
1210 /* If all masks are ~0, then we allow all algorithms. */
1211 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1212 t->encap_family = ut->family;
1216 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1220 if (nr > XFRM_MAX_DEPTH)
1223 for (i = 0; i < nr; i++) {
1224 /* We never validated the ut->family value, so many
1225 * applications simply leave it at zero. The check was
1226 * never made and ut->family was ignored because all
1227 * templates could be assumed to have the same family as
1228 * the policy itself. Now that we will have ipv4-in-ipv6
1229 * and ipv6-in-ipv4 tunnels, this is no longer true.
1232 ut[i].family = family;
1234 switch (ut[i].family) {
1237 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1249 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1251 struct nlattr *rt = attrs[XFRMA_TMPL];
1256 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1257 int nr = nla_len(rt) / sizeof(*utmpl);
1260 err = validate_tmpl(nr, utmpl, pol->family);
1264 copy_templates(pol, utmpl, nr);
1269 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1271 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1272 struct xfrm_userpolicy_type *upt;
1273 u8 type = XFRM_POLICY_TYPE_MAIN;
1281 err = verify_policy_type(type);
1289 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1291 xp->priority = p->priority;
1292 xp->index = p->index;
1293 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1294 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1295 xp->action = p->action;
1296 xp->flags = p->flags;
1297 xp->family = p->sel.family;
1298 /* XXX xp->share = p->share; */
1301 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1303 memset(p, 0, sizeof(*p));
1304 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1305 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1306 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1307 p->priority = xp->priority;
1308 p->index = xp->index;
1309 p->sel.family = xp->family;
1311 p->action = xp->action;
1312 p->flags = xp->flags;
1313 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1316 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1318 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1326 copy_from_user_policy(xp, p);
1328 err = copy_from_user_policy_type(&xp->type, attrs);
1332 if (!(err = copy_from_user_tmpl(xp, attrs)))
1333 err = copy_from_user_sec_ctx(xp, attrs);
1337 xfrm_mark_get(attrs, &xp->mark);
1343 xfrm_policy_destroy(xp);
1347 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1348 struct nlattr **attrs)
1350 struct net *net = sock_net(skb->sk);
1351 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1352 struct xfrm_policy *xp;
1356 uid_t loginuid = audit_get_loginuid(current);
1357 u32 sessionid = audit_get_sessionid(current);
1360 err = verify_newpolicy_info(p);
1363 err = verify_sec_ctx_len(attrs);
1367 xp = xfrm_policy_construct(net, p, attrs, &err);
1371 /* shouldn't excl be based on nlh flags??
1372 * Aha! this is anti-netlink really i.e more pfkey derived
1373 * in netlink excl is a flag and you wouldnt need
1374 * a type XFRM_MSG_UPDPOLICY - JHS */
1375 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1376 err = xfrm_policy_insert(p->dir, xp, excl);
1377 security_task_getsecid(current, &sid);
1378 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1381 security_xfrm_policy_free(xp->security);
1386 c.event = nlh->nlmsg_type;
1387 c.seq = nlh->nlmsg_seq;
1388 c.pid = nlh->nlmsg_pid;
1389 km_policy_notify(xp, p->dir, &c);
1396 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1398 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1401 if (xp->xfrm_nr == 0)
1404 for (i = 0; i < xp->xfrm_nr; i++) {
1405 struct xfrm_user_tmpl *up = &vec[i];
1406 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1408 memset(up, 0, sizeof(*up));
1409 memcpy(&up->id, &kp->id, sizeof(up->id));
1410 up->family = kp->encap_family;
1411 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1412 up->reqid = kp->reqid;
1413 up->mode = kp->mode;
1414 up->share = kp->share;
1415 up->optional = kp->optional;
1416 up->aalgos = kp->aalgos;
1417 up->ealgos = kp->ealgos;
1418 up->calgos = kp->calgos;
1421 return nla_put(skb, XFRMA_TMPL,
1422 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1425 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1428 return copy_sec_ctx(x->security, skb);
1433 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1436 return copy_sec_ctx(xp->security, skb);
1440 static inline size_t userpolicy_type_attrsize(void)
1442 #ifdef CONFIG_XFRM_SUB_POLICY
1443 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1449 #ifdef CONFIG_XFRM_SUB_POLICY
1450 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1452 struct xfrm_userpolicy_type upt = {
1456 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1460 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1466 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1468 struct xfrm_dump_info *sp = ptr;
1469 struct xfrm_userpolicy_info *p;
1470 struct sk_buff *in_skb = sp->in_skb;
1471 struct sk_buff *skb = sp->out_skb;
1472 struct nlmsghdr *nlh;
1474 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1475 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1479 p = nlmsg_data(nlh);
1480 copy_to_user_policy(xp, p, dir);
1481 if (copy_to_user_tmpl(xp, skb) < 0)
1483 if (copy_to_user_sec_ctx(xp, skb))
1485 if (copy_to_user_policy_type(xp->type, skb) < 0)
1487 if (xfrm_mark_put(skb, &xp->mark))
1488 goto nla_put_failure;
1490 nlmsg_end(skb, nlh);
1495 nlmsg_cancel(skb, nlh);
1499 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1501 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1503 xfrm_policy_walk_done(walk);
1507 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1509 struct net *net = sock_net(skb->sk);
1510 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1511 struct xfrm_dump_info info;
1513 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1514 sizeof(cb->args) - sizeof(cb->args[0]));
1516 info.in_skb = cb->skb;
1518 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1519 info.nlmsg_flags = NLM_F_MULTI;
1523 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1526 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1531 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1532 struct xfrm_policy *xp,
1535 struct xfrm_dump_info info;
1536 struct sk_buff *skb;
1539 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1541 return ERR_PTR(-ENOMEM);
1543 info.in_skb = in_skb;
1545 info.nlmsg_seq = seq;
1546 info.nlmsg_flags = 0;
1548 err = dump_one_policy(xp, dir, 0, &info);
1551 return ERR_PTR(err);
1557 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1558 struct nlattr **attrs)
1560 struct net *net = sock_net(skb->sk);
1561 struct xfrm_policy *xp;
1562 struct xfrm_userpolicy_id *p;
1563 u8 type = XFRM_POLICY_TYPE_MAIN;
1568 u32 mark = xfrm_mark_get(attrs, &m);
1570 p = nlmsg_data(nlh);
1571 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1573 err = copy_from_user_policy_type(&type, attrs);
1577 err = verify_policy_dir(p->dir);
1582 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1584 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1585 struct xfrm_sec_ctx *ctx;
1587 err = verify_sec_ctx_len(attrs);
1593 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1595 err = security_xfrm_policy_alloc(&ctx, uctx);
1599 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1601 security_xfrm_policy_free(ctx);
1607 struct sk_buff *resp_skb;
1609 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1610 if (IS_ERR(resp_skb)) {
1611 err = PTR_ERR(resp_skb);
1613 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1614 NETLINK_CB(skb).pid);
1617 uid_t loginuid = audit_get_loginuid(current);
1618 u32 sessionid = audit_get_sessionid(current);
1621 security_task_getsecid(current, &sid);
1622 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1628 c.data.byid = p->index;
1629 c.event = nlh->nlmsg_type;
1630 c.seq = nlh->nlmsg_seq;
1631 c.pid = nlh->nlmsg_pid;
1632 km_policy_notify(xp, p->dir, &c);
1640 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1641 struct nlattr **attrs)
1643 struct net *net = sock_net(skb->sk);
1645 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1646 struct xfrm_audit audit_info;
1649 audit_info.loginuid = audit_get_loginuid(current);
1650 audit_info.sessionid = audit_get_sessionid(current);
1651 security_task_getsecid(current, &audit_info.secid);
1652 err = xfrm_state_flush(net, p->proto, &audit_info);
1654 if (err == -ESRCH) /* empty table */
1658 c.data.proto = p->proto;
1659 c.event = nlh->nlmsg_type;
1660 c.seq = nlh->nlmsg_seq;
1661 c.pid = nlh->nlmsg_pid;
1663 km_state_notify(NULL, &c);
1668 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1670 size_t replay_size = x->replay_esn ?
1671 xfrm_replay_state_esn_len(x->replay_esn) :
1672 sizeof(struct xfrm_replay_state);
1674 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1675 + nla_total_size(replay_size)
1676 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1677 + nla_total_size(sizeof(struct xfrm_mark))
1678 + nla_total_size(4) /* XFRM_AE_RTHR */
1679 + nla_total_size(4); /* XFRM_AE_ETHR */
1682 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1684 struct xfrm_aevent_id *id;
1685 struct nlmsghdr *nlh;
1687 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1691 id = nlmsg_data(nlh);
1692 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1693 id->sa_id.spi = x->id.spi;
1694 id->sa_id.family = x->props.family;
1695 id->sa_id.proto = x->id.proto;
1696 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1697 id->reqid = x->props.reqid;
1698 id->flags = c->data.aevent;
1701 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1702 xfrm_replay_state_esn_len(x->replay_esn),
1705 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1707 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1709 if (id->flags & XFRM_AE_RTHR)
1710 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1712 if (id->flags & XFRM_AE_ETHR)
1713 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1714 x->replay_maxage * 10 / HZ);
1716 if (xfrm_mark_put(skb, &x->mark))
1717 goto nla_put_failure;
1719 return nlmsg_end(skb, nlh);
1722 nlmsg_cancel(skb, nlh);
1726 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1727 struct nlattr **attrs)
1729 struct net *net = sock_net(skb->sk);
1730 struct xfrm_state *x;
1731 struct sk_buff *r_skb;
1736 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1737 struct xfrm_usersa_id *id = &p->sa_id;
1739 mark = xfrm_mark_get(attrs, &m);
1741 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1745 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1746 if (r_skb == NULL) {
1752 * XXX: is this lock really needed - none of the other
1753 * gets lock (the concern is things getting updated
1754 * while we are still reading) - jhs
1756 spin_lock_bh(&x->lock);
1757 c.data.aevent = p->flags;
1758 c.seq = nlh->nlmsg_seq;
1759 c.pid = nlh->nlmsg_pid;
1761 if (build_aevent(r_skb, x, &c) < 0)
1763 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1764 spin_unlock_bh(&x->lock);
1769 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1770 struct nlattr **attrs)
1772 struct net *net = sock_net(skb->sk);
1773 struct xfrm_state *x;
1778 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1779 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1780 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1781 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1783 if (!lt && !rp && !re)
1786 /* pedantic mode - thou shalt sayeth replaceth */
1787 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1790 mark = xfrm_mark_get(attrs, &m);
1792 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1796 if (x->km.state != XFRM_STATE_VALID)
1799 err = xfrm_replay_verify_len(x->replay_esn, rp);
1803 spin_lock_bh(&x->lock);
1804 xfrm_update_ae_params(x, attrs);
1805 spin_unlock_bh(&x->lock);
1807 c.event = nlh->nlmsg_type;
1808 c.seq = nlh->nlmsg_seq;
1809 c.pid = nlh->nlmsg_pid;
1810 c.data.aevent = XFRM_AE_CU;
1811 km_state_notify(x, &c);
1818 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1819 struct nlattr **attrs)
1821 struct net *net = sock_net(skb->sk);
1823 u8 type = XFRM_POLICY_TYPE_MAIN;
1825 struct xfrm_audit audit_info;
1827 err = copy_from_user_policy_type(&type, attrs);
1831 audit_info.loginuid = audit_get_loginuid(current);
1832 audit_info.sessionid = audit_get_sessionid(current);
1833 security_task_getsecid(current, &audit_info.secid);
1834 err = xfrm_policy_flush(net, type, &audit_info);
1836 if (err == -ESRCH) /* empty table */
1842 c.event = nlh->nlmsg_type;
1843 c.seq = nlh->nlmsg_seq;
1844 c.pid = nlh->nlmsg_pid;
1846 km_policy_notify(NULL, 0, &c);
1850 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1851 struct nlattr **attrs)
1853 struct net *net = sock_net(skb->sk);
1854 struct xfrm_policy *xp;
1855 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1856 struct xfrm_userpolicy_info *p = &up->pol;
1857 u8 type = XFRM_POLICY_TYPE_MAIN;
1860 u32 mark = xfrm_mark_get(attrs, &m);
1862 err = copy_from_user_policy_type(&type, attrs);
1866 err = verify_policy_dir(p->dir);
1871 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1873 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1874 struct xfrm_sec_ctx *ctx;
1876 err = verify_sec_ctx_len(attrs);
1882 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1884 err = security_xfrm_policy_alloc(&ctx, uctx);
1888 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1889 &p->sel, ctx, 0, &err);
1890 security_xfrm_policy_free(ctx);
1895 if (unlikely(xp->walk.dead))
1900 uid_t loginuid = audit_get_loginuid(current);
1901 u32 sessionid = audit_get_sessionid(current);
1904 security_task_getsecid(current, &sid);
1905 xfrm_policy_delete(xp, p->dir);
1906 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1909 // reset the timers here?
1910 WARN(1, "Dont know what to do with soft policy expire\n");
1912 km_policy_expired(xp, p->dir, up->hard, current->pid);
1919 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1920 struct nlattr **attrs)
1922 struct net *net = sock_net(skb->sk);
1923 struct xfrm_state *x;
1925 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1926 struct xfrm_usersa_info *p = &ue->state;
1928 u32 mark = xfrm_mark_get(attrs, &m);
1930 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1936 spin_lock_bh(&x->lock);
1938 if (x->km.state != XFRM_STATE_VALID)
1940 km_state_expired(x, ue->hard, current->pid);
1943 uid_t loginuid = audit_get_loginuid(current);
1944 u32 sessionid = audit_get_sessionid(current);
1947 security_task_getsecid(current, &sid);
1948 __xfrm_state_delete(x);
1949 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1953 spin_unlock_bh(&x->lock);
1958 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1959 struct nlattr **attrs)
1961 struct net *net = sock_net(skb->sk);
1962 struct xfrm_policy *xp;
1963 struct xfrm_user_tmpl *ut;
1965 struct nlattr *rt = attrs[XFRMA_TMPL];
1966 struct xfrm_mark mark;
1968 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1969 struct xfrm_state *x = xfrm_state_alloc(net);
1975 xfrm_mark_get(attrs, &mark);
1977 err = verify_newpolicy_info(&ua->policy);
1982 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1986 memcpy(&x->id, &ua->id, sizeof(ua->id));
1987 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1988 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1989 xp->mark.m = x->mark.m = mark.m;
1990 xp->mark.v = x->mark.v = mark.v;
1992 /* extract the templates and for each call km_key */
1993 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1994 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1995 memcpy(&x->id, &t->id, sizeof(x->id));
1996 x->props.mode = t->mode;
1997 x->props.reqid = t->reqid;
1998 x->props.family = ut->family;
1999 t->aalgos = ua->aalgos;
2000 t->ealgos = ua->ealgos;
2001 t->calgos = ua->calgos;
2002 err = km_query(x, t, xp);
2012 WARN(1, "BAD policy passed\n");
2019 #ifdef CONFIG_XFRM_MIGRATE
2020 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2021 struct xfrm_kmaddress *k,
2022 struct nlattr **attrs, int *num)
2024 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2025 struct xfrm_user_migrate *um;
2029 struct xfrm_user_kmaddress *uk;
2031 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2032 memcpy(&k->local, &uk->local, sizeof(k->local));
2033 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2034 k->family = uk->family;
2035 k->reserved = uk->reserved;
2039 num_migrate = nla_len(rt) / sizeof(*um);
2041 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2044 for (i = 0; i < num_migrate; i++, um++, ma++) {
2045 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2046 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2047 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2048 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2050 ma->proto = um->proto;
2051 ma->mode = um->mode;
2052 ma->reqid = um->reqid;
2054 ma->old_family = um->old_family;
2055 ma->new_family = um->new_family;
2062 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2063 struct nlattr **attrs)
2065 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2066 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2067 struct xfrm_kmaddress km, *kmp;
2072 if (attrs[XFRMA_MIGRATE] == NULL)
2075 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2077 err = copy_from_user_policy_type(&type, attrs);
2081 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2088 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2093 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2094 struct nlattr **attrs)
2096 return -ENOPROTOOPT;
2100 #ifdef CONFIG_XFRM_MIGRATE
2101 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2103 struct xfrm_user_migrate um;
2105 memset(&um, 0, sizeof(um));
2106 um.proto = m->proto;
2108 um.reqid = m->reqid;
2109 um.old_family = m->old_family;
2110 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2111 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2112 um.new_family = m->new_family;
2113 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2114 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2116 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2119 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2121 struct xfrm_user_kmaddress uk;
2123 memset(&uk, 0, sizeof(uk));
2124 uk.family = k->family;
2125 uk.reserved = k->reserved;
2126 memcpy(&uk.local, &k->local, sizeof(uk.local));
2127 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2129 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2132 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2134 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2135 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2136 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2137 + userpolicy_type_attrsize();
2140 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2141 int num_migrate, const struct xfrm_kmaddress *k,
2142 const struct xfrm_selector *sel, u8 dir, u8 type)
2144 const struct xfrm_migrate *mp;
2145 struct xfrm_userpolicy_id *pol_id;
2146 struct nlmsghdr *nlh;
2149 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2153 pol_id = nlmsg_data(nlh);
2154 /* copy data from selector, dir, and type to the pol_id */
2155 memset(pol_id, 0, sizeof(*pol_id));
2156 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2159 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2162 if (copy_to_user_policy_type(type, skb) < 0)
2165 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2166 if (copy_to_user_migrate(mp, skb) < 0)
2170 return nlmsg_end(skb, nlh);
2172 nlmsg_cancel(skb, nlh);
2176 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2177 const struct xfrm_migrate *m, int num_migrate,
2178 const struct xfrm_kmaddress *k)
2180 struct net *net = &init_net;
2181 struct sk_buff *skb;
2183 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2188 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2191 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2194 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2195 const struct xfrm_migrate *m, int num_migrate,
2196 const struct xfrm_kmaddress *k)
2198 return -ENOPROTOOPT;
2202 #define XMSGSIZE(type) sizeof(struct type)
2204 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2205 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2206 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2207 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2208 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2209 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2210 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2211 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2212 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2213 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2214 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2215 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2216 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2217 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2218 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2219 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2220 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2221 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2222 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2223 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2224 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2229 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2230 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2231 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2232 [XFRMA_LASTUSED] = { .type = NLA_U64},
2233 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2234 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2235 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2236 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2237 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2238 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2239 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2240 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2241 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2242 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2243 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2244 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2245 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2246 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2247 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2248 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2249 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2250 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2251 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2252 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2255 static struct xfrm_link {
2256 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2257 int (*dump)(struct sk_buff *, struct netlink_callback *);
2258 int (*done)(struct netlink_callback *);
2259 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2260 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2261 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2262 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2263 .dump = xfrm_dump_sa,
2264 .done = xfrm_dump_sa_done },
2265 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2266 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2267 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2268 .dump = xfrm_dump_policy,
2269 .done = xfrm_dump_policy_done },
2270 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2271 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2272 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2273 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2274 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2275 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2276 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2277 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2278 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2279 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2280 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2281 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2282 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2285 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2287 struct net *net = sock_net(skb->sk);
2288 struct nlattr *attrs[XFRMA_MAX+1];
2289 struct xfrm_link *link;
2292 type = nlh->nlmsg_type;
2293 if (type > XFRM_MSG_MAX)
2296 type -= XFRM_MSG_BASE;
2297 link = &xfrm_dispatch[type];
2299 /* All operations require privileges, even GET */
2300 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2303 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2304 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2305 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2306 if (link->dump == NULL)
2309 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
2312 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2317 if (link->doit == NULL)
2320 return link->doit(skb, nlh, attrs);
2323 static void xfrm_netlink_rcv(struct sk_buff *skb)
2325 mutex_lock(&xfrm_cfg_mutex);
2326 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2327 mutex_unlock(&xfrm_cfg_mutex);
2330 static inline size_t xfrm_expire_msgsize(void)
2332 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2333 + nla_total_size(sizeof(struct xfrm_mark));
2336 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2338 struct xfrm_user_expire *ue;
2339 struct nlmsghdr *nlh;
2341 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2345 ue = nlmsg_data(nlh);
2346 copy_to_user_state(x, &ue->state);
2347 ue->hard = (c->data.hard != 0) ? 1 : 0;
2349 if (xfrm_mark_put(skb, &x->mark))
2350 goto nla_put_failure;
2352 return nlmsg_end(skb, nlh);
2358 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2360 struct net *net = xs_net(x);
2361 struct sk_buff *skb;
2363 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2367 if (build_expire(skb, x, c) < 0) {
2372 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2375 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2377 struct net *net = xs_net(x);
2378 struct sk_buff *skb;
2380 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2384 if (build_aevent(skb, x, c) < 0)
2387 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2390 static int xfrm_notify_sa_flush(const struct km_event *c)
2392 struct net *net = c->net;
2393 struct xfrm_usersa_flush *p;
2394 struct nlmsghdr *nlh;
2395 struct sk_buff *skb;
2396 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2398 skb = nlmsg_new(len, GFP_ATOMIC);
2402 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2408 p = nlmsg_data(nlh);
2409 p->proto = c->data.proto;
2411 nlmsg_end(skb, nlh);
2413 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2416 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2420 l += nla_total_size(aead_len(x->aead));
2422 l += nla_total_size(sizeof(struct xfrm_algo) +
2423 (x->aalg->alg_key_len + 7) / 8);
2424 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2427 l += nla_total_size(xfrm_alg_len(x->ealg));
2429 l += nla_total_size(sizeof(*x->calg));
2431 l += nla_total_size(sizeof(*x->encap));
2433 l += nla_total_size(sizeof(x->tfcpad));
2435 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2437 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2438 x->security->ctx_len);
2440 l += nla_total_size(sizeof(*x->coaddr));
2442 /* Must count x->lastused as it may become non-zero behind our back. */
2443 l += nla_total_size(sizeof(u64));
2448 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2450 struct net *net = xs_net(x);
2451 struct xfrm_usersa_info *p;
2452 struct xfrm_usersa_id *id;
2453 struct nlmsghdr *nlh;
2454 struct sk_buff *skb;
2455 int len = xfrm_sa_len(x);
2458 headlen = sizeof(*p);
2459 if (c->event == XFRM_MSG_DELSA) {
2460 len += nla_total_size(headlen);
2461 headlen = sizeof(*id);
2462 len += nla_total_size(sizeof(struct xfrm_mark));
2464 len += NLMSG_ALIGN(headlen);
2466 skb = nlmsg_new(len, GFP_ATOMIC);
2470 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2472 goto nla_put_failure;
2474 p = nlmsg_data(nlh);
2475 if (c->event == XFRM_MSG_DELSA) {
2476 struct nlattr *attr;
2478 id = nlmsg_data(nlh);
2479 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2480 id->spi = x->id.spi;
2481 id->family = x->props.family;
2482 id->proto = x->id.proto;
2484 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2486 goto nla_put_failure;
2491 if (copy_to_user_state_extra(x, p, skb))
2492 goto nla_put_failure;
2494 nlmsg_end(skb, nlh);
2496 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2499 /* Somebody screwed up with xfrm_sa_len! */
2505 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2509 case XFRM_MSG_EXPIRE:
2510 return xfrm_exp_state_notify(x, c);
2511 case XFRM_MSG_NEWAE:
2512 return xfrm_aevent_state_notify(x, c);
2513 case XFRM_MSG_DELSA:
2514 case XFRM_MSG_UPDSA:
2515 case XFRM_MSG_NEWSA:
2516 return xfrm_notify_sa(x, c);
2517 case XFRM_MSG_FLUSHSA:
2518 return xfrm_notify_sa_flush(c);
2520 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2529 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2530 struct xfrm_policy *xp)
2532 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2533 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2534 + nla_total_size(sizeof(struct xfrm_mark))
2535 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2536 + userpolicy_type_attrsize();
2539 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2540 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2543 struct xfrm_user_acquire *ua;
2544 struct nlmsghdr *nlh;
2545 __u32 seq = xfrm_get_acqseq();
2547 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2551 ua = nlmsg_data(nlh);
2552 memcpy(&ua->id, &x->id, sizeof(ua->id));
2553 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2554 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2555 copy_to_user_policy(xp, &ua->policy, dir);
2556 ua->aalgos = xt->aalgos;
2557 ua->ealgos = xt->ealgos;
2558 ua->calgos = xt->calgos;
2559 ua->seq = x->km.seq = seq;
2561 if (copy_to_user_tmpl(xp, skb) < 0)
2563 if (copy_to_user_state_sec_ctx(x, skb))
2565 if (copy_to_user_policy_type(xp->type, skb) < 0)
2567 if (xfrm_mark_put(skb, &xp->mark))
2568 goto nla_put_failure;
2570 return nlmsg_end(skb, nlh);
2574 nlmsg_cancel(skb, nlh);
2578 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2579 struct xfrm_policy *xp, int dir)
2581 struct net *net = xs_net(x);
2582 struct sk_buff *skb;
2584 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2588 if (build_acquire(skb, x, xt, xp, dir) < 0)
2591 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2594 /* User gives us xfrm_user_policy_info followed by an array of 0
2595 * or more templates.
2597 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2598 u8 *data, int len, int *dir)
2600 struct net *net = sock_net(sk);
2601 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2602 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2603 struct xfrm_policy *xp;
2606 switch (sk->sk_family) {
2608 if (opt != IP_XFRM_POLICY) {
2613 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2615 if (opt != IPV6_XFRM_POLICY) {
2628 if (len < sizeof(*p) ||
2629 verify_newpolicy_info(p))
2632 nr = ((len - sizeof(*p)) / sizeof(*ut));
2633 if (validate_tmpl(nr, ut, p->sel.family))
2636 if (p->dir > XFRM_POLICY_OUT)
2639 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2645 copy_from_user_policy(xp, p);
2646 xp->type = XFRM_POLICY_TYPE_MAIN;
2647 copy_templates(xp, ut, nr);
2654 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2656 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2657 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2658 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2659 + nla_total_size(sizeof(struct xfrm_mark))
2660 + userpolicy_type_attrsize();
2663 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2664 int dir, const struct km_event *c)
2666 struct xfrm_user_polexpire *upe;
2667 struct nlmsghdr *nlh;
2668 int hard = c->data.hard;
2670 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2674 upe = nlmsg_data(nlh);
2675 copy_to_user_policy(xp, &upe->pol, dir);
2676 if (copy_to_user_tmpl(xp, skb) < 0)
2678 if (copy_to_user_sec_ctx(xp, skb))
2680 if (copy_to_user_policy_type(xp->type, skb) < 0)
2682 if (xfrm_mark_put(skb, &xp->mark))
2683 goto nla_put_failure;
2686 return nlmsg_end(skb, nlh);
2690 nlmsg_cancel(skb, nlh);
2694 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2696 struct net *net = xp_net(xp);
2697 struct sk_buff *skb;
2699 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2703 if (build_polexpire(skb, xp, dir, c) < 0)
2706 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2709 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2711 struct net *net = xp_net(xp);
2712 struct xfrm_userpolicy_info *p;
2713 struct xfrm_userpolicy_id *id;
2714 struct nlmsghdr *nlh;
2715 struct sk_buff *skb;
2716 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2719 headlen = sizeof(*p);
2720 if (c->event == XFRM_MSG_DELPOLICY) {
2721 len += nla_total_size(headlen);
2722 headlen = sizeof(*id);
2724 len += userpolicy_type_attrsize();
2725 len += nla_total_size(sizeof(struct xfrm_mark));
2726 len += NLMSG_ALIGN(headlen);
2728 skb = nlmsg_new(len, GFP_ATOMIC);
2732 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2736 p = nlmsg_data(nlh);
2737 if (c->event == XFRM_MSG_DELPOLICY) {
2738 struct nlattr *attr;
2740 id = nlmsg_data(nlh);
2741 memset(id, 0, sizeof(*id));
2744 id->index = xp->index;
2746 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2748 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2755 copy_to_user_policy(xp, p, dir);
2756 if (copy_to_user_tmpl(xp, skb) < 0)
2758 if (copy_to_user_policy_type(xp->type, skb) < 0)
2761 if (xfrm_mark_put(skb, &xp->mark))
2762 goto nla_put_failure;
2764 nlmsg_end(skb, nlh);
2766 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2774 static int xfrm_notify_policy_flush(const struct km_event *c)
2776 struct net *net = c->net;
2777 struct nlmsghdr *nlh;
2778 struct sk_buff *skb;
2780 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2784 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2787 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2790 nlmsg_end(skb, nlh);
2792 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2799 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2803 case XFRM_MSG_NEWPOLICY:
2804 case XFRM_MSG_UPDPOLICY:
2805 case XFRM_MSG_DELPOLICY:
2806 return xfrm_notify_policy(xp, dir, c);
2807 case XFRM_MSG_FLUSHPOLICY:
2808 return xfrm_notify_policy_flush(c);
2809 case XFRM_MSG_POLEXPIRE:
2810 return xfrm_exp_policy_notify(xp, dir, c);
2812 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2820 static inline size_t xfrm_report_msgsize(void)
2822 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2825 static int build_report(struct sk_buff *skb, u8 proto,
2826 struct xfrm_selector *sel, xfrm_address_t *addr)
2828 struct xfrm_user_report *ur;
2829 struct nlmsghdr *nlh;
2831 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2835 ur = nlmsg_data(nlh);
2837 memcpy(&ur->sel, sel, sizeof(ur->sel));
2840 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2842 return nlmsg_end(skb, nlh);
2845 nlmsg_cancel(skb, nlh);
2849 static int xfrm_send_report(struct net *net, u8 proto,
2850 struct xfrm_selector *sel, xfrm_address_t *addr)
2852 struct sk_buff *skb;
2854 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2858 if (build_report(skb, proto, sel, addr) < 0)
2861 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2864 static inline size_t xfrm_mapping_msgsize(void)
2866 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2869 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2870 xfrm_address_t *new_saddr, __be16 new_sport)
2872 struct xfrm_user_mapping *um;
2873 struct nlmsghdr *nlh;
2875 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2879 um = nlmsg_data(nlh);
2881 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2882 um->id.spi = x->id.spi;
2883 um->id.family = x->props.family;
2884 um->id.proto = x->id.proto;
2885 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2886 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2887 um->new_sport = new_sport;
2888 um->old_sport = x->encap->encap_sport;
2889 um->reqid = x->props.reqid;
2891 return nlmsg_end(skb, nlh);
2894 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2897 struct net *net = xs_net(x);
2898 struct sk_buff *skb;
2900 if (x->id.proto != IPPROTO_ESP)
2906 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2910 if (build_mapping(skb, x, ipaddr, sport) < 0)
2913 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2916 static struct xfrm_mgr netlink_mgr = {
2918 .notify = xfrm_send_state_notify,
2919 .acquire = xfrm_send_acquire,
2920 .compile_policy = xfrm_compile_policy,
2921 .notify_policy = xfrm_send_policy_notify,
2922 .report = xfrm_send_report,
2923 .migrate = xfrm_send_migrate,
2924 .new_mapping = xfrm_send_mapping,
2927 static int __net_init xfrm_user_net_init(struct net *net)
2931 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2932 xfrm_netlink_rcv, NULL, THIS_MODULE);
2935 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2936 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2940 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2943 list_for_each_entry(net, net_exit_list, exit_list)
2944 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2946 list_for_each_entry(net, net_exit_list, exit_list)
2947 netlink_kernel_release(net->xfrm.nlsk_stash);
2950 static struct pernet_operations xfrm_user_net_ops = {
2951 .init = xfrm_user_net_init,
2952 .exit_batch = xfrm_user_net_exit,
2955 static int __init xfrm_user_init(void)
2959 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2961 rv = register_pernet_subsys(&xfrm_user_net_ops);
2964 rv = xfrm_register_km(&netlink_mgr);
2966 unregister_pernet_subsys(&xfrm_user_net_ops);
2970 static void __exit xfrm_user_exit(void)
2972 xfrm_unregister_km(&netlink_mgr);
2973 unregister_pernet_subsys(&xfrm_user_net_ops);
2976 module_init(xfrm_user_init);
2977 module_exit(xfrm_user_exit);
2978 MODULE_LICENSE("GPL");
2979 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);