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,
39 struct netlink_ext_ack *extack)
41 struct nlattr *rt = attrs[type];
42 struct xfrm_algo *algp;
48 if (nla_len(rt) < (int)xfrm_alg_len(algp)) {
49 NL_SET_ERR_MSG(extack, "Invalid AUTH/CRYPT/COMP attribute length");
60 NL_SET_ERR_MSG(extack, "Invalid algorithm attribute type");
64 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
68 static int verify_auth_trunc(struct nlattr **attrs,
69 struct netlink_ext_ack *extack)
71 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
72 struct xfrm_algo_auth *algp;
78 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp)) {
79 NL_SET_ERR_MSG(extack, "Invalid AUTH_TRUNC attribute length");
83 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
87 static int verify_aead(struct nlattr **attrs, struct netlink_ext_ack *extack)
89 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
90 struct xfrm_algo_aead *algp;
96 if (nla_len(rt) < (int)aead_len(algp)) {
97 NL_SET_ERR_MSG(extack, "Invalid AEAD attribute length");
101 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
105 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
106 xfrm_address_t **addrp)
108 struct nlattr *rt = attrs[type];
111 *addrp = nla_data(rt);
114 static inline int verify_sec_ctx_len(struct nlattr **attrs, struct netlink_ext_ack *extack)
116 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
117 struct xfrm_user_sec_ctx *uctx;
123 if (uctx->len > nla_len(rt) ||
124 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) {
125 NL_SET_ERR_MSG(extack, "Invalid security context length");
132 static inline int verify_replay(struct xfrm_usersa_info *p,
133 struct nlattr **attrs,
134 struct netlink_ext_ack *extack)
136 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
137 struct xfrm_replay_state_esn *rs;
140 if (p->flags & XFRM_STATE_ESN) {
141 NL_SET_ERR_MSG(extack, "Missing required attribute for ESN");
149 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) {
150 NL_SET_ERR_MSG(extack, "ESN bitmap length must be <= 128");
154 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
155 nla_len(rt) != sizeof(*rs)) {
156 NL_SET_ERR_MSG(extack, "ESN attribute is too short to fit the full bitmap length");
160 /* As only ESP and AH support ESN feature. */
161 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) {
162 NL_SET_ERR_MSG(extack, "ESN only supported for ESP and AH");
166 if (p->replay_window != 0) {
167 NL_SET_ERR_MSG(extack, "ESN not compatible with legacy replay_window");
174 static int verify_newsa_info(struct xfrm_usersa_info *p,
175 struct nlattr **attrs,
176 struct netlink_ext_ack *extack)
186 #if IS_ENABLED(CONFIG_IPV6)
190 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
195 NL_SET_ERR_MSG(extack, "Invalid address family");
199 switch (p->sel.family) {
204 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
205 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
212 #if IS_ENABLED(CONFIG_IPV6)
213 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
214 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
220 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
226 NL_SET_ERR_MSG(extack, "Invalid address family in selector");
231 switch (p->id.proto) {
233 if (!attrs[XFRMA_ALG_AUTH] &&
234 !attrs[XFRMA_ALG_AUTH_TRUNC]) {
235 NL_SET_ERR_MSG(extack, "Missing required attribute for AH: AUTH_TRUNC or AUTH");
239 if (attrs[XFRMA_ALG_AEAD] ||
240 attrs[XFRMA_ALG_CRYPT] ||
241 attrs[XFRMA_ALG_COMP] ||
242 attrs[XFRMA_TFCPAD]) {
243 NL_SET_ERR_MSG(extack, "Invalid attributes for AH: AEAD, CRYPT, COMP, TFCPAD");
249 if (attrs[XFRMA_ALG_COMP]) {
250 NL_SET_ERR_MSG(extack, "Invalid attribute for ESP: COMP");
254 if (!attrs[XFRMA_ALG_AUTH] &&
255 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
256 !attrs[XFRMA_ALG_CRYPT] &&
257 !attrs[XFRMA_ALG_AEAD]) {
258 NL_SET_ERR_MSG(extack, "Missing required attribute for ESP: at least one of AUTH, AUTH_TRUNC, CRYPT, AEAD");
262 if ((attrs[XFRMA_ALG_AUTH] ||
263 attrs[XFRMA_ALG_AUTH_TRUNC] ||
264 attrs[XFRMA_ALG_CRYPT]) &&
265 attrs[XFRMA_ALG_AEAD]) {
266 NL_SET_ERR_MSG(extack, "Invalid attribute combination for ESP: AEAD can't be used with AUTH, AUTH_TRUNC, CRYPT");
270 if (attrs[XFRMA_TFCPAD] &&
271 p->mode != XFRM_MODE_TUNNEL) {
272 NL_SET_ERR_MSG(extack, "TFC padding can only be used in tunnel mode");
278 if (!attrs[XFRMA_ALG_COMP]) {
279 NL_SET_ERR_MSG(extack, "Missing required attribute for COMP: COMP");
283 if (attrs[XFRMA_ALG_AEAD] ||
284 attrs[XFRMA_ALG_AUTH] ||
285 attrs[XFRMA_ALG_AUTH_TRUNC] ||
286 attrs[XFRMA_ALG_CRYPT] ||
287 attrs[XFRMA_TFCPAD]) {
288 NL_SET_ERR_MSG(extack, "Invalid attributes for COMP: AEAD, AUTH, AUTH_TRUNC, CRYPT, TFCPAD");
292 if (ntohl(p->id.spi) >= 0x10000) {
293 NL_SET_ERR_MSG(extack, "SPI is too large for COMP (must be < 0x10000)");
298 #if IS_ENABLED(CONFIG_IPV6)
299 case IPPROTO_DSTOPTS:
300 case IPPROTO_ROUTING:
301 if (attrs[XFRMA_ALG_COMP] ||
302 attrs[XFRMA_ALG_AUTH] ||
303 attrs[XFRMA_ALG_AUTH_TRUNC] ||
304 attrs[XFRMA_ALG_AEAD] ||
305 attrs[XFRMA_ALG_CRYPT] ||
306 attrs[XFRMA_ENCAP] ||
307 attrs[XFRMA_SEC_CTX] ||
308 attrs[XFRMA_TFCPAD]) {
309 NL_SET_ERR_MSG(extack, "Invalid attributes for DSTOPTS/ROUTING");
313 if (!attrs[XFRMA_COADDR]) {
314 NL_SET_ERR_MSG(extack, "Missing required COADDR attribute for DSTOPTS/ROUTING");
321 NL_SET_ERR_MSG(extack, "Unsupported protocol");
325 if ((err = verify_aead(attrs, extack)))
327 if ((err = verify_auth_trunc(attrs, extack)))
329 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH, extack)))
331 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT, extack)))
333 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP, extack)))
335 if ((err = verify_sec_ctx_len(attrs, extack)))
337 if ((err = verify_replay(p, attrs, extack)))
342 case XFRM_MODE_TRANSPORT:
343 case XFRM_MODE_TUNNEL:
344 case XFRM_MODE_ROUTEOPTIMIZATION:
349 NL_SET_ERR_MSG(extack, "Unsupported mode");
355 if (attrs[XFRMA_MTIMER_THRESH]) {
356 if (!attrs[XFRMA_ENCAP]) {
357 NL_SET_ERR_MSG(extack, "MTIMER_THRESH attribute can only be set on ENCAP states");
367 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
368 struct xfrm_algo_desc *(*get_byname)(const char *, int),
369 struct nlattr *rta, struct netlink_ext_ack *extack)
371 struct xfrm_algo *p, *ualg;
372 struct xfrm_algo_desc *algo;
377 ualg = nla_data(rta);
379 algo = get_byname(ualg->alg_name, 1);
381 NL_SET_ERR_MSG(extack, "Requested COMP algorithm not found");
384 *props = algo->desc.sadb_alg_id;
386 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
390 strcpy(p->alg_name, algo->name);
395 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta,
396 struct netlink_ext_ack *extack)
398 struct xfrm_algo *p, *ualg;
399 struct xfrm_algo_desc *algo;
404 ualg = nla_data(rta);
406 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
408 NL_SET_ERR_MSG(extack, "Requested CRYPT algorithm not found");
411 x->props.ealgo = algo->desc.sadb_alg_id;
413 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
417 strcpy(p->alg_name, algo->name);
419 x->geniv = algo->uinfo.encr.geniv;
423 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
424 struct nlattr *rta, struct netlink_ext_ack *extack)
426 struct xfrm_algo *ualg;
427 struct xfrm_algo_auth *p;
428 struct xfrm_algo_desc *algo;
433 ualg = nla_data(rta);
435 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
437 NL_SET_ERR_MSG(extack, "Requested AUTH algorithm not found");
440 *props = algo->desc.sadb_alg_id;
442 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
446 strcpy(p->alg_name, algo->name);
447 p->alg_key_len = ualg->alg_key_len;
448 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
449 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
455 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
456 struct nlattr *rta, struct netlink_ext_ack *extack)
458 struct xfrm_algo_auth *p, *ualg;
459 struct xfrm_algo_desc *algo;
464 ualg = nla_data(rta);
466 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
468 NL_SET_ERR_MSG(extack, "Requested AUTH_TRUNC algorithm not found");
471 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) {
472 NL_SET_ERR_MSG(extack, "Invalid length requested for truncated ICV");
475 *props = algo->desc.sadb_alg_id;
477 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
481 strcpy(p->alg_name, algo->name);
482 if (!p->alg_trunc_len)
483 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
489 static int attach_aead(struct xfrm_state *x, struct nlattr *rta,
490 struct netlink_ext_ack *extack)
492 struct xfrm_algo_aead *p, *ualg;
493 struct xfrm_algo_desc *algo;
498 ualg = nla_data(rta);
500 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
502 NL_SET_ERR_MSG(extack, "Requested AEAD algorithm not found");
505 x->props.ealgo = algo->desc.sadb_alg_id;
507 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
511 strcpy(p->alg_name, algo->name);
513 x->geniv = algo->uinfo.aead.geniv;
517 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
520 struct xfrm_replay_state_esn *up;
523 if (!replay_esn || !rp)
527 ulen = xfrm_replay_state_esn_len(up);
529 /* Check the overall length and the internal bitmap length to avoid
530 * potential overflow. */
531 if (nla_len(rp) < (int)ulen ||
532 xfrm_replay_state_esn_len(replay_esn) != ulen ||
533 replay_esn->bmp_len != up->bmp_len)
536 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
542 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
543 struct xfrm_replay_state_esn **preplay_esn,
546 struct xfrm_replay_state_esn *p, *pp, *up;
547 unsigned int klen, ulen;
553 klen = xfrm_replay_state_esn_len(up);
554 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
556 p = kzalloc(klen, GFP_KERNEL);
560 pp = kzalloc(klen, GFP_KERNEL);
567 memcpy(pp, up, ulen);
575 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
577 unsigned int len = 0;
580 len += sizeof(struct xfrm_user_sec_ctx);
581 len += xfrm_ctx->ctx_len;
586 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
588 memcpy(&x->id, &p->id, sizeof(x->id));
589 memcpy(&x->sel, &p->sel, sizeof(x->sel));
590 memcpy(&x->lft, &p->lft, sizeof(x->lft));
591 x->props.mode = p->mode;
592 x->props.replay_window = min_t(unsigned int, p->replay_window,
593 sizeof(x->replay.bitmap) * 8);
594 x->props.reqid = p->reqid;
595 x->props.family = p->family;
596 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
597 x->props.flags = p->flags;
599 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
600 x->sel.family = p->family;
604 * someday when pfkey also has support, we could have the code
605 * somehow made shareable and move it to xfrm_state.c - JHS
608 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
611 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
612 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
613 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
614 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
615 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
616 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
619 struct xfrm_replay_state_esn *replay_esn;
620 replay_esn = nla_data(re);
621 memcpy(x->replay_esn, replay_esn,
622 xfrm_replay_state_esn_len(replay_esn));
623 memcpy(x->preplay_esn, replay_esn,
624 xfrm_replay_state_esn_len(replay_esn));
628 struct xfrm_replay_state *replay;
629 replay = nla_data(rp);
630 memcpy(&x->replay, replay, sizeof(*replay));
631 memcpy(&x->preplay, replay, sizeof(*replay));
635 struct xfrm_lifetime_cur *ltime;
636 ltime = nla_data(lt);
637 x->curlft.bytes = ltime->bytes;
638 x->curlft.packets = ltime->packets;
639 x->curlft.add_time = ltime->add_time;
640 x->curlft.use_time = ltime->use_time;
644 x->replay_maxage = nla_get_u32(et);
647 x->replay_maxdiff = nla_get_u32(rt);
650 x->mapping_maxage = nla_get_u32(mt);
653 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
655 if (attrs[XFRMA_SET_MARK]) {
656 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
657 if (attrs[XFRMA_SET_MARK_MASK])
658 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
666 static struct xfrm_state *xfrm_state_construct(struct net *net,
667 struct xfrm_usersa_info *p,
668 struct nlattr **attrs,
670 struct netlink_ext_ack *extack)
672 struct xfrm_state *x = xfrm_state_alloc(net);
678 copy_from_user_state(x, p);
680 if (attrs[XFRMA_ENCAP]) {
681 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
682 sizeof(*x->encap), GFP_KERNEL);
683 if (x->encap == NULL)
687 if (attrs[XFRMA_COADDR]) {
688 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
689 sizeof(*x->coaddr), GFP_KERNEL);
690 if (x->coaddr == NULL)
694 if (attrs[XFRMA_SA_EXTRA_FLAGS])
695 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
697 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD], extack)))
699 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
700 attrs[XFRMA_ALG_AUTH_TRUNC], extack)))
702 if (!x->props.aalgo) {
703 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
704 attrs[XFRMA_ALG_AUTH], extack)))
707 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT], extack)))
709 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
710 xfrm_calg_get_byname,
711 attrs[XFRMA_ALG_COMP], extack)))
714 if (attrs[XFRMA_TFCPAD])
715 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
717 xfrm_mark_get(attrs, &x->mark);
719 xfrm_smark_init(attrs, &x->props.smark);
721 if (attrs[XFRMA_IF_ID])
722 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
724 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV], extack);
728 if (attrs[XFRMA_SEC_CTX]) {
729 err = security_xfrm_state_alloc(x,
730 nla_data(attrs[XFRMA_SEC_CTX]));
735 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
736 attrs[XFRMA_REPLAY_ESN_VAL])))
740 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
741 /* sysctl_xfrm_aevent_etime is in 100ms units */
742 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
744 if ((err = xfrm_init_replay(x, extack)))
747 /* override default values from above */
748 xfrm_update_ae_params(x, attrs, 0);
750 /* configure the hardware if offload is requested */
751 if (attrs[XFRMA_OFFLOAD_DEV]) {
752 err = xfrm_dev_state_add(net, x,
753 nla_data(attrs[XFRMA_OFFLOAD_DEV]),
762 x->km.state = XFRM_STATE_DEAD;
769 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
770 struct nlattr **attrs, struct netlink_ext_ack *extack)
772 struct net *net = sock_net(skb->sk);
773 struct xfrm_usersa_info *p = nlmsg_data(nlh);
774 struct xfrm_state *x;
778 err = verify_newsa_info(p, attrs, extack);
782 x = xfrm_state_construct(net, p, attrs, &err, extack);
787 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
788 err = xfrm_state_add(x);
790 err = xfrm_state_update(x);
792 xfrm_audit_state_add(x, err ? 0 : 1, true);
795 x->km.state = XFRM_STATE_DEAD;
796 xfrm_dev_state_delete(x);
801 if (x->km.state == XFRM_STATE_VOID)
802 x->km.state = XFRM_STATE_VALID;
804 c.seq = nlh->nlmsg_seq;
805 c.portid = nlh->nlmsg_pid;
806 c.event = nlh->nlmsg_type;
808 km_state_notify(x, &c);
814 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
815 struct xfrm_usersa_id *p,
816 struct nlattr **attrs,
819 struct xfrm_state *x = NULL;
822 u32 mark = xfrm_mark_get(attrs, &m);
824 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
826 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
828 xfrm_address_t *saddr = NULL;
830 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
837 x = xfrm_state_lookup_byaddr(net, mark,
839 p->proto, p->family);
848 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
849 struct nlattr **attrs, struct netlink_ext_ack *extack)
851 struct net *net = sock_net(skb->sk);
852 struct xfrm_state *x;
855 struct xfrm_usersa_id *p = nlmsg_data(nlh);
857 x = xfrm_user_state_lookup(net, p, attrs, &err);
861 if ((err = security_xfrm_state_delete(x)) != 0)
864 if (xfrm_state_kern(x)) {
869 err = xfrm_state_delete(x);
874 c.seq = nlh->nlmsg_seq;
875 c.portid = nlh->nlmsg_pid;
876 c.event = nlh->nlmsg_type;
877 km_state_notify(x, &c);
880 xfrm_audit_state_delete(x, err ? 0 : 1, true);
885 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
887 memset(p, 0, sizeof(*p));
888 memcpy(&p->id, &x->id, sizeof(p->id));
889 memcpy(&p->sel, &x->sel, sizeof(p->sel));
890 memcpy(&p->lft, &x->lft, sizeof(p->lft));
891 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
892 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
893 put_unaligned(x->stats.replay, &p->stats.replay);
894 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
895 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
896 p->mode = x->props.mode;
897 p->replay_window = x->props.replay_window;
898 p->reqid = x->props.reqid;
899 p->family = x->props.family;
900 p->flags = x->props.flags;
904 struct xfrm_dump_info {
905 struct sk_buff *in_skb;
906 struct sk_buff *out_skb;
911 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
913 struct xfrm_user_sec_ctx *uctx;
915 int ctx_size = sizeof(*uctx) + s->ctx_len;
917 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
921 uctx = nla_data(attr);
922 uctx->exttype = XFRMA_SEC_CTX;
923 uctx->len = ctx_size;
924 uctx->ctx_doi = s->ctx_doi;
925 uctx->ctx_alg = s->ctx_alg;
926 uctx->ctx_len = s->ctx_len;
927 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
932 static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
934 struct xfrm_user_offload *xuo;
937 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
941 xuo = nla_data(attr);
942 memset(xuo, 0, sizeof(*xuo));
943 xuo->ifindex = xso->dev->ifindex;
944 if (xso->dir == XFRM_DEV_OFFLOAD_IN)
945 xuo->flags = XFRM_OFFLOAD_INBOUND;
950 static bool xfrm_redact(void)
952 return IS_ENABLED(CONFIG_SECURITY) &&
953 security_locked_down(LOCKDOWN_XFRM_SECRET);
956 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
958 struct xfrm_algo *algo;
959 struct xfrm_algo_auth *ap;
961 bool redact_secret = xfrm_redact();
963 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
964 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
967 algo = nla_data(nla);
968 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
970 if (redact_secret && auth->alg_key_len)
971 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
973 memcpy(algo->alg_key, auth->alg_key,
974 (auth->alg_key_len + 7) / 8);
975 algo->alg_key_len = auth->alg_key_len;
977 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
981 memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
982 if (redact_secret && auth->alg_key_len)
983 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
985 memcpy(ap->alg_key, auth->alg_key,
986 (auth->alg_key_len + 7) / 8);
990 static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
992 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
993 struct xfrm_algo_aead *ap;
994 bool redact_secret = xfrm_redact();
1000 memcpy(ap, aead, sizeof(*aead));
1002 if (redact_secret && aead->alg_key_len)
1003 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
1005 memcpy(ap->alg_key, aead->alg_key,
1006 (aead->alg_key_len + 7) / 8);
1010 static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
1012 struct xfrm_algo *ap;
1013 bool redact_secret = xfrm_redact();
1014 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
1015 xfrm_alg_len(ealg));
1020 memcpy(ap, ealg, sizeof(*ealg));
1022 if (redact_secret && ealg->alg_key_len)
1023 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
1025 memcpy(ap->alg_key, ealg->alg_key,
1026 (ealg->alg_key_len + 7) / 8);
1031 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
1036 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
1038 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
1043 /* Don't change this without updating xfrm_sa_len! */
1044 static int copy_to_user_state_extra(struct xfrm_state *x,
1045 struct xfrm_usersa_info *p,
1046 struct sk_buff *skb)
1050 copy_to_user_state(x, p);
1052 if (x->props.extra_flags) {
1053 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
1054 x->props.extra_flags);
1060 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
1065 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
1071 ret = copy_to_user_aead(x->aead, skb);
1076 ret = copy_to_user_auth(x->aalg, skb);
1081 ret = copy_to_user_ealg(x->ealg, skb);
1086 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1091 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1096 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1100 ret = xfrm_mark_put(skb, &x->mark);
1104 ret = xfrm_smark_put(skb, &x->props.smark);
1109 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1110 xfrm_replay_state_esn_len(x->replay_esn),
1113 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1118 ret = copy_user_offload(&x->xso, skb);
1122 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1127 ret = copy_sec_ctx(x->security, skb);
1131 if (x->mapping_maxage)
1132 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
1137 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1139 struct xfrm_dump_info *sp = ptr;
1140 struct sk_buff *in_skb = sp->in_skb;
1141 struct sk_buff *skb = sp->out_skb;
1142 struct xfrm_translator *xtr;
1143 struct xfrm_usersa_info *p;
1144 struct nlmsghdr *nlh;
1147 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1148 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1152 p = nlmsg_data(nlh);
1154 err = copy_to_user_state_extra(x, p, skb);
1156 nlmsg_cancel(skb, nlh);
1159 nlmsg_end(skb, nlh);
1161 xtr = xfrm_get_translator();
1163 err = xtr->alloc_compat(skb, nlh);
1165 xfrm_put_translator(xtr);
1167 nlmsg_cancel(skb, nlh);
1175 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1177 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1178 struct sock *sk = cb->skb->sk;
1179 struct net *net = sock_net(sk);
1182 xfrm_state_walk_done(walk, net);
1186 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1188 struct net *net = sock_net(skb->sk);
1189 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1190 struct xfrm_dump_info info;
1192 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1193 sizeof(cb->args) - sizeof(cb->args[0]));
1195 info.in_skb = cb->skb;
1197 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1198 info.nlmsg_flags = NLM_F_MULTI;
1201 struct nlattr *attrs[XFRMA_MAX+1];
1202 struct xfrm_address_filter *filter = NULL;
1206 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1207 xfrma_policy, cb->extack);
1211 if (attrs[XFRMA_ADDRESS_FILTER]) {
1212 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1213 sizeof(*filter), GFP_KERNEL);
1218 if (attrs[XFRMA_PROTO])
1219 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1221 xfrm_state_walk_init(walk, proto, filter);
1225 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1230 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1231 struct xfrm_state *x, u32 seq)
1233 struct xfrm_dump_info info;
1234 struct sk_buff *skb;
1237 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1239 return ERR_PTR(-ENOMEM);
1241 info.in_skb = in_skb;
1243 info.nlmsg_seq = seq;
1244 info.nlmsg_flags = 0;
1246 err = dump_one_state(x, 0, &info);
1249 return ERR_PTR(err);
1255 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1256 * Must be called with RCU read lock.
1258 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1259 u32 pid, unsigned int group)
1261 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1262 struct xfrm_translator *xtr;
1269 xtr = xfrm_get_translator();
1271 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1273 xfrm_put_translator(xtr);
1280 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1283 static inline unsigned int xfrm_spdinfo_msgsize(void)
1285 return NLMSG_ALIGN(4)
1286 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1287 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1288 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1289 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1292 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1293 u32 portid, u32 seq, u32 flags)
1295 struct xfrmk_spdinfo si;
1296 struct xfrmu_spdinfo spc;
1297 struct xfrmu_spdhinfo sph;
1298 struct xfrmu_spdhthresh spt4, spt6;
1299 struct nlmsghdr *nlh;
1304 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1305 if (nlh == NULL) /* shouldn't really happen ... */
1308 f = nlmsg_data(nlh);
1310 xfrm_spd_getinfo(net, &si);
1311 spc.incnt = si.incnt;
1312 spc.outcnt = si.outcnt;
1313 spc.fwdcnt = si.fwdcnt;
1314 spc.inscnt = si.inscnt;
1315 spc.outscnt = si.outscnt;
1316 spc.fwdscnt = si.fwdscnt;
1317 sph.spdhcnt = si.spdhcnt;
1318 sph.spdhmcnt = si.spdhmcnt;
1321 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1323 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1324 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1325 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1326 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1327 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1329 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1331 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1333 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1335 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1337 nlmsg_cancel(skb, nlh);
1341 nlmsg_end(skb, nlh);
1345 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1346 struct nlattr **attrs,
1347 struct netlink_ext_ack *extack)
1349 struct net *net = sock_net(skb->sk);
1350 struct xfrmu_spdhthresh *thresh4 = NULL;
1351 struct xfrmu_spdhthresh *thresh6 = NULL;
1353 /* selector prefixlen thresholds to hash policies */
1354 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1355 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1357 if (nla_len(rta) < sizeof(*thresh4))
1359 thresh4 = nla_data(rta);
1360 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1363 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1364 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1366 if (nla_len(rta) < sizeof(*thresh6))
1368 thresh6 = nla_data(rta);
1369 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1373 if (thresh4 || thresh6) {
1374 write_seqlock(&net->xfrm.policy_hthresh.lock);
1376 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1377 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1380 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1381 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1383 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1385 xfrm_policy_hash_rebuild(net);
1391 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1392 struct nlattr **attrs,
1393 struct netlink_ext_ack *extack)
1395 struct net *net = sock_net(skb->sk);
1396 struct sk_buff *r_skb;
1397 u32 *flags = nlmsg_data(nlh);
1398 u32 sportid = NETLINK_CB(skb).portid;
1399 u32 seq = nlh->nlmsg_seq;
1402 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1406 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1409 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1412 static inline unsigned int xfrm_sadinfo_msgsize(void)
1414 return NLMSG_ALIGN(4)
1415 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1416 + nla_total_size(4); /* XFRMA_SAD_CNT */
1419 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1420 u32 portid, u32 seq, u32 flags)
1422 struct xfrmk_sadinfo si;
1423 struct xfrmu_sadhinfo sh;
1424 struct nlmsghdr *nlh;
1428 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1429 if (nlh == NULL) /* shouldn't really happen ... */
1432 f = nlmsg_data(nlh);
1434 xfrm_sad_getinfo(net, &si);
1436 sh.sadhmcnt = si.sadhmcnt;
1437 sh.sadhcnt = si.sadhcnt;
1439 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1441 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1443 nlmsg_cancel(skb, nlh);
1447 nlmsg_end(skb, nlh);
1451 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1452 struct nlattr **attrs,
1453 struct netlink_ext_ack *extack)
1455 struct net *net = sock_net(skb->sk);
1456 struct sk_buff *r_skb;
1457 u32 *flags = nlmsg_data(nlh);
1458 u32 sportid = NETLINK_CB(skb).portid;
1459 u32 seq = nlh->nlmsg_seq;
1462 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1466 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1469 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1472 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1473 struct nlattr **attrs, struct netlink_ext_ack *extack)
1475 struct net *net = sock_net(skb->sk);
1476 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1477 struct xfrm_state *x;
1478 struct sk_buff *resp_skb;
1481 x = xfrm_user_state_lookup(net, p, attrs, &err);
1485 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1486 if (IS_ERR(resp_skb)) {
1487 err = PTR_ERR(resp_skb);
1489 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1496 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1497 struct nlattr **attrs,
1498 struct netlink_ext_ack *extack)
1500 struct net *net = sock_net(skb->sk);
1501 struct xfrm_state *x;
1502 struct xfrm_userspi_info *p;
1503 struct xfrm_translator *xtr;
1504 struct sk_buff *resp_skb;
1505 xfrm_address_t *daddr;
1512 p = nlmsg_data(nlh);
1513 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1517 family = p->info.family;
1518 daddr = &p->info.id.daddr;
1522 mark = xfrm_mark_get(attrs, &m);
1524 if (attrs[XFRMA_IF_ID])
1525 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1528 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1529 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1536 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1537 if_id, p->info.id.proto, daddr,
1544 err = xfrm_alloc_spi(x, p->min, p->max);
1548 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1549 if (IS_ERR(resp_skb)) {
1550 err = PTR_ERR(resp_skb);
1554 xtr = xfrm_get_translator();
1556 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1558 xfrm_put_translator(xtr);
1560 kfree_skb(resp_skb);
1565 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1573 static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack)
1576 case XFRM_POLICY_IN:
1577 case XFRM_POLICY_OUT:
1578 case XFRM_POLICY_FWD:
1582 NL_SET_ERR_MSG(extack, "Invalid policy direction");
1589 static int verify_policy_type(u8 type, struct netlink_ext_ack *extack)
1592 case XFRM_POLICY_TYPE_MAIN:
1593 #ifdef CONFIG_XFRM_SUB_POLICY
1594 case XFRM_POLICY_TYPE_SUB:
1599 NL_SET_ERR_MSG(extack, "Invalid policy type");
1606 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p,
1607 struct netlink_ext_ack *extack)
1612 case XFRM_SHARE_ANY:
1613 case XFRM_SHARE_SESSION:
1614 case XFRM_SHARE_USER:
1615 case XFRM_SHARE_UNIQUE:
1619 NL_SET_ERR_MSG(extack, "Invalid policy share");
1623 switch (p->action) {
1624 case XFRM_POLICY_ALLOW:
1625 case XFRM_POLICY_BLOCK:
1629 NL_SET_ERR_MSG(extack, "Invalid policy action");
1633 switch (p->sel.family) {
1635 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
1636 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
1643 #if IS_ENABLED(CONFIG_IPV6)
1644 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
1645 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
1651 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
1652 return -EAFNOSUPPORT;
1656 NL_SET_ERR_MSG(extack, "Invalid selector family");
1660 ret = verify_policy_dir(p->dir, extack);
1663 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) {
1664 NL_SET_ERR_MSG(extack, "Policy index doesn't match direction");
1671 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1673 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1674 struct xfrm_user_sec_ctx *uctx;
1679 uctx = nla_data(rt);
1680 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1683 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1689 for (i = 0; i < nr; i++, ut++) {
1690 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1692 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1693 memcpy(&t->saddr, &ut->saddr,
1694 sizeof(xfrm_address_t));
1695 t->reqid = ut->reqid;
1697 t->share = ut->share;
1698 t->optional = ut->optional;
1699 t->aalgos = ut->aalgos;
1700 t->ealgos = ut->ealgos;
1701 t->calgos = ut->calgos;
1702 /* If all masks are ~0, then we allow all algorithms. */
1703 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1704 t->encap_family = ut->family;
1708 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family,
1709 struct netlink_ext_ack *extack)
1714 if (nr > XFRM_MAX_DEPTH) {
1715 NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")");
1719 prev_family = family;
1721 for (i = 0; i < nr; i++) {
1722 /* We never validated the ut->family value, so many
1723 * applications simply leave it at zero. The check was
1724 * never made and ut->family was ignored because all
1725 * templates could be assumed to have the same family as
1726 * the policy itself. Now that we will have ipv4-in-ipv6
1727 * and ipv6-in-ipv4 tunnels, this is no longer true.
1730 ut[i].family = family;
1732 switch (ut[i].mode) {
1733 case XFRM_MODE_TUNNEL:
1734 case XFRM_MODE_BEET:
1737 if (ut[i].family != prev_family) {
1738 NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change");
1743 if (ut[i].mode >= XFRM_MODE_MAX) {
1744 NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")");
1748 prev_family = ut[i].family;
1750 switch (ut[i].family) {
1753 #if IS_ENABLED(CONFIG_IPV6)
1758 NL_SET_ERR_MSG(extack, "Invalid family in template");
1762 if (!xfrm_id_proto_valid(ut[i].id.proto)) {
1763 NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template");
1771 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs,
1772 struct netlink_ext_ack *extack)
1774 struct nlattr *rt = attrs[XFRMA_TMPL];
1779 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1780 int nr = nla_len(rt) / sizeof(*utmpl);
1783 err = validate_tmpl(nr, utmpl, pol->family, extack);
1787 copy_templates(pol, utmpl, nr);
1792 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs,
1793 struct netlink_ext_ack *extack)
1795 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1796 struct xfrm_userpolicy_type *upt;
1797 u8 type = XFRM_POLICY_TYPE_MAIN;
1805 err = verify_policy_type(type, extack);
1813 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1815 xp->priority = p->priority;
1816 xp->index = p->index;
1817 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1818 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1819 xp->action = p->action;
1820 xp->flags = p->flags;
1821 xp->family = p->sel.family;
1822 /* XXX xp->share = p->share; */
1825 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1827 memset(p, 0, sizeof(*p));
1828 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1829 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1830 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1831 p->priority = xp->priority;
1832 p->index = xp->index;
1833 p->sel.family = xp->family;
1835 p->action = xp->action;
1836 p->flags = xp->flags;
1837 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1840 static struct xfrm_policy *xfrm_policy_construct(struct net *net,
1841 struct xfrm_userpolicy_info *p,
1842 struct nlattr **attrs,
1844 struct netlink_ext_ack *extack)
1846 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1854 copy_from_user_policy(xp, p);
1856 err = copy_from_user_policy_type(&xp->type, attrs, extack);
1860 if (!(err = copy_from_user_tmpl(xp, attrs, extack)))
1861 err = copy_from_user_sec_ctx(xp, attrs);
1865 xfrm_mark_get(attrs, &xp->mark);
1867 if (attrs[XFRMA_IF_ID])
1868 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1874 xfrm_policy_destroy(xp);
1878 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1879 struct nlattr **attrs,
1880 struct netlink_ext_ack *extack)
1882 struct net *net = sock_net(skb->sk);
1883 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1884 struct xfrm_policy *xp;
1889 err = verify_newpolicy_info(p, extack);
1892 err = verify_sec_ctx_len(attrs, extack);
1896 xp = xfrm_policy_construct(net, p, attrs, &err, extack);
1900 /* shouldn't excl be based on nlh flags??
1901 * Aha! this is anti-netlink really i.e more pfkey derived
1902 * in netlink excl is a flag and you wouldn't need
1903 * a type XFRM_MSG_UPDPOLICY - JHS */
1904 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1905 err = xfrm_policy_insert(p->dir, xp, excl);
1906 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1909 security_xfrm_policy_free(xp->security);
1914 c.event = nlh->nlmsg_type;
1915 c.seq = nlh->nlmsg_seq;
1916 c.portid = nlh->nlmsg_pid;
1917 km_policy_notify(xp, p->dir, &c);
1924 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1926 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1929 if (xp->xfrm_nr == 0)
1932 for (i = 0; i < xp->xfrm_nr; i++) {
1933 struct xfrm_user_tmpl *up = &vec[i];
1934 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1936 memset(up, 0, sizeof(*up));
1937 memcpy(&up->id, &kp->id, sizeof(up->id));
1938 up->family = kp->encap_family;
1939 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1940 up->reqid = kp->reqid;
1941 up->mode = kp->mode;
1942 up->share = kp->share;
1943 up->optional = kp->optional;
1944 up->aalgos = kp->aalgos;
1945 up->ealgos = kp->ealgos;
1946 up->calgos = kp->calgos;
1949 return nla_put(skb, XFRMA_TMPL,
1950 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1953 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1956 return copy_sec_ctx(x->security, skb);
1961 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1964 return copy_sec_ctx(xp->security, skb);
1967 static inline unsigned int userpolicy_type_attrsize(void)
1969 #ifdef CONFIG_XFRM_SUB_POLICY
1970 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1976 #ifdef CONFIG_XFRM_SUB_POLICY
1977 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1979 struct xfrm_userpolicy_type upt;
1981 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1982 memset(&upt, 0, sizeof(upt));
1985 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1989 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1995 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1997 struct xfrm_dump_info *sp = ptr;
1998 struct xfrm_userpolicy_info *p;
1999 struct sk_buff *in_skb = sp->in_skb;
2000 struct sk_buff *skb = sp->out_skb;
2001 struct xfrm_translator *xtr;
2002 struct nlmsghdr *nlh;
2005 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
2006 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
2010 p = nlmsg_data(nlh);
2011 copy_to_user_policy(xp, p, dir);
2012 err = copy_to_user_tmpl(xp, skb);
2014 err = copy_to_user_sec_ctx(xp, skb);
2016 err = copy_to_user_policy_type(xp->type, skb);
2018 err = xfrm_mark_put(skb, &xp->mark);
2020 err = xfrm_if_id_put(skb, xp->if_id);
2022 nlmsg_cancel(skb, nlh);
2025 nlmsg_end(skb, nlh);
2027 xtr = xfrm_get_translator();
2029 err = xtr->alloc_compat(skb, nlh);
2031 xfrm_put_translator(xtr);
2033 nlmsg_cancel(skb, nlh);
2041 static int xfrm_dump_policy_done(struct netlink_callback *cb)
2043 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2044 struct net *net = sock_net(cb->skb->sk);
2046 xfrm_policy_walk_done(walk, net);
2050 static int xfrm_dump_policy_start(struct netlink_callback *cb)
2052 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2054 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
2056 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
2060 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
2062 struct net *net = sock_net(skb->sk);
2063 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2064 struct xfrm_dump_info info;
2066 info.in_skb = cb->skb;
2068 info.nlmsg_seq = cb->nlh->nlmsg_seq;
2069 info.nlmsg_flags = NLM_F_MULTI;
2071 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
2076 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
2077 struct xfrm_policy *xp,
2080 struct xfrm_dump_info info;
2081 struct sk_buff *skb;
2084 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2086 return ERR_PTR(-ENOMEM);
2088 info.in_skb = in_skb;
2090 info.nlmsg_seq = seq;
2091 info.nlmsg_flags = 0;
2093 err = dump_one_policy(xp, dir, 0, &info);
2096 return ERR_PTR(err);
2102 static int xfrm_notify_userpolicy(struct net *net)
2104 struct xfrm_userpolicy_default *up;
2105 int len = NLMSG_ALIGN(sizeof(*up));
2106 struct nlmsghdr *nlh;
2107 struct sk_buff *skb;
2110 skb = nlmsg_new(len, GFP_ATOMIC);
2114 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
2120 up = nlmsg_data(nlh);
2121 up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2122 up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2123 up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2125 nlmsg_end(skb, nlh);
2128 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2134 static bool xfrm_userpolicy_is_valid(__u8 policy)
2136 return policy == XFRM_USERPOLICY_BLOCK ||
2137 policy == XFRM_USERPOLICY_ACCEPT;
2140 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2141 struct nlattr **attrs, struct netlink_ext_ack *extack)
2143 struct net *net = sock_net(skb->sk);
2144 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2146 if (xfrm_userpolicy_is_valid(up->in))
2147 net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
2149 if (xfrm_userpolicy_is_valid(up->fwd))
2150 net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
2152 if (xfrm_userpolicy_is_valid(up->out))
2153 net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
2155 rt_genid_bump_all(net);
2157 xfrm_notify_userpolicy(net);
2161 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2162 struct nlattr **attrs, struct netlink_ext_ack *extack)
2164 struct sk_buff *r_skb;
2165 struct nlmsghdr *r_nlh;
2166 struct net *net = sock_net(skb->sk);
2167 struct xfrm_userpolicy_default *r_up;
2168 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2169 u32 portid = NETLINK_CB(skb).portid;
2170 u32 seq = nlh->nlmsg_seq;
2172 r_skb = nlmsg_new(len, GFP_ATOMIC);
2176 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2182 r_up = nlmsg_data(r_nlh);
2183 r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2184 r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2185 r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2186 nlmsg_end(r_skb, r_nlh);
2188 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2191 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2192 struct nlattr **attrs,
2193 struct netlink_ext_ack *extack)
2195 struct net *net = sock_net(skb->sk);
2196 struct xfrm_policy *xp;
2197 struct xfrm_userpolicy_id *p;
2198 u8 type = XFRM_POLICY_TYPE_MAIN;
2205 p = nlmsg_data(nlh);
2206 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2208 err = copy_from_user_policy_type(&type, attrs, extack);
2212 err = verify_policy_dir(p->dir, extack);
2216 if (attrs[XFRMA_IF_ID])
2217 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2219 xfrm_mark_get(attrs, &m);
2222 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2223 p->index, delete, &err);
2225 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2226 struct xfrm_sec_ctx *ctx;
2228 err = verify_sec_ctx_len(attrs, extack);
2234 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2236 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2240 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2241 &p->sel, ctx, delete, &err);
2242 security_xfrm_policy_free(ctx);
2248 struct sk_buff *resp_skb;
2250 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2251 if (IS_ERR(resp_skb)) {
2252 err = PTR_ERR(resp_skb);
2254 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
2255 NETLINK_CB(skb).portid);
2258 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2263 c.data.byid = p->index;
2264 c.event = nlh->nlmsg_type;
2265 c.seq = nlh->nlmsg_seq;
2266 c.portid = nlh->nlmsg_pid;
2267 km_policy_notify(xp, p->dir, &c);
2275 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2276 struct nlattr **attrs,
2277 struct netlink_ext_ack *extack)
2279 struct net *net = sock_net(skb->sk);
2281 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2284 err = xfrm_state_flush(net, p->proto, true, false);
2286 if (err == -ESRCH) /* empty table */
2290 c.data.proto = p->proto;
2291 c.event = nlh->nlmsg_type;
2292 c.seq = nlh->nlmsg_seq;
2293 c.portid = nlh->nlmsg_pid;
2295 km_state_notify(NULL, &c);
2300 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2302 unsigned int replay_size = x->replay_esn ?
2303 xfrm_replay_state_esn_len(x->replay_esn) :
2304 sizeof(struct xfrm_replay_state);
2306 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2307 + nla_total_size(replay_size)
2308 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2309 + nla_total_size(sizeof(struct xfrm_mark))
2310 + nla_total_size(4) /* XFRM_AE_RTHR */
2311 + nla_total_size(4); /* XFRM_AE_ETHR */
2314 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2316 struct xfrm_aevent_id *id;
2317 struct nlmsghdr *nlh;
2320 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2324 id = nlmsg_data(nlh);
2325 memset(&id->sa_id, 0, sizeof(id->sa_id));
2326 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2327 id->sa_id.spi = x->id.spi;
2328 id->sa_id.family = x->props.family;
2329 id->sa_id.proto = x->id.proto;
2330 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2331 id->reqid = x->props.reqid;
2332 id->flags = c->data.aevent;
2334 if (x->replay_esn) {
2335 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2336 xfrm_replay_state_esn_len(x->replay_esn),
2339 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2344 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2349 if (id->flags & XFRM_AE_RTHR) {
2350 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2354 if (id->flags & XFRM_AE_ETHR) {
2355 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2356 x->replay_maxage * 10 / HZ);
2360 err = xfrm_mark_put(skb, &x->mark);
2364 err = xfrm_if_id_put(skb, x->if_id);
2368 nlmsg_end(skb, nlh);
2372 nlmsg_cancel(skb, nlh);
2376 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2377 struct nlattr **attrs, struct netlink_ext_ack *extack)
2379 struct net *net = sock_net(skb->sk);
2380 struct xfrm_state *x;
2381 struct sk_buff *r_skb;
2386 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2387 struct xfrm_usersa_id *id = &p->sa_id;
2389 mark = xfrm_mark_get(attrs, &m);
2391 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2395 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2396 if (r_skb == NULL) {
2402 * XXX: is this lock really needed - none of the other
2403 * gets lock (the concern is things getting updated
2404 * while we are still reading) - jhs
2406 spin_lock_bh(&x->lock);
2407 c.data.aevent = p->flags;
2408 c.seq = nlh->nlmsg_seq;
2409 c.portid = nlh->nlmsg_pid;
2411 err = build_aevent(r_skb, x, &c);
2414 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2415 spin_unlock_bh(&x->lock);
2420 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2421 struct nlattr **attrs, struct netlink_ext_ack *extack)
2423 struct net *net = sock_net(skb->sk);
2424 struct xfrm_state *x;
2429 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2430 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2431 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2432 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2433 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2434 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2436 if (!lt && !rp && !re && !et && !rt)
2439 /* pedantic mode - thou shalt sayeth replaceth */
2440 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2443 mark = xfrm_mark_get(attrs, &m);
2445 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2449 if (x->km.state != XFRM_STATE_VALID)
2452 err = xfrm_replay_verify_len(x->replay_esn, re);
2456 spin_lock_bh(&x->lock);
2457 xfrm_update_ae_params(x, attrs, 1);
2458 spin_unlock_bh(&x->lock);
2460 c.event = nlh->nlmsg_type;
2461 c.seq = nlh->nlmsg_seq;
2462 c.portid = nlh->nlmsg_pid;
2463 c.data.aevent = XFRM_AE_CU;
2464 km_state_notify(x, &c);
2471 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2472 struct nlattr **attrs,
2473 struct netlink_ext_ack *extack)
2475 struct net *net = sock_net(skb->sk);
2477 u8 type = XFRM_POLICY_TYPE_MAIN;
2480 err = copy_from_user_policy_type(&type, attrs, extack);
2484 err = xfrm_policy_flush(net, type, true);
2486 if (err == -ESRCH) /* empty table */
2492 c.event = nlh->nlmsg_type;
2493 c.seq = nlh->nlmsg_seq;
2494 c.portid = nlh->nlmsg_pid;
2496 km_policy_notify(NULL, 0, &c);
2500 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2501 struct nlattr **attrs,
2502 struct netlink_ext_ack *extack)
2504 struct net *net = sock_net(skb->sk);
2505 struct xfrm_policy *xp;
2506 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2507 struct xfrm_userpolicy_info *p = &up->pol;
2508 u8 type = XFRM_POLICY_TYPE_MAIN;
2513 err = copy_from_user_policy_type(&type, attrs, extack);
2517 err = verify_policy_dir(p->dir, extack);
2521 if (attrs[XFRMA_IF_ID])
2522 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2524 xfrm_mark_get(attrs, &m);
2527 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2530 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2531 struct xfrm_sec_ctx *ctx;
2533 err = verify_sec_ctx_len(attrs, extack);
2539 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2541 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2545 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2546 &p->sel, ctx, 0, &err);
2547 security_xfrm_policy_free(ctx);
2552 if (unlikely(xp->walk.dead))
2557 xfrm_policy_delete(xp, p->dir);
2558 xfrm_audit_policy_delete(xp, 1, true);
2560 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2567 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2568 struct nlattr **attrs,
2569 struct netlink_ext_ack *extack)
2571 struct net *net = sock_net(skb->sk);
2572 struct xfrm_state *x;
2574 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2575 struct xfrm_usersa_info *p = &ue->state;
2577 u32 mark = xfrm_mark_get(attrs, &m);
2579 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2585 spin_lock_bh(&x->lock);
2587 if (x->km.state != XFRM_STATE_VALID)
2589 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2592 __xfrm_state_delete(x);
2593 xfrm_audit_state_delete(x, 1, true);
2597 spin_unlock_bh(&x->lock);
2602 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2603 struct nlattr **attrs,
2604 struct netlink_ext_ack *extack)
2606 struct net *net = sock_net(skb->sk);
2607 struct xfrm_policy *xp;
2608 struct xfrm_user_tmpl *ut;
2610 struct nlattr *rt = attrs[XFRMA_TMPL];
2611 struct xfrm_mark mark;
2613 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2614 struct xfrm_state *x = xfrm_state_alloc(net);
2620 xfrm_mark_get(attrs, &mark);
2622 err = verify_newpolicy_info(&ua->policy, extack);
2625 err = verify_sec_ctx_len(attrs, extack);
2630 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack);
2634 memcpy(&x->id, &ua->id, sizeof(ua->id));
2635 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2636 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2637 xp->mark.m = x->mark.m = mark.m;
2638 xp->mark.v = x->mark.v = mark.v;
2640 /* extract the templates and for each call km_key */
2641 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2642 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2643 memcpy(&x->id, &t->id, sizeof(x->id));
2644 x->props.mode = t->mode;
2645 x->props.reqid = t->reqid;
2646 x->props.family = ut->family;
2647 t->aalgos = ua->aalgos;
2648 t->ealgos = ua->ealgos;
2649 t->calgos = ua->calgos;
2650 err = km_query(x, t, xp);
2665 #ifdef CONFIG_XFRM_MIGRATE
2666 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2667 struct xfrm_kmaddress *k,
2668 struct nlattr **attrs, int *num)
2670 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2671 struct xfrm_user_migrate *um;
2675 struct xfrm_user_kmaddress *uk;
2677 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2678 memcpy(&k->local, &uk->local, sizeof(k->local));
2679 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2680 k->family = uk->family;
2681 k->reserved = uk->reserved;
2685 num_migrate = nla_len(rt) / sizeof(*um);
2687 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2690 for (i = 0; i < num_migrate; i++, um++, ma++) {
2691 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2692 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2693 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2694 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2696 ma->proto = um->proto;
2697 ma->mode = um->mode;
2698 ma->reqid = um->reqid;
2700 ma->old_family = um->old_family;
2701 ma->new_family = um->new_family;
2708 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2709 struct nlattr **attrs, struct netlink_ext_ack *extack)
2711 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2712 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2713 struct xfrm_kmaddress km, *kmp;
2717 struct net *net = sock_net(skb->sk);
2718 struct xfrm_encap_tmpl *encap = NULL;
2721 if (attrs[XFRMA_MIGRATE] == NULL)
2724 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2726 err = copy_from_user_policy_type(&type, attrs, extack);
2730 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2737 if (attrs[XFRMA_ENCAP]) {
2738 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2739 sizeof(*encap), GFP_KERNEL);
2744 if (attrs[XFRMA_IF_ID])
2745 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2747 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
2754 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2755 struct nlattr **attrs, struct netlink_ext_ack *extack)
2757 return -ENOPROTOOPT;
2761 #ifdef CONFIG_XFRM_MIGRATE
2762 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2764 struct xfrm_user_migrate um;
2766 memset(&um, 0, sizeof(um));
2767 um.proto = m->proto;
2769 um.reqid = m->reqid;
2770 um.old_family = m->old_family;
2771 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2772 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2773 um.new_family = m->new_family;
2774 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2775 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2777 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2780 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2782 struct xfrm_user_kmaddress uk;
2784 memset(&uk, 0, sizeof(uk));
2785 uk.family = k->family;
2786 uk.reserved = k->reserved;
2787 memcpy(&uk.local, &k->local, sizeof(uk.local));
2788 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2790 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2793 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2796 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2797 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2798 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2799 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2800 + userpolicy_type_attrsize();
2803 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2804 int num_migrate, const struct xfrm_kmaddress *k,
2805 const struct xfrm_selector *sel,
2806 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2808 const struct xfrm_migrate *mp;
2809 struct xfrm_userpolicy_id *pol_id;
2810 struct nlmsghdr *nlh;
2813 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2817 pol_id = nlmsg_data(nlh);
2818 /* copy data from selector, dir, and type to the pol_id */
2819 memset(pol_id, 0, sizeof(*pol_id));
2820 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2824 err = copy_to_user_kmaddress(k, skb);
2829 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2833 err = copy_to_user_policy_type(type, skb);
2836 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2837 err = copy_to_user_migrate(mp, skb);
2842 nlmsg_end(skb, nlh);
2846 nlmsg_cancel(skb, nlh);
2850 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2851 const struct xfrm_migrate *m, int num_migrate,
2852 const struct xfrm_kmaddress *k,
2853 const struct xfrm_encap_tmpl *encap)
2855 struct net *net = &init_net;
2856 struct sk_buff *skb;
2859 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2865 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2868 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2871 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2872 const struct xfrm_migrate *m, int num_migrate,
2873 const struct xfrm_kmaddress *k,
2874 const struct xfrm_encap_tmpl *encap)
2876 return -ENOPROTOOPT;
2880 #define XMSGSIZE(type) sizeof(struct type)
2882 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2883 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2884 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2885 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2886 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2887 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2888 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2889 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2890 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2891 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2892 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2893 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2894 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2895 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2896 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2897 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2898 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2899 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2900 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2901 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2902 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2903 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2904 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2905 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2907 EXPORT_SYMBOL_GPL(xfrm_msg_min);
2911 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2912 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2913 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2914 [XFRMA_LASTUSED] = { .type = NLA_U64},
2915 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2916 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2917 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2918 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2919 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2920 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2921 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2922 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2923 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2924 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2925 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2926 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2927 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2928 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2929 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2930 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2931 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2932 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2933 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2934 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2935 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2936 [XFRMA_PROTO] = { .type = NLA_U8 },
2937 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2938 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2939 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2940 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2941 [XFRMA_IF_ID] = { .type = NLA_U32 },
2943 EXPORT_SYMBOL_GPL(xfrma_policy);
2945 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2946 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2947 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2950 static const struct xfrm_link {
2951 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **,
2952 struct netlink_ext_ack *);
2953 int (*start)(struct netlink_callback *);
2954 int (*dump)(struct sk_buff *, struct netlink_callback *);
2955 int (*done)(struct netlink_callback *);
2956 const struct nla_policy *nla_pol;
2958 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2959 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2960 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2961 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2962 .dump = xfrm_dump_sa,
2963 .done = xfrm_dump_sa_done },
2964 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2965 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2966 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2967 .start = xfrm_dump_policy_start,
2968 .dump = xfrm_dump_policy,
2969 .done = xfrm_dump_policy_done },
2970 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2971 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2972 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2973 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2974 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2975 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2976 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2977 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2978 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2979 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2980 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2981 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2982 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2983 .nla_pol = xfrma_spd_policy,
2984 .nla_max = XFRMA_SPD_MAX },
2985 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2986 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
2987 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
2990 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2991 struct netlink_ext_ack *extack)
2993 struct net *net = sock_net(skb->sk);
2994 struct nlattr *attrs[XFRMA_MAX+1];
2995 const struct xfrm_link *link;
2996 struct nlmsghdr *nlh64 = NULL;
2999 type = nlh->nlmsg_type;
3000 if (type > XFRM_MSG_MAX)
3003 type -= XFRM_MSG_BASE;
3004 link = &xfrm_dispatch[type];
3006 /* All operations require privileges, even GET */
3007 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
3010 if (in_compat_syscall()) {
3011 struct xfrm_translator *xtr = xfrm_get_translator();
3016 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
3017 link->nla_pol, extack);
3018 xfrm_put_translator(xtr);
3020 return PTR_ERR(nlh64);
3025 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
3026 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
3027 (nlh->nlmsg_flags & NLM_F_DUMP)) {
3028 struct netlink_dump_control c = {
3029 .start = link->start,
3034 if (link->dump == NULL) {
3039 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
3043 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
3044 link->nla_max ? : XFRMA_MAX,
3045 link->nla_pol ? : xfrma_policy, extack);
3049 if (link->doit == NULL) {
3054 err = link->doit(skb, nlh, attrs, extack);
3056 /* We need to free skb allocated in xfrm_alloc_compat() before
3057 * returning from this function, because consume_skb() won't take
3058 * care of frag_list since netlink destructor sets
3059 * sbk->head to NULL. (see netlink_skb_destructor())
3061 if (skb_has_frag_list(skb)) {
3062 kfree_skb(skb_shinfo(skb)->frag_list);
3063 skb_shinfo(skb)->frag_list = NULL;
3071 static void xfrm_netlink_rcv(struct sk_buff *skb)
3073 struct net *net = sock_net(skb->sk);
3075 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
3076 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
3077 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
3080 static inline unsigned int xfrm_expire_msgsize(void)
3082 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
3083 + nla_total_size(sizeof(struct xfrm_mark));
3086 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
3088 struct xfrm_user_expire *ue;
3089 struct nlmsghdr *nlh;
3092 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
3096 ue = nlmsg_data(nlh);
3097 copy_to_user_state(x, &ue->state);
3098 ue->hard = (c->data.hard != 0) ? 1 : 0;
3099 /* clear the padding bytes */
3100 memset_after(ue, 0, hard);
3102 err = xfrm_mark_put(skb, &x->mark);
3106 err = xfrm_if_id_put(skb, x->if_id);
3110 nlmsg_end(skb, nlh);
3114 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
3116 struct net *net = xs_net(x);
3117 struct sk_buff *skb;
3119 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
3123 if (build_expire(skb, x, c) < 0) {
3128 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3131 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3133 struct net *net = xs_net(x);
3134 struct sk_buff *skb;
3137 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3141 err = build_aevent(skb, x, c);
3144 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3147 static int xfrm_notify_sa_flush(const struct km_event *c)
3149 struct net *net = c->net;
3150 struct xfrm_usersa_flush *p;
3151 struct nlmsghdr *nlh;
3152 struct sk_buff *skb;
3153 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
3155 skb = nlmsg_new(len, GFP_ATOMIC);
3159 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
3165 p = nlmsg_data(nlh);
3166 p->proto = c->data.proto;
3168 nlmsg_end(skb, nlh);
3170 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3173 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
3177 l += nla_total_size(aead_len(x->aead));
3179 l += nla_total_size(sizeof(struct xfrm_algo) +
3180 (x->aalg->alg_key_len + 7) / 8);
3181 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3184 l += nla_total_size(xfrm_alg_len(x->ealg));
3186 l += nla_total_size(sizeof(*x->calg));
3188 l += nla_total_size(sizeof(*x->encap));
3190 l += nla_total_size(sizeof(x->tfcpad));
3192 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3194 l += nla_total_size(sizeof(struct xfrm_replay_state));
3196 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3197 x->security->ctx_len);
3199 l += nla_total_size(sizeof(*x->coaddr));
3200 if (x->props.extra_flags)
3201 l += nla_total_size(sizeof(x->props.extra_flags));
3203 l += nla_total_size(sizeof(struct xfrm_user_offload));
3204 if (x->props.smark.v | x->props.smark.m) {
3205 l += nla_total_size(sizeof(x->props.smark.v));
3206 l += nla_total_size(sizeof(x->props.smark.m));
3209 l += nla_total_size(sizeof(x->if_id));
3211 /* Must count x->lastused as it may become non-zero behind our back. */
3212 l += nla_total_size_64bit(sizeof(u64));
3214 if (x->mapping_maxage)
3215 l += nla_total_size(sizeof(x->mapping_maxage));
3220 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
3222 struct net *net = xs_net(x);
3223 struct xfrm_usersa_info *p;
3224 struct xfrm_usersa_id *id;
3225 struct nlmsghdr *nlh;
3226 struct sk_buff *skb;
3227 unsigned int len = xfrm_sa_len(x);
3228 unsigned int headlen;
3231 headlen = sizeof(*p);
3232 if (c->event == XFRM_MSG_DELSA) {
3233 len += nla_total_size(headlen);
3234 headlen = sizeof(*id);
3235 len += nla_total_size(sizeof(struct xfrm_mark));
3237 len += NLMSG_ALIGN(headlen);
3239 skb = nlmsg_new(len, GFP_ATOMIC);
3243 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3248 p = nlmsg_data(nlh);
3249 if (c->event == XFRM_MSG_DELSA) {
3250 struct nlattr *attr;
3252 id = nlmsg_data(nlh);
3253 memset(id, 0, sizeof(*id));
3254 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3255 id->spi = x->id.spi;
3256 id->family = x->props.family;
3257 id->proto = x->id.proto;
3259 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
3266 err = copy_to_user_state_extra(x, p, skb);
3270 nlmsg_end(skb, nlh);
3272 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3279 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
3283 case XFRM_MSG_EXPIRE:
3284 return xfrm_exp_state_notify(x, c);
3285 case XFRM_MSG_NEWAE:
3286 return xfrm_aevent_state_notify(x, c);
3287 case XFRM_MSG_DELSA:
3288 case XFRM_MSG_UPDSA:
3289 case XFRM_MSG_NEWSA:
3290 return xfrm_notify_sa(x, c);
3291 case XFRM_MSG_FLUSHSA:
3292 return xfrm_notify_sa_flush(c);
3294 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3303 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3304 struct xfrm_policy *xp)
3306 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3307 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3308 + nla_total_size(sizeof(struct xfrm_mark))
3309 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3310 + userpolicy_type_attrsize();
3313 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
3314 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3316 __u32 seq = xfrm_get_acqseq();
3317 struct xfrm_user_acquire *ua;
3318 struct nlmsghdr *nlh;
3321 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3325 ua = nlmsg_data(nlh);
3326 memcpy(&ua->id, &x->id, sizeof(ua->id));
3327 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3328 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3329 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3330 ua->aalgos = xt->aalgos;
3331 ua->ealgos = xt->ealgos;
3332 ua->calgos = xt->calgos;
3333 ua->seq = x->km.seq = seq;
3335 err = copy_to_user_tmpl(xp, skb);
3337 err = copy_to_user_state_sec_ctx(x, skb);
3339 err = copy_to_user_policy_type(xp->type, skb);
3341 err = xfrm_mark_put(skb, &xp->mark);
3343 err = xfrm_if_id_put(skb, xp->if_id);
3345 nlmsg_cancel(skb, nlh);
3349 nlmsg_end(skb, nlh);
3353 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3354 struct xfrm_policy *xp)
3356 struct net *net = xs_net(x);
3357 struct sk_buff *skb;
3360 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3364 err = build_acquire(skb, x, xt, xp);
3367 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3370 /* User gives us xfrm_user_policy_info followed by an array of 0
3371 * or more templates.
3373 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3374 u8 *data, int len, int *dir)
3376 struct net *net = sock_net(sk);
3377 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3378 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3379 struct xfrm_policy *xp;
3382 switch (sk->sk_family) {
3384 if (opt != IP_XFRM_POLICY) {
3389 #if IS_ENABLED(CONFIG_IPV6)
3391 if (opt != IPV6_XFRM_POLICY) {
3404 if (len < sizeof(*p) ||
3405 verify_newpolicy_info(p, NULL))
3408 nr = ((len - sizeof(*p)) / sizeof(*ut));
3409 if (validate_tmpl(nr, ut, p->sel.family, NULL))
3412 if (p->dir > XFRM_POLICY_OUT)
3415 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3421 copy_from_user_policy(xp, p);
3422 xp->type = XFRM_POLICY_TYPE_MAIN;
3423 copy_templates(xp, ut, nr);
3430 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3432 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3433 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3434 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3435 + nla_total_size(sizeof(struct xfrm_mark))
3436 + userpolicy_type_attrsize();
3439 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3440 int dir, const struct km_event *c)
3442 struct xfrm_user_polexpire *upe;
3443 int hard = c->data.hard;
3444 struct nlmsghdr *nlh;
3447 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3451 upe = nlmsg_data(nlh);
3452 copy_to_user_policy(xp, &upe->pol, dir);
3453 err = copy_to_user_tmpl(xp, skb);
3455 err = copy_to_user_sec_ctx(xp, skb);
3457 err = copy_to_user_policy_type(xp->type, skb);
3459 err = xfrm_mark_put(skb, &xp->mark);
3461 err = xfrm_if_id_put(skb, xp->if_id);
3463 nlmsg_cancel(skb, nlh);
3468 nlmsg_end(skb, nlh);
3472 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3474 struct net *net = xp_net(xp);
3475 struct sk_buff *skb;
3478 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3482 err = build_polexpire(skb, xp, dir, c);
3485 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3488 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3490 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3491 struct net *net = xp_net(xp);
3492 struct xfrm_userpolicy_info *p;
3493 struct xfrm_userpolicy_id *id;
3494 struct nlmsghdr *nlh;
3495 struct sk_buff *skb;
3496 unsigned int headlen;
3499 headlen = sizeof(*p);
3500 if (c->event == XFRM_MSG_DELPOLICY) {
3501 len += nla_total_size(headlen);
3502 headlen = sizeof(*id);
3504 len += userpolicy_type_attrsize();
3505 len += nla_total_size(sizeof(struct xfrm_mark));
3506 len += NLMSG_ALIGN(headlen);
3508 skb = nlmsg_new(len, GFP_ATOMIC);
3512 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3517 p = nlmsg_data(nlh);
3518 if (c->event == XFRM_MSG_DELPOLICY) {
3519 struct nlattr *attr;
3521 id = nlmsg_data(nlh);
3522 memset(id, 0, sizeof(*id));
3525 id->index = xp->index;
3527 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3529 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3537 copy_to_user_policy(xp, p, dir);
3538 err = copy_to_user_tmpl(xp, skb);
3540 err = copy_to_user_policy_type(xp->type, skb);
3542 err = xfrm_mark_put(skb, &xp->mark);
3544 err = xfrm_if_id_put(skb, xp->if_id);
3548 nlmsg_end(skb, nlh);
3550 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3557 static int xfrm_notify_policy_flush(const struct km_event *c)
3559 struct net *net = c->net;
3560 struct nlmsghdr *nlh;
3561 struct sk_buff *skb;
3564 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3568 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3572 err = copy_to_user_policy_type(c->data.type, skb);
3576 nlmsg_end(skb, nlh);
3578 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3585 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3589 case XFRM_MSG_NEWPOLICY:
3590 case XFRM_MSG_UPDPOLICY:
3591 case XFRM_MSG_DELPOLICY:
3592 return xfrm_notify_policy(xp, dir, c);
3593 case XFRM_MSG_FLUSHPOLICY:
3594 return xfrm_notify_policy_flush(c);
3595 case XFRM_MSG_POLEXPIRE:
3596 return xfrm_exp_policy_notify(xp, dir, c);
3598 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3606 static inline unsigned int xfrm_report_msgsize(void)
3608 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3611 static int build_report(struct sk_buff *skb, u8 proto,
3612 struct xfrm_selector *sel, xfrm_address_t *addr)
3614 struct xfrm_user_report *ur;
3615 struct nlmsghdr *nlh;
3617 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3621 ur = nlmsg_data(nlh);
3623 memcpy(&ur->sel, sel, sizeof(ur->sel));
3626 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3628 nlmsg_cancel(skb, nlh);
3632 nlmsg_end(skb, nlh);
3636 static int xfrm_send_report(struct net *net, u8 proto,
3637 struct xfrm_selector *sel, xfrm_address_t *addr)
3639 struct sk_buff *skb;
3642 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3646 err = build_report(skb, proto, sel, addr);
3649 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3652 static inline unsigned int xfrm_mapping_msgsize(void)
3654 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3657 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3658 xfrm_address_t *new_saddr, __be16 new_sport)
3660 struct xfrm_user_mapping *um;
3661 struct nlmsghdr *nlh;
3663 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3667 um = nlmsg_data(nlh);
3669 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3670 um->id.spi = x->id.spi;
3671 um->id.family = x->props.family;
3672 um->id.proto = x->id.proto;
3673 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3674 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3675 um->new_sport = new_sport;
3676 um->old_sport = x->encap->encap_sport;
3677 um->reqid = x->props.reqid;
3679 nlmsg_end(skb, nlh);
3683 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3686 struct net *net = xs_net(x);
3687 struct sk_buff *skb;
3690 if (x->id.proto != IPPROTO_ESP)
3696 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3700 err = build_mapping(skb, x, ipaddr, sport);
3703 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3706 static bool xfrm_is_alive(const struct km_event *c)
3708 return (bool)xfrm_acquire_is_on(c->net);
3711 static struct xfrm_mgr netlink_mgr = {
3712 .notify = xfrm_send_state_notify,
3713 .acquire = xfrm_send_acquire,
3714 .compile_policy = xfrm_compile_policy,
3715 .notify_policy = xfrm_send_policy_notify,
3716 .report = xfrm_send_report,
3717 .migrate = xfrm_send_migrate,
3718 .new_mapping = xfrm_send_mapping,
3719 .is_alive = xfrm_is_alive,
3722 static int __net_init xfrm_user_net_init(struct net *net)
3725 struct netlink_kernel_cfg cfg = {
3726 .groups = XFRMNLGRP_MAX,
3727 .input = xfrm_netlink_rcv,
3730 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3733 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3734 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3738 static void __net_exit xfrm_user_net_pre_exit(struct net *net)
3740 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3743 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3747 list_for_each_entry(net, net_exit_list, exit_list)
3748 netlink_kernel_release(net->xfrm.nlsk_stash);
3751 static struct pernet_operations xfrm_user_net_ops = {
3752 .init = xfrm_user_net_init,
3753 .pre_exit = xfrm_user_net_pre_exit,
3754 .exit_batch = xfrm_user_net_exit,
3757 static int __init xfrm_user_init(void)
3761 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3763 rv = register_pernet_subsys(&xfrm_user_net_ops);
3766 xfrm_register_km(&netlink_mgr);
3770 static void __exit xfrm_user_exit(void)
3772 xfrm_unregister_km(&netlink_mgr);
3773 unregister_pernet_subsys(&xfrm_user_net_ops);
3776 module_init(xfrm_user_init);
3777 module_exit(xfrm_user_exit);
3778 MODULE_LICENSE("GPL");
3779 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);