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]);
636 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
640 if (attrs[XFRMA_SEC_CTX]) {
641 err = security_xfrm_state_alloc(x,
642 nla_data(attrs[XFRMA_SEC_CTX]));
647 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
648 attrs[XFRMA_REPLAY_ESN_VAL])))
652 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
653 /* sysctl_xfrm_aevent_etime is in 100ms units */
654 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
656 if ((err = xfrm_init_replay(x)))
659 /* override default values from above */
660 xfrm_update_ae_params(x, attrs, 0);
662 /* configure the hardware if offload is requested */
663 if (attrs[XFRMA_OFFLOAD_DEV]) {
664 err = xfrm_dev_state_add(net, x,
665 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
673 x->km.state = XFRM_STATE_DEAD;
680 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
681 struct nlattr **attrs)
683 struct net *net = sock_net(skb->sk);
684 struct xfrm_usersa_info *p = nlmsg_data(nlh);
685 struct xfrm_state *x;
689 err = verify_newsa_info(p, attrs);
693 x = xfrm_state_construct(net, p, attrs, &err);
698 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
699 err = xfrm_state_add(x);
701 err = xfrm_state_update(x);
703 xfrm_audit_state_add(x, err ? 0 : 1, true);
706 x->km.state = XFRM_STATE_DEAD;
707 xfrm_dev_state_delete(x);
712 if (x->km.state == XFRM_STATE_VOID)
713 x->km.state = XFRM_STATE_VALID;
715 c.seq = nlh->nlmsg_seq;
716 c.portid = nlh->nlmsg_pid;
717 c.event = nlh->nlmsg_type;
719 km_state_notify(x, &c);
725 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
726 struct xfrm_usersa_id *p,
727 struct nlattr **attrs,
730 struct xfrm_state *x = NULL;
733 u32 mark = xfrm_mark_get(attrs, &m);
735 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
737 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
739 xfrm_address_t *saddr = NULL;
741 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
748 x = xfrm_state_lookup_byaddr(net, mark,
750 p->proto, p->family);
759 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
760 struct nlattr **attrs)
762 struct net *net = sock_net(skb->sk);
763 struct xfrm_state *x;
766 struct xfrm_usersa_id *p = nlmsg_data(nlh);
768 x = xfrm_user_state_lookup(net, p, attrs, &err);
772 if ((err = security_xfrm_state_delete(x)) != 0)
775 if (xfrm_state_kern(x)) {
780 err = xfrm_state_delete(x);
785 c.seq = nlh->nlmsg_seq;
786 c.portid = nlh->nlmsg_pid;
787 c.event = nlh->nlmsg_type;
788 km_state_notify(x, &c);
791 xfrm_audit_state_delete(x, err ? 0 : 1, true);
796 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
798 memset(p, 0, sizeof(*p));
799 memcpy(&p->id, &x->id, sizeof(p->id));
800 memcpy(&p->sel, &x->sel, sizeof(p->sel));
801 memcpy(&p->lft, &x->lft, sizeof(p->lft));
802 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
803 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
804 put_unaligned(x->stats.replay, &p->stats.replay);
805 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
806 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
807 p->mode = x->props.mode;
808 p->replay_window = x->props.replay_window;
809 p->reqid = x->props.reqid;
810 p->family = x->props.family;
811 p->flags = x->props.flags;
815 struct xfrm_dump_info {
816 struct sk_buff *in_skb;
817 struct sk_buff *out_skb;
822 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
824 struct xfrm_user_sec_ctx *uctx;
826 int ctx_size = sizeof(*uctx) + s->ctx_len;
828 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
832 uctx = nla_data(attr);
833 uctx->exttype = XFRMA_SEC_CTX;
834 uctx->len = ctx_size;
835 uctx->ctx_doi = s->ctx_doi;
836 uctx->ctx_alg = s->ctx_alg;
837 uctx->ctx_len = s->ctx_len;
838 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
843 static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
845 struct xfrm_user_offload *xuo;
848 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
852 xuo = nla_data(attr);
853 memset(xuo, 0, sizeof(*xuo));
854 xuo->ifindex = xso->dev->ifindex;
855 if (xso->dir == XFRM_DEV_OFFLOAD_IN)
856 xuo->flags = XFRM_OFFLOAD_INBOUND;
861 static bool xfrm_redact(void)
863 return IS_ENABLED(CONFIG_SECURITY) &&
864 security_locked_down(LOCKDOWN_XFRM_SECRET);
867 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
869 struct xfrm_algo *algo;
870 struct xfrm_algo_auth *ap;
872 bool redact_secret = xfrm_redact();
874 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
875 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
878 algo = nla_data(nla);
879 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
881 if (redact_secret && auth->alg_key_len)
882 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
884 memcpy(algo->alg_key, auth->alg_key,
885 (auth->alg_key_len + 7) / 8);
886 algo->alg_key_len = auth->alg_key_len;
888 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
892 memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
893 if (redact_secret && auth->alg_key_len)
894 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
896 memcpy(ap->alg_key, auth->alg_key,
897 (auth->alg_key_len + 7) / 8);
901 static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
903 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
904 struct xfrm_algo_aead *ap;
905 bool redact_secret = xfrm_redact();
911 memcpy(ap, aead, sizeof(*aead));
913 if (redact_secret && aead->alg_key_len)
914 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
916 memcpy(ap->alg_key, aead->alg_key,
917 (aead->alg_key_len + 7) / 8);
921 static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
923 struct xfrm_algo *ap;
924 bool redact_secret = xfrm_redact();
925 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
931 memcpy(ap, ealg, sizeof(*ealg));
933 if (redact_secret && ealg->alg_key_len)
934 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
936 memcpy(ap->alg_key, ealg->alg_key,
937 (ealg->alg_key_len + 7) / 8);
942 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
947 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
949 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
954 /* Don't change this without updating xfrm_sa_len! */
955 static int copy_to_user_state_extra(struct xfrm_state *x,
956 struct xfrm_usersa_info *p,
961 copy_to_user_state(x, p);
963 if (x->props.extra_flags) {
964 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
965 x->props.extra_flags);
971 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
976 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
982 ret = copy_to_user_aead(x->aead, skb);
987 ret = copy_to_user_auth(x->aalg, skb);
992 ret = copy_to_user_ealg(x->ealg, skb);
997 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1002 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1007 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1011 ret = xfrm_mark_put(skb, &x->mark);
1015 ret = xfrm_smark_put(skb, &x->props.smark);
1020 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1021 xfrm_replay_state_esn_len(x->replay_esn),
1024 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1029 ret = copy_user_offload(&x->xso, skb);
1033 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1038 ret = copy_sec_ctx(x->security, skb);
1042 if (x->mapping_maxage)
1043 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
1048 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1050 struct xfrm_dump_info *sp = ptr;
1051 struct sk_buff *in_skb = sp->in_skb;
1052 struct sk_buff *skb = sp->out_skb;
1053 struct xfrm_translator *xtr;
1054 struct xfrm_usersa_info *p;
1055 struct nlmsghdr *nlh;
1058 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1059 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1063 p = nlmsg_data(nlh);
1065 err = copy_to_user_state_extra(x, p, skb);
1067 nlmsg_cancel(skb, nlh);
1070 nlmsg_end(skb, nlh);
1072 xtr = xfrm_get_translator();
1074 err = xtr->alloc_compat(skb, nlh);
1076 xfrm_put_translator(xtr);
1078 nlmsg_cancel(skb, nlh);
1086 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1088 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1089 struct sock *sk = cb->skb->sk;
1090 struct net *net = sock_net(sk);
1093 xfrm_state_walk_done(walk, net);
1097 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1099 struct net *net = sock_net(skb->sk);
1100 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1101 struct xfrm_dump_info info;
1103 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1104 sizeof(cb->args) - sizeof(cb->args[0]));
1106 info.in_skb = cb->skb;
1108 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1109 info.nlmsg_flags = NLM_F_MULTI;
1112 struct nlattr *attrs[XFRMA_MAX+1];
1113 struct xfrm_address_filter *filter = NULL;
1117 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1118 xfrma_policy, cb->extack);
1122 if (attrs[XFRMA_ADDRESS_FILTER]) {
1123 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1124 sizeof(*filter), GFP_KERNEL);
1129 if (attrs[XFRMA_PROTO])
1130 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1132 xfrm_state_walk_init(walk, proto, filter);
1136 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1141 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1142 struct xfrm_state *x, u32 seq)
1144 struct xfrm_dump_info info;
1145 struct sk_buff *skb;
1148 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1150 return ERR_PTR(-ENOMEM);
1152 info.in_skb = in_skb;
1154 info.nlmsg_seq = seq;
1155 info.nlmsg_flags = 0;
1157 err = dump_one_state(x, 0, &info);
1160 return ERR_PTR(err);
1166 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1167 * Must be called with RCU read lock.
1169 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1170 u32 pid, unsigned int group)
1172 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1173 struct xfrm_translator *xtr;
1180 xtr = xfrm_get_translator();
1182 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1184 xfrm_put_translator(xtr);
1191 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1194 static inline unsigned int xfrm_spdinfo_msgsize(void)
1196 return NLMSG_ALIGN(4)
1197 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1198 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1199 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1200 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1203 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1204 u32 portid, u32 seq, u32 flags)
1206 struct xfrmk_spdinfo si;
1207 struct xfrmu_spdinfo spc;
1208 struct xfrmu_spdhinfo sph;
1209 struct xfrmu_spdhthresh spt4, spt6;
1210 struct nlmsghdr *nlh;
1215 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1216 if (nlh == NULL) /* shouldn't really happen ... */
1219 f = nlmsg_data(nlh);
1221 xfrm_spd_getinfo(net, &si);
1222 spc.incnt = si.incnt;
1223 spc.outcnt = si.outcnt;
1224 spc.fwdcnt = si.fwdcnt;
1225 spc.inscnt = si.inscnt;
1226 spc.outscnt = si.outscnt;
1227 spc.fwdscnt = si.fwdscnt;
1228 sph.spdhcnt = si.spdhcnt;
1229 sph.spdhmcnt = si.spdhmcnt;
1232 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1234 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1235 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1236 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1237 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1238 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1240 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1242 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1244 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1246 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1248 nlmsg_cancel(skb, nlh);
1252 nlmsg_end(skb, nlh);
1256 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1257 struct nlattr **attrs)
1259 struct net *net = sock_net(skb->sk);
1260 struct xfrmu_spdhthresh *thresh4 = NULL;
1261 struct xfrmu_spdhthresh *thresh6 = NULL;
1263 /* selector prefixlen thresholds to hash policies */
1264 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1265 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1267 if (nla_len(rta) < sizeof(*thresh4))
1269 thresh4 = nla_data(rta);
1270 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1273 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1274 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1276 if (nla_len(rta) < sizeof(*thresh6))
1278 thresh6 = nla_data(rta);
1279 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1283 if (thresh4 || thresh6) {
1284 write_seqlock(&net->xfrm.policy_hthresh.lock);
1286 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1287 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1290 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1291 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1293 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1295 xfrm_policy_hash_rebuild(net);
1301 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1302 struct nlattr **attrs)
1304 struct net *net = sock_net(skb->sk);
1305 struct sk_buff *r_skb;
1306 u32 *flags = nlmsg_data(nlh);
1307 u32 sportid = NETLINK_CB(skb).portid;
1308 u32 seq = nlh->nlmsg_seq;
1311 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1315 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1318 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1321 static inline unsigned int xfrm_sadinfo_msgsize(void)
1323 return NLMSG_ALIGN(4)
1324 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1325 + nla_total_size(4); /* XFRMA_SAD_CNT */
1328 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1329 u32 portid, u32 seq, u32 flags)
1331 struct xfrmk_sadinfo si;
1332 struct xfrmu_sadhinfo sh;
1333 struct nlmsghdr *nlh;
1337 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1338 if (nlh == NULL) /* shouldn't really happen ... */
1341 f = nlmsg_data(nlh);
1343 xfrm_sad_getinfo(net, &si);
1345 sh.sadhmcnt = si.sadhmcnt;
1346 sh.sadhcnt = si.sadhcnt;
1348 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1350 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1352 nlmsg_cancel(skb, nlh);
1356 nlmsg_end(skb, nlh);
1360 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1361 struct nlattr **attrs)
1363 struct net *net = sock_net(skb->sk);
1364 struct sk_buff *r_skb;
1365 u32 *flags = nlmsg_data(nlh);
1366 u32 sportid = NETLINK_CB(skb).portid;
1367 u32 seq = nlh->nlmsg_seq;
1370 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1374 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1377 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1380 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1381 struct nlattr **attrs)
1383 struct net *net = sock_net(skb->sk);
1384 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1385 struct xfrm_state *x;
1386 struct sk_buff *resp_skb;
1389 x = xfrm_user_state_lookup(net, p, attrs, &err);
1393 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1394 if (IS_ERR(resp_skb)) {
1395 err = PTR_ERR(resp_skb);
1397 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1404 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1405 struct nlattr **attrs)
1407 struct net *net = sock_net(skb->sk);
1408 struct xfrm_state *x;
1409 struct xfrm_userspi_info *p;
1410 struct xfrm_translator *xtr;
1411 struct sk_buff *resp_skb;
1412 xfrm_address_t *daddr;
1419 p = nlmsg_data(nlh);
1420 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1424 family = p->info.family;
1425 daddr = &p->info.id.daddr;
1429 mark = xfrm_mark_get(attrs, &m);
1431 if (attrs[XFRMA_IF_ID])
1432 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1435 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1436 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1443 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1444 if_id, p->info.id.proto, daddr,
1451 err = xfrm_alloc_spi(x, p->min, p->max);
1455 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1456 if (IS_ERR(resp_skb)) {
1457 err = PTR_ERR(resp_skb);
1461 xtr = xfrm_get_translator();
1463 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1465 xfrm_put_translator(xtr);
1467 kfree_skb(resp_skb);
1472 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1480 static int verify_policy_dir(u8 dir)
1483 case XFRM_POLICY_IN:
1484 case XFRM_POLICY_OUT:
1485 case XFRM_POLICY_FWD:
1495 static int verify_policy_type(u8 type)
1498 case XFRM_POLICY_TYPE_MAIN:
1499 #ifdef CONFIG_XFRM_SUB_POLICY
1500 case XFRM_POLICY_TYPE_SUB:
1511 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1516 case XFRM_SHARE_ANY:
1517 case XFRM_SHARE_SESSION:
1518 case XFRM_SHARE_USER:
1519 case XFRM_SHARE_UNIQUE:
1526 switch (p->action) {
1527 case XFRM_POLICY_ALLOW:
1528 case XFRM_POLICY_BLOCK:
1535 switch (p->sel.family) {
1537 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1543 #if IS_ENABLED(CONFIG_IPV6)
1544 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1549 return -EAFNOSUPPORT;
1556 ret = verify_policy_dir(p->dir);
1559 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1565 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1567 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1568 struct xfrm_user_sec_ctx *uctx;
1573 uctx = nla_data(rt);
1574 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1577 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1583 for (i = 0; i < nr; i++, ut++) {
1584 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1586 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1587 memcpy(&t->saddr, &ut->saddr,
1588 sizeof(xfrm_address_t));
1589 t->reqid = ut->reqid;
1591 t->share = ut->share;
1592 t->optional = ut->optional;
1593 t->aalgos = ut->aalgos;
1594 t->ealgos = ut->ealgos;
1595 t->calgos = ut->calgos;
1596 /* If all masks are ~0, then we allow all algorithms. */
1597 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1598 t->encap_family = ut->family;
1602 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1607 if (nr > XFRM_MAX_DEPTH)
1610 prev_family = family;
1612 for (i = 0; i < nr; i++) {
1613 /* We never validated the ut->family value, so many
1614 * applications simply leave it at zero. The check was
1615 * never made and ut->family was ignored because all
1616 * templates could be assumed to have the same family as
1617 * the policy itself. Now that we will have ipv4-in-ipv6
1618 * and ipv6-in-ipv4 tunnels, this is no longer true.
1621 ut[i].family = family;
1623 switch (ut[i].mode) {
1624 case XFRM_MODE_TUNNEL:
1625 case XFRM_MODE_BEET:
1628 if (ut[i].family != prev_family)
1632 if (ut[i].mode >= XFRM_MODE_MAX)
1635 prev_family = ut[i].family;
1637 switch (ut[i].family) {
1640 #if IS_ENABLED(CONFIG_IPV6)
1648 if (!xfrm_id_proto_valid(ut[i].id.proto))
1655 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1657 struct nlattr *rt = attrs[XFRMA_TMPL];
1662 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1663 int nr = nla_len(rt) / sizeof(*utmpl);
1666 err = validate_tmpl(nr, utmpl, pol->family);
1670 copy_templates(pol, utmpl, nr);
1675 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1677 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1678 struct xfrm_userpolicy_type *upt;
1679 u8 type = XFRM_POLICY_TYPE_MAIN;
1687 err = verify_policy_type(type);
1695 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1697 xp->priority = p->priority;
1698 xp->index = p->index;
1699 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1700 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1701 xp->action = p->action;
1702 xp->flags = p->flags;
1703 xp->family = p->sel.family;
1704 /* XXX xp->share = p->share; */
1707 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1709 memset(p, 0, sizeof(*p));
1710 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1711 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1712 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1713 p->priority = xp->priority;
1714 p->index = xp->index;
1715 p->sel.family = xp->family;
1717 p->action = xp->action;
1718 p->flags = xp->flags;
1719 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1722 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1724 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1732 copy_from_user_policy(xp, p);
1734 err = copy_from_user_policy_type(&xp->type, attrs);
1738 if (!(err = copy_from_user_tmpl(xp, attrs)))
1739 err = copy_from_user_sec_ctx(xp, attrs);
1743 xfrm_mark_get(attrs, &xp->mark);
1745 if (attrs[XFRMA_IF_ID])
1746 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1752 xfrm_policy_destroy(xp);
1756 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1757 struct nlattr **attrs)
1759 struct net *net = sock_net(skb->sk);
1760 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1761 struct xfrm_policy *xp;
1766 err = verify_newpolicy_info(p);
1769 err = verify_sec_ctx_len(attrs);
1773 xp = xfrm_policy_construct(net, p, attrs, &err);
1777 /* shouldn't excl be based on nlh flags??
1778 * Aha! this is anti-netlink really i.e more pfkey derived
1779 * in netlink excl is a flag and you wouldn't need
1780 * a type XFRM_MSG_UPDPOLICY - JHS */
1781 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1782 err = xfrm_policy_insert(p->dir, xp, excl);
1783 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1786 security_xfrm_policy_free(xp->security);
1791 c.event = nlh->nlmsg_type;
1792 c.seq = nlh->nlmsg_seq;
1793 c.portid = nlh->nlmsg_pid;
1794 km_policy_notify(xp, p->dir, &c);
1801 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1803 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1806 if (xp->xfrm_nr == 0)
1809 for (i = 0; i < xp->xfrm_nr; i++) {
1810 struct xfrm_user_tmpl *up = &vec[i];
1811 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1813 memset(up, 0, sizeof(*up));
1814 memcpy(&up->id, &kp->id, sizeof(up->id));
1815 up->family = kp->encap_family;
1816 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1817 up->reqid = kp->reqid;
1818 up->mode = kp->mode;
1819 up->share = kp->share;
1820 up->optional = kp->optional;
1821 up->aalgos = kp->aalgos;
1822 up->ealgos = kp->ealgos;
1823 up->calgos = kp->calgos;
1826 return nla_put(skb, XFRMA_TMPL,
1827 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1830 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1833 return copy_sec_ctx(x->security, skb);
1838 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1841 return copy_sec_ctx(xp->security, skb);
1844 static inline unsigned int userpolicy_type_attrsize(void)
1846 #ifdef CONFIG_XFRM_SUB_POLICY
1847 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1853 #ifdef CONFIG_XFRM_SUB_POLICY
1854 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1856 struct xfrm_userpolicy_type upt;
1858 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1859 memset(&upt, 0, sizeof(upt));
1862 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1866 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1872 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1874 struct xfrm_dump_info *sp = ptr;
1875 struct xfrm_userpolicy_info *p;
1876 struct sk_buff *in_skb = sp->in_skb;
1877 struct sk_buff *skb = sp->out_skb;
1878 struct xfrm_translator *xtr;
1879 struct nlmsghdr *nlh;
1882 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1883 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1887 p = nlmsg_data(nlh);
1888 copy_to_user_policy(xp, p, dir);
1889 err = copy_to_user_tmpl(xp, skb);
1891 err = copy_to_user_sec_ctx(xp, skb);
1893 err = copy_to_user_policy_type(xp->type, skb);
1895 err = xfrm_mark_put(skb, &xp->mark);
1897 err = xfrm_if_id_put(skb, xp->if_id);
1899 nlmsg_cancel(skb, nlh);
1902 nlmsg_end(skb, nlh);
1904 xtr = xfrm_get_translator();
1906 err = xtr->alloc_compat(skb, nlh);
1908 xfrm_put_translator(xtr);
1910 nlmsg_cancel(skb, nlh);
1918 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1920 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1921 struct net *net = sock_net(cb->skb->sk);
1923 xfrm_policy_walk_done(walk, net);
1927 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1929 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1931 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1933 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1937 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1939 struct net *net = sock_net(skb->sk);
1940 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1941 struct xfrm_dump_info info;
1943 info.in_skb = cb->skb;
1945 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1946 info.nlmsg_flags = NLM_F_MULTI;
1948 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1953 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1954 struct xfrm_policy *xp,
1957 struct xfrm_dump_info info;
1958 struct sk_buff *skb;
1961 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1963 return ERR_PTR(-ENOMEM);
1965 info.in_skb = in_skb;
1967 info.nlmsg_seq = seq;
1968 info.nlmsg_flags = 0;
1970 err = dump_one_policy(xp, dir, 0, &info);
1973 return ERR_PTR(err);
1979 static int xfrm_notify_userpolicy(struct net *net)
1981 struct xfrm_userpolicy_default *up;
1982 int len = NLMSG_ALIGN(sizeof(*up));
1983 struct nlmsghdr *nlh;
1984 struct sk_buff *skb;
1987 skb = nlmsg_new(len, GFP_ATOMIC);
1991 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
1997 up = nlmsg_data(nlh);
1998 up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
1999 up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2000 up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2002 nlmsg_end(skb, nlh);
2005 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2011 static bool xfrm_userpolicy_is_valid(__u8 policy)
2013 return policy == XFRM_USERPOLICY_BLOCK ||
2014 policy == XFRM_USERPOLICY_ACCEPT;
2017 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2018 struct nlattr **attrs)
2020 struct net *net = sock_net(skb->sk);
2021 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2023 if (xfrm_userpolicy_is_valid(up->in))
2024 net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
2026 if (xfrm_userpolicy_is_valid(up->fwd))
2027 net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
2029 if (xfrm_userpolicy_is_valid(up->out))
2030 net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
2032 rt_genid_bump_all(net);
2034 xfrm_notify_userpolicy(net);
2038 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2039 struct nlattr **attrs)
2041 struct sk_buff *r_skb;
2042 struct nlmsghdr *r_nlh;
2043 struct net *net = sock_net(skb->sk);
2044 struct xfrm_userpolicy_default *r_up;
2045 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2046 u32 portid = NETLINK_CB(skb).portid;
2047 u32 seq = nlh->nlmsg_seq;
2049 r_skb = nlmsg_new(len, GFP_ATOMIC);
2053 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2059 r_up = nlmsg_data(r_nlh);
2060 r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2061 r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2062 r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2063 nlmsg_end(r_skb, r_nlh);
2065 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2068 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2069 struct nlattr **attrs)
2071 struct net *net = sock_net(skb->sk);
2072 struct xfrm_policy *xp;
2073 struct xfrm_userpolicy_id *p;
2074 u8 type = XFRM_POLICY_TYPE_MAIN;
2081 p = nlmsg_data(nlh);
2082 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2084 err = copy_from_user_policy_type(&type, attrs);
2088 err = verify_policy_dir(p->dir);
2092 if (attrs[XFRMA_IF_ID])
2093 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2095 xfrm_mark_get(attrs, &m);
2098 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2099 p->index, delete, &err);
2101 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2102 struct xfrm_sec_ctx *ctx;
2104 err = verify_sec_ctx_len(attrs);
2110 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2112 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2116 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2117 &p->sel, ctx, delete, &err);
2118 security_xfrm_policy_free(ctx);
2124 struct sk_buff *resp_skb;
2126 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2127 if (IS_ERR(resp_skb)) {
2128 err = PTR_ERR(resp_skb);
2130 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
2131 NETLINK_CB(skb).portid);
2134 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2139 c.data.byid = p->index;
2140 c.event = nlh->nlmsg_type;
2141 c.seq = nlh->nlmsg_seq;
2142 c.portid = nlh->nlmsg_pid;
2143 km_policy_notify(xp, p->dir, &c);
2151 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2152 struct nlattr **attrs)
2154 struct net *net = sock_net(skb->sk);
2156 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2159 err = xfrm_state_flush(net, p->proto, true, false);
2161 if (err == -ESRCH) /* empty table */
2165 c.data.proto = p->proto;
2166 c.event = nlh->nlmsg_type;
2167 c.seq = nlh->nlmsg_seq;
2168 c.portid = nlh->nlmsg_pid;
2170 km_state_notify(NULL, &c);
2175 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2177 unsigned int replay_size = x->replay_esn ?
2178 xfrm_replay_state_esn_len(x->replay_esn) :
2179 sizeof(struct xfrm_replay_state);
2181 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2182 + nla_total_size(replay_size)
2183 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2184 + nla_total_size(sizeof(struct xfrm_mark))
2185 + nla_total_size(4) /* XFRM_AE_RTHR */
2186 + nla_total_size(4); /* XFRM_AE_ETHR */
2189 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2191 struct xfrm_aevent_id *id;
2192 struct nlmsghdr *nlh;
2195 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2199 id = nlmsg_data(nlh);
2200 memset(&id->sa_id, 0, sizeof(id->sa_id));
2201 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2202 id->sa_id.spi = x->id.spi;
2203 id->sa_id.family = x->props.family;
2204 id->sa_id.proto = x->id.proto;
2205 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2206 id->reqid = x->props.reqid;
2207 id->flags = c->data.aevent;
2209 if (x->replay_esn) {
2210 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2211 xfrm_replay_state_esn_len(x->replay_esn),
2214 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2219 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2224 if (id->flags & XFRM_AE_RTHR) {
2225 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2229 if (id->flags & XFRM_AE_ETHR) {
2230 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2231 x->replay_maxage * 10 / HZ);
2235 err = xfrm_mark_put(skb, &x->mark);
2239 err = xfrm_if_id_put(skb, x->if_id);
2243 nlmsg_end(skb, nlh);
2247 nlmsg_cancel(skb, nlh);
2251 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2252 struct nlattr **attrs)
2254 struct net *net = sock_net(skb->sk);
2255 struct xfrm_state *x;
2256 struct sk_buff *r_skb;
2261 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2262 struct xfrm_usersa_id *id = &p->sa_id;
2264 mark = xfrm_mark_get(attrs, &m);
2266 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2270 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2271 if (r_skb == NULL) {
2277 * XXX: is this lock really needed - none of the other
2278 * gets lock (the concern is things getting updated
2279 * while we are still reading) - jhs
2281 spin_lock_bh(&x->lock);
2282 c.data.aevent = p->flags;
2283 c.seq = nlh->nlmsg_seq;
2284 c.portid = nlh->nlmsg_pid;
2286 err = build_aevent(r_skb, x, &c);
2289 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2290 spin_unlock_bh(&x->lock);
2295 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2296 struct nlattr **attrs)
2298 struct net *net = sock_net(skb->sk);
2299 struct xfrm_state *x;
2304 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2305 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2306 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2307 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2308 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2309 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2311 if (!lt && !rp && !re && !et && !rt)
2314 /* pedantic mode - thou shalt sayeth replaceth */
2315 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2318 mark = xfrm_mark_get(attrs, &m);
2320 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2324 if (x->km.state != XFRM_STATE_VALID)
2327 err = xfrm_replay_verify_len(x->replay_esn, re);
2331 spin_lock_bh(&x->lock);
2332 xfrm_update_ae_params(x, attrs, 1);
2333 spin_unlock_bh(&x->lock);
2335 c.event = nlh->nlmsg_type;
2336 c.seq = nlh->nlmsg_seq;
2337 c.portid = nlh->nlmsg_pid;
2338 c.data.aevent = XFRM_AE_CU;
2339 km_state_notify(x, &c);
2346 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2347 struct nlattr **attrs)
2349 struct net *net = sock_net(skb->sk);
2351 u8 type = XFRM_POLICY_TYPE_MAIN;
2354 err = copy_from_user_policy_type(&type, attrs);
2358 err = xfrm_policy_flush(net, type, true);
2360 if (err == -ESRCH) /* empty table */
2366 c.event = nlh->nlmsg_type;
2367 c.seq = nlh->nlmsg_seq;
2368 c.portid = nlh->nlmsg_pid;
2370 km_policy_notify(NULL, 0, &c);
2374 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2375 struct nlattr **attrs)
2377 struct net *net = sock_net(skb->sk);
2378 struct xfrm_policy *xp;
2379 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2380 struct xfrm_userpolicy_info *p = &up->pol;
2381 u8 type = XFRM_POLICY_TYPE_MAIN;
2386 err = copy_from_user_policy_type(&type, attrs);
2390 err = verify_policy_dir(p->dir);
2394 if (attrs[XFRMA_IF_ID])
2395 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2397 xfrm_mark_get(attrs, &m);
2400 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2403 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2404 struct xfrm_sec_ctx *ctx;
2406 err = verify_sec_ctx_len(attrs);
2412 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2414 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2418 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2419 &p->sel, ctx, 0, &err);
2420 security_xfrm_policy_free(ctx);
2425 if (unlikely(xp->walk.dead))
2430 xfrm_policy_delete(xp, p->dir);
2431 xfrm_audit_policy_delete(xp, 1, true);
2433 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2440 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2441 struct nlattr **attrs)
2443 struct net *net = sock_net(skb->sk);
2444 struct xfrm_state *x;
2446 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2447 struct xfrm_usersa_info *p = &ue->state;
2449 u32 mark = xfrm_mark_get(attrs, &m);
2451 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2457 spin_lock_bh(&x->lock);
2459 if (x->km.state != XFRM_STATE_VALID)
2461 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2464 __xfrm_state_delete(x);
2465 xfrm_audit_state_delete(x, 1, true);
2469 spin_unlock_bh(&x->lock);
2474 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2475 struct nlattr **attrs)
2477 struct net *net = sock_net(skb->sk);
2478 struct xfrm_policy *xp;
2479 struct xfrm_user_tmpl *ut;
2481 struct nlattr *rt = attrs[XFRMA_TMPL];
2482 struct xfrm_mark mark;
2484 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2485 struct xfrm_state *x = xfrm_state_alloc(net);
2491 xfrm_mark_get(attrs, &mark);
2493 err = verify_newpolicy_info(&ua->policy);
2496 err = verify_sec_ctx_len(attrs);
2501 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2505 memcpy(&x->id, &ua->id, sizeof(ua->id));
2506 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2507 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2508 xp->mark.m = x->mark.m = mark.m;
2509 xp->mark.v = x->mark.v = mark.v;
2511 /* extract the templates and for each call km_key */
2512 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2513 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2514 memcpy(&x->id, &t->id, sizeof(x->id));
2515 x->props.mode = t->mode;
2516 x->props.reqid = t->reqid;
2517 x->props.family = ut->family;
2518 t->aalgos = ua->aalgos;
2519 t->ealgos = ua->ealgos;
2520 t->calgos = ua->calgos;
2521 err = km_query(x, t, xp);
2536 #ifdef CONFIG_XFRM_MIGRATE
2537 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2538 struct xfrm_kmaddress *k,
2539 struct nlattr **attrs, int *num)
2541 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2542 struct xfrm_user_migrate *um;
2546 struct xfrm_user_kmaddress *uk;
2548 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2549 memcpy(&k->local, &uk->local, sizeof(k->local));
2550 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2551 k->family = uk->family;
2552 k->reserved = uk->reserved;
2556 num_migrate = nla_len(rt) / sizeof(*um);
2558 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2561 for (i = 0; i < num_migrate; i++, um++, ma++) {
2562 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2563 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2564 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2565 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2567 ma->proto = um->proto;
2568 ma->mode = um->mode;
2569 ma->reqid = um->reqid;
2571 ma->old_family = um->old_family;
2572 ma->new_family = um->new_family;
2579 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2580 struct nlattr **attrs)
2582 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2583 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2584 struct xfrm_kmaddress km, *kmp;
2588 struct net *net = sock_net(skb->sk);
2589 struct xfrm_encap_tmpl *encap = NULL;
2592 if (attrs[XFRMA_MIGRATE] == NULL)
2595 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2597 err = copy_from_user_policy_type(&type, attrs);
2601 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2608 if (attrs[XFRMA_ENCAP]) {
2609 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2610 sizeof(*encap), GFP_KERNEL);
2615 if (attrs[XFRMA_IF_ID])
2616 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2618 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
2625 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2626 struct nlattr **attrs)
2628 return -ENOPROTOOPT;
2632 #ifdef CONFIG_XFRM_MIGRATE
2633 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2635 struct xfrm_user_migrate um;
2637 memset(&um, 0, sizeof(um));
2638 um.proto = m->proto;
2640 um.reqid = m->reqid;
2641 um.old_family = m->old_family;
2642 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2643 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2644 um.new_family = m->new_family;
2645 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2646 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2648 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2651 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2653 struct xfrm_user_kmaddress uk;
2655 memset(&uk, 0, sizeof(uk));
2656 uk.family = k->family;
2657 uk.reserved = k->reserved;
2658 memcpy(&uk.local, &k->local, sizeof(uk.local));
2659 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2661 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2664 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2667 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2668 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2669 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2670 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2671 + userpolicy_type_attrsize();
2674 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2675 int num_migrate, const struct xfrm_kmaddress *k,
2676 const struct xfrm_selector *sel,
2677 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2679 const struct xfrm_migrate *mp;
2680 struct xfrm_userpolicy_id *pol_id;
2681 struct nlmsghdr *nlh;
2684 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2688 pol_id = nlmsg_data(nlh);
2689 /* copy data from selector, dir, and type to the pol_id */
2690 memset(pol_id, 0, sizeof(*pol_id));
2691 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2695 err = copy_to_user_kmaddress(k, skb);
2700 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2704 err = copy_to_user_policy_type(type, skb);
2707 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2708 err = copy_to_user_migrate(mp, skb);
2713 nlmsg_end(skb, nlh);
2717 nlmsg_cancel(skb, nlh);
2721 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2722 const struct xfrm_migrate *m, int num_migrate,
2723 const struct xfrm_kmaddress *k,
2724 const struct xfrm_encap_tmpl *encap)
2726 struct net *net = &init_net;
2727 struct sk_buff *skb;
2730 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2736 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2739 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
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 return -ENOPROTOOPT;
2751 #define XMSGSIZE(type) sizeof(struct type)
2753 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2754 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2755 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2756 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2757 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2758 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2759 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2760 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2761 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2762 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2763 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2764 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2765 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2766 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2767 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2768 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2769 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2770 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2771 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2772 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2773 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2774 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2775 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2776 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2778 EXPORT_SYMBOL_GPL(xfrm_msg_min);
2782 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2783 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2784 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2785 [XFRMA_LASTUSED] = { .type = NLA_U64},
2786 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2787 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2788 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2789 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2790 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2791 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2792 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2793 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2794 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2795 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2796 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2797 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2798 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2799 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2800 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2801 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2802 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2803 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2804 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2805 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2806 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2807 [XFRMA_PROTO] = { .type = NLA_U8 },
2808 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2809 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2810 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2811 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2812 [XFRMA_IF_ID] = { .type = NLA_U32 },
2814 EXPORT_SYMBOL_GPL(xfrma_policy);
2816 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2817 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2818 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2821 static const struct xfrm_link {
2822 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2823 int (*start)(struct netlink_callback *);
2824 int (*dump)(struct sk_buff *, struct netlink_callback *);
2825 int (*done)(struct netlink_callback *);
2826 const struct nla_policy *nla_pol;
2828 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2829 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2830 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2831 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2832 .dump = xfrm_dump_sa,
2833 .done = xfrm_dump_sa_done },
2834 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2835 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2836 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2837 .start = xfrm_dump_policy_start,
2838 .dump = xfrm_dump_policy,
2839 .done = xfrm_dump_policy_done },
2840 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2841 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2842 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2843 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2844 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2845 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2846 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2847 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2848 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2849 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2850 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2851 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2852 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2853 .nla_pol = xfrma_spd_policy,
2854 .nla_max = XFRMA_SPD_MAX },
2855 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2856 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
2857 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
2860 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2861 struct netlink_ext_ack *extack)
2863 struct net *net = sock_net(skb->sk);
2864 struct nlattr *attrs[XFRMA_MAX+1];
2865 const struct xfrm_link *link;
2866 struct nlmsghdr *nlh64 = NULL;
2869 type = nlh->nlmsg_type;
2870 if (type > XFRM_MSG_MAX)
2873 type -= XFRM_MSG_BASE;
2874 link = &xfrm_dispatch[type];
2876 /* All operations require privileges, even GET */
2877 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2880 if (in_compat_syscall()) {
2881 struct xfrm_translator *xtr = xfrm_get_translator();
2886 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2887 link->nla_pol, extack);
2888 xfrm_put_translator(xtr);
2890 return PTR_ERR(nlh64);
2895 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2896 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2897 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2898 struct netlink_dump_control c = {
2899 .start = link->start,
2904 if (link->dump == NULL) {
2909 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2913 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2914 link->nla_max ? : XFRMA_MAX,
2915 link->nla_pol ? : xfrma_policy, extack);
2919 if (link->doit == NULL) {
2924 err = link->doit(skb, nlh, attrs);
2926 /* We need to free skb allocated in xfrm_alloc_compat() before
2927 * returning from this function, because consume_skb() won't take
2928 * care of frag_list since netlink destructor sets
2929 * sbk->head to NULL. (see netlink_skb_destructor())
2931 if (skb_has_frag_list(skb)) {
2932 kfree_skb(skb_shinfo(skb)->frag_list);
2933 skb_shinfo(skb)->frag_list = NULL;
2941 static void xfrm_netlink_rcv(struct sk_buff *skb)
2943 struct net *net = sock_net(skb->sk);
2945 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2946 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2947 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2950 static inline unsigned int xfrm_expire_msgsize(void)
2952 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2953 + nla_total_size(sizeof(struct xfrm_mark));
2956 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2958 struct xfrm_user_expire *ue;
2959 struct nlmsghdr *nlh;
2962 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2966 ue = nlmsg_data(nlh);
2967 copy_to_user_state(x, &ue->state);
2968 ue->hard = (c->data.hard != 0) ? 1 : 0;
2969 /* clear the padding bytes */
2970 memset_after(ue, 0, hard);
2972 err = xfrm_mark_put(skb, &x->mark);
2976 err = xfrm_if_id_put(skb, x->if_id);
2980 nlmsg_end(skb, nlh);
2984 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2986 struct net *net = xs_net(x);
2987 struct sk_buff *skb;
2989 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2993 if (build_expire(skb, x, c) < 0) {
2998 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3001 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3003 struct net *net = xs_net(x);
3004 struct sk_buff *skb;
3007 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3011 err = build_aevent(skb, x, c);
3014 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3017 static int xfrm_notify_sa_flush(const struct km_event *c)
3019 struct net *net = c->net;
3020 struct xfrm_usersa_flush *p;
3021 struct nlmsghdr *nlh;
3022 struct sk_buff *skb;
3023 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
3025 skb = nlmsg_new(len, GFP_ATOMIC);
3029 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
3035 p = nlmsg_data(nlh);
3036 p->proto = c->data.proto;
3038 nlmsg_end(skb, nlh);
3040 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3043 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
3047 l += nla_total_size(aead_len(x->aead));
3049 l += nla_total_size(sizeof(struct xfrm_algo) +
3050 (x->aalg->alg_key_len + 7) / 8);
3051 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3054 l += nla_total_size(xfrm_alg_len(x->ealg));
3056 l += nla_total_size(sizeof(*x->calg));
3058 l += nla_total_size(sizeof(*x->encap));
3060 l += nla_total_size(sizeof(x->tfcpad));
3062 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3064 l += nla_total_size(sizeof(struct xfrm_replay_state));
3066 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3067 x->security->ctx_len);
3069 l += nla_total_size(sizeof(*x->coaddr));
3070 if (x->props.extra_flags)
3071 l += nla_total_size(sizeof(x->props.extra_flags));
3073 l += nla_total_size(sizeof(struct xfrm_user_offload));
3074 if (x->props.smark.v | x->props.smark.m) {
3075 l += nla_total_size(sizeof(x->props.smark.v));
3076 l += nla_total_size(sizeof(x->props.smark.m));
3079 l += nla_total_size(sizeof(x->if_id));
3081 /* Must count x->lastused as it may become non-zero behind our back. */
3082 l += nla_total_size_64bit(sizeof(u64));
3084 if (x->mapping_maxage)
3085 l += nla_total_size(sizeof(x->mapping_maxage));
3090 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
3092 struct net *net = xs_net(x);
3093 struct xfrm_usersa_info *p;
3094 struct xfrm_usersa_id *id;
3095 struct nlmsghdr *nlh;
3096 struct sk_buff *skb;
3097 unsigned int len = xfrm_sa_len(x);
3098 unsigned int headlen;
3101 headlen = sizeof(*p);
3102 if (c->event == XFRM_MSG_DELSA) {
3103 len += nla_total_size(headlen);
3104 headlen = sizeof(*id);
3105 len += nla_total_size(sizeof(struct xfrm_mark));
3107 len += NLMSG_ALIGN(headlen);
3109 skb = nlmsg_new(len, GFP_ATOMIC);
3113 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3118 p = nlmsg_data(nlh);
3119 if (c->event == XFRM_MSG_DELSA) {
3120 struct nlattr *attr;
3122 id = nlmsg_data(nlh);
3123 memset(id, 0, sizeof(*id));
3124 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3125 id->spi = x->id.spi;
3126 id->family = x->props.family;
3127 id->proto = x->id.proto;
3129 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
3136 err = copy_to_user_state_extra(x, p, skb);
3140 nlmsg_end(skb, nlh);
3142 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3149 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
3153 case XFRM_MSG_EXPIRE:
3154 return xfrm_exp_state_notify(x, c);
3155 case XFRM_MSG_NEWAE:
3156 return xfrm_aevent_state_notify(x, c);
3157 case XFRM_MSG_DELSA:
3158 case XFRM_MSG_UPDSA:
3159 case XFRM_MSG_NEWSA:
3160 return xfrm_notify_sa(x, c);
3161 case XFRM_MSG_FLUSHSA:
3162 return xfrm_notify_sa_flush(c);
3164 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3173 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3174 struct xfrm_policy *xp)
3176 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3177 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3178 + nla_total_size(sizeof(struct xfrm_mark))
3179 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3180 + userpolicy_type_attrsize();
3183 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
3184 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3186 __u32 seq = xfrm_get_acqseq();
3187 struct xfrm_user_acquire *ua;
3188 struct nlmsghdr *nlh;
3191 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3195 ua = nlmsg_data(nlh);
3196 memcpy(&ua->id, &x->id, sizeof(ua->id));
3197 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3198 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3199 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3200 ua->aalgos = xt->aalgos;
3201 ua->ealgos = xt->ealgos;
3202 ua->calgos = xt->calgos;
3203 ua->seq = x->km.seq = seq;
3205 err = copy_to_user_tmpl(xp, skb);
3207 err = copy_to_user_state_sec_ctx(x, skb);
3209 err = copy_to_user_policy_type(xp->type, skb);
3211 err = xfrm_mark_put(skb, &xp->mark);
3213 err = xfrm_if_id_put(skb, xp->if_id);
3215 nlmsg_cancel(skb, nlh);
3219 nlmsg_end(skb, nlh);
3223 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3224 struct xfrm_policy *xp)
3226 struct net *net = xs_net(x);
3227 struct sk_buff *skb;
3230 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3234 err = build_acquire(skb, x, xt, xp);
3237 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3240 /* User gives us xfrm_user_policy_info followed by an array of 0
3241 * or more templates.
3243 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3244 u8 *data, int len, int *dir)
3246 struct net *net = sock_net(sk);
3247 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3248 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3249 struct xfrm_policy *xp;
3252 switch (sk->sk_family) {
3254 if (opt != IP_XFRM_POLICY) {
3259 #if IS_ENABLED(CONFIG_IPV6)
3261 if (opt != IPV6_XFRM_POLICY) {
3274 if (len < sizeof(*p) ||
3275 verify_newpolicy_info(p))
3278 nr = ((len - sizeof(*p)) / sizeof(*ut));
3279 if (validate_tmpl(nr, ut, p->sel.family))
3282 if (p->dir > XFRM_POLICY_OUT)
3285 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3291 copy_from_user_policy(xp, p);
3292 xp->type = XFRM_POLICY_TYPE_MAIN;
3293 copy_templates(xp, ut, nr);
3300 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3302 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3303 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3304 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3305 + nla_total_size(sizeof(struct xfrm_mark))
3306 + userpolicy_type_attrsize();
3309 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3310 int dir, const struct km_event *c)
3312 struct xfrm_user_polexpire *upe;
3313 int hard = c->data.hard;
3314 struct nlmsghdr *nlh;
3317 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3321 upe = nlmsg_data(nlh);
3322 copy_to_user_policy(xp, &upe->pol, dir);
3323 err = copy_to_user_tmpl(xp, skb);
3325 err = copy_to_user_sec_ctx(xp, skb);
3327 err = copy_to_user_policy_type(xp->type, skb);
3329 err = xfrm_mark_put(skb, &xp->mark);
3331 err = xfrm_if_id_put(skb, xp->if_id);
3333 nlmsg_cancel(skb, nlh);
3338 nlmsg_end(skb, nlh);
3342 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3344 struct net *net = xp_net(xp);
3345 struct sk_buff *skb;
3348 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3352 err = build_polexpire(skb, xp, dir, c);
3355 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3358 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3360 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3361 struct net *net = xp_net(xp);
3362 struct xfrm_userpolicy_info *p;
3363 struct xfrm_userpolicy_id *id;
3364 struct nlmsghdr *nlh;
3365 struct sk_buff *skb;
3366 unsigned int headlen;
3369 headlen = sizeof(*p);
3370 if (c->event == XFRM_MSG_DELPOLICY) {
3371 len += nla_total_size(headlen);
3372 headlen = sizeof(*id);
3374 len += userpolicy_type_attrsize();
3375 len += nla_total_size(sizeof(struct xfrm_mark));
3376 len += NLMSG_ALIGN(headlen);
3378 skb = nlmsg_new(len, GFP_ATOMIC);
3382 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3387 p = nlmsg_data(nlh);
3388 if (c->event == XFRM_MSG_DELPOLICY) {
3389 struct nlattr *attr;
3391 id = nlmsg_data(nlh);
3392 memset(id, 0, sizeof(*id));
3395 id->index = xp->index;
3397 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3399 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3407 copy_to_user_policy(xp, p, dir);
3408 err = copy_to_user_tmpl(xp, skb);
3410 err = copy_to_user_policy_type(xp->type, skb);
3412 err = xfrm_mark_put(skb, &xp->mark);
3414 err = xfrm_if_id_put(skb, xp->if_id);
3418 nlmsg_end(skb, nlh);
3420 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3427 static int xfrm_notify_policy_flush(const struct km_event *c)
3429 struct net *net = c->net;
3430 struct nlmsghdr *nlh;
3431 struct sk_buff *skb;
3434 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3438 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3442 err = copy_to_user_policy_type(c->data.type, skb);
3446 nlmsg_end(skb, nlh);
3448 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3455 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3459 case XFRM_MSG_NEWPOLICY:
3460 case XFRM_MSG_UPDPOLICY:
3461 case XFRM_MSG_DELPOLICY:
3462 return xfrm_notify_policy(xp, dir, c);
3463 case XFRM_MSG_FLUSHPOLICY:
3464 return xfrm_notify_policy_flush(c);
3465 case XFRM_MSG_POLEXPIRE:
3466 return xfrm_exp_policy_notify(xp, dir, c);
3468 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3476 static inline unsigned int xfrm_report_msgsize(void)
3478 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3481 static int build_report(struct sk_buff *skb, u8 proto,
3482 struct xfrm_selector *sel, xfrm_address_t *addr)
3484 struct xfrm_user_report *ur;
3485 struct nlmsghdr *nlh;
3487 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3491 ur = nlmsg_data(nlh);
3493 memcpy(&ur->sel, sel, sizeof(ur->sel));
3496 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3498 nlmsg_cancel(skb, nlh);
3502 nlmsg_end(skb, nlh);
3506 static int xfrm_send_report(struct net *net, u8 proto,
3507 struct xfrm_selector *sel, xfrm_address_t *addr)
3509 struct sk_buff *skb;
3512 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3516 err = build_report(skb, proto, sel, addr);
3519 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3522 static inline unsigned int xfrm_mapping_msgsize(void)
3524 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3527 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3528 xfrm_address_t *new_saddr, __be16 new_sport)
3530 struct xfrm_user_mapping *um;
3531 struct nlmsghdr *nlh;
3533 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3537 um = nlmsg_data(nlh);
3539 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3540 um->id.spi = x->id.spi;
3541 um->id.family = x->props.family;
3542 um->id.proto = x->id.proto;
3543 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3544 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3545 um->new_sport = new_sport;
3546 um->old_sport = x->encap->encap_sport;
3547 um->reqid = x->props.reqid;
3549 nlmsg_end(skb, nlh);
3553 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3556 struct net *net = xs_net(x);
3557 struct sk_buff *skb;
3560 if (x->id.proto != IPPROTO_ESP)
3566 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3570 err = build_mapping(skb, x, ipaddr, sport);
3573 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3576 static bool xfrm_is_alive(const struct km_event *c)
3578 return (bool)xfrm_acquire_is_on(c->net);
3581 static struct xfrm_mgr netlink_mgr = {
3582 .notify = xfrm_send_state_notify,
3583 .acquire = xfrm_send_acquire,
3584 .compile_policy = xfrm_compile_policy,
3585 .notify_policy = xfrm_send_policy_notify,
3586 .report = xfrm_send_report,
3587 .migrate = xfrm_send_migrate,
3588 .new_mapping = xfrm_send_mapping,
3589 .is_alive = xfrm_is_alive,
3592 static int __net_init xfrm_user_net_init(struct net *net)
3595 struct netlink_kernel_cfg cfg = {
3596 .groups = XFRMNLGRP_MAX,
3597 .input = xfrm_netlink_rcv,
3600 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3603 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3604 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3608 static void __net_exit xfrm_user_net_pre_exit(struct net *net)
3610 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3613 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3617 list_for_each_entry(net, net_exit_list, exit_list)
3618 netlink_kernel_release(net->xfrm.nlsk_stash);
3621 static struct pernet_operations xfrm_user_net_ops = {
3622 .init = xfrm_user_net_init,
3623 .pre_exit = xfrm_user_net_pre_exit,
3624 .exit_batch = xfrm_user_net_exit,
3627 static int __init xfrm_user_init(void)
3631 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3633 rv = register_pernet_subsys(&xfrm_user_net_ops);
3636 rv = xfrm_register_km(&netlink_mgr);
3638 unregister_pernet_subsys(&xfrm_user_net_ops);
3642 static void __exit xfrm_user_exit(void)
3644 xfrm_unregister_km(&netlink_mgr);
3645 unregister_pernet_subsys(&xfrm_user_net_ops);
3648 module_init(xfrm_user_init);
3649 module_exit(xfrm_user_exit);
3650 MODULE_LICENSE("GPL");
3651 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);