2 This file is part of systemd.
4 Copyright 2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 * o<index> -- on-board device index
28 * s<slot>f<function> -- hotplug slot number
29 * x<MAC> -- MAC address
30 * p<bus>s<slot>f<function> -- PCI/physical location
33 * ID_NET_NAME_ONBOARD=eno1
34 * ID_NET_NAME_SLOT=ens1f0
35 * ID_NET_NAME_MAC=enxf0def180d479
36 * ID_NET_NAME_PATH=enp19s0f0
48 /* retrieve on-board index number and label from firmware */
49 static int dev_pci_onboard(struct udev_device *dev, struct udev_device *parent, const char *prefix, bool test) {
56 /* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */
57 index = udev_device_get_sysattr_value(parent, "acpi_index");
58 /* SMBIOS type 41 -- Onboard Devices Extended Information */
60 index = udev_device_get_sysattr_value(parent, "index");
63 idx = strtoul(index, NULL, 0);
66 snprintf(s, sizeof(s), "%so%d", prefix, idx);
67 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", s);
71 label = udev_device_get_sysattr_value(parent, "label");
73 err = udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", label);
80 static int dev_pci_slot(struct udev_device *dev, struct udev_device *parent, const char *prefix, bool test) {
81 struct udev *udev = udev_device_get_udev(dev);
85 struct udev_device *pci = NULL;
93 /* compose a name based on the raw kernel's PCI bus, slot numbers */
94 if (sscanf(udev_device_get_sysname(parent), "0000:%x:%x.%d", &bus, &slot, &func) != 3)
96 snprintf(str, sizeof(str), "%sp%ds%df%d", prefix, bus, slot, func);
97 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
101 /* ACPI _SUN -- slot user number */
102 pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
107 snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
108 dir = opendir(slots);
114 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
119 if (dent->d_name[0] == '.')
121 i = strtol(dent->d_name, &rest, 10);
126 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
127 if (read_one_line_file(str, &address) >= 0) {
128 /* match slot address with device by stripping the function */
129 if (strncmp(address, udev_device_get_sysname(parent), strlen(address)) == 0)
134 if (hotplug_slot > 0)
139 if (hotplug_slot > 0) {
140 snprintf(str, sizeof(str), "%ss%df%d", prefix, hotplug_slot, func);
141 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
144 udev_device_unref(pci);
148 static int dev_pci(struct udev_device *dev, const char *prefix, bool test) {
149 struct udev_device *parent;
151 /* skip other buses than direct PCI parents */
152 parent = udev_device_get_parent(dev);
153 if (!parent || !streq("pci", udev_device_get_subsystem(parent)))
156 dev_pci_onboard(dev, parent, prefix, test);
157 dev_pci_slot(dev, parent, prefix, test);
161 static int dev_mac(struct udev_device *dev, const char *prefix, bool test) {
164 unsigned int a1, a2, a3, a4, a5, a6;
167 /* check for NET_ADDR_PERM, skip random MAC addresses */
168 s = udev_device_get_sysattr_value(dev, "addr_assign_type");
171 i = strtoul(s, NULL, 0);
175 s = udev_device_get_sysattr_value(dev, "address");
178 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
181 /* skip empty MAC addresses */
182 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
185 /* add IEEE Organizationally Unique Identifier */
186 snprintf(str, sizeof(str), "OUI:%X%X%X", a1, a2, a3);
187 udev_builtin_hwdb_lookup(dev, str, test);
189 snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix, a1, a2, a3, a4, a5, a6);
190 return udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
193 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
197 const char *prefix = "en";
199 /* handle only ARPHRD_ETHER devices */
200 s = udev_device_get_sysattr_value(dev, "type");
203 i = strtoul(s, NULL, 0);
207 devtype = udev_device_get_devtype(dev);
209 if (streq("wlan", devtype))
211 else if (streq("wwan", devtype))
215 dev_pci(dev, prefix, test);
216 dev_mac(dev, prefix, test);
220 const struct udev_builtin udev_builtin_net_id = {
222 .cmd = builtin_net_id,
223 .help = "network device properties",