From 42c48866f1c1fce068f41536baa8dd2e80fc08d7 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 30 Nov 2020 12:34:13 +0100 Subject: [PATCH] config: parse default mac address from board.json Example: { "network-device": { "eth0": { "macaddr": "bc:a5:11:16:76:d7" } } } Signed-off-by: Felix Fietkau --- config.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++ config.h | 1 + config/board.json | 52 +++++++++++++++++++++++++++++++++++++++++++ device.c | 23 ++++++++++++++----- device.h | 3 ++- interface-ip.c | 6 ----- macvlan.c | 5 ----- netifd.h | 8 +++++++ system-dummy.c | 4 +++- system-linux.c | 4 ++-- veth.c | 5 ----- 11 files changed, 141 insertions(+), 26 deletions(-) create mode 100644 config/board.json diff --git a/config.c b/config.c index d91830c..fa7cbe4 100644 --- a/config.c +++ b/config.c @@ -18,6 +18,8 @@ #include +#include + #include "netifd.h" #include "interface.h" #include "interface-ip.h" @@ -31,6 +33,7 @@ bool config_init = false; static struct uci_context *uci_ctx; static struct uci_package *uci_network; static struct uci_package *uci_wireless; +static struct blob_attr *board_netdevs; static struct blob_buf b; static int @@ -654,6 +657,57 @@ config_init_wireless(void) } } + +static struct blob_attr * +config_find_blobmsg_attr(struct blob_attr *attr, const char *name, int type) +{ + struct blobmsg_policy policy = { .name = name, .type = type }; + struct blob_attr *cur; + + blobmsg_parse(&policy, 1, &cur, blobmsg_data(attr), blobmsg_len(attr)); + + return cur; +} + +struct ether_addr *config_get_default_macaddr(const char *ifname) +{ + struct blob_attr *cur; + + if (!board_netdevs) + return NULL; + + cur = config_find_blobmsg_attr(board_netdevs, ifname, BLOBMSG_TYPE_TABLE); + if (!cur) + return NULL; + + cur = config_find_blobmsg_attr(cur, "macaddr", BLOBMSG_TYPE_STRING); + if (!cur) + return NULL; + + return ether_aton(blobmsg_get_string(cur)); +} + +static void +config_init_board(void) +{ + struct blob_attr *cur; + + blob_buf_init(&b, 0); + + if (!blobmsg_add_json_from_file(&b, DEFAULT_BOARD_JSON)) + return; + + free(board_netdevs); + board_netdevs = NULL; + + cur = config_find_blobmsg_attr(b.head, "network-device", + BLOBMSG_TYPE_TABLE); + if (!cur) + return; + + board_netdevs = blob_memdup(cur); +} + int config_init_all(void) { @@ -676,6 +730,8 @@ config_init_all(void) ret = -1; } + config_init_board(); + vlist_update(&interfaces); config_init = true; device_lock(); diff --git a/config.h b/config.h index fae7cd8..ae77ed1 100644 --- a/config.h +++ b/config.h @@ -20,5 +20,6 @@ extern bool config_init; int config_init_all(void); +struct ether_addr *config_get_default_macaddr(const char *ifname); #endif diff --git a/config/board.json b/config/board.json new file mode 100644 index 0000000..112a511 --- /dev/null +++ b/config/board.json @@ -0,0 +1,52 @@ +{ + "model": { + "id": "netgear,gs110tpp-v1", + "name": "Netgear GS110TPP" + }, + "bridge": { + "name": "switch", + "macaddr": "bc:a5:11:16:76:d7" + }, + "network": { + "wan": { + "ifname": " lan1 lan2 lan3 lan4 lan5 lan6 lan7 lan8", + "protocol": "dhcp", + "macaddr": "bc:a5:11:16:76:d7" + }, + "lan": { + "ifname": "lan1", + "protocol": "static", + "vlan": "100", + "macaddr": "be:a5:11:16:76:d7" + } + }, + "network-device": { + "eth0": { + "macaddr": "bc:a5:11:16:76:d7" + }, + "lan1": { + "macaddr": "be:a5:11:16:76:d7" + }, + "lan2": { + "macaddr": "be:a5:11:16:76:d8" + }, + "lan3": { + "macaddr": "be:a5:11:16:76:d9" + }, + "lan4": { + "macaddr": "be:a5:11:16:76:da" + }, + "lan5": { + "macaddr": "be:a5:11:16:76:db" + }, + "lan6": { + "macaddr": "be:a5:11:16:76:dc" + }, + "lan7": { + "macaddr": "be:a5:11:16:76:dd" + }, + "lan8": { + "macaddr": "be:a5:11:16:76:de" + } + } +} diff --git a/device.c b/device.c index 627f1a2..73cc4bf 100644 --- a/device.c +++ b/device.c @@ -18,11 +18,6 @@ #include #include -#include - -#ifdef linux -#include -#endif #include @@ -232,7 +227,7 @@ device_merge_settings(struct device *dev, struct device_settings *n) n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ? s->txqueuelen : os->txqueuelen; memcpy(n->macaddr, - (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr), + (s->flags & (DEV_OPT_MACADDR|DEV_OPT_DEFAULT_MACADDR) ? s->macaddr : os->macaddr), sizeof(n->macaddr)); n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6; n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc; @@ -430,6 +425,21 @@ void device_broadcast_event(struct device *dev, enum device_event ev) safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev); } +static void +device_fill_default_settings(struct device *dev) +{ + struct device_settings *s = &dev->settings; + struct ether_addr *ea; + + if (!(s->flags & DEV_OPT_MACADDR)) { + ea = config_get_default_macaddr(dev->ifname); + if (ea) { + memcpy(s->macaddr, ea, 6); + s->flags |= DEV_OPT_DEFAULT_MACADDR; + } + } +} + int device_claim(struct device_user *dep) { struct device *dev = dep->dev; @@ -447,6 +457,7 @@ int device_claim(struct device_user *dep) return 0; device_broadcast_event(dev, DEV_EVENT_SETUP); + device_fill_default_settings(dev); if (dev->external) { /* Get ifindex for external claimed devices so a valid */ /* ifindex is in place avoiding possible race conditions */ diff --git a/device.h b/device.h index b2b18ab..ab5a162 100644 --- a/device.h +++ b/device.h @@ -93,7 +93,8 @@ enum { DEV_OPT_IGMPVERSION = (1 << 7), DEV_OPT_MLDVERSION = (1 << 8), DEV_OPT_NEIGHREACHABLETIME = (1 << 9), - /* 2 bit hole */ + DEV_OPT_DEFAULT_MACADDR = (1 << 10), + /* 1 bit hole */ DEV_OPT_MTU6 = (1 << 12), DEV_OPT_DADTRANSMITS = (1 << 13), DEV_OPT_MULTICAST_TO_UNICAST = (1 << 14), diff --git a/interface-ip.c b/interface-ip.c index 6efc3c5..024c5b8 100644 --- a/interface-ip.c +++ b/interface-ip.c @@ -22,12 +22,6 @@ #include #include -#ifdef linux -#include -#else -#include -#endif - #include "netifd.h" #include "device.h" #include "interface.h" diff --git a/macvlan.c b/macvlan.c index 092f1dd..c4ebdd0 100644 --- a/macvlan.c +++ b/macvlan.c @@ -17,11 +17,6 @@ #include #include #include -#include - -#ifdef linux -#include -#endif #include "netifd.h" #include "device.h" diff --git a/netifd.h b/netifd.h index a4a146c..9645a0a 100644 --- a/netifd.h +++ b/netifd.h @@ -26,6 +26,12 @@ #include +#ifdef linux +#include +#else +#include +#endif + #include "utils.h" #ifdef DUMMY_MODE @@ -33,11 +39,13 @@ #define DEFAULT_CONFIG_PATH "./config" #define DEFAULT_HOTPLUG_PATH "./examples/hotplug-cmd" #define DEFAULT_RESOLV_CONF "./tmp/resolv.conf" +#define DEFAULT_BOARD_JSON "./config/board.json" #else #define DEFAULT_MAIN_PATH "/lib/netifd" #define DEFAULT_CONFIG_PATH NULL /* use the default set in libuci */ #define DEFAULT_HOTPLUG_PATH "/sbin/hotplug-call" #define DEFAULT_RESOLV_CONF "/tmp/resolv.conf.d/resolv.conf.auto" +#define DEFAULT_BOARD_JSON "/etc/board.json" #endif extern const char *resolv_conf; diff --git a/system-dummy.c b/system-dummy.c index 4ad9db5..6bf0f8b 100644 --- a/system-dummy.c +++ b/system-dummy.c @@ -159,7 +159,9 @@ system_if_dump_stats(struct device *dev, struct blob_buf *b) void system_if_apply_settings(struct device *dev, struct device_settings *s, unsigned int apply_mask) { - if ((s->flags & DEV_OPT_MACADDR & apply_mask) && !dev->external) { + apply_mask &= s->flags; + + if ((apply_mask & (DEV_OPT_MACADDR | DEV_OPT_DEFAULT_MACADDR)) && !dev->external) { D(SYSTEM, "ifconfig %s hw ether %s\n", dev->ifname, format_macaddr(s->macaddr)); } diff --git a/system-linux.c b/system-linux.c index 880cd23..bf746f9 100644 --- a/system-linux.c +++ b/system-linux.c @@ -28,8 +28,8 @@ #include #include -#include #include +#include #include #include @@ -1659,7 +1659,7 @@ system_if_apply_settings(struct device *dev, struct device_settings *s, unsigned if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0) s->flags &= ~DEV_OPT_TXQUEUELEN; } - if ((apply_mask & DEV_OPT_MACADDR) && !dev->external) { + if ((apply_mask & (DEV_OPT_MACADDR | DEV_OPT_DEFAULT_MACADDR)) && !dev->external) { ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr)); if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0) diff --git a/veth.c b/veth.c index eafa4cf..fdab38a 100644 --- a/veth.c +++ b/veth.c @@ -18,11 +18,6 @@ #include #include #include -#include - -#ifdef linux -#include -#endif #include "netifd.h" #include "device.h" -- 2.30.2