Commit 99e139d5902e488eb779cd4f00c978f3803c39be
Committed by
Albert ARIBAUD
1 parent
9acf1ca50d
Exists in
master
and in
54 other branches
net: use common rand()/srand() functions
Replace rand() with the functions from lib/. The link-local network code stores its own seed, derived from the MAC address. Thus making it independent from calls to srand() in other modules. Signed-off-by: Michael Walle <michael@walle.cc> Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Showing 6 changed files with 34 additions and 82 deletions Side-by-side Diff
include/common.h
... | ... | @@ -761,7 +761,9 @@ |
761 | 761 | #include <u-boot/crc.h> |
762 | 762 | |
763 | 763 | /* lib/rand.c */ |
764 | -#ifdef CONFIG_RANDOM_MACADDR | |
764 | +#if defined(CONFIG_RANDOM_MACADDR) || \ | |
765 | + defined(CONFIG_BOOTP_RANDOM_DELAY) || \ | |
766 | + defined(CONFIG_CMD_LINK_LOCAL) | |
765 | 767 | #define RAND_MAX -1U |
766 | 768 | void srand(unsigned int seed); |
767 | 769 | unsigned int rand(void); |
lib/Makefile
... | ... | @@ -67,8 +67,10 @@ |
67 | 67 | COBJS-$(CONFIG_BOOTP_PXE) += uuid.o |
68 | 68 | COBJS-y += vsprintf.o |
69 | 69 | COBJS-$(CONFIG_RANDOM_MACADDR) += rand.o |
70 | +COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += rand.o | |
71 | +COBJS-$(CONFIG_CMD_LINK_LOCAL) += rand.o | |
70 | 72 | |
71 | -COBJS := $(COBJS-y) | |
73 | +COBJS := $(sort $(COBJS-y)) | |
72 | 74 | SRCS := $(COBJS:.o=.c) |
73 | 75 | OBJS := $(addprefix $(obj),$(COBJS)) |
74 | 76 |
net/Makefile
... | ... | @@ -34,8 +34,6 @@ |
34 | 34 | COBJS-$(CONFIG_CMD_NET) += eth.o |
35 | 35 | COBJS-$(CONFIG_CMD_LINK_LOCAL) += link_local.o |
36 | 36 | COBJS-$(CONFIG_CMD_NET) += net.o |
37 | -COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o | |
38 | -COBJS-$(CONFIG_CMD_LINK_LOCAL) += net_rand.o | |
39 | 37 | COBJS-$(CONFIG_CMD_NFS) += nfs.o |
40 | 38 | COBJS-$(CONFIG_CMD_PING) += ping.o |
41 | 39 | COBJS-$(CONFIG_CMD_RARP) += rarp.o |
net/link_local.c
... | ... | @@ -56,6 +56,7 @@ |
56 | 56 | static unsigned nprobes; |
57 | 57 | static unsigned nclaims; |
58 | 58 | static int ready; |
59 | +static unsigned int seed; | |
59 | 60 | |
60 | 61 | static void link_local_timeout(void); |
61 | 62 | |
... | ... | @@ -68,7 +69,7 @@ |
68 | 69 | unsigned tmp; |
69 | 70 | |
70 | 71 | do { |
71 | - tmp = rand() & IN_CLASSB_HOST; | |
72 | + tmp = rand_r(&seed) & IN_CLASSB_HOST; | |
72 | 73 | } while (tmp > (IN_CLASSB_HOST - 0x0200)); |
73 | 74 | return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp); |
74 | 75 | } |
... | ... | @@ -78,7 +79,7 @@ |
78 | 79 | */ |
79 | 80 | static inline unsigned random_delay_ms(unsigned secs) |
80 | 81 | { |
81 | - return rand() % (secs * 1000); | |
82 | + return rand_r(&seed) % (secs * 1000); | |
82 | 83 | } |
83 | 84 | |
84 | 85 | static void configure_wait(void) |
... | ... | @@ -109,7 +110,7 @@ |
109 | 110 | } |
110 | 111 | NetOurSubnetMask = IN_CLASSB_NET; |
111 | 112 | |
112 | - srand_mac(); | |
113 | + seed = seed_mac(); | |
113 | 114 | if (ip == 0) |
114 | 115 | ip = pick(); |
115 | 116 |
net/net_rand.c
1 | -/* | |
2 | - * Based on LiMon - BOOTP. | |
3 | - * | |
4 | - * Copyright 1994, 1995, 2000 Neil Russell. | |
5 | - * (See License) | |
6 | - * Copyright 2000 Roland Borde | |
7 | - * Copyright 2000 Paolo Scaffardi | |
8 | - * Copyright 2000-2004 Wolfgang Denk, wd@denx.de | |
9 | - */ | |
10 | - | |
11 | -#include <common.h> | |
12 | -#include <net.h> | |
13 | -#include "net_rand.h" | |
14 | - | |
15 | -static ulong seed1, seed2; | |
16 | - | |
17 | -void srand_mac(void) | |
18 | -{ | |
19 | - ulong tst1, tst2, m_mask; | |
20 | - ulong m_value = 0; | |
21 | - int reg; | |
22 | - unsigned char bi_enetaddr[6]; | |
23 | - | |
24 | - /* get our mac */ | |
25 | - eth_getenv_enetaddr("ethaddr", bi_enetaddr); | |
26 | - | |
27 | - debug("BootpRequest => Our Mac: "); | |
28 | - for (reg = 0; reg < 6; reg++) | |
29 | - debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':'); | |
30 | - | |
31 | - /* Mac-Manipulation 2 get seed1 */ | |
32 | - tst1 = 0; | |
33 | - tst2 = 0; | |
34 | - for (reg = 2; reg < 6; reg++) { | |
35 | - tst1 = tst1 << 8; | |
36 | - tst1 = tst1 | bi_enetaddr[reg]; | |
37 | - } | |
38 | - for (reg = 0; reg < 2; reg++) { | |
39 | - tst2 = tst2 | bi_enetaddr[reg]; | |
40 | - tst2 = tst2 << 8; | |
41 | - } | |
42 | - | |
43 | - seed1 = tst1^tst2; | |
44 | - | |
45 | - /* Mirror seed1*/ | |
46 | - m_mask = 0x1; | |
47 | - for (reg = 1; reg <= 32; reg++) { | |
48 | - m_value |= (m_mask & seed1); | |
49 | - seed1 = seed1 >> 1; | |
50 | - m_value = m_value << 1; | |
51 | - } | |
52 | - seed1 = m_value; | |
53 | - seed2 = 0xb78d0945; | |
54 | -} | |
55 | - | |
56 | -unsigned long rand(void) | |
57 | -{ | |
58 | - ulong sum; | |
59 | - | |
60 | - /* Random Number Generator */ | |
61 | - sum = seed1 + seed2; | |
62 | - if (sum < seed1 || sum < seed2) | |
63 | - sum++; | |
64 | - seed2 = seed1; | |
65 | - seed1 = sum; | |
66 | - | |
67 | - return sum; | |
68 | -} |
net/net_rand.h
... | ... | @@ -9,19 +9,36 @@ |
9 | 9 | #ifndef __NET_RAND_H__ |
10 | 10 | #define __NET_RAND_H__ |
11 | 11 | |
12 | -#define RAND_MAX 0xffffffff | |
12 | +#include <common.h> | |
13 | 13 | |
14 | 14 | /* |
15 | - * Seed the random number generator using the eth0 MAC address | |
15 | + * Return a seed for the PRNG derived from the eth0 MAC address. | |
16 | 16 | */ |
17 | -void srand_mac(void); | |
17 | +static inline unsigned int seed_mac(void) | |
18 | +{ | |
19 | + unsigned char enetaddr[6]; | |
20 | + unsigned int seed; | |
18 | 21 | |
22 | + /* get our mac */ | |
23 | + eth_getenv_enetaddr("ethaddr", enetaddr); | |
24 | + | |
25 | + seed = enetaddr[5]; | |
26 | + seed ^= enetaddr[4] << 8; | |
27 | + seed ^= enetaddr[3] << 16; | |
28 | + seed ^= enetaddr[2] << 24; | |
29 | + seed ^= enetaddr[1]; | |
30 | + seed ^= enetaddr[0] << 8; | |
31 | + | |
32 | + return seed; | |
33 | +} | |
34 | + | |
19 | 35 | /* |
20 | - * Get a random number (after seeding with MAC address) | |
21 | - * | |
22 | - * @return random number | |
36 | + * Seed the random number generator using the eth0 MAC address. | |
23 | 37 | */ |
24 | -unsigned long rand(void); | |
38 | +static inline void srand_mac(void) | |
39 | +{ | |
40 | + srand(seed_mac()); | |
41 | +} | |
25 | 42 | |
26 | 43 | #endif /* __NET_RAND_H__ */ |