Import 2.3.10pre5
[davej-history.git] / net / core / utils.c
blob415926b8eefa840bf699c85b9dcb673eb760cc7e
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 unsigned long toks = 10*5*HZ;
50 static unsigned long last_msg;
51 static int missed;
52 unsigned long now = jiffies;
54 toks += now - xchg(&last_msg, now);
55 if (toks > net_msg_burst)
56 toks = net_msg_burst;
57 if (toks >= net_msg_cost) {
58 toks -= net_msg_cost;
59 if (missed)
60 printk(KERN_WARNING "NET: %d messages suppressed.\n", missed);
61 missed = 0;
62 return 1;
64 missed++;
65 return 0;