diff options
author | Stephen Hemminger <shemminger@osdl.org> | 2006-10-17 03:09:42 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-10-17 11:18:43 -0400 |
commit | aaa248f6c9c81b2683db7dbb0689cd5ed1c86d88 (patch) | |
tree | 106d9ce5ebac4175b0fb97ff80a92e399fcf2a33 | |
parent | 0187f879ee8d4b914e74ffa3cc5df268311fc2d2 (diff) |
[PATCH] rename net_random to random32
Make net_random() more widely available by calling it random32
akpm: hopefully this will permit the removal of carta_random32. That needs
confirmation from Stephane - this code looks somewhat more computationally
expensive, and has a different (ie: callee-stateful) interface.
[akpm@osdl.org: lots of build fixes, cleanups]
Signed-off-by: Stephen Hemminger <shemminger@osdl.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
Cc: Stephane Eranian <eranian@hpl.hp.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r-- | include/linux/net.h | 7 | ||||
-rw-r--r-- | include/linux/random.h | 3 | ||||
-rw-r--r-- | lib/Makefile | 2 | ||||
-rw-r--r-- | lib/random32.c | 142 | ||||
-rw-r--r-- | net/core/dev.c | 2 | ||||
-rw-r--r-- | net/core/utils.c | 116 |
6 files changed, 150 insertions, 122 deletions
diff --git a/include/linux/net.h b/include/linux/net.h index c257f716e00f..15c733b816f0 100644 --- a/include/linux/net.h +++ b/include/linux/net.h | |||
@@ -19,6 +19,7 @@ | |||
19 | #define _LINUX_NET_H | 19 | #define _LINUX_NET_H |
20 | 20 | ||
21 | #include <linux/wait.h> | 21 | #include <linux/wait.h> |
22 | #include <linux/random.h> | ||
22 | #include <asm/socket.h> | 23 | #include <asm/socket.h> |
23 | 24 | ||
24 | struct poll_table_struct; | 25 | struct poll_table_struct; |
@@ -193,9 +194,9 @@ extern int sock_map_fd(struct socket *sock); | |||
193 | extern struct socket *sockfd_lookup(int fd, int *err); | 194 | extern struct socket *sockfd_lookup(int fd, int *err); |
194 | #define sockfd_put(sock) fput(sock->file) | 195 | #define sockfd_put(sock) fput(sock->file) |
195 | extern int net_ratelimit(void); | 196 | extern int net_ratelimit(void); |
196 | extern unsigned long net_random(void); | 197 | |
197 | extern void net_srandom(unsigned long); | 198 | #define net_random() random32() |
198 | extern void net_random_init(void); | 199 | #define net_srandom(seed) srandom32(seed) |
199 | 200 | ||
200 | extern int kernel_sendmsg(struct socket *sock, struct msghdr *msg, | 201 | extern int kernel_sendmsg(struct socket *sock, struct msghdr *msg, |
201 | struct kvec *vec, size_t num, size_t len); | 202 | struct kvec *vec, size_t num, size_t len); |
diff --git a/include/linux/random.h b/include/linux/random.h index 5d6456bcdeba..0248b30e306d 100644 --- a/include/linux/random.h +++ b/include/linux/random.h | |||
@@ -69,6 +69,9 @@ extern struct file_operations random_fops, urandom_fops; | |||
69 | unsigned int get_random_int(void); | 69 | unsigned int get_random_int(void); |
70 | unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len); | 70 | unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len); |
71 | 71 | ||
72 | u32 random32(void); | ||
73 | void srandom32(u32 seed); | ||
74 | |||
72 | #endif /* __KERNEL___ */ | 75 | #endif /* __KERNEL___ */ |
73 | 76 | ||
74 | #endif /* _LINUX_RANDOM_H */ | 77 | #endif /* _LINUX_RANDOM_H */ |
diff --git a/lib/Makefile b/lib/Makefile index 59070dbfbeb4..4b8052f6a7b0 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -12,7 +12,7 @@ lib-$(CONFIG_SMP) += cpumask.o | |||
12 | 12 | ||
13 | lib-y += kobject.o kref.o kobject_uevent.o klist.o | 13 | lib-y += kobject.o kref.o kobject_uevent.o klist.o |
14 | 14 | ||
15 | obj-y += sort.o parser.o halfmd4.o iomap_copy.o debug_locks.o | 15 | obj-y += sort.o parser.o halfmd4.o iomap_copy.o debug_locks.o random32.o |
16 | 16 | ||
17 | ifeq ($(CONFIG_DEBUG_KOBJECT),y) | 17 | ifeq ($(CONFIG_DEBUG_KOBJECT),y) |
18 | CFLAGS_kobject.o += -DDEBUG | 18 | CFLAGS_kobject.o += -DDEBUG |
diff --git a/lib/random32.c b/lib/random32.c new file mode 100644 index 000000000000..4a15ce51cea7 --- /dev/null +++ b/lib/random32.c | |||
@@ -0,0 +1,142 @@ | |||
1 | /* | ||
2 | This is a maximally equidistributed combined Tausworthe generator | ||
3 | based on code from GNU Scientific Library 1.5 (30 Jun 2004) | ||
4 | |||
5 | x_n = (s1_n ^ s2_n ^ s3_n) | ||
6 | |||
7 | s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19)) | ||
8 | s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25)) | ||
9 | s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11)) | ||
10 | |||
11 | The period of this generator is about 2^88. | ||
12 | |||
13 | From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe | ||
14 | Generators", Mathematics of Computation, 65, 213 (1996), 203--213. | ||
15 | |||
16 | This is available on the net from L'Ecuyer's home page, | ||
17 | |||
18 | http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps | ||
19 | ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps | ||
20 | |||
21 | There is an erratum in the paper "Tables of Maximally | ||
22 | Equidistributed Combined LFSR Generators", Mathematics of | ||
23 | Computation, 68, 225 (1999), 261--269: | ||
24 | http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps | ||
25 | |||
26 | ... the k_j most significant bits of z_j must be non- | ||
27 | zero, for each j. (Note: this restriction also applies to the | ||
28 | computer code given in [4], but was mistakenly not mentioned in | ||
29 | that paper.) | ||
30 | |||
31 | This affects the seeding procedure by imposing the requirement | ||
32 | s1 > 1, s2 > 7, s3 > 15. | ||
33 | |||
34 | */ | ||
35 | |||
36 | #include <linux/types.h> | ||
37 | #include <linux/percpu.h> | ||
38 | #include <linux/module.h> | ||
39 | #include <linux/random.h> | ||
40 | |||
41 | struct rnd_state { | ||
42 | u32 s1, s2, s3; | ||
43 | }; | ||
44 | |||
45 | static DEFINE_PER_CPU(struct rnd_state, net_rand_state); | ||
46 | |||
47 | static u32 __random32(struct rnd_state *state) | ||
48 | { | ||
49 | #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b) | ||
50 | |||
51 | state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12); | ||
52 | state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4); | ||
53 | state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17); | ||
54 | |||
55 | return (state->s1 ^ state->s2 ^ state->s3); | ||
56 | } | ||
57 | |||
58 | static void __set_random32(struct rnd_state *state, unsigned long s) | ||
59 | { | ||
60 | if (s == 0) | ||
61 | s = 1; /* default seed is 1 */ | ||
62 | |||
63 | #define LCG(n) (69069 * n) | ||
64 | state->s1 = LCG(s); | ||
65 | state->s2 = LCG(state->s1); | ||
66 | state->s3 = LCG(state->s2); | ||
67 | |||
68 | /* "warm it up" */ | ||
69 | __random32(state); | ||
70 | __random32(state); | ||
71 | __random32(state); | ||
72 | __random32(state); | ||
73 | __random32(state); | ||
74 | __random32(state); | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * random32 - pseudo random number generator | ||
79 | * | ||
80 | * A 32 bit pseudo-random number is generated using a fast | ||
81 | * algorithm suitable for simulation. This algorithm is NOT | ||
82 | * considered safe for cryptographic use. | ||
83 | */ | ||
84 | u32 random32(void) | ||
85 | { | ||
86 | unsigned long r; | ||
87 | struct rnd_state *state = &get_cpu_var(net_rand_state); | ||
88 | r = __random32(state); | ||
89 | put_cpu_var(state); | ||
90 | return r; | ||
91 | } | ||
92 | EXPORT_SYMBOL(random32); | ||
93 | |||
94 | /** | ||
95 | * srandom32 - add entropy to pseudo random number generator | ||
96 | * @seed: seed value | ||
97 | * | ||
98 | * Add some additional seeding to the random32() pool. | ||
99 | * Note: this pool is per cpu so it only affects current CPU. | ||
100 | */ | ||
101 | void srandom32(u32 entropy) | ||
102 | { | ||
103 | struct rnd_state *state = &get_cpu_var(net_rand_state); | ||
104 | __set_random32(state, state->s1 ^ entropy); | ||
105 | put_cpu_var(state); | ||
106 | } | ||
107 | EXPORT_SYMBOL(srandom32); | ||
108 | |||
109 | /* | ||
110 | * Generate some initially weak seeding values to allow | ||
111 | * to start the random32() engine. | ||
112 | */ | ||
113 | static int __init random32_init(void) | ||
114 | { | ||
115 | int i; | ||
116 | |||
117 | for_each_possible_cpu(i) { | ||
118 | struct rnd_state *state = &per_cpu(net_rand_state,i); | ||
119 | __set_random32(state, i + jiffies); | ||
120 | } | ||
121 | return 0; | ||
122 | } | ||
123 | core_initcall(random32_init); | ||
124 | |||
125 | /* | ||
126 | * Generate better values after random number generator | ||
127 | * is fully initalized. | ||
128 | */ | ||
129 | static int __init random32_reseed(void) | ||
130 | { | ||
131 | int i; | ||
132 | unsigned long seed; | ||
133 | |||
134 | for_each_possible_cpu(i) { | ||
135 | struct rnd_state *state = &per_cpu(net_rand_state,i); | ||
136 | |||
137 | get_random_bytes(&seed, sizeof(seed)); | ||
138 | __set_random32(state, seed); | ||
139 | } | ||
140 | return 0; | ||
141 | } | ||
142 | late_initcall(random32_reseed); | ||
diff --git a/net/core/dev.c b/net/core/dev.c index 4d891beab138..81c426adcd1e 100644 --- a/net/core/dev.c +++ b/net/core/dev.c | |||
@@ -3502,8 +3502,6 @@ static int __init net_dev_init(void) | |||
3502 | 3502 | ||
3503 | BUG_ON(!dev_boot_phase); | 3503 | BUG_ON(!dev_boot_phase); |
3504 | 3504 | ||
3505 | net_random_init(); | ||
3506 | |||
3507 | if (dev_proc_init()) | 3505 | if (dev_proc_init()) |
3508 | goto out; | 3506 | goto out; |
3509 | 3507 | ||
diff --git a/net/core/utils.c b/net/core/utils.c index 94c5d761c830..d93fe64f6693 100644 --- a/net/core/utils.c +++ b/net/core/utils.c | |||
@@ -30,119 +30,6 @@ | |||
30 | #include <asm/system.h> | 30 | #include <asm/system.h> |
31 | #include <asm/uaccess.h> | 31 | #include <asm/uaccess.h> |
32 | 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 | */ | ||
67 | struct nrnd_state { | ||
68 | u32 s1, s2, s3; | ||
69 | }; | ||
70 | |||
71 | static DEFINE_PER_CPU(struct nrnd_state, net_rand_state); | ||
72 | |||
73 | static 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 | |||
84 | static 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 | |||
104 | unsigned 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 | |||
114 | void 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 | |||
121 | void __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 | |||
131 | static 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 | } | ||
144 | late_initcall(net_random_reseed); | ||
145 | |||
146 | int net_msg_cost = 5*HZ; | 33 | int net_msg_cost = 5*HZ; |
147 | int net_msg_burst = 10; | 34 | int net_msg_burst = 10; |
148 | 35 | ||
@@ -153,10 +40,7 @@ int net_ratelimit(void) | |||
153 | { | 40 | { |
154 | return __printk_ratelimit(net_msg_cost, net_msg_burst); | 41 | return __printk_ratelimit(net_msg_cost, net_msg_burst); |
155 | } | 42 | } |
156 | |||
157 | EXPORT_SYMBOL(net_random); | ||
158 | EXPORT_SYMBOL(net_ratelimit); | 43 | EXPORT_SYMBOL(net_ratelimit); |
159 | EXPORT_SYMBOL(net_srandom); | ||
160 | 44 | ||
161 | /* | 45 | /* |
162 | * Convert an ASCII string to binary IP. | 46 | * Convert an ASCII string to binary IP. |