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 * http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames
29 * Two character prefixes based on the type of interface:
31 * sl -- serial line IP (slip)
36 * b<number> -- BCMA bus core number
37 * ccw<name> -- CCW bus group name
38 * o<index>[d<dev_port>] -- on-board device index number
39 * s<slot>[f<function>][d<dev_port>] -- hotplug slot index number
40 * x<MAC> -- MAC address
41 * [P<domain>]p<bus>s<slot>[f<function>][d<dev_port>]
42 * -- PCI geographical location
43 * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
44 * -- USB port number chain
46 * All multi-function PCI devices will carry the [f<function>] number in the
47 * device name, including the function 0 device.
49 * When using PCI geography, The PCI domain is only prepended when it is not 0.
51 * For USB devices the full chain of port numbers of hubs is composed. If the
52 * name gets longer than the maximum number of 15 characters, the name is not
54 * The usual USB configuration == 1 and interface == 0 values are suppressed.
56 * PCI Ethernet card with firmware index "1":
57 * ID_NET_NAME_ONBOARD=eno1
58 * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
60 * PCI Ethernet card in hotplug slot with firmware index number:
61 * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
62 * ID_NET_NAME_MAC=enx000000000466
63 * ID_NET_NAME_PATH=enp5s0
64 * ID_NET_NAME_SLOT=ens1
66 * PCI Ethernet multi-function card with 2 ports:
67 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0/net/enp2s0f0
68 * ID_NET_NAME_MAC=enx78e7d1ea46da
69 * ID_NET_NAME_PATH=enp2s0f0
70 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.1/net/enp2s0f1
71 * ID_NET_NAME_MAC=enx78e7d1ea46dc
72 * ID_NET_NAME_PATH=enp2s0f1
75 * /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
76 * ID_NET_NAME_MAC=wlx0024d7e31130
77 * ID_NET_NAME_PATH=wlp3s0
79 * USB built-in 3G modem:
80 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
81 * ID_NET_NAME_MAC=wwx028037ec0200
82 * ID_NET_NAME_PATH=wwp0s29u1u4i6
85 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
86 * ID_NET_NAME_MAC=enxd626b3450fb5
87 * ID_NET_NAME_PATH=enp0s29u1u2
93 #include <net/if_arp.h>
99 #include <linux/pci_regs.h>
103 #include "string-util.h"
116 enum netname_type type
;
121 struct udev_device
*pcidev
;
122 char pci_slot
[IFNAMSIZ
];
123 char pci_path
[IFNAMSIZ
];
124 char pci_onboard
[IFNAMSIZ
];
125 const char *pci_onboard_label
;
127 char usb_ports
[IFNAMSIZ
];
128 char bcma_core
[IFNAMSIZ
];
129 char ccw_group
[IFNAMSIZ
];
132 /* retrieve on-board index number and label from firmware */
133 static int dev_pci_onboard(struct udev_device
*dev
, struct netnames
*names
) {
134 unsigned dev_port
= 0;
140 /* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */
141 attr
= udev_device_get_sysattr_value(names
->pcidev
, "acpi_index");
142 /* SMBIOS type 41 -- Onboard Devices Extended Information */
144 attr
= udev_device_get_sysattr_value(names
->pcidev
, "index");
148 idx
= strtoul(attr
, NULL
, 0);
152 /* kernel provided port index for multiple ports on a single PCI function */
153 attr
= udev_device_get_sysattr_value(dev
, "dev_port");
155 dev_port
= strtol(attr
, NULL
, 10);
157 s
= names
->pci_onboard
;
158 l
= sizeof(names
->pci_onboard
);
159 l
= strpcpyf(&s
, l
, "o%d", idx
);
161 l
= strpcpyf(&s
, l
, "d%d", dev_port
);
163 names
->pci_onboard
[0] = '\0';
165 names
->pci_onboard_label
= udev_device_get_sysattr_value(names
->pcidev
, "label");
170 /* read the 256 bytes PCI configuration space to check the multi-function bit */
171 static bool is_pci_multifunction(struct udev_device
*dev
) {
172 _cleanup_close_
int fd
= -1;
173 const char *filename
;
176 filename
= strjoina(udev_device_get_syspath(dev
), "/config");
177 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
180 if (read(fd
, &config
, sizeof(config
)) != sizeof(config
))
183 /* bit 0-6 header type, bit 7 multi/single function device */
184 if ((config
[PCI_HEADER_TYPE
] & 0x80) != 0)
190 static int dev_pci_slot(struct udev_device
*dev
, struct netnames
*names
) {
191 struct udev
*udev
= udev_device_get_udev(names
->pcidev
);
192 unsigned domain
, bus
, slot
, func
, dev_port
= 0;
196 struct udev_device
*pci
= NULL
;
197 char slots
[256], str
[256];
198 _cleanup_closedir_
DIR *dir
= NULL
;
200 int hotplug_slot
= 0, err
= 0;
202 if (sscanf(udev_device_get_sysname(names
->pcidev
), "%x:%x:%x.%u", &domain
, &bus
, &slot
, &func
) != 4)
205 /* kernel provided port index for multiple ports on a single PCI function */
206 attr
= udev_device_get_sysattr_value(dev
, "dev_port");
208 dev_port
= strtol(attr
, NULL
, 10);
210 /* compose a name based on the raw kernel's PCI bus, slot numbers */
212 l
= sizeof(names
->pci_path
);
214 l
= strpcpyf(&s
, l
, "P%u", domain
);
215 l
= strpcpyf(&s
, l
, "p%us%u", bus
, slot
);
216 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
217 l
= strpcpyf(&s
, l
, "f%u", func
);
219 l
= strpcpyf(&s
, l
, "d%u", dev_port
);
221 names
->pci_path
[0] = '\0';
223 /* ACPI _SUN -- slot user number */
224 pci
= udev_device_new_from_subsystem_sysname(udev
, "subsystem", "pci");
229 snprintf(slots
, sizeof(slots
), "%s/slots", udev_device_get_syspath(pci
));
230 dir
= opendir(slots
);
236 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
241 if (dent
->d_name
[0] == '.')
243 i
= strtol(dent
->d_name
, &rest
, 10);
248 snprintf(str
, sizeof(str
), "%s/%s/address", slots
, dent
->d_name
);
249 if (read_one_line_file(str
, &address
) >= 0) {
250 /* match slot address with device by stripping the function */
251 if (strneq(address
, udev_device_get_sysname(names
->pcidev
), strlen(address
)))
256 if (hotplug_slot
> 0)
260 if (hotplug_slot
> 0) {
262 l
= sizeof(names
->pci_slot
);
264 l
= strpcpyf(&s
, l
, "P%d", domain
);
265 l
= strpcpyf(&s
, l
, "s%d", hotplug_slot
);
266 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
267 l
= strpcpyf(&s
, l
, "f%d", func
);
269 l
= strpcpyf(&s
, l
, "d%d", dev_port
);
271 names
->pci_slot
[0] = '\0';
274 udev_device_unref(pci
);
278 static int names_pci(struct udev_device
*dev
, struct netnames
*names
) {
279 struct udev_device
*parent
;
284 parent
= udev_device_get_parent(dev
);
286 /* there can only ever be one virtio bus per parent device, so we can
287 safely ignore any virtio buses. see
288 <http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html> */
289 while (parent
&& streq_ptr("virtio", udev_device_get_subsystem(parent
)))
290 parent
= udev_device_get_parent(parent
);
295 /* check if our direct parent is a PCI device with no other bus in-between */
296 if (streq_ptr("pci", udev_device_get_subsystem(parent
))) {
297 names
->type
= NET_PCI
;
298 names
->pcidev
= parent
;
300 names
->pcidev
= udev_device_get_parent_with_subsystem_devtype(dev
, "pci", NULL
);
304 dev_pci_onboard(dev
, names
);
305 dev_pci_slot(dev
, names
);
309 static int names_usb(struct udev_device
*dev
, struct netnames
*names
) {
310 struct udev_device
*usbdev
;
321 usbdev
= udev_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface");
325 /* get USB port number chain, configuration, interface */
326 strscpy(name
, sizeof(name
), udev_device_get_sysname(usbdev
));
327 s
= strchr(name
, '-');
332 s
= strchr(ports
, ':');
338 s
= strchr(config
, '.');
344 /* prefix every port number in the chain with "u" */
346 while ((s
= strchr(s
, '.')))
348 s
= names
->usb_ports
;
349 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "u", ports
, NULL
);
351 /* append USB config number, suppress the common config == 1 */
352 if (!streq(config
, "1"))
353 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "c", config
, NULL
);
355 /* append USB interface number, suppress the interface == 0 */
356 if (!streq(interf
, "0"))
357 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "i", interf
, NULL
);
359 return -ENAMETOOLONG
;
361 names
->type
= NET_USB
;
365 static int names_bcma(struct udev_device
*dev
, struct netnames
*names
) {
366 struct udev_device
*bcmadev
;
372 bcmadev
= udev_device_get_parent_with_subsystem_devtype(dev
, "bcma", NULL
);
376 /* bus num:core num */
377 if (sscanf(udev_device_get_sysname(bcmadev
), "bcma%*u:%u", &core
) != 1)
379 /* suppress the common core == 0 */
381 snprintf(names
->bcma_core
, sizeof(names
->bcma_core
), "b%u", core
);
383 names
->type
= NET_BCMA
;
387 static int names_ccw(struct udev_device
*dev
, struct netnames
*names
) {
388 struct udev_device
*cdev
;
396 /* Retrieve the associated CCW device */
397 cdev
= udev_device_get_parent(dev
);
401 /* Network devices are always grouped CCW devices */
402 if (!streq_ptr("ccwgroup", udev_device_get_subsystem(cdev
)))
405 /* Retrieve bus-ID of the grouped CCW device. The bus-ID uniquely
406 * identifies the network device on the Linux on System z channel
407 * subsystem. Note that the bus-ID contains lowercase characters.
409 bus_id
= udev_device_get_sysname(cdev
);
413 /* Check the length of the bus-ID. Rely on that the kernel provides
414 * a correct bus-ID; alternatively, improve this check and parse and
415 * verify each bus-ID part...
417 bus_id_len
= strlen(bus_id
);
418 if (!bus_id_len
|| bus_id_len
< 8 || bus_id_len
> 9)
421 /* Store the CCW bus-ID for use as network device name */
422 rc
= snprintf(names
->ccw_group
, sizeof(names
->ccw_group
), "ccw%s", bus_id
);
423 if (rc
>= 0 && rc
< (int)sizeof(names
->ccw_group
))
424 names
->type
= NET_CCWGROUP
;
428 static int names_mac(struct udev_device
*dev
, struct netnames
*names
) {
431 unsigned int a1
, a2
, a3
, a4
, a5
, a6
;
433 /* check for NET_ADDR_PERM, skip random MAC addresses */
434 s
= udev_device_get_sysattr_value(dev
, "addr_assign_type");
437 i
= strtoul(s
, NULL
, 0);
441 s
= udev_device_get_sysattr_value(dev
, "address");
444 if (sscanf(s
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6)
447 /* skip empty MAC addresses */
448 if (a1
+ a2
+ a3
+ a4
+ a5
+ a6
== 0)
457 names
->mac_valid
= true;
461 /* IEEE Organizationally Unique Identifier vendor string */
462 static int ieee_oui(struct udev_device
*dev
, struct netnames
*names
, bool test
) {
465 if (!names
->mac_valid
)
467 /* skip commonly misused 00:00:00 (Xerox) prefix */
468 if (memcmp(names
->mac
, "\0\0\0", 3) == 0)
470 snprintf(str
, sizeof(str
), "OUI:%02X%02X%02X%02X%02X%02X",
471 names
->mac
[0], names
->mac
[1], names
->mac
[2],
472 names
->mac
[3], names
->mac
[4], names
->mac
[5]);
473 udev_builtin_hwdb_lookup(dev
, NULL
, str
, NULL
, test
);
477 static int builtin_net_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
482 const char *prefix
= "en";
483 struct netnames names
= {};
486 /* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */
487 s
= udev_device_get_sysattr_value(dev
, "type");
490 i
= strtoul(s
, NULL
, 0);
502 /* skip stacked devices, like VLANs, ... */
503 s
= udev_device_get_sysattr_value(dev
, "ifindex");
506 p
= udev_device_get_sysattr_value(dev
, "iflink");
512 devtype
= udev_device_get_devtype(dev
);
514 if (streq("wlan", devtype
))
516 else if (streq("wwan", devtype
))
520 err
= names_mac(dev
, &names
);
521 if (err
>= 0 && names
.mac_valid
) {
524 snprintf(str
, sizeof(str
), "%sx%02x%02x%02x%02x%02x%02x", prefix
,
525 names
.mac
[0], names
.mac
[1], names
.mac
[2],
526 names
.mac
[3], names
.mac
[4], names
.mac
[5]);
527 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_MAC", str
);
529 ieee_oui(dev
, &names
, test
);
532 /* get path names for Linux on System z network devices */
533 err
= names_ccw(dev
, &names
);
534 if (err
>= 0 && names
.type
== NET_CCWGROUP
) {
537 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.ccw_group
) < (int)sizeof(str
))
538 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
542 /* get PCI based path names, we compose only PCI based paths */
543 err
= names_pci(dev
, &names
);
547 /* plain PCI device */
548 if (names
.type
== NET_PCI
) {
551 if (names
.pci_onboard
[0])
552 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_onboard
) < (int)sizeof(str
))
553 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_ONBOARD", str
);
555 if (names
.pci_onboard_label
)
556 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_onboard_label
) < (int)sizeof(str
))
557 udev_builtin_add_property(dev
, test
, "ID_NET_LABEL_ONBOARD", str
);
559 if (names
.pci_path
[0])
560 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_path
) < (int)sizeof(str
))
561 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
563 if (names
.pci_slot
[0])
564 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_slot
) < (int)sizeof(str
))
565 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
570 err
= names_usb(dev
, &names
);
571 if (err
>= 0 && names
.type
== NET_USB
) {
574 if (names
.pci_path
[0])
575 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_path
, names
.usb_ports
) < (int)sizeof(str
))
576 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
578 if (names
.pci_slot
[0])
579 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_slot
, names
.usb_ports
) < (int)sizeof(str
))
580 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
585 err
= names_bcma(dev
, &names
);
586 if (err
>= 0 && names
.type
== NET_BCMA
) {
589 if (names
.pci_path
[0])
590 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_path
, names
.bcma_core
) < (int)sizeof(str
))
591 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
593 if (names
.pci_slot
[0])
594 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_slot
, names
.bcma_core
) < (int)sizeof(str
))
595 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
602 const struct udev_builtin udev_builtin_net_id
= {
604 .cmd
= builtin_net_id
,
605 .help
= "Network device properties",