Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /* xfrm_user.c: User interface to configure xfrm engine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *	Mitsuru KANDA @USAGI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * 	Kazunori MIYAZAWA @USAGI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * 		IPv6 support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/pfkeyv2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/ipsec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <net/ah.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	struct nlattr *rt = attrs[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	struct xfrm_algo *algp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	algp = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	if (nla_len(rt) < (int)xfrm_alg_len(algp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	case XFRMA_ALG_AUTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	case XFRMA_ALG_CRYPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	case XFRMA_ALG_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) static int verify_auth_trunc(struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	struct xfrm_algo_auth *algp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	algp = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) static int verify_aead(struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	struct xfrm_algo_aead *algp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	algp = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	if (nla_len(rt) < (int)aead_len(algp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 			   xfrm_address_t **addrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	struct nlattr *rt = attrs[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	if (rt && addrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		*addrp = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) static inline int verify_sec_ctx_len(struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	struct xfrm_user_sec_ctx *uctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	uctx = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	if (uctx->len > nla_len(rt) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	    uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) static inline int verify_replay(struct xfrm_usersa_info *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 				struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	struct xfrm_replay_state_esn *rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	rs = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	    nla_len(rt) != sizeof(*rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	/* As only ESP and AH support ESN feature. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (p->replay_window != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) static int verify_newsa_info(struct xfrm_usersa_info *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 			     struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	switch (p->family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		err = -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	switch (p->sel.family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	case AF_UNSPEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		err = -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	switch (p->id.proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	case IPPROTO_AH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		if ((!attrs[XFRMA_ALG_AUTH]	&&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		    attrs[XFRMA_ALG_AEAD]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		    attrs[XFRMA_ALG_CRYPT]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		    attrs[XFRMA_ALG_COMP]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		    attrs[XFRMA_TFCPAD])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	case IPPROTO_ESP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		if (attrs[XFRMA_ALG_COMP])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		if (!attrs[XFRMA_ALG_AUTH] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		    !attrs[XFRMA_ALG_CRYPT] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		    !attrs[XFRMA_ALG_AEAD])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		if ((attrs[XFRMA_ALG_AUTH] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		     attrs[XFRMA_ALG_CRYPT]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		    attrs[XFRMA_ALG_AEAD])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		if (attrs[XFRMA_TFCPAD] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		    p->mode != XFRM_MODE_TUNNEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	case IPPROTO_COMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		if (!attrs[XFRMA_ALG_COMP]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		    attrs[XFRMA_ALG_AEAD]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		    attrs[XFRMA_ALG_AUTH]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		    attrs[XFRMA_ALG_CRYPT]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		    attrs[XFRMA_TFCPAD]		||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		    (ntohl(p->id.spi) >= 0x10000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	case IPPROTO_DSTOPTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	case IPPROTO_ROUTING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		if (attrs[XFRMA_ALG_COMP]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		    attrs[XFRMA_ALG_AUTH]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		    attrs[XFRMA_ALG_AEAD]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		    attrs[XFRMA_ALG_CRYPT]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		    attrs[XFRMA_ENCAP]		||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		    attrs[XFRMA_SEC_CTX]	||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		    attrs[XFRMA_TFCPAD]		||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		    !attrs[XFRMA_COADDR])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	if ((err = verify_aead(attrs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	if ((err = verify_auth_trunc(attrs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	if ((err = verify_sec_ctx_len(attrs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	if ((err = verify_replay(p, attrs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	switch (p->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	case XFRM_MODE_TRANSPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	case XFRM_MODE_TUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	case XFRM_MODE_ROUTEOPTIMIZATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	case XFRM_MODE_BEET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 			   struct nlattr *rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	struct xfrm_algo *p, *ualg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	struct xfrm_algo_desc *algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	if (!rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	ualg = nla_data(rta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	algo = get_byname(ualg->alg_name, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	if (!algo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	*props = algo->desc.sadb_alg_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	strcpy(p->alg_name, algo->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	*algpp = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	struct xfrm_algo *p, *ualg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	struct xfrm_algo_desc *algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	if (!rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	ualg = nla_data(rta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	if (!algo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	x->props.ealgo = algo->desc.sadb_alg_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	strcpy(p->alg_name, algo->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	x->ealg = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	x->geniv = algo->uinfo.encr.geniv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		       struct nlattr *rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	struct xfrm_algo *ualg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	struct xfrm_algo_auth *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	struct xfrm_algo_desc *algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if (!rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	ualg = nla_data(rta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	if (!algo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	*props = algo->desc.sadb_alg_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	strcpy(p->alg_name, algo->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	p->alg_key_len = ualg->alg_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	*algpp = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 			     struct nlattr *rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	struct xfrm_algo_auth *p, *ualg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	struct xfrm_algo_desc *algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	if (!rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	ualg = nla_data(rta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	if (!algo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	*props = algo->desc.sadb_alg_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	strcpy(p->alg_name, algo->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	if (!p->alg_trunc_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	*algpp = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	struct xfrm_algo_aead *p, *ualg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	struct xfrm_algo_desc *algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	if (!rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	ualg = nla_data(rta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	if (!algo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	x->props.ealgo = algo->desc.sadb_alg_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	strcpy(p->alg_name, algo->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	x->aead = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	x->geniv = algo->uinfo.aead.geniv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 					 struct nlattr *rp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	struct xfrm_replay_state_esn *up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	unsigned int ulen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (!replay_esn || !rp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	up = nla_data(rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	ulen = xfrm_replay_state_esn_len(up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	/* Check the overall length and the internal bitmap length to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	 * potential overflow. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	if (nla_len(rp) < (int)ulen ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	    replay_esn->bmp_len != up->bmp_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 				       struct xfrm_replay_state_esn **preplay_esn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 				       struct nlattr *rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	struct xfrm_replay_state_esn *p, *pp, *up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	unsigned int klen, ulen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	if (!rta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	up = nla_data(rta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	klen = xfrm_replay_state_esn_len(up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	p = kzalloc(klen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	pp = kzalloc(klen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	if (!pp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	memcpy(p, up, ulen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	memcpy(pp, up, ulen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	*replay_esn = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	*preplay_esn = pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	unsigned int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (xfrm_ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		len += sizeof(struct xfrm_user_sec_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		len += xfrm_ctx->ctx_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	memcpy(&x->id, &p->id, sizeof(x->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	x->props.mode = p->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	x->props.replay_window = min_t(unsigned int, p->replay_window,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 					sizeof(x->replay.bitmap) * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	x->props.reqid = p->reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	x->props.family = p->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	x->props.flags = p->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		x->sel.family = p->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512)  * someday when pfkey also has support, we could have the code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513)  * somehow made shareable and move it to xfrm_state.c - JHS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 				  int update_esn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (re) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		struct xfrm_replay_state_esn *replay_esn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		replay_esn = nla_data(re);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		memcpy(x->replay_esn, replay_esn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		       xfrm_replay_state_esn_len(replay_esn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		memcpy(x->preplay_esn, replay_esn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		       xfrm_replay_state_esn_len(replay_esn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	if (rp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		struct xfrm_replay_state *replay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		replay = nla_data(rp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		memcpy(&x->replay, replay, sizeof(*replay));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		memcpy(&x->preplay, replay, sizeof(*replay));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	if (lt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		struct xfrm_lifetime_cur *ltime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		ltime = nla_data(lt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		x->curlft.bytes = ltime->bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		x->curlft.packets = ltime->packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		x->curlft.add_time = ltime->add_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		x->curlft.use_time = ltime->use_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (et)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		x->replay_maxage = nla_get_u32(et);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		x->replay_maxdiff = nla_get_u32(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	if (attrs[XFRMA_SET_MARK]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		if (attrs[XFRMA_SET_MARK_MASK])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 			m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 			m->m = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		m->v = m->m = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) static struct xfrm_state *xfrm_state_construct(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 					       struct xfrm_usersa_info *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 					       struct nlattr **attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 					       int *errp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	struct xfrm_state *x = xfrm_state_alloc(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (!x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		goto error_no_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	copy_from_user_state(x, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	if (attrs[XFRMA_ENCAP]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 				   sizeof(*x->encap), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		if (x->encap == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	if (attrs[XFRMA_COADDR]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 				    sizeof(*x->coaddr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		if (x->coaddr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (!x->props.aalgo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 				       attrs[XFRMA_ALG_AUTH])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 				   xfrm_calg_get_byname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 				   attrs[XFRMA_ALG_COMP])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	if (attrs[XFRMA_TFCPAD])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	xfrm_mark_get(attrs, &x->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	xfrm_smark_init(attrs, &x->props.smark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	if (attrs[XFRMA_IF_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	if (attrs[XFRMA_SEC_CTX]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		err = security_xfrm_state_alloc(x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 						nla_data(attrs[XFRMA_SEC_CTX]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 					       attrs[XFRMA_REPLAY_ESN_VAL])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	x->km.seq = p->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	/* sysctl_xfrm_aevent_etime is in 100ms units */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if ((err = xfrm_init_replay(x)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	/* override default values from above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	xfrm_update_ae_params(x, attrs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	/* configure the hardware if offload is requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	if (attrs[XFRMA_OFFLOAD_DEV]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		err = xfrm_dev_state_add(net, x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	return x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	x->km.state = XFRM_STATE_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) error_no_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	*errp = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	struct km_event c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	err = verify_newsa_info(p, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	x = xfrm_state_construct(net, p, attrs, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (!x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	xfrm_state_hold(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		err = xfrm_state_add(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		err = xfrm_state_update(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	xfrm_audit_state_add(x, err ? 0 : 1, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		x->km.state = XFRM_STATE_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		xfrm_dev_state_delete(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		__xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	if (x->km.state == XFRM_STATE_VOID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		x->km.state = XFRM_STATE_VALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	c.seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	c.portid = nlh->nlmsg_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	c.event = nlh->nlmsg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	km_state_notify(x, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 						 struct xfrm_usersa_id *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 						 struct nlattr **attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 						 int *errp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	struct xfrm_state *x = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	struct xfrm_mark m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	u32 mark = xfrm_mark_get(attrs, &m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		err = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		xfrm_address_t *saddr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		if (!saddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		err = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		x = xfrm_state_lookup_byaddr(net, mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 					     &p->daddr, saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 					     p->proto, p->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	if (!x && errp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		*errp = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	return x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	int err = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	struct km_event c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	x = xfrm_user_state_lookup(net, p, attrs, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (x == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	if ((err = security_xfrm_state_delete(x)) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	if (xfrm_state_kern(x)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	err = xfrm_state_delete(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	c.seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	c.portid = nlh->nlmsg_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	c.event = nlh->nlmsg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	km_state_notify(x, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	memset(p, 0, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	memcpy(&p->id, &x->id, sizeof(p->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	put_unaligned(x->stats.replay, &p->stats.replay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	p->mode = x->props.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	p->replay_window = x->props.replay_window;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	p->reqid = x->props.reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	p->family = x->props.family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	p->flags = x->props.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	p->seq = x->km.seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) struct xfrm_dump_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	struct sk_buff *in_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	struct sk_buff *out_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	u32 nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	u16 nlmsg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	struct xfrm_user_sec_ctx *uctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	int ctx_size = sizeof(*uctx) + s->ctx_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	if (attr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	uctx = nla_data(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	uctx->exttype = XFRMA_SEC_CTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	uctx->len = ctx_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	uctx->ctx_doi = s->ctx_doi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	uctx->ctx_alg = s->ctx_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	uctx->ctx_len = s->ctx_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	struct xfrm_user_offload *xuo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	if (attr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	xuo = nla_data(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	memset(xuo, 0, sizeof(*xuo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	xuo->ifindex = xso->dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	xuo->flags = xso->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	struct xfrm_algo *algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (!nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	algo = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	algo->alg_key_len = auth->alg_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (m->v | m->m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 			ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) /* Don't change this without updating xfrm_sa_len! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) static int copy_to_user_state_extra(struct xfrm_state *x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 				    struct xfrm_usersa_info *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 				    struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	copy_to_user_state(x, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	if (x->props.extra_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 				  x->props.extra_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	if (x->coaddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (x->lastused) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 					XFRMA_PAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	if (x->aead) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	if (x->aalg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		ret = copy_to_user_auth(x->aalg, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 				      xfrm_alg_auth_len(x->aalg), x->aalg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (x->ealg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	if (x->calg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	if (x->encap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (x->tfcpad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	ret = xfrm_mark_put(skb, &x->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	ret = xfrm_smark_put(skb, &x->props.smark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (x->replay_esn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			      xfrm_replay_state_esn_len(x->replay_esn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			      x->replay_esn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			      &x->replay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	if(x->xso.dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		ret = copy_user_offload(&x->xso, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	if (x->if_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	if (x->security)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		ret = copy_sec_ctx(x->security, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	struct xfrm_dump_info *sp = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	struct sk_buff *in_skb = sp->in_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	struct sk_buff *skb = sp->out_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	struct xfrm_translator *xtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	struct xfrm_usersa_info *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	err = copy_to_user_state_extra(x, p, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	xtr = xfrm_get_translator();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	if (xtr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		err = xtr->alloc_compat(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		xfrm_put_translator(xtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) static int xfrm_dump_sa_done(struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	struct sock *sk = cb->skb->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	if (cb->args[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		xfrm_state_walk_done(walk, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	struct xfrm_dump_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		     sizeof(cb->args) - sizeof(cb->args[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	info.in_skb = cb->skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	info.out_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	info.nlmsg_flags = NLM_F_MULTI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	if (!cb->args[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		struct nlattr *attrs[XFRMA_MAX+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		struct xfrm_address_filter *filter = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		u8 proto = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 					     xfrma_policy, cb->extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		if (attrs[XFRMA_ADDRESS_FILTER]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 					 sizeof(*filter), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			if (filter == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		if (attrs[XFRMA_PROTO])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		xfrm_state_walk_init(walk, proto, filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		cb->args[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 					  struct xfrm_state *x, u32 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	struct xfrm_dump_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	info.in_skb = in_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	info.out_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	info.nlmsg_seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	info.nlmsg_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	err = dump_one_state(x, 0, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)  * Must be called with RCU read lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 				       u32 pid, unsigned int group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	struct xfrm_translator *xtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	if (!nlsk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	xtr = xfrm_get_translator();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	if (xtr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		xfrm_put_translator(xtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 			kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static inline unsigned int xfrm_spdinfo_msgsize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	return NLMSG_ALIGN(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) static int build_spdinfo(struct sk_buff *skb, struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			 u32 portid, u32 seq, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	struct xfrmk_spdinfo si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	struct xfrmu_spdinfo spc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	struct xfrmu_spdhinfo sph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	struct xfrmu_spdhthresh spt4, spt6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	u32 *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	unsigned lseq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	if (nlh == NULL) /* shouldn't really happen ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	f = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	*f = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	xfrm_spd_getinfo(net, &si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	spc.incnt = si.incnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	spc.outcnt = si.outcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	spc.fwdcnt = si.fwdcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	spc.inscnt = si.inscnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	spc.outscnt = si.outscnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	spc.fwdscnt = si.fwdscnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	sph.spdhcnt = si.spdhcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	sph.spdhmcnt = si.spdhmcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			    struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	struct xfrmu_spdhthresh *thresh4 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	struct xfrmu_spdhthresh *thresh6 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	/* selector prefixlen thresholds to hash policies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		if (nla_len(rta) < sizeof(*thresh4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		thresh4 = nla_data(rta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		if (nla_len(rta) < sizeof(*thresh6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		thresh6 = nla_data(rta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	if (thresh4 || thresh6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		write_seqlock(&net->xfrm.policy_hthresh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		if (thresh4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		if (thresh6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		xfrm_policy_hash_rebuild(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	struct sk_buff *r_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	u32 *flags = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	u32 sportid = NETLINK_CB(skb).portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	u32 seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (r_skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) static inline unsigned int xfrm_sadinfo_msgsize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	return NLMSG_ALIGN(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) static int build_sadinfo(struct sk_buff *skb, struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 			 u32 portid, u32 seq, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	struct xfrmk_sadinfo si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	struct xfrmu_sadhinfo sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	u32 *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	if (nlh == NULL) /* shouldn't really happen ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	f = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	*f = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	xfrm_sad_getinfo(net, &si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	sh.sadhmcnt = si.sadhmcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	sh.sadhcnt = si.sadhcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	struct sk_buff *r_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	u32 *flags = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	u32 sportid = NETLINK_CB(skb).portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	u32 seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	if (r_skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	struct sk_buff *resp_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	int err = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	x = xfrm_user_state_lookup(net, p, attrs, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	if (x == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		goto out_noput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	if (IS_ERR(resp_skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		err = PTR_ERR(resp_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) out_noput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	struct xfrm_userspi_info *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	struct xfrm_translator *xtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	struct sk_buff *resp_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	xfrm_address_t *daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	int family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	u32 mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	struct xfrm_mark m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	u32 if_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		goto out_noput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	family = p->info.family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	daddr = &p->info.id.daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	x = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	mark = xfrm_mark_get(attrs, &m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	if (attrs[XFRMA_IF_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	if (p->info.seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 			xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 			x = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	if (!x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 				  if_id, p->info.id.proto, daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 				  &p->info.saddr, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 				  family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	if (x == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		goto out_noput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	err = xfrm_alloc_spi(x, p->min, p->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	if (IS_ERR(resp_skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		err = PTR_ERR(resp_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	xtr = xfrm_get_translator();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (xtr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		xfrm_put_translator(xtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			kfree_skb(resp_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) out_noput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) static int verify_policy_dir(u8 dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	switch (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	case XFRM_POLICY_IN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	case XFRM_POLICY_OUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	case XFRM_POLICY_FWD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) static int verify_policy_type(u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	case XFRM_POLICY_TYPE_MAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) #ifdef CONFIG_XFRM_SUB_POLICY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	case XFRM_POLICY_TYPE_SUB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	switch (p->share) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	case XFRM_SHARE_ANY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	case XFRM_SHARE_SESSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	case XFRM_SHARE_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	case XFRM_SHARE_UNIQUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	switch (p->action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	case XFRM_POLICY_ALLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	case XFRM_POLICY_BLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	switch (p->sel.family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		return  -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	ret = verify_policy_dir(p->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	struct xfrm_user_sec_ctx *uctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	uctx = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 			   int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	xp->xfrm_nr = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	for (i = 0; i < nr; i++, ut++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		memcpy(&t->saddr, &ut->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 		       sizeof(xfrm_address_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		t->reqid = ut->reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		t->mode = ut->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		t->share = ut->share;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		t->optional = ut->optional;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		t->aalgos = ut->aalgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		t->ealgos = ut->ealgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		t->calgos = ut->calgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		/* If all masks are ~0, then we allow all algorithms. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		t->encap_family = ut->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	u16 prev_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	if (nr > XFRM_MAX_DEPTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	prev_family = family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		/* We never validated the ut->family value, so many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		 * applications simply leave it at zero.  The check was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		 * never made and ut->family was ignored because all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		 * templates could be assumed to have the same family as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		 * the policy itself.  Now that we will have ipv4-in-ipv6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		if (!ut[i].family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			ut[i].family = family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		switch (ut[i].mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		case XFRM_MODE_TUNNEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		case XFRM_MODE_BEET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			if (ut[i].family != prev_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		if (ut[i].mode >= XFRM_MODE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		prev_family = ut[i].family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		switch (ut[i].family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		if (!xfrm_id_proto_valid(ut[i].id.proto))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	struct nlattr *rt = attrs[XFRMA_TMPL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		pol->xfrm_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		int nr = nla_len(rt) / sizeof(*utmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		err = validate_tmpl(nr, utmpl, pol->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		copy_templates(pol, utmpl, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	struct xfrm_userpolicy_type *upt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	u8 type = XFRM_POLICY_TYPE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		upt = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		type = upt->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	err = verify_policy_type(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	*tp = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	xp->priority = p->priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	xp->index = p->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	xp->action = p->action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	xp->flags = p->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	xp->family = p->sel.family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	/* XXX xp->share = p->share; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	memset(p, 0, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	p->priority = xp->priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	p->index = xp->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	p->sel.family = xp->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	p->dir = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	p->action = xp->action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	p->flags = xp->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	if (!xp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		*errp = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	copy_from_user_policy(xp, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	err = copy_from_user_policy_type(&xp->type, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	if (!(err = copy_from_user_tmpl(xp, attrs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		err = copy_from_user_sec_ctx(xp, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	xfrm_mark_get(attrs, &xp->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	if (attrs[XFRMA_IF_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	return xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)  error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	*errp = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	xp->walk.dead = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	xfrm_policy_destroy(xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	struct xfrm_policy *xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	struct km_event c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	int excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	err = verify_newpolicy_info(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	err = verify_sec_ctx_len(attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	xp = xfrm_policy_construct(net, p, attrs, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	if (!xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	/* shouldn't excl be based on nlh flags??
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	 * Aha! this is anti-netlink really i.e  more pfkey derived
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	 * in netlink excl is a flag and you wouldnt need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	 * a type XFRM_MSG_UPDPOLICY - JHS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	err = xfrm_policy_insert(p->dir, xp, excl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		security_xfrm_policy_free(xp->security);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		kfree(xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	c.event = nlh->nlmsg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	c.seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	c.portid = nlh->nlmsg_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	km_policy_notify(xp, p->dir, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	xfrm_pol_put(xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	if (xp->xfrm_nr == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	for (i = 0; i < xp->xfrm_nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		struct xfrm_user_tmpl *up = &vec[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		memset(up, 0, sizeof(*up));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		memcpy(&up->id, &kp->id, sizeof(up->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		up->family = kp->encap_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		up->reqid = kp->reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		up->mode = kp->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		up->share = kp->share;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		up->optional = kp->optional;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		up->aalgos = kp->aalgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		up->ealgos = kp->ealgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		up->calgos = kp->calgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	return nla_put(skb, XFRMA_TMPL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	if (x->security) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		return copy_sec_ctx(x->security, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	if (xp->security)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		return copy_sec_ctx(xp->security, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) static inline unsigned int userpolicy_type_attrsize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) #ifdef CONFIG_XFRM_SUB_POLICY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) #ifdef CONFIG_XFRM_SUB_POLICY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	struct xfrm_userpolicy_type upt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	memset(&upt, 0, sizeof(upt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	upt.type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	struct xfrm_dump_info *sp = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	struct xfrm_userpolicy_info *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	struct sk_buff *in_skb = sp->in_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	struct sk_buff *skb = sp->out_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	struct xfrm_translator *xtr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	copy_to_user_policy(xp, p, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	err = copy_to_user_tmpl(xp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		err = copy_to_user_sec_ctx(xp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		err = copy_to_user_policy_type(xp->type, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		err = xfrm_mark_put(skb, &xp->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		err = xfrm_if_id_put(skb, xp->if_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	xtr = xfrm_get_translator();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	if (xtr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		err = xtr->alloc_compat(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		xfrm_put_translator(xtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 			nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) static int xfrm_dump_policy_done(struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	struct net *net = sock_net(cb->skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	xfrm_policy_walk_done(walk, net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) static int xfrm_dump_policy_start(struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	struct xfrm_dump_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	info.in_skb = cb->skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	info.out_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	info.nlmsg_flags = NLM_F_MULTI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 					  struct xfrm_policy *xp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 					  int dir, u32 seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	struct xfrm_dump_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	info.in_skb = in_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	info.out_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	info.nlmsg_seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	info.nlmsg_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	err = dump_one_policy(xp, dir, 0, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	struct xfrm_policy *xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	struct xfrm_userpolicy_id *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	u8 type = XFRM_POLICY_TYPE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	struct km_event c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	int delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	struct xfrm_mark m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	u32 if_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	err = copy_from_user_policy_type(&type, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	err = verify_policy_dir(p->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	if (attrs[XFRMA_IF_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	xfrm_mark_get(attrs, &m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	if (p->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 				      p->index, delete, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		struct xfrm_sec_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		err = verify_sec_ctx_len(attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 					   &p->sel, ctx, delete, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 		security_xfrm_policy_free(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	if (xp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	if (!delete) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		struct sk_buff *resp_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		if (IS_ERR(resp_skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 			err = PTR_ERR(resp_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 					    NETLINK_CB(skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		c.data.byid = p->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		c.event = nlh->nlmsg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		c.seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		c.portid = nlh->nlmsg_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		km_policy_notify(xp, p->dir, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	xfrm_pol_put(xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	struct km_event c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	err = xfrm_state_flush(net, p->proto, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		if (err == -ESRCH) /* empty table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	c.data.proto = p->proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	c.event = nlh->nlmsg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	c.seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	c.portid = nlh->nlmsg_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	c.net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	km_state_notify(NULL, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	unsigned int replay_size = x->replay_esn ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 			      xfrm_replay_state_esn_len(x->replay_esn) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 			      sizeof(struct xfrm_replay_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	       + nla_total_size(replay_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	       + nla_total_size(sizeof(struct xfrm_mark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	       + nla_total_size(4) /* XFRM_AE_RTHR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	       + nla_total_size(4); /* XFRM_AE_ETHR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	struct xfrm_aevent_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 	id = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	memset(&id->sa_id, 0, sizeof(id->sa_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	id->sa_id.spi = x->id.spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	id->sa_id.family = x->props.family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	id->sa_id.proto = x->id.proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	id->reqid = x->props.reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	id->flags = c->data.aevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	if (x->replay_esn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 			      xfrm_replay_state_esn_len(x->replay_esn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 			      x->replay_esn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 			      &x->replay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 			    XFRMA_PAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	if (id->flags & XFRM_AE_RTHR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 			goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	if (id->flags & XFRM_AE_ETHR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 				  x->replay_maxage * 10 / HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 			goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	err = xfrm_mark_put(skb, &x->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	err = xfrm_if_id_put(skb, x->if_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	struct sk_buff *r_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	struct km_event c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	u32 mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	struct xfrm_mark m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	struct xfrm_usersa_id *id = &p->sa_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	mark = xfrm_mark_get(attrs, &m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	if (x == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 		return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	if (r_skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	 * XXX: is this lock really needed - none of the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	 * gets lock (the concern is things getting updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	 * while we are still reading) - jhs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	spin_lock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	c.data.aevent = p->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	c.seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	c.portid = nlh->nlmsg_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	err = build_aevent(r_skb, x, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	spin_unlock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	struct km_event c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	u32 mark = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	struct xfrm_mark m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	if (!lt && !rp && !re && !et && !rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	/* pedantic mode - thou shalt sayeth replaceth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	mark = xfrm_mark_get(attrs, &m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	if (x == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	if (x->km.state != XFRM_STATE_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	err = xfrm_replay_verify_len(x->replay_esn, re);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	spin_lock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	xfrm_update_ae_params(x, attrs, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	spin_unlock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	c.event = nlh->nlmsg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	c.seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	c.portid = nlh->nlmsg_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	c.data.aevent = XFRM_AE_CU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	km_state_notify(x, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	struct km_event c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	u8 type = XFRM_POLICY_TYPE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	err = copy_from_user_policy_type(&type, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	err = xfrm_policy_flush(net, type, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		if (err == -ESRCH) /* empty table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	c.data.type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	c.event = nlh->nlmsg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	c.seq = nlh->nlmsg_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	c.portid = nlh->nlmsg_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	c.net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	km_policy_notify(NULL, 0, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	struct xfrm_policy *xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	struct xfrm_userpolicy_info *p = &up->pol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	u8 type = XFRM_POLICY_TYPE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	struct xfrm_mark m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	u32 if_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	err = copy_from_user_policy_type(&type, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	err = verify_policy_dir(p->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	if (attrs[XFRMA_IF_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	xfrm_mark_get(attrs, &m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	if (p->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 				      0, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 		struct xfrm_sec_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		err = verify_sec_ctx_len(attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 		if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 					   &p->sel, ctx, 0, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 		security_xfrm_policy_free(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	if (xp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 	if (unlikely(xp->walk.dead))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 	if (up->hard) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		xfrm_policy_delete(xp, p->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		xfrm_audit_policy_delete(xp, 1, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	xfrm_pol_put(xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	struct xfrm_state *x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	struct xfrm_usersa_info *p = &ue->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	struct xfrm_mark m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	u32 mark = xfrm_mark_get(attrs, &m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	if (x == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	spin_lock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	if (x->km.state != XFRM_STATE_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	if (ue->hard) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 		__xfrm_state_delete(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 		xfrm_audit_state_delete(x, 1, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	spin_unlock_bh(&x->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	xfrm_state_put(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 		struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	struct xfrm_policy *xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 	struct xfrm_user_tmpl *ut;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 	struct nlattr *rt = attrs[XFRMA_TMPL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	struct xfrm_mark mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	struct xfrm_state *x = xfrm_state_alloc(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	if (!x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 		goto nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	xfrm_mark_get(attrs, &mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	err = verify_newpolicy_info(&ua->policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 		goto free_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	err = verify_sec_ctx_len(attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 		goto free_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	/*   build an XP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	if (!xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		goto free_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	memcpy(&x->id, &ua->id, sizeof(ua->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	xp->mark.m = x->mark.m = mark.m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	xp->mark.v = x->mark.v = mark.v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	ut = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	/* extract the templates and for each call km_key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 		memcpy(&x->id, &t->id, sizeof(x->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 		x->props.mode = t->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		x->props.reqid = t->reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		x->props.family = ut->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		t->aalgos = ua->aalgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 		t->ealgos = ua->ealgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		t->calgos = ua->calgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 		err = km_query(x, t, xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	xfrm_state_free(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	kfree(xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) free_state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	xfrm_state_free(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) #ifdef CONFIG_XFRM_MIGRATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) static int copy_from_user_migrate(struct xfrm_migrate *ma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 				  struct xfrm_kmaddress *k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 				  struct nlattr **attrs, int *num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	struct xfrm_user_migrate *um;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	int i, num_migrate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	if (k != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		struct xfrm_user_kmaddress *uk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		memcpy(&k->local, &uk->local, sizeof(k->local));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		k->family = uk->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 		k->reserved = uk->reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	um = nla_data(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	num_migrate = nla_len(rt) / sizeof(*um);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	for (i = 0; i < num_migrate; i++, um++, ma++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		ma->proto = um->proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 		ma->mode = um->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 		ma->reqid = um->reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 		ma->old_family = um->old_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 		ma->new_family = um->new_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	*num = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 			   struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	struct xfrm_kmaddress km, *kmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	int n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	struct xfrm_encap_tmpl  *encap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	u32 if_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	if (attrs[XFRMA_MIGRATE] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	err = copy_from_user_policy_type(&type, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	if (attrs[XFRMA_ENCAP]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 				sizeof(*encap), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 		if (!encap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	if (attrs[XFRMA_IF_ID])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	kfree(encap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 			   struct nlattr **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) #ifdef CONFIG_XFRM_MIGRATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	struct xfrm_user_migrate um;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	memset(&um, 0, sizeof(um));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	um.proto = m->proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	um.mode = m->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	um.reqid = m->reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	um.old_family = m->old_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	um.new_family = m->new_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	struct xfrm_user_kmaddress uk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	memset(&uk, 0, sizeof(uk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	uk.family = k->family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	uk.reserved = k->reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	memcpy(&uk.local, &k->local, sizeof(uk.local));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 						int with_encp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	      + userpolicy_type_attrsize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 			 int num_migrate, const struct xfrm_kmaddress *k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 			 const struct xfrm_selector *sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	const struct xfrm_migrate *mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	struct xfrm_userpolicy_id *pol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	pol_id = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	/* copy data from selector, dir, and type to the pol_id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	memset(pol_id, 0, sizeof(*pol_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	pol_id->dir = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	if (k != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 		err = copy_to_user_kmaddress(k, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 			goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	if (encap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 			goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	err = copy_to_user_policy_type(type, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 		err = copy_to_user_migrate(mp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 			goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 			     const struct xfrm_migrate *m, int num_migrate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 			     const struct xfrm_kmaddress *k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 			     const struct xfrm_encap_tmpl *encap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	struct net *net = &init_net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 			GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	/* build migrate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 			     const struct xfrm_migrate *m, int num_migrate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 			     const struct xfrm_kmaddress *k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 			     const struct xfrm_encap_tmpl *encap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) #define XMSGSIZE(type) sizeof(struct type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) EXPORT_SYMBOL_GPL(xfrm_msg_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) #undef XMSGSIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	[XFRMA_PROTO]		= { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) EXPORT_SYMBOL_GPL(xfrma_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) static const struct xfrm_link {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 	int (*start)(struct netlink_callback *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	int (*dump)(struct sk_buff *, struct netlink_callback *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 	int (*done)(struct netlink_callback *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	const struct nla_policy *nla_pol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	int nla_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 						   .dump = xfrm_dump_sa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 						   .done = xfrm_dump_sa_done  },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 						   .start = xfrm_dump_policy_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 						   .dump = xfrm_dump_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 						   .done = xfrm_dump_policy_done },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 						   .nla_pol = xfrma_spd_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 						   .nla_max = XFRMA_SPD_MAX },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 			     struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	struct nlattr *attrs[XFRMA_MAX+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	const struct xfrm_link *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	struct nlmsghdr *nlh64 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	int type, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 	type = nlh->nlmsg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	if (type > XFRM_MSG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	type -= XFRM_MSG_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 	link = &xfrm_dispatch[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	/* All operations require privileges, even GET */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	/* Use the 64-bit / untranslated format on Android, even for compat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	if (!IS_ENABLED(CONFIG_ANDROID) || IS_ENABLED(CONFIG_XFRM_USER_COMPAT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 		if (in_compat_syscall()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 			struct xfrm_translator *xtr = xfrm_get_translator();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 			if (!xtr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 				return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 			nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 						    link->nla_pol, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 			xfrm_put_translator(xtr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 			if (IS_ERR(nlh64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 				return PTR_ERR(nlh64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 			if (nlh64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 				nlh = nlh64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 		struct netlink_dump_control c = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 			.start = link->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 			.dump = link->dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 			.done = link->done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 		if (link->dump == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 		err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 				     link->nla_max ? : XFRMA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 				     link->nla_pol ? : xfrma_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	if (link->doit == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	err = link->doit(skb, nlh, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	/* We need to free skb allocated in xfrm_alloc_compat() before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	 * returning from this function, because consume_skb() won't take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	 * care of frag_list since netlink destructor sets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	 * sbk->head to NULL. (see netlink_skb_destructor())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	if (skb_has_frag_list(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 		kfree_skb(skb_shinfo(skb)->frag_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 		skb_shinfo(skb)->frag_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 	kvfree(nlh64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) static void xfrm_netlink_rcv(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) static inline unsigned int xfrm_expire_msgsize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	       + nla_total_size(sizeof(struct xfrm_mark));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 	struct xfrm_user_expire *ue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 	ue = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	copy_to_user_state(x, &ue->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 	ue->hard = (c->data.hard != 0) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	/* clear the padding bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	err = xfrm_mark_put(skb, &x->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 	err = xfrm_if_id_put(skb, x->if_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	struct net *net = xs_net(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	if (build_expire(skb, x, c) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 	struct net *net = xs_net(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	err = build_aevent(skb, x, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) static int xfrm_notify_sa_flush(const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	struct net *net = c->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	struct xfrm_usersa_flush *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	skb = nlmsg_new(len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	if (nlh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 	p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 	p->proto = c->data.proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	unsigned int l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	if (x->aead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 		l += nla_total_size(aead_len(x->aead));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 	if (x->aalg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 		l += nla_total_size(sizeof(struct xfrm_algo) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 				    (x->aalg->alg_key_len + 7) / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 	if (x->ealg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 		l += nla_total_size(xfrm_alg_len(x->ealg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 	if (x->calg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 		l += nla_total_size(sizeof(*x->calg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 	if (x->encap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 		l += nla_total_size(sizeof(*x->encap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 	if (x->tfcpad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 		l += nla_total_size(sizeof(x->tfcpad));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 	if (x->replay_esn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 		l += nla_total_size(sizeof(struct xfrm_replay_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	if (x->security)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 				    x->security->ctx_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 	if (x->coaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 		l += nla_total_size(sizeof(*x->coaddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 	if (x->props.extra_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 		l += nla_total_size(sizeof(x->props.extra_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 	if (x->xso.dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 		 l += nla_total_size(sizeof(struct xfrm_user_offload));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 	if (x->props.smark.v | x->props.smark.m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 		l += nla_total_size(sizeof(x->props.smark.v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 		l += nla_total_size(sizeof(x->props.smark.m));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 	if (x->if_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 		l += nla_total_size(sizeof(x->if_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 	/* Must count x->lastused as it may become non-zero behind our back. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 	l += nla_total_size_64bit(sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 	return l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 	struct net *net = xs_net(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 	struct xfrm_usersa_info *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	struct xfrm_usersa_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 	unsigned int len = xfrm_sa_len(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 	unsigned int headlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 	headlen = sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 	if (c->event == XFRM_MSG_DELSA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 		len += nla_total_size(headlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 		headlen = sizeof(*id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 		len += nla_total_size(sizeof(struct xfrm_mark));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 	len += NLMSG_ALIGN(headlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 	skb = nlmsg_new(len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 	err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	if (c->event == XFRM_MSG_DELSA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 		struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 		id = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 		memset(id, 0, sizeof(*id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 		id->spi = x->id.spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 		id->family = x->props.family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 		id->proto = x->id.proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 		err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 		if (attr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 			goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 		p = nla_data(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 	err = copy_to_user_state_extra(x, p, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) out_free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	switch (c->event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 	case XFRM_MSG_EXPIRE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 		return xfrm_exp_state_notify(x, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	case XFRM_MSG_NEWAE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 		return xfrm_aevent_state_notify(x, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 	case XFRM_MSG_DELSA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 	case XFRM_MSG_UPDSA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 	case XFRM_MSG_NEWSA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 		return xfrm_notify_sa(x, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 	case XFRM_MSG_FLUSHSA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 		return xfrm_notify_sa_flush(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 		       c->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 						struct xfrm_policy *xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	       + nla_total_size(sizeof(struct xfrm_mark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	       + userpolicy_type_attrsize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 	__u32 seq = xfrm_get_acqseq();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 	struct xfrm_user_acquire *ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 	ua = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 	memcpy(&ua->id, &x->id, sizeof(ua->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	ua->aalgos = xt->aalgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 	ua->ealgos = xt->ealgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 	ua->calgos = xt->calgos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 	ua->seq = x->km.seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 	err = copy_to_user_tmpl(xp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 		err = copy_to_user_state_sec_ctx(x, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 		err = copy_to_user_policy_type(xp->type, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 		err = xfrm_mark_put(skb, &xp->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 		err = xfrm_if_id_put(skb, xp->if_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 		nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 			     struct xfrm_policy *xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	struct net *net = xs_net(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 	err = build_acquire(skb, x, xt, xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) /* User gives us xfrm_user_policy_info followed by an array of 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073)  * or more templates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 					       u8 *data, int len, int *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	struct xfrm_policy *xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	switch (sk->sk_family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 		if (opt != IP_XFRM_POLICY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 			*dir = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 	case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 		if (opt != IPV6_XFRM_POLICY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 			*dir = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 		*dir = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	*dir = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 	if (len < sizeof(*p) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 	    verify_newpolicy_info(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	nr = ((len - sizeof(*p)) / sizeof(*ut));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	if (validate_tmpl(nr, ut, p->sel.family))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	if (p->dir > XFRM_POLICY_OUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 	if (xp == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 		*dir = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 	copy_from_user_policy(xp, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 	xp->type = XFRM_POLICY_TYPE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 	copy_templates(xp, ut, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 	*dir = p->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	return xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 	       + nla_total_size(sizeof(struct xfrm_mark))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 	       + userpolicy_type_attrsize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 			   int dir, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 	struct xfrm_user_polexpire *upe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	int hard = c->data.hard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 	upe = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	copy_to_user_policy(xp, &upe->pol, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	err = copy_to_user_tmpl(xp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 		err = copy_to_user_sec_ctx(xp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 		err = copy_to_user_policy_type(xp->type, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 		err = xfrm_mark_put(skb, &xp->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 		err = xfrm_if_id_put(skb, xp->if_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 		nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 	upe->hard = !!hard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 	struct net *net = xp_net(xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 	err = build_polexpire(skb, xp, dir, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 	struct net *net = xp_net(xp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 	struct xfrm_userpolicy_info *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	struct xfrm_userpolicy_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	unsigned int headlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	headlen = sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	if (c->event == XFRM_MSG_DELPOLICY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 		len += nla_total_size(headlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 		headlen = sizeof(*id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	len += userpolicy_type_attrsize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 	len += nla_total_size(sizeof(struct xfrm_mark));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	len += NLMSG_ALIGN(headlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	skb = nlmsg_new(len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 	err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 	p = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 	if (c->event == XFRM_MSG_DELPOLICY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 		struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 		id = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 		memset(id, 0, sizeof(*id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 		id->dir = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 		if (c->data.byid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 			id->index = xp->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 		err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 		if (attr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 			goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 		p = nla_data(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 	copy_to_user_policy(xp, p, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 	err = copy_to_user_tmpl(xp, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 		err = copy_to_user_policy_type(xp->type, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 		err = xfrm_mark_put(skb, &xp->mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 		err = xfrm_if_id_put(skb, xp->if_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) out_free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) static int xfrm_notify_policy_flush(const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 	struct net *net = c->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 	err = -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 	err = copy_to_user_policy_type(c->data.type, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 		goto out_free_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) out_free_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 	switch (c->event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 	case XFRM_MSG_NEWPOLICY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 	case XFRM_MSG_UPDPOLICY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 	case XFRM_MSG_DELPOLICY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 		return xfrm_notify_policy(xp, dir, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 	case XFRM_MSG_FLUSHPOLICY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 		return xfrm_notify_policy_flush(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 	case XFRM_MSG_POLEXPIRE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 		return xfrm_exp_policy_notify(xp, dir, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 		       c->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) static inline unsigned int xfrm_report_msgsize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) static int build_report(struct sk_buff *skb, u8 proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 			struct xfrm_selector *sel, xfrm_address_t *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 	struct xfrm_user_report *ur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 	ur = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 	ur->proto = proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 	memcpy(&ur->sel, sel, sizeof(ur->sel));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 	if (addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 			nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) static int xfrm_send_report(struct net *net, u8 proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 			    struct xfrm_selector *sel, xfrm_address_t *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 	err = build_report(skb, proto, sel, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) static inline unsigned int xfrm_mapping_msgsize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 			 xfrm_address_t *new_saddr, __be16 new_sport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 	struct xfrm_user_mapping *um;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 	if (nlh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 	um = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 	um->id.spi = x->id.spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 	um->id.family = x->props.family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 	um->id.proto = x->id.proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 	um->new_sport = new_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 	um->old_sport = x->encap->encap_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 	um->reqid = x->props.reqid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 			     __be16 sport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 	struct net *net = xs_net(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 	if (x->id.proto != IPPROTO_ESP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 	if (!x->encap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 	err = build_mapping(skb, x, ipaddr, sport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 	BUG_ON(err < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) static bool xfrm_is_alive(const struct km_event *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 	return (bool)xfrm_acquire_is_on(c->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) static struct xfrm_mgr netlink_mgr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 	.notify		= xfrm_send_state_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 	.acquire	= xfrm_send_acquire,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 	.compile_policy	= xfrm_compile_policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 	.notify_policy	= xfrm_send_policy_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 	.report		= xfrm_send_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 	.migrate	= xfrm_send_migrate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 	.new_mapping	= xfrm_send_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 	.is_alive	= xfrm_is_alive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) static int __net_init xfrm_user_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 	struct sock *nlsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 	struct netlink_kernel_cfg cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 		.groups	= XFRMNLGRP_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 		.input	= xfrm_netlink_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 	if (nlsk == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 	struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 	list_for_each_entry(net, net_exit_list, exit_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 	synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	list_for_each_entry(net, net_exit_list, exit_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 		netlink_kernel_release(net->xfrm.nlsk_stash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) static struct pernet_operations xfrm_user_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 	.init	    = xfrm_user_net_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	.exit_batch = xfrm_user_net_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) static int __init xfrm_user_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 	rv = register_pernet_subsys(&xfrm_user_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 		return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 	rv = xfrm_register_km(&netlink_mgr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 	if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 		unregister_pernet_subsys(&xfrm_user_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) static void __exit xfrm_user_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 	xfrm_unregister_km(&netlink_mgr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 	unregister_pernet_subsys(&xfrm_user_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) module_init(xfrm_user_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) module_exit(xfrm_user_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);