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=ens1
35 * ID_NET_NAME_SLOT=ens2f0
36 * ID_NET_NAME_SLOT=ens2f1
37 * ID_NET_NAME_MAC=enxf0def180d479
38 * ID_NET_NAME_PATH=enp0s25
39 * ID_NET_NAME_PATH=enp19s3f0
40 * ID_NET_NAME_PATH=enp19s3f1
49 #include <linux/pci_regs.h>
53 /* retrieve on-board index number and label from firmware */
54 static int dev_pci_onboard(struct udev_device *dev, struct udev_device *parent, const char *prefix, bool test) {
61 /* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */
62 index = udev_device_get_sysattr_value(parent, "acpi_index");
63 /* SMBIOS type 41 -- Onboard Devices Extended Information */
65 index = udev_device_get_sysattr_value(parent, "index");
68 idx = strtoul(index, NULL, 0);
71 snprintf(s, sizeof(s), "%so%d", prefix, idx);
72 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", s);
76 label = udev_device_get_sysattr_value(parent, "label");
78 err = udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", label);
85 static bool is_pci_singlefunction(struct udev_device *dev) {
91 snprintf(filename, sizeof(filename), "%s/config", udev_device_get_syspath(dev));
92 f = fopen(filename, "re");
95 if (fread(&config, sizeof(config), 1, f) != 1)
98 /* bit 0-6 header type, bit 7 multi/single function device */
99 if ((config[PCI_HEADER_TYPE] & 0x80) == 0)
106 static int dev_pci_slot(struct udev_device *dev, struct udev_device *parent, const char *prefix, bool test) {
107 struct udev *udev = udev_device_get_udev(dev);
111 struct udev_device *pci = NULL;
116 int hotplug_slot = 0;
119 /* compose a name based on the raw kernel's PCI bus, slot numbers */
120 if (sscanf(udev_device_get_sysname(parent), "0000:%x:%x.%d", &bus, &slot, &func) != 3)
122 if (func == 0 && is_pci_singlefunction(parent))
123 snprintf(str, sizeof(str), "%sp%ds%d", prefix, bus, slot);
125 snprintf(str, sizeof(str), "%sp%ds%df%d", prefix, bus, slot, func);
126 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
130 /* ACPI _SUN -- slot user number */
131 pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
136 snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
137 dir = opendir(slots);
143 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
148 if (dent->d_name[0] == '.')
150 i = strtol(dent->d_name, &rest, 10);
155 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
156 if (read_one_line_file(str, &address) >= 0) {
157 /* match slot address with device by stripping the function */
158 if (strncmp(address, udev_device_get_sysname(parent), strlen(address)) == 0)
163 if (hotplug_slot > 0)
168 if (hotplug_slot > 0) {
169 if (func == 0 && is_pci_singlefunction(parent))
170 snprintf(str, sizeof(str), "%ss%d", prefix, hotplug_slot);
172 snprintf(str, sizeof(str), "%ss%df%d", prefix, hotplug_slot, func);
173 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
176 udev_device_unref(pci);
180 static int dev_pci(struct udev_device *dev, const char *prefix, bool test) {
181 struct udev_device *parent;
183 /* skip other buses than direct PCI parents */
184 parent = udev_device_get_parent(dev);
185 if (!parent || !streq("pci", udev_device_get_subsystem(parent)))
188 dev_pci_onboard(dev, parent, prefix, test);
189 dev_pci_slot(dev, parent, prefix, test);
193 static int dev_mac(struct udev_device *dev, const char *prefix, bool test) {
196 unsigned int a1, a2, a3, a4, a5, a6;
199 /* check for NET_ADDR_PERM, skip random MAC addresses */
200 s = udev_device_get_sysattr_value(dev, "addr_assign_type");
203 i = strtoul(s, NULL, 0);
207 s = udev_device_get_sysattr_value(dev, "address");
210 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
213 /* skip empty MAC addresses */
214 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
217 /* add IEEE Organizationally Unique Identifier */
218 snprintf(str, sizeof(str), "OUI:%X%X%X", a1, a2, a3);
219 udev_builtin_hwdb_lookup(dev, str, test);
221 snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix, a1, a2, a3, a4, a5, a6);
222 return udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
225 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
229 const char *prefix = "en";
231 /* handle only ARPHRD_ETHER devices */
232 s = udev_device_get_sysattr_value(dev, "type");
235 i = strtoul(s, NULL, 0);
239 devtype = udev_device_get_devtype(dev);
241 if (streq("wlan", devtype))
243 else if (streq("wwan", devtype))
247 dev_pci(dev, prefix, test);
248 dev_mac(dev, prefix, test);
252 const struct udev_builtin udev_builtin_net_id = {
254 .cmd = builtin_net_id,
255 .help = "network device properties",