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> — CCW bus group name, without leading zeros [s390]
38 * o<index>[n<phys_port_name>|d<dev_port>]
39 * — on-board device index number
40 * s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
41 * — hotplug slot index number
42 * x<MAC> — MAC address
43 * [P<domain>]p<bus>s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
44 * — PCI geographical location
45 * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
46 * — USB port number chain
48 * All multi-function PCI devices will carry the [f<function>] number in the
49 * device name, including the function 0 device.
51 * When using PCI geography, The PCI domain is only prepended when it is not 0.
53 * For USB devices the full chain of port numbers of hubs is composed. If the
54 * name gets longer than the maximum number of 15 characters, the name is not
56 * The usual USB configuration == 1 and interface == 0 values are suppressed.
58 * PCI Ethernet card with firmware index "1":
59 * ID_NET_NAME_ONBOARD=eno1
60 * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
62 * PCI Ethernet card in hotplug slot with firmware index number:
63 * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
64 * ID_NET_NAME_MAC=enx000000000466
65 * ID_NET_NAME_PATH=enp5s0
66 * ID_NET_NAME_SLOT=ens1
68 * PCI Ethernet multi-function card with 2 ports:
69 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0/net/enp2s0f0
70 * ID_NET_NAME_MAC=enx78e7d1ea46da
71 * ID_NET_NAME_PATH=enp2s0f0
72 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.1/net/enp2s0f1
73 * ID_NET_NAME_MAC=enx78e7d1ea46dc
74 * ID_NET_NAME_PATH=enp2s0f1
77 * /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
78 * ID_NET_NAME_MAC=wlx0024d7e31130
79 * ID_NET_NAME_PATH=wlp3s0
81 * USB built-in 3G modem:
82 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
83 * ID_NET_NAME_MAC=wwx028037ec0200
84 * ID_NET_NAME_PATH=wwp0s29u1u4i6
87 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
88 * ID_NET_NAME_MAC=enxd626b3450fb5
89 * ID_NET_NAME_PATH=enp0s29u1u2
95 #include <net/if_arp.h>
101 #include <linux/pci_regs.h>
103 #include "dirent-util.h"
106 #include "stdio-util.h"
107 #include "string-util.h"
110 #define ONBOARD_INDEX_MAX (16*1024-1)
122 enum netname_type type
;
127 struct udev_device
*pcidev
;
128 char pci_slot
[IFNAMSIZ
];
129 char pci_path
[IFNAMSIZ
];
130 char pci_onboard
[IFNAMSIZ
];
131 const char *pci_onboard_label
;
133 char usb_ports
[IFNAMSIZ
];
134 char bcma_core
[IFNAMSIZ
];
135 char ccw_group
[IFNAMSIZ
];
138 /* retrieve on-board index number and label from firmware */
139 static int dev_pci_onboard(struct udev_device
*dev
, struct netnames
*names
) {
140 unsigned dev_port
= 0;
143 const char *attr
, *port_name
;
146 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
147 attr
= udev_device_get_sysattr_value(names
->pcidev
, "acpi_index");
148 /* SMBIOS type 41 — Onboard Devices Extended Information */
150 attr
= udev_device_get_sysattr_value(names
->pcidev
, "index");
154 idx
= strtoul(attr
, NULL
, 0);
158 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
159 * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
160 * cut-off, which is somewhere beyond the realistic number of physical network interface a system might
161 * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
162 if (idx
> ONBOARD_INDEX_MAX
)
165 /* kernel provided port index for multiple ports on a single PCI function */
166 attr
= udev_device_get_sysattr_value(dev
, "dev_port");
168 dev_port
= strtol(attr
, NULL
, 10);
170 /* kernel provided front panel port name for multiple port PCI device */
171 port_name
= udev_device_get_sysattr_value(dev
, "phys_port_name");
173 s
= names
->pci_onboard
;
174 l
= sizeof(names
->pci_onboard
);
175 l
= strpcpyf(&s
, l
, "o%d", idx
);
177 l
= strpcpyf(&s
, l
, "n%s", port_name
);
178 else if (dev_port
> 0)
179 l
= strpcpyf(&s
, l
, "d%d", dev_port
);
181 names
->pci_onboard
[0] = '\0';
183 names
->pci_onboard_label
= udev_device_get_sysattr_value(names
->pcidev
, "label");
188 /* read the 256 bytes PCI configuration space to check the multi-function bit */
189 static bool is_pci_multifunction(struct udev_device
*dev
) {
190 _cleanup_close_
int fd
= -1;
191 const char *filename
;
194 filename
= strjoina(udev_device_get_syspath(dev
), "/config");
195 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
198 if (read(fd
, &config
, sizeof(config
)) != sizeof(config
))
201 /* bit 0-6 header type, bit 7 multi/single function device */
202 if ((config
[PCI_HEADER_TYPE
] & 0x80) != 0)
208 static int dev_pci_slot(struct udev_device
*dev
, struct netnames
*names
) {
209 struct udev
*udev
= udev_device_get_udev(names
->pcidev
);
210 unsigned domain
, bus
, slot
, func
, dev_port
= 0;
213 const char *attr
, *port_name
;
214 struct udev_device
*pci
= NULL
;
215 char slots
[PATH_MAX
];
216 _cleanup_closedir_
DIR *dir
= NULL
;
218 int hotplug_slot
= 0, err
= 0;
220 if (sscanf(udev_device_get_sysname(names
->pcidev
), "%x:%x:%x.%u", &domain
, &bus
, &slot
, &func
) != 4)
223 /* kernel provided port index for multiple ports on a single PCI function */
224 attr
= udev_device_get_sysattr_value(dev
, "dev_port");
226 dev_port
= strtol(attr
, NULL
, 10);
228 /* kernel provided front panel port name for multiple port PCI device */
229 port_name
= udev_device_get_sysattr_value(dev
, "phys_port_name");
231 /* compose a name based on the raw kernel's PCI bus, slot numbers */
233 l
= sizeof(names
->pci_path
);
235 l
= strpcpyf(&s
, l
, "P%u", domain
);
236 l
= strpcpyf(&s
, l
, "p%us%u", bus
, slot
);
237 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
238 l
= strpcpyf(&s
, l
, "f%u", func
);
240 l
= strpcpyf(&s
, l
, "n%s", port_name
);
241 else if (dev_port
> 0)
242 l
= strpcpyf(&s
, l
, "d%u", dev_port
);
244 names
->pci_path
[0] = '\0';
246 /* ACPI _SUN — slot user number */
247 pci
= udev_device_new_from_subsystem_sysname(udev
, "subsystem", "pci");
253 snprintf(slots
, sizeof slots
, "%s/slots", udev_device_get_syspath(pci
));
254 dir
= opendir(slots
);
260 FOREACH_DIRENT_ALL(dent
, dir
, break) {
262 char *rest
, *address
, str
[PATH_MAX
];
264 if (dent
->d_name
[0] == '.')
266 i
= strtol(dent
->d_name
, &rest
, 10);
272 snprintf(str
, sizeof str
, "%s/%s/address", slots
, dent
->d_name
);
273 if (read_one_line_file(str
, &address
) >= 0) {
274 /* match slot address with device by stripping the function */
275 if (strneq(address
, udev_device_get_sysname(names
->pcidev
), strlen(address
)))
280 if (hotplug_slot
> 0)
284 if (hotplug_slot
> 0) {
286 l
= sizeof(names
->pci_slot
);
288 l
= strpcpyf(&s
, l
, "P%d", domain
);
289 l
= strpcpyf(&s
, l
, "s%d", hotplug_slot
);
290 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
291 l
= strpcpyf(&s
, l
, "f%d", func
);
293 l
= strpcpyf(&s
, l
, "n%s", port_name
);
294 else if (dev_port
> 0)
295 l
= strpcpyf(&s
, l
, "d%d", dev_port
);
297 names
->pci_slot
[0] = '\0';
300 udev_device_unref(pci
);
304 static int names_pci(struct udev_device
*dev
, struct netnames
*names
) {
305 struct udev_device
*parent
;
310 parent
= udev_device_get_parent(dev
);
312 /* there can only ever be one virtio bus per parent device, so we can
313 safely ignore any virtio buses. see
314 <http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html> */
315 while (parent
&& streq_ptr("virtio", udev_device_get_subsystem(parent
)))
316 parent
= udev_device_get_parent(parent
);
321 /* check if our direct parent is a PCI device with no other bus in-between */
322 if (streq_ptr("pci", udev_device_get_subsystem(parent
))) {
323 names
->type
= NET_PCI
;
324 names
->pcidev
= parent
;
326 names
->pcidev
= udev_device_get_parent_with_subsystem_devtype(dev
, "pci", NULL
);
330 dev_pci_onboard(dev
, names
);
331 dev_pci_slot(dev
, names
);
335 static int names_usb(struct udev_device
*dev
, struct netnames
*names
) {
336 struct udev_device
*usbdev
;
347 usbdev
= udev_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface");
351 /* get USB port number chain, configuration, interface */
352 strscpy(name
, sizeof(name
), udev_device_get_sysname(usbdev
));
353 s
= strchr(name
, '-');
358 s
= strchr(ports
, ':');
364 s
= strchr(config
, '.');
370 /* prefix every port number in the chain with "u" */
372 while ((s
= strchr(s
, '.')))
374 s
= names
->usb_ports
;
375 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "u", ports
, NULL
);
377 /* append USB config number, suppress the common config == 1 */
378 if (!streq(config
, "1"))
379 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "c", config
, NULL
);
381 /* append USB interface number, suppress the interface == 0 */
382 if (!streq(interf
, "0"))
383 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "i", interf
, NULL
);
385 return -ENAMETOOLONG
;
387 names
->type
= NET_USB
;
391 static int names_bcma(struct udev_device
*dev
, struct netnames
*names
) {
392 struct udev_device
*bcmadev
;
398 bcmadev
= udev_device_get_parent_with_subsystem_devtype(dev
, "bcma", NULL
);
402 /* bus num:core num */
403 if (sscanf(udev_device_get_sysname(bcmadev
), "bcma%*u:%u", &core
) != 1)
405 /* suppress the common core == 0 */
407 xsprintf(names
->bcma_core
, "b%u", core
);
409 names
->type
= NET_BCMA
;
413 static int names_ccw(struct udev_device
*dev
, struct netnames
*names
) {
414 struct udev_device
*cdev
;
422 /* Retrieve the associated CCW device */
423 cdev
= udev_device_get_parent(dev
);
427 /* Network devices are always grouped CCW devices */
428 if (!streq_ptr("ccwgroup", udev_device_get_subsystem(cdev
)))
431 /* Retrieve bus-ID of the grouped CCW device. The bus-ID uniquely
432 * identifies the network device on the Linux on System z channel
433 * subsystem. Note that the bus-ID contains lowercase characters.
435 bus_id
= udev_device_get_sysname(cdev
);
439 /* Check the length of the bus-ID. Rely on that the kernel provides
440 * a correct bus-ID; alternatively, improve this check and parse and
441 * verify each bus-ID part...
443 bus_id_len
= strlen(bus_id
);
444 if (!bus_id_len
|| bus_id_len
< 8 || bus_id_len
> 9)
447 /* Strip leading zeros from the bus id for aesthetic purposes. This
448 * keeps the ccw names stable, yet much shorter in general case of
449 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
450 * not prepended when it is zero.
452 bus_id
+= strspn(bus_id
, ".0");
454 /* Store the CCW bus-ID for use as network device name */
455 rc
= snprintf(names
->ccw_group
, sizeof(names
->ccw_group
), "c%s", bus_id
);
456 if (rc
>= 0 && rc
< (int)sizeof(names
->ccw_group
))
457 names
->type
= NET_CCWGROUP
;
461 static int names_mac(struct udev_device
*dev
, struct netnames
*names
) {
464 unsigned int a1
, a2
, a3
, a4
, a5
, a6
;
466 /* check for NET_ADDR_PERM, skip random MAC addresses */
467 s
= udev_device_get_sysattr_value(dev
, "addr_assign_type");
470 i
= strtoul(s
, NULL
, 0);
474 s
= udev_device_get_sysattr_value(dev
, "address");
477 if (sscanf(s
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6)
480 /* skip empty MAC addresses */
481 if (a1
+ a2
+ a3
+ a4
+ a5
+ a6
== 0)
490 names
->mac_valid
= true;
494 /* IEEE Organizationally Unique Identifier vendor string */
495 static int ieee_oui(struct udev_device
*dev
, struct netnames
*names
, bool test
) {
498 if (!names
->mac_valid
)
500 /* skip commonly misused 00:00:00 (Xerox) prefix */
501 if (memcmp(names
->mac
, "\0\0\0", 3) == 0)
503 xsprintf(str
, "OUI:%02X%02X%02X%02X%02X%02X", names
->mac
[0],
504 names
->mac
[1], names
->mac
[2], names
->mac
[3], names
->mac
[4],
506 udev_builtin_hwdb_lookup(dev
, NULL
, str
, NULL
, test
);
510 static int builtin_net_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
515 const char *prefix
= "en";
516 struct netnames names
= {};
519 /* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */
520 s
= udev_device_get_sysattr_value(dev
, "type");
523 i
= strtoul(s
, NULL
, 0);
535 /* skip stacked devices, like VLANs, ... */
536 s
= udev_device_get_sysattr_value(dev
, "ifindex");
539 p
= udev_device_get_sysattr_value(dev
, "iflink");
545 devtype
= udev_device_get_devtype(dev
);
547 if (streq("wlan", devtype
))
549 else if (streq("wwan", devtype
))
553 err
= names_mac(dev
, &names
);
554 if (err
>= 0 && names
.mac_valid
) {
557 xsprintf(str
, "%sx%02x%02x%02x%02x%02x%02x", prefix
,
558 names
.mac
[0], names
.mac
[1], names
.mac
[2],
559 names
.mac
[3], names
.mac
[4], names
.mac
[5]);
560 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_MAC", str
);
562 ieee_oui(dev
, &names
, test
);
565 /* get path names for Linux on System z network devices */
566 err
= names_ccw(dev
, &names
);
567 if (err
>= 0 && names
.type
== NET_CCWGROUP
) {
570 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.ccw_group
) < (int)sizeof(str
))
571 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
575 /* get PCI based path names, we compose only PCI based paths */
576 err
= names_pci(dev
, &names
);
580 /* plain PCI device */
581 if (names
.type
== NET_PCI
) {
584 if (names
.pci_onboard
[0])
585 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_onboard
) < (int)sizeof(str
))
586 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_ONBOARD", str
);
588 if (names
.pci_onboard_label
)
589 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_onboard_label
) < (int)sizeof(str
))
590 udev_builtin_add_property(dev
, test
, "ID_NET_LABEL_ONBOARD", str
);
592 if (names
.pci_path
[0])
593 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_path
) < (int)sizeof(str
))
594 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
596 if (names
.pci_slot
[0])
597 if (snprintf(str
, sizeof(str
), "%s%s", prefix
, names
.pci_slot
) < (int)sizeof(str
))
598 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
603 err
= names_usb(dev
, &names
);
604 if (err
>= 0 && names
.type
== NET_USB
) {
607 if (names
.pci_path
[0])
608 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_path
, names
.usb_ports
) < (int)sizeof(str
))
609 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
611 if (names
.pci_slot
[0])
612 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_slot
, names
.usb_ports
) < (int)sizeof(str
))
613 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
618 err
= names_bcma(dev
, &names
);
619 if (err
>= 0 && names
.type
== NET_BCMA
) {
622 if (names
.pci_path
[0])
623 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_path
, names
.bcma_core
) < (int)sizeof(str
))
624 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
626 if (names
.pci_slot
[0])
627 if (snprintf(str
, sizeof(str
), "%s%s%s", prefix
, names
.pci_slot
, names
.bcma_core
) < (int)sizeof(str
))
628 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
635 const struct udev_builtin udev_builtin_net_id
= {
637 .cmd
= builtin_net_id
,
638 .help
= "Network device properties",