1 /* 2 * Generic address resultion entity 3 * 4 * Authors: 5 * net_random Alan Cox 6 * net_ratelimit Andy Kleen 7 * 8 * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 9 * 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. 14 */ 15 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> 23 24 static unsigned long net_rand_seed = 152L; 25 net_random(void)26unsigned long net_random(void) 27 { 28 net_rand_seed=net_rand_seed*69069L+1; 29 return net_rand_seed^jiffies; 30 } 31 net_srandom(unsigned long entropy)32void net_srandom(unsigned long entropy) 33 { 34 net_rand_seed ^= entropy; 35 net_random(); 36 } 37 38 int net_msg_cost = 5*HZ; 39 int net_msg_burst = 10*5*HZ; 40 41 /* 42 * This enforces a rate limit: not more than one kernel message 43 * every 5secs to make a denial-of-service attack impossible. 44 * 45 * All warning printk()s should be guarded by this function. 46 */ net_ratelimit(void)47int net_ratelimit(void) 48 { 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; 55 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; 69 } 70 missed++; 71 spin_unlock_irqrestore(&ratelimit_lock, flags); 72 return 0; 73 } 74