1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 * https://systemd.io/PREDICTABLE_INTERFACE_NAMES
12 * When the code here is changed, man/systemd.net-naming-scheme.xml must be updated too.
18 #include <net/if_arp.h>
23 #include <linux/pci_regs.h>
25 #include "alloc-util.h"
26 #include "dirent-util.h"
30 #include "netif-naming-scheme.h"
31 #include "parse-util.h"
32 #include "proc-cmdline.h"
33 #include "stdio-util.h"
34 #include "string-util.h"
37 #include "udev-builtin.h"
39 #define ONBOARD_INDEX_MAX (16*1024-1)
54 enum netname_type type
;
60 char pci_slot
[ALTIFNAMSIZ
];
61 char pci_path
[ALTIFNAMSIZ
];
62 char pci_onboard
[ALTIFNAMSIZ
];
63 const char *pci_onboard_label
;
65 char usb_ports
[ALTIFNAMSIZ
];
66 char bcma_core
[ALTIFNAMSIZ
];
67 char ccw_busid
[ALTIFNAMSIZ
];
68 char vio_slot
[ALTIFNAMSIZ
];
69 char platform_path
[ALTIFNAMSIZ
];
70 char netdevsim_path
[ALTIFNAMSIZ
];
74 sd_device
*physfn_pcidev
;
75 char suffix
[ALTIFNAMSIZ
];
78 /* skip intermediate virtio devices */
79 static sd_device
*skip_virtio(sd_device
*dev
) {
82 /* there can only ever be one virtio bus per parent device, so we can
83 * safely ignore any virtio buses. see
84 * http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html */
85 for (parent
= dev
; parent
; ) {
86 const char *subsystem
;
88 if (sd_device_get_subsystem(parent
, &subsystem
) < 0)
91 if (!streq(subsystem
, "virtio"))
94 if (sd_device_get_parent(parent
, &parent
) < 0)
101 static int get_virtfn_info(sd_device
*dev
, struct netnames
*names
, struct virtfn_info
*ret
) {
102 _cleanup_(sd_device_unrefp
) sd_device
*physfn_pcidev
= NULL
;
103 const char *physfn_link_file
, *syspath
;
104 _cleanup_free_
char *physfn_pci_syspath
= NULL
;
105 _cleanup_free_
char *virtfn_pci_syspath
= NULL
;
107 _cleanup_closedir_
DIR *dir
= NULL
;
108 char suffix
[ALTIFNAMSIZ
];
115 r
= sd_device_get_syspath(names
->pcidev
, &syspath
);
119 /* Check if this is a virtual function. */
120 physfn_link_file
= strjoina(syspath
, "/physfn");
121 r
= chase_symlinks(physfn_link_file
, NULL
, 0, &physfn_pci_syspath
, NULL
);
125 /* Get physical function's pci device. */
126 r
= sd_device_new_from_syspath(&physfn_pcidev
, physfn_pci_syspath
);
130 /* Find the virtual function number by finding the right virtfn link. */
131 dir
= opendir(physfn_pci_syspath
);
135 FOREACH_DIRENT_ALL(dent
, dir
, break) {
136 _cleanup_free_
char *virtfn_link_file
= NULL
;
138 if (!startswith(dent
->d_name
, "virtfn"))
141 virtfn_link_file
= path_join(physfn_pci_syspath
, dent
->d_name
);
142 if (!virtfn_link_file
)
145 if (chase_symlinks(virtfn_link_file
, NULL
, 0, &virtfn_pci_syspath
, NULL
) < 0)
148 if (streq(syspath
, virtfn_pci_syspath
)) {
149 if (!snprintf_ok(suffix
, sizeof(suffix
), "v%s", &dent
->d_name
[6]))
158 ret
->physfn_pcidev
= TAKE_PTR(physfn_pcidev
);
159 strncpy(ret
->suffix
, suffix
, sizeof(ret
->suffix
));
164 /* retrieve on-board index number and label from firmware */
165 static int dev_pci_onboard(sd_device
*dev
, struct netnames
*names
) {
166 unsigned long idx
, dev_port
= 0;
167 const char *attr
, *port_name
= NULL
;
172 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
173 if (sd_device_get_sysattr_value(names
->pcidev
, "acpi_index", &attr
) < 0) {
174 /* SMBIOS type 41 — Onboard Devices Extended Information */
175 r
= sd_device_get_sysattr_value(names
->pcidev
, "index", &attr
);
180 r
= safe_atolu(attr
, &idx
);
183 if (idx
== 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX
))
186 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to
187 * report for example). Let's define a cut-off where we don't consider the index reliable anymore. We
188 * pick some arbitrary cut-off, which is somewhere beyond the realistic number of physical network
189 * interface a system might have. Ideally the kernel would already filter this crap for us, but it
190 * doesn't currently. */
191 if (idx
> ONBOARD_INDEX_MAX
)
194 /* kernel provided port index for multiple ports on a single PCI function */
195 if (sd_device_get_sysattr_value(dev
, "dev_port", &attr
) >= 0)
196 dev_port
= strtoul(attr
, NULL
, 10);
198 /* kernel provided front panel port name for multiple port PCI device */
199 (void) sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
201 s
= names
->pci_onboard
;
202 l
= sizeof(names
->pci_onboard
);
203 l
= strpcpyf(&s
, l
, "o%lu", idx
);
205 l
= strpcpyf(&s
, l
, "n%s", port_name
);
206 else if (dev_port
> 0)
207 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
209 names
->pci_onboard
[0] = '\0';
211 if (sd_device_get_sysattr_value(names
->pcidev
, "label", &names
->pci_onboard_label
) < 0)
212 names
->pci_onboard_label
= NULL
;
217 /* read the 256 bytes PCI configuration space to check the multi-function bit */
218 static bool is_pci_multifunction(sd_device
*dev
) {
219 _cleanup_close_
int fd
= -1;
220 const char *filename
, *syspath
;
223 if (sd_device_get_syspath(dev
, &syspath
) < 0)
226 filename
= strjoina(syspath
, "/config");
227 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
230 if (read(fd
, &config
, sizeof(config
)) != sizeof(config
))
233 /* bit 0-6 header type, bit 7 multi/single function device */
234 return config
[PCI_HEADER_TYPE
] & 0x80;
237 static bool is_pci_ari_enabled(sd_device
*dev
) {
240 if (sd_device_get_sysattr_value(dev
, "ari_enabled", &a
) < 0)
243 return streq(a
, "1");
246 static bool is_pci_bridge(sd_device
*dev
) {
249 if (sd_device_get_sysattr_value(dev
, "modalias", &v
) < 0)
252 if (!startswith(v
, "pci:"))
261 /* PCI device subclass 04 corresponds to PCI bridge */
262 return strneq(p
+ 2, "04", 2);
265 static int dev_pci_slot(sd_device
*dev
, struct netnames
*names
) {
266 unsigned long dev_port
= 0;
267 unsigned domain
, bus
, slot
, func
;
268 int hotplug_slot
= -1;
271 const char *sysname
, *attr
, *port_name
= NULL
, *syspath
;
272 _cleanup_(sd_device_unrefp
) sd_device
*pci
= NULL
;
273 sd_device
*hotplug_slot_dev
;
274 char slots
[PATH_MAX
];
275 _cleanup_closedir_
DIR *dir
= NULL
;
279 r
= sd_device_get_sysname(names
->pcidev
, &sysname
);
283 if (sscanf(sysname
, "%x:%x:%x.%u", &domain
, &bus
, &slot
, &func
) != 4)
286 if (naming_scheme_has(NAMING_NPAR_ARI
) &&
287 is_pci_ari_enabled(names
->pcidev
))
288 /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
289 * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
290 * where the slot makes up the upper 5 bits. */
293 /* kernel provided port index for multiple ports on a single PCI function */
294 if (sd_device_get_sysattr_value(dev
, "dev_port", &attr
) >= 0) {
295 dev_port
= strtoul(attr
, NULL
, 10);
296 /* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously
297 * provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
298 * which thus stays initialized as 0. */
300 sd_device_get_sysattr_value(dev
, "type", &attr
) >= 0) {
303 type
= strtoul(attr
, NULL
, 10);
304 if (type
== ARPHRD_INFINIBAND
&&
305 sd_device_get_sysattr_value(dev
, "dev_id", &attr
) >= 0)
306 dev_port
= strtoul(attr
, NULL
, 16);
310 /* kernel provided front panel port name for multi-port PCI device */
311 (void) sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
313 /* compose a name based on the raw kernel's PCI bus, slot numbers */
315 l
= sizeof(names
->pci_path
);
317 l
= strpcpyf(&s
, l
, "P%u", domain
);
318 l
= strpcpyf(&s
, l
, "p%us%u", bus
, slot
);
319 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
320 l
= strpcpyf(&s
, l
, "f%u", func
);
322 l
= strpcpyf(&s
, l
, "n%s", port_name
);
323 else if (dev_port
> 0)
324 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
326 names
->pci_path
[0] = '\0';
328 /* ACPI _SUN — slot user number */
329 r
= sd_device_new_from_subsystem_sysname(&pci
, "subsystem", "pci");
333 r
= sd_device_get_syspath(pci
, &syspath
);
336 if (!snprintf_ok(slots
, sizeof slots
, "%s/slots", syspath
))
337 return -ENAMETOOLONG
;
339 dir
= opendir(slots
);
343 hotplug_slot_dev
= names
->pcidev
;
344 while (hotplug_slot_dev
) {
345 if (sd_device_get_sysname(hotplug_slot_dev
, &sysname
) < 0)
348 FOREACH_DIRENT_ALL(dent
, dir
, break) {
351 _cleanup_free_
char *address
= NULL
;
353 if (dot_or_dot_dot(dent
->d_name
))
356 r
= safe_atoi(dent
->d_name
, &i
);
360 /* match slot address with device by stripping the function */
361 if (snprintf_ok(str
, sizeof str
, "%s/%s/address", slots
, dent
->d_name
) &&
362 read_one_line_file(str
, &address
) >= 0 &&
363 startswith(sysname
, address
)) {
366 /* We found the match between PCI device and slot. However, we won't use the
367 * slot index if the device is a PCI bridge, because it can have other child
368 * devices that will try to claim the same index and that would create name
370 if (naming_scheme_has(NAMING_BRIDGE_NO_SLOT
) && is_pci_bridge(hotplug_slot_dev
))
376 if (hotplug_slot
>= 0)
378 if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev
, "pci", NULL
, &hotplug_slot_dev
) < 0)
383 if (hotplug_slot
> 0) {
385 l
= sizeof(names
->pci_slot
);
387 l
= strpcpyf(&s
, l
, "P%d", domain
);
388 l
= strpcpyf(&s
, l
, "s%d", hotplug_slot
);
389 if (func
> 0 || is_pci_multifunction(names
->pcidev
))
390 l
= strpcpyf(&s
, l
, "f%d", func
);
392 l
= strpcpyf(&s
, l
, "n%s", port_name
);
393 else if (dev_port
> 0)
394 l
= strpcpyf(&s
, l
, "d%lu", dev_port
);
396 names
->pci_slot
[0] = '\0';
402 static int names_vio(sd_device
*dev
, struct netnames
*names
) {
404 unsigned busid
, slotid
, ethid
;
405 const char *syspath
, *subsystem
;
408 /* check if our direct parent is a VIO device with no other bus in-between */
409 r
= sd_device_get_parent(dev
, &parent
);
413 r
= sd_device_get_subsystem(parent
, &subsystem
);
416 if (!streq("vio", subsystem
))
419 /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
420 * selected in the HMC), thus this provides a reliable naming (e.g.
421 * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
422 * there should only ever be one bus, and then remove leading zeros. */
423 r
= sd_device_get_syspath(dev
, &syspath
);
427 if (sscanf(syspath
, "/sys/devices/vio/%4x%4x/net/eth%u", &busid
, &slotid
, ðid
) != 3)
430 xsprintf(names
->vio_slot
, "v%u", slotid
);
431 names
->type
= NET_VIO
;
435 #define _PLATFORM_TEST "/sys/devices/platform/vvvvPPPP"
436 #define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
437 #define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
439 static int names_platform(sd_device
*dev
, struct netnames
*names
, bool test
) {
442 unsigned model
, instance
, ethid
;
443 const char *syspath
, *pattern
, *validchars
, *subsystem
;
446 /* check if our direct parent is a platform device with no other bus in-between */
447 r
= sd_device_get_parent(dev
, &parent
);
451 r
= sd_device_get_subsystem(parent
, &subsystem
);
455 if (!streq("platform", subsystem
))
458 r
= sd_device_get_syspath(dev
, &syspath
);
462 /* syspath is too short, to have a valid ACPI instance */
463 if (strlen(syspath
) < sizeof _PLATFORM_TEST
)
466 /* Vendor ID can be either PNP ID (3 chars A-Z) or ACPI ID (4 chars A-Z and numerals) */
467 if (syspath
[sizeof _PLATFORM_TEST
- 1] == ':') {
468 pattern
= _PLATFORM_PATTERN4
;
469 validchars
= UPPERCASE_LETTERS DIGITS
;
471 pattern
= _PLATFORM_PATTERN3
;
472 validchars
= UPPERCASE_LETTERS
;
475 /* Platform devices are named after ACPI table match, and instance id
476 * eg. "/sys/devices/platform/HISI00C2:00");
477 * The Vendor (3 or 4 char), followed by hexdecimal model number : instance id.
480 DISABLE_WARNING_FORMAT_NONLITERAL
;
481 if (sscanf(syspath
, pattern
, vendor
, &model
, &instance
, ðid
) != 4)
485 if (!in_charset(vendor
, validchars
))
488 ascii_strlower(vendor
);
490 xsprintf(names
->platform_path
, "a%s%xi%u", vendor
, model
, instance
);
491 names
->type
= NET_PLATFORM
;
495 static int names_pci(sd_device
*dev
, struct netnames
*names
) {
497 struct netnames vf_names
= {};
498 struct virtfn_info vf_info
= {};
499 const char *subsystem
;
505 r
= sd_device_get_parent(dev
, &parent
);
508 /* skip virtio subsystem if present */
509 parent
= skip_virtio(parent
);
514 /* check if our direct parent is a PCI device with no other bus in-between */
515 if (sd_device_get_subsystem(parent
, &subsystem
) >= 0 &&
516 streq("pci", subsystem
)) {
517 names
->type
= NET_PCI
;
518 names
->pcidev
= parent
;
520 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "pci", NULL
, &names
->pcidev
);
525 if (naming_scheme_has(NAMING_SR_IOV_V
) &&
526 get_virtfn_info(dev
, names
, &vf_info
) >= 0) {
527 /* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
528 vf_names
.pcidev
= vf_info
.physfn_pcidev
;
529 dev_pci_onboard(dev
, &vf_names
);
530 dev_pci_slot(dev
, &vf_names
);
531 if (vf_names
.pci_onboard
[0])
532 if (strlen(vf_names
.pci_onboard
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_onboard
))
533 strscpyl(names
->pci_onboard
, sizeof(names
->pci_onboard
),
534 vf_names
.pci_onboard
, vf_info
.suffix
, NULL
);
535 if (vf_names
.pci_slot
[0])
536 if (strlen(vf_names
.pci_slot
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_slot
))
537 strscpyl(names
->pci_slot
, sizeof(names
->pci_slot
),
538 vf_names
.pci_slot
, vf_info
.suffix
, NULL
);
539 if (vf_names
.pci_path
[0])
540 if (strlen(vf_names
.pci_path
) + strlen(vf_info
.suffix
) < sizeof(names
->pci_path
))
541 strscpyl(names
->pci_path
, sizeof(names
->pci_path
),
542 vf_names
.pci_path
, vf_info
.suffix
, NULL
);
543 sd_device_unref(vf_info
.physfn_pcidev
);
545 dev_pci_onboard(dev
, names
);
546 dev_pci_slot(dev
, names
);
552 static int names_usb(sd_device
*dev
, struct netnames
*names
) {
554 char name
[256], *ports
, *config
, *interf
, *s
;
562 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "usb", "usb_interface", &usbdev
);
566 r
= sd_device_get_sysname(usbdev
, &sysname
);
570 /* get USB port number chain, configuration, interface */
571 strscpy(name
, sizeof(name
), sysname
);
572 s
= strchr(name
, '-');
577 s
= strchr(ports
, ':');
583 s
= strchr(config
, '.');
589 /* prefix every port number in the chain with "u" */
591 while ((s
= strchr(s
, '.')))
593 s
= names
->usb_ports
;
594 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "u", ports
, NULL
);
596 /* append USB config number, suppress the common config == 1 */
597 if (!streq(config
, "1"))
598 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "c", config
, NULL
);
600 /* append USB interface number, suppress the interface == 0 */
601 if (!streq(interf
, "0"))
602 l
= strpcpyl(&s
, sizeof(names
->usb_ports
), "i", interf
, NULL
);
604 return -ENAMETOOLONG
;
606 names
->type
= NET_USB
;
610 static int names_bcma(sd_device
*dev
, struct netnames
*names
) {
619 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "bcma", NULL
, &bcmadev
);
623 r
= sd_device_get_sysname(bcmadev
, &sysname
);
627 /* bus num:core num */
628 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
630 /* suppress the common core == 0 */
632 xsprintf(names
->bcma_core
, "b%u", core
);
634 names
->type
= NET_BCMA
;
638 static int names_ccw(sd_device
*dev
, struct netnames
*names
) {
640 const char *bus_id
, *subsys
;
648 /* Retrieve the associated CCW device */
649 r
= sd_device_get_parent(dev
, &cdev
);
653 /* skip virtio subsystem if present */
654 cdev
= skip_virtio(cdev
);
658 r
= sd_device_get_subsystem(cdev
, &subsys
);
662 /* Network devices are either single or grouped CCW devices */
663 if (!STR_IN_SET(subsys
, "ccwgroup", "ccw"))
666 /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
667 * identifies the network device on the Linux on System z channel
668 * subsystem. Note that the bus-ID contains lowercase characters.
670 r
= sd_device_get_sysname(cdev
, &bus_id
);
674 /* Check the length of the bus-ID. Rely on the fact that the kernel provides a correct bus-ID;
675 * alternatively, improve this check and parse and verify each bus-ID part...
677 bus_id_len
= strlen(bus_id
);
678 if (!IN_SET(bus_id_len
, 8, 9))
681 /* Strip leading zeros from the bus id for aesthetic purposes. This
682 * keeps the ccw names stable, yet much shorter in general case of
683 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
684 * not prepended when it is zero. Preserve the last 0 for 0.0.0000.
686 bus_id_start
= strspn(bus_id
, ".0");
687 bus_id
+= bus_id_start
< bus_id_len
? bus_id_start
: bus_id_len
- 1;
689 /* Store the CCW bus-ID for use as network device name */
690 if (snprintf_ok(names
->ccw_busid
, sizeof(names
->ccw_busid
), "c%s", bus_id
))
691 names
->type
= NET_CCW
;
696 static int names_mac(sd_device
*dev
, struct netnames
*names
) {
699 unsigned a1
, a2
, a3
, a4
, a5
, a6
;
702 /* Some kinds of devices tend to have hardware addresses
703 * that are impossible to use in an iface name.
705 r
= sd_device_get_sysattr_value(dev
, "type", &s
);
709 i
= strtoul(s
, NULL
, 0);
711 /* The persistent part of a hardware address of an InfiniBand NIC
712 * is 8 bytes long. We cannot fit this much in an iface name.
714 case ARPHRD_INFINIBAND
:
720 /* check for NET_ADDR_PERM, skip random MAC addresses */
721 r
= sd_device_get_sysattr_value(dev
, "addr_assign_type", &s
);
724 i
= strtoul(s
, NULL
, 0);
728 r
= sd_device_get_sysattr_value(dev
, "address", &s
);
731 if (sscanf(s
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6)
734 /* skip empty MAC addresses */
735 if (a1
+ a2
+ a3
+ a4
+ a5
+ a6
== 0)
744 names
->mac_valid
= true;
748 static int names_netdevsim(sd_device
*dev
, struct netnames
*names
) {
749 sd_device
*netdevsimdev
;
752 const char *port_name
= NULL
;
756 if (!naming_scheme_has(NAMING_NETDEVSIM
))
762 r
= sd_device_get_parent_with_subsystem_devtype(dev
, "netdevsim", NULL
, &netdevsimdev
);
765 r
= sd_device_get_sysname(netdevsimdev
, &sysname
);
769 if (sscanf(sysname
, "netdevsim%u", &addr
) != 1)
772 r
= sd_device_get_sysattr_value(dev
, "phys_port_name", &port_name
);
776 ok
= snprintf_ok(names
->netdevsim_path
, sizeof(names
->netdevsim_path
), "i%un%s", addr
, port_name
);
780 names
->type
= NET_NETDEVSIM
;
785 /* IEEE Organizationally Unique Identifier vendor string */
786 static int ieee_oui(sd_device
*dev
, struct netnames
*names
, bool test
) {
789 if (!names
->mac_valid
)
791 /* skip commonly misused 00:00:00 (Xerox) prefix */
792 if (memcmp(names
->mac
, "\0\0\0", 3) == 0)
794 xsprintf(str
, "OUI:%02X%02X%02X%02X%02X%02X", names
->mac
[0],
795 names
->mac
[1], names
->mac
[2], names
->mac
[3], names
->mac
[4],
797 udev_builtin_hwdb_lookup(dev
, NULL
, str
, NULL
, test
);
801 static int builtin_net_id(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
802 const char *s
, *p
, *devtype
, *prefix
= "en";
803 struct netnames names
= {};
807 /* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
808 r
= sd_device_get_sysattr_value(dev
, "type", &s
);
812 i
= strtoul(s
, NULL
, 0);
817 case ARPHRD_INFINIBAND
:
818 if (naming_scheme_has(NAMING_INFINIBAND
))
830 /* skip stacked devices, like VLANs, ... */
831 r
= sd_device_get_sysattr_value(dev
, "ifindex", &s
);
834 r
= sd_device_get_sysattr_value(dev
, "iflink", &p
);
840 if (sd_device_get_devtype(dev
, &devtype
) >= 0) {
841 if (streq("wlan", devtype
))
843 else if (streq("wwan", devtype
))
847 udev_builtin_add_property(dev
, test
, "ID_NET_NAMING_SCHEME", naming_scheme()->name
);
849 r
= names_mac(dev
, &names
);
850 if (r
>= 0 && names
.mac_valid
) {
851 char str
[ALTIFNAMSIZ
];
853 xsprintf(str
, "%sx%02x%02x%02x%02x%02x%02x", prefix
,
854 names
.mac
[0], names
.mac
[1], names
.mac
[2],
855 names
.mac
[3], names
.mac
[4], names
.mac
[5]);
856 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_MAC", str
);
858 ieee_oui(dev
, &names
, test
);
861 /* get path names for Linux on System z network devices */
862 if (names_ccw(dev
, &names
) >= 0 && names
.type
== NET_CCW
) {
863 char str
[ALTIFNAMSIZ
];
865 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.ccw_busid
))
866 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
870 /* get ibmveth/ibmvnic slot-based names. */
871 if (names_vio(dev
, &names
) >= 0 && names
.type
== NET_VIO
) {
872 char str
[ALTIFNAMSIZ
];
874 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.vio_slot
))
875 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
879 /* get ACPI path names for ARM64 platform devices */
880 if (names_platform(dev
, &names
, test
) >= 0 && names
.type
== NET_PLATFORM
) {
881 char str
[ALTIFNAMSIZ
];
883 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.platform_path
))
884 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
888 /* get netdevsim path names */
889 if (names_netdevsim(dev
, &names
) >= 0 && names
.type
== NET_NETDEVSIM
) {
890 char str
[ALTIFNAMSIZ
];
892 if (snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.netdevsim_path
))
893 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
898 /* get PCI based path names, we compose only PCI based paths */
899 if (names_pci(dev
, &names
) < 0)
902 /* plain PCI device */
903 if (names
.type
== NET_PCI
) {
904 char str
[ALTIFNAMSIZ
];
906 if (names
.pci_onboard
[0] &&
907 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_onboard
))
908 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_ONBOARD", str
);
910 if (names
.pci_onboard_label
&&
911 snprintf_ok(str
, sizeof str
, "%s%s",
912 naming_scheme_has(NAMING_LABEL_NOPREFIX
) ? "" : prefix
,
913 names
.pci_onboard_label
))
914 udev_builtin_add_property(dev
, test
, "ID_NET_LABEL_ONBOARD", str
);
916 if (names
.pci_path
[0] &&
917 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_path
))
918 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
920 if (names
.pci_slot
[0] &&
921 snprintf_ok(str
, sizeof str
, "%s%s", prefix
, names
.pci_slot
))
922 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
927 if (names_usb(dev
, &names
) >= 0 && names
.type
== NET_USB
) {
928 char str
[ALTIFNAMSIZ
];
930 if (names
.pci_path
[0] &&
931 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_path
, names
.usb_ports
))
932 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
934 if (names
.pci_slot
[0] &&
935 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_slot
, names
.usb_ports
))
936 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
941 if (names_bcma(dev
, &names
) >= 0 && names
.type
== NET_BCMA
) {
942 char str
[ALTIFNAMSIZ
];
944 if (names
.pci_path
[0] &&
945 snprintf_ok(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_path
, names
.bcma_core
))
946 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_PATH", str
);
948 if (names
.pci_slot
[0] &&
949 snprintf(str
, sizeof str
, "%s%s%s", prefix
, names
.pci_slot
, names
.bcma_core
))
950 udev_builtin_add_property(dev
, test
, "ID_NET_NAME_SLOT", str
);
957 const UdevBuiltin udev_builtin_net_id
= {
959 .cmd
= builtin_net_id
,
960 .help
= "Network device properties",