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 * c<bus_id> — bus id of a grouped CCW or CCW device,
38 * with all leading zeros stripped [s390]
39 * o<index>[n<phys_port_name>|d<dev_port>]
40 * — on-board device index number
41 * s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
42 * — hotplug slot index number
43 * x<MAC> — MAC address
44 * [P<domain>]p<bus>s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
45 * — PCI geographical location
46 * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
47 * — USB port number chain
48 * v<slot> - VIO slot number (IBM PowerVM)
50 * All multi-function PCI devices will carry the [f<function>] number in the
51 * device name, including the function 0 device.
53 * When using PCI geography, The PCI domain is only prepended when it is not 0.
55 * For USB devices the full chain of port numbers of hubs is composed. If the
56 * name gets longer than the maximum number of 15 characters, the name is not
58 * The usual USB configuration == 1 and interface == 0 values are suppressed.
60 * PCI Ethernet card with firmware index "1":
61 * ID_NET_NAME_ONBOARD=eno1
62 * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
64 * PCI Ethernet card in hotplug slot with firmware index number:
65 * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
66 * ID_NET_NAME_MAC=enx000000000466
67 * ID_NET_NAME_PATH=enp5s0
68 * ID_NET_NAME_SLOT=ens1
70 * PCI Ethernet multi-function card with 2 ports:
71 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0/net/enp2s0f0
72 * ID_NET_NAME_MAC=enx78e7d1ea46da
73 * ID_NET_NAME_PATH=enp2s0f0
74 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.1/net/enp2s0f1
75 * ID_NET_NAME_MAC=enx78e7d1ea46dc
76 * ID_NET_NAME_PATH=enp2s0f1
79 * /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
80 * ID_NET_NAME_MAC=wlx0024d7e31130
81 * ID_NET_NAME_PATH=wlp3s0
83 * USB built-in 3G modem:
84 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
85 * ID_NET_NAME_MAC=wwx028037ec0200
86 * ID_NET_NAME_PATH=wwp0s29u1u4i6
89 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
90 * ID_NET_NAME_MAC=enxd626b3450fb5
91 * ID_NET_NAME_PATH=enp0s29u1u2
93 * s390 grouped CCW interface:
94 * /sys/devices/css0/0.0.0007/0.0.f5f0/group_device/net/encf5f0
95 * ID_NET_NAME_MAC=enx026d3c00000a
96 * ID_NET_NAME_PATH=encf5f0
102 #include <net/if_arp.h>
108 #include <linux/pci_regs.h>
110 #include "dirent-util.h"
113 #include "stdio-util.h"
114 #include "string-util.h"
117 #define ONBOARD_INDEX_MAX (16*1024-1)
130 enum netname_type type
;
135 struct udev_device
*pcidev
;
136 char pci_slot
[IFNAMSIZ
];
137 char pci_path
[IFNAMSIZ
];
138 char pci_onboard
[IFNAMSIZ
];
139 const char *pci_onboard_label
;
141 char usb_ports
[IFNAMSIZ
];
142 char bcma_core
[IFNAMSIZ
];
143 char ccw_busid
[IFNAMSIZ
];
144 char vio_slot
[IFNAMSIZ
];
147 /* skip intermediate virtio devices */
148 static struct udev_device
*skip_virtio(struct udev_device
*dev
) {
149 struct udev_device
*parent
= dev
;
151 /* there can only ever be one virtio bus per parent device, so we can
152 safely ignore any virtio buses. see
153 <http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html> */
154 while (parent
&& streq_ptr("virtio", udev_device_get_subsystem(parent
)))
155 parent
= udev_device_get_parent(parent
);
159 /* retrieve on-board index number and label from firmware */
160 static int dev_pci_onboard(struct udev_device
*dev
, struct netnames
*names
) {
161 unsigned dev_port
= 0;
164 const char *attr
, *port_name
;
167 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
168 attr
= udev_device_get_sysattr_value(names
->pcidev
, "acpi_index");
169 /* SMBIOS type 41 — Onboard Devices Extended Information */
171 attr
= udev_device_get_sysattr_value(names
->pcidev
, "index");
175 idx
= strtoul(attr
, NULL
, 0);
179 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
180 * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
181 * cut-off, which is somewhere beyond the realistic number of physical network interface a system might
182 * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
183 if (idx
> ONBOARD_INDEX_MAX
)
186 /* kernel provided port index for multiple ports on a single PCI function */
187 attr
= udev_device_get_sysattr_value(dev
, "dev_port");
189 dev_port
= strtol(attr
, NULL
, 10);
191 /* kernel provided front panel port name for multiple port PCI device */
192 port_name
= udev_device_get_sysattr_value(dev
, "phys_port_name");
194 s
= names
->pci_onboard
;
195 l
= sizeof(names
->pci_onboard
);
196 l
= strpcpyf(&s
, l
, "o%d", idx
);
198 l
= strpcpyf(&s
, l
, "n%s", port_name
);
199 else if (dev_port
> 0)
200 l
= strpcpyf(&s
, l
, "d%d", dev_port
);
202 names
->pci_onboard
[0] = '\0';
204 names
->pci_onboard_label
= udev_device_get_sysattr_value(names
->pcidev
, "label");
209 /* read the 256 bytes PCI configuration space to check the multi-function bit */
210 static bool is_pci_multifunction(struct udev_device
*dev
) {
211 _cleanup_close_
int fd
= -1;
212 const char *filename
;
215 filename
= strjoina(udev_device_get_syspath(dev
), "/config");
216 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
219 if (read(fd
, &config
, sizeof(config
)) != sizeof(config
))
222 /* bit 0-6 header type, bit 7 multi/single function device */
223 if ((config
[PCI_HEADER_TYPE
] & 0x80) != 0)
229 static int dev_pci_slot(struct udev_device
*dev
, struct netnames
*names
) {
230 struct udev
*udev
= udev_device_get_udev(names
->pcidev
);
231 unsigned domain
, bus
, slot
, func
, dev_port
= 0;
234 const char *attr
, *port_name
;
235 struct udev_device
*pci
= NULL
;
236 char slots
[PATH_MAX
];
237 _cleanup_closedir_
DIR *dir
= NULL
;
239 int hotplug_slot
= 0, err
= 0;
241 if (sscanf(udev_device_get_sysname(names
->pcidev
), "%x:%x:%x.%u", &domain
, &bus
, &slot
, &func
) != 4)
244 /* kernel provided port index for multiple ports on a single PCI function */
245 attr
= udev_device_get_sysattr_value(dev
, "dev_port");
247 dev_port
= strtol(attr
, NULL
, 10);
249 /* kernel provided front panel port name for multiple port PCI device */
250 port_name
= udev_device_get_sysattr_value(dev
, "phys_port_name");
252 /* compose a name based on the raw kernel's PCI bus, slot numbers */
254 l
= sizeof(names
->pci_path
);
256 l
= strpcpyf(&s
, l
, "P%u", domain
);
257 l
= strpcpyf(&s
, l
, "p%us%u", bus
, slot
);
258 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
259 l
= strpcpyf(&s
, l
, "f%u", func
);
261 l
= strpcpyf(&s
, l
, "n%s", port_name
);
262 else if (dev_port
> 0)
263 l
= strpcpyf(&s
, l
, "d%u", dev_port
);
265 names
->pci_path
[0] = '\0';
267 /* ACPI _SUN — slot user number */
268 pci
= udev_device_new_from_subsystem_sysname(udev
, "subsystem", "pci");
274 snprintf(slots
, sizeof slots
, "%s/slots", udev_device_get_syspath(pci
));
275 dir
= opendir(slots
);
281 FOREACH_DIRENT_ALL(dent
, dir
, break) {
283 char *rest
, *address
, str
[PATH_MAX
];
285 if (dent
->d_name
[0] == '.')
287 i
= strtol(dent
->d_name
, &rest
, 10);
293 snprintf(str
, sizeof str
, "%s/%s/address", slots
, dent
->d_name
);
294 if (read_one_line_file(str
, &address
) >= 0) {
295 /* match slot address with device by stripping the function */
296 if (strneq(address
, udev_device_get_sysname(names
->pcidev
), strlen(address
)))
301 if (hotplug_slot
> 0)
305 if (hotplug_slot
> 0) {
307 l
= sizeof(names
->pci_slot
);
309 l
= strpcpyf(&s
, l
, "P%d", domain
);
310 l
= strpcpyf(&s
, l
, "s%d", hotplug_slot
);
311 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
312 l
= strpcpyf(&s
, l
, "f%d", func
);
314 l
= strpcpyf(&s
, l
, "n%s", port_name
);
315 else if (dev_port
> 0)
316 l
= strpcpyf(&s
, l
, "d%d", dev_port
);
318 names
->pci_slot
[0] = '\0';
321 udev_device_unref(pci
);
325 static int names_vio(struct udev_device
*dev
, struct netnames
*names
) {
326 struct udev_device
*parent
;
327 unsigned busid
, slotid
, ethid
;
330 /* check if our direct parent is a VIO device with no other bus in-between */
331 parent
= udev_device_get_parent(dev
);
335 if (!streq_ptr("vio", udev_device_get_subsystem(parent
)))
338 /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
339 * selected in the HMC), thus this provides a reliable naming (e.g.
340 * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
341 * there should only ever be one bus, and then remove leading zeros. */
342 syspath
= udev_device_get_syspath(dev
);
344 if (sscanf(syspath
, "/sys/devices/vio/%4x%4x/net/eth%u", &busid
, &slotid
, ðid
) != 3)
347 xsprintf(names
->vio_slot
, "v%u", slotid
);
348 names
->type
= NET_VIO
;
352 static int names_pci(struct udev_device
*dev
, struct netnames
*names
) {
353 struct udev_device
*parent
;
358 parent
= udev_device_get_parent(dev
);
359 /* skip virtio subsystem if present */
360 parent
= skip_virtio(parent
);
365 /* check if our direct parent is a PCI device with no other bus in-between */
366 if (streq_ptr("pci", udev_device_get_subsystem(parent
))) {
367 names
->type
= NET_PCI
;
368 names
->pcidev
= parent
;
370 names
->pcidev
= udev_device_get_parent_with_subsystem_devtype(dev
, "pci", NULL
);
374 dev_pci_onboard(dev
, names
);
375 dev_pci_slot(dev
, names
);
379 static int names_usb(struct udev_device
*dev
, struct netnames
*names
) {
380 struct udev_device
*usbdev
;
391 usbdev
= udev_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface");
395 /* get USB port number chain, configuration, interface */
396 strscpy(name
, sizeof(name
), udev_device_get_sysname(usbdev
));
397 s
= strchr(name
, '-');
402 s
= strchr(ports
, ':');
408 s
= strchr(config
, '.');
414 /* prefix every port number in the chain with "u" */
416 while ((s
= strchr(s
, '.')))
418 s
= names
->usb_ports
;
419 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "u", ports
, NULL
);
421 /* append USB config number, suppress the common config == 1 */
422 if (!streq(config
, "1"))
423 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "c", config
, NULL
);
425 /* append USB interface number, suppress the interface == 0 */
426 if (!streq(interf
, "0"))
427 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "i", interf
, NULL
);
429 return -ENAMETOOLONG
;
431 names
->type
= NET_USB
;
435 static int names_bcma(struct udev_device
*dev
, struct netnames
*names
) {
436 struct udev_device
*bcmadev
;
442 bcmadev
= udev_device_get_parent_with_subsystem_devtype(dev
, "bcma", NULL
);
446 /* bus num:core num */
447 if (sscanf(udev_device_get_sysname(bcmadev
), "bcma%*u:%u", &core
) != 1)
449 /* suppress the common core == 0 */
451 xsprintf(names
->bcma_core
, "b%u", core
);
453 names
->type
= NET_BCMA
;
457 static int names_ccw(struct udev_device
*dev
, struct netnames
*names
) {
458 struct udev_device
*cdev
;
459 const char *bus_id
, *subsys
;
467 /* Retrieve the associated CCW device */
468 cdev
= udev_device_get_parent(dev
);
469 /* skip virtio subsystem if present */
470 cdev
= skip_virtio(cdev
);
474 /* Network devices are either single or grouped CCW devices */
475 subsys
= udev_device_get_subsystem(cdev
);
476 if (!STRPTR_IN_SET(subsys
, "ccwgroup", "ccw"))
479 /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
480 * identifies the network device on the Linux on System z channel
481 * subsystem. Note that the bus-ID contains lowercase characters.
483 bus_id
= udev_device_get_sysname(cdev
);
487 /* Check the length of the bus-ID. Rely on that the kernel provides
488 * a correct bus-ID; alternatively, improve this check and parse and
489 * verify each bus-ID part...
491 bus_id_len
= strlen(bus_id
);
492 if (!bus_id_len
|| bus_id_len
< 8 || bus_id_len
> 9)
495 /* Strip leading zeros from the bus id for aesthetic purposes. This
496 * keeps the ccw names stable, yet much shorter in general case of
497 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
498 * not prepended when it is zero. Preserve the last 0 for 0.0.0000.
500 bus_id_start
= strspn(bus_id
, ".0");
501 bus_id
+= bus_id_start
< bus_id_len
? bus_id_start
: bus_id_len
- 1;
503 /* Store the CCW bus-ID for use as network device name */
504 rc
= snprintf(names
->ccw_busid
, sizeof(names
->ccw_busid
), "c%s", bus_id
);
505 if (rc
>= 0 && rc
< (int)sizeof(names
->ccw_busid
))
506 names
->type
= NET_CCW
;
510 static int names_mac(struct udev_device
*dev
, struct netnames
*names
) {
513 unsigned int a1
, a2
, a3
, a4
, a5
, a6
;
515 /* check for NET_ADDR_PERM, skip random MAC addresses */
516 s
= udev_device_get_sysattr_value(dev
, "addr_assign_type");
519 i
= strtoul(s
, NULL
, 0);
523 s
= udev_device_get_sysattr_value(dev
, "address");
526 if (sscanf(s
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6)
529 /* skip empty MAC addresses */
530 if (a1
+ a2
+ a3
+ a4
+ a5
+ a6
== 0)
539 names
->mac_valid
= true;
543 /* IEEE Organizationally Unique Identifier vendor string */
544 static int ieee_oui(struct udev_device
*dev
, struct netnames
*names
, bool test
) {
547 if (!names
->mac_valid
)
549 /* skip commonly misused 00:00:00 (Xerox) prefix */
550 if (memcmp(names
->mac
, "\0\0\0", 3) == 0)
552 xsprintf(str
, "OUI:%02X%02X%02X%02X%02X%02X", names
->mac
[0],
553 names
->mac
[1], names
->mac
[2], names
->mac
[3], names
->mac
[4],
555 udev_builtin_hwdb_lookup(dev
, NULL
, str
, NULL
, test
);
559 static int builtin_net_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
564 const char *prefix
= "en";
565 struct netnames names
= {};
568 /* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */
569 s
= udev_device_get_sysattr_value(dev
, "type");
572 i
= strtoul(s
, NULL
, 0);
584 /* skip stacked devices, like VLANs, ... */
585 s
= udev_device_get_sysattr_value(dev
, "ifindex");
588 p
= udev_device_get_sysattr_value(dev
, "iflink");
594 devtype
= udev_device_get_devtype(dev
);
596 if (streq("wlan", devtype
))
598 else if (streq("wwan", devtype
))
602 err
= names_mac(dev
, &names
);
603 if (err
>= 0 && names
.mac_valid
) {
606 xsprintf(str
, "%sx%02x%02x%02x%02x%02x%02x", prefix
,
607 names
.mac
[0], names
.mac
[1], names
.mac
[2],
608 names
.mac
[3], names
.mac
[4], names
.mac
[5]);
609 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_MAC", str
);
611 ieee_oui(dev
, &names
, test
);
614 /* get path names for Linux on System z network devices */
615 err
= names_ccw(dev
, &names
);
616 if (err
>= 0 && names
.type
== NET_CCW
) {
619 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.ccw_busid
) < (int)sizeof(str
))
620 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
624 /* get ibmveth/ibmvnic slot-based names. */
625 err
= names_vio(dev
, &names
);
626 if (err
>= 0 && names
.type
== NET_VIO
) {
629 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.vio_slot
) < (int)sizeof(str
))
630 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
634 /* get PCI based path names, we compose only PCI based paths */
635 err
= names_pci(dev
, &names
);
639 /* plain PCI device */
640 if (names
.type
== NET_PCI
) {
643 if (names
.pci_onboard
[0])
644 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_onboard
) < (int)sizeof(str
))
645 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_ONBOARD", str
);
647 if (names
.pci_onboard_label
)
648 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_onboard_label
) < (int)sizeof(str
))
649 udev_builtin_add_property(dev
, test
, "ID_NET_LABEL_ONBOARD", str
);
651 if (names
.pci_path
[0])
652 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_path
) < (int)sizeof(str
))
653 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
655 if (names
.pci_slot
[0])
656 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_slot
) < (int)sizeof(str
))
657 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
662 err
= names_usb(dev
, &names
);
663 if (err
>= 0 && names
.type
== NET_USB
) {
666 if (names
.pci_path
[0])
667 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_path
, names
.usb_ports
) < (int)sizeof(str
))
668 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
670 if (names
.pci_slot
[0])
671 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_slot
, names
.usb_ports
) < (int)sizeof(str
))
672 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
677 err
= names_bcma(dev
, &names
);
678 if (err
>= 0 && names
.type
== NET_BCMA
) {
681 if (names
.pci_path
[0])
682 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_path
, names
.bcma_core
) < (int)sizeof(str
))
683 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
685 if (names
.pci_slot
[0])
686 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_slot
, names
.bcma_core
) < (int)sizeof(str
))
687 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
694 const struct udev_builtin udev_builtin_net_id
= {
696 .cmd
= builtin_net_id
,
697 .help
= "Network device properties",