1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 * Predictable network interface device names based on:
5 * - firmware/bios-provided index numbers for on-board devices
6 * - firmware-provided pci-express hotplug slot index number
7 * - physical/geographical location of the hardware
8 * - the interface's MAC address
10 * http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames
12 * Two character prefixes based on the type of interface:
15 * sl — serial line IP (slip)
20 * b<number> — BCMA bus core number
21 * c<bus_id> — bus id of a grouped CCW or CCW device,
22 * with all leading zeros stripped [s390]
23 * o<index>[n<phys_port_name>|d<dev_port>]
24 * — on-board device index number
25 * s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
26 * — hotplug slot index number
27 * x<MAC> — MAC address
28 * [P<domain>]p<bus>s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
29 * — PCI geographical location
30 * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
31 * — USB port number chain
32 * v<slot> - VIO slot number (IBM PowerVM)
33 * a<vendor><model>i<instance> — Platform bus ACPI instance id
35 * All multi-function PCI devices will carry the [f<function>] number in the
36 * device name, including the function 0 device.
38 * SR-IOV virtual devices are named based on the name of the parent interface,
39 * with a suffix of "v<N>", where <N> is the virtual device number.
41 * When using PCI geography, The PCI domain is only prepended when it is not 0.
43 * For USB devices the full chain of port numbers of hubs is composed. If the
44 * name gets longer than the maximum number of 15 characters, the name is not
46 * The usual USB configuration == 1 and interface == 0 values are suppressed.
48 * PCI Ethernet card with firmware index "1":
49 * ID_NET_NAME_ONBOARD=eno1
50 * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
52 * PCI Ethernet card in hotplug slot with firmware index number:
53 * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
54 * ID_NET_NAME_MAC=enx000000000466
55 * ID_NET_NAME_PATH=enp5s0
56 * ID_NET_NAME_SLOT=ens1
58 * PCI Ethernet multi-function card with 2 ports:
59 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0/net/enp2s0f0
60 * ID_NET_NAME_MAC=enx78e7d1ea46da
61 * ID_NET_NAME_PATH=enp2s0f0
62 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.1/net/enp2s0f1
63 * ID_NET_NAME_MAC=enx78e7d1ea46dc
64 * ID_NET_NAME_PATH=enp2s0f1
67 * /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
68 * ID_NET_NAME_MAC=wlx0024d7e31130
69 * ID_NET_NAME_PATH=wlp3s0
71 * PCI IB host adapter with 2 ports:
72 * /sys/devices/pci0000:00/0000:00:03.0/0000:15:00.0/net/ibp21s0f0
73 * ID_NET_NAME_PATH=ibp21s0f0
74 * /sys/devices/pci0000:00/0000:00:03.0/0000:15:00.1/net/ibp21s0f1
75 * ID_NET_NAME_PATH=ibp21s0f1
77 * USB built-in 3G modem:
78 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
79 * ID_NET_NAME_MAC=wwx028037ec0200
80 * ID_NET_NAME_PATH=wwp0s29u1u4i6
83 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
84 * ID_NET_NAME_MAC=enxd626b3450fb5
85 * ID_NET_NAME_PATH=enp0s29u1u2
87 * s390 grouped CCW interface:
88 * /sys/devices/css0/0.0.0007/0.0.f5f0/group_device/net/encf5f0
89 * ID_NET_NAME_MAC=enx026d3c00000a
90 * ID_NET_NAME_PATH=encf5f0
96 #include <net/if_arp.h>
102 #include <linux/pci_regs.h>
104 #include "alloc-util.h"
105 #include "dirent-util.h"
109 #include "parse-util.h"
110 #include "stdio-util.h"
111 #include "string-util.h"
113 #include "strxcpyx.h"
114 #include "udev-builtin.h"
116 #define ONBOARD_INDEX_MAX (16*1024-1)
130 enum netname_type type
;
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
];
145 char platform_path
[IFNAMSIZ
];
149 sd_device
*physfn_pcidev
;
150 char suffix
[IFNAMSIZ
];
153 /* skip intermediate virtio devices */
154 static sd_device
*skip_virtio(sd_device
*dev
) {
157 /* there can only ever be one virtio bus per parent device, so we can
158 * safely ignore any virtio buses. see
159 * http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html */
160 for (parent
= dev
; parent
; ) {
161 const char *subsystem
;
163 if (sd_device_get_subsystem(parent
, &subsystem
) < 0)
166 if (!streq(subsystem
, "virtio"))
169 if (sd_device_get_parent(parent
, &parent
) < 0)
176 static int get_virtfn_info(sd_device
*dev
, struct netnames
*names
, struct virtfn_info
*ret
) {
177 _cleanup_(sd_device_unrefp
) sd_device
*physfn_pcidev
= NULL
;
178 const char *physfn_link_file
, *syspath
;
179 _cleanup_free_
char *physfn_pci_syspath
= NULL
;
180 _cleanup_free_
char *virtfn_pci_syspath
= NULL
;
182 _cleanup_closedir_
DIR *dir
= NULL
;
183 char suffix
[IFNAMSIZ
];
190 r
= sd_device_get_syspath(names
->pcidev
, &syspath
);
194 /* Check if this is a virtual function. */
195 physfn_link_file
= strjoina(syspath
, "/physfn");
196 r
= chase_symlinks(physfn_link_file
, NULL
, 0, &physfn_pci_syspath
);
200 /* Get physical function's pci device. */
201 r
= sd_device_new_from_syspath(&physfn_pcidev
, physfn_pci_syspath
);
205 /* Find the virtual function number by finding the right virtfn link. */
206 dir
= opendir(physfn_pci_syspath
);
210 FOREACH_DIRENT_ALL(dent
, dir
, break) {
211 _cleanup_free_
char *virtfn_link_file
= NULL
;
213 if (!startswith(dent
->d_name
, "virtfn"))
216 virtfn_link_file
= strjoin(physfn_pci_syspath
, "/", dent
->d_name
);
217 if (!virtfn_link_file
)
220 if (chase_symlinks(virtfn_link_file
, NULL
, 0, &virtfn_pci_syspath
) < 0)
223 if (streq(syspath
, virtfn_pci_syspath
)) {
224 if (!snprintf_ok(suffix
, sizeof(suffix
), "v%s", &dent
->d_name
[6]))
233 ret
->physfn_pcidev
= TAKE_PTR(physfn_pcidev
);
234 strncpy(ret
->suffix
, suffix
, sizeof(ret
->suffix
));
239 /* retrieve on-board index number and label from firmware */
240 static int dev_pci_onboard(sd_device
*dev
, struct netnames
*names
) {
241 unsigned long idx
, dev_port
= 0;
242 const char *attr
, *port_name
= NULL
;
247 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
248 if (sd_device_get_sysattr_value(names
->pcidev
, "acpi_index", &attr
) < 0) {
249 /* SMBIOS type 41 — Onboard Devices Extended Information */
250 r
= sd_device_get_sysattr_value(names
->pcidev
, "index", &attr
);
255 r
= safe_atolu(attr
, &idx
);
259 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
260 * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
261 * cut-off, which is somewhere beyond the realistic number of physical network interface a system might
262 * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
263 if (idx
> ONBOARD_INDEX_MAX
)
266 /* kernel provided port index for multiple ports on a single PCI function */
267 if (sd_device_get_sysattr_value(dev
, "dev_port", &attr
) >= 0)
268 dev_port
= strtoul(attr
, NULL
, 10);
270 /* kernel provided front panel port name for multiple port PCI device */
271 (void) sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
273 s
= names
->pci_onboard
;
274 l
= sizeof(names
->pci_onboard
);
275 l
= strpcpyf(&s
, l
, "o%lu", idx
);
277 l
= strpcpyf(&s
, l
, "n%s", port_name
);
278 else if (dev_port
> 0)
279 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
281 names
->pci_onboard
[0] = '\0';
283 if (sd_device_get_sysattr_value(names
->pcidev
, "label", &names
->pci_onboard_label
) < 0)
284 names
->pci_onboard_label
= NULL
;
289 /* read the 256 bytes PCI configuration space to check the multi-function bit */
290 static bool is_pci_multifunction(sd_device
*dev
) {
291 _cleanup_close_
int fd
= -1;
292 const char *filename
, *syspath
;
295 if (sd_device_get_syspath(dev
, &syspath
) < 0)
298 filename
= strjoina(syspath
, "/config");
299 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
302 if (read(fd
, &config
, sizeof(config
)) != sizeof(config
))
305 /* bit 0-6 header type, bit 7 multi/single function device */
306 return config
[PCI_HEADER_TYPE
] & 0x80;
309 static bool is_pci_ari_enabled(sd_device
*dev
) {
312 if (sd_device_get_sysattr_value(dev
, "ari_enabled", &a
) < 0)
315 return streq(a
, "1");
318 static int dev_pci_slot(sd_device
*dev
, struct netnames
*names
) {
319 unsigned long dev_port
= 0;
320 unsigned domain
, bus
, slot
, func
, hotplug_slot
= 0;
323 const char *sysname
, *attr
, *port_name
= NULL
, *syspath
;
324 _cleanup_(sd_device_unrefp
) sd_device
*pci
= NULL
;
325 sd_device
*hotplug_slot_dev
;
326 char slots
[PATH_MAX
];
327 _cleanup_closedir_
DIR *dir
= NULL
;
331 r
= sd_device_get_sysname(names
->pcidev
, &sysname
);
335 if (sscanf(sysname
, "%x:%x:%x.%u", &domain
, &bus
, &slot
, &func
) != 4)
338 if (is_pci_ari_enabled(names
->pcidev
))
339 /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
340 * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
341 * where the slot makes up the upper 5 bits. */
344 /* kernel provided port index for multiple ports on a single PCI function */
345 if (sd_device_get_sysattr_value(dev
, "dev_port", &attr
) >= 0) {
346 dev_port
= strtoul(attr
, NULL
, 10);
347 /* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously
348 * provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
349 * which thus stays initialized as 0. */
351 sd_device_get_sysattr_value(dev
, "type", &attr
) >= 0) {
354 type
= strtoul(attr
, NULL
, 10);
355 if (type
== ARPHRD_INFINIBAND
&&
356 sd_device_get_sysattr_value(dev
, "dev_id", &attr
) >= 0)
357 dev_port
= strtoul(attr
, NULL
, 16);
361 /* kernel provided front panel port name for multiple port PCI device */
362 (void) sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
364 /* compose a name based on the raw kernel's PCI bus, slot numbers */
366 l
= sizeof(names
->pci_path
);
368 l
= strpcpyf(&s
, l
, "P%u", domain
);
369 l
= strpcpyf(&s
, l
, "p%us%u", bus
, slot
);
370 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
371 l
= strpcpyf(&s
, l
, "f%u", func
);
373 l
= strpcpyf(&s
, l
, "n%s", port_name
);
374 else if (dev_port
> 0)
375 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
377 names
->pci_path
[0] = '\0';
379 /* ACPI _SUN — slot user number */
380 r
= sd_device_new_from_subsystem_sysname(&pci
, "subsystem", "pci");
384 r
= sd_device_get_syspath(pci
, &syspath
);
387 if (!snprintf_ok(slots
, sizeof slots
, "%s/slots", syspath
))
388 return -ENAMETOOLONG
;
390 dir
= opendir(slots
);
394 hotplug_slot_dev
= names
->pcidev
;
395 while (hotplug_slot_dev
) {
396 if (sd_device_get_sysname(hotplug_slot_dev
, &sysname
) < 0)
399 FOREACH_DIRENT_ALL(dent
, dir
, break) {
402 _cleanup_free_
char *address
= NULL
;
404 if (dent
->d_name
[0] == '.')
406 r
= safe_atou_full(dent
->d_name
, 10, &i
);
410 /* match slot address with device by stripping the function */
411 if (snprintf_ok(str
, sizeof str
, "%s/%s/address", slots
, dent
->d_name
) &&
412 read_one_line_file(str
, &address
) >= 0 &&
413 startswith(sysname
, address
)) {
418 if (hotplug_slot
> 0)
420 if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev
, "pci", NULL
, &hotplug_slot_dev
) < 0)
425 if (hotplug_slot
> 0) {
427 l
= sizeof(names
->pci_slot
);
429 l
= strpcpyf(&s
, l
, "P%d", domain
);
430 l
= strpcpyf(&s
, l
, "s%d", hotplug_slot
);
431 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
432 l
= strpcpyf(&s
, l
, "f%d", func
);
434 l
= strpcpyf(&s
, l
, "n%s", port_name
);
435 else if (dev_port
> 0)
436 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
438 names
->pci_slot
[0] = '\0';
444 static int names_vio(sd_device
*dev
, struct netnames
*names
) {
446 unsigned busid
, slotid
, ethid
;
447 const char *syspath
, *subsystem
;
450 /* check if our direct parent is a VIO device with no other bus in-between */
451 r
= sd_device_get_parent(dev
, &parent
);
455 r
= sd_device_get_subsystem(parent
, &subsystem
);
458 if (!streq("vio", subsystem
))
461 /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
462 * selected in the HMC), thus this provides a reliable naming (e.g.
463 * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
464 * there should only ever be one bus, and then remove leading zeros. */
465 r
= sd_device_get_syspath(dev
, &syspath
);
469 if (sscanf(syspath
, "/sys/devices/vio/%4x%4x/net/eth%u", &busid
, &slotid
, ðid
) != 3)
472 xsprintf(names
->vio_slot
, "v%u", slotid
);
473 names
->type
= NET_VIO
;
477 #define _PLATFORM_TEST "/sys/devices/platform/vvvvPPPP"
478 #define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
479 #define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
481 static int names_platform(sd_device
*dev
, struct netnames
*names
, bool test
) {
484 unsigned model
, instance
, ethid
;
485 const char *syspath
, *pattern
, *validchars
, *subsystem
;
488 /* check if our direct parent is a platform device with no other bus in-between */
489 r
= sd_device_get_parent(dev
, &parent
);
493 r
= sd_device_get_subsystem(parent
, &subsystem
);
497 if (!streq("platform", subsystem
))
500 r
= sd_device_get_syspath(dev
, &syspath
);
504 /* syspath is too short, to have a valid ACPI instance */
505 if (strlen(syspath
) < sizeof _PLATFORM_TEST
)
508 /* Vendor ID can be either PNP ID (3 chars A-Z) or ACPI ID (4 chars A-Z and numerals) */
509 if (syspath
[sizeof _PLATFORM_TEST
- 1] == ':') {
510 pattern
= _PLATFORM_PATTERN4
;
511 validchars
= UPPERCASE_LETTERS DIGITS
;
513 pattern
= _PLATFORM_PATTERN3
;
514 validchars
= UPPERCASE_LETTERS
;
517 /* Platform devices are named after ACPI table match, and instance id
518 * eg. "/sys/devices/platform/HISI00C2:00");
519 * The Vendor (3 or 4 char), followed by hexdecimal model number : instance id.
522 #pragma GCC diagnostic push
523 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
524 if (sscanf(syspath
, pattern
, vendor
, &model
, &instance
, ðid
) != 4)
526 #pragma GCC diagnostic pop
528 if (!in_charset(vendor
, validchars
))
531 ascii_strlower(vendor
);
533 xsprintf(names
->platform_path
, "a%s%xi%u", vendor
, model
, instance
);
534 names
->type
= NET_PLATFORM
;
538 static int names_pci(sd_device
*dev
, struct netnames
*names
) {
540 struct netnames vf_names
= {};
541 struct virtfn_info vf_info
= {};
542 const char *subsystem
;
548 r
= sd_device_get_parent(dev
, &parent
);
551 /* skip virtio subsystem if present */
552 parent
= skip_virtio(parent
);
557 /* check if our direct parent is a PCI device with no other bus in-between */
558 if (sd_device_get_subsystem(parent
, &subsystem
) >= 0 &&
559 streq("pci", subsystem
)) {
560 names
->type
= NET_PCI
;
561 names
->pcidev
= parent
;
563 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "pci", NULL
, &names
->pcidev
);
568 if (get_virtfn_info(dev
, names
, &vf_info
) >= 0) {
569 /* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
570 vf_names
.pcidev
= vf_info
.physfn_pcidev
;
571 dev_pci_onboard(dev
, &vf_names
);
572 dev_pci_slot(dev
, &vf_names
);
573 if (vf_names
.pci_onboard
[0])
574 if (strlen(vf_names
.pci_onboard
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_onboard
))
575 strscpyl(names
->pci_onboard
, sizeof(names
->pci_onboard
),
576 vf_names
.pci_onboard
, vf_info
.suffix
, NULL
);
577 if (vf_names
.pci_slot
[0])
578 if (strlen(vf_names
.pci_slot
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_slot
))
579 strscpyl(names
->pci_slot
, sizeof(names
->pci_slot
),
580 vf_names
.pci_slot
, vf_info
.suffix
, NULL
);
581 if (vf_names
.pci_path
[0])
582 if (strlen(vf_names
.pci_path
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_path
))
583 strscpyl(names
->pci_path
, sizeof(names
->pci_path
),
584 vf_names
.pci_path
, vf_info
.suffix
, NULL
);
585 sd_device_unref(vf_info
.physfn_pcidev
);
587 dev_pci_onboard(dev
, names
);
588 dev_pci_slot(dev
, names
);
594 static int names_usb(sd_device
*dev
, struct netnames
*names
) {
596 char name
[256], *ports
, *config
, *interf
, *s
;
604 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface", &usbdev
);
608 r
= sd_device_get_sysname(usbdev
, &sysname
);
612 /* get USB port number chain, configuration, interface */
613 strscpy(name
, sizeof(name
), sysname
);
614 s
= strchr(name
, '-');
619 s
= strchr(ports
, ':');
625 s
= strchr(config
, '.');
631 /* prefix every port number in the chain with "u" */
633 while ((s
= strchr(s
, '.')))
635 s
= names
->usb_ports
;
636 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "u", ports
, NULL
);
638 /* append USB config number, suppress the common config == 1 */
639 if (!streq(config
, "1"))
640 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "c", config
, NULL
);
642 /* append USB interface number, suppress the interface == 0 */
643 if (!streq(interf
, "0"))
644 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "i", interf
, NULL
);
646 return -ENAMETOOLONG
;
648 names
->type
= NET_USB
;
652 static int names_bcma(sd_device
*dev
, struct netnames
*names
) {
661 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "bcma", NULL
, &bcmadev
);
665 r
= sd_device_get_sysname(bcmadev
, &sysname
);
669 /* bus num:core num */
670 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
672 /* suppress the common core == 0 */
674 xsprintf(names
->bcma_core
, "b%u", core
);
676 names
->type
= NET_BCMA
;
680 static int names_ccw(sd_device
*dev
, struct netnames
*names
) {
682 const char *bus_id
, *subsys
;
690 /* Retrieve the associated CCW device */
691 r
= sd_device_get_parent(dev
, &cdev
);
695 /* skip virtio subsystem if present */
696 cdev
= skip_virtio(cdev
);
700 r
= sd_device_get_subsystem(cdev
, &subsys
);
704 /* Network devices are either single or grouped CCW devices */
705 if (!STR_IN_SET(subsys
, "ccwgroup", "ccw"))
708 /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
709 * identifies the network device on the Linux on System z channel
710 * subsystem. Note that the bus-ID contains lowercase characters.
712 r
= sd_device_get_sysname(cdev
, &bus_id
);
716 /* Check the length of the bus-ID. Rely on that the kernel provides
717 * a correct bus-ID; alternatively, improve this check and parse and
718 * verify each bus-ID part...
720 bus_id_len
= strlen(bus_id
);
721 if (!IN_SET(bus_id_len
, 8, 9))
724 /* Strip leading zeros from the bus id for aesthetic purposes. This
725 * keeps the ccw names stable, yet much shorter in general case of
726 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
727 * not prepended when it is zero. Preserve the last 0 for 0.0.0000.
729 bus_id_start
= strspn(bus_id
, ".0");
730 bus_id
+= bus_id_start
< bus_id_len
? bus_id_start
: bus_id_len
- 1;
732 /* Store the CCW bus-ID for use as network device name */
733 if (snprintf_ok(names
->ccw_busid
, sizeof(names
->ccw_busid
), "c%s", bus_id
))
734 names
->type
= NET_CCW
;
739 static int names_mac(sd_device
*dev
, struct netnames
*names
) {
742 unsigned a1
, a2
, a3
, a4
, a5
, a6
;
745 /* Some kinds of devices tend to have hardware addresses
746 * that are impossible to use in an iface name.
748 r
= sd_device_get_sysattr_value(dev
, "type", &s
);
752 i
= strtoul(s
, NULL
, 0);
754 /* The persistent part of a hardware address of an InfiniBand NIC
755 * is 8 bytes long. We cannot fit this much in an iface name.
757 case ARPHRD_INFINIBAND
:
763 /* check for NET_ADDR_PERM, skip random MAC addresses */
764 r
= sd_device_get_sysattr_value(dev
, "addr_assign_type", &s
);
767 i
= strtoul(s
, NULL
, 0);
771 r
= sd_device_get_sysattr_value(dev
, "address", &s
);
774 if (sscanf(s
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6)
777 /* skip empty MAC addresses */
778 if (a1
+ a2
+ a3
+ a4
+ a5
+ a6
== 0)
787 names
->mac_valid
= true;
791 /* IEEE Organizationally Unique Identifier vendor string */
792 static int ieee_oui(sd_device
*dev
, struct netnames
*names
, bool test
) {
795 if (!names
->mac_valid
)
797 /* skip commonly misused 00:00:00 (Xerox) prefix */
798 if (memcmp(names
->mac
, "\0\0\0", 3) == 0)
800 xsprintf(str
, "OUI:%02X%02X%02X%02X%02X%02X", names
->mac
[0],
801 names
->mac
[1], names
->mac
[2], names
->mac
[3], names
->mac
[4],
803 udev_builtin_hwdb_lookup(dev
, NULL
, str
, NULL
, test
);
807 static int builtin_net_id(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
808 const char *s
, *p
, *devtype
, *prefix
= "en";
809 struct netnames names
= {};
813 /* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
814 r
= sd_device_get_sysattr_value(dev
, "type", &s
);
818 i
= strtoul(s
, NULL
, 0);
823 case ARPHRD_INFINIBAND
:
833 /* skip stacked devices, like VLANs, ... */
834 r
= sd_device_get_sysattr_value(dev
, "ifindex", &s
);
837 r
= sd_device_get_sysattr_value(dev
, "iflink", &p
);
843 if (sd_device_get_devtype(dev
, &devtype
) >= 0) {
844 if (streq("wlan", devtype
))
846 else if (streq("wwan", devtype
))
850 r
= names_mac(dev
, &names
);
851 if (r
>= 0 && names
.mac_valid
) {
854 xsprintf(str
, "%sx%02x%02x%02x%02x%02x%02x", prefix
,
855 names
.mac
[0], names
.mac
[1], names
.mac
[2],
856 names
.mac
[3], names
.mac
[4], names
.mac
[5]);
857 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_MAC", str
);
859 ieee_oui(dev
, &names
, test
);
862 /* get path names for Linux on System z network devices */
863 if (names_ccw(dev
, &names
) >= 0 && names
.type
== NET_CCW
) {
866 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.ccw_busid
))
867 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
871 /* get ibmveth/ibmvnic slot-based names. */
872 if (names_vio(dev
, &names
) >= 0 && names
.type
== NET_VIO
) {
875 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.vio_slot
))
876 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
880 /* get ACPI path names for ARM64 platform devices */
881 if (names_platform(dev
, &names
, test
) >= 0 && names
.type
== NET_PLATFORM
) {
884 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.platform_path
))
885 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
889 /* get PCI based path names, we compose only PCI based paths */
890 if (names_pci(dev
, &names
) < 0)
893 /* plain PCI device */
894 if (names
.type
== NET_PCI
) {
897 if (names
.pci_onboard
[0] &&
898 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_onboard
))
899 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_ONBOARD", str
);
901 if (names
.pci_onboard_label
&&
902 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_onboard_label
))
903 udev_builtin_add_property(dev
, test
, "ID_NET_LABEL_ONBOARD", str
);
905 if (names
.pci_path
[0] &&
906 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_path
))
907 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
909 if (names
.pci_slot
[0] &&
910 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_slot
))
911 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
916 if (names_usb(dev
, &names
) >= 0 && names
.type
== NET_USB
) {
919 if (names
.pci_path
[0] &&
920 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_path
, names
.usb_ports
))
921 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
923 if (names
.pci_slot
[0] &&
924 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_slot
, names
.usb_ports
))
925 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
930 if (names_bcma(dev
, &names
) >= 0 && names
.type
== NET_BCMA
) {
933 if (names
.pci_path
[0] &&
934 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_path
, names
.bcma_core
))
935 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
937 if (names
.pci_slot
[0] &&
938 snprintf(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_slot
, names
.bcma_core
))
939 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
946 const struct udev_builtin udev_builtin_net_id
= {
948 .cmd
= builtin_net_id
,
949 .help
= "Network device properties",