@@ -209,27 +209,25 @@ static struct sock *icmp_sk(struct net *net)
209
209
return * this_cpu_ptr (net -> ipv4 .icmp_sk );
210
210
}
211
211
212
+ /* Called with BH disabled */
212
213
static inline struct sock * icmp_xmit_lock (struct net * net )
213
214
{
214
215
struct sock * sk ;
215
216
216
- local_bh_disable ();
217
-
218
217
sk = icmp_sk (net );
219
218
220
219
if (unlikely (!spin_trylock (& sk -> sk_lock .slock ))) {
221
220
/* This can happen if the output path signals a
222
221
* dst_link_failure() for an outgoing ICMP packet.
223
222
*/
224
- local_bh_enable ();
225
223
return NULL ;
226
224
}
227
225
return sk ;
228
226
}
229
227
230
228
static inline void icmp_xmit_unlock (struct sock * sk )
231
229
{
232
- spin_unlock_bh (& sk -> sk_lock .slock );
230
+ spin_unlock (& sk -> sk_lock .slock );
233
231
}
234
232
235
233
int sysctl_icmp_msgs_per_sec __read_mostly = 1000 ;
@@ -282,6 +280,33 @@ bool icmp_global_allow(void)
282
280
}
283
281
EXPORT_SYMBOL (icmp_global_allow );
284
282
283
+ static bool icmpv4_mask_allow (struct net * net , int type , int code )
284
+ {
285
+ if (type > NR_ICMP_TYPES )
286
+ return true;
287
+
288
+ /* Don't limit PMTU discovery. */
289
+ if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED )
290
+ return true;
291
+
292
+ /* Limit if icmp type is enabled in ratemask. */
293
+ if (!((1 << type ) & net -> ipv4 .sysctl_icmp_ratemask ))
294
+ return true;
295
+
296
+ return false;
297
+ }
298
+
299
+ static bool icmpv4_global_allow (struct net * net , int type , int code )
300
+ {
301
+ if (icmpv4_mask_allow (net , type , code ))
302
+ return true;
303
+
304
+ if (icmp_global_allow ())
305
+ return true;
306
+
307
+ return false;
308
+ }
309
+
285
310
/*
286
311
* Send an ICMP frame.
287
312
*/
@@ -290,34 +315,22 @@ static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
290
315
struct flowi4 * fl4 , int type , int code )
291
316
{
292
317
struct dst_entry * dst = & rt -> dst ;
318
+ struct inet_peer * peer ;
293
319
bool rc = true;
320
+ int vif ;
294
321
295
- if (type > NR_ICMP_TYPES )
296
- goto out ;
297
-
298
- /* Don't limit PMTU discovery. */
299
- if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED )
322
+ if (icmpv4_mask_allow (net , type , code ))
300
323
goto out ;
301
324
302
325
/* No rate limit on loopback */
303
326
if (dst -> dev && (dst -> dev -> flags & IFF_LOOPBACK ))
304
327
goto out ;
305
328
306
- /* Limit if icmp type is enabled in ratemask. */
307
- if (!((1 << type ) & net -> ipv4 .sysctl_icmp_ratemask ))
308
- goto out ;
309
-
310
- rc = false;
311
- if (icmp_global_allow ()) {
312
- int vif = l3mdev_master_ifindex (dst -> dev );
313
- struct inet_peer * peer ;
314
-
315
- peer = inet_getpeer_v4 (net -> ipv4 .peers , fl4 -> daddr , vif , 1 );
316
- rc = inet_peer_xrlim_allow (peer ,
317
- net -> ipv4 .sysctl_icmp_ratelimit );
318
- if (peer )
319
- inet_putpeer (peer );
320
- }
329
+ vif = l3mdev_master_ifindex (dst -> dev );
330
+ peer = inet_getpeer_v4 (net -> ipv4 .peers , fl4 -> daddr , vif , 1 );
331
+ rc = inet_peer_xrlim_allow (peer , net -> ipv4 .sysctl_icmp_ratelimit );
332
+ if (peer )
333
+ inet_putpeer (peer );
321
334
out :
322
335
return rc ;
323
336
}
@@ -396,13 +409,22 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
396
409
struct inet_sock * inet ;
397
410
__be32 daddr , saddr ;
398
411
u32 mark = IP4_REPLY_MARK (net , skb -> mark );
412
+ int type = icmp_param -> data .icmph .type ;
413
+ int code = icmp_param -> data .icmph .code ;
399
414
400
415
if (ip_options_echo (& icmp_param -> replyopts .opt .opt , skb ))
401
416
return ;
402
417
418
+ /* Needed by both icmp_global_allow and icmp_xmit_lock */
419
+ local_bh_disable ();
420
+
421
+ /* global icmp_msgs_per_sec */
422
+ if (!icmpv4_global_allow (net , type , code ))
423
+ goto out_bh_enable ;
424
+
403
425
sk = icmp_xmit_lock (net );
404
426
if (!sk )
405
- return ;
427
+ goto out_bh_enable ;
406
428
inet = inet_sk (sk );
407
429
408
430
icmp_param -> data .icmph .checksum = 0 ;
@@ -433,12 +455,13 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
433
455
rt = ip_route_output_key (net , & fl4 );
434
456
if (IS_ERR (rt ))
435
457
goto out_unlock ;
436
- if (icmpv4_xrlim_allow (net , rt , & fl4 , icmp_param -> data .icmph .type ,
437
- icmp_param -> data .icmph .code ))
458
+ if (icmpv4_xrlim_allow (net , rt , & fl4 , type , code ))
438
459
icmp_push_reply (icmp_param , & fl4 , & ipc , & rt );
439
460
ip_rt_put (rt );
440
461
out_unlock :
441
462
icmp_xmit_unlock (sk );
463
+ out_bh_enable :
464
+ local_bh_enable ();
442
465
}
443
466
444
467
#ifdef CONFIG_IP_ROUTE_MULTIPATH
@@ -571,7 +594,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
571
594
{
572
595
struct iphdr * iph ;
573
596
int room ;
574
- struct icmp_bxm * icmp_param ;
597
+ struct icmp_bxm icmp_param ;
575
598
struct rtable * rt = skb_rtable (skb_in );
576
599
struct ipcm_cookie ipc ;
577
600
struct flowi4 fl4 ;
@@ -648,13 +671,16 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
648
671
}
649
672
}
650
673
651
- icmp_param = kmalloc (sizeof (* icmp_param ), GFP_ATOMIC );
652
- if (!icmp_param )
653
- return ;
674
+ /* Needed by both icmp_global_allow and icmp_xmit_lock */
675
+ local_bh_disable ();
676
+
677
+ /* Check global sysctl_icmp_msgs_per_sec ratelimit */
678
+ if (!icmpv4_global_allow (net , type , code ))
679
+ goto out_bh_enable ;
654
680
655
681
sk = icmp_xmit_lock (net );
656
682
if (!sk )
657
- goto out_free ;
683
+ goto out_bh_enable ;
658
684
659
685
/*
660
686
* Construct source address and options.
@@ -681,33 +707,34 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
681
707
iph -> tos ;
682
708
mark = IP4_REPLY_MARK (net , skb_in -> mark );
683
709
684
- if (ip_options_echo (& icmp_param -> replyopts .opt .opt , skb_in ))
710
+ if (ip_options_echo (& icmp_param . replyopts .opt .opt , skb_in ))
685
711
goto out_unlock ;
686
712
687
713
688
714
/*
689
715
* Prepare data for ICMP header.
690
716
*/
691
717
692
- icmp_param -> data .icmph .type = type ;
693
- icmp_param -> data .icmph .code = code ;
694
- icmp_param -> data .icmph .un .gateway = info ;
695
- icmp_param -> data .icmph .checksum = 0 ;
696
- icmp_param -> skb = skb_in ;
697
- icmp_param -> offset = skb_network_offset (skb_in );
718
+ icmp_param . data .icmph .type = type ;
719
+ icmp_param . data .icmph .code = code ;
720
+ icmp_param . data .icmph .un .gateway = info ;
721
+ icmp_param . data .icmph .checksum = 0 ;
722
+ icmp_param . skb = skb_in ;
723
+ icmp_param . offset = skb_network_offset (skb_in );
698
724
inet_sk (sk )-> tos = tos ;
699
725
sk -> sk_mark = mark ;
700
726
ipc .addr = iph -> saddr ;
701
- ipc .opt = & icmp_param -> replyopts .opt ;
727
+ ipc .opt = & icmp_param . replyopts .opt ;
702
728
ipc .tx_flags = 0 ;
703
729
ipc .ttl = 0 ;
704
730
ipc .tos = -1 ;
705
731
706
732
rt = icmp_route_lookup (net , & fl4 , skb_in , iph , saddr , tos , mark ,
707
- type , code , icmp_param );
733
+ type , code , & icmp_param );
708
734
if (IS_ERR (rt ))
709
735
goto out_unlock ;
710
736
737
+ /* peer icmp_ratelimit */
711
738
if (!icmpv4_xrlim_allow (net , rt , & fl4 , type , code ))
712
739
goto ende ;
713
740
@@ -716,21 +743,21 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
716
743
room = dst_mtu (& rt -> dst );
717
744
if (room > 576 )
718
745
room = 576 ;
719
- room -= sizeof (struct iphdr ) + icmp_param -> replyopts .opt .opt .optlen ;
746
+ room -= sizeof (struct iphdr ) + icmp_param . replyopts .opt .opt .optlen ;
720
747
room -= sizeof (struct icmphdr );
721
748
722
- icmp_param -> data_len = skb_in -> len - icmp_param -> offset ;
723
- if (icmp_param -> data_len > room )
724
- icmp_param -> data_len = room ;
725
- icmp_param -> head_len = sizeof (struct icmphdr );
749
+ icmp_param . data_len = skb_in -> len - icmp_param . offset ;
750
+ if (icmp_param . data_len > room )
751
+ icmp_param . data_len = room ;
752
+ icmp_param . head_len = sizeof (struct icmphdr );
726
753
727
- icmp_push_reply (icmp_param , & fl4 , & ipc , & rt );
754
+ icmp_push_reply (& icmp_param , & fl4 , & ipc , & rt );
728
755
ende :
729
756
ip_rt_put (rt );
730
757
out_unlock :
731
758
icmp_xmit_unlock (sk );
732
- out_free :
733
- kfree ( icmp_param );
759
+ out_bh_enable :
760
+ local_bh_enable ( );
734
761
out :;
735
762
}
736
763
EXPORT_SYMBOL (icmp_send );
0 commit comments