The ethernet gadget driver requires the hex formatted MAC address
bytes with leading zero, in other words each byte needs to be two
characters in length (see get_ether_addr in u_ether.c). The libc
implementation ether_ntoa does not print leading zeros. Hence use
our own implementation which provides the format expected by the
kernel.

Signed-off-by: Stefan Agner <[email protected]>
---
Hi Matt,

Discovered this while trying to integrate gadget-export/import
on our Colibri VF61 module.

This bug leads to various interesting effects when using export/
import functionality, e.g.
A configuration of 10:14:3d:0f:ff:fe leads to 10:14:3d:ff:ff:fe
A configuration of 00:14:3d:0f:ff:fe leads to an error:
Error on import gadget
Error: USBG_ERROR_INVALID_PARAM : Invalid parameter

 src/usbg.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/usbg.c b/src/usbg.c
index 1703ff1..47903cc 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -782,6 +782,15 @@ static int usbg_rm_all_dirs(const char *path)
        return ret;
 }
 
+static char *usbg_ether_ntoa_r(const struct ether_addr *addr, char *buf)
+{
+       sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
+               addr->ether_addr_octet[0], addr->ether_addr_octet[1],
+               addr->ether_addr_octet[2], addr->ether_addr_octet[3],
+               addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
+       return buf;
+}
+
 static int usbg_parse_function_net_attrs(usbg_function *f,
                usbg_function_attrs *f_attrs)
 {
@@ -2367,12 +2376,12 @@ int usbg_set_function_net_attrs(usbg_function *f, 
usbg_f_net_attrs *attrs)
                goto out;
        }
 
-       addr = ether_ntoa_r(&attrs->dev_addr, addr_buf);
+       addr = usbg_ether_ntoa_r(&attrs->dev_addr, addr_buf);
        ret = usbg_write_string(f->path, f->name, "dev_addr", addr);
        if (ret != USBG_SUCCESS)
                goto out;
 
-       addr = ether_ntoa_r(&attrs->host_addr, addr_buf);
+       addr = usbg_ether_ntoa_r(&attrs->host_addr, addr_buf);
        ret = usbg_write_string(f->path, f->name, "host_addr", addr);
        if (ret != USBG_SUCCESS)
                goto out;
@@ -2432,7 +2441,7 @@ int usbg_set_net_dev_addr(usbg_function *f, struct 
ether_addr *dev_addr)
 
        if (f && dev_addr) {
                char str_buf[USBG_MAX_STR_LENGTH];
-               char *str_addr = ether_ntoa_r(dev_addr, str_buf);
+               char *str_addr = usbg_ether_ntoa_r(dev_addr, str_buf);
                ret = usbg_write_string(f->path, f->name, "dev_addr", str_addr);
        } else {
                ret = USBG_ERROR_INVALID_PARAM;
@@ -2447,7 +2456,7 @@ int usbg_set_net_host_addr(usbg_function *f, struct 
ether_addr *host_addr)
 
        if (f && host_addr) {
                char str_buf[USBG_MAX_STR_LENGTH];
-               char *str_addr = ether_ntoa_r(host_addr, str_buf);
+               char *str_addr = usbg_ether_ntoa_r(host_addr, str_buf);
                ret = usbg_write_string(f->path, f->name, "host_addr", 
str_addr);
        } else {
                ret = USBG_ERROR_INVALID_PARAM;
@@ -2811,7 +2820,7 @@ static int usbg_export_f_net_attrs(usbg_f_net_attrs 
*attrs,
        if (!node)
                goto out;
 
-       addr = ether_ntoa_r(&attrs->dev_addr, addr_buf);
+       addr = usbg_ether_ntoa_r(&attrs->dev_addr, addr_buf);
        cfg_ret = config_setting_set_string(node, addr);
        if (cfg_ret != CONFIG_TRUE) {
                ret = USBG_ERROR_OTHER_ERROR;
@@ -2822,7 +2831,7 @@ static int usbg_export_f_net_attrs(usbg_f_net_attrs 
*attrs,
        if (!node)
                goto out;
 
-       addr = ether_ntoa_r(&attrs->host_addr, addr_buf);
+       addr = usbg_ether_ntoa_r(&attrs->host_addr, addr_buf);
        cfg_ret = config_setting_set_string(node, addr);
        if (cfg_ret != CONFIG_TRUE) {
                ret = USBG_ERROR_OTHER_ERROR;
-- 
1.9.3

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to