2 * Generic address resultion entity
6 * net_ratelimit Andy Kleen
8 * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
24 static unsigned long net_rand_seed
= 152L;
26 unsigned long net_random(void)
28 net_rand_seed
=net_rand_seed
*69069L+1;
29 return net_rand_seed
^jiffies
;
32 void net_srandom(unsigned long entropy
)
34 net_rand_seed
^= entropy
;
38 int net_msg_cost
= 5*HZ
;
39 int net_msg_burst
= 10*5*HZ
;
42 * This enforces a rate limit: not more than one kernel message
43 * every 5secs to make a denial-of-service attack impossible.
45 * All warning printk()s should be guarded by this function.
47 int net_ratelimit(void)
49 static spinlock_t ratelimit_lock
= SPIN_LOCK_UNLOCKED
;
50 static unsigned long toks
= 10*5*HZ
;
51 static unsigned long last_msg
;
54 unsigned long now
= jiffies
;
56 spin_lock_irqsave(&ratelimit_lock
, flags
);
57 toks
+= now
- last_msg
;
59 if (toks
> net_msg_burst
)
61 if (toks
>= net_msg_cost
) {
65 spin_unlock_irqrestore(&ratelimit_lock
, flags
);
67 printk(KERN_WARNING
"NET: %d messages suppressed.\n", lost
);
71 spin_unlock_irqrestore(&ratelimit_lock
, flags
);