From c63a4e673d0c596735cb238f06bd33fb0f62747d Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Wed, 4 Mar 2015 13:06:05 +0100 Subject: [PATCH 149/257] ifconfig: Implement MAC address randomization Usage: ifconfig $nic ether random Inspired by the OpenBSD code which unfortunately can't be imported directly due to code differences. Obtained from: ElectroBSD --- sbin/ifconfig/af_link.c | 8 ++++++++ sbin/ifconfig/ifconfig.8 | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sbin/ifconfig/af_link.c b/sbin/ifconfig/af_link.c index c96cbd2ce92e..c32319b30398 100644 --- a/sbin/ifconfig/af_link.c +++ b/sbin/ifconfig/af_link.c @@ -90,6 +90,14 @@ link_getaddr(const char *addr, int which) if (which != ADDR) errx(1, "can't set link-level netmask or broadcast"); + if (!strcmp(addr, "random")) { + sa->sa_family = AF_LINK; + sa->sa_len = ETHER_ADDR_LEN; + arc4random_buf(&sa->sa_data, sa->sa_len); + /* Make sure it's a non-multicast hardware address */ + sa->sa_data[0] &= 0xfc; + return; + } if ((temp = malloc(strlen(addr) + 2)) == NULL) errx(1, "malloc failed"); temp[0] = ':'; diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8 index e8db561f45a0..05d23d358b01 100644 --- a/sbin/ifconfig/ifconfig.8 +++ b/sbin/ifconfig/ifconfig.8 @@ -141,7 +141,10 @@ parameter below for more information. The link-level .Pq Dq link address -is specified as a series of colon-separated hex digits. +is specified as a series of colon-separated hex digits +or, if the address is +.Dq random , +will be chosen randomly. This can be used to, for example, set a new MAC address on an Ethernet interface, though the mechanism used is not Ethernet specific. -- 2.11.0