Main Page | Class List | File List | Class Members | File Members

utils.c

Go to the documentation of this file.
00001 /* 00002 * Generic address resultion entity 00003 * 00004 * Authors: 00005 * net_random Alan Cox 00006 * net_ratelimit Andy Kleen 00007 * 00008 * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 00009 * 00010 * This program is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU General Public License 00012 * as published by the Free Software Foundation; either version 00013 * 2 of the License, or (at your option) any later version. 00014 */ 00015 00016 #include <asm/uaccess.h> 00017 #include <asm/system.h> 00018 #include <linux/types.h> 00019 #include <linux/kernel.h> 00020 #include <linux/sched.h> 00021 #include <linux/string.h> 00022 #include <linux/mm.h> 00023 00024 static unsigned long net_rand_seed = 152L; 00025 00026 unsigned long net_random(void) 00027 { 00028 net_rand_seed=net_rand_seed*69069L+1; 00029 return net_rand_seed^jiffies; 00030 } 00031 00032 void net_srandom(unsigned long entropy) 00033 { 00034 net_rand_seed ^= entropy; 00035 net_random(); 00036 } 00037 00038 int net_msg_cost = 5*HZ; 00039 int net_msg_burst = 10*5*HZ; 00040 00041 /* 00042 * This enforces a rate limit: not more than one kernel message 00043 * every 5secs to make a denial-of-service attack impossible. 00044 * 00045 * All warning printk()s should be guarded by this function. 00046 */ 00047 int net_ratelimit(void) 00048 { 00049 static spinlock_t ratelimit_lock = SPIN_LOCK_UNLOCKED; 00050 static unsigned long toks = 10*5*HZ; 00051 static unsigned long last_msg; 00052 static int missed; 00053 unsigned long flags; 00054 unsigned long now = jiffies; 00055 00056 spin_lock_irqsave(&ratelimit_lock, flags); 00057 toks += now - last_msg; 00058 last_msg = now; 00059 if (toks > net_msg_burst) 00060 toks = net_msg_burst; 00061 if (toks >= net_msg_cost) { 00062 int lost = missed; 00063 missed = 0; 00064 toks -= net_msg_cost; 00065 spin_unlock_irqrestore(&ratelimit_lock, flags); 00066 if (lost) 00067 printk(KERN_WARNING "NET: %d messages suppressed.\n", lost); 00068 return 1; 00069 } 00070 missed++; 00071 spin_unlock_irqrestore(&ratelimit_lock, flags); 00072 return 0; 00073 }

Generated on Wed Dec 1 21:25:33 2004 for Linux 2.4.23 Networking by doxygen 1.3.8