utils.c (59458f40e25915a355d8b1d701425fe9f4f9ea23) utils.c (aaa248f6c9c81b2683db7dbb0689cd5ed1c86d88)
1/*
2 * Generic address resultion entity
3 *
4 * Authors:
5 * net_random Alan Cox
6 * net_ratelimit Andi Kleen
7 * in{4,6}_pton YOSHIFUJI Hideaki, Copyright (C)2006 USAGI/WIDE Project
8 *

--- 16 unchanged lines hidden (view full) ---

25#include <linux/random.h>
26#include <linux/percpu.h>
27#include <linux/init.h>
28
29#include <asm/byteorder.h>
30#include <asm/system.h>
31#include <asm/uaccess.h>
32
1/*
2 * Generic address resultion entity
3 *
4 * Authors:
5 * net_random Alan Cox
6 * net_ratelimit Andi Kleen
7 * in{4,6}_pton YOSHIFUJI Hideaki, Copyright (C)2006 USAGI/WIDE Project
8 *

--- 16 unchanged lines hidden (view full) ---

25#include <linux/random.h>
26#include <linux/percpu.h>
27#include <linux/init.h>
28
29#include <asm/byteorder.h>
30#include <asm/system.h>
31#include <asm/uaccess.h>
32
33/*
34 This is a maximally equidistributed combined Tausworthe generator
35 based on code from GNU Scientific Library 1.5 (30 Jun 2004)
36
37 x_n = (s1_n ^ s2_n ^ s3_n)
38
39 s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
40 s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
41 s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
42
43 The period of this generator is about 2^88.
44
45 From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
46 Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
47
48 This is available on the net from L'Ecuyer's home page,
49
50 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
51 ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
52
53 There is an erratum in the paper "Tables of Maximally
54 Equidistributed Combined LFSR Generators", Mathematics of
55 Computation, 68, 225 (1999), 261--269:
56 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
57
58 ... the k_j most significant bits of z_j must be non-
59 zero, for each j. (Note: this restriction also applies to the
60 computer code given in [4], but was mistakenly not mentioned in
61 that paper.)
62
63 This affects the seeding procedure by imposing the requirement
64 s1 > 1, s2 > 7, s3 > 15.
65
66*/
67struct nrnd_state {
68 u32 s1, s2, s3;
69};
70
71static DEFINE_PER_CPU(struct nrnd_state, net_rand_state);
72
73static u32 __net_random(struct nrnd_state *state)
74{
75#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
76
77 state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
78 state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
79 state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
80
81 return (state->s1 ^ state->s2 ^ state->s3);
82}
83
84static void __net_srandom(struct nrnd_state *state, unsigned long s)
85{
86 if (s == 0)
87 s = 1; /* default seed is 1 */
88
89#define LCG(n) (69069 * n)
90 state->s1 = LCG(s);
91 state->s2 = LCG(state->s1);
92 state->s3 = LCG(state->s2);
93
94 /* "warm it up" */
95 __net_random(state);
96 __net_random(state);
97 __net_random(state);
98 __net_random(state);
99 __net_random(state);
100 __net_random(state);
101}
102
103
104unsigned long net_random(void)
105{
106 unsigned long r;
107 struct nrnd_state *state = &get_cpu_var(net_rand_state);
108 r = __net_random(state);
109 put_cpu_var(state);
110 return r;
111}
112
113
114void net_srandom(unsigned long entropy)
115{
116 struct nrnd_state *state = &get_cpu_var(net_rand_state);
117 __net_srandom(state, state->s1^entropy);
118 put_cpu_var(state);
119}
120
121void __init net_random_init(void)
122{
123 int i;
124
125 for_each_possible_cpu(i) {
126 struct nrnd_state *state = &per_cpu(net_rand_state,i);
127 __net_srandom(state, i+jiffies);
128 }
129}
130
131static int net_random_reseed(void)
132{
133 int i;
134 unsigned long seed;
135
136 for_each_possible_cpu(i) {
137 struct nrnd_state *state = &per_cpu(net_rand_state,i);
138
139 get_random_bytes(&seed, sizeof(seed));
140 __net_srandom(state, seed);
141 }
142 return 0;
143}
144late_initcall(net_random_reseed);
145
146int net_msg_cost = 5*HZ;
147int net_msg_burst = 10;
148
149/*
150 * All net warning printk()s should be guarded by this function.
151 */
152int net_ratelimit(void)
153{
154 return __printk_ratelimit(net_msg_cost, net_msg_burst);
155}
33int net_msg_cost = 5*HZ;
34int net_msg_burst = 10;
35
36/*
37 * All net warning printk()s should be guarded by this function.
38 */
39int net_ratelimit(void)
40{
41 return __printk_ratelimit(net_msg_cost, net_msg_burst);
42}
156
157EXPORT_SYMBOL(net_random);
158EXPORT_SYMBOL(net_ratelimit);
43EXPORT_SYMBOL(net_ratelimit);
159EXPORT_SYMBOL(net_srandom);
160
161/*
162 * Convert an ASCII string to binary IP.
163 * This is outside of net/ipv4/ because various code that uses IP addresses
164 * is otherwise not dependent on the TCP/IP stack.
165 */
166
167__be32 in_aton(const char *str)

--- 239 unchanged lines hidden ---
44
45/*
46 * Convert an ASCII string to binary IP.
47 * This is outside of net/ipv4/ because various code that uses IP addresses
48 * is otherwise not dependent on the TCP/IP stack.
49 */
50
51__be32 in_aton(const char *str)

--- 239 unchanged lines hidden ---