net: ip: make ip_route_input_slow() return drop reasons

In this commit, we make ip_route_input_slow() return skb drop reasons,
and following new skb drop reasons are added:

  SKB_DROP_REASON_IP_INVALID_DEST

The only caller of ip_route_input_slow() is ip_route_input_rcu(), and we
adjust it by making it return -EINVAL on error.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
This commit is contained in:
Menglong Dong 2024-11-07 20:55:56 +08:00 committed by Paolo Abeni
parent d46f827016
commit 5b92112acd
2 changed files with 41 additions and 21 deletions

View file

@ -79,6 +79,7 @@
FN(IP_LOCAL_SOURCE) \ FN(IP_LOCAL_SOURCE) \
FN(IP_INVALID_SOURCE) \ FN(IP_INVALID_SOURCE) \
FN(IP_LOCALNET) \ FN(IP_LOCALNET) \
FN(IP_INVALID_DEST) \
FN(PKT_TOO_BIG) \ FN(PKT_TOO_BIG) \
FN(DUP_FRAG) \ FN(DUP_FRAG) \
FN(FRAG_REASM_TIMEOUT) \ FN(FRAG_REASM_TIMEOUT) \
@ -386,6 +387,11 @@ enum skb_drop_reason {
SKB_DROP_REASON_IP_INVALID_SOURCE, SKB_DROP_REASON_IP_INVALID_SOURCE,
/** @SKB_DROP_REASON_IP_LOCALNET: source or dest ip is local net */ /** @SKB_DROP_REASON_IP_LOCALNET: source or dest ip is local net */
SKB_DROP_REASON_IP_LOCALNET, SKB_DROP_REASON_IP_LOCALNET,
/**
* @SKB_DROP_REASON_IP_INVALID_DEST: the dest ip is invalid:
* 1) dest ip is 0
*/
SKB_DROP_REASON_IP_INVALID_DEST,
/** /**
* @SKB_DROP_REASON_PKT_TOO_BIG: packet size is too big (maybe exceed the * @SKB_DROP_REASON_PKT_TOO_BIG: packet size is too big (maybe exceed the
* MTU) * MTU)

View file

@ -2217,9 +2217,10 @@ static struct net_device *ip_rt_get_dev(struct net *net,
* called with rcu_read_lock() * called with rcu_read_lock()
*/ */
static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr, static enum skb_drop_reason
dscp_t dscp, struct net_device *dev, ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
struct fib_result *res) dscp_t dscp, struct net_device *dev,
struct fib_result *res)
{ {
enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
struct in_device *in_dev = __in_dev_get_rcu(dev); struct in_device *in_dev = __in_dev_get_rcu(dev);
@ -2249,8 +2250,10 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
fl4.flowi4_tun_key.tun_id = 0; fl4.flowi4_tun_key.tun_id = 0;
skb_dst_drop(skb); skb_dst_drop(skb);
if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) {
reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
goto martian_source; goto martian_source;
}
res->fi = NULL; res->fi = NULL;
res->table = NULL; res->table = NULL;
@ -2260,21 +2263,29 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
/* Accept zero addresses only to limited broadcast; /* Accept zero addresses only to limited broadcast;
* I even do not know to fix it or not. Waiting for complains :-) * I even do not know to fix it or not. Waiting for complains :-)
*/ */
if (ipv4_is_zeronet(saddr)) if (ipv4_is_zeronet(saddr)) {
reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
goto martian_source; goto martian_source;
}
if (ipv4_is_zeronet(daddr)) if (ipv4_is_zeronet(daddr)) {
reason = SKB_DROP_REASON_IP_INVALID_DEST;
goto martian_destination; goto martian_destination;
}
/* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(), /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(),
* and call it once if daddr or/and saddr are loopback addresses * and call it once if daddr or/and saddr are loopback addresses
*/ */
if (ipv4_is_loopback(daddr)) { if (ipv4_is_loopback(daddr)) {
if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) {
reason = SKB_DROP_REASON_IP_LOCALNET;
goto martian_destination; goto martian_destination;
}
} else if (ipv4_is_loopback(saddr)) { } else if (ipv4_is_loopback(saddr)) {
if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) {
reason = SKB_DROP_REASON_IP_LOCALNET;
goto martian_source; goto martian_source;
}
} }
/* /*
@ -2329,19 +2340,26 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
err = -EHOSTUNREACH; err = -EHOSTUNREACH;
goto no_route; goto no_route;
} }
if (res->type != RTN_UNICAST) if (res->type != RTN_UNICAST) {
reason = SKB_DROP_REASON_IP_INVALID_DEST;
goto martian_destination; goto martian_destination;
}
make_route: make_route:
err = ip_mkroute_input(skb, res, in_dev, daddr, saddr, dscp, flkeys); err = ip_mkroute_input(skb, res, in_dev, daddr, saddr, dscp, flkeys);
out: return err; if (!err)
reason = SKB_NOT_DROPPED_YET;
out:
return reason;
brd_input: brd_input:
if (skb->protocol != htons(ETH_P_IP)) if (skb->protocol != htons(ETH_P_IP)) {
goto e_inval; reason = SKB_DROP_REASON_INVALID_PROTO;
goto out;
}
if (!ipv4_is_zeronet(saddr)) { if (!ipv4_is_zeronet(saddr)) {
err = -EINVAL;
reason = fib_validate_source_reason(skb, saddr, 0, dscp, 0, reason = fib_validate_source_reason(skb, saddr, 0, dscp, 0,
dev, in_dev, &itag); dev, in_dev, &itag);
if (reason) if (reason)
@ -2362,7 +2380,7 @@ local_input:
rth = rcu_dereference(nhc->nhc_rth_input); rth = rcu_dereference(nhc->nhc_rth_input);
if (rt_cache_valid(rth)) { if (rt_cache_valid(rth)) {
skb_dst_set_noref(skb, &rth->dst); skb_dst_set_noref(skb, &rth->dst);
err = 0; reason = SKB_NOT_DROPPED_YET;
goto out; goto out;
} }
} }
@ -2399,7 +2417,7 @@ local_input:
rt_add_uncached_list(rth); rt_add_uncached_list(rth);
} }
skb_dst_set(skb, &rth->dst); skb_dst_set(skb, &rth->dst);
err = 0; reason = SKB_NOT_DROPPED_YET;
goto out; goto out;
no_route: no_route:
@ -2420,12 +2438,8 @@ martian_destination:
&daddr, &saddr, dev->name); &daddr, &saddr, dev->name);
#endif #endif
e_inval:
err = -EINVAL;
goto out;
e_nobufs: e_nobufs:
err = -ENOBUFS; reason = SKB_DROP_REASON_NOMEM;
goto out; goto out;
martian_source: martian_source:
@ -2482,7 +2496,7 @@ static int ip_route_input_rcu(struct sk_buff *skb, __be32 daddr, __be32 saddr,
return reason ? -EINVAL : 0; return reason ? -EINVAL : 0;
} }
return ip_route_input_slow(skb, daddr, saddr, dscp, dev, res); return ip_route_input_slow(skb, daddr, saddr, dscp, dev, res) ? -EINVAL : 0;
} }
int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr, int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,