hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/drivers/usb/gadget/function/u_ether.c
....@@ -80,6 +80,7 @@
8080
8181 bool zlp;
8282 bool no_skb_reserve;
83
+ bool ifname_set;
8384 u8 host_mac[ETH_ALEN];
8485 u8 dev_mac[ETH_ALEN];
8586 };
....@@ -322,7 +323,7 @@
322323 /* data overrun */
323324 case -EOVERFLOW:
324325 dev->net->stats.rx_over_errors++;
325
- /* FALLTHROUGH */
326
+ fallthrough;
326327
327328 default:
328329 dev->net->stats.rx_errors++;
....@@ -445,7 +446,7 @@
445446 default:
446447 dev->net->stats.tx_errors++;
447448 VDBG(dev, "tx err %d\n", req->status);
448
- /* FALLTHROUGH */
449
+ fallthrough;
449450 case -ECONNRESET: /* unlink */
450451 case -ESHUTDOWN: /* disconnect etc */
451452 dev_kfree_skb_any(skb);
....@@ -732,7 +733,7 @@
732733 .name = "gadget",
733734 };
734735
735
-/**
736
+/*
736737 * gether_setup_name - initialize one ethernet-over-usb link
737738 * @g: gadget to associated with these links
738739 * @ethaddr: NULL, or a buffer in which the ethernet address of the
....@@ -772,9 +773,13 @@
772773 dev->qmult = qmult;
773774 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
774775
775
- if (get_ether_addr(dev_addr, net->dev_addr))
776
+ if (get_ether_addr(dev_addr, net->dev_addr)) {
777
+ net->addr_assign_type = NET_ADDR_RANDOM;
776778 dev_warn(&g->dev,
777779 "using random %s ethernet address\n", "self");
780
+ } else {
781
+ net->addr_assign_type = NET_ADDR_SET;
782
+ }
778783 if (get_ether_addr(host_addr, dev->host_mac))
779784 dev_warn(&g->dev,
780785 "using random %s ethernet address\n", "host");
....@@ -831,6 +836,9 @@
831836 INIT_LIST_HEAD(&dev->tx_reqs);
832837 INIT_LIST_HEAD(&dev->rx_reqs);
833838
839
+ /* by default we always have a random MAC address */
840
+ net->addr_assign_type = NET_ADDR_RANDOM;
841
+
834842 skb_queue_head_init(&dev->rx_frames);
835843
836844 /* network device setup */
....@@ -868,7 +876,6 @@
868876 g = dev->gadget;
869877
870878 memcpy(net->dev_addr, dev->dev_mac, ETH_ALEN);
871
- net->addr_assign_type = NET_ADDR_RANDOM;
872879
873880 status = register_netdev(net);
874881 if (status < 0) {
....@@ -908,6 +915,7 @@
908915 if (get_ether_addr(dev_addr, new_addr))
909916 return -EINVAL;
910917 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
918
+ net->addr_assign_type = NET_ADDR_SET;
911919 return 0;
912920 }
913921 EXPORT_SYMBOL_GPL(gether_set_dev_addr);
....@@ -1000,16 +1008,46 @@
10001008
10011009 int gether_get_ifname(struct net_device *net, char *name, int len)
10021010 {
1011
+ struct eth_dev *dev = netdev_priv(net);
10031012 int ret;
10041013
10051014 rtnl_lock();
1006
- ret = snprintf(name, len, "%s\n", netdev_name(net));
1015
+ ret = scnprintf(name, len, "%s\n",
1016
+ dev->ifname_set ? net->name : netdev_name(net));
10071017 rtnl_unlock();
1008
- return ret < len ? ret : len;
1018
+ return ret;
10091019 }
10101020 EXPORT_SYMBOL_GPL(gether_get_ifname);
10111021
1012
-/**
1022
+int gether_set_ifname(struct net_device *net, const char *name, int len)
1023
+{
1024
+ struct eth_dev *dev = netdev_priv(net);
1025
+ char tmp[IFNAMSIZ];
1026
+ const char *p;
1027
+
1028
+ if (name[len - 1] == '\n')
1029
+ len--;
1030
+
1031
+ if (len >= sizeof(tmp))
1032
+ return -E2BIG;
1033
+
1034
+ strscpy(tmp, name, len + 1);
1035
+ if (!dev_valid_name(tmp))
1036
+ return -EINVAL;
1037
+
1038
+ /* Require exactly one %d, so binding will not fail with EEXIST. */
1039
+ p = strchr(name, '%');
1040
+ if (!p || p[1] != 'd' || strchr(p + 2, '%'))
1041
+ return -EINVAL;
1042
+
1043
+ strncpy(net->name, tmp, sizeof(net->name));
1044
+ dev->ifname_set = true;
1045
+
1046
+ return 0;
1047
+}
1048
+EXPORT_SYMBOL_GPL(gether_set_ifname);
1049
+
1050
+/*
10131051 * gether_cleanup - remove Ethernet-over-USB device
10141052 * Context: may sleep
10151053 *