From: Michael Walle <michael@walle.cc>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v8 2/4] net: use common rand()/srand() functions
Date: Fri, 1 Jun 2012 22:39:38 +0200 [thread overview]
Message-ID: <1338583180-422-3-git-send-email-michael@walle.cc> (raw)
In-Reply-To: <1338583180-422-1-git-send-email-michael@walle.cc>
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>
Cc: Joe Hershberger <joe.hershberger@ni.com>
---
include/common.h | 4 ++-
lib/Makefile | 4 ++-
net/Makefile | 2 -
net/link_local.c | 7 +++--
net/net_rand.c | 68 ------------------------------------------------------
net/net_rand.h | 31 +++++++++++++++++++-----
6 files changed, 34 insertions(+), 82 deletions(-)
delete mode 100644 net/net_rand.c
diff --git a/include/common.h b/include/common.h
index 6ba27bc..c42f93b 100644
--- a/include/common.h
+++ b/include/common.h
@@ -752,7 +752,9 @@ char * strmhz(char *buf, unsigned long hz);
#include <u-boot/crc.h>
/* lib/rand.c */
-#ifdef CONFIG_RANDOM_MACADDR
+#if defined(CONFIG_RANDOM_MACADDR) || \
+ defined(CONFIG_BOOTP_RANDOM_DELAY) || \
+ defined(CONFIG_CMD_LINK_LOCAL)
#define RAND_MAX -1U
void srand(unsigned int seed);
unsigned int rand(void);
diff --git a/lib/Makefile b/lib/Makefile
index 0ca45fc..556601c 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -67,8 +67,10 @@ COBJS-y += time.o
COBJS-$(CONFIG_BOOTP_PXE) += uuid.o
COBJS-y += vsprintf.o
COBJS-$(CONFIG_RANDOM_MACADDR) += rand.o
+COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += rand.o
+COBJS-$(CONFIG_CMD_LINK_LOCAL) += rand.o
-COBJS := $(COBJS-y)
+COBJS := $(sort $(COBJS-y))
SRCS := $(COBJS:.o=.c)
OBJS := $(addprefix $(obj),$(COBJS))
diff --git a/net/Makefile b/net/Makefile
index 5264687..e7764ce 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -34,8 +34,6 @@ COBJS-$(CONFIG_CMD_DNS) += dns.o
COBJS-$(CONFIG_CMD_NET) += eth.o
COBJS-$(CONFIG_CMD_LINK_LOCAL) += link_local.o
COBJS-$(CONFIG_CMD_NET) += net.o
-COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o
-COBJS-$(CONFIG_CMD_LINK_LOCAL) += net_rand.o
COBJS-$(CONFIG_CMD_NFS) += nfs.o
COBJS-$(CONFIG_CMD_PING) += ping.o
COBJS-$(CONFIG_CMD_RARP) += rarp.o
diff --git a/net/link_local.c b/net/link_local.c
index 3362863..582d011 100644
--- a/net/link_local.c
+++ b/net/link_local.c
@@ -56,6 +56,7 @@ static unsigned conflicts;
static unsigned nprobes;
static unsigned nclaims;
static int ready;
+static unsigned int seed;
static void link_local_timeout(void);
@@ -68,7 +69,7 @@ static IPaddr_t pick(void)
unsigned tmp;
do {
- tmp = rand() & IN_CLASSB_HOST;
+ tmp = rand_r(&seed) & IN_CLASSB_HOST;
} while (tmp > (IN_CLASSB_HOST - 0x0200));
return (IPaddr_t) htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
}
@@ -78,7 +79,7 @@ static IPaddr_t pick(void)
*/
static inline unsigned random_delay_ms(unsigned secs)
{
- return rand() % (secs * 1000);
+ return rand_r(&seed) % (secs * 1000);
}
static void configure_wait(void)
@@ -109,7 +110,7 @@ void link_local_start(void)
}
NetOurSubnetMask = IN_CLASSB_NET;
- srand_mac();
+ seed = seed_mac();
if (ip == 0)
ip = pick();
diff --git a/net/net_rand.c b/net/net_rand.c
deleted file mode 100644
index 5387aba..0000000
--- a/net/net_rand.c
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Based on LiMon - BOOTP.
- *
- * Copyright 1994, 1995, 2000 Neil Russell.
- * (See License)
- * Copyright 2000 Roland Borde
- * Copyright 2000 Paolo Scaffardi
- * Copyright 2000-2004 Wolfgang Denk, wd at denx.de
- */
-
-#include <common.h>
-#include <net.h>
-#include "net_rand.h"
-
-static ulong seed1, seed2;
-
-void srand_mac(void)
-{
- ulong tst1, tst2, m_mask;
- ulong m_value = 0;
- int reg;
- unsigned char bi_enetaddr[6];
-
- /* get our mac */
- eth_getenv_enetaddr("ethaddr", bi_enetaddr);
-
- debug("BootpRequest => Our Mac: ");
- for (reg = 0; reg < 6; reg++)
- debug("%x%c", bi_enetaddr[reg], reg == 5 ? '\n' : ':');
-
- /* Mac-Manipulation 2 get seed1 */
- tst1 = 0;
- tst2 = 0;
- for (reg = 2; reg < 6; reg++) {
- tst1 = tst1 << 8;
- tst1 = tst1 | bi_enetaddr[reg];
- }
- for (reg = 0; reg < 2; reg++) {
- tst2 = tst2 | bi_enetaddr[reg];
- tst2 = tst2 << 8;
- }
-
- seed1 = tst1^tst2;
-
- /* Mirror seed1*/
- m_mask = 0x1;
- for (reg = 1; reg <= 32; reg++) {
- m_value |= (m_mask & seed1);
- seed1 = seed1 >> 1;
- m_value = m_value << 1;
- }
- seed1 = m_value;
- seed2 = 0xb78d0945;
-}
-
-unsigned long rand(void)
-{
- ulong sum;
-
- /* Random Number Generator */
- sum = seed1 + seed2;
- if (sum < seed1 || sum < seed2)
- sum++;
- seed2 = seed1;
- seed1 = sum;
-
- return sum;
-}
diff --git a/net/net_rand.h b/net/net_rand.h
index c98db64..ba9d064 100644
--- a/net/net_rand.h
+++ b/net/net_rand.h
@@ -9,18 +9,35 @@
#ifndef __NET_RAND_H__
#define __NET_RAND_H__
-#define RAND_MAX 0xffffffff
+#include <common.h>
/*
- * Seed the random number generator using the eth0 MAC address
+ * Return a seed for the PRNG derived from the eth0 MAC address.
*/
-void srand_mac(void);
+static inline unsigned int seed_mac(void)
+{
+ unsigned char enetaddr[6];
+ unsigned int seed;
+
+ /* get our mac */
+ eth_getenv_enetaddr("ethaddr", enetaddr);
+
+ seed = enetaddr[5];
+ seed ^= enetaddr[4] << 8;
+ seed ^= enetaddr[3] << 16;
+ seed ^= enetaddr[2] << 24;
+ seed ^= enetaddr[1];
+ seed ^= enetaddr[0] << 8;
+
+ return seed;
+}
/*
- * Get a random number (after seeding with MAC address)
- *
- * @return random number
+ * Seed the random number generator using the eth0 MAC address.
*/
-unsigned long rand(void);
+static inline void srand_mac(void)
+{
+ srand(seed_mac());
+}
#endif /* __NET_RAND_H__ */
--
1.7.2.5
next prev parent reply other threads:[~2012-06-01 20:39 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-01 20:39 [U-Boot] [PATCH v8 0/4] Kirkwood: add lschlv2 and lsxhl board support Michael Walle
2012-06-01 20:39 ` [U-Boot] [PATCH v8 1/4] lib: add rand() function Michael Walle
2012-06-01 20:39 ` Michael Walle [this message]
2012-06-04 20:10 ` [U-Boot] [PATCH v8 2/4] net: use common rand()/srand() functions Joe Hershberger
2012-07-05 18:18 ` Kim Phillips
2012-07-05 21:37 ` Michael Walle
2012-07-05 23:19 ` [U-Boot] [PATCH] net: make net_rand.h inclusion depend on BOOTP_RANDOM_DELAY Kim Phillips
2012-07-06 7:00 ` Michael Walle
2012-07-08 20:40 ` Wolfgang Denk
2012-06-01 20:39 ` [U-Boot] [PATCH v8 3/4] net: add helper to generate random mac address Michael Walle
2012-06-01 20:39 ` [U-Boot] [PATCH v8 4/4] Kirkwood: add lschlv2 and lsxhl board support Michael Walle
2012-06-05 11:09 ` [U-Boot] [PATCH v8 0/4] " Michael Walle
2012-06-05 14:10 ` Prafulla Wadaskar
2012-06-05 14:31 ` Joe Hershberger
2012-06-05 14:26 ` Prafulla Wadaskar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1338583180-422-3-git-send-email-michael@walle.cc \
--to=michael@walle.cc \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox