Skip to content

Commit

Permalink
try read mac of tap0 and fallback to /dev/Xrandom: rootless-container…
Browse files Browse the repository at this point in the history
…s#259

Signed-off-by: fassl <[email protected]>
  • Loading branch information
pfandl committed Sep 11, 2021
1 parent 6430d31 commit 6d6396f
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ static const char *pseudo_random_global_id(const char *device)
unsigned char hash[SHA_DIGEST_LENGTH];
struct ntptimeval ntv;
struct ifreq ifr;
const unsigned char *mac;
unsigned char mac[18];
int sockfd;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
Expand All @@ -246,11 +246,20 @@ static const char *pseudo_random_global_id(const char *device)
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);

if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
perror("cannot get dev hwaddr");
return NULL;
int rand = open("/dev/urandom", O_RDONLY);
if (rand == -1) {
rand = open("/dev/random", O_RDONLY);
}
if (rand == -1) {
perror("cannot get dev hwaddr and cannot open random");
return NULL;
}
read(rand, &mac, sizeof(mac));
close(rand);
}
else {
strncpy(mac, ifr.ifr_ifru.ifru_addr.sa_data, sizeof(mac));
}

mac = (unsigned char*)ifr.ifr_ifru.ifru_addr.sa_data;

/* https://tools.ietf.org/html/rfc4193
*
Expand Down Expand Up @@ -1177,7 +1186,7 @@ static int slirp4netns_config_from_options(struct slirp4netns_config *cfg,
if (cidr == NULL) {
cidr = DEFAULT_CIDR6;
if (opt->ipv6_random) {
cidr = pseudo_random_global_id("lo");
cidr = pseudo_random_global_id("tap0");
if (cidr == NULL) {
fprintf(stderr, "cannot create pseudo random global id\n");
rc = -1;
Expand Down

0 comments on commit 6d6396f

Please sign in to comment.