drivers: slip: Allow specifying the MAC address from config file

The current state of affairs will not allow to set a MAC address and
thus will initialize a random MAC address.

However, when we are in a situation where we are trying to interface
Zephyr OS QEMU VMs with physical targets for testing, this is not
practical. We need to create a macvtap interface, on which we need to
set the MAC address of the Zephyr OS SLIP device so packets can be
routed.  There is no way to guess which random address is going to be
used, and there is no practical way to query it.

So this patch adds a CONFIG_SLIP_MAC_ADDR config
setting. slip_iface_init() is changed to take the code from
slip_get_mac() for initializing the MAC address (which should be
happening there anyway) and if CONFIG_SLIP_MAC_ADDR is set, it takes
that one -- otherwise goes to generate a random one.

Change-Id: I55a68f2743604f310d41c934783eca82084d7e94
Signed-off-by: Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
This commit is contained in:
Inaky Perez-Gonzalez 2017-03-09 22:32:50 -08:00 committed by Jukka Rissanen
parent fc2542b88b
commit e1a6c7d472
2 changed files with 52 additions and 11 deletions

View file

@ -77,4 +77,15 @@ config SLIP_TAP
By default TUN is used. In TAP the Ethernet frames
are transferred over SLIP.
config SLIP_MAC_ADDR
string "MAC address for the interface"
default ""
help
Specify a MAC address for the SLIP interface in the form of
six hex 8-bit chars separaed by colons (eg:
aa:33:cc:22:e2:c0). The default is an empty string, which
means the code will make 00:00:5E:00:53:XX, where XX will be
random.
endif

View file

@ -387,6 +387,33 @@ static uint8_t *recv_cb(uint8_t *buf, size_t *off)
return buf;
}
static inline int _slip_mac_addr_from_str(struct slip_context *slip,
const char *src)
{
unsigned int len, i;
char *endptr;
len = strlen(src);
for (i = 0; i < len; i++) {
if (!(src[i] >= '0' && src[i] <= '9') &&
!(src[i] >= 'A' && src[i] <= 'F') &&
!(src[i] >= 'a' && src[i] <= 'f') &&
src[i] != ':') {
return -EINVAL;
}
}
memset(slip->mac_addr, 0, sizeof(slip->mac_addr));
for (i = 0; i < sizeof(slip->mac_addr); i++) {
slip->mac_addr[i] = strtol(src, &endptr, 16);
src = ++endptr;
}
return 0;
}
static int slip_init(struct device *dev)
{
struct slip_context *slip = dev->driver_data;
@ -408,17 +435,6 @@ static int slip_init(struct device *dev)
static inline struct net_linkaddr *slip_get_mac(struct slip_context *slip)
{
if (slip->mac_addr[0] == 0x00) {
/* 10-00-00-00-00 to 10-00-00-00-FF Documentation RFC7042 */
slip->mac_addr[0] = 0x10;
slip->mac_addr[1] = 0x00;
slip->mac_addr[2] = 0x00;
slip->mac_addr[3] = 0x00;
slip->mac_addr[4] = 0x00;
slip->mac_addr[5] = sys_rand32_get();
}
slip->ll_addr.addr = slip->mac_addr;
slip->ll_addr.len = sizeof(slip->mac_addr);
@ -432,6 +448,20 @@ static void slip_iface_init(struct net_if *iface)
slip->init_done = true;
if (CONFIG_SLIP_MAC_ADDR[0] != 0) {
if (_slip_mac_addr_from_str(slip, CONFIG_SLIP_MAC_ADDR) < 0) {
goto use_random_mac;
}
} else {
use_random_mac:
/* 00-00-5E-00-53-xx Documentation RFC 7042 */
slip->mac_addr[0] = 0x00;
slip->mac_addr[1] = 0x00;
slip->mac_addr[2] = 0x5E;
slip->mac_addr[3] = 0x00;
slip->mac_addr[4] = 0x53;
slip->mac_addr[5] = sys_rand32_get();
}
net_if_set_link_addr(iface, ll_addr->addr, ll_addr->len,
NET_LINK_ETHERNET);
}