Skip to contentSkip to conversation

net_random_init needs to get seed later in boot process

PATCH In review Stephen Hemminger 2 participants 2 messages Aug 30, 2004 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 net_random_init needs to get seed later in boot process

In reply to a message not in the archive

Argh.. random_init isn't called until the device's are initialized. Rather than mess with random.c initialization, how about this; it resets the net_random state late in initialization when get_random_bytes is ready.

net/core/utils.c +15 -7
diff -Nru a/net/core/utils.c b/net/core/utils.c
@@ -21,6 +21,7 @@
 #include <linux/types.h>
 #include <linux/random.h>
 #include <linux/percpu.h>
+#include <linux/init.h>
 
 #include <asm/system.h>
 #include <asm/uaccess.h>
@@ -77,10 +78,8 @@
 	return (state->s1 ^ state->s2 ^ state->s3);
 }
 
-static void __net_srandom(struct nrnd_state *state, unsigned long entropy)
+static void __net_srandom(struct nrnd_state *state, unsigned long s)
 {
-	u32 s = state->s1 ^ entropy;
-
 	if (s == 0)
 		s = 1;      /* default seed is 1 */
 
@@ -112,24 +111,33 @@
 void net_srandom(unsigned long entropy)
 {
 	struct nrnd_state *state = &get_cpu_var(net_rand_state);
-	__net_srandom(state, entropy);
+	__net_srandom(state, state->s1^entropy);
 	put_cpu_var(state);
 }
 
 void __init net_random_init(void)
 {
 	int i;
+
+	for (i = 0; i < NR_CPUS; i++) {
+		struct nrnd_state *state = &per_cpu(net_rand_state,i);
+		__net_srandom(state, i+jiffies);
+	}
+}
+
+static int net_random_reseed(void)
+{
+	int i;
 	unsigned long seed[NR_CPUS];
 
 	get_random_bytes(seed, sizeof(seed));
-
 	for (i = 0; i < NR_CPUS; i++) {
 		struct nrnd_state *state = &per_cpu(net_rand_state,i);
-
-		memset(state, 0, sizeof(*state));
 		__net_srandom(state, seed[i]);
 	}
+	return 0;
 }
+late_initcall(net_random_reseed);
 
 int net_msg_cost = 5*HZ;
 int net_msg_burst = 10;
maintainer
#2 lore raw
Quoted 3 lines · On Mon, 30 Aug 2004 13:20:30 -0700 Stephen Hemminger <[email protected]>…

Argh.. random_init isn't called until the device's are initialized. Rather than mess with random.c initialization, how about this; it resets the net_random state late in initialization when get_random_bytes is ready.

I hate this too. This is the same reason we have to play games in picking the initial hash random seed in the ipv4 routing cache, for example.