1 // SPDX-License-Identifier: GPL-2.0-only
2 /* xfrm_user.c: User interface to configure xfrm engine.
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
8 * Kazunori MIYAZAWA @USAGI
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
14 #include <linux/compat.h>
15 #include <linux/crypto.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/socket.h>
21 #include <linux/string.h>
22 #include <linux/net.h>
23 #include <linux/skbuff.h>
24 #include <linux/pfkeyv2.h>
25 #include <linux/ipsec.h>
26 #include <linux/init.h>
27 #include <linux/security.h>
30 #include <net/netlink.h>
32 #include <linux/uaccess.h>
33 #if IS_ENABLED(CONFIG_IPV6)
34 #include <linux/in6.h>
36 #include <asm/unaligned.h>
38 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
40 struct nlattr *rt = attrs[type];
41 struct xfrm_algo *algp;
47 if (nla_len(rt) < (int)xfrm_alg_len(algp))
60 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
64 static int verify_auth_trunc(struct nlattr **attrs)
66 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
67 struct xfrm_algo_auth *algp;
73 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
76 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
80 static int verify_aead(struct nlattr **attrs)
82 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
83 struct xfrm_algo_aead *algp;
89 if (nla_len(rt) < (int)aead_len(algp))
92 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
96 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
97 xfrm_address_t **addrp)
99 struct nlattr *rt = attrs[type];
102 *addrp = nla_data(rt);
105 static inline int verify_sec_ctx_len(struct nlattr **attrs)
107 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
108 struct xfrm_user_sec_ctx *uctx;
114 if (uctx->len > nla_len(rt) ||
115 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
121 static inline int verify_replay(struct xfrm_usersa_info *p,
122 struct nlattr **attrs)
124 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
125 struct xfrm_replay_state_esn *rs;
128 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
132 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
135 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
136 nla_len(rt) != sizeof(*rs))
139 /* As only ESP and AH support ESN feature. */
140 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
143 if (p->replay_window != 0)
149 static int verify_newsa_info(struct xfrm_usersa_info *p,
150 struct nlattr **attrs)
160 #if IS_ENABLED(CONFIG_IPV6)
171 switch (p->sel.family) {
176 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
182 #if IS_ENABLED(CONFIG_IPV6)
183 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
197 switch (p->id.proto) {
199 if ((!attrs[XFRMA_ALG_AUTH] &&
200 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
201 attrs[XFRMA_ALG_AEAD] ||
202 attrs[XFRMA_ALG_CRYPT] ||
203 attrs[XFRMA_ALG_COMP] ||
209 if (attrs[XFRMA_ALG_COMP])
211 if (!attrs[XFRMA_ALG_AUTH] &&
212 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
213 !attrs[XFRMA_ALG_CRYPT] &&
214 !attrs[XFRMA_ALG_AEAD])
216 if ((attrs[XFRMA_ALG_AUTH] ||
217 attrs[XFRMA_ALG_AUTH_TRUNC] ||
218 attrs[XFRMA_ALG_CRYPT]) &&
219 attrs[XFRMA_ALG_AEAD])
221 if (attrs[XFRMA_TFCPAD] &&
222 p->mode != XFRM_MODE_TUNNEL)
227 if (!attrs[XFRMA_ALG_COMP] ||
228 attrs[XFRMA_ALG_AEAD] ||
229 attrs[XFRMA_ALG_AUTH] ||
230 attrs[XFRMA_ALG_AUTH_TRUNC] ||
231 attrs[XFRMA_ALG_CRYPT] ||
232 attrs[XFRMA_TFCPAD] ||
233 (ntohl(p->id.spi) >= 0x10000))
237 #if IS_ENABLED(CONFIG_IPV6)
238 case IPPROTO_DSTOPTS:
239 case IPPROTO_ROUTING:
240 if (attrs[XFRMA_ALG_COMP] ||
241 attrs[XFRMA_ALG_AUTH] ||
242 attrs[XFRMA_ALG_AUTH_TRUNC] ||
243 attrs[XFRMA_ALG_AEAD] ||
244 attrs[XFRMA_ALG_CRYPT] ||
245 attrs[XFRMA_ENCAP] ||
246 attrs[XFRMA_SEC_CTX] ||
247 attrs[XFRMA_TFCPAD] ||
248 !attrs[XFRMA_COADDR])
257 if ((err = verify_aead(attrs)))
259 if ((err = verify_auth_trunc(attrs)))
261 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
263 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
265 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
267 if ((err = verify_sec_ctx_len(attrs)))
269 if ((err = verify_replay(p, attrs)))
274 case XFRM_MODE_TRANSPORT:
275 case XFRM_MODE_TUNNEL:
276 case XFRM_MODE_ROUTEOPTIMIZATION:
286 if (attrs[XFRMA_MTIMER_THRESH])
287 if (!attrs[XFRMA_ENCAP])
294 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
295 struct xfrm_algo_desc *(*get_byname)(const char *, int),
298 struct xfrm_algo *p, *ualg;
299 struct xfrm_algo_desc *algo;
304 ualg = nla_data(rta);
306 algo = get_byname(ualg->alg_name, 1);
309 *props = algo->desc.sadb_alg_id;
311 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
315 strcpy(p->alg_name, algo->name);
320 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
322 struct xfrm_algo *p, *ualg;
323 struct xfrm_algo_desc *algo;
328 ualg = nla_data(rta);
330 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
333 x->props.ealgo = algo->desc.sadb_alg_id;
335 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
339 strcpy(p->alg_name, algo->name);
341 x->geniv = algo->uinfo.encr.geniv;
345 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
348 struct xfrm_algo *ualg;
349 struct xfrm_algo_auth *p;
350 struct xfrm_algo_desc *algo;
355 ualg = nla_data(rta);
357 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
360 *props = algo->desc.sadb_alg_id;
362 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
366 strcpy(p->alg_name, algo->name);
367 p->alg_key_len = ualg->alg_key_len;
368 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
369 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
375 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
378 struct xfrm_algo_auth *p, *ualg;
379 struct xfrm_algo_desc *algo;
384 ualg = nla_data(rta);
386 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
389 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
391 *props = algo->desc.sadb_alg_id;
393 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
397 strcpy(p->alg_name, algo->name);
398 if (!p->alg_trunc_len)
399 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
405 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
407 struct xfrm_algo_aead *p, *ualg;
408 struct xfrm_algo_desc *algo;
413 ualg = nla_data(rta);
415 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
418 x->props.ealgo = algo->desc.sadb_alg_id;
420 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
424 strcpy(p->alg_name, algo->name);
426 x->geniv = algo->uinfo.aead.geniv;
430 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
433 struct xfrm_replay_state_esn *up;
436 if (!replay_esn || !rp)
440 ulen = xfrm_replay_state_esn_len(up);
442 /* Check the overall length and the internal bitmap length to avoid
443 * potential overflow. */
444 if (nla_len(rp) < (int)ulen ||
445 xfrm_replay_state_esn_len(replay_esn) != ulen ||
446 replay_esn->bmp_len != up->bmp_len)
449 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
455 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
456 struct xfrm_replay_state_esn **preplay_esn,
459 struct xfrm_replay_state_esn *p, *pp, *up;
460 unsigned int klen, ulen;
466 klen = xfrm_replay_state_esn_len(up);
467 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
469 p = kzalloc(klen, GFP_KERNEL);
473 pp = kzalloc(klen, GFP_KERNEL);
480 memcpy(pp, up, ulen);
488 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
490 unsigned int len = 0;
493 len += sizeof(struct xfrm_user_sec_ctx);
494 len += xfrm_ctx->ctx_len;
499 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
501 memcpy(&x->id, &p->id, sizeof(x->id));
502 memcpy(&x->sel, &p->sel, sizeof(x->sel));
503 memcpy(&x->lft, &p->lft, sizeof(x->lft));
504 x->props.mode = p->mode;
505 x->props.replay_window = min_t(unsigned int, p->replay_window,
506 sizeof(x->replay.bitmap) * 8);
507 x->props.reqid = p->reqid;
508 x->props.family = p->family;
509 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
510 x->props.flags = p->flags;
512 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
513 x->sel.family = p->family;
517 * someday when pfkey also has support, we could have the code
518 * somehow made shareable and move it to xfrm_state.c - JHS
521 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
524 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
525 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
526 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
527 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
528 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
529 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
532 struct xfrm_replay_state_esn *replay_esn;
533 replay_esn = nla_data(re);
534 memcpy(x->replay_esn, replay_esn,
535 xfrm_replay_state_esn_len(replay_esn));
536 memcpy(x->preplay_esn, replay_esn,
537 xfrm_replay_state_esn_len(replay_esn));
541 struct xfrm_replay_state *replay;
542 replay = nla_data(rp);
543 memcpy(&x->replay, replay, sizeof(*replay));
544 memcpy(&x->preplay, replay, sizeof(*replay));
548 struct xfrm_lifetime_cur *ltime;
549 ltime = nla_data(lt);
550 x->curlft.bytes = ltime->bytes;
551 x->curlft.packets = ltime->packets;
552 x->curlft.add_time = ltime->add_time;
553 x->curlft.use_time = ltime->use_time;
557 x->replay_maxage = nla_get_u32(et);
560 x->replay_maxdiff = nla_get_u32(rt);
563 x->mapping_maxage = nla_get_u32(mt);
566 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
568 if (attrs[XFRMA_SET_MARK]) {
569 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
570 if (attrs[XFRMA_SET_MARK_MASK])
571 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
579 static struct xfrm_state *xfrm_state_construct(struct net *net,
580 struct xfrm_usersa_info *p,
581 struct nlattr **attrs,
584 struct xfrm_state *x = xfrm_state_alloc(net);
590 copy_from_user_state(x, p);
592 if (attrs[XFRMA_ENCAP]) {
593 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
594 sizeof(*x->encap), GFP_KERNEL);
595 if (x->encap == NULL)
599 if (attrs[XFRMA_COADDR]) {
600 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
601 sizeof(*x->coaddr), GFP_KERNEL);
602 if (x->coaddr == NULL)
606 if (attrs[XFRMA_SA_EXTRA_FLAGS])
607 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
609 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
611 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
612 attrs[XFRMA_ALG_AUTH_TRUNC])))
614 if (!x->props.aalgo) {
615 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
616 attrs[XFRMA_ALG_AUTH])))
619 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
621 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
622 xfrm_calg_get_byname,
623 attrs[XFRMA_ALG_COMP])))
626 if (attrs[XFRMA_TFCPAD])
627 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
629 xfrm_mark_get(attrs, &x->mark);
631 xfrm_smark_init(attrs, &x->props.smark);
633 if (attrs[XFRMA_IF_ID]) {
634 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
641 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
645 if (attrs[XFRMA_SEC_CTX]) {
646 err = security_xfrm_state_alloc(x,
647 nla_data(attrs[XFRMA_SEC_CTX]));
652 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
653 attrs[XFRMA_REPLAY_ESN_VAL])))
657 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
658 /* sysctl_xfrm_aevent_etime is in 100ms units */
659 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
661 if ((err = xfrm_init_replay(x)))
664 /* override default values from above */
665 xfrm_update_ae_params(x, attrs, 0);
667 /* configure the hardware if offload is requested */
668 if (attrs[XFRMA_OFFLOAD_DEV]) {
669 err = xfrm_dev_state_add(net, x,
670 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
678 x->km.state = XFRM_STATE_DEAD;
685 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
686 struct nlattr **attrs)
688 struct net *net = sock_net(skb->sk);
689 struct xfrm_usersa_info *p = nlmsg_data(nlh);
690 struct xfrm_state *x;
694 err = verify_newsa_info(p, attrs);
698 x = xfrm_state_construct(net, p, attrs, &err);
703 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
704 err = xfrm_state_add(x);
706 err = xfrm_state_update(x);
708 xfrm_audit_state_add(x, err ? 0 : 1, true);
711 x->km.state = XFRM_STATE_DEAD;
712 xfrm_dev_state_delete(x);
717 if (x->km.state == XFRM_STATE_VOID)
718 x->km.state = XFRM_STATE_VALID;
720 c.seq = nlh->nlmsg_seq;
721 c.portid = nlh->nlmsg_pid;
722 c.event = nlh->nlmsg_type;
724 km_state_notify(x, &c);
730 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
731 struct xfrm_usersa_id *p,
732 struct nlattr **attrs,
735 struct xfrm_state *x = NULL;
738 u32 mark = xfrm_mark_get(attrs, &m);
740 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
742 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
744 xfrm_address_t *saddr = NULL;
746 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
753 x = xfrm_state_lookup_byaddr(net, mark,
755 p->proto, p->family);
764 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
765 struct nlattr **attrs)
767 struct net *net = sock_net(skb->sk);
768 struct xfrm_state *x;
771 struct xfrm_usersa_id *p = nlmsg_data(nlh);
773 x = xfrm_user_state_lookup(net, p, attrs, &err);
777 if ((err = security_xfrm_state_delete(x)) != 0)
780 if (xfrm_state_kern(x)) {
785 err = xfrm_state_delete(x);
790 c.seq = nlh->nlmsg_seq;
791 c.portid = nlh->nlmsg_pid;
792 c.event = nlh->nlmsg_type;
793 km_state_notify(x, &c);
796 xfrm_audit_state_delete(x, err ? 0 : 1, true);
801 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
803 memset(p, 0, sizeof(*p));
804 memcpy(&p->id, &x->id, sizeof(p->id));
805 memcpy(&p->sel, &x->sel, sizeof(p->sel));
806 memcpy(&p->lft, &x->lft, sizeof(p->lft));
807 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
808 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
809 put_unaligned(x->stats.replay, &p->stats.replay);
810 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
811 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
812 p->mode = x->props.mode;
813 p->replay_window = x->props.replay_window;
814 p->reqid = x->props.reqid;
815 p->family = x->props.family;
816 p->flags = x->props.flags;
820 struct xfrm_dump_info {
821 struct sk_buff *in_skb;
822 struct sk_buff *out_skb;
827 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
829 struct xfrm_user_sec_ctx *uctx;
831 int ctx_size = sizeof(*uctx) + s->ctx_len;
833 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
837 uctx = nla_data(attr);
838 uctx->exttype = XFRMA_SEC_CTX;
839 uctx->len = ctx_size;
840 uctx->ctx_doi = s->ctx_doi;
841 uctx->ctx_alg = s->ctx_alg;
842 uctx->ctx_len = s->ctx_len;
843 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
848 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
850 struct xfrm_user_offload *xuo;
853 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
857 xuo = nla_data(attr);
858 memset(xuo, 0, sizeof(*xuo));
859 xuo->ifindex = xso->dev->ifindex;
860 xuo->flags = xso->flags;
865 static bool xfrm_redact(void)
867 return IS_ENABLED(CONFIG_SECURITY) &&
868 security_locked_down(LOCKDOWN_XFRM_SECRET);
871 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
873 struct xfrm_algo *algo;
874 struct xfrm_algo_auth *ap;
876 bool redact_secret = xfrm_redact();
878 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
879 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
882 algo = nla_data(nla);
883 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
885 if (redact_secret && auth->alg_key_len)
886 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
888 memcpy(algo->alg_key, auth->alg_key,
889 (auth->alg_key_len + 7) / 8);
890 algo->alg_key_len = auth->alg_key_len;
892 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
896 memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
897 if (redact_secret && auth->alg_key_len)
898 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
900 memcpy(ap->alg_key, auth->alg_key,
901 (auth->alg_key_len + 7) / 8);
905 static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
907 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
908 struct xfrm_algo_aead *ap;
909 bool redact_secret = xfrm_redact();
915 memcpy(ap, aead, sizeof(*aead));
917 if (redact_secret && aead->alg_key_len)
918 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
920 memcpy(ap->alg_key, aead->alg_key,
921 (aead->alg_key_len + 7) / 8);
925 static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
927 struct xfrm_algo *ap;
928 bool redact_secret = xfrm_redact();
929 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
935 memcpy(ap, ealg, sizeof(*ealg));
937 if (redact_secret && ealg->alg_key_len)
938 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
940 memcpy(ap->alg_key, ealg->alg_key,
941 (ealg->alg_key_len + 7) / 8);
946 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
951 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
953 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
958 /* Don't change this without updating xfrm_sa_len! */
959 static int copy_to_user_state_extra(struct xfrm_state *x,
960 struct xfrm_usersa_info *p,
965 copy_to_user_state(x, p);
967 if (x->props.extra_flags) {
968 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
969 x->props.extra_flags);
975 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
980 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
986 ret = copy_to_user_aead(x->aead, skb);
991 ret = copy_to_user_auth(x->aalg, skb);
996 ret = copy_to_user_ealg(x->ealg, skb);
1001 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1006 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1011 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1015 ret = xfrm_mark_put(skb, &x->mark);
1019 ret = xfrm_smark_put(skb, &x->props.smark);
1024 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1025 xfrm_replay_state_esn_len(x->replay_esn),
1028 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1033 ret = copy_user_offload(&x->xso, skb);
1037 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1042 ret = copy_sec_ctx(x->security, skb);
1046 if (x->mapping_maxage)
1047 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
1052 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1054 struct xfrm_dump_info *sp = ptr;
1055 struct sk_buff *in_skb = sp->in_skb;
1056 struct sk_buff *skb = sp->out_skb;
1057 struct xfrm_translator *xtr;
1058 struct xfrm_usersa_info *p;
1059 struct nlmsghdr *nlh;
1062 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1063 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1067 p = nlmsg_data(nlh);
1069 err = copy_to_user_state_extra(x, p, skb);
1071 nlmsg_cancel(skb, nlh);
1074 nlmsg_end(skb, nlh);
1076 xtr = xfrm_get_translator();
1078 err = xtr->alloc_compat(skb, nlh);
1080 xfrm_put_translator(xtr);
1082 nlmsg_cancel(skb, nlh);
1090 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1092 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1093 struct sock *sk = cb->skb->sk;
1094 struct net *net = sock_net(sk);
1097 xfrm_state_walk_done(walk, net);
1101 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1103 struct net *net = sock_net(skb->sk);
1104 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1105 struct xfrm_dump_info info;
1107 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1108 sizeof(cb->args) - sizeof(cb->args[0]));
1110 info.in_skb = cb->skb;
1112 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1113 info.nlmsg_flags = NLM_F_MULTI;
1116 struct nlattr *attrs[XFRMA_MAX+1];
1117 struct xfrm_address_filter *filter = NULL;
1121 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1122 xfrma_policy, cb->extack);
1126 if (attrs[XFRMA_ADDRESS_FILTER]) {
1127 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1128 sizeof(*filter), GFP_KERNEL);
1133 if (attrs[XFRMA_PROTO])
1134 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1136 xfrm_state_walk_init(walk, proto, filter);
1140 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1145 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1146 struct xfrm_state *x, u32 seq)
1148 struct xfrm_dump_info info;
1149 struct sk_buff *skb;
1152 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1154 return ERR_PTR(-ENOMEM);
1156 info.in_skb = in_skb;
1158 info.nlmsg_seq = seq;
1159 info.nlmsg_flags = 0;
1161 err = dump_one_state(x, 0, &info);
1164 return ERR_PTR(err);
1170 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1171 * Must be called with RCU read lock.
1173 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1174 u32 pid, unsigned int group)
1176 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1177 struct xfrm_translator *xtr;
1184 xtr = xfrm_get_translator();
1186 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1188 xfrm_put_translator(xtr);
1195 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1198 static inline unsigned int xfrm_spdinfo_msgsize(void)
1200 return NLMSG_ALIGN(4)
1201 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1202 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1203 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1204 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1207 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1208 u32 portid, u32 seq, u32 flags)
1210 struct xfrmk_spdinfo si;
1211 struct xfrmu_spdinfo spc;
1212 struct xfrmu_spdhinfo sph;
1213 struct xfrmu_spdhthresh spt4, spt6;
1214 struct nlmsghdr *nlh;
1219 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1220 if (nlh == NULL) /* shouldn't really happen ... */
1223 f = nlmsg_data(nlh);
1225 xfrm_spd_getinfo(net, &si);
1226 spc.incnt = si.incnt;
1227 spc.outcnt = si.outcnt;
1228 spc.fwdcnt = si.fwdcnt;
1229 spc.inscnt = si.inscnt;
1230 spc.outscnt = si.outscnt;
1231 spc.fwdscnt = si.fwdscnt;
1232 sph.spdhcnt = si.spdhcnt;
1233 sph.spdhmcnt = si.spdhmcnt;
1236 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1238 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1239 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1240 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1241 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1242 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1244 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1246 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1248 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1250 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1252 nlmsg_cancel(skb, nlh);
1256 nlmsg_end(skb, nlh);
1260 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1261 struct nlattr **attrs)
1263 struct net *net = sock_net(skb->sk);
1264 struct xfrmu_spdhthresh *thresh4 = NULL;
1265 struct xfrmu_spdhthresh *thresh6 = NULL;
1267 /* selector prefixlen thresholds to hash policies */
1268 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1269 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1271 if (nla_len(rta) < sizeof(*thresh4))
1273 thresh4 = nla_data(rta);
1274 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1277 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1278 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1280 if (nla_len(rta) < sizeof(*thresh6))
1282 thresh6 = nla_data(rta);
1283 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1287 if (thresh4 || thresh6) {
1288 write_seqlock(&net->xfrm.policy_hthresh.lock);
1290 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1291 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1294 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1295 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1297 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1299 xfrm_policy_hash_rebuild(net);
1305 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1306 struct nlattr **attrs)
1308 struct net *net = sock_net(skb->sk);
1309 struct sk_buff *r_skb;
1310 u32 *flags = nlmsg_data(nlh);
1311 u32 sportid = NETLINK_CB(skb).portid;
1312 u32 seq = nlh->nlmsg_seq;
1315 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1319 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1322 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1325 static inline unsigned int xfrm_sadinfo_msgsize(void)
1327 return NLMSG_ALIGN(4)
1328 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1329 + nla_total_size(4); /* XFRMA_SAD_CNT */
1332 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1333 u32 portid, u32 seq, u32 flags)
1335 struct xfrmk_sadinfo si;
1336 struct xfrmu_sadhinfo sh;
1337 struct nlmsghdr *nlh;
1341 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1342 if (nlh == NULL) /* shouldn't really happen ... */
1345 f = nlmsg_data(nlh);
1347 xfrm_sad_getinfo(net, &si);
1349 sh.sadhmcnt = si.sadhmcnt;
1350 sh.sadhcnt = si.sadhcnt;
1352 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1354 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1356 nlmsg_cancel(skb, nlh);
1360 nlmsg_end(skb, nlh);
1364 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1365 struct nlattr **attrs)
1367 struct net *net = sock_net(skb->sk);
1368 struct sk_buff *r_skb;
1369 u32 *flags = nlmsg_data(nlh);
1370 u32 sportid = NETLINK_CB(skb).portid;
1371 u32 seq = nlh->nlmsg_seq;
1374 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1378 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1381 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1384 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1385 struct nlattr **attrs)
1387 struct net *net = sock_net(skb->sk);
1388 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1389 struct xfrm_state *x;
1390 struct sk_buff *resp_skb;
1393 x = xfrm_user_state_lookup(net, p, attrs, &err);
1397 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1398 if (IS_ERR(resp_skb)) {
1399 err = PTR_ERR(resp_skb);
1401 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1408 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1409 struct nlattr **attrs)
1411 struct net *net = sock_net(skb->sk);
1412 struct xfrm_state *x;
1413 struct xfrm_userspi_info *p;
1414 struct xfrm_translator *xtr;
1415 struct sk_buff *resp_skb;
1416 xfrm_address_t *daddr;
1423 p = nlmsg_data(nlh);
1424 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1428 family = p->info.family;
1429 daddr = &p->info.id.daddr;
1433 mark = xfrm_mark_get(attrs, &m);
1435 if (attrs[XFRMA_IF_ID]) {
1436 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1444 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1445 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1452 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1453 if_id, p->info.id.proto, daddr,
1460 err = xfrm_alloc_spi(x, p->min, p->max);
1464 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1465 if (IS_ERR(resp_skb)) {
1466 err = PTR_ERR(resp_skb);
1470 xtr = xfrm_get_translator();
1472 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1474 xfrm_put_translator(xtr);
1476 kfree_skb(resp_skb);
1481 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1489 static int verify_policy_dir(u8 dir)
1492 case XFRM_POLICY_IN:
1493 case XFRM_POLICY_OUT:
1494 case XFRM_POLICY_FWD:
1504 static int verify_policy_type(u8 type)
1507 case XFRM_POLICY_TYPE_MAIN:
1508 #ifdef CONFIG_XFRM_SUB_POLICY
1509 case XFRM_POLICY_TYPE_SUB:
1520 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1525 case XFRM_SHARE_ANY:
1526 case XFRM_SHARE_SESSION:
1527 case XFRM_SHARE_USER:
1528 case XFRM_SHARE_UNIQUE:
1535 switch (p->action) {
1536 case XFRM_POLICY_ALLOW:
1537 case XFRM_POLICY_BLOCK:
1544 switch (p->sel.family) {
1546 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1552 #if IS_ENABLED(CONFIG_IPV6)
1553 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1558 return -EAFNOSUPPORT;
1565 ret = verify_policy_dir(p->dir);
1568 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1574 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1576 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1577 struct xfrm_user_sec_ctx *uctx;
1582 uctx = nla_data(rt);
1583 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1586 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1592 for (i = 0; i < nr; i++, ut++) {
1593 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1595 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1596 memcpy(&t->saddr, &ut->saddr,
1597 sizeof(xfrm_address_t));
1598 t->reqid = ut->reqid;
1600 t->share = ut->share;
1601 t->optional = ut->optional;
1602 t->aalgos = ut->aalgos;
1603 t->ealgos = ut->ealgos;
1604 t->calgos = ut->calgos;
1605 /* If all masks are ~0, then we allow all algorithms. */
1606 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1607 t->encap_family = ut->family;
1611 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1616 if (nr > XFRM_MAX_DEPTH)
1619 prev_family = family;
1621 for (i = 0; i < nr; i++) {
1622 /* We never validated the ut->family value, so many
1623 * applications simply leave it at zero. The check was
1624 * never made and ut->family was ignored because all
1625 * templates could be assumed to have the same family as
1626 * the policy itself. Now that we will have ipv4-in-ipv6
1627 * and ipv6-in-ipv4 tunnels, this is no longer true.
1630 ut[i].family = family;
1632 switch (ut[i].mode) {
1633 case XFRM_MODE_TUNNEL:
1634 case XFRM_MODE_BEET:
1637 if (ut[i].family != prev_family)
1641 if (ut[i].mode >= XFRM_MODE_MAX)
1644 prev_family = ut[i].family;
1646 switch (ut[i].family) {
1649 #if IS_ENABLED(CONFIG_IPV6)
1657 if (!xfrm_id_proto_valid(ut[i].id.proto))
1664 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1666 struct nlattr *rt = attrs[XFRMA_TMPL];
1671 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1672 int nr = nla_len(rt) / sizeof(*utmpl);
1675 err = validate_tmpl(nr, utmpl, pol->family);
1679 copy_templates(pol, utmpl, nr);
1684 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1686 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1687 struct xfrm_userpolicy_type *upt;
1688 u8 type = XFRM_POLICY_TYPE_MAIN;
1696 err = verify_policy_type(type);
1704 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1706 xp->priority = p->priority;
1707 xp->index = p->index;
1708 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1709 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1710 xp->action = p->action;
1711 xp->flags = p->flags;
1712 xp->family = p->sel.family;
1713 /* XXX xp->share = p->share; */
1716 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1718 memset(p, 0, sizeof(*p));
1719 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1720 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1721 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1722 p->priority = xp->priority;
1723 p->index = xp->index;
1724 p->sel.family = xp->family;
1726 p->action = xp->action;
1727 p->flags = xp->flags;
1728 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1731 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1733 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1741 copy_from_user_policy(xp, p);
1743 err = copy_from_user_policy_type(&xp->type, attrs);
1747 if (!(err = copy_from_user_tmpl(xp, attrs)))
1748 err = copy_from_user_sec_ctx(xp, attrs);
1752 xfrm_mark_get(attrs, &xp->mark);
1754 if (attrs[XFRMA_IF_ID]) {
1755 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1766 xfrm_policy_destroy(xp);
1770 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1771 struct nlattr **attrs)
1773 struct net *net = sock_net(skb->sk);
1774 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1775 struct xfrm_policy *xp;
1780 err = verify_newpolicy_info(p);
1783 err = verify_sec_ctx_len(attrs);
1787 xp = xfrm_policy_construct(net, p, attrs, &err);
1791 /* shouldn't excl be based on nlh flags??
1792 * Aha! this is anti-netlink really i.e more pfkey derived
1793 * in netlink excl is a flag and you wouldn't need
1794 * a type XFRM_MSG_UPDPOLICY - JHS */
1795 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1796 err = xfrm_policy_insert(p->dir, xp, excl);
1797 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1800 security_xfrm_policy_free(xp->security);
1805 c.event = nlh->nlmsg_type;
1806 c.seq = nlh->nlmsg_seq;
1807 c.portid = nlh->nlmsg_pid;
1808 km_policy_notify(xp, p->dir, &c);
1815 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1817 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1820 if (xp->xfrm_nr == 0)
1823 for (i = 0; i < xp->xfrm_nr; i++) {
1824 struct xfrm_user_tmpl *up = &vec[i];
1825 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1827 memset(up, 0, sizeof(*up));
1828 memcpy(&up->id, &kp->id, sizeof(up->id));
1829 up->family = kp->encap_family;
1830 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1831 up->reqid = kp->reqid;
1832 up->mode = kp->mode;
1833 up->share = kp->share;
1834 up->optional = kp->optional;
1835 up->aalgos = kp->aalgos;
1836 up->ealgos = kp->ealgos;
1837 up->calgos = kp->calgos;
1840 return nla_put(skb, XFRMA_TMPL,
1841 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1844 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1847 return copy_sec_ctx(x->security, skb);
1852 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1855 return copy_sec_ctx(xp->security, skb);
1858 static inline unsigned int userpolicy_type_attrsize(void)
1860 #ifdef CONFIG_XFRM_SUB_POLICY
1861 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1867 #ifdef CONFIG_XFRM_SUB_POLICY
1868 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1870 struct xfrm_userpolicy_type upt;
1872 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1873 memset(&upt, 0, sizeof(upt));
1876 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1880 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1886 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1888 struct xfrm_dump_info *sp = ptr;
1889 struct xfrm_userpolicy_info *p;
1890 struct sk_buff *in_skb = sp->in_skb;
1891 struct sk_buff *skb = sp->out_skb;
1892 struct xfrm_translator *xtr;
1893 struct nlmsghdr *nlh;
1896 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1897 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1901 p = nlmsg_data(nlh);
1902 copy_to_user_policy(xp, p, dir);
1903 err = copy_to_user_tmpl(xp, skb);
1905 err = copy_to_user_sec_ctx(xp, skb);
1907 err = copy_to_user_policy_type(xp->type, skb);
1909 err = xfrm_mark_put(skb, &xp->mark);
1911 err = xfrm_if_id_put(skb, xp->if_id);
1913 nlmsg_cancel(skb, nlh);
1916 nlmsg_end(skb, nlh);
1918 xtr = xfrm_get_translator();
1920 err = xtr->alloc_compat(skb, nlh);
1922 xfrm_put_translator(xtr);
1924 nlmsg_cancel(skb, nlh);
1932 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1934 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1935 struct net *net = sock_net(cb->skb->sk);
1937 xfrm_policy_walk_done(walk, net);
1941 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1943 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1945 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1947 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1951 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1953 struct net *net = sock_net(skb->sk);
1954 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1955 struct xfrm_dump_info info;
1957 info.in_skb = cb->skb;
1959 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1960 info.nlmsg_flags = NLM_F_MULTI;
1962 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1967 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1968 struct xfrm_policy *xp,
1971 struct xfrm_dump_info info;
1972 struct sk_buff *skb;
1975 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1977 return ERR_PTR(-ENOMEM);
1979 info.in_skb = in_skb;
1981 info.nlmsg_seq = seq;
1982 info.nlmsg_flags = 0;
1984 err = dump_one_policy(xp, dir, 0, &info);
1987 return ERR_PTR(err);
1993 static int xfrm_notify_userpolicy(struct net *net)
1995 struct xfrm_userpolicy_default *up;
1996 int len = NLMSG_ALIGN(sizeof(*up));
1997 struct nlmsghdr *nlh;
1998 struct sk_buff *skb;
2001 skb = nlmsg_new(len, GFP_ATOMIC);
2005 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
2011 up = nlmsg_data(nlh);
2012 up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
2013 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2014 up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2015 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2016 up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2017 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2019 nlmsg_end(skb, nlh);
2022 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2028 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2029 struct nlattr **attrs)
2031 struct net *net = sock_net(skb->sk);
2032 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2034 if (up->in == XFRM_USERPOLICY_BLOCK)
2035 net->xfrm.policy_default |= XFRM_POL_DEFAULT_IN;
2036 else if (up->in == XFRM_USERPOLICY_ACCEPT)
2037 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_IN;
2039 if (up->fwd == XFRM_USERPOLICY_BLOCK)
2040 net->xfrm.policy_default |= XFRM_POL_DEFAULT_FWD;
2041 else if (up->fwd == XFRM_USERPOLICY_ACCEPT)
2042 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_FWD;
2044 if (up->out == XFRM_USERPOLICY_BLOCK)
2045 net->xfrm.policy_default |= XFRM_POL_DEFAULT_OUT;
2046 else if (up->out == XFRM_USERPOLICY_ACCEPT)
2047 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_OUT;
2049 rt_genid_bump_all(net);
2051 xfrm_notify_userpolicy(net);
2055 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2056 struct nlattr **attrs)
2058 struct sk_buff *r_skb;
2059 struct nlmsghdr *r_nlh;
2060 struct net *net = sock_net(skb->sk);
2061 struct xfrm_userpolicy_default *r_up;
2062 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2063 u32 portid = NETLINK_CB(skb).portid;
2064 u32 seq = nlh->nlmsg_seq;
2066 r_skb = nlmsg_new(len, GFP_ATOMIC);
2070 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2076 r_up = nlmsg_data(r_nlh);
2078 r_up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
2079 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2080 r_up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2081 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2082 r_up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2083 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2084 nlmsg_end(r_skb, r_nlh);
2086 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2089 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2090 struct nlattr **attrs)
2092 struct net *net = sock_net(skb->sk);
2093 struct xfrm_policy *xp;
2094 struct xfrm_userpolicy_id *p;
2095 u8 type = XFRM_POLICY_TYPE_MAIN;
2102 p = nlmsg_data(nlh);
2103 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2105 err = copy_from_user_policy_type(&type, attrs);
2109 err = verify_policy_dir(p->dir);
2113 if (attrs[XFRMA_IF_ID])
2114 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2116 xfrm_mark_get(attrs, &m);
2119 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2120 p->index, delete, &err);
2122 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2123 struct xfrm_sec_ctx *ctx;
2125 err = verify_sec_ctx_len(attrs);
2131 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2133 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2137 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2138 &p->sel, ctx, delete, &err);
2139 security_xfrm_policy_free(ctx);
2145 struct sk_buff *resp_skb;
2147 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2148 if (IS_ERR(resp_skb)) {
2149 err = PTR_ERR(resp_skb);
2151 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
2152 NETLINK_CB(skb).portid);
2155 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2160 c.data.byid = p->index;
2161 c.event = nlh->nlmsg_type;
2162 c.seq = nlh->nlmsg_seq;
2163 c.portid = nlh->nlmsg_pid;
2164 km_policy_notify(xp, p->dir, &c);
2172 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2173 struct nlattr **attrs)
2175 struct net *net = sock_net(skb->sk);
2177 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2180 err = xfrm_state_flush(net, p->proto, true, false);
2182 if (err == -ESRCH) /* empty table */
2186 c.data.proto = p->proto;
2187 c.event = nlh->nlmsg_type;
2188 c.seq = nlh->nlmsg_seq;
2189 c.portid = nlh->nlmsg_pid;
2191 km_state_notify(NULL, &c);
2196 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2198 unsigned int replay_size = x->replay_esn ?
2199 xfrm_replay_state_esn_len(x->replay_esn) :
2200 sizeof(struct xfrm_replay_state);
2202 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2203 + nla_total_size(replay_size)
2204 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2205 + nla_total_size(sizeof(struct xfrm_mark))
2206 + nla_total_size(4) /* XFRM_AE_RTHR */
2207 + nla_total_size(4); /* XFRM_AE_ETHR */
2210 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2212 struct xfrm_aevent_id *id;
2213 struct nlmsghdr *nlh;
2216 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2220 id = nlmsg_data(nlh);
2221 memset(&id->sa_id, 0, sizeof(id->sa_id));
2222 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2223 id->sa_id.spi = x->id.spi;
2224 id->sa_id.family = x->props.family;
2225 id->sa_id.proto = x->id.proto;
2226 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2227 id->reqid = x->props.reqid;
2228 id->flags = c->data.aevent;
2230 if (x->replay_esn) {
2231 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2232 xfrm_replay_state_esn_len(x->replay_esn),
2235 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2240 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2245 if (id->flags & XFRM_AE_RTHR) {
2246 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2250 if (id->flags & XFRM_AE_ETHR) {
2251 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2252 x->replay_maxage * 10 / HZ);
2256 err = xfrm_mark_put(skb, &x->mark);
2260 err = xfrm_if_id_put(skb, x->if_id);
2264 nlmsg_end(skb, nlh);
2268 nlmsg_cancel(skb, nlh);
2272 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2273 struct nlattr **attrs)
2275 struct net *net = sock_net(skb->sk);
2276 struct xfrm_state *x;
2277 struct sk_buff *r_skb;
2282 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2283 struct xfrm_usersa_id *id = &p->sa_id;
2285 mark = xfrm_mark_get(attrs, &m);
2287 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2291 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2292 if (r_skb == NULL) {
2298 * XXX: is this lock really needed - none of the other
2299 * gets lock (the concern is things getting updated
2300 * while we are still reading) - jhs
2302 spin_lock_bh(&x->lock);
2303 c.data.aevent = p->flags;
2304 c.seq = nlh->nlmsg_seq;
2305 c.portid = nlh->nlmsg_pid;
2307 err = build_aevent(r_skb, x, &c);
2310 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2311 spin_unlock_bh(&x->lock);
2316 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2317 struct nlattr **attrs)
2319 struct net *net = sock_net(skb->sk);
2320 struct xfrm_state *x;
2325 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2326 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2327 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2328 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2329 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2330 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2332 if (!lt && !rp && !re && !et && !rt)
2335 /* pedantic mode - thou shalt sayeth replaceth */
2336 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2339 mark = xfrm_mark_get(attrs, &m);
2341 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2345 if (x->km.state != XFRM_STATE_VALID)
2348 err = xfrm_replay_verify_len(x->replay_esn, re);
2352 spin_lock_bh(&x->lock);
2353 xfrm_update_ae_params(x, attrs, 1);
2354 spin_unlock_bh(&x->lock);
2356 c.event = nlh->nlmsg_type;
2357 c.seq = nlh->nlmsg_seq;
2358 c.portid = nlh->nlmsg_pid;
2359 c.data.aevent = XFRM_AE_CU;
2360 km_state_notify(x, &c);
2367 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2368 struct nlattr **attrs)
2370 struct net *net = sock_net(skb->sk);
2372 u8 type = XFRM_POLICY_TYPE_MAIN;
2375 err = copy_from_user_policy_type(&type, attrs);
2379 err = xfrm_policy_flush(net, type, true);
2381 if (err == -ESRCH) /* empty table */
2387 c.event = nlh->nlmsg_type;
2388 c.seq = nlh->nlmsg_seq;
2389 c.portid = nlh->nlmsg_pid;
2391 km_policy_notify(NULL, 0, &c);
2395 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2396 struct nlattr **attrs)
2398 struct net *net = sock_net(skb->sk);
2399 struct xfrm_policy *xp;
2400 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2401 struct xfrm_userpolicy_info *p = &up->pol;
2402 u8 type = XFRM_POLICY_TYPE_MAIN;
2407 err = copy_from_user_policy_type(&type, attrs);
2411 err = verify_policy_dir(p->dir);
2415 if (attrs[XFRMA_IF_ID])
2416 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2418 xfrm_mark_get(attrs, &m);
2421 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2424 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2425 struct xfrm_sec_ctx *ctx;
2427 err = verify_sec_ctx_len(attrs);
2433 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2435 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2439 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2440 &p->sel, ctx, 0, &err);
2441 security_xfrm_policy_free(ctx);
2446 if (unlikely(xp->walk.dead))
2451 xfrm_policy_delete(xp, p->dir);
2452 xfrm_audit_policy_delete(xp, 1, true);
2454 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2461 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2462 struct nlattr **attrs)
2464 struct net *net = sock_net(skb->sk);
2465 struct xfrm_state *x;
2467 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2468 struct xfrm_usersa_info *p = &ue->state;
2470 u32 mark = xfrm_mark_get(attrs, &m);
2472 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2478 spin_lock_bh(&x->lock);
2480 if (x->km.state != XFRM_STATE_VALID)
2482 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2485 __xfrm_state_delete(x);
2486 xfrm_audit_state_delete(x, 1, true);
2490 spin_unlock_bh(&x->lock);
2495 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2496 struct nlattr **attrs)
2498 struct net *net = sock_net(skb->sk);
2499 struct xfrm_policy *xp;
2500 struct xfrm_user_tmpl *ut;
2502 struct nlattr *rt = attrs[XFRMA_TMPL];
2503 struct xfrm_mark mark;
2505 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2506 struct xfrm_state *x = xfrm_state_alloc(net);
2512 xfrm_mark_get(attrs, &mark);
2514 err = verify_newpolicy_info(&ua->policy);
2517 err = verify_sec_ctx_len(attrs);
2522 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2526 memcpy(&x->id, &ua->id, sizeof(ua->id));
2527 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2528 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2529 xp->mark.m = x->mark.m = mark.m;
2530 xp->mark.v = x->mark.v = mark.v;
2532 /* extract the templates and for each call km_key */
2533 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2534 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2535 memcpy(&x->id, &t->id, sizeof(x->id));
2536 x->props.mode = t->mode;
2537 x->props.reqid = t->reqid;
2538 x->props.family = ut->family;
2539 t->aalgos = ua->aalgos;
2540 t->ealgos = ua->ealgos;
2541 t->calgos = ua->calgos;
2542 err = km_query(x, t, xp);
2557 #ifdef CONFIG_XFRM_MIGRATE
2558 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2559 struct xfrm_kmaddress *k,
2560 struct nlattr **attrs, int *num)
2562 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2563 struct xfrm_user_migrate *um;
2567 struct xfrm_user_kmaddress *uk;
2569 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2570 memcpy(&k->local, &uk->local, sizeof(k->local));
2571 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2572 k->family = uk->family;
2573 k->reserved = uk->reserved;
2577 num_migrate = nla_len(rt) / sizeof(*um);
2579 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2582 for (i = 0; i < num_migrate; i++, um++, ma++) {
2583 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2584 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2585 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2586 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2588 ma->proto = um->proto;
2589 ma->mode = um->mode;
2590 ma->reqid = um->reqid;
2592 ma->old_family = um->old_family;
2593 ma->new_family = um->new_family;
2600 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2601 struct nlattr **attrs)
2603 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2604 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2605 struct xfrm_kmaddress km, *kmp;
2609 struct net *net = sock_net(skb->sk);
2610 struct xfrm_encap_tmpl *encap = NULL;
2613 if (attrs[XFRMA_MIGRATE] == NULL)
2616 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2618 err = copy_from_user_policy_type(&type, attrs);
2622 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2629 if (attrs[XFRMA_ENCAP]) {
2630 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2631 sizeof(*encap), GFP_KERNEL);
2636 if (attrs[XFRMA_IF_ID])
2637 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2639 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
2646 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2647 struct nlattr **attrs)
2649 return -ENOPROTOOPT;
2653 #ifdef CONFIG_XFRM_MIGRATE
2654 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2656 struct xfrm_user_migrate um;
2658 memset(&um, 0, sizeof(um));
2659 um.proto = m->proto;
2661 um.reqid = m->reqid;
2662 um.old_family = m->old_family;
2663 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2664 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2665 um.new_family = m->new_family;
2666 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2667 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2669 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2672 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2674 struct xfrm_user_kmaddress uk;
2676 memset(&uk, 0, sizeof(uk));
2677 uk.family = k->family;
2678 uk.reserved = k->reserved;
2679 memcpy(&uk.local, &k->local, sizeof(uk.local));
2680 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2682 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2685 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2688 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2689 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2690 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2691 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2692 + userpolicy_type_attrsize();
2695 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2696 int num_migrate, const struct xfrm_kmaddress *k,
2697 const struct xfrm_selector *sel,
2698 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2700 const struct xfrm_migrate *mp;
2701 struct xfrm_userpolicy_id *pol_id;
2702 struct nlmsghdr *nlh;
2705 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2709 pol_id = nlmsg_data(nlh);
2710 /* copy data from selector, dir, and type to the pol_id */
2711 memset(pol_id, 0, sizeof(*pol_id));
2712 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2716 err = copy_to_user_kmaddress(k, skb);
2721 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2725 err = copy_to_user_policy_type(type, skb);
2728 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2729 err = copy_to_user_migrate(mp, skb);
2734 nlmsg_end(skb, nlh);
2738 nlmsg_cancel(skb, nlh);
2742 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2743 const struct xfrm_migrate *m, int num_migrate,
2744 const struct xfrm_kmaddress *k,
2745 const struct xfrm_encap_tmpl *encap)
2747 struct net *net = &init_net;
2748 struct sk_buff *skb;
2751 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2757 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2760 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2763 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2764 const struct xfrm_migrate *m, int num_migrate,
2765 const struct xfrm_kmaddress *k,
2766 const struct xfrm_encap_tmpl *encap)
2768 return -ENOPROTOOPT;
2772 #define XMSGSIZE(type) sizeof(struct type)
2774 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2775 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2776 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2777 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2778 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2779 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2780 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2781 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2782 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2783 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2784 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2785 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2786 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2787 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2788 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2789 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2790 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2791 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2792 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2793 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2794 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2795 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2796 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2797 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2799 EXPORT_SYMBOL_GPL(xfrm_msg_min);
2803 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2804 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2805 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2806 [XFRMA_LASTUSED] = { .type = NLA_U64},
2807 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2808 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2809 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2810 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2811 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2812 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2813 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2814 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2815 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2816 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2817 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2818 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2819 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2820 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2821 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2822 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2823 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2824 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2825 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2826 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2827 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2828 [XFRMA_PROTO] = { .type = NLA_U8 },
2829 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2830 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2831 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2832 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2833 [XFRMA_IF_ID] = { .type = NLA_U32 },
2835 EXPORT_SYMBOL_GPL(xfrma_policy);
2837 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2838 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2839 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2842 static const struct xfrm_link {
2843 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2844 int (*start)(struct netlink_callback *);
2845 int (*dump)(struct sk_buff *, struct netlink_callback *);
2846 int (*done)(struct netlink_callback *);
2847 const struct nla_policy *nla_pol;
2849 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2850 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2851 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2852 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2853 .dump = xfrm_dump_sa,
2854 .done = xfrm_dump_sa_done },
2855 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2856 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2857 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2858 .start = xfrm_dump_policy_start,
2859 .dump = xfrm_dump_policy,
2860 .done = xfrm_dump_policy_done },
2861 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2862 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2863 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2864 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2865 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2866 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2867 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2868 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2869 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2870 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2871 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2872 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2873 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2874 .nla_pol = xfrma_spd_policy,
2875 .nla_max = XFRMA_SPD_MAX },
2876 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2877 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
2878 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
2881 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2882 struct netlink_ext_ack *extack)
2884 struct net *net = sock_net(skb->sk);
2885 struct nlattr *attrs[XFRMA_MAX+1];
2886 const struct xfrm_link *link;
2887 struct nlmsghdr *nlh64 = NULL;
2890 type = nlh->nlmsg_type;
2891 if (type > XFRM_MSG_MAX)
2894 type -= XFRM_MSG_BASE;
2895 link = &xfrm_dispatch[type];
2897 /* All operations require privileges, even GET */
2898 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2901 if (in_compat_syscall()) {
2902 struct xfrm_translator *xtr = xfrm_get_translator();
2907 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2908 link->nla_pol, extack);
2909 xfrm_put_translator(xtr);
2911 return PTR_ERR(nlh64);
2916 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2917 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2918 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2919 struct netlink_dump_control c = {
2920 .start = link->start,
2925 if (link->dump == NULL) {
2930 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2934 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2935 link->nla_max ? : XFRMA_MAX,
2936 link->nla_pol ? : xfrma_policy, extack);
2940 if (link->doit == NULL) {
2945 err = link->doit(skb, nlh, attrs);
2947 /* We need to free skb allocated in xfrm_alloc_compat() before
2948 * returning from this function, because consume_skb() won't take
2949 * care of frag_list since netlink destructor sets
2950 * sbk->head to NULL. (see netlink_skb_destructor())
2952 if (skb_has_frag_list(skb)) {
2953 kfree_skb(skb_shinfo(skb)->frag_list);
2954 skb_shinfo(skb)->frag_list = NULL;
2962 static void xfrm_netlink_rcv(struct sk_buff *skb)
2964 struct net *net = sock_net(skb->sk);
2966 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2967 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2968 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2971 static inline unsigned int xfrm_expire_msgsize(void)
2973 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2974 + nla_total_size(sizeof(struct xfrm_mark));
2977 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2979 struct xfrm_user_expire *ue;
2980 struct nlmsghdr *nlh;
2983 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2987 ue = nlmsg_data(nlh);
2988 copy_to_user_state(x, &ue->state);
2989 ue->hard = (c->data.hard != 0) ? 1 : 0;
2990 /* clear the padding bytes */
2991 memset_after(ue, 0, hard);
2993 err = xfrm_mark_put(skb, &x->mark);
2997 err = xfrm_if_id_put(skb, x->if_id);
3001 nlmsg_end(skb, nlh);
3005 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
3007 struct net *net = xs_net(x);
3008 struct sk_buff *skb;
3010 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
3014 if (build_expire(skb, x, c) < 0) {
3019 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3022 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3024 struct net *net = xs_net(x);
3025 struct sk_buff *skb;
3028 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3032 err = build_aevent(skb, x, c);
3035 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3038 static int xfrm_notify_sa_flush(const struct km_event *c)
3040 struct net *net = c->net;
3041 struct xfrm_usersa_flush *p;
3042 struct nlmsghdr *nlh;
3043 struct sk_buff *skb;
3044 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
3046 skb = nlmsg_new(len, GFP_ATOMIC);
3050 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
3056 p = nlmsg_data(nlh);
3057 p->proto = c->data.proto;
3059 nlmsg_end(skb, nlh);
3061 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3064 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
3068 l += nla_total_size(aead_len(x->aead));
3070 l += nla_total_size(sizeof(struct xfrm_algo) +
3071 (x->aalg->alg_key_len + 7) / 8);
3072 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3075 l += nla_total_size(xfrm_alg_len(x->ealg));
3077 l += nla_total_size(sizeof(*x->calg));
3079 l += nla_total_size(sizeof(*x->encap));
3081 l += nla_total_size(sizeof(x->tfcpad));
3083 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3085 l += nla_total_size(sizeof(struct xfrm_replay_state));
3087 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3088 x->security->ctx_len);
3090 l += nla_total_size(sizeof(*x->coaddr));
3091 if (x->props.extra_flags)
3092 l += nla_total_size(sizeof(x->props.extra_flags));
3094 l += nla_total_size(sizeof(struct xfrm_user_offload));
3095 if (x->props.smark.v | x->props.smark.m) {
3096 l += nla_total_size(sizeof(x->props.smark.v));
3097 l += nla_total_size(sizeof(x->props.smark.m));
3100 l += nla_total_size(sizeof(x->if_id));
3102 /* Must count x->lastused as it may become non-zero behind our back. */
3103 l += nla_total_size_64bit(sizeof(u64));
3105 if (x->mapping_maxage)
3106 l += nla_total_size(sizeof(x->mapping_maxage));
3111 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
3113 struct net *net = xs_net(x);
3114 struct xfrm_usersa_info *p;
3115 struct xfrm_usersa_id *id;
3116 struct nlmsghdr *nlh;
3117 struct sk_buff *skb;
3118 unsigned int len = xfrm_sa_len(x);
3119 unsigned int headlen;
3122 headlen = sizeof(*p);
3123 if (c->event == XFRM_MSG_DELSA) {
3124 len += nla_total_size(headlen);
3125 headlen = sizeof(*id);
3126 len += nla_total_size(sizeof(struct xfrm_mark));
3128 len += NLMSG_ALIGN(headlen);
3130 skb = nlmsg_new(len, GFP_ATOMIC);
3134 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3139 p = nlmsg_data(nlh);
3140 if (c->event == XFRM_MSG_DELSA) {
3141 struct nlattr *attr;
3143 id = nlmsg_data(nlh);
3144 memset(id, 0, sizeof(*id));
3145 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3146 id->spi = x->id.spi;
3147 id->family = x->props.family;
3148 id->proto = x->id.proto;
3150 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
3157 err = copy_to_user_state_extra(x, p, skb);
3161 nlmsg_end(skb, nlh);
3163 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3170 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
3174 case XFRM_MSG_EXPIRE:
3175 return xfrm_exp_state_notify(x, c);
3176 case XFRM_MSG_NEWAE:
3177 return xfrm_aevent_state_notify(x, c);
3178 case XFRM_MSG_DELSA:
3179 case XFRM_MSG_UPDSA:
3180 case XFRM_MSG_NEWSA:
3181 return xfrm_notify_sa(x, c);
3182 case XFRM_MSG_FLUSHSA:
3183 return xfrm_notify_sa_flush(c);
3185 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3194 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3195 struct xfrm_policy *xp)
3197 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3198 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3199 + nla_total_size(sizeof(struct xfrm_mark))
3200 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3201 + userpolicy_type_attrsize();
3204 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
3205 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3207 __u32 seq = xfrm_get_acqseq();
3208 struct xfrm_user_acquire *ua;
3209 struct nlmsghdr *nlh;
3212 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3216 ua = nlmsg_data(nlh);
3217 memcpy(&ua->id, &x->id, sizeof(ua->id));
3218 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3219 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3220 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3221 ua->aalgos = xt->aalgos;
3222 ua->ealgos = xt->ealgos;
3223 ua->calgos = xt->calgos;
3224 ua->seq = x->km.seq = seq;
3226 err = copy_to_user_tmpl(xp, skb);
3228 err = copy_to_user_state_sec_ctx(x, skb);
3230 err = copy_to_user_policy_type(xp->type, skb);
3232 err = xfrm_mark_put(skb, &xp->mark);
3234 err = xfrm_if_id_put(skb, xp->if_id);
3236 nlmsg_cancel(skb, nlh);
3240 nlmsg_end(skb, nlh);
3244 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3245 struct xfrm_policy *xp)
3247 struct net *net = xs_net(x);
3248 struct sk_buff *skb;
3251 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3255 err = build_acquire(skb, x, xt, xp);
3258 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3261 /* User gives us xfrm_user_policy_info followed by an array of 0
3262 * or more templates.
3264 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3265 u8 *data, int len, int *dir)
3267 struct net *net = sock_net(sk);
3268 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3269 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3270 struct xfrm_policy *xp;
3273 switch (sk->sk_family) {
3275 if (opt != IP_XFRM_POLICY) {
3280 #if IS_ENABLED(CONFIG_IPV6)
3282 if (opt != IPV6_XFRM_POLICY) {
3295 if (len < sizeof(*p) ||
3296 verify_newpolicy_info(p))
3299 nr = ((len - sizeof(*p)) / sizeof(*ut));
3300 if (validate_tmpl(nr, ut, p->sel.family))
3303 if (p->dir > XFRM_POLICY_OUT)
3306 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3312 copy_from_user_policy(xp, p);
3313 xp->type = XFRM_POLICY_TYPE_MAIN;
3314 copy_templates(xp, ut, nr);
3321 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3323 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3324 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3325 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3326 + nla_total_size(sizeof(struct xfrm_mark))
3327 + userpolicy_type_attrsize();
3330 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3331 int dir, const struct km_event *c)
3333 struct xfrm_user_polexpire *upe;
3334 int hard = c->data.hard;
3335 struct nlmsghdr *nlh;
3338 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3342 upe = nlmsg_data(nlh);
3343 copy_to_user_policy(xp, &upe->pol, dir);
3344 err = copy_to_user_tmpl(xp, skb);
3346 err = copy_to_user_sec_ctx(xp, skb);
3348 err = copy_to_user_policy_type(xp->type, skb);
3350 err = xfrm_mark_put(skb, &xp->mark);
3352 err = xfrm_if_id_put(skb, xp->if_id);
3354 nlmsg_cancel(skb, nlh);
3359 nlmsg_end(skb, nlh);
3363 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3365 struct net *net = xp_net(xp);
3366 struct sk_buff *skb;
3369 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3373 err = build_polexpire(skb, xp, dir, c);
3376 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3379 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3381 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3382 struct net *net = xp_net(xp);
3383 struct xfrm_userpolicy_info *p;
3384 struct xfrm_userpolicy_id *id;
3385 struct nlmsghdr *nlh;
3386 struct sk_buff *skb;
3387 unsigned int headlen;
3390 headlen = sizeof(*p);
3391 if (c->event == XFRM_MSG_DELPOLICY) {
3392 len += nla_total_size(headlen);
3393 headlen = sizeof(*id);
3395 len += userpolicy_type_attrsize();
3396 len += nla_total_size(sizeof(struct xfrm_mark));
3397 len += NLMSG_ALIGN(headlen);
3399 skb = nlmsg_new(len, GFP_ATOMIC);
3403 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3408 p = nlmsg_data(nlh);
3409 if (c->event == XFRM_MSG_DELPOLICY) {
3410 struct nlattr *attr;
3412 id = nlmsg_data(nlh);
3413 memset(id, 0, sizeof(*id));
3416 id->index = xp->index;
3418 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3420 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3428 copy_to_user_policy(xp, p, dir);
3429 err = copy_to_user_tmpl(xp, skb);
3431 err = copy_to_user_policy_type(xp->type, skb);
3433 err = xfrm_mark_put(skb, &xp->mark);
3435 err = xfrm_if_id_put(skb, xp->if_id);
3439 nlmsg_end(skb, nlh);
3441 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3448 static int xfrm_notify_policy_flush(const struct km_event *c)
3450 struct net *net = c->net;
3451 struct nlmsghdr *nlh;
3452 struct sk_buff *skb;
3455 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3459 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3463 err = copy_to_user_policy_type(c->data.type, skb);
3467 nlmsg_end(skb, nlh);
3469 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3476 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3480 case XFRM_MSG_NEWPOLICY:
3481 case XFRM_MSG_UPDPOLICY:
3482 case XFRM_MSG_DELPOLICY:
3483 return xfrm_notify_policy(xp, dir, c);
3484 case XFRM_MSG_FLUSHPOLICY:
3485 return xfrm_notify_policy_flush(c);
3486 case XFRM_MSG_POLEXPIRE:
3487 return xfrm_exp_policy_notify(xp, dir, c);
3489 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3497 static inline unsigned int xfrm_report_msgsize(void)
3499 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3502 static int build_report(struct sk_buff *skb, u8 proto,
3503 struct xfrm_selector *sel, xfrm_address_t *addr)
3505 struct xfrm_user_report *ur;
3506 struct nlmsghdr *nlh;
3508 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3512 ur = nlmsg_data(nlh);
3514 memcpy(&ur->sel, sel, sizeof(ur->sel));
3517 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3519 nlmsg_cancel(skb, nlh);
3523 nlmsg_end(skb, nlh);
3527 static int xfrm_send_report(struct net *net, u8 proto,
3528 struct xfrm_selector *sel, xfrm_address_t *addr)
3530 struct sk_buff *skb;
3533 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3537 err = build_report(skb, proto, sel, addr);
3540 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3543 static inline unsigned int xfrm_mapping_msgsize(void)
3545 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3548 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3549 xfrm_address_t *new_saddr, __be16 new_sport)
3551 struct xfrm_user_mapping *um;
3552 struct nlmsghdr *nlh;
3554 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3558 um = nlmsg_data(nlh);
3560 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3561 um->id.spi = x->id.spi;
3562 um->id.family = x->props.family;
3563 um->id.proto = x->id.proto;
3564 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3565 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3566 um->new_sport = new_sport;
3567 um->old_sport = x->encap->encap_sport;
3568 um->reqid = x->props.reqid;
3570 nlmsg_end(skb, nlh);
3574 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3577 struct net *net = xs_net(x);
3578 struct sk_buff *skb;
3581 if (x->id.proto != IPPROTO_ESP)
3587 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3591 err = build_mapping(skb, x, ipaddr, sport);
3594 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3597 static bool xfrm_is_alive(const struct km_event *c)
3599 return (bool)xfrm_acquire_is_on(c->net);
3602 static struct xfrm_mgr netlink_mgr = {
3603 .notify = xfrm_send_state_notify,
3604 .acquire = xfrm_send_acquire,
3605 .compile_policy = xfrm_compile_policy,
3606 .notify_policy = xfrm_send_policy_notify,
3607 .report = xfrm_send_report,
3608 .migrate = xfrm_send_migrate,
3609 .new_mapping = xfrm_send_mapping,
3610 .is_alive = xfrm_is_alive,
3613 static int __net_init xfrm_user_net_init(struct net *net)
3616 struct netlink_kernel_cfg cfg = {
3617 .groups = XFRMNLGRP_MAX,
3618 .input = xfrm_netlink_rcv,
3621 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3624 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3625 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3629 static void __net_exit xfrm_user_net_pre_exit(struct net *net)
3631 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3634 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3638 list_for_each_entry(net, net_exit_list, exit_list)
3639 netlink_kernel_release(net->xfrm.nlsk_stash);
3642 static struct pernet_operations xfrm_user_net_ops = {
3643 .init = xfrm_user_net_init,
3644 .pre_exit = xfrm_user_net_pre_exit,
3645 .exit_batch = xfrm_user_net_exit,
3648 static int __init xfrm_user_init(void)
3652 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3654 rv = register_pernet_subsys(&xfrm_user_net_ops);
3657 rv = xfrm_register_km(&netlink_mgr);
3659 unregister_pernet_subsys(&xfrm_user_net_ops);
3663 static void __exit xfrm_user_exit(void)
3665 xfrm_unregister_km(&netlink_mgr);
3666 unregister_pernet_subsys(&xfrm_user_net_ops);
3669 module_init(xfrm_user_init);
3670 module_exit(xfrm_user_exit);
3671 MODULE_LICENSE("GPL");
3672 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);