From 7739a90d11344631aa745f9388848cbdde7b2e09 Mon Sep 17 00:00:00 2001 From: Amin Ben Ramdhane Date: Fri, 18 Mar 2022 12:00:08 +0100 Subject: [PATCH] T#7625: Empty MacAddress for VLAN interface --- dmtree/tr181/ethernet.c | 72 ++++++++++++++++++++--------------------- dmtree/tr181/ip.c | 13 ++------ libbbf_api/dmcommon.c | 23 +++++++------ libbbf_api/dmcommon.h | 1 + 4 files changed, 52 insertions(+), 57 deletions(-) diff --git a/dmtree/tr181/ethernet.c b/dmtree/tr181/ethernet.c index 8157252d..15192e7b 100644 --- a/dmtree/tr181/ethernet.c +++ b/dmtree/tr181/ethernet.c @@ -160,45 +160,55 @@ bool ethernet___name_exists_in_devices(char *name) return false; } -static void add_new_dmmap_section(char *macaddr, char*interface, char *section_name) +static void add_new_dmmap_section(char *macaddr, char *iface_device, char *iface_name) { struct uci_section *dmmap = NULL; dmuci_add_section_bbfdm(DMMAP, "link", &dmmap); dmuci_set_value_by_section(dmmap, "mac", macaddr); - dmuci_set_value_by_section(dmmap, "device", interface); - dmuci_set_value_by_section(dmmap, "is_eth", (!DM_LSTRNCMP(interface, "atm", 3) || !DM_LSTRNCMP(interface, "ptm", 3)) ? "0" : "1"); - dmuci_set_value_by_section(dmmap, "section_name", section_name); + dmuci_set_value_by_section(dmmap, "device", iface_device); + dmuci_set_value_by_section(dmmap, "is_eth", (!DM_LSTRNCMP(iface_device, "atm", 3) || !DM_LSTRNCMP(iface_device, "ptm", 3)) ? "0" : "1"); + dmuci_set_value_by_section(dmmap, "section_name", iface_name); } -static void create_link(char *sec_name, char *mac_addr) +static void create_link(char *sec_name, char *mac_addr, char *iface_device) { - char *macaddr = (*mac_addr != '\0') ? mac_addr : get_macaddr(sec_name); + struct uci_section *s = NULL; + char iface_dev[32] = {0}; + char *macaddr = NULL; + + char *device = get_device(sec_name); + if (device[0] == '\0') + device = iface_device; + + DM_STRNCPY(iface_dev, device, sizeof(iface_dev)); + + char *has_vid = DM_STRCHR(iface_dev, '.'); + if (has_vid) { + *has_vid = '\0'; + get_net_device_sysfs(iface_dev, "address", &macaddr); + } else { + macaddr = (*mac_addr != '\0') ? mac_addr : get_macaddr(sec_name); + } + if (macaddr[0] == '\0') return; - struct uci_section *s = NULL; - char *dev_sec_name; uci_path_foreach_sections(bbfdm, DMMAP, "link", s) { - dmuci_get_value_by_section_string(s, "section_name", &dev_sec_name); - if (DM_STRCMP(sec_name, dev_sec_name) == 0) { + char *link_device = NULL; + + dmuci_get_value_by_section_string(s, "device", &link_device); + if (DM_STRCMP(iface_dev, link_device) == 0) { dmuci_set_value_by_section(s, "mac", macaddr); return; } } - char *device = get_device(sec_name); - if (device[0] == '\0') - return; - /* For all the Ethernet link objects pointing to same Ethernet Interface, only one ethernet link */ - char intf[32] = {0}; - DM_STRNCPY(intf, device, sizeof(intf)); - char *vid = DM_STRCHR(intf, '.'); - char *macvlan = DM_STRCHR(intf, '_'); - if (vid != NULL || !macvlan) { - if (vid) *vid = '\0'; - struct uci_section *dmmap_section = is_device_section_exist(intf); + char *is_macvlan = DM_STRCHR(iface_dev, '_'); + if (has_vid != NULL || !is_macvlan) { + + struct uci_section *dmmap_section = is_device_section_exist(iface_dev); if (dmmap_section) { char *section_name; dmuci_get_value_by_section_string(dmmap_section, "section_name", §ion_name); @@ -212,13 +222,13 @@ static void create_link(char *sec_name, char *mac_addr) } else { /* Add new dmmap section */ - add_new_dmmap_section(macaddr, intf, sec_name); + add_new_dmmap_section(macaddr, iface_dev, sec_name); } return; } /* Add new dmmap section */ - add_new_dmmap_section(macaddr, intf, sec_name); + add_new_dmmap_section(macaddr, iface_dev, sec_name); } static int dmmap_synchronizeEthernetLink(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) @@ -243,7 +253,7 @@ static int dmmap_synchronizeEthernetLink(struct dmctx *dmctx, DMNODE *parent_nod continue; dmuci_get_value_by_section_string(s, "macaddr", &macaddr); - create_link(section_name(s), macaddr); + create_link(section_name(s), macaddr, device); } return 0; } @@ -1308,19 +1318,7 @@ static int set_EthernetLink_LowerLayers(char *refparam, struct dmctx *ctx, void static int get_EthernetLink_MACAddress(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - char *mac_addr; - char address[64] = {0}; - int i; - - dmuci_get_value_by_section_string((struct uci_section *)data, "mac", &mac_addr); - DM_STRNCPY(address, mac_addr, sizeof(address)); - for (i = 0; address[i] != '\0'; i++) { - if(address[i] >= 'a' && address[i] <= 'z') { - address[i] = address[i] - 32; - } - } - - *value = dmstrdup(address); + dmuci_get_value_by_section_string((struct uci_section *)data, "mac", value); return 0; } diff --git a/dmtree/tr181/ip.c b/dmtree/tr181/ip.c index e66723da..777578cc 100644 --- a/dmtree/tr181/ip.c +++ b/dmtree/tr181/ip.c @@ -1475,10 +1475,11 @@ static int set_IPInterface_LowerLayers(char *refparam, struct dmctx *ctx, void * // Check if there is an dmmap link section that has the same device ==> if yes, update section name get_dmmap_section_of_config_section_eq("dmmap", "link", "device", linker_buf, &s); - + dmuci_set_value_by_section_bbfdm(s, "section_name", section_name((struct uci_section *)data)); } else { // Check if there is an interface that has the same name of device ==> if yes, remove it char dev_buf[32] = {0}; + DM_STRNCPY(dev_buf, linker_buf, sizeof(dev_buf)); char *vid = DM_STRCHR(dev_buf, '.'); if (vid) { @@ -1487,20 +1488,10 @@ static int set_IPInterface_LowerLayers(char *refparam, struct dmctx *ctx, void * dmuci_delete_by_section(s, NULL, NULL); } } - - get_dmmap_section_of_config_section_eq("dmmap", "link", "device", dev_buf, &s); } // Update device option dmuci_set_value_by_section((struct uci_section *)data, "device", linker_buf); - - if (s) { - char *macaddr = NULL; - - dmuci_get_value_by_section_string((struct uci_section *)data, "macaddr", &macaddr); - dmuci_set_value_by_section_bbfdm(s, "mac", macaddr); - dmuci_set_value_by_section_bbfdm(s, "section_name", section_name((struct uci_section *)data)); - } } else if (DM_STRNCMP(value, eth_link, DM_STRLEN(eth_link)) == 0) { // Get interface name from Ethernet.Link. object diff --git a/libbbf_api/dmcommon.c b/libbbf_api/dmcommon.c index c5ea2322..f7175240 100644 --- a/libbbf_api/dmcommon.c +++ b/libbbf_api/dmcommon.c @@ -758,6 +758,15 @@ char **strsplit_by_str(const char str[], char *delim) return tokens; } +void convert_str_to_uppercase(char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + if (str[i] >= 'a' && str[i] <= 'z') { + str[i] = str[i] - 32; + } + } +} + char *get_macaddr(char *interface_name) { char *device = get_device(interface_name); @@ -769,6 +778,7 @@ char *get_macaddr(char *interface_name) snprintf(file, sizeof(file), "/sys/class/net/%s/address", device); dm_read_sysfs_file(file, val, sizeof(val)); + convert_str_to_uppercase(val); mac = dmstrdup(val); } else { mac = ""; @@ -993,18 +1003,13 @@ int get_net_device_sysfs(const char *device, const char *name, char **value) { if (device && device[0]) { char file[256]; - char val[64] = {0}; + char val[32] = {0}; snprintf(file, sizeof(file), "/sys/class/net/%s/%s", device, name); dm_read_sysfs_file(file, val, sizeof(val)); - if (0 == strcmp(name, "address")) { - // Convert the mac address to upper case. - int i; - for (i = 0; val[i] != '\0'; i++) { - if (val[i] >= 'a' && val[i] <= 'z') { - val[i] = val[i] - 32; - } - } + if (strcmp(name, "address") == 0) { + // Convert the mac address to upper case + convert_str_to_uppercase(val); } *value = dmstrdup(val); } else { diff --git a/libbbf_api/dmcommon.h b/libbbf_api/dmcommon.h index d24cd3d4..6454bbed 100644 --- a/libbbf_api/dmcommon.h +++ b/libbbf_api/dmcommon.h @@ -242,6 +242,7 @@ unsigned char isdigit_str(char *str); char *dm_strword(char *src, char *str); char **strsplit(const char* str, const char* delim, size_t* numtokens); char **strsplit_by_str(const char str[], char *delim); +void convert_str_to_uppercase(char *str); char *get_macaddr(char *interface_name); char *get_device(char *interface_name); char *get_l3_device(char *interface_name);