net-snk: Seed the pseudo-random number generator

Use the MAC address and the current timebase value to seed the
pseudo-random number generator - this will hopefully give use
enough pseudo-randomness so that two guests that are booting in
parallel won't use the same rand() numbers.

Signed-off-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
This commit is contained in:
Thomas Huth 2015-12-22 11:08:40 +01:00 committed by Alexey Kardashevskiy
parent 48ae29e4c0
commit 2c49891b55
1 changed files with 14 additions and 0 deletions

View File

@ -367,6 +367,18 @@ int dhcp(char *ret_buffer, filename_ip_t * fn_ip, unsigned int retries, int flag
return rc;
}
/**
* Seed the random number generator with our mac and current timestamp
*/
static void seed_rng(uint8_t mac[])
{
unsigned int seed;
asm volatile("mftbl %0" : "=r"(seed));
seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5];
srand(seed);
}
int
netboot(int argc, char *argv[])
{
@ -437,6 +449,8 @@ netboot(int argc, char *argv[])
// init ethernet layer
set_mac_address(own_mac);
seed_rng(own_mac);
if (argc > 6) {
parse_args(argv[6], &obp_tftp_args);
if(obp_tftp_args.bootp_retries - rc < DEFAULT_BOOT_RETRIES)