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/>.
21 * predictable network interface device names based on:
22 * - firmware/bios-provided index numbers for on-board devices
23 * - firmware-provided pci-express hotplug slot index number
24 * - physical/geographical location of the hardware
25 * - the interface's MAC address
27 * two character prefixes based on the type of interface:
33 * o<index> -- on-board device index number
34 * s<slot>[f<function>] -- hotplug slot index number
35 * x<MAC> -- MAC address
36 * p<bus>s<slot>[f<function>] -- PCI geographical location
38 * All multi-function devices will carry the [f<function>] number in the
39 * device name, including the function 0 device.
42 * ID_NET_NAME_ONBOARD=eno1
43 * ID_NET_NAME_SLOT=ens1
44 * ID_NET_NAME_SLOT=ens2f0
45 * ID_NET_NAME_SLOT=ens2f1
46 * ID_NET_NAME_MAC=enxf0def180d479
47 * ID_NET_NAME_PATH=enp0s25
48 * ID_NET_NAME_PATH=enp19s3f0
49 * ID_NET_NAME_PATH=enp19s3f1
59 #include <linux/pci_regs.h>
70 enum netname_type type;
75 struct udev_device *pcidev;
76 char pci_slot[IFNAMSIZ];
77 char pci_path[IFNAMSIZ];
78 char pci_onboard[IFNAMSIZ];
79 const char *pci_onboard_label;
81 struct udev_device *usbdev;
82 char usb_ports[IFNAMSIZ];
85 /* retrieve on-board index number and label from firmware */
86 static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
90 /* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */
91 index = udev_device_get_sysattr_value(names->pcidev, "acpi_index");
92 /* SMBIOS type 41 -- Onboard Devices Extended Information */
94 index = udev_device_get_sysattr_value(names->pcidev, "index");
97 idx = strtoul(index, NULL, 0);
100 snprintf(names->pci_onboard, sizeof(names->pci_onboard), "o%d", idx);
102 names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label");
106 /* read the 256 bytes PCI configuration space to check the multi-function bit */
107 static bool is_pci_singlefunction(struct udev_device *dev) {
113 snprintf(filename, sizeof(filename), "%s/config", udev_device_get_syspath(dev));
114 f = fopen(filename, "re");
117 if (fread(&config, sizeof(config), 1, f) != 1)
120 /* bit 0-6 header type, bit 7 multi/single function device */
121 if ((config[PCI_HEADER_TYPE] & 0x80) == 0)
128 static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
129 struct udev *udev = udev_device_get_udev(names->pcidev);
133 struct udev_device *pci = NULL;
138 int hotplug_slot = 0;
141 /* compose a name based on the raw kernel's PCI bus, slot numbers */
142 if (sscanf(udev_device_get_sysname(names->pcidev), "0000:%x:%x.%d", &bus, &slot, &func) != 3)
144 if (func == 0 && is_pci_singlefunction(names->pcidev))
145 snprintf(names->pci_path, sizeof(names->pci_path), "p%ds%d", bus, slot);
147 snprintf(names->pci_path, sizeof(names->pci_path), "p%ds%df%d", bus, slot, func);
149 /* ACPI _SUN -- slot user number */
150 pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
155 snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
156 dir = opendir(slots);
162 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
167 if (dent->d_name[0] == '.')
169 i = strtol(dent->d_name, &rest, 10);
174 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
175 if (read_one_line_file(str, &address) >= 0) {
176 /* match slot address with device by stripping the function */
177 if (strncmp(address, udev_device_get_sysname(names->pcidev), strlen(address)) == 0)
182 if (hotplug_slot > 0)
187 if (hotplug_slot > 0) {
188 if (func == 0 && is_pci_singlefunction(names->pcidev))
189 snprintf(names->pci_slot, sizeof(names->pci_slot), "s%d", hotplug_slot);
191 snprintf(names->pci_slot, sizeof(names->pci_slot), "s%df%d", hotplug_slot, func);
194 udev_device_unref(pci);
198 static int names_pci(struct udev_device *dev, struct netnames *names) {
199 struct udev_device *parent;
201 parent = udev_device_get_parent(dev);
204 /* check if our direct parent is a PCI device with no other bus in-between */
205 if (streq("pci", udev_device_get_subsystem(parent))) {
206 names->type = NET_PCI;
207 names->pcidev = parent;
209 names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
213 dev_pci_onboard(dev, names);
214 dev_pci_slot(dev, names);
218 static int names_usb(struct udev_device *dev, struct netnames *names) {
226 names->usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
230 /* get USB port number chain, configuration, interface */
231 util_strscpy(name, sizeof(name), udev_device_get_sysname(names->usbdev));
232 s = strchr(name, '-');
237 s = strchr(ports, ':');
243 s = strchr(config, '.');
249 /* prefix every port number in the chain with "u"*/
251 while ((s = strchr(s, '.')))
253 s = names->usb_ports;
254 l = util_strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
256 /* append USB config number, suppress the common config == 1 */
257 if (!streq(config, "1"))
258 l = util_strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
260 /* append USB interface number, suppress the interface == 0 */
261 if (!streq(interf, "0"))
262 l = util_strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
264 return -ENAMETOOLONG;
266 names->type = NET_USB;
270 static int names_mac(struct udev_device *dev, struct netnames *names) {
273 unsigned int a1, a2, a3, a4, a5, a6;
275 /* check for NET_ADDR_PERM, skip random MAC addresses */
276 s = udev_device_get_sysattr_value(dev, "addr_assign_type");
279 i = strtoul(s, NULL, 0);
283 s = udev_device_get_sysattr_value(dev, "address");
286 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
289 /* skip empty MAC addresses */
290 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
299 names->mac_valid = true;
303 /* IEEE Organizationally Unique Identifier vendor string */
304 static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) {
307 if (names->mac_valid)
309 /* skip commonly misused 00:00:00 (Xerox) prefix */
310 if (memcmp(names->mac, "\0\0\0", 3) == 0)
312 snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X",
313 names->mac[0], names->mac[1], names->mac[2],
314 names->mac[3], names->mac[4], names->mac[5]);
315 udev_builtin_hwdb_lookup(dev, str, test);
319 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
323 const char *prefix = "en";
324 struct netnames names;
327 /* handle only ARPHRD_ETHER devices */
328 s = udev_device_get_sysattr_value(dev, "type");
331 i = strtoul(s, NULL, 0);
335 devtype = udev_device_get_devtype(dev);
337 if (streq("wlan", devtype))
339 else if (streq("wwan", devtype))
344 err = names_mac(dev, &names);
345 if (err >= 0 && names.mac_valid) {
348 snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix,
349 names.mac[0], names.mac[1], names.mac[2],
350 names.mac[3], names.mac[4], names.mac[5]);
351 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
353 ieee_oui(dev, &names, test);
356 /* get PCI based path names, we compose only PCI based paths */
357 err = names_pci(dev, &names);
361 /* plain PCI device */
362 if (names.type == NET_PCI) {
365 if (names.pci_onboard[0])
366 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str))
367 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
369 if (names.pci_onboard_label)
370 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str))
371 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
373 if (names.pci_path[0])
374 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (int)sizeof(str))
375 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
377 if (names.pci_slot[0])
378 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_slot) < (int)sizeof(str))
379 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
384 err = names_usb(dev, &names);
385 if (err >= 0 && names.type == NET_USB) {
388 if (names.pci_path[0])
389 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (int)sizeof(str))
390 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
392 if (names.pci_slot[0])
393 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str))
394 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
400 const struct udev_builtin udev_builtin_net_id = {
402 .cmd = builtin_net_id,
403 .help = "network device properties",