Skip to content

Commit 5b92112

Browse files
image-dragonPaolo Abeni
authored and
Paolo Abeni
committed
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 <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent d46f827 commit 5b92112

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

include/net/dropreason-core.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
FN(IP_LOCAL_SOURCE) \
8080
FN(IP_INVALID_SOURCE) \
8181
FN(IP_LOCALNET) \
82+
FN(IP_INVALID_DEST) \
8283
FN(PKT_TOO_BIG) \
8384
FN(DUP_FRAG) \
8485
FN(FRAG_REASM_TIMEOUT) \
@@ -386,6 +387,11 @@ enum skb_drop_reason {
386387
SKB_DROP_REASON_IP_INVALID_SOURCE,
387388
/** @SKB_DROP_REASON_IP_LOCALNET: source or dest ip is local net */
388389
SKB_DROP_REASON_IP_LOCALNET,
390+
/**
391+
* @SKB_DROP_REASON_IP_INVALID_DEST: the dest ip is invalid:
392+
* 1) dest ip is 0
393+
*/
394+
SKB_DROP_REASON_IP_INVALID_DEST,
389395
/**
390396
* @SKB_DROP_REASON_PKT_TOO_BIG: packet size is too big (maybe exceed the
391397
* MTU)

net/ipv4/route.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,9 +2217,10 @@ static struct net_device *ip_rt_get_dev(struct net *net,
22172217
* called with rcu_read_lock()
22182218
*/
22192219

2220-
static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
2221-
dscp_t dscp, struct net_device *dev,
2222-
struct fib_result *res)
2220+
static enum skb_drop_reason
2221+
ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
2222+
dscp_t dscp, struct net_device *dev,
2223+
struct fib_result *res)
22232224
{
22242225
enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
22252226
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,
22492250
fl4.flowi4_tun_key.tun_id = 0;
22502251
skb_dst_drop(skb);
22512252

2252-
if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
2253+
if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) {
2254+
reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
22532255
goto martian_source;
2256+
}
22542257

22552258
res->fi = NULL;
22562259
res->table = NULL;
@@ -2260,21 +2263,29 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
22602263
/* Accept zero addresses only to limited broadcast;
22612264
* I even do not know to fix it or not. Waiting for complains :-)
22622265
*/
2263-
if (ipv4_is_zeronet(saddr))
2266+
if (ipv4_is_zeronet(saddr)) {
2267+
reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
22642268
goto martian_source;
2269+
}
22652270

2266-
if (ipv4_is_zeronet(daddr))
2271+
if (ipv4_is_zeronet(daddr)) {
2272+
reason = SKB_DROP_REASON_IP_INVALID_DEST;
22672273
goto martian_destination;
2274+
}
22682275

22692276
/* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(),
22702277
* and call it once if daddr or/and saddr are loopback addresses
22712278
*/
22722279
if (ipv4_is_loopback(daddr)) {
2273-
if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
2280+
if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) {
2281+
reason = SKB_DROP_REASON_IP_LOCALNET;
22742282
goto martian_destination;
2283+
}
22752284
} else if (ipv4_is_loopback(saddr)) {
2276-
if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net))
2285+
if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) {
2286+
reason = SKB_DROP_REASON_IP_LOCALNET;
22772287
goto martian_source;
2288+
}
22782289
}
22792290

22802291
/*
@@ -2329,19 +2340,26 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
23292340
err = -EHOSTUNREACH;
23302341
goto no_route;
23312342
}
2332-
if (res->type != RTN_UNICAST)
2343+
if (res->type != RTN_UNICAST) {
2344+
reason = SKB_DROP_REASON_IP_INVALID_DEST;
23332345
goto martian_destination;
2346+
}
23342347

23352348
make_route:
23362349
err = ip_mkroute_input(skb, res, in_dev, daddr, saddr, dscp, flkeys);
2337-
out: return err;
2350+
if (!err)
2351+
reason = SKB_NOT_DROPPED_YET;
2352+
2353+
out:
2354+
return reason;
23382355

23392356
brd_input:
2340-
if (skb->protocol != htons(ETH_P_IP))
2341-
goto e_inval;
2357+
if (skb->protocol != htons(ETH_P_IP)) {
2358+
reason = SKB_DROP_REASON_INVALID_PROTO;
2359+
goto out;
2360+
}
23422361

23432362
if (!ipv4_is_zeronet(saddr)) {
2344-
err = -EINVAL;
23452363
reason = fib_validate_source_reason(skb, saddr, 0, dscp, 0,
23462364
dev, in_dev, &itag);
23472365
if (reason)
@@ -2362,7 +2380,7 @@ out: return err;
23622380
rth = rcu_dereference(nhc->nhc_rth_input);
23632381
if (rt_cache_valid(rth)) {
23642382
skb_dst_set_noref(skb, &rth->dst);
2365-
err = 0;
2383+
reason = SKB_NOT_DROPPED_YET;
23662384
goto out;
23672385
}
23682386
}
@@ -2399,7 +2417,7 @@ out: return err;
23992417
rt_add_uncached_list(rth);
24002418
}
24012419
skb_dst_set(skb, &rth->dst);
2402-
err = 0;
2420+
reason = SKB_NOT_DROPPED_YET;
24032421
goto out;
24042422

24052423
no_route:
@@ -2420,12 +2438,8 @@ out: return err;
24202438
&daddr, &saddr, dev->name);
24212439
#endif
24222440

2423-
e_inval:
2424-
err = -EINVAL;
2425-
goto out;
2426-
24272441
e_nobufs:
2428-
err = -ENOBUFS;
2442+
reason = SKB_DROP_REASON_NOMEM;
24292443
goto out;
24302444

24312445
martian_source:
@@ -2482,7 +2496,7 @@ static int ip_route_input_rcu(struct sk_buff *skb, __be32 daddr, __be32 saddr,
24822496
return reason ? -EINVAL : 0;
24832497
}
24842498

2485-
return ip_route_input_slow(skb, daddr, saddr, dscp, dev, res);
2499+
return ip_route_input_slow(skb, daddr, saddr, dscp, dev, res) ? -EINVAL : 0;
24862500
}
24872501

24882502
int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr,

0 commit comments

Comments
 (0)