From nobody Thu Nov 28 13:59:20 2024 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 42ADE1A08AD for ; Wed, 2 Oct 2024 09:03:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727859819; cv=none; b=qgdQyQGh+bDPZBFsXqTI3E6BF7CAwxDnV2Skc/sq4sbcMvCbPNYKAQRn13IBadiXykQPx2QihKX7UYjmj/5pEZENRp3jYWrB/rKG3b7SZeWHCwActmM6IsKMZ9+FYPyd9Kq3PL52o4mXMm/qxia7A9MknDA/kE0mNnCgtWNBajE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727859819; c=relaxed/simple; bh=q0PhaZp9wJNYhCUmStZzBiNVB2VevlrBweECXMf1Rpc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dDGMNzhDo9KysF7Vm/TMrCNZWq67ha0CEo7QAXGV5KrzqkhYyy8yodtQnuQ/gAr2ei6Y865SfVTCQvqHPh77ylIkXsV/rbmLuGhx/eLTh81mxagCl32GqRmo0CXcXc5AQPcRsEcR4dwIH/v7K9kEiKnr1F7UNhpd9OeZmR5Dtl0= 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=RIfCoUd7; arc=none smtp.client-ip=209.85.221.41 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="RIfCoUd7" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-37ccc600466so2879170f8f.1 for ; Wed, 02 Oct 2024 02:03:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1727859815; x=1728464615; 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=gHXA82IaKBwNCAyOFeIau6drzsQmyQRGnjGyMzwhyz4=; b=RIfCoUd7zafvlf874SQCxYEROetUC6KJwG6aUEdl4QhZEHMM7kgCflG85rpItznXvt GNy2nnF0efyjAWmYsbThXI6V8rfPv/WFZbBtXGMYX6+vPsOhih4s642eudrRD/txUI9p MzKnqjC1vLATnDbmKxaOCMxcnKlwvv2pRug+TZr5w2uviA9mNaTe1QIxSkQ97tXQ1hOl mazumA0xmi56nkLG0vZVJh71ZGfAvQOdWoBrIHF5DFnRr/cGoeg2Bk8PXNXlM5E2REc6 kVm4xaeKXRydPyfh1RJZGDIPQI42ceMi3jSVe9FF3aWb9EhcTMUGVSLZH9V2EPHtBsYb eqMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727859815; x=1728464615; 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=gHXA82IaKBwNCAyOFeIau6drzsQmyQRGnjGyMzwhyz4=; b=DhN23GeUUCFrYi4UOBQX2lcc4yu/1q5HhT01WxVWvOt58YYnG9NYPwEEBvB1Y4WBcj +lDWlI+6X4FlPSRJyipp718Mc0LtKo5kbt0pNZTxO8moInc3yv5kVtt8wAkUiaQaha+C EOLUyUX6Jxo6Fw/nN0sYzsOX+xFRLNR0BrlzN1HQoAi4KvpzjCjGg7+eofoRbJ/UTznx +ha6e/VkCbZeGS+za77wByuVauTLGevya9UvviqAYIqbHhRP51moLmdoZPnIhJ9J3NdR qHRNEtXQBG1R8vWYtxSAsVEVeWXUHqlZlOl2tH7o7HwXNZukMcpAq9XGPAGd0WHriyC3 ewSA== X-Forwarded-Encrypted: i=1; AJvYcCVqa0avou7lJbMx5LtQS5CEeyZ5GaS6qBn7+zGvGMWv4kQPvFGHSfqB2Aa/NYtjpMglFqEBrZRtdXVLAb4=@vger.kernel.org X-Gm-Message-State: AOJu0YxIjZVk+D4OfmrLoHPTdWVSCx3O4T+78wNForiiKtJwEDx2ASD8 9kJgai1ybIcUvyj7ClwrvTQUfWb4tn5CgfpZpQI4cV84nLd1A/vLet8DqxzbqJ0= X-Google-Smtp-Source: AGHT+IGFkU4ubUMr1Ec6IAhYLJl+TeBbSejZ6TpcKm1lmXuf8R2xaciclbXNIiFxH+RJk/WKitOqoQ== X-Received: by 2002:a05:6000:547:b0:374:c56e:1d44 with SMTP id ffacd0b85a97d-37cfba0f398mr1357871f8f.48.1727859814499; Wed, 02 Oct 2024 02:03:34 -0700 (PDT) Received: from serenity.mandelbit.com ([2001:67c:2fbc:1:da6e:ecd8:2234:c32e]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37cd56e8822sm13602320f8f.50.2024.10.02.02.03.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 02:03:34 -0700 (PDT) From: Antonio Quartulli Date: Wed, 02 Oct 2024 11:02:29 +0200 Subject: [PATCH net-next v8 15/24] 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: <20241002-b4-ovpn-v8-15-37ceffcffbde@openvpn.net> References: <20241002-b4-ovpn-v8-0-37ceffcffbde@openvpn.net> In-Reply-To: <20241002-b4-ovpn-v8-0-37ceffcffbde@openvpn.net> To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Antonio Quartulli , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, sd@queasysnail.net, ryazanov.s.a@gmail.com X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=10819; i=antonio@openvpn.net; h=from:subject:message-id; bh=q0PhaZp9wJNYhCUmStZzBiNVB2VevlrBweECXMf1Rpc=; b=owGbwMvMwMHIXfDUaoHF1XbG02pJDGl/eYJkL3s5ah3e8k0hVvvU0cvqtZ5v3RQ9vAVUn8bYT 4pw/HO3k9GYhYGRg0FWTJFl5uo7OT+uCD25F3/gD8wgViaQKQxcnAIwkSB/9r8yF31se96dOtGp Zqj//ZNvYirjqWPsJilJb04w3Bd9nPo7z77gzekoTTPvQ93G2h4P/YssLUIcGeIF6n1VP2w15rh cUaPAJNI6OXaW2nI3r1ab0ndn/h8ztvEOLkpVXeu4NDi2KyakbDWLsGDkvKbGiVsC06L0rq7fM9 /0qIN9h7+JuODHywqF23baa105vDVQ4dg6lVmr66peCV8884ON8691Tsvf+5xLxZs63mp9Ylbb2 /qL84HqVI3SXjUuY7Wrty07Yh9M+DSfi/GQcfLny+tis2Qyrpf2yS5lzhO9PPuUW+yExI/p6kvn FTrndfx/vKXsgVzTzOVZ0pulHX7vT9y7Piv6/3eW4K+bAA== 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 | 272 ++++++++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 264 insertions(+), 8 deletions(-) diff --git a/drivers/net/ovpn/peer.c b/drivers/net/ovpn/peer.c index 3c6fbf99f696c18d8a2dbe169c7f6f5933fb71ac..54d0a416f6f91513f42d5ecc1f1= f65d688f3e908 100644 --- a/drivers/net/ovpn/peer.c +++ b/drivers/net/ovpn/peer.c @@ -10,6 +10,7 @@ #include #include #include +#include =20 #include "ovpnstruct.h" #include "bind.h" @@ -126,6 +127,94 @@ static bool ovpn_peer_skb_to_sockaddr(struct sk_buff *= skb, return true; } =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; +} + +#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_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_struct *ov= pn, + __be32 addr) +{ + struct hlist_nulls_head *nhead; + struct hlist_nulls_node *ntmp; + struct ovpn_peer *tmp; + + nhead =3D ovpn_get_hash_head(ovpn->peers->by_vpn_addr, &addr, + sizeof(addr)); + + hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, hash_entry_addr4) + if (addr =3D=3D tmp->vpn_addrs.ipv4.s_addr) + return tmp; + + 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_struct *ov= pn, + struct in6_addr *addr) +{ + struct hlist_nulls_head *nhead; + struct hlist_nulls_node *ntmp; + struct ovpn_peer *tmp; + + nhead =3D ovpn_get_hash_head(ovpn->peers->by_vpn_addr, addr, + sizeof(*addr)); + + hlist_nulls_for_each_entry_rcu(tmp, ntmp, nhead, hash_entry_addr6) + if (ipv6_addr_equal(addr, &tmp->vpn_addrs.ipv6)) + return tmp; + + return NULL; +} + /** * ovpn_peer_transp_match - check if sockaddr and peer binding match * @peer: the peer to get the binding from @@ -203,14 +292,44 @@ ovpn_peer_get_by_transp_addr_p2p(struct ovpn_struct *= ovpn, struct ovpn_peer *ovpn_peer_get_by_transp_addr(struct ovpn_struct *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; + size_t sa_len; =20 if (unlikely(!ovpn_peer_skb_to_sockaddr(skb, &ss))) 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); + + switch (ss.ss_family) { + case AF_INET: + sa_len =3D sizeof(struct sockaddr_in); + break; + case AF_INET6: + sa_len =3D sizeof(struct sockaddr_in6); + break; + default: + return NULL; + } + + nhead =3D ovpn_get_hash_head(ovpn->peers->by_transp_addr, &ss, sa_len); + + rcu_read_lock(); + 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; + } + rcu_read_unlock(); =20 return peer; } @@ -245,10 +364,27 @@ static struct ovpn_peer *ovpn_peer_get_by_id_p2p(stru= ct ovpn_struct *ovpn, */ struct ovpn_peer *ovpn_peer_get_by_id(struct ovpn_struct *ovpn, u32 peer_i= d) { - 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; } @@ -270,6 +406,8 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_stru= ct *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 @@ -280,11 +418,109 @@ struct ovpn_peer *ovpn_peer_get_by_dst(struct ovpn_s= truct *ovpn, if (unlikely(peer && !ovpn_peer_hold(peer))) peer =3D NULL; rcu_read_unlock(); + return peer; } =20 + rcu_read_lock(); + switch (skb_protocol_to_family(skb)) { + case AF_INET: + addr4 =3D ovpn_nexthop_from_skb4(skb); + peer =3D ovpn_peer_get_by_vpn_addr4(ovpn, addr4); + break; + case AF_INET6: + 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_struct *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", __func__, + &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_struct *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", __func__, + &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 @@ -297,6 +533,8 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *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 @@ -305,15 +543,33 @@ bool ovpn_peer_check_by_src(struct ovpn_struct *ovpn,= struct sk_buff *skb, rcu_read_lock(); match =3D (peer =3D=3D rcu_dereference(ovpn->peer)); rcu_read_unlock(); + return match; + } + + /* 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_to_family(skb)) { + case AF_INET: + 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 AF_INET6: + 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.45.2