From: Menglong Dong <menglong8.dong@gmail.com>
To: pabeni@redhat.com
Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
dsahern@kernel.org, pablo@netfilter.org, kadlec@netfilter.org,
roopa@nvidia.com, razor@blackwall.org, gnault@redhat.com,
bigeasy@linutronix.de, idosch@nvidia.com, ast@kernel.org,
dongml2@chinatelecom.cn, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, netfilter-devel@vger.kernel.org,
coreteam@netfilter.org, bridge@lists.linux.dev,
bpf@vger.kernel.org
Subject: [PATCH net-next v3 01/10] net: ip: refactor fib_validate_source/__fib_validate_source
Date: Tue, 15 Oct 2024 22:07:51 +0800 [thread overview]
Message-ID: <20241015140800.159466-2-dongml2@chinatelecom.cn> (raw)
In-Reply-To: <20241015140800.159466-1-dongml2@chinatelecom.cn>
The only caller of __fib_validate_source() is fib_validate_source(), so
we can combine fib_validate_source() into __fib_validate_source(), and
make fib_validate_source() an inline call to __fib_validate_source().
This will make it easier to make fib_validate_source() return drop reasons
in the next patch.
Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
include/net/ip_fib.h | 15 ++++++++--
net/ipv4/fib_frontend.c | 64 +++++++++++++++++------------------------
2 files changed, 38 insertions(+), 41 deletions(-)
diff --git a/include/net/ip_fib.h b/include/net/ip_fib.h
index b6e44f4eaa4c..90ff815f212b 100644
--- a/include/net/ip_fib.h
+++ b/include/net/ip_fib.h
@@ -448,9 +448,18 @@ int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
struct netlink_ext_ack *extack);
__be32 fib_compute_spec_dst(struct sk_buff *skb);
bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
-int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
- dscp_t dscp, int oif, struct net_device *dev,
- struct in_device *idev, u32 *itag);
+int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
+ dscp_t dscp, int oif, struct net_device *dev,
+ struct in_device *idev, u32 *itag);
+
+static inline int
+fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
+ dscp_t dscp, int oif, struct net_device *dev,
+ struct in_device *idev, u32 *itag)
+{
+ return __fib_validate_source(skb, src, dst, dscp, oif, dev, idev,
+ itag);
+}
#ifdef CONFIG_IP_ROUTE_CLASSID
static inline int fib_num_tclassid_users(struct net *net)
diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
index 8353518b110a..f74138f4d748 100644
--- a/net/ipv4/fib_frontend.c
+++ b/net/ipv4/fib_frontend.c
@@ -341,10 +341,11 @@ EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev);
* - check, that packet arrived from expected physical interface.
* called with rcu_read_lock()
*/
-static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
- dscp_t dscp, int oif, struct net_device *dev,
- int rpf, struct in_device *idev, u32 *itag)
+int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
+ dscp_t dscp, int oif, struct net_device *dev,
+ struct in_device *idev, u32 *itag)
{
+ int rpf = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
struct net *net = dev_net(dev);
struct flow_keys flkeys;
int ret, no_addr;
@@ -352,6 +353,28 @@ static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
struct flowi4 fl4;
bool dev_match;
+ /* Ignore rp_filter for packets protected by IPsec. */
+ if (!rpf && !fib_num_tclassid_users(net) &&
+ (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
+ if (IN_DEV_ACCEPT_LOCAL(idev))
+ goto last_resort;
+ /* with custom local routes in place, checking local addresses
+ * only will be too optimistic, with custom rules, checking
+ * local addresses only can be too strict, e.g. due to vrf
+ */
+ if (net->ipv4.fib_has_custom_local_routes ||
+ fib4_has_custom_rules(net))
+ goto full_check;
+ /* Within the same container, it is regarded as a martian source,
+ * and the same host but different containers are not.
+ */
+ if (inet_lookup_ifaddr_rcu(net, src))
+ return -EINVAL;
+
+ goto last_resort;
+ }
+
+full_check:
fl4.flowi4_oif = 0;
fl4.flowi4_l3mdev = l3mdev_master_ifindex_rcu(dev);
fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
@@ -417,41 +440,6 @@ static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
return -EXDEV;
}
-/* Ignore rp_filter for packets protected by IPsec. */
-int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
- dscp_t dscp, int oif, struct net_device *dev,
- struct in_device *idev, u32 *itag)
-{
- int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
- struct net *net = dev_net(dev);
-
- if (!r && !fib_num_tclassid_users(net) &&
- (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
- if (IN_DEV_ACCEPT_LOCAL(idev))
- goto ok;
- /* with custom local routes in place, checking local addresses
- * only will be too optimistic, with custom rules, checking
- * local addresses only can be too strict, e.g. due to vrf
- */
- if (net->ipv4.fib_has_custom_local_routes ||
- fib4_has_custom_rules(net))
- goto full_check;
- /* Within the same container, it is regarded as a martian source,
- * and the same host but different containers are not.
- */
- if (inet_lookup_ifaddr_rcu(net, src))
- return -EINVAL;
-
-ok:
- *itag = 0;
- return 0;
- }
-
-full_check:
- return __fib_validate_source(skb, src, dst, dscp, oif, dev, r, idev,
- itag);
-}
-
static inline __be32 sk_extract_addr(struct sockaddr *addr)
{
return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
--
2.39.5
next prev parent reply other threads:[~2024-10-15 14:08 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-15 14:07 [PATCH net-next v3 00/10] net: ip: add drop reasons to input route Menglong Dong
2024-10-15 14:07 ` Menglong Dong [this message]
2024-10-21 10:03 ` [PATCH net-next v3 01/10] net: ip: refactor fib_validate_source/__fib_validate_source Paolo Abeni
2024-10-15 14:07 ` [PATCH net-next v3 02/10] net: ip: make fib_validate_source() return drop reason Menglong Dong
2024-10-21 10:20 ` Paolo Abeni
2024-10-21 10:36 ` Paolo Abeni
2024-10-22 8:47 ` Menglong Dong
2024-10-15 14:07 ` [PATCH net-next v3 03/10] net: ip: make ip_route_input_mc() " Menglong Dong
2024-10-15 14:07 ` [PATCH net-next v3 04/10] net: ip: make ip_mc_validate_source() " Menglong Dong
2024-10-15 14:07 ` [PATCH net-next v3 05/10] net: ip: make ip_route_input_slow() return drop reasons Menglong Dong
2024-10-21 10:52 ` Paolo Abeni
2024-10-22 8:50 ` Menglong Dong
2024-10-15 14:07 ` [PATCH net-next v3 06/10] net: ip: make ip_route_input_rcu() " Menglong Dong
2024-10-15 14:07 ` [PATCH net-next v3 07/10] net: ip: make ip_route_input_noref() " Menglong Dong
2024-10-21 10:44 ` Paolo Abeni
2024-10-21 10:49 ` Paolo Abeni
2024-10-22 8:49 ` Menglong Dong
2024-10-15 14:07 ` [PATCH net-next v3 08/10] net: ip: make ip_route_input() " Menglong Dong
2024-10-15 14:07 ` [PATCH net-next v3 09/10] net: ip: make ip_mkroute_input/__mkroute_input " Menglong Dong
2024-10-15 14:08 ` [PATCH net-next v3 10/10] net: ip: make ip_route_use_hint() " Menglong Dong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241015140800.159466-2-dongml2@chinatelecom.cn \
--to=menglong8.dong@gmail.com \
--cc=ast@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=bpf@vger.kernel.org \
--cc=bridge@lists.linux.dev \
--cc=coreteam@netfilter.org \
--cc=davem@davemloft.net \
--cc=dongml2@chinatelecom.cn \
--cc=dsahern@kernel.org \
--cc=edumazet@google.com \
--cc=gnault@redhat.com \
--cc=idosch@nvidia.com \
--cc=kadlec@netfilter.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pablo@netfilter.org \
--cc=razor@blackwall.org \
--cc=roopa@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).