From nobody Wed Dec 17 13:54:38 2025 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B042A1D6DD8 for ; Tue, 18 Mar 2025 01:41:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742262086; cv=none; b=RFGFP6R07lfNgPe4wEKsk/6ZXRKOIl/XlD90+rrV0tWybzDCQq4e+NmFRK2Pw9kxhPLYpjDcawFuAwgnuGNGQMSNLkKoLxT3T5h4qGVamglS1M6ktJchpQQujwCqNLVLSXE5l04lZl8eP2zQDtJqKcDiDsroKCCQk6BxWUtxxfs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742262086; c=relaxed/simple; bh=iB5Qbo870WqlbaCwTJXZDkTFSeANy8yaa5A4feFl5uI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FYsIiBL2n9v9oe9aq0wpWBuV5wphAxzr57sG/DPXmMfTUrBTCZLGdIFdf72DOMYiZLRnfJKBByeer0dNe6hF0rHGQPRlhFtTXLijZtlIR0QIPnTRdOx2EvBlQdUzuwncLJdEg388I+2zcYXESBR4X4nfy9JhlUev3Whj7gdiReQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=openvpn.net; spf=pass smtp.mailfrom=openvpn.com; dkim=pass (2048-bit key) header.d=openvpn.net header.i=@openvpn.net header.b=PBdSH8X4; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=openvpn.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=openvpn.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=openvpn.net header.i=@openvpn.net header.b="PBdSH8X4" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-43cf0d787eeso29359785e9.3 for ; Mon, 17 Mar 2025 18:41:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1742262081; x=1742866881; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=EX27ynroTle2hMLi/lQB2ni1edTfOYhzu1lG1eRku3I=; b=PBdSH8X4r3Bx1IJJkJ4iwiSpvBvFLLyQtZfc3sjqhhVM6IJ8U+dja/tR8T1pB6xzkS xoUCCYJea72HLTfUm4dHisidGG5D9teC3POr8T4hp28V4RYMhJEGtxUU8vMJ1NIMxTmS PFRwfweUWFUJtkyxnKE8FZinWtD//fwHXtZJDpEE4AbL96zZa+V6GznGAxS3alfGC84h HwHoEgr67oaw2zYAFA6GcbvomiTNpWInEP/MmOyrZetWgljOf9wTPyHC66la/Ui5esH7 66AAp+LxFKQlvM0sfhEZcikm49b21v5vYwD/ivwNwpnCEn4hGLgCSZi8rvhlOrAAJfXO GAqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742262081; x=1742866881; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EX27ynroTle2hMLi/lQB2ni1edTfOYhzu1lG1eRku3I=; b=mrT67mAg861xT0q2Oc5zIBpY4diF4LchdU8X4FI+HkHMEQ5E4JEkWX8yuOGtSZDiZq 2Nogm1qByl5Cm1WuGzXwtAmiODtgy6kEX9a7GXp1ExK9/hqjmf7yzmyhi6GPVy1QWnUn r91NkiR040I6HBMez6y5xCTQcCsMwV0qNgA3M1CNWeOMQdWrmoqzOOSVYsHNLwhvUJYd QIE+BRLuFkI4aHTzQHVw7ARHACMdE2/Q2i0/cfyc8bmsCOdS7mkWE5zVfW+/oxqn+xeW EEXtdrfAS8Dp+sxPC03mmuWvKvPgY9TnY1zOwpWT5FO9fT7dx/hswbO9OwrSNU2DcZ8v T3ww== X-Forwarded-Encrypted: i=1; AJvYcCXSZ07NUAiZsXQgDGBEBI4YyTBQN5JQFBXxRvPtc/FOa5zMRNUbFU25WzbTZhXUCD9j8yFUS1Nl02axX50=@vger.kernel.org X-Gm-Message-State: AOJu0YyfnqSyQ8sCYPRFHP8qXjq3xHwiOnU4Wgm6GEgMVNCN94Ui/nRV uYyYfO6TTXEF+lKnVOnyDkg6pplxEvVe5BWwwe5FF/bp8X3x41vZtz8u5cWGuRoA2neojmHLYhf qdaa9J/OREO9grjTniWXMCD0cQ6pXMVwoDFodKR03GbMyJllB76dURXs= X-Gm-Gg: ASbGncvmrcTGKO7hw8Yqi98JQcow0WeVrb2yDFoBGMXIyCQmBOS4e8T2m9HVkmwvUrN snorahDr9SbAS05l8LftaLhbuqKOhcb+lSUlSMtrlchfnkpaWxb1Ht904c0qrEc/HhU7p4ojccO YxgK4SvQH7vtye/MaHz+MS7WG+GjFStHLwQ0Bb2G2FgLp3RaFDGWa77F4LrRWMc932KYL9862qX MWqBhdpb4fG/YEuh0dLEpNiVTnQdBVWZ5O16nTF3ZwgWYTaqB9InV7jjnkNQkHzMDUllal0NxiR 8PEPMoYl/3cG/znSsE5H/9CmM5OxPKcg9r2Os4VxIw== X-Google-Smtp-Source: AGHT+IG6UN66Yg4RLgS3mKX2KYWiyqq0UfcLDvTiKFzJ67BRfqupQp7HG2zgzK3ILEAgZeARFaL5rQ== X-Received: by 2002:a05:600c:1991:b0:43d:db5:7b21 with SMTP id 5b1f17b1804b1-43d3ba10cc5mr2757415e9.28.1742262080681; Mon, 17 Mar 2025 18:41:20 -0700 (PDT) Received: from [127.0.0.1] ([2001:67c:2fbc:1:3577:c57d:1329:9a15]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-395c83b6b2bsm16230838f8f.26.2025.03.17.18.41.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Mar 2025 18:41:20 -0700 (PDT) From: Antonio Quartulli Date: Tue, 18 Mar 2025 02:40:50 +0100 Subject: [PATCH net-next v24 15/23] ovpn: implement peer lookup logic Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250318-b4-ovpn-v24-15-3ec4ab5c4a77@openvpn.net> References: <20250318-b4-ovpn-v24-0-3ec4ab5c4a77@openvpn.net> In-Reply-To: <20250318-b4-ovpn-v24-0-3ec4ab5c4a77@openvpn.net> To: netdev@vger.kernel.org, Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan , sd@queasysnail.net, ryazanov.s.a@gmail.com, Andrew Lunn Cc: Simon Horman , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Xiao Liang X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=11637; i=antonio@openvpn.net; h=from:subject:message-id; bh=iB5Qbo870WqlbaCwTJXZDkTFSeANy8yaa5A4feFl5uI=; b=owEBbQGS/pANAwAIAQtw5TqgONWHAcsmYgBn2M8qy8p0GsGbpugkT1SAMv94rVXZup6mCBUa6 MrBR3wtgFGJATMEAAEIAB0WIQSZq9xs+NQS5N5fwPwLcOU6oDjVhwUCZ9jPKgAKCRALcOU6oDjV h/9nCACLcdU4z44DHtcLELhUfGLdHp3Ygnisd8qD+eYmMRQbcKfDLHSnS6zBmIXnesGyRn7oPBx YCBo08IIfwQCr5nv19vAqCrPq9p3PVJ2VlFfDbzqnt3+6j9xK6ZsrmGmpcrtFM3yzdHIxBHLKPa oC27QzFS2NA3GRQ6K2UxsZxgxxr6Yt23mF6T9+sYQG09gHYEgrYuhMNls3G2vUWLBIAhMOJVsIr 3LGU2IeoCbXXPZlqDem/HpJrS1/Ynxwx6LvVkFF77RnMdtcos5Xxao//Fj9ThnjIplHWH7lNIW5 5/jocZA3O4QYfSUoXrK3IKX4yQ1DbF04qhSqZBMhRsqIdnj6 X-Developer-Key: i=antonio@openvpn.net; a=openpgp; fpr=CABDA1282017C267219885C748F0CCB68F59D14C In a multi-peer scenario there are a number of situations when a specific peer needs to be looked up. We may want to lookup a peer by: 1. its ID 2. its VPN destination IP 3. its transport IP/port couple For each of the above, there is a specific routing table referencing all peers for fast look up. Case 2. is a bit special in the sense that an outgoing packet may not be sent to the peer VPN IP directly, but rather to a network behind it. For this reason we first perform a nexthop lookup in the system routing table and then we use the retrieved nexthop as peer search key. Signed-off-by: Antonio Quartulli --- drivers/net/ovpn/peer.c | 301 ++++++++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 291 insertions(+), 10 deletions(-) diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index bed2e591c000c4efecdcd92db484e590f97f9f7f..2d6cecc28c5d1f7d5516f7f89bc= 0ba274c72d5e1 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -10,6 +10,7 @@ #include #include #include +#include =20 #include "ovpnpriv.h" #include "bind.h" @@ -150,6 +151,121 @@ static int ovpn_peer_skb_to_sockaddr(struct sk_buff *= skb, return -1; } =20 +/** + * ovpn_nexthop_from_skb4 - retrieve IPv4 nexthop for outgoing skb + * @skb: the outgoing packet + * + * Return: the IPv4 of the nexthop + */ +static __be32 ovpn_nexthop_from_skb4(struct sk_buff *skb) +{ + const struct rtable *rt =3D skb_rtable(skb); + + if (rt && rt->rt_uses_gateway) + return rt->rt_gw4; + + return ip_hdr(skb)->daddr; +} + +/** + * ovpn_nexthop_from_skb6 - retrieve IPv6 nexthop for outgoing skb + * @skb: the outgoing packet + * + * Return: the IPv6 of the nexthop + */ +static struct in6_addr ovpn_nexthop_from_skb6(struct sk_buff *skb) +{ + const struct rt6_info *rt =3D skb_rt6_info(skb); + + if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) + return ipv6_hdr(skb)->daddr; + + return rt->rt6i_gateway; +} + +/* variable name __tbl2 needs to be different from __tbl1 + * in the macro below to avoid confusing clang + */ +#define ovpn_get_hash_slot(_tbl, _key, _key_len) ({ \ + typeof(_tbl) *__tbl2 =3D &(_tbl); \ + jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl2); \ +}) + +#define ovpn_get_hash_head(_tbl, _key, _key_len) ({ \ + typeof(_tbl) *__tbl1 =3D &(_tbl); \ + &(*__tbl1)[ovpn_get_hash_slot(*__tbl1, _key, _key_len)];\ +}) + +/** + * ovpn_peer_get_by_vpn_addr4 - retrieve peer by its VPN IPv4 address + * @ovpn: the openvpn instance to search + * @addr: VPN IPv4 to use as search key + * + * Refcounter is not increased for the returned peer. + * + * Return: the peer if found or NULL otherwise + */ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr4(struct ovpn_priv *ovpn, + __be32 addr) +{ + struct hlist_nulls_head *nhead; + struct hlist_nulls_node *ntmp; + struct ovpn_peer *tmp; + unsigned int slot; + +begin: + slot =3D ovpn_get_hash_slot(ovpn->peers->by_vpn_addr4, &addr, + sizeof(addr)); + nhead =3D &ovpn->peers->by_vpn_addr4[slot]; + + hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, hash_entry_addr4) + if (addr =3D=3D tmp->vpn_addrs.ipv4.s_addr) + return tmp; + + /* item may have moved during lookup - check nulls and restart + * if that's the case + */ + if (get_nulls_value(ntmp) !=3D slot) + goto begin; + + return NULL; +} + +/** + * ovpn_peer_get_by_vpn_addr6 - retrieve peer by its VPN IPv6 address + * @ovpn: the openvpn instance to search + * @addr: VPN IPv6 to use as search key + * + * Refcounter is not increased for the returned peer. + * + * Return: the peer if found or NULL otherwise + */ +static struct ovpn_peer *ovpn_peer_get_by_vpn_addr6(struct ovpn_priv *ovpn, + struct in6_addr *addr) +{ + struct hlist_nulls_head *nhead; + struct hlist_nulls_node *ntmp; + struct ovpn_peer *tmp; + unsigned int slot; + +begin: + slot =3D ovpn_get_hash_slot(ovpn->peers->by_vpn_addr6, addr, + sizeof(*addr)); + nhead =3D &ovpn->peers->by_vpn_addr6[slot]; + + hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, hash_entry_addr6) + if (ipv6_addr_equal(addr, &tmp->vpn_addrs.ipv6)) + return tmp; + + /* item may have moved during lookup - check nulls and restart + * if that's the case + */ + if (get_nulls_value(ntmp) !=3D slot) + goto begin; + + return NULL; +} + /** * ovpn_peer_transp_match - check if sockaddr and peer binding match * @peer: the peer to get the binding from @@ -227,14 +343,43 @@ ovpn_peer_get_by_transp_addr_p2p(struct ovpn_priv *ov= pn, struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_priv *ovpn, struct sk_buff *skb) { - struct ovpn_peer *peer =3D NULL; + struct ovpn_peer *tmp, *peer =3D NULL; struct sockaddr_storage ss =3D { 0 }; + struct hlist_nulls_head *nhead; + struct hlist_nulls_node *ntmp; + unsigned int slot; + ssize_t sa_len; =20 - if (unlikely(!ovpn_peer_skb_to_sockaddr(skb, &ss))) + sa_len =3D ovpn_peer_skb_to_sockaddr(skb, &ss); + if (unlikely(sa_len < 0)) return NULL; =20 if (ovpn->mode =3D=3D OVPN_MODE_P2P) - peer =3D ovpn_peer_get_by_transp_addr_p2p(ovpn, &ss); + return ovpn_peer_get_by_transp_addr_p2p(ovpn, &ss); + + rcu_read_lock(); +begin: + slot =3D ovpn_get_hash_slot(ovpn->peers->by_transp_addr, &ss, sa_len); + nhead =3D &ovpn->peers->by_transp_addr[slot]; + + hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, + hash_entry_transp_addr) { + if (!ovpn_peer_transp_match(tmp, &ss)) + continue; + + if (!ovpn_peer_hold(tmp)) + continue; + + peer =3D tmp; + break; + } + + /* item may have moved during lookup - check nulls and restart + * if that's the case + */ + if (!peer && get_nulls_value(ntmp) !=3D slot) + goto begin; + rcu_read_unlock(); =20 return peer; } @@ -269,10 +414,27 @@ static struct ovpn_peer *ovpn_peer_get_by_id_p2p(stru= ct ovpn_priv *ovpn, */ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_priv *ovpn, u32 peer_id) { - struct ovpn_peer *peer =3D NULL; + struct ovpn_peer *tmp, *peer =3D NULL; + struct hlist_head *head; =20 if (ovpn->mode =3D=3D OVPN_MODE_P2P) - peer =3D ovpn_peer_get_by_id_p2p(ovpn, peer_id); + return ovpn_peer_get_by_id_p2p(ovpn, peer_id); + + head =3D ovpn_get_hash_head(ovpn->peers->by_id, &peer_id, + sizeof(peer_id)); + + rcu_read_lock(); + hlist_for_each_entry_rcu(tmp, head, hash_entry_id) { + if (tmp->id !=3D peer_id) + continue; + + if (!ovpn_peer_hold(tmp)) + continue; + + peer =3D tmp; + break; + } + rcu_read_unlock(); =20 return peer; } @@ -330,6 +492,8 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_priv= *ovpn, struct sk_buff *skb) { struct ovpn_peer *peer =3D NULL; + struct in6_addr addr6; + __be32 addr4; =20 /* in P2P mode, no matter the destination, packets are always sent to * the single peer listening on the other side @@ -340,11 +504,109 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_p= riv *ovpn, if (unlikely(peer && !ovpn_peer_hold(peer))) peer =3D NULL; rcu_read_unlock(); + return peer; } =20 + rcu_read_lock(); + switch (skb->protocol) { + case htons(ETH_P_IP): + addr4 =3D ovpn_nexthop_from_skb4(skb); + peer =3D ovpn_peer_get_by_vpn_addr4(ovpn, addr4); + break; + case htons(ETH_P_IPV6): + addr6 =3D ovpn_nexthop_from_skb6(skb); + peer =3D ovpn_peer_get_by_vpn_addr6(ovpn, &addr6); + break; + } + + if (unlikely(peer && !ovpn_peer_hold(peer))) + peer =3D NULL; + rcu_read_unlock(); + return peer; } =20 +/** + * ovpn_nexthop_from_rt4 - look up the IPv4 nexthop for the given destinat= ion + * @ovpn: the private data representing the current VPN session + * @dest: the destination to be looked up + * + * Looks up in the IPv4 system routing table the IP of the nexthop to be u= sed + * to reach the destination passed as argument. If no nexthop can be found= , the + * destination itself is returned as it probably has to be used as nexthop. + * + * Return: the IP of the next hop if found or dest itself otherwise + */ +static __be32 ovpn_nexthop_from_rt4(struct ovpn_priv *ovpn, __be32 dest) +{ + struct rtable *rt; + struct flowi4 fl =3D { + .daddr =3D dest + }; + + rt =3D ip_route_output_flow(dev_net(ovpn->dev), &fl, NULL); + if (IS_ERR(rt)) { + net_dbg_ratelimited("%s: no route to host %pI4\n", + netdev_name(ovpn->dev), &dest); + /* if we end up here this packet is probably going to be + * thrown away later + */ + return dest; + } + + if (!rt->rt_uses_gateway) + goto out; + + dest =3D rt->rt_gw4; +out: + ip_rt_put(rt); + return dest; +} + +/** + * ovpn_nexthop_from_rt6 - look up the IPv6 nexthop for the given destinat= ion + * @ovpn: the private data representing the current VPN session + * @dest: the destination to be looked up + * + * Looks up in the IPv6 system routing table the IP of the nexthop to be u= sed + * to reach the destination passed as argument. If no nexthop can be found= , the + * destination itself is returned as it probably has to be used as nexthop. + * + * Return: the IP of the next hop if found or dest itself otherwise + */ +static struct in6_addr ovpn_nexthop_from_rt6(struct ovpn_priv *ovpn, + struct in6_addr dest) +{ +#if IS_ENABLED(CONFIG_IPV6) + struct dst_entry *entry; + struct rt6_info *rt; + struct flowi6 fl =3D { + .daddr =3D dest, + }; + + entry =3D ipv6_stub->ipv6_dst_lookup_flow(dev_net(ovpn->dev), NULL, &fl, + NULL); + if (IS_ERR(entry)) { + net_dbg_ratelimited("%s: no route to host %pI6c\n", + netdev_name(ovpn->dev), &dest); + /* if we end up here this packet is probably going to be + * thrown away later + */ + return dest; + } + + rt =3D dst_rt6_info(entry); + + if (!(rt->rt6i_flags & RTF_GATEWAY)) + goto out; + + dest =3D rt->rt6i_gateway; +out: + dst_release((struct dst_entry *)rt); +#endif + return dest; +} + /** * ovpn_peer_check_by_src - check that skb source is routed via peer * @ovpn: the openvpn instance to search @@ -357,21 +619,40 @@ bool ovpn_peer_check_by_src(struct ovpn_priv *ovpn, s= truct sk_buff *skb, struct ovpn_peer *peer) { bool match =3D false; + struct in6_addr addr6; + __be32 addr4; =20 if (ovpn->mode =3D=3D OVPN_MODE_P2P) { /* in P2P mode, no matter the destination, packets are always * sent to the single peer listening on the other side */ - match =3D (peer =3D=3D rcu_access_pointer(ovpn->peer)); + return peer =3D=3D rcu_access_pointer(ovpn->peer); + } + + /* This function performs a reverse path check, therefore we now + * lookup the nexthop we would use if we wanted to route a packet + * to the source IP. If the nexthop matches the sender we know the + * latter is valid and we allow the packet to come in + */ + + switch (skb->protocol) { + case htons(ETH_P_IP): + addr4 =3D ovpn_nexthop_from_rt4(ovpn, ip_hdr(skb)->saddr); + rcu_read_lock(); + match =3D (peer =3D=3D ovpn_peer_get_by_vpn_addr4(ovpn, addr4)); + rcu_read_unlock(); + break; + case htons(ETH_P_IPV6): + addr6 =3D ovpn_nexthop_from_rt6(ovpn, ipv6_hdr(skb)->saddr); + rcu_read_lock(); + match =3D (peer =3D=3D ovpn_peer_get_by_vpn_addr6(ovpn, &addr6)); + rcu_read_unlock(); + break; } =20 return match; } =20 -#define ovpn_get_hash_head(_tbl, _key, _key_len) ({ \ - typeof(_tbl) *__tbl =3D &(_tbl); \ - (&(*__tbl)[jhash(_key, _key_len, 0) % HASH_SIZE(*__tbl)]); }) \ - /** * ovpn_peer_add_mp - add peer to related tables in a MP instance * @ovpn: the instance to add the peer to --=20 2.48.1