hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/usb/gadget/function/u_ether.c
....@@ -17,6 +17,7 @@
1717 #include <linux/etherdevice.h>
1818 #include <linux/ethtool.h>
1919 #include <linux/if_vlan.h>
20
+#include <linux/string_helpers.h>
2021
2122 #include "u_ether.h"
2223
....@@ -80,6 +81,7 @@
8081
8182 bool zlp;
8283 bool no_skb_reserve;
84
+ bool ifname_set;
8385 u8 host_mac[ETH_ALEN];
8486 u8 dev_mac[ETH_ALEN];
8587 };
....@@ -322,7 +324,7 @@
322324 /* data overrun */
323325 case -EOVERFLOW:
324326 dev->net->stats.rx_over_errors++;
325
- /* FALLTHROUGH */
327
+ fallthrough;
326328
327329 default:
328330 dev->net->stats.rx_errors++;
....@@ -445,7 +447,7 @@
445447 default:
446448 dev->net->stats.tx_errors++;
447449 VDBG(dev, "tx err %d\n", req->status);
448
- /* FALLTHROUGH */
450
+ fallthrough;
449451 case -ECONNRESET: /* unlink */
450452 case -ESHUTDOWN: /* disconnect etc */
451453 dev_kfree_skb_any(skb);
....@@ -732,7 +734,7 @@
732734 .name = "gadget",
733735 };
734736
735
-/**
737
+/*
736738 * gether_setup_name - initialize one ethernet-over-usb link
737739 * @g: gadget to associated with these links
738740 * @ethaddr: NULL, or a buffer in which the ethernet address of the
....@@ -772,9 +774,13 @@
772774 dev->qmult = qmult;
773775 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
774776
775
- if (get_ether_addr(dev_addr, net->dev_addr))
777
+ if (get_ether_addr(dev_addr, net->dev_addr)) {
778
+ net->addr_assign_type = NET_ADDR_RANDOM;
776779 dev_warn(&g->dev,
777780 "using random %s ethernet address\n", "self");
781
+ } else {
782
+ net->addr_assign_type = NET_ADDR_SET;
783
+ }
778784 if (get_ether_addr(host_addr, dev->host_mac))
779785 dev_warn(&g->dev,
780786 "using random %s ethernet address\n", "host");
....@@ -831,6 +837,9 @@
831837 INIT_LIST_HEAD(&dev->tx_reqs);
832838 INIT_LIST_HEAD(&dev->rx_reqs);
833839
840
+ /* by default we always have a random MAC address */
841
+ net->addr_assign_type = NET_ADDR_RANDOM;
842
+
834843 skb_queue_head_init(&dev->rx_frames);
835844
836845 /* network device setup */
....@@ -868,7 +877,6 @@
868877 g = dev->gadget;
869878
870879 memcpy(net->dev_addr, dev->dev_mac, ETH_ALEN);
871
- net->addr_assign_type = NET_ADDR_RANDOM;
872880
873881 status = register_netdev(net);
874882 if (status < 0) {
....@@ -908,6 +916,7 @@
908916 if (get_ether_addr(dev_addr, new_addr))
909917 return -EINVAL;
910918 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
919
+ net->addr_assign_type = NET_ADDR_SET;
911920 return 0;
912921 }
913922 EXPORT_SYMBOL_GPL(gether_set_dev_addr);
....@@ -967,6 +976,8 @@
967976 dev = netdev_priv(net);
968977 snprintf(host_addr, len, "%pm", dev->host_mac);
969978
979
+ string_upper(host_addr, host_addr);
980
+
970981 return strlen(host_addr);
971982 }
972983 EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
....@@ -1000,16 +1011,46 @@
10001011
10011012 int gether_get_ifname(struct net_device *net, char *name, int len)
10021013 {
1014
+ struct eth_dev *dev = netdev_priv(net);
10031015 int ret;
10041016
10051017 rtnl_lock();
1006
- ret = snprintf(name, len, "%s\n", netdev_name(net));
1018
+ ret = scnprintf(name, len, "%s\n",
1019
+ dev->ifname_set ? net->name : netdev_name(net));
10071020 rtnl_unlock();
1008
- return ret < len ? ret : len;
1021
+ return ret;
10091022 }
10101023 EXPORT_SYMBOL_GPL(gether_get_ifname);
10111024
1012
-/**
1025
+int gether_set_ifname(struct net_device *net, const char *name, int len)
1026
+{
1027
+ struct eth_dev *dev = netdev_priv(net);
1028
+ char tmp[IFNAMSIZ];
1029
+ const char *p;
1030
+
1031
+ if (name[len - 1] == '\n')
1032
+ len--;
1033
+
1034
+ if (len >= sizeof(tmp))
1035
+ return -E2BIG;
1036
+
1037
+ strscpy(tmp, name, len + 1);
1038
+ if (!dev_valid_name(tmp))
1039
+ return -EINVAL;
1040
+
1041
+ /* Require exactly one %d, so binding will not fail with EEXIST. */
1042
+ p = strchr(name, '%');
1043
+ if (!p || p[1] != 'd' || strchr(p + 2, '%'))
1044
+ return -EINVAL;
1045
+
1046
+ strncpy(net->name, tmp, sizeof(net->name));
1047
+ dev->ifname_set = true;
1048
+
1049
+ return 0;
1050
+}
1051
+EXPORT_SYMBOL_GPL(gether_set_ifname);
1052
+
1053
+/*
10131054 * gether_cleanup - remove Ethernet-over-USB device
10141055 * Context: may sleep
10151056 *