diff mbox

[U-Boot,04/28] net: Make the MAC-seeded random number generator available to /net

Message ID 1327020811-1538-5-git-send-email-joe.hershberger@ni.com
State Superseded
Delegated to: Joe Hershberger
Headers show

Commit Message

Joe Hershberger Jan. 20, 2012, 12:53 a.m. UTC
MAC-seeded rand() will be needed by link-local as well, so give it
an interface

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
Cc: Joe Hershberger <joe.hershberger@gmail.com>
Cc: Wolfgang Denk <wd@denx.de>
---
 net/Makefile   |    1 +
 net/bootp.c    |   69 +++++++++++--------------------------------------------
 net/bootp.h    |    3 --
 net/net_rand.c |   68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/net_rand.h |   18 ++++++++++++++
 5 files changed, 101 insertions(+), 58 deletions(-)
 create mode 100644 net/net_rand.c
 create mode 100644 net/net_rand.h

Comments

Simon Glass Jan. 24, 2012, 5:30 a.m. UTC | #1
Hi Joe,

On Thu, Jan 19, 2012 at 4:53 PM, Joe Hershberger <joe.hershberger@ni.com> wrote:
> MAC-seeded rand() will be needed by link-local as well, so give it
> an interface
>
> Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

Acked-by: Simon Glass <sjg@chromium.org>

> Cc: Joe Hershberger <joe.hershberger@gmail.com>
> Cc: Wolfgang Denk <wd@denx.de>
> ---
>  net/Makefile   |    1 +
>  net/bootp.c    |   69 +++++++++++--------------------------------------------
>  net/bootp.h    |    3 --
>  net/net_rand.c |   68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  net/net_rand.h |   18 ++++++++++++++
>  5 files changed, 101 insertions(+), 58 deletions(-)
>  create mode 100644 net/net_rand.c
>  create mode 100644 net/net_rand.h
>
> diff --git a/net/Makefile b/net/Makefile
> index 0544f6b..5901046 100644
> --- a/net/Makefile
> +++ b/net/Makefile
> @@ -31,6 +31,7 @@ COBJS-$(CONFIG_CMD_NET)  += bootp.o
>  COBJS-$(CONFIG_CMD_DNS)  += dns.o
>  COBJS-$(CONFIG_CMD_NET)  += eth.o
>  COBJS-$(CONFIG_CMD_NET)  += net.o
> +COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o
>  COBJS-$(CONFIG_CMD_NFS)  += nfs.o
>  COBJS-$(CONFIG_CMD_RARP) += rarp.o
>  COBJS-$(CONFIG_CMD_SNTP) += sntp.o
> diff --git a/net/bootp.c b/net/bootp.c
> index 07870d0..9824cd1 100644
> --- a/net/bootp.c
> +++ b/net/bootp.c
> @@ -12,6 +12,9 @@
>  #include <command.h>
>  #include <net.h>
>  #include "bootp.h"
> +#ifdef CONFIG_BOOTP_RANDOM_DELAY
> +#include "net_rand.h"
> +#endif
>  #include "tftp.h"
>  #include "nfs.h"
>  #ifdef CONFIG_STATUS_LED
> @@ -37,9 +40,6 @@
>
>  ulong          BootpID;
>  int            BootpTry;
> -#ifdef CONFIG_BOOTP_RANDOM_DELAY
> -ulong          seed1, seed2;
> -#endif
>
>  #if defined(CONFIG_CMD_DHCP)
>  dhcp_state_t dhcp_state = INIT;
> @@ -583,66 +583,25 @@ BootpRequest(void)
>        uchar *pkt, *iphdr;
>        struct Bootp_t *bp;
>        int ext_len, pktlen, iplen;
> +#ifdef CONFIG_BOOTP_RANDOM_DELAY
> +       ulong i, rand_ms;
> +#endif
>
>  #if defined(CONFIG_CMD_DHCP)
>        dhcp_state = INIT;
>  #endif
>
>  #ifdef CONFIG_BOOTP_RANDOM_DELAY               /* Random BOOTP delay */
> -       unsigned char bi_enetaddr[6];
> -       int   reg;
> -       ulong tst1, tst2, sum, m_mask, m_value = 0;
> -
> -       if (BootpTry == 0) {
> -               /* 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;
> +       if (BootpTry == 0)
> +               srand_mac();
>
> -               /* 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;
> -       }
> -
> -       /* Random Number Generator */
> -       for (reg = 0; reg <= 0; reg++) {
> -               sum = seed1 + seed2;
> -               if (sum < seed1 || sum < seed2)
> -                       sum++;
> -               seed2 = seed1;
> -               seed1 = sum;
> -
> -               if (BootpTry <= 2) {    /* Start with max 1024 * 1ms */
> -                       sum = sum >> (22-BootpTry);
> -               } else {        /*After 3rd BOOTP request max 8192 * 1ms */
> -                       sum = sum >> 19;
> -               }
> -       }
> +       if (BootpTry <= 2)      /* Start with max 1024 * 1ms */
> +               rand_ms = rand() >> (22-BootpTry);
> +       else            /* After 3rd BOOTP request max 8192 * 1ms */
> +               rand_ms = rand() >> 19;
>
> -       printf("Random delay: %ld ms...\n", sum);
> -       for (reg = 0; reg < sum; reg++)
> +       printf("Random delay: %ld ms...\n", rand_ms);
> +       for (i = 0; i < rand_ms; i++)
>                udelay(1000); /*Wait 1ms*/
>
>  #endif /* CONFIG_BOOTP_RANDOM_DELAY */
> diff --git a/net/bootp.h b/net/bootp.h
> index ce73734..bf4e875 100644
> --- a/net/bootp.h
> +++ b/net/bootp.h
> @@ -63,9 +63,6 @@ struct Bootp_t {
>  extern ulong   BootpID;                /* ID of cur BOOTP request      */
>  extern char    BootFile[128];          /* Boot file name               */
>  extern int     BootpTry;
> -#ifdef CONFIG_BOOTP_RANDOM_DELAY
> -extern ulong   seed1, seed2;           /* seed for random BOOTP delay  */
> -#endif
>
>
>  /* Send a BOOTP request */
> diff --git a/net/net_rand.c b/net/net_rand.c
> new file mode 100644
> index 0000000..744e2f9
> --- /dev/null
> +++ b/net/net_rand.c
> @@ -0,0 +1,68 @@
> +/*
> + *     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@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
> new file mode 100644
> index 0000000..2c6a6ac
> --- /dev/null
> +++ b/net/net_rand.h
> @@ -0,0 +1,18 @@
> +/*
> + *     Copied from LiMon - BOOTP.
> + *
> + *     Copyright 1994, 1995, 2000 Neil Russell.
> + *     (See License)
> + *     Copyright 2000 Paolo Scaffardi
> + */
> +
> +#ifndef __NET_RAND_H__
> +#define __NET_RAND_H__
> +
> +#define RAND_MAX 0xFFFFFFFF
> +
> +void srand_mac(void);
> +unsigned long rand(void);
> +
> +#endif /* __NET_RAND_H__ */
> +
> --
> 1.6.0.2
>
> _______________________________________________
> U-Boot mailing list
> U-Boot@lists.denx.de
> http://lists.denx.de/mailman/listinfo/u-boot
Mike Frysinger Feb. 3, 2012, 11:51 a.m. UTC | #2
On Thursday 19 January 2012 19:53:07 Joe Hershberger wrote:
> --- a/net/bootp.c
> +++ b/net/bootp.c
>
> +#ifdef CONFIG_BOOTP_RANDOM_DELAY
> +#include "net_rand.h"
> +#endif

let's push this CONFIG_xxx check into net_rand.h so we don't have to check it 
every time we include the file
-mike
Joe Hershberger Feb. 10, 2012, 9:51 p.m. UTC | #3
Hi Mike,

On Fri, Feb 3, 2012 at 5:51 AM, Mike Frysinger <vapier@gentoo.org> wrote:
> On Thursday 19 January 2012 19:53:07 Joe Hershberger wrote:
>> --- a/net/bootp.c
>> +++ b/net/bootp.c
>>
>> +#ifdef CONFIG_BOOTP_RANDOM_DELAY
>> +#include "net_rand.h"
>> +#endif
>
> let's push this CONFIG_xxx check into net_rand.h so we don't have to check it
> every time we include the file

In this case, it doesn't make sense.  The rand is enabled based on
other features that need it... it does not have a config of its own.
As a result, putting this in the header would make it worse.  This
guard is still needed in bootp.c, since rand is only needed when that
subfeature is enabled.  For link-local, it will always be included.

-Joe
Mike Frysinger Feb. 14, 2012, 6:49 a.m. UTC | #4
On Friday 10 February 2012 16:51:00 Joe Hershberger wrote:
> On Fri, Feb 3, 2012 at 5:51 AM, Mike Frysinger wrote:
> > On Thursday 19 January 2012 19:53:07 Joe Hershberger wrote:
> >> --- a/net/bootp.c
> >> +++ b/net/bootp.c
> >> 
> >> +#ifdef CONFIG_BOOTP_RANDOM_DELAY
> >> +#include "net_rand.h"
> >> +#endif
> > 
> > let's push this CONFIG_xxx check into net_rand.h so we don't have to
> > check it every time we include the file
> 
> In this case, it doesn't make sense.  The rand is enabled based on
> other features that need it... it does not have a config of its own.
> As a result, putting this in the header would make it worse.  This
> guard is still needed in bootp.c, since rand is only needed when that
> subfeature is enabled.  For link-local, it will always be included.

i'm not sure guarding the include really gains us anything.  i'm inclined to 
just always include it and be done.
-mike
diff mbox

Patch

diff --git a/net/Makefile b/net/Makefile
index 0544f6b..5901046 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -31,6 +31,7 @@  COBJS-$(CONFIG_CMD_NET)  += bootp.o
 COBJS-$(CONFIG_CMD_DNS)  += dns.o
 COBJS-$(CONFIG_CMD_NET)  += eth.o
 COBJS-$(CONFIG_CMD_NET)  += net.o
+COBJS-$(CONFIG_BOOTP_RANDOM_DELAY) += net_rand.o
 COBJS-$(CONFIG_CMD_NFS)  += nfs.o
 COBJS-$(CONFIG_CMD_RARP) += rarp.o
 COBJS-$(CONFIG_CMD_SNTP) += sntp.o
diff --git a/net/bootp.c b/net/bootp.c
index 07870d0..9824cd1 100644
--- a/net/bootp.c
+++ b/net/bootp.c
@@ -12,6 +12,9 @@ 
 #include <command.h>
 #include <net.h>
 #include "bootp.h"
+#ifdef CONFIG_BOOTP_RANDOM_DELAY
+#include "net_rand.h"
+#endif
 #include "tftp.h"
 #include "nfs.h"
 #ifdef CONFIG_STATUS_LED
@@ -37,9 +40,6 @@ 
 
 ulong		BootpID;
 int		BootpTry;
-#ifdef CONFIG_BOOTP_RANDOM_DELAY
-ulong		seed1, seed2;
-#endif
 
 #if defined(CONFIG_CMD_DHCP)
 dhcp_state_t dhcp_state = INIT;
@@ -583,66 +583,25 @@  BootpRequest(void)
 	uchar *pkt, *iphdr;
 	struct Bootp_t *bp;
 	int ext_len, pktlen, iplen;
+#ifdef CONFIG_BOOTP_RANDOM_DELAY
+	ulong i, rand_ms;
+#endif
 
 #if defined(CONFIG_CMD_DHCP)
 	dhcp_state = INIT;
 #endif
 
 #ifdef CONFIG_BOOTP_RANDOM_DELAY		/* Random BOOTP delay */
-	unsigned char bi_enetaddr[6];
-	int   reg;
-	ulong tst1, tst2, sum, m_mask, m_value = 0;
-
-	if (BootpTry == 0) {
-		/* 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;
+	if (BootpTry == 0)
+		srand_mac();
 
-		/* 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;
-	}
-
-	/* Random Number Generator */
-	for (reg = 0; reg <= 0; reg++) {
-		sum = seed1 + seed2;
-		if (sum < seed1 || sum < seed2)
-			sum++;
-		seed2 = seed1;
-		seed1 = sum;
-
-		if (BootpTry <= 2) {	/* Start with max 1024 * 1ms */
-			sum = sum >> (22-BootpTry);
-		} else {	/*After 3rd BOOTP request max 8192 * 1ms */
-			sum = sum >> 19;
-		}
-	}
+	if (BootpTry <= 2)	/* Start with max 1024 * 1ms */
+		rand_ms = rand() >> (22-BootpTry);
+	else		/* After 3rd BOOTP request max 8192 * 1ms */
+		rand_ms = rand() >> 19;
 
-	printf("Random delay: %ld ms...\n", sum);
-	for (reg = 0; reg < sum; reg++)
+	printf("Random delay: %ld ms...\n", rand_ms);
+	for (i = 0; i < rand_ms; i++)
 		udelay(1000); /*Wait 1ms*/
 
 #endif	/* CONFIG_BOOTP_RANDOM_DELAY */
diff --git a/net/bootp.h b/net/bootp.h
index ce73734..bf4e875 100644
--- a/net/bootp.h
+++ b/net/bootp.h
@@ -63,9 +63,6 @@  struct Bootp_t {
 extern ulong	BootpID;		/* ID of cur BOOTP request	*/
 extern char	BootFile[128];		/* Boot file name		*/
 extern int	BootpTry;
-#ifdef CONFIG_BOOTP_RANDOM_DELAY
-extern ulong	seed1, seed2;		/* seed for random BOOTP delay	*/
-#endif
 
 
 /* Send a BOOTP request */
diff --git a/net/net_rand.c b/net/net_rand.c
new file mode 100644
index 0000000..744e2f9
--- /dev/null
+++ b/net/net_rand.c
@@ -0,0 +1,68 @@ 
+/*
+ *	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@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
new file mode 100644
index 0000000..2c6a6ac
--- /dev/null
+++ b/net/net_rand.h
@@ -0,0 +1,18 @@ 
+/*
+ *	Copied from LiMon - BOOTP.
+ *
+ *	Copyright 1994, 1995, 2000 Neil Russell.
+ *	(See License)
+ *	Copyright 2000 Paolo Scaffardi
+ */
+
+#ifndef __NET_RAND_H__
+#define __NET_RAND_H__
+
+#define RAND_MAX 0xFFFFFFFF
+
+void srand_mac(void);
+unsigned long rand(void);
+
+#endif /* __NET_RAND_H__ */
+