]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-net_id.c
docs: direct to systemd.io version of naming scheme docs
[thirdparty/systemd.git] / src / udev / udev-builtin-net_id.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 /*
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
9 *
10 * https://systemd.io/PREDICTABLE_INTERFACE_NAMES
11 *
12 * When the code here is changed, man/systemd.net-naming-scheme.xml must be updated too.
13 */
14
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <net/if.h>
18 #include <net/if_arp.h>
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <linux/pci_regs.h>
23
24 #include "alloc-util.h"
25 #include "dirent-util.h"
26 #include "fd-util.h"
27 #include "fileio.h"
28 #include "fs-util.h"
29 #include "naming-scheme.h"
30 #include "parse-util.h"
31 #include "proc-cmdline.h"
32 #include "stdio-util.h"
33 #include "string-util.h"
34 #include "strv.h"
35 #include "strxcpyx.h"
36 #include "udev-builtin.h"
37
38 #define ONBOARD_INDEX_MAX (16*1024-1)
39
40 enum netname_type{
41 NET_UNDEF,
42 NET_PCI,
43 NET_USB,
44 NET_BCMA,
45 NET_VIRTIO,
46 NET_CCW,
47 NET_VIO,
48 NET_PLATFORM,
49 NET_NETDEVSIM,
50 };
51
52 struct netnames {
53 enum netname_type type;
54
55 uint8_t mac[6];
56 bool mac_valid;
57
58 sd_device *pcidev;
59 char pci_slot[IFNAMSIZ];
60 char pci_path[IFNAMSIZ];
61 char pci_onboard[IFNAMSIZ];
62 const char *pci_onboard_label;
63
64 char usb_ports[IFNAMSIZ];
65 char bcma_core[IFNAMSIZ];
66 char ccw_busid[IFNAMSIZ];
67 char vio_slot[IFNAMSIZ];
68 char platform_path[IFNAMSIZ];
69 char netdevsim_path[IFNAMSIZ];
70 };
71
72 struct virtfn_info {
73 sd_device *physfn_pcidev;
74 char suffix[IFNAMSIZ];
75 };
76
77 /* skip intermediate virtio devices */
78 static sd_device *skip_virtio(sd_device *dev) {
79 sd_device *parent;
80
81 /* there can only ever be one virtio bus per parent device, so we can
82 * safely ignore any virtio buses. see
83 * http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html */
84 for (parent = dev; parent; ) {
85 const char *subsystem;
86
87 if (sd_device_get_subsystem(parent, &subsystem) < 0)
88 break;
89
90 if (!streq(subsystem, "virtio"))
91 break;
92
93 if (sd_device_get_parent(parent, &parent) < 0)
94 return NULL;
95 }
96
97 return parent;
98 }
99
100 static int get_virtfn_info(sd_device *dev, struct netnames *names, struct virtfn_info *ret) {
101 _cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL;
102 const char *physfn_link_file, *syspath;
103 _cleanup_free_ char *physfn_pci_syspath = NULL;
104 _cleanup_free_ char *virtfn_pci_syspath = NULL;
105 struct dirent *dent;
106 _cleanup_closedir_ DIR *dir = NULL;
107 char suffix[IFNAMSIZ];
108 int r;
109
110 assert(dev);
111 assert(names);
112 assert(ret);
113
114 r = sd_device_get_syspath(names->pcidev, &syspath);
115 if (r < 0)
116 return r;
117
118 /* Check if this is a virtual function. */
119 physfn_link_file = strjoina(syspath, "/physfn");
120 r = chase_symlinks(physfn_link_file, NULL, 0, &physfn_pci_syspath, NULL);
121 if (r < 0)
122 return r;
123
124 /* Get physical function's pci device. */
125 r = sd_device_new_from_syspath(&physfn_pcidev, physfn_pci_syspath);
126 if (r < 0)
127 return r;
128
129 /* Find the virtual function number by finding the right virtfn link. */
130 dir = opendir(physfn_pci_syspath);
131 if (!dir)
132 return -errno;
133
134 FOREACH_DIRENT_ALL(dent, dir, break) {
135 _cleanup_free_ char *virtfn_link_file = NULL;
136
137 if (!startswith(dent->d_name, "virtfn"))
138 continue;
139
140 virtfn_link_file = path_join(physfn_pci_syspath, dent->d_name);
141 if (!virtfn_link_file)
142 return -ENOMEM;
143
144 if (chase_symlinks(virtfn_link_file, NULL, 0, &virtfn_pci_syspath, NULL) < 0)
145 continue;
146
147 if (streq(syspath, virtfn_pci_syspath)) {
148 if (!snprintf_ok(suffix, sizeof(suffix), "v%s", &dent->d_name[6]))
149 return -ENOENT;
150
151 break;
152 }
153 }
154 if (isempty(suffix))
155 return -ENOENT;
156
157 ret->physfn_pcidev = TAKE_PTR(physfn_pcidev);
158 strncpy(ret->suffix, suffix, sizeof(ret->suffix));
159
160 return 0;
161 }
162
163 /* retrieve on-board index number and label from firmware */
164 static int dev_pci_onboard(sd_device *dev, struct netnames *names) {
165 unsigned long idx, dev_port = 0;
166 const char *attr, *port_name = NULL;
167 size_t l;
168 char *s;
169 int r;
170
171 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
172 if (sd_device_get_sysattr_value(names->pcidev, "acpi_index", &attr) < 0) {
173 /* SMBIOS type 41 — Onboard Devices Extended Information */
174 r = sd_device_get_sysattr_value(names->pcidev, "index", &attr);
175 if (r < 0)
176 return r;
177 }
178
179 r = safe_atolu(attr, &idx);
180 if (r < 0)
181 return r;
182 if (idx == 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX))
183 return -EINVAL;
184
185 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to
186 * report for example). Let's define a cut-off where we don't consider the index reliable anymore. We
187 * pick some arbitrary cut-off, which is somewhere beyond the realistic number of physical network
188 * interface a system might have. Ideally the kernel would already filter his crap for us, but it
189 * doesn't currently. */
190 if (idx > ONBOARD_INDEX_MAX)
191 return -ENOENT;
192
193 /* kernel provided port index for multiple ports on a single PCI function */
194 if (sd_device_get_sysattr_value(dev, "dev_port", &attr) >= 0)
195 dev_port = strtoul(attr, NULL, 10);
196
197 /* kernel provided front panel port name for multiple port PCI device */
198 (void) sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
199
200 s = names->pci_onboard;
201 l = sizeof(names->pci_onboard);
202 l = strpcpyf(&s, l, "o%lu", idx);
203 if (port_name)
204 l = strpcpyf(&s, l, "n%s", port_name);
205 else if (dev_port > 0)
206 l = strpcpyf(&s, l, "d%lu", dev_port);
207 if (l == 0)
208 names->pci_onboard[0] = '\0';
209
210 if (sd_device_get_sysattr_value(names->pcidev, "label", &names->pci_onboard_label) < 0)
211 names->pci_onboard_label = NULL;
212
213 return 0;
214 }
215
216 /* read the 256 bytes PCI configuration space to check the multi-function bit */
217 static bool is_pci_multifunction(sd_device *dev) {
218 _cleanup_close_ int fd = -1;
219 const char *filename, *syspath;
220 uint8_t config[64];
221
222 if (sd_device_get_syspath(dev, &syspath) < 0)
223 return false;
224
225 filename = strjoina(syspath, "/config");
226 fd = open(filename, O_RDONLY | O_CLOEXEC);
227 if (fd < 0)
228 return false;
229 if (read(fd, &config, sizeof(config)) != sizeof(config))
230 return false;
231
232 /* bit 0-6 header type, bit 7 multi/single function device */
233 return config[PCI_HEADER_TYPE] & 0x80;
234 }
235
236 static bool is_pci_ari_enabled(sd_device *dev) {
237 const char *a;
238
239 if (sd_device_get_sysattr_value(dev, "ari_enabled", &a) < 0)
240 return false;
241
242 return streq(a, "1");
243 }
244
245 static int dev_pci_slot(sd_device *dev, struct netnames *names) {
246 unsigned long dev_port = 0;
247 unsigned domain, bus, slot, func, hotplug_slot = 0;
248 size_t l;
249 char *s;
250 const char *sysname, *attr, *port_name = NULL, *syspath;
251 _cleanup_(sd_device_unrefp) sd_device *pci = NULL;
252 sd_device *hotplug_slot_dev;
253 char slots[PATH_MAX];
254 _cleanup_closedir_ DIR *dir = NULL;
255 struct dirent *dent;
256 int r;
257
258 r = sd_device_get_sysname(names->pcidev, &sysname);
259 if (r < 0)
260 return r;
261
262 if (sscanf(sysname, "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
263 return -ENOENT;
264
265 if (naming_scheme_has(NAMING_NPAR_ARI) &&
266 is_pci_ari_enabled(names->pcidev))
267 /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
268 * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
269 * where the slot makes up the upper 5 bits. */
270 func += slot * 8;
271
272 /* kernel provided port index for multiple ports on a single PCI function */
273 if (sd_device_get_sysattr_value(dev, "dev_port", &attr) >= 0) {
274 dev_port = strtoul(attr, NULL, 10);
275 /* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously
276 * provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
277 * which thus stays initialized as 0. */
278 if (dev_port == 0 &&
279 sd_device_get_sysattr_value(dev, "type", &attr) >= 0) {
280 unsigned long type;
281
282 type = strtoul(attr, NULL, 10);
283 if (type == ARPHRD_INFINIBAND &&
284 sd_device_get_sysattr_value(dev, "dev_id", &attr) >= 0)
285 dev_port = strtoul(attr, NULL, 16);
286 }
287 }
288
289 /* kernel provided front panel port name for multi-port PCI device */
290 (void) sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
291
292 /* compose a name based on the raw kernel's PCI bus, slot numbers */
293 s = names->pci_path;
294 l = sizeof(names->pci_path);
295 if (domain > 0)
296 l = strpcpyf(&s, l, "P%u", domain);
297 l = strpcpyf(&s, l, "p%us%u", bus, slot);
298 if (func > 0 || is_pci_multifunction(names->pcidev))
299 l = strpcpyf(&s, l, "f%u", func);
300 if (port_name)
301 l = strpcpyf(&s, l, "n%s", port_name);
302 else if (dev_port > 0)
303 l = strpcpyf(&s, l, "d%lu", dev_port);
304 if (l == 0)
305 names->pci_path[0] = '\0';
306
307 /* ACPI _SUN — slot user number */
308 r = sd_device_new_from_subsystem_sysname(&pci, "subsystem", "pci");
309 if (r < 0)
310 return r;
311
312 r = sd_device_get_syspath(pci, &syspath);
313 if (r < 0)
314 return r;
315 if (!snprintf_ok(slots, sizeof slots, "%s/slots", syspath))
316 return -ENAMETOOLONG;
317
318 dir = opendir(slots);
319 if (!dir)
320 return -errno;
321
322 hotplug_slot_dev = names->pcidev;
323 while (hotplug_slot_dev) {
324 if (sd_device_get_sysname(hotplug_slot_dev, &sysname) < 0)
325 continue;
326
327 FOREACH_DIRENT_ALL(dent, dir, break) {
328 unsigned i;
329 char str[PATH_MAX];
330 _cleanup_free_ char *address = NULL;
331
332 if (dent->d_name[0] == '.')
333 continue;
334 r = safe_atou_full(dent->d_name, 10, &i);
335 if (r < 0 || i <= 0)
336 continue;
337
338 /* match slot address with device by stripping the function */
339 if (snprintf_ok(str, sizeof str, "%s/%s/address", slots, dent->d_name) &&
340 read_one_line_file(str, &address) >= 0 &&
341 startswith(sysname, address)) {
342 hotplug_slot = i;
343 break;
344 }
345 }
346 if (hotplug_slot > 0)
347 break;
348 if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev, "pci", NULL, &hotplug_slot_dev) < 0)
349 break;
350 rewinddir(dir);
351 }
352
353 if (hotplug_slot > 0) {
354 s = names->pci_slot;
355 l = sizeof(names->pci_slot);
356 if (domain > 0)
357 l = strpcpyf(&s, l, "P%d", domain);
358 l = strpcpyf(&s, l, "s%d", hotplug_slot);
359 if (func > 0 || is_pci_multifunction(names->pcidev))
360 l = strpcpyf(&s, l, "f%d", func);
361 if (port_name)
362 l = strpcpyf(&s, l, "n%s", port_name);
363 else if (dev_port > 0)
364 l = strpcpyf(&s, l, "d%lu", dev_port);
365 if (l == 0)
366 names->pci_slot[0] = '\0';
367 }
368
369 return 0;
370 }
371
372 static int names_vio(sd_device *dev, struct netnames *names) {
373 sd_device *parent;
374 unsigned busid, slotid, ethid;
375 const char *syspath, *subsystem;
376 int r;
377
378 /* check if our direct parent is a VIO device with no other bus in-between */
379 r = sd_device_get_parent(dev, &parent);
380 if (r < 0)
381 return r;
382
383 r = sd_device_get_subsystem(parent, &subsystem);
384 if (r < 0)
385 return r;
386 if (!streq("vio", subsystem))
387 return -ENOENT;
388
389 /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
390 * selected in the HMC), thus this provides a reliable naming (e.g.
391 * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
392 * there should only ever be one bus, and then remove leading zeros. */
393 r = sd_device_get_syspath(dev, &syspath);
394 if (r < 0)
395 return r;
396
397 if (sscanf(syspath, "/sys/devices/vio/%4x%4x/net/eth%u", &busid, &slotid, &ethid) != 3)
398 return -EINVAL;
399
400 xsprintf(names->vio_slot, "v%u", slotid);
401 names->type = NET_VIO;
402 return 0;
403 }
404
405 #define _PLATFORM_TEST "/sys/devices/platform/vvvvPPPP"
406 #define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
407 #define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
408
409 static int names_platform(sd_device *dev, struct netnames *names, bool test) {
410 sd_device *parent;
411 char vendor[5];
412 unsigned model, instance, ethid;
413 const char *syspath, *pattern, *validchars, *subsystem;
414 int r;
415
416 /* check if our direct parent is a platform device with no other bus in-between */
417 r = sd_device_get_parent(dev, &parent);
418 if (r < 0)
419 return r;
420
421 r = sd_device_get_subsystem(parent, &subsystem);
422 if (r < 0)
423 return r;
424
425 if (!streq("platform", subsystem))
426 return -ENOENT;
427
428 r = sd_device_get_syspath(dev, &syspath);
429 if (r < 0)
430 return r;
431
432 /* syspath is too short, to have a valid ACPI instance */
433 if (strlen(syspath) < sizeof _PLATFORM_TEST)
434 return -EINVAL;
435
436 /* Vendor ID can be either PNP ID (3 chars A-Z) or ACPI ID (4 chars A-Z and numerals) */
437 if (syspath[sizeof _PLATFORM_TEST - 1] == ':') {
438 pattern = _PLATFORM_PATTERN4;
439 validchars = UPPERCASE_LETTERS DIGITS;
440 } else {
441 pattern = _PLATFORM_PATTERN3;
442 validchars = UPPERCASE_LETTERS;
443 }
444
445 /* Platform devices are named after ACPI table match, and instance id
446 * eg. "/sys/devices/platform/HISI00C2:00");
447 * The Vendor (3 or 4 char), followed by hexdecimal model number : instance id.
448 */
449
450 #pragma GCC diagnostic push
451 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
452 if (sscanf(syspath, pattern, vendor, &model, &instance, &ethid) != 4)
453 return -EINVAL;
454 #pragma GCC diagnostic pop
455
456 if (!in_charset(vendor, validchars))
457 return -ENOENT;
458
459 ascii_strlower(vendor);
460
461 xsprintf(names->platform_path, "a%s%xi%u", vendor, model, instance);
462 names->type = NET_PLATFORM;
463 return 0;
464 }
465
466 static int names_pci(sd_device *dev, struct netnames *names) {
467 sd_device *parent;
468 struct netnames vf_names = {};
469 struct virtfn_info vf_info = {};
470 const char *subsystem;
471 int r;
472
473 assert(dev);
474 assert(names);
475
476 r = sd_device_get_parent(dev, &parent);
477 if (r < 0)
478 return r;
479 /* skip virtio subsystem if present */
480 parent = skip_virtio(parent);
481
482 if (!parent)
483 return -ENOENT;
484
485 /* check if our direct parent is a PCI device with no other bus in-between */
486 if (sd_device_get_subsystem(parent, &subsystem) >= 0 &&
487 streq("pci", subsystem)) {
488 names->type = NET_PCI;
489 names->pcidev = parent;
490 } else {
491 r = sd_device_get_parent_with_subsystem_devtype(dev, "pci", NULL, &names->pcidev);
492 if (r < 0)
493 return r;
494 }
495
496 if (naming_scheme_has(NAMING_SR_IOV_V) &&
497 get_virtfn_info(dev, names, &vf_info) >= 0) {
498 /* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
499 vf_names.pcidev = vf_info.physfn_pcidev;
500 dev_pci_onboard(dev, &vf_names);
501 dev_pci_slot(dev, &vf_names);
502 if (vf_names.pci_onboard[0])
503 if (strlen(vf_names.pci_onboard) + strlen(vf_info.suffix) < sizeof(names->pci_onboard))
504 strscpyl(names->pci_onboard, sizeof(names->pci_onboard),
505 vf_names.pci_onboard, vf_info.suffix, NULL);
506 if (vf_names.pci_slot[0])
507 if (strlen(vf_names.pci_slot) + strlen(vf_info.suffix) < sizeof(names->pci_slot))
508 strscpyl(names->pci_slot, sizeof(names->pci_slot),
509 vf_names.pci_slot, vf_info.suffix, NULL);
510 if (vf_names.pci_path[0])
511 if (strlen(vf_names.pci_path) + strlen(vf_info.suffix) < sizeof(names->pci_path))
512 strscpyl(names->pci_path, sizeof(names->pci_path),
513 vf_names.pci_path, vf_info.suffix, NULL);
514 sd_device_unref(vf_info.physfn_pcidev);
515 } else {
516 dev_pci_onboard(dev, names);
517 dev_pci_slot(dev, names);
518 }
519
520 return 0;
521 }
522
523 static int names_usb(sd_device *dev, struct netnames *names) {
524 sd_device *usbdev;
525 char name[256], *ports, *config, *interf, *s;
526 const char *sysname;
527 size_t l;
528 int r;
529
530 assert(dev);
531 assert(names);
532
533 r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &usbdev);
534 if (r < 0)
535 return r;
536
537 r = sd_device_get_sysname(usbdev, &sysname);
538 if (r < 0)
539 return r;
540
541 /* get USB port number chain, configuration, interface */
542 strscpy(name, sizeof(name), sysname);
543 s = strchr(name, '-');
544 if (!s)
545 return -EINVAL;
546 ports = s+1;
547
548 s = strchr(ports, ':');
549 if (!s)
550 return -EINVAL;
551 s[0] = '\0';
552 config = s+1;
553
554 s = strchr(config, '.');
555 if (!s)
556 return -EINVAL;
557 s[0] = '\0';
558 interf = s+1;
559
560 /* prefix every port number in the chain with "u" */
561 s = ports;
562 while ((s = strchr(s, '.')))
563 s[0] = 'u';
564 s = names->usb_ports;
565 l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
566
567 /* append USB config number, suppress the common config == 1 */
568 if (!streq(config, "1"))
569 l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
570
571 /* append USB interface number, suppress the interface == 0 */
572 if (!streq(interf, "0"))
573 l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
574 if (l == 0)
575 return -ENAMETOOLONG;
576
577 names->type = NET_USB;
578 return 0;
579 }
580
581 static int names_bcma(sd_device *dev, struct netnames *names) {
582 sd_device *bcmadev;
583 unsigned core;
584 const char *sysname;
585 int r;
586
587 assert(dev);
588 assert(names);
589
590 r = sd_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL, &bcmadev);
591 if (r < 0)
592 return r;
593
594 r = sd_device_get_sysname(bcmadev, &sysname);
595 if (r < 0)
596 return r;
597
598 /* bus num:core num */
599 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
600 return -EINVAL;
601 /* suppress the common core == 0 */
602 if (core > 0)
603 xsprintf(names->bcma_core, "b%u", core);
604
605 names->type = NET_BCMA;
606 return 0;
607 }
608
609 static int names_ccw(sd_device *dev, struct netnames *names) {
610 sd_device *cdev;
611 const char *bus_id, *subsys;
612 size_t bus_id_len;
613 size_t bus_id_start;
614 int r;
615
616 assert(dev);
617 assert(names);
618
619 /* Retrieve the associated CCW device */
620 r = sd_device_get_parent(dev, &cdev);
621 if (r < 0)
622 return r;
623
624 /* skip virtio subsystem if present */
625 cdev = skip_virtio(cdev);
626 if (!cdev)
627 return -ENOENT;
628
629 r = sd_device_get_subsystem(cdev, &subsys);
630 if (r < 0)
631 return r;
632
633 /* Network devices are either single or grouped CCW devices */
634 if (!STR_IN_SET(subsys, "ccwgroup", "ccw"))
635 return -ENOENT;
636
637 /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
638 * identifies the network device on the Linux on System z channel
639 * subsystem. Note that the bus-ID contains lowercase characters.
640 */
641 r = sd_device_get_sysname(cdev, &bus_id);
642 if (r < 0)
643 return r;
644
645 /* Check the length of the bus-ID. Rely on the fact that the kernel provides a correct bus-ID;
646 * alternatively, improve this check and parse and verify each bus-ID part...
647 */
648 bus_id_len = strlen(bus_id);
649 if (!IN_SET(bus_id_len, 8, 9))
650 return -EINVAL;
651
652 /* Strip leading zeros from the bus id for aesthetic purposes. This
653 * keeps the ccw names stable, yet much shorter in general case of
654 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
655 * not prepended when it is zero. Preserve the last 0 for 0.0.0000.
656 */
657 bus_id_start = strspn(bus_id, ".0");
658 bus_id += bus_id_start < bus_id_len ? bus_id_start : bus_id_len - 1;
659
660 /* Store the CCW bus-ID for use as network device name */
661 if (snprintf_ok(names->ccw_busid, sizeof(names->ccw_busid), "c%s", bus_id))
662 names->type = NET_CCW;
663
664 return 0;
665 }
666
667 static int names_mac(sd_device *dev, struct netnames *names) {
668 const char *s;
669 unsigned long i;
670 unsigned a1, a2, a3, a4, a5, a6;
671 int r;
672
673 /* Some kinds of devices tend to have hardware addresses
674 * that are impossible to use in an iface name.
675 */
676 r = sd_device_get_sysattr_value(dev, "type", &s);
677 if (r < 0)
678 return r;
679
680 i = strtoul(s, NULL, 0);
681 switch (i) {
682 /* The persistent part of a hardware address of an InfiniBand NIC
683 * is 8 bytes long. We cannot fit this much in an iface name.
684 */
685 case ARPHRD_INFINIBAND:
686 return -EINVAL;
687 default:
688 break;
689 }
690
691 /* check for NET_ADDR_PERM, skip random MAC addresses */
692 r = sd_device_get_sysattr_value(dev, "addr_assign_type", &s);
693 if (r < 0)
694 return r;
695 i = strtoul(s, NULL, 0);
696 if (i != 0)
697 return 0;
698
699 r = sd_device_get_sysattr_value(dev, "address", &s);
700 if (r < 0)
701 return r;
702 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
703 return -EINVAL;
704
705 /* skip empty MAC addresses */
706 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
707 return -EINVAL;
708
709 names->mac[0] = a1;
710 names->mac[1] = a2;
711 names->mac[2] = a3;
712 names->mac[3] = a4;
713 names->mac[4] = a5;
714 names->mac[5] = a6;
715 names->mac_valid = true;
716 return 0;
717 }
718
719 static int names_netdevsim(sd_device *dev, struct netnames *names) {
720 sd_device *netdevsimdev;
721 const char *sysname;
722 unsigned addr;
723 const char *port_name = NULL;
724 int r;
725 bool ok;
726
727 if (!naming_scheme_has(NAMING_NETDEVSIM))
728 return 0;
729
730 assert(dev);
731 assert(names);
732
733 r = sd_device_get_parent_with_subsystem_devtype(dev, "netdevsim", NULL, &netdevsimdev);
734 if (r < 0)
735 return r;
736 r = sd_device_get_sysname(netdevsimdev, &sysname);
737 if (r < 0)
738 return r;
739
740 if (sscanf(sysname, "netdevsim%u", &addr) != 1)
741 return -EINVAL;
742
743 r = sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
744 if (r < 0)
745 return r;
746
747 ok = snprintf_ok(names->netdevsim_path, sizeof(names->netdevsim_path), "i%un%s", addr, port_name);
748 if (!ok)
749 return -ENOBUFS;
750
751 names->type = NET_NETDEVSIM;
752
753 return 0;
754 }
755
756 /* IEEE Organizationally Unique Identifier vendor string */
757 static int ieee_oui(sd_device *dev, struct netnames *names, bool test) {
758 char str[32];
759
760 if (!names->mac_valid)
761 return -ENOENT;
762 /* skip commonly misused 00:00:00 (Xerox) prefix */
763 if (memcmp(names->mac, "\0\0\0", 3) == 0)
764 return -EINVAL;
765 xsprintf(str, "OUI:%02X%02X%02X%02X%02X%02X", names->mac[0],
766 names->mac[1], names->mac[2], names->mac[3], names->mac[4],
767 names->mac[5]);
768 udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
769 return 0;
770 }
771
772 static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
773 const char *s, *p, *devtype, *prefix = "en";
774 struct netnames names = {};
775 unsigned long i;
776 int r;
777
778 /* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
779 r = sd_device_get_sysattr_value(dev, "type", &s);
780 if (r < 0)
781 return r;
782
783 i = strtoul(s, NULL, 0);
784 switch (i) {
785 case ARPHRD_ETHER:
786 prefix = "en";
787 break;
788 case ARPHRD_INFINIBAND:
789 if (naming_scheme_has(NAMING_INFINIBAND))
790 prefix = "ib";
791 else
792 return 0;
793 break;
794 case ARPHRD_SLIP:
795 prefix = "sl";
796 break;
797 default:
798 return 0;
799 }
800
801 /* skip stacked devices, like VLANs, ... */
802 r = sd_device_get_sysattr_value(dev, "ifindex", &s);
803 if (r < 0)
804 return r;
805 r = sd_device_get_sysattr_value(dev, "iflink", &p);
806 if (r < 0)
807 return r;
808 if (!streq(s, p))
809 return 0;
810
811 if (sd_device_get_devtype(dev, &devtype) >= 0) {
812 if (streq("wlan", devtype))
813 prefix = "wl";
814 else if (streq("wwan", devtype))
815 prefix = "ww";
816 }
817
818 udev_builtin_add_property(dev, test, "ID_NET_NAMING_SCHEME", naming_scheme()->name);
819
820 r = names_mac(dev, &names);
821 if (r >= 0 && names.mac_valid) {
822 char str[IFNAMSIZ];
823
824 xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix,
825 names.mac[0], names.mac[1], names.mac[2],
826 names.mac[3], names.mac[4], names.mac[5]);
827 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
828
829 ieee_oui(dev, &names, test);
830 }
831
832 /* get path names for Linux on System z network devices */
833 if (names_ccw(dev, &names) >= 0 && names.type == NET_CCW) {
834 char str[IFNAMSIZ];
835
836 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.ccw_busid))
837 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
838 return 0;
839 }
840
841 /* get ibmveth/ibmvnic slot-based names. */
842 if (names_vio(dev, &names) >= 0 && names.type == NET_VIO) {
843 char str[IFNAMSIZ];
844
845 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.vio_slot))
846 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
847 return 0;
848 }
849
850 /* get ACPI path names for ARM64 platform devices */
851 if (names_platform(dev, &names, test) >= 0 && names.type == NET_PLATFORM) {
852 char str[IFNAMSIZ];
853
854 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.platform_path))
855 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
856 return 0;
857 }
858
859 /* get netdevsim path names */
860 if (names_netdevsim(dev, &names) >= 0 && names.type == NET_NETDEVSIM) {
861 char str[IFNAMSIZ];
862
863 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.netdevsim_path))
864 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
865
866 return 0;
867 }
868
869 /* get PCI based path names, we compose only PCI based paths */
870 if (names_pci(dev, &names) < 0)
871 return 0;
872
873 /* plain PCI device */
874 if (names.type == NET_PCI) {
875 char str[IFNAMSIZ];
876
877 if (names.pci_onboard[0] &&
878 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_onboard))
879 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
880
881 if (names.pci_onboard_label &&
882 snprintf_ok(str, sizeof str, "%s%s",
883 naming_scheme_has(NAMING_LABEL_NOPREFIX) ? "" : prefix,
884 names.pci_onboard_label))
885 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
886
887 if (names.pci_path[0] &&
888 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_path))
889 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
890
891 if (names.pci_slot[0] &&
892 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_slot))
893 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
894 return 0;
895 }
896
897 /* USB device */
898 if (names_usb(dev, &names) >= 0 && names.type == NET_USB) {
899 char str[IFNAMSIZ];
900
901 if (names.pci_path[0] &&
902 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.usb_ports))
903 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
904
905 if (names.pci_slot[0] &&
906 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.usb_ports))
907 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
908 return 0;
909 }
910
911 /* Broadcom bus */
912 if (names_bcma(dev, &names) >= 0 && names.type == NET_BCMA) {
913 char str[IFNAMSIZ];
914
915 if (names.pci_path[0] &&
916 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.bcma_core))
917 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
918
919 if (names.pci_slot[0] &&
920 snprintf(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.bcma_core))
921 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
922 return 0;
923 }
924
925 return 0;
926 }
927
928 const UdevBuiltin udev_builtin_net_id = {
929 .name = "net_id",
930 .cmd = builtin_net_id,
931 .help = "Network device properties",
932 };