1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
30 #include <asm/uaccess.h>
31 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 #include <linux/in6.h>
35 static inline int aead_len(struct xfrm_algo_aead *alg)
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
40 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
42 struct nlattr *rt = attrs[type];
43 struct xfrm_algo *algp;
49 if (nla_len(rt) < xfrm_alg_len(algp))
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
66 static int verify_auth_trunc(struct nlattr **attrs)
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
82 static int verify_aead(struct nlattr **attrs)
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
91 if (nla_len(rt) < aead_len(algp))
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
98 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
99 xfrm_address_t **addrp)
101 struct nlattr *rt = attrs[type];
104 *addrp = nla_data(rt);
107 static inline int verify_sec_ctx_len(struct nlattr **attrs)
109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
110 struct xfrm_user_sec_ctx *uctx;
116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
122 static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
126 struct xfrm_replay_state_esn *rs;
129 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
133 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
136 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
137 nla_len(rt) != sizeof(*rs))
140 if (p->id.proto != IPPROTO_ESP)
143 if (p->replay_window != 0)
149 static int verify_newsa_info(struct xfrm_usersa_info *p,
150 struct nlattr **attrs)
160 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
172 switch (p->id.proto) {
174 if ((!attrs[XFRMA_ALG_AUTH] &&
175 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
176 attrs[XFRMA_ALG_AEAD] ||
177 attrs[XFRMA_ALG_CRYPT] ||
178 attrs[XFRMA_ALG_COMP] ||
184 if (attrs[XFRMA_ALG_COMP])
186 if (!attrs[XFRMA_ALG_AUTH] &&
187 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
188 !attrs[XFRMA_ALG_CRYPT] &&
189 !attrs[XFRMA_ALG_AEAD])
191 if ((attrs[XFRMA_ALG_AUTH] ||
192 attrs[XFRMA_ALG_AUTH_TRUNC] ||
193 attrs[XFRMA_ALG_CRYPT]) &&
194 attrs[XFRMA_ALG_AEAD])
196 if (attrs[XFRMA_TFCPAD] &&
197 p->mode != XFRM_MODE_TUNNEL)
202 if (!attrs[XFRMA_ALG_COMP] ||
203 attrs[XFRMA_ALG_AEAD] ||
204 attrs[XFRMA_ALG_AUTH] ||
205 attrs[XFRMA_ALG_AUTH_TRUNC] ||
206 attrs[XFRMA_ALG_CRYPT] ||
211 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
212 case IPPROTO_DSTOPTS:
213 case IPPROTO_ROUTING:
214 if (attrs[XFRMA_ALG_COMP] ||
215 attrs[XFRMA_ALG_AUTH] ||
216 attrs[XFRMA_ALG_AUTH_TRUNC] ||
217 attrs[XFRMA_ALG_AEAD] ||
218 attrs[XFRMA_ALG_CRYPT] ||
219 attrs[XFRMA_ENCAP] ||
220 attrs[XFRMA_SEC_CTX] ||
221 attrs[XFRMA_TFCPAD] ||
222 !attrs[XFRMA_COADDR])
231 if ((err = verify_aead(attrs)))
233 if ((err = verify_auth_trunc(attrs)))
235 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
237 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
239 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
241 if ((err = verify_sec_ctx_len(attrs)))
243 if ((err = verify_replay(p, attrs)))
248 case XFRM_MODE_TRANSPORT:
249 case XFRM_MODE_TUNNEL:
250 case XFRM_MODE_ROUTEOPTIMIZATION:
264 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
265 struct xfrm_algo_desc *(*get_byname)(const char *, int),
268 struct xfrm_algo *p, *ualg;
269 struct xfrm_algo_desc *algo;
274 ualg = nla_data(rta);
276 algo = get_byname(ualg->alg_name, 1);
279 *props = algo->desc.sadb_alg_id;
281 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
285 strcpy(p->alg_name, algo->name);
290 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
293 struct xfrm_algo *ualg;
294 struct xfrm_algo_auth *p;
295 struct xfrm_algo_desc *algo;
300 ualg = nla_data(rta);
302 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
305 *props = algo->desc.sadb_alg_id;
307 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
311 strcpy(p->alg_name, algo->name);
312 p->alg_key_len = ualg->alg_key_len;
313 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
314 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
320 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
323 struct xfrm_algo_auth *p, *ualg;
324 struct xfrm_algo_desc *algo;
329 ualg = nla_data(rta);
331 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
334 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
335 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
337 *props = algo->desc.sadb_alg_id;
339 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
343 strcpy(p->alg_name, algo->name);
344 if (!p->alg_trunc_len)
345 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
351 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
354 struct xfrm_algo_aead *p, *ualg;
355 struct xfrm_algo_desc *algo;
360 ualg = nla_data(rta);
362 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
365 *props = algo->desc.sadb_alg_id;
367 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
371 strcpy(p->alg_name, algo->name);
376 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
379 struct xfrm_replay_state_esn *up;
382 if (!replay_esn || !rp)
386 ulen = xfrm_replay_state_esn_len(up);
388 /* Check the overall length and the internal bitmap length to avoid
389 * potential overflow. */
390 if (nla_len(rp) < ulen ||
391 xfrm_replay_state_esn_len(replay_esn) != ulen ||
392 replay_esn->bmp_len != up->bmp_len)
395 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
401 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
402 struct xfrm_replay_state_esn **preplay_esn,
405 struct xfrm_replay_state_esn *p, *pp, *up;
412 klen = xfrm_replay_state_esn_len(up);
413 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
415 p = kzalloc(klen, GFP_KERNEL);
419 pp = kzalloc(klen, GFP_KERNEL);
426 memcpy(pp, up, ulen);
434 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
439 len += sizeof(struct xfrm_user_sec_ctx);
440 len += xfrm_ctx->ctx_len;
445 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
447 memcpy(&x->id, &p->id, sizeof(x->id));
448 memcpy(&x->sel, &p->sel, sizeof(x->sel));
449 memcpy(&x->lft, &p->lft, sizeof(x->lft));
450 x->props.mode = p->mode;
451 x->props.replay_window = p->replay_window;
452 x->props.reqid = p->reqid;
453 x->props.family = p->family;
454 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
455 x->props.flags = p->flags;
457 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
458 x->sel.family = p->family;
462 * someday when pfkey also has support, we could have the code
463 * somehow made shareable and move it to xfrm_state.c - JHS
466 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
469 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
470 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
471 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
472 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
473 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
476 struct xfrm_replay_state_esn *replay_esn;
477 replay_esn = nla_data(re);
478 memcpy(x->replay_esn, replay_esn,
479 xfrm_replay_state_esn_len(replay_esn));
480 memcpy(x->preplay_esn, replay_esn,
481 xfrm_replay_state_esn_len(replay_esn));
485 struct xfrm_replay_state *replay;
486 replay = nla_data(rp);
487 memcpy(&x->replay, replay, sizeof(*replay));
488 memcpy(&x->preplay, replay, sizeof(*replay));
492 struct xfrm_lifetime_cur *ltime;
493 ltime = nla_data(lt);
494 x->curlft.bytes = ltime->bytes;
495 x->curlft.packets = ltime->packets;
496 x->curlft.add_time = ltime->add_time;
497 x->curlft.use_time = ltime->use_time;
501 x->replay_maxage = nla_get_u32(et);
504 x->replay_maxdiff = nla_get_u32(rt);
507 static struct xfrm_state *xfrm_state_construct(struct net *net,
508 struct xfrm_usersa_info *p,
509 struct nlattr **attrs,
512 struct xfrm_state *x = xfrm_state_alloc(net);
518 copy_from_user_state(x, p);
520 if ((err = attach_aead(&x->aead, &x->props.ealgo,
521 attrs[XFRMA_ALG_AEAD])))
523 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
524 attrs[XFRMA_ALG_AUTH_TRUNC])))
526 if (!x->props.aalgo) {
527 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
528 attrs[XFRMA_ALG_AUTH])))
531 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
532 xfrm_ealg_get_byname,
533 attrs[XFRMA_ALG_CRYPT])))
535 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
536 xfrm_calg_get_byname,
537 attrs[XFRMA_ALG_COMP])))
540 if (attrs[XFRMA_ENCAP]) {
541 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
542 sizeof(*x->encap), GFP_KERNEL);
543 if (x->encap == NULL)
547 if (attrs[XFRMA_TFCPAD])
548 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
550 if (attrs[XFRMA_COADDR]) {
551 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
552 sizeof(*x->coaddr), GFP_KERNEL);
553 if (x->coaddr == NULL)
557 xfrm_mark_get(attrs, &x->mark);
559 err = __xfrm_init_state(x, false);
563 if (attrs[XFRMA_SEC_CTX]) {
564 err = security_xfrm_state_alloc(x,
565 nla_data(attrs[XFRMA_SEC_CTX]));
570 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
571 attrs[XFRMA_REPLAY_ESN_VAL])))
575 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
576 /* sysctl_xfrm_aevent_etime is in 100ms units */
577 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
579 if ((err = xfrm_init_replay(x)))
582 /* override default values from above */
583 xfrm_update_ae_params(x, attrs, 0);
588 x->km.state = XFRM_STATE_DEAD;
595 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
596 struct nlattr **attrs)
598 struct net *net = sock_net(skb->sk);
599 struct xfrm_usersa_info *p = nlmsg_data(nlh);
600 struct xfrm_state *x;
603 uid_t loginuid = audit_get_loginuid(current);
604 u32 sessionid = audit_get_sessionid(current);
607 err = verify_newsa_info(p, attrs);
611 x = xfrm_state_construct(net, p, attrs, &err);
616 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
617 err = xfrm_state_add(x);
619 err = xfrm_state_update(x);
621 security_task_getsecid(current, &sid);
622 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
625 x->km.state = XFRM_STATE_DEAD;
630 c.seq = nlh->nlmsg_seq;
631 c.pid = nlh->nlmsg_pid;
632 c.event = nlh->nlmsg_type;
634 km_state_notify(x, &c);
640 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
641 struct xfrm_usersa_id *p,
642 struct nlattr **attrs,
645 struct xfrm_state *x = NULL;
648 u32 mark = xfrm_mark_get(attrs, &m);
650 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
652 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
654 xfrm_address_t *saddr = NULL;
656 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
663 x = xfrm_state_lookup_byaddr(net, mark,
665 p->proto, p->family);
674 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
675 struct nlattr **attrs)
677 struct net *net = sock_net(skb->sk);
678 struct xfrm_state *x;
681 struct xfrm_usersa_id *p = nlmsg_data(nlh);
682 uid_t loginuid = audit_get_loginuid(current);
683 u32 sessionid = audit_get_sessionid(current);
686 x = xfrm_user_state_lookup(net, p, attrs, &err);
690 if ((err = security_xfrm_state_delete(x)) != 0)
693 if (xfrm_state_kern(x)) {
698 err = xfrm_state_delete(x);
703 c.seq = nlh->nlmsg_seq;
704 c.pid = nlh->nlmsg_pid;
705 c.event = nlh->nlmsg_type;
706 km_state_notify(x, &c);
709 security_task_getsecid(current, &sid);
710 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
715 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
717 memset(p, 0, sizeof(*p));
718 memcpy(&p->id, &x->id, sizeof(p->id));
719 memcpy(&p->sel, &x->sel, sizeof(p->sel));
720 memcpy(&p->lft, &x->lft, sizeof(p->lft));
721 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
722 memcpy(&p->stats, &x->stats, sizeof(p->stats));
723 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
724 p->mode = x->props.mode;
725 p->replay_window = x->props.replay_window;
726 p->reqid = x->props.reqid;
727 p->family = x->props.family;
728 p->flags = x->props.flags;
732 struct xfrm_dump_info {
733 struct sk_buff *in_skb;
734 struct sk_buff *out_skb;
739 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
741 struct xfrm_user_sec_ctx *uctx;
743 int ctx_size = sizeof(*uctx) + s->ctx_len;
745 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
749 uctx = nla_data(attr);
750 uctx->exttype = XFRMA_SEC_CTX;
751 uctx->len = ctx_size;
752 uctx->ctx_doi = s->ctx_doi;
753 uctx->ctx_alg = s->ctx_alg;
754 uctx->ctx_len = s->ctx_len;
755 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
760 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
762 struct xfrm_algo *algo;
765 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
766 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
770 algo = nla_data(nla);
771 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
772 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
773 algo->alg_key_len = auth->alg_key_len;
778 /* Don't change this without updating xfrm_sa_len! */
779 static int copy_to_user_state_extra(struct xfrm_state *x,
780 struct xfrm_usersa_info *p,
783 copy_to_user_state(x, p);
786 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
789 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
792 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
794 if (copy_to_user_auth(x->aalg, skb))
795 goto nla_put_failure;
797 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
798 xfrm_alg_auth_len(x->aalg), x->aalg);
801 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
803 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
806 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
809 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
811 if (xfrm_mark_put(skb, &x->mark))
812 goto nla_put_failure;
815 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
816 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
818 if (x->security && copy_sec_ctx(x->security, skb) < 0)
819 goto nla_put_failure;
827 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
829 struct xfrm_dump_info *sp = ptr;
830 struct sk_buff *in_skb = sp->in_skb;
831 struct sk_buff *skb = sp->out_skb;
832 struct xfrm_usersa_info *p;
833 struct nlmsghdr *nlh;
836 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
837 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
843 err = copy_to_user_state_extra(x, p, skb);
845 goto nla_put_failure;
851 nlmsg_cancel(skb, nlh);
855 static int xfrm_dump_sa_done(struct netlink_callback *cb)
857 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
858 xfrm_state_walk_done(walk);
862 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
864 struct net *net = sock_net(skb->sk);
865 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
866 struct xfrm_dump_info info;
868 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
869 sizeof(cb->args) - sizeof(cb->args[0]));
871 info.in_skb = cb->skb;
873 info.nlmsg_seq = cb->nlh->nlmsg_seq;
874 info.nlmsg_flags = NLM_F_MULTI;
878 xfrm_state_walk_init(walk, 0);
881 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
886 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
887 struct xfrm_state *x, u32 seq)
889 struct xfrm_dump_info info;
893 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
895 return ERR_PTR(-ENOMEM);
897 info.in_skb = in_skb;
899 info.nlmsg_seq = seq;
900 info.nlmsg_flags = 0;
902 err = dump_one_state(x, 0, &info);
911 static inline size_t xfrm_spdinfo_msgsize(void)
913 return NLMSG_ALIGN(4)
914 + nla_total_size(sizeof(struct xfrmu_spdinfo))
915 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
918 static int build_spdinfo(struct sk_buff *skb, struct net *net,
919 u32 pid, u32 seq, u32 flags)
921 struct xfrmk_spdinfo si;
922 struct xfrmu_spdinfo spc;
923 struct xfrmu_spdhinfo sph;
924 struct nlmsghdr *nlh;
927 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
928 if (nlh == NULL) /* shouldn't really happen ... */
933 xfrm_spd_getinfo(net, &si);
934 spc.incnt = si.incnt;
935 spc.outcnt = si.outcnt;
936 spc.fwdcnt = si.fwdcnt;
937 spc.inscnt = si.inscnt;
938 spc.outscnt = si.outscnt;
939 spc.fwdscnt = si.fwdscnt;
940 sph.spdhcnt = si.spdhcnt;
941 sph.spdhmcnt = si.spdhmcnt;
943 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
944 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
946 return nlmsg_end(skb, nlh);
949 nlmsg_cancel(skb, nlh);
953 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
954 struct nlattr **attrs)
956 struct net *net = sock_net(skb->sk);
957 struct sk_buff *r_skb;
958 u32 *flags = nlmsg_data(nlh);
959 u32 spid = NETLINK_CB(skb).pid;
960 u32 seq = nlh->nlmsg_seq;
962 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
966 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
969 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
972 static inline size_t xfrm_sadinfo_msgsize(void)
974 return NLMSG_ALIGN(4)
975 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
976 + nla_total_size(4); /* XFRMA_SAD_CNT */
979 static int build_sadinfo(struct sk_buff *skb, struct net *net,
980 u32 pid, u32 seq, u32 flags)
982 struct xfrmk_sadinfo si;
983 struct xfrmu_sadhinfo sh;
984 struct nlmsghdr *nlh;
987 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
988 if (nlh == NULL) /* shouldn't really happen ... */
993 xfrm_sad_getinfo(net, &si);
995 sh.sadhmcnt = si.sadhmcnt;
996 sh.sadhcnt = si.sadhcnt;
998 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
999 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1001 return nlmsg_end(skb, nlh);
1004 nlmsg_cancel(skb, nlh);
1008 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1009 struct nlattr **attrs)
1011 struct net *net = sock_net(skb->sk);
1012 struct sk_buff *r_skb;
1013 u32 *flags = nlmsg_data(nlh);
1014 u32 spid = NETLINK_CB(skb).pid;
1015 u32 seq = nlh->nlmsg_seq;
1017 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1021 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
1024 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
1027 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1028 struct nlattr **attrs)
1030 struct net *net = sock_net(skb->sk);
1031 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1032 struct xfrm_state *x;
1033 struct sk_buff *resp_skb;
1036 x = xfrm_user_state_lookup(net, p, attrs, &err);
1040 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1041 if (IS_ERR(resp_skb)) {
1042 err = PTR_ERR(resp_skb);
1044 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1051 static int verify_userspi_info(struct xfrm_userspi_info *p)
1053 switch (p->info.id.proto) {
1059 /* IPCOMP spi is 16-bits. */
1060 if (p->max >= 0x10000)
1068 if (p->min > p->max)
1074 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1075 struct nlattr **attrs)
1077 struct net *net = sock_net(skb->sk);
1078 struct xfrm_state *x;
1079 struct xfrm_userspi_info *p;
1080 struct sk_buff *resp_skb;
1081 xfrm_address_t *daddr;
1087 p = nlmsg_data(nlh);
1088 err = verify_userspi_info(p);
1092 family = p->info.family;
1093 daddr = &p->info.id.daddr;
1097 mark = xfrm_mark_get(attrs, &m);
1099 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1100 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1107 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1108 p->info.id.proto, daddr,
1115 err = xfrm_alloc_spi(x, p->min, p->max);
1119 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1120 if (IS_ERR(resp_skb)) {
1121 err = PTR_ERR(resp_skb);
1125 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1133 static int verify_policy_dir(u8 dir)
1136 case XFRM_POLICY_IN:
1137 case XFRM_POLICY_OUT:
1138 case XFRM_POLICY_FWD:
1148 static int verify_policy_type(u8 type)
1151 case XFRM_POLICY_TYPE_MAIN:
1152 #ifdef CONFIG_XFRM_SUB_POLICY
1153 case XFRM_POLICY_TYPE_SUB:
1164 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1167 case XFRM_SHARE_ANY:
1168 case XFRM_SHARE_SESSION:
1169 case XFRM_SHARE_USER:
1170 case XFRM_SHARE_UNIQUE:
1177 switch (p->action) {
1178 case XFRM_POLICY_ALLOW:
1179 case XFRM_POLICY_BLOCK:
1186 switch (p->sel.family) {
1191 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1194 return -EAFNOSUPPORT;
1201 return verify_policy_dir(p->dir);
1204 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1206 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1207 struct xfrm_user_sec_ctx *uctx;
1212 uctx = nla_data(rt);
1213 return security_xfrm_policy_alloc(&pol->security, uctx);
1216 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1222 for (i = 0; i < nr; i++, ut++) {
1223 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1225 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1226 memcpy(&t->saddr, &ut->saddr,
1227 sizeof(xfrm_address_t));
1228 t->reqid = ut->reqid;
1230 t->share = ut->share;
1231 t->optional = ut->optional;
1232 t->aalgos = ut->aalgos;
1233 t->ealgos = ut->ealgos;
1234 t->calgos = ut->calgos;
1235 /* If all masks are ~0, then we allow all algorithms. */
1236 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1237 t->encap_family = ut->family;
1241 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1245 if (nr > XFRM_MAX_DEPTH)
1248 for (i = 0; i < nr; i++) {
1249 /* We never validated the ut->family value, so many
1250 * applications simply leave it at zero. The check was
1251 * never made and ut->family was ignored because all
1252 * templates could be assumed to have the same family as
1253 * the policy itself. Now that we will have ipv4-in-ipv6
1254 * and ipv6-in-ipv4 tunnels, this is no longer true.
1257 ut[i].family = family;
1259 switch (ut[i].family) {
1262 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1274 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1276 struct nlattr *rt = attrs[XFRMA_TMPL];
1281 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1282 int nr = nla_len(rt) / sizeof(*utmpl);
1285 err = validate_tmpl(nr, utmpl, pol->family);
1289 copy_templates(pol, utmpl, nr);
1294 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1296 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1297 struct xfrm_userpolicy_type *upt;
1298 u8 type = XFRM_POLICY_TYPE_MAIN;
1306 err = verify_policy_type(type);
1314 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1316 xp->priority = p->priority;
1317 xp->index = p->index;
1318 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1319 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1320 xp->action = p->action;
1321 xp->flags = p->flags;
1322 xp->family = p->sel.family;
1323 /* XXX xp->share = p->share; */
1326 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1328 memset(p, 0, sizeof(*p));
1329 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1330 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1331 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1332 p->priority = xp->priority;
1333 p->index = xp->index;
1334 p->sel.family = xp->family;
1336 p->action = xp->action;
1337 p->flags = xp->flags;
1338 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1341 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1343 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1351 copy_from_user_policy(xp, p);
1353 err = copy_from_user_policy_type(&xp->type, attrs);
1357 if (!(err = copy_from_user_tmpl(xp, attrs)))
1358 err = copy_from_user_sec_ctx(xp, attrs);
1362 xfrm_mark_get(attrs, &xp->mark);
1368 xfrm_policy_destroy(xp);
1372 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1373 struct nlattr **attrs)
1375 struct net *net = sock_net(skb->sk);
1376 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1377 struct xfrm_policy *xp;
1381 uid_t loginuid = audit_get_loginuid(current);
1382 u32 sessionid = audit_get_sessionid(current);
1385 err = verify_newpolicy_info(p);
1388 err = verify_sec_ctx_len(attrs);
1392 xp = xfrm_policy_construct(net, p, attrs, &err);
1396 /* shouldn't excl be based on nlh flags??
1397 * Aha! this is anti-netlink really i.e more pfkey derived
1398 * in netlink excl is a flag and you wouldnt need
1399 * a type XFRM_MSG_UPDPOLICY - JHS */
1400 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1401 err = xfrm_policy_insert(p->dir, xp, excl);
1402 security_task_getsecid(current, &sid);
1403 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1406 security_xfrm_policy_free(xp->security);
1411 c.event = nlh->nlmsg_type;
1412 c.seq = nlh->nlmsg_seq;
1413 c.pid = nlh->nlmsg_pid;
1414 km_policy_notify(xp, p->dir, &c);
1421 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1423 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1426 if (xp->xfrm_nr == 0)
1429 for (i = 0; i < xp->xfrm_nr; i++) {
1430 struct xfrm_user_tmpl *up = &vec[i];
1431 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1433 memset(up, 0, sizeof(*up));
1434 memcpy(&up->id, &kp->id, sizeof(up->id));
1435 up->family = kp->encap_family;
1436 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1437 up->reqid = kp->reqid;
1438 up->mode = kp->mode;
1439 up->share = kp->share;
1440 up->optional = kp->optional;
1441 up->aalgos = kp->aalgos;
1442 up->ealgos = kp->ealgos;
1443 up->calgos = kp->calgos;
1446 return nla_put(skb, XFRMA_TMPL,
1447 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1450 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1453 return copy_sec_ctx(x->security, skb);
1458 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1461 return copy_sec_ctx(xp->security, skb);
1465 static inline size_t userpolicy_type_attrsize(void)
1467 #ifdef CONFIG_XFRM_SUB_POLICY
1468 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1474 #ifdef CONFIG_XFRM_SUB_POLICY
1475 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1477 struct xfrm_userpolicy_type upt = {
1481 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1485 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1491 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1493 struct xfrm_dump_info *sp = ptr;
1494 struct xfrm_userpolicy_info *p;
1495 struct sk_buff *in_skb = sp->in_skb;
1496 struct sk_buff *skb = sp->out_skb;
1497 struct nlmsghdr *nlh;
1499 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1500 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1504 p = nlmsg_data(nlh);
1505 copy_to_user_policy(xp, p, dir);
1506 if (copy_to_user_tmpl(xp, skb) < 0)
1508 if (copy_to_user_sec_ctx(xp, skb))
1510 if (copy_to_user_policy_type(xp->type, skb) < 0)
1512 if (xfrm_mark_put(skb, &xp->mark))
1513 goto nla_put_failure;
1515 nlmsg_end(skb, nlh);
1520 nlmsg_cancel(skb, nlh);
1524 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1526 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1529 xfrm_policy_walk_done(walk);
1533 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1535 struct net *net = sock_net(skb->sk);
1536 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1537 struct xfrm_dump_info info;
1539 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1540 sizeof(cb->args) - sizeof(cb->args[0]));
1542 info.in_skb = cb->skb;
1544 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1545 info.nlmsg_flags = NLM_F_MULTI;
1549 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1552 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1557 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1558 struct xfrm_policy *xp,
1561 struct xfrm_dump_info info;
1562 struct sk_buff *skb;
1565 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1567 return ERR_PTR(-ENOMEM);
1569 info.in_skb = in_skb;
1571 info.nlmsg_seq = seq;
1572 info.nlmsg_flags = 0;
1574 err = dump_one_policy(xp, dir, 0, &info);
1577 return ERR_PTR(err);
1583 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1584 struct nlattr **attrs)
1586 struct net *net = sock_net(skb->sk);
1587 struct xfrm_policy *xp;
1588 struct xfrm_userpolicy_id *p;
1589 u8 type = XFRM_POLICY_TYPE_MAIN;
1594 u32 mark = xfrm_mark_get(attrs, &m);
1596 p = nlmsg_data(nlh);
1597 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1599 err = copy_from_user_policy_type(&type, attrs);
1603 err = verify_policy_dir(p->dir);
1608 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1610 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1611 struct xfrm_sec_ctx *ctx;
1613 err = verify_sec_ctx_len(attrs);
1619 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1621 err = security_xfrm_policy_alloc(&ctx, uctx);
1625 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1627 security_xfrm_policy_free(ctx);
1633 struct sk_buff *resp_skb;
1635 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1636 if (IS_ERR(resp_skb)) {
1637 err = PTR_ERR(resp_skb);
1639 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1640 NETLINK_CB(skb).pid);
1643 uid_t loginuid = audit_get_loginuid(current);
1644 u32 sessionid = audit_get_sessionid(current);
1647 security_task_getsecid(current, &sid);
1648 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1654 c.data.byid = p->index;
1655 c.event = nlh->nlmsg_type;
1656 c.seq = nlh->nlmsg_seq;
1657 c.pid = nlh->nlmsg_pid;
1658 km_policy_notify(xp, p->dir, &c);
1666 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1667 struct nlattr **attrs)
1669 struct net *net = sock_net(skb->sk);
1671 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1672 struct xfrm_audit audit_info;
1675 audit_info.loginuid = audit_get_loginuid(current);
1676 audit_info.sessionid = audit_get_sessionid(current);
1677 security_task_getsecid(current, &audit_info.secid);
1678 err = xfrm_state_flush(net, p->proto, &audit_info);
1680 if (err == -ESRCH) /* empty table */
1684 c.data.proto = p->proto;
1685 c.event = nlh->nlmsg_type;
1686 c.seq = nlh->nlmsg_seq;
1687 c.pid = nlh->nlmsg_pid;
1689 km_state_notify(NULL, &c);
1694 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1696 size_t replay_size = x->replay_esn ?
1697 xfrm_replay_state_esn_len(x->replay_esn) :
1698 sizeof(struct xfrm_replay_state);
1700 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1701 + nla_total_size(replay_size)
1702 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1703 + nla_total_size(sizeof(struct xfrm_mark))
1704 + nla_total_size(4) /* XFRM_AE_RTHR */
1705 + nla_total_size(4); /* XFRM_AE_ETHR */
1708 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1710 struct xfrm_aevent_id *id;
1711 struct nlmsghdr *nlh;
1713 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1717 id = nlmsg_data(nlh);
1718 memset(&id->sa_id, 0, sizeof(id->sa_id));
1719 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1720 id->sa_id.spi = x->id.spi;
1721 id->sa_id.family = x->props.family;
1722 id->sa_id.proto = x->id.proto;
1723 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1724 id->reqid = x->props.reqid;
1725 id->flags = c->data.aevent;
1728 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1729 xfrm_replay_state_esn_len(x->replay_esn),
1732 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1734 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1736 if (id->flags & XFRM_AE_RTHR)
1737 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1739 if (id->flags & XFRM_AE_ETHR)
1740 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1741 x->replay_maxage * 10 / HZ);
1743 if (xfrm_mark_put(skb, &x->mark))
1744 goto nla_put_failure;
1746 return nlmsg_end(skb, nlh);
1749 nlmsg_cancel(skb, nlh);
1753 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1754 struct nlattr **attrs)
1756 struct net *net = sock_net(skb->sk);
1757 struct xfrm_state *x;
1758 struct sk_buff *r_skb;
1763 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1764 struct xfrm_usersa_id *id = &p->sa_id;
1766 mark = xfrm_mark_get(attrs, &m);
1768 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1772 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1773 if (r_skb == NULL) {
1779 * XXX: is this lock really needed - none of the other
1780 * gets lock (the concern is things getting updated
1781 * while we are still reading) - jhs
1783 spin_lock_bh(&x->lock);
1784 c.data.aevent = p->flags;
1785 c.seq = nlh->nlmsg_seq;
1786 c.pid = nlh->nlmsg_pid;
1788 if (build_aevent(r_skb, x, &c) < 0)
1790 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1791 spin_unlock_bh(&x->lock);
1796 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1797 struct nlattr **attrs)
1799 struct net *net = sock_net(skb->sk);
1800 struct xfrm_state *x;
1805 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1806 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1807 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1808 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1810 if (!lt && !rp && !re)
1813 /* pedantic mode - thou shalt sayeth replaceth */
1814 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1817 mark = xfrm_mark_get(attrs, &m);
1819 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1823 if (x->km.state != XFRM_STATE_VALID)
1826 err = xfrm_replay_verify_len(x->replay_esn, rp);
1830 spin_lock_bh(&x->lock);
1831 xfrm_update_ae_params(x, attrs, 1);
1832 spin_unlock_bh(&x->lock);
1834 c.event = nlh->nlmsg_type;
1835 c.seq = nlh->nlmsg_seq;
1836 c.pid = nlh->nlmsg_pid;
1837 c.data.aevent = XFRM_AE_CU;
1838 km_state_notify(x, &c);
1845 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1846 struct nlattr **attrs)
1848 struct net *net = sock_net(skb->sk);
1850 u8 type = XFRM_POLICY_TYPE_MAIN;
1852 struct xfrm_audit audit_info;
1854 err = copy_from_user_policy_type(&type, attrs);
1858 audit_info.loginuid = audit_get_loginuid(current);
1859 audit_info.sessionid = audit_get_sessionid(current);
1860 security_task_getsecid(current, &audit_info.secid);
1861 err = xfrm_policy_flush(net, type, &audit_info);
1863 if (err == -ESRCH) /* empty table */
1869 c.event = nlh->nlmsg_type;
1870 c.seq = nlh->nlmsg_seq;
1871 c.pid = nlh->nlmsg_pid;
1873 km_policy_notify(NULL, 0, &c);
1877 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1878 struct nlattr **attrs)
1880 struct net *net = sock_net(skb->sk);
1881 struct xfrm_policy *xp;
1882 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1883 struct xfrm_userpolicy_info *p = &up->pol;
1884 u8 type = XFRM_POLICY_TYPE_MAIN;
1887 u32 mark = xfrm_mark_get(attrs, &m);
1889 err = copy_from_user_policy_type(&type, attrs);
1893 err = verify_policy_dir(p->dir);
1898 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1900 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1901 struct xfrm_sec_ctx *ctx;
1903 err = verify_sec_ctx_len(attrs);
1909 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1911 err = security_xfrm_policy_alloc(&ctx, uctx);
1915 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1916 &p->sel, ctx, 0, &err);
1917 security_xfrm_policy_free(ctx);
1922 if (unlikely(xp->walk.dead))
1927 uid_t loginuid = audit_get_loginuid(current);
1928 u32 sessionid = audit_get_sessionid(current);
1931 security_task_getsecid(current, &sid);
1932 xfrm_policy_delete(xp, p->dir);
1933 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1936 // reset the timers here?
1937 WARN(1, "Dont know what to do with soft policy expire\n");
1939 km_policy_expired(xp, p->dir, up->hard, current->pid);
1946 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1947 struct nlattr **attrs)
1949 struct net *net = sock_net(skb->sk);
1950 struct xfrm_state *x;
1952 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1953 struct xfrm_usersa_info *p = &ue->state;
1955 u32 mark = xfrm_mark_get(attrs, &m);
1957 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1963 spin_lock_bh(&x->lock);
1965 if (x->km.state != XFRM_STATE_VALID)
1967 km_state_expired(x, ue->hard, current->pid);
1970 uid_t loginuid = audit_get_loginuid(current);
1971 u32 sessionid = audit_get_sessionid(current);
1974 security_task_getsecid(current, &sid);
1975 __xfrm_state_delete(x);
1976 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1980 spin_unlock_bh(&x->lock);
1985 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1986 struct nlattr **attrs)
1988 struct net *net = sock_net(skb->sk);
1989 struct xfrm_policy *xp;
1990 struct xfrm_user_tmpl *ut;
1992 struct nlattr *rt = attrs[XFRMA_TMPL];
1993 struct xfrm_mark mark;
1995 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1996 struct xfrm_state *x = xfrm_state_alloc(net);
2002 xfrm_mark_get(attrs, &mark);
2004 err = verify_newpolicy_info(&ua->policy);
2009 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2013 memcpy(&x->id, &ua->id, sizeof(ua->id));
2014 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2015 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2016 xp->mark.m = x->mark.m = mark.m;
2017 xp->mark.v = x->mark.v = mark.v;
2019 /* extract the templates and for each call km_key */
2020 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2021 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2022 memcpy(&x->id, &t->id, sizeof(x->id));
2023 x->props.mode = t->mode;
2024 x->props.reqid = t->reqid;
2025 x->props.family = ut->family;
2026 t->aalgos = ua->aalgos;
2027 t->ealgos = ua->ealgos;
2028 t->calgos = ua->calgos;
2029 err = km_query(x, t, xp);
2039 WARN(1, "BAD policy passed\n");
2046 #ifdef CONFIG_XFRM_MIGRATE
2047 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2048 struct xfrm_kmaddress *k,
2049 struct nlattr **attrs, int *num)
2051 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2052 struct xfrm_user_migrate *um;
2056 struct xfrm_user_kmaddress *uk;
2058 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2059 memcpy(&k->local, &uk->local, sizeof(k->local));
2060 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2061 k->family = uk->family;
2062 k->reserved = uk->reserved;
2066 num_migrate = nla_len(rt) / sizeof(*um);
2068 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2071 for (i = 0; i < num_migrate; i++, um++, ma++) {
2072 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2073 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2074 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2075 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2077 ma->proto = um->proto;
2078 ma->mode = um->mode;
2079 ma->reqid = um->reqid;
2081 ma->old_family = um->old_family;
2082 ma->new_family = um->new_family;
2089 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2090 struct nlattr **attrs)
2092 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2093 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2094 struct xfrm_kmaddress km, *kmp;
2099 if (attrs[XFRMA_MIGRATE] == NULL)
2102 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2104 err = copy_from_user_policy_type(&type, attrs);
2108 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2115 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2120 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2121 struct nlattr **attrs)
2123 return -ENOPROTOOPT;
2127 #ifdef CONFIG_XFRM_MIGRATE
2128 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2130 struct xfrm_user_migrate um;
2132 memset(&um, 0, sizeof(um));
2133 um.proto = m->proto;
2135 um.reqid = m->reqid;
2136 um.old_family = m->old_family;
2137 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2138 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2139 um.new_family = m->new_family;
2140 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2141 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2143 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2146 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2148 struct xfrm_user_kmaddress uk;
2150 memset(&uk, 0, sizeof(uk));
2151 uk.family = k->family;
2152 uk.reserved = k->reserved;
2153 memcpy(&uk.local, &k->local, sizeof(uk.local));
2154 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2156 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2159 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2161 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2162 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2163 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2164 + userpolicy_type_attrsize();
2167 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2168 int num_migrate, const struct xfrm_kmaddress *k,
2169 const struct xfrm_selector *sel, u8 dir, u8 type)
2171 const struct xfrm_migrate *mp;
2172 struct xfrm_userpolicy_id *pol_id;
2173 struct nlmsghdr *nlh;
2176 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2180 pol_id = nlmsg_data(nlh);
2181 /* copy data from selector, dir, and type to the pol_id */
2182 memset(pol_id, 0, sizeof(*pol_id));
2183 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2186 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2189 if (copy_to_user_policy_type(type, skb) < 0)
2192 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2193 if (copy_to_user_migrate(mp, skb) < 0)
2197 return nlmsg_end(skb, nlh);
2199 nlmsg_cancel(skb, nlh);
2203 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2204 const struct xfrm_migrate *m, int num_migrate,
2205 const struct xfrm_kmaddress *k)
2207 struct net *net = &init_net;
2208 struct sk_buff *skb;
2210 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2215 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2218 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2221 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2222 const struct xfrm_migrate *m, int num_migrate,
2223 const struct xfrm_kmaddress *k)
2225 return -ENOPROTOOPT;
2229 #define XMSGSIZE(type) sizeof(struct type)
2231 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2232 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2233 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2234 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2235 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2236 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2237 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2238 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2239 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2240 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2241 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2242 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2243 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2244 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2245 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2246 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2247 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2248 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2249 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2250 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2251 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2256 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2257 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2258 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2259 [XFRMA_LASTUSED] = { .type = NLA_U64},
2260 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2261 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2262 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2263 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2264 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2265 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2266 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2267 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2268 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2269 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2270 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2271 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2272 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2273 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2274 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2275 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2276 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2277 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2278 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2279 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2282 static struct xfrm_link {
2283 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2284 int (*dump)(struct sk_buff *, struct netlink_callback *);
2285 int (*done)(struct netlink_callback *);
2286 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2287 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2288 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2289 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2290 .dump = xfrm_dump_sa,
2291 .done = xfrm_dump_sa_done },
2292 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2293 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2294 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2295 .dump = xfrm_dump_policy,
2296 .done = xfrm_dump_policy_done },
2297 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2298 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2299 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2300 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2301 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2302 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2303 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2304 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2305 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2306 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2307 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2308 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2309 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2312 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2314 struct net *net = sock_net(skb->sk);
2315 struct nlattr *attrs[XFRMA_MAX+1];
2316 struct xfrm_link *link;
2319 type = nlh->nlmsg_type;
2320 if (type > XFRM_MSG_MAX)
2323 type -= XFRM_MSG_BASE;
2324 link = &xfrm_dispatch[type];
2326 /* All operations require privileges, even GET */
2327 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2330 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2331 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2332 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2333 if (link->dump == NULL)
2336 return netlink_dump_start(net->xfrm.nlsk, skb, nlh,
2337 link->dump, link->done, 0);
2340 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2345 if (link->doit == NULL)
2348 return link->doit(skb, nlh, attrs);
2351 static void xfrm_netlink_rcv(struct sk_buff *skb)
2353 mutex_lock(&xfrm_cfg_mutex);
2354 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2355 mutex_unlock(&xfrm_cfg_mutex);
2358 static inline size_t xfrm_expire_msgsize(void)
2360 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2361 + nla_total_size(sizeof(struct xfrm_mark));
2364 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2366 struct xfrm_user_expire *ue;
2367 struct nlmsghdr *nlh;
2369 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2373 ue = nlmsg_data(nlh);
2374 copy_to_user_state(x, &ue->state);
2375 ue->hard = (c->data.hard != 0) ? 1 : 0;
2377 if (xfrm_mark_put(skb, &x->mark))
2378 goto nla_put_failure;
2380 return nlmsg_end(skb, nlh);
2386 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2388 struct net *net = xs_net(x);
2389 struct sk_buff *skb;
2391 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2395 if (build_expire(skb, x, c) < 0) {
2400 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2403 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2405 struct net *net = xs_net(x);
2406 struct sk_buff *skb;
2408 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2412 if (build_aevent(skb, x, c) < 0)
2415 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2418 static int xfrm_notify_sa_flush(const struct km_event *c)
2420 struct net *net = c->net;
2421 struct xfrm_usersa_flush *p;
2422 struct nlmsghdr *nlh;
2423 struct sk_buff *skb;
2424 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2426 skb = nlmsg_new(len, GFP_ATOMIC);
2430 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2436 p = nlmsg_data(nlh);
2437 p->proto = c->data.proto;
2439 nlmsg_end(skb, nlh);
2441 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2444 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2448 l += nla_total_size(aead_len(x->aead));
2450 l += nla_total_size(sizeof(struct xfrm_algo) +
2451 (x->aalg->alg_key_len + 7) / 8);
2452 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2455 l += nla_total_size(xfrm_alg_len(x->ealg));
2457 l += nla_total_size(sizeof(*x->calg));
2459 l += nla_total_size(sizeof(*x->encap));
2461 l += nla_total_size(sizeof(x->tfcpad));
2463 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2465 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2466 x->security->ctx_len);
2468 l += nla_total_size(sizeof(*x->coaddr));
2470 /* Must count x->lastused as it may become non-zero behind our back. */
2471 l += nla_total_size(sizeof(u64));
2476 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2478 struct net *net = xs_net(x);
2479 struct xfrm_usersa_info *p;
2480 struct xfrm_usersa_id *id;
2481 struct nlmsghdr *nlh;
2482 struct sk_buff *skb;
2483 int len = xfrm_sa_len(x);
2486 headlen = sizeof(*p);
2487 if (c->event == XFRM_MSG_DELSA) {
2488 len += nla_total_size(headlen);
2489 headlen = sizeof(*id);
2490 len += nla_total_size(sizeof(struct xfrm_mark));
2492 len += NLMSG_ALIGN(headlen);
2494 skb = nlmsg_new(len, GFP_ATOMIC);
2498 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2500 goto nla_put_failure;
2502 p = nlmsg_data(nlh);
2503 if (c->event == XFRM_MSG_DELSA) {
2504 struct nlattr *attr;
2506 id = nlmsg_data(nlh);
2507 memset(id, 0, sizeof(*id));
2508 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2509 id->spi = x->id.spi;
2510 id->family = x->props.family;
2511 id->proto = x->id.proto;
2513 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2515 goto nla_put_failure;
2520 if (copy_to_user_state_extra(x, p, skb))
2521 goto nla_put_failure;
2523 nlmsg_end(skb, nlh);
2525 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2528 /* Somebody screwed up with xfrm_sa_len! */
2534 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2538 case XFRM_MSG_EXPIRE:
2539 return xfrm_exp_state_notify(x, c);
2540 case XFRM_MSG_NEWAE:
2541 return xfrm_aevent_state_notify(x, c);
2542 case XFRM_MSG_DELSA:
2543 case XFRM_MSG_UPDSA:
2544 case XFRM_MSG_NEWSA:
2545 return xfrm_notify_sa(x, c);
2546 case XFRM_MSG_FLUSHSA:
2547 return xfrm_notify_sa_flush(c);
2549 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2558 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2559 struct xfrm_policy *xp)
2561 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2562 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2563 + nla_total_size(sizeof(struct xfrm_mark))
2564 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2565 + userpolicy_type_attrsize();
2568 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2569 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2572 struct xfrm_user_acquire *ua;
2573 struct nlmsghdr *nlh;
2574 __u32 seq = xfrm_get_acqseq();
2576 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2580 ua = nlmsg_data(nlh);
2581 memcpy(&ua->id, &x->id, sizeof(ua->id));
2582 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2583 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2584 copy_to_user_policy(xp, &ua->policy, dir);
2585 ua->aalgos = xt->aalgos;
2586 ua->ealgos = xt->ealgos;
2587 ua->calgos = xt->calgos;
2588 ua->seq = x->km.seq = seq;
2590 if (copy_to_user_tmpl(xp, skb) < 0)
2592 if (copy_to_user_state_sec_ctx(x, skb))
2594 if (copy_to_user_policy_type(xp->type, skb) < 0)
2596 if (xfrm_mark_put(skb, &xp->mark))
2597 goto nla_put_failure;
2599 return nlmsg_end(skb, nlh);
2603 nlmsg_cancel(skb, nlh);
2607 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2608 struct xfrm_policy *xp, int dir)
2610 struct net *net = xs_net(x);
2611 struct sk_buff *skb;
2613 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2617 if (build_acquire(skb, x, xt, xp, dir) < 0)
2620 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2623 /* User gives us xfrm_user_policy_info followed by an array of 0
2624 * or more templates.
2626 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2627 u8 *data, int len, int *dir)
2629 struct net *net = sock_net(sk);
2630 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2631 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2632 struct xfrm_policy *xp;
2635 switch (sk->sk_family) {
2637 if (opt != IP_XFRM_POLICY) {
2642 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2644 if (opt != IPV6_XFRM_POLICY) {
2657 if (len < sizeof(*p) ||
2658 verify_newpolicy_info(p))
2661 nr = ((len - sizeof(*p)) / sizeof(*ut));
2662 if (validate_tmpl(nr, ut, p->sel.family))
2665 if (p->dir > XFRM_POLICY_OUT)
2668 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2674 copy_from_user_policy(xp, p);
2675 xp->type = XFRM_POLICY_TYPE_MAIN;
2676 copy_templates(xp, ut, nr);
2683 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2685 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2686 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2687 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2688 + nla_total_size(sizeof(struct xfrm_mark))
2689 + userpolicy_type_attrsize();
2692 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2693 int dir, const struct km_event *c)
2695 struct xfrm_user_polexpire *upe;
2696 struct nlmsghdr *nlh;
2697 int hard = c->data.hard;
2699 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2703 upe = nlmsg_data(nlh);
2704 copy_to_user_policy(xp, &upe->pol, dir);
2705 if (copy_to_user_tmpl(xp, skb) < 0)
2707 if (copy_to_user_sec_ctx(xp, skb))
2709 if (copy_to_user_policy_type(xp->type, skb) < 0)
2711 if (xfrm_mark_put(skb, &xp->mark))
2712 goto nla_put_failure;
2715 return nlmsg_end(skb, nlh);
2719 nlmsg_cancel(skb, nlh);
2723 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2725 struct net *net = xp_net(xp);
2726 struct sk_buff *skb;
2728 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2732 if (build_polexpire(skb, xp, dir, c) < 0)
2735 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2738 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2740 struct net *net = xp_net(xp);
2741 struct xfrm_userpolicy_info *p;
2742 struct xfrm_userpolicy_id *id;
2743 struct nlmsghdr *nlh;
2744 struct sk_buff *skb;
2745 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2748 headlen = sizeof(*p);
2749 if (c->event == XFRM_MSG_DELPOLICY) {
2750 len += nla_total_size(headlen);
2751 headlen = sizeof(*id);
2753 len += userpolicy_type_attrsize();
2754 len += nla_total_size(sizeof(struct xfrm_mark));
2755 len += NLMSG_ALIGN(headlen);
2757 skb = nlmsg_new(len, GFP_ATOMIC);
2761 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2765 p = nlmsg_data(nlh);
2766 if (c->event == XFRM_MSG_DELPOLICY) {
2767 struct nlattr *attr;
2769 id = nlmsg_data(nlh);
2770 memset(id, 0, sizeof(*id));
2773 id->index = xp->index;
2775 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2777 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2784 copy_to_user_policy(xp, p, dir);
2785 if (copy_to_user_tmpl(xp, skb) < 0)
2787 if (copy_to_user_policy_type(xp->type, skb) < 0)
2790 if (xfrm_mark_put(skb, &xp->mark))
2791 goto nla_put_failure;
2793 nlmsg_end(skb, nlh);
2795 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2803 static int xfrm_notify_policy_flush(const struct km_event *c)
2805 struct net *net = c->net;
2806 struct nlmsghdr *nlh;
2807 struct sk_buff *skb;
2809 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2813 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2816 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2819 nlmsg_end(skb, nlh);
2821 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2828 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2832 case XFRM_MSG_NEWPOLICY:
2833 case XFRM_MSG_UPDPOLICY:
2834 case XFRM_MSG_DELPOLICY:
2835 return xfrm_notify_policy(xp, dir, c);
2836 case XFRM_MSG_FLUSHPOLICY:
2837 return xfrm_notify_policy_flush(c);
2838 case XFRM_MSG_POLEXPIRE:
2839 return xfrm_exp_policy_notify(xp, dir, c);
2841 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2849 static inline size_t xfrm_report_msgsize(void)
2851 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2854 static int build_report(struct sk_buff *skb, u8 proto,
2855 struct xfrm_selector *sel, xfrm_address_t *addr)
2857 struct xfrm_user_report *ur;
2858 struct nlmsghdr *nlh;
2860 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2864 ur = nlmsg_data(nlh);
2866 memcpy(&ur->sel, sel, sizeof(ur->sel));
2869 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2871 return nlmsg_end(skb, nlh);
2874 nlmsg_cancel(skb, nlh);
2878 static int xfrm_send_report(struct net *net, u8 proto,
2879 struct xfrm_selector *sel, xfrm_address_t *addr)
2881 struct sk_buff *skb;
2883 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2887 if (build_report(skb, proto, sel, addr) < 0)
2890 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2893 static inline size_t xfrm_mapping_msgsize(void)
2895 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2898 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2899 xfrm_address_t *new_saddr, __be16 new_sport)
2901 struct xfrm_user_mapping *um;
2902 struct nlmsghdr *nlh;
2904 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2908 um = nlmsg_data(nlh);
2910 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2911 um->id.spi = x->id.spi;
2912 um->id.family = x->props.family;
2913 um->id.proto = x->id.proto;
2914 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2915 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2916 um->new_sport = new_sport;
2917 um->old_sport = x->encap->encap_sport;
2918 um->reqid = x->props.reqid;
2920 return nlmsg_end(skb, nlh);
2923 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2926 struct net *net = xs_net(x);
2927 struct sk_buff *skb;
2929 if (x->id.proto != IPPROTO_ESP)
2935 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2939 if (build_mapping(skb, x, ipaddr, sport) < 0)
2942 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2945 static struct xfrm_mgr netlink_mgr = {
2947 .notify = xfrm_send_state_notify,
2948 .acquire = xfrm_send_acquire,
2949 .compile_policy = xfrm_compile_policy,
2950 .notify_policy = xfrm_send_policy_notify,
2951 .report = xfrm_send_report,
2952 .migrate = xfrm_send_migrate,
2953 .new_mapping = xfrm_send_mapping,
2956 static int __net_init xfrm_user_net_init(struct net *net)
2960 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2961 xfrm_netlink_rcv, NULL, THIS_MODULE);
2964 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2965 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2969 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2972 list_for_each_entry(net, net_exit_list, exit_list)
2973 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
2975 list_for_each_entry(net, net_exit_list, exit_list)
2976 netlink_kernel_release(net->xfrm.nlsk_stash);
2979 static struct pernet_operations xfrm_user_net_ops = {
2980 .init = xfrm_user_net_init,
2981 .exit_batch = xfrm_user_net_exit,
2984 static int __init xfrm_user_init(void)
2988 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2990 rv = register_pernet_subsys(&xfrm_user_net_ops);
2993 rv = xfrm_register_km(&netlink_mgr);
2995 unregister_pernet_subsys(&xfrm_user_net_ops);
2999 static void __exit xfrm_user_exit(void)
3001 xfrm_unregister_km(&netlink_mgr);
3002 unregister_pernet_subsys(&xfrm_user_net_ops);
3005 module_init(xfrm_user_init);
3006 module_exit(xfrm_user_exit);
3007 MODULE_LICENSE("GPL");
3008 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);