Skip to content

Commit d8d9ef2

Browse files
edumazetkuba-moo
authored andcommitted
ipv4: icmp: icmpv4_xrlim_allow() optimization if net.ipv4.icmp_ratelimit is zero
If net.ipv4.icmp_ratelimit is zero, we do not have to call inet_getpeer_v4() and inet_peer_xrlim_allow(). Both can be very expensive under DDOS. Signed-off-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com> Link: https://patch.msgid.link/20260216142832.3834174-5-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 0201eed commit d8d9ef2

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

net/ipv4/icmp.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,23 +316,29 @@ static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
316316
struct dst_entry *dst = &rt->dst;
317317
struct inet_peer *peer;
318318
struct net_device *dev;
319+
int peer_timeout;
319320
bool rc = true;
320321

321322
if (!apply_ratelimit)
322323
return true;
323324

325+
peer_timeout = READ_ONCE(net->ipv4.sysctl_icmp_ratelimit);
326+
if (!peer_timeout)
327+
goto out;
328+
324329
/* No rate limit on loopback */
325330
rcu_read_lock();
326331
dev = dst_dev_rcu(dst);
327332
if (dev && (dev->flags & IFF_LOOPBACK))
328-
goto out;
333+
goto out_unlock;
329334

330335
peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr,
331336
l3mdev_master_ifindex_rcu(dev));
332-
rc = inet_peer_xrlim_allow(peer,
333-
READ_ONCE(net->ipv4.sysctl_icmp_ratelimit));
334-
out:
337+
rc = inet_peer_xrlim_allow(peer, peer_timeout);
338+
339+
out_unlock:
335340
rcu_read_unlock();
341+
out:
336342
if (!rc)
337343
__ICMP_INC_STATS(net, ICMP_MIB_RATELIMITHOST);
338344
else

0 commit comments

Comments
 (0)