* better
[mascara-docs.git] / i386 / linux-2.3.21 / net / core / utils.c
blob3103934531810fb62d8cde8ff52bb4d7e74f0c31
1 /*
2 * Generic address resultion entity
4 * Authors:
5 * net_random Alan Cox
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>
22 #include <linux/mm.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;
35 net_random();
38 int net_msg_cost = 5*HZ;
39 int net_msg_burst = 10*5*HZ;
41 /*
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.
46 */
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;
52 static int missed;
53 unsigned long flags;
54 unsigned long now = jiffies;
56 spin_lock_irqsave(&ratelimit_lock, flags);
57 toks += now - last_msg;
58 last_msg = now;
59 if (toks > net_msg_burst)
60 toks = net_msg_burst;
61 if (toks >= net_msg_cost) {
62 int lost = missed;
63 missed = 0;
64 toks -= net_msg_cost;
65 spin_unlock_irqrestore(&ratelimit_lock, flags);
66 if (lost)
67 printk(KERN_WARNING "NET: %d messages suppressed.\n", lost);
68 return 1;
70 missed++;
71 spin_unlock_irqrestore(&ratelimit_lock, flags);
72 return 0;