Skip to content

Commit 8b34060

Browse files
committed
net: ip: make fib_validate_source() support drop reasons
JIRA: https://issues.redhat.com/browse/RHEL-88891 Upstream Status: linux.git Conflicts:\ - Minor code differences as the tos to dscp conversion wasn't done in c10s yet. - The newly added fib_validate_source_reason uses dscp_t while c10s will feed tos values. Also it's a wrapper around fib_validate_source which again in c10s works with tos values. This should be fine though as tos and dscp values are the same storage (u8) underneath. commit 37653a0 Author: Menglong Dong <menglong8.dong@gmail.com> Date: Thu Nov 7 20:55:53 2024 +0800 net: ip: make fib_validate_source() support drop reasons In this commit, we make fib_validate_source() and __fib_validate_source() return -reason instead of errno on error. The return value of fib_validate_source can be -errno, 0, and 1. It's hard to make fib_validate_source() return drop reasons directly. The fib_validate_source() will return 1 if the scope of the source(revert) route is HOST. And the __mkroute_input() will mark the skb with IPSKB_DOREDIRECT in this case (combine with some other conditions). And then, a REDIRECT ICMP will be sent in ip_forward() if this flag exists. We can't pass this information to __mkroute_input if we make fib_validate_source() return drop reasons. Therefore, we introduce the wrapper fib_validate_source_reason() for fib_validate_source(), which will return the drop reasons on error. In the origin logic, LINUX_MIB_IPRPFILTER will be counted if fib_validate_source() return -EXDEV. And now, we need to adjust it by checking "reason == SKB_DROP_REASON_IP_RPFILTER". However, this will take effect only after the patch "net: ip: make ip_route_input_noref() return drop reasons", as we can't pass the drop reasons from fib_validate_source() to ip_rcv_finish_core() in this patch. Following new drop reasons are added in this patch: SKB_DROP_REASON_IP_LOCAL_SOURCE SKB_DROP_REASON_IP_INVALID_SOURCE Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn> Signed-off-by: Paolo Abeni <pabeni@redhat.com> Signed-off-by: Antoine Tenart <atenart@redhat.com>
1 parent 82f03bd commit 8b34060

File tree

5 files changed

+55
-21
lines changed

5 files changed

+55
-21
lines changed

include/net/dropreason-core.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
FN(INVALID_PROTO) \
7777
FN(IP_INADDRERRORS) \
7878
FN(IP_INNOROUTES) \
79+
FN(IP_LOCAL_SOURCE) \
80+
FN(IP_INVALID_SOURCE) \
7981
FN(PKT_TOO_BIG) \
8082
FN(DUP_FRAG) \
8183
FN(FRAG_REASM_TIMEOUT) \
@@ -373,6 +375,14 @@ enum skb_drop_reason {
373375
* IPSTATS_MIB_INADDRERRORS
374376
*/
375377
SKB_DROP_REASON_IP_INNOROUTES,
378+
/** @SKB_DROP_REASON_IP_LOCAL_SOURCE: the source ip is local */
379+
SKB_DROP_REASON_IP_LOCAL_SOURCE,
380+
/**
381+
* @SKB_DROP_REASON_IP_INVALID_SOURCE: the source ip is invalid:
382+
* 1) source ip is multicast or limited broadcast
383+
* 2) source ip is zero and not IGMP
384+
*/
385+
SKB_DROP_REASON_IP_INVALID_SOURCE,
376386
/**
377387
* @SKB_DROP_REASON_PKT_TOO_BIG: packet size is too big (maybe exceed the
378388
* MTU)

include/net/ip_fib.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,19 @@ bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev);
451451
int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
452452
u8 tos, int oif, struct net_device *dev,
453453
struct in_device *idev, u32 *itag);
454+
455+
static inline enum skb_drop_reason
456+
fib_validate_source_reason(struct sk_buff *skb, __be32 src, __be32 dst,
457+
dscp_t dscp, int oif, struct net_device *dev,
458+
struct in_device *idev, u32 *itag)
459+
{
460+
int err = fib_validate_source(skb, src, dst, dscp, oif, dev, idev,
461+
itag);
462+
if (err < 0)
463+
return -err;
464+
return SKB_NOT_DROPPED_YET;
465+
}
466+
454467
#ifdef CONFIG_IP_ROUTE_CLASSID
455468
static inline int fib_num_tclassid_users(struct net *net)
456469
{

net/ipv4/fib_frontend.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
346346
int rpf, struct in_device *idev, u32 *itag)
347347
{
348348
struct net *net = dev_net(dev);
349+
enum skb_drop_reason reason;
349350
struct flow_keys flkeys;
350351
int ret, no_addr;
351352
struct fib_result res;
@@ -377,9 +378,15 @@ static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
377378

378379
if (fib_lookup(net, &fl4, &res, 0))
379380
goto last_resort;
380-
if (res.type != RTN_UNICAST &&
381-
(res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev)))
382-
goto e_inval;
381+
if (res.type != RTN_UNICAST) {
382+
if (res.type != RTN_LOCAL) {
383+
reason = SKB_DROP_REASON_IP_INVALID_SOURCE;
384+
goto e_inval;
385+
} else if (!IN_DEV_ACCEPT_LOCAL(idev)) {
386+
reason = SKB_DROP_REASON_IP_LOCAL_SOURCE;
387+
goto e_inval;
388+
}
389+
}
383390
fib_combine_itag(itag, &res);
384391

385392
dev_match = fib_info_nh_uses_dev(res.fi, dev);
@@ -412,9 +419,9 @@ static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
412419
return 0;
413420

414421
e_inval:
415-
return -EINVAL;
422+
return -reason;
416423
e_rpf:
417-
return -EXDEV;
424+
return -SKB_DROP_REASON_IP_RPFILTER;
418425
}
419426

420427
/* Ignore rp_filter for packets protected by IPsec. */

net/ipv4/ip_input.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,8 @@ static int ip_rcv_finish_core(struct net *net, struct sock *sk,
425425
return NET_RX_DROP;
426426

427427
drop_error:
428-
if (err == -EXDEV) {
429-
drop_reason = SKB_DROP_REASON_IP_RPFILTER;
428+
if (drop_reason == SKB_DROP_REASON_IP_RPFILTER)
430429
__NET_INC_STATS(net, LINUX_MIB_IPRPFILTER);
431-
}
432430
goto drop;
433431
}
434432

net/ipv4/route.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
16801680
u8 tos, struct net_device *dev,
16811681
struct in_device *in_dev, u32 *itag)
16821682
{
1683-
int err;
1683+
enum skb_drop_reason reason;
16841684

16851685
/* Primary sanity checks. */
16861686
if (!in_dev)
@@ -1698,10 +1698,10 @@ int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
16981698
ip_hdr(skb)->protocol != IPPROTO_IGMP)
16991699
return -EINVAL;
17001700
} else {
1701-
err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
1702-
in_dev, itag);
1703-
if (err < 0)
1704-
return err;
1701+
reason = fib_validate_source_reason(skb, saddr, 0, tos, 0,
1702+
dev, in_dev, itag);
1703+
if (reason)
1704+
return -EINVAL;
17051705
}
17061706
return 0;
17071707
}
@@ -1799,6 +1799,7 @@ static int __mkroute_input(struct sk_buff *skb,
17991799
err = fib_validate_source(skb, saddr, daddr, tos, FIB_RES_OIF(*res),
18001800
in_dev->dev, in_dev, &itag);
18011801
if (err < 0) {
1802+
err = -EINVAL;
18021803
ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
18031804
saddr);
18041805

@@ -2153,6 +2154,7 @@ int ip_route_use_hint(struct sk_buff *skb, __be32 daddr, __be32 saddr,
21532154
struct in_device *in_dev = __in_dev_get_rcu(dev);
21542155
struct rtable *rt = skb_rtable(hint);
21552156
struct net *net = dev_net(dev);
2157+
enum skb_drop_reason reason;
21562158
int err = -EINVAL;
21572159
u32 tag = 0;
21582160

@@ -2172,8 +2174,9 @@ int ip_route_use_hint(struct sk_buff *skb, __be32 daddr, __be32 saddr,
21722174
goto skip_validate_source;
21732175

21742176
tos &= INET_DSCP_MASK;
2175-
err = fib_validate_source(skb, saddr, daddr, tos, 0, dev, in_dev, &tag);
2176-
if (err < 0)
2177+
reason = fib_validate_source_reason(skb, saddr, daddr, tos, 0, dev,
2178+
in_dev, &tag);
2179+
if (reason)
21772180
goto martian_source;
21782181

21792182
skip_validate_source:
@@ -2215,6 +2218,7 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
22152218
u8 tos, struct net_device *dev,
22162219
struct fib_result *res)
22172220
{
2221+
enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
22182222
struct in_device *in_dev = __in_dev_get_rcu(dev);
22192223
struct flow_keys *flkeys = NULL, _flkeys;
22202224
struct net *net = dev_net(dev);
@@ -2309,10 +2313,11 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
23092313
goto brd_input;
23102314
}
23112315

2316+
err = -EINVAL;
23122317
if (res->type == RTN_LOCAL) {
2313-
err = fib_validate_source(skb, saddr, daddr, tos,
2314-
0, dev, in_dev, &itag);
2315-
if (err < 0)
2318+
reason = fib_validate_source_reason(skb, saddr, daddr, tos,
2319+
0, dev, in_dev, &itag);
2320+
if (reason)
23162321
goto martian_source;
23172322
goto local_input;
23182323
}
@@ -2333,9 +2338,10 @@ out: return err;
23332338
goto e_inval;
23342339

23352340
if (!ipv4_is_zeronet(saddr)) {
2336-
err = fib_validate_source(skb, saddr, 0, tos, 0, dev,
2337-
in_dev, &itag);
2338-
if (err < 0)
2341+
err = -EINVAL;
2342+
reason = fib_validate_source_reason(skb, saddr, 0, tos, 0,
2343+
dev, in_dev, &itag);
2344+
if (reason)
23392345
goto martian_source;
23402346
}
23412347
flags |= RTCF_BROADCAST;

0 commit comments

Comments
 (0)