]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-net_id.c
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / src / udev / udev-builtin-net_id.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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/if.h>
23 #include <linux/pci_regs.h>
24
25 #include "alloc-util.h"
26 #include "dirent-util.h"
27 #include "fd-util.h"
28 #include "fileio.h"
29 #include "fs-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"
35 #include "strv.h"
36 #include "strxcpyx.h"
37 #include "udev-builtin.h"
38
39 #define ONBOARD_INDEX_MAX (16*1024-1)
40
41 enum netname_type{
42 NET_UNDEF,
43 NET_PCI,
44 NET_USB,
45 NET_BCMA,
46 NET_VIRTIO,
47 NET_CCW,
48 NET_VIO,
49 NET_PLATFORM,
50 NET_NETDEVSIM,
51 };
52
53 struct netnames {
54 enum netname_type type;
55
56 uint8_t mac[6];
57 bool mac_valid;
58
59 sd_device *pcidev;
60 char pci_slot[ALTIFNAMSIZ];
61 char pci_path[ALTIFNAMSIZ];
62 char pci_onboard[ALTIFNAMSIZ];
63 const char *pci_onboard_label;
64
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];
71 };
72
73 struct virtfn_info {
74 sd_device *physfn_pcidev;
75 char suffix[ALTIFNAMSIZ];
76 };
77
78 /* skip intermediate virtio devices */
79 static sd_device *skip_virtio(sd_device *dev) {
80 sd_device *parent;
81
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;
87
88 if (sd_device_get_subsystem(parent, &subsystem) < 0)
89 break;
90
91 if (!streq(subsystem, "virtio"))
92 break;
93
94 if (sd_device_get_parent(parent, &parent) < 0)
95 return NULL;
96 }
97
98 return parent;
99 }
100
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;
106 struct dirent *dent;
107 _cleanup_closedir_ DIR *dir = NULL;
108 char suffix[ALTIFNAMSIZ];
109 int r;
110
111 assert(dev);
112 assert(names);
113 assert(ret);
114
115 r = sd_device_get_syspath(names->pcidev, &syspath);
116 if (r < 0)
117 return r;
118
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);
122 if (r < 0)
123 return r;
124
125 /* Get physical function's pci device. */
126 r = sd_device_new_from_syspath(&physfn_pcidev, physfn_pci_syspath);
127 if (r < 0)
128 return r;
129
130 /* Find the virtual function number by finding the right virtfn link. */
131 dir = opendir(physfn_pci_syspath);
132 if (!dir)
133 return -errno;
134
135 FOREACH_DIRENT_ALL(dent, dir, break) {
136 _cleanup_free_ char *virtfn_link_file = NULL;
137
138 if (!startswith(dent->d_name, "virtfn"))
139 continue;
140
141 virtfn_link_file = path_join(physfn_pci_syspath, dent->d_name);
142 if (!virtfn_link_file)
143 return -ENOMEM;
144
145 if (chase_symlinks(virtfn_link_file, NULL, 0, &virtfn_pci_syspath, NULL) < 0)
146 continue;
147
148 if (streq(syspath, virtfn_pci_syspath)) {
149 if (!snprintf_ok(suffix, sizeof(suffix), "v%s", &dent->d_name[6]))
150 return -ENOENT;
151
152 break;
153 }
154 }
155 if (isempty(suffix))
156 return -ENOENT;
157
158 ret->physfn_pcidev = TAKE_PTR(physfn_pcidev);
159 strncpy(ret->suffix, suffix, sizeof(ret->suffix));
160
161 return 0;
162 }
163
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;
168 size_t l;
169 char *s;
170 int r;
171
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);
176 if (r < 0)
177 return r;
178 }
179
180 r = safe_atolu(attr, &idx);
181 if (r < 0)
182 return r;
183 if (idx == 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX))
184 return -EINVAL;
185
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)
192 return -ENOENT;
193
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);
197
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);
200
201 s = names->pci_onboard;
202 l = sizeof(names->pci_onboard);
203 l = strpcpyf(&s, l, "o%lu", idx);
204 if (port_name)
205 l = strpcpyf(&s, l, "n%s", port_name);
206 else if (dev_port > 0)
207 l = strpcpyf(&s, l, "d%lu", dev_port);
208 if (l == 0)
209 names->pci_onboard[0] = '\0';
210
211 if (sd_device_get_sysattr_value(names->pcidev, "label", &names->pci_onboard_label) < 0)
212 names->pci_onboard_label = NULL;
213
214 return 0;
215 }
216
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;
221 uint8_t config[64];
222
223 if (sd_device_get_syspath(dev, &syspath) < 0)
224 return false;
225
226 filename = strjoina(syspath, "/config");
227 fd = open(filename, O_RDONLY | O_CLOEXEC);
228 if (fd < 0)
229 return false;
230 if (read(fd, &config, sizeof(config)) != sizeof(config))
231 return false;
232
233 /* bit 0-6 header type, bit 7 multi/single function device */
234 return config[PCI_HEADER_TYPE] & 0x80;
235 }
236
237 static bool is_pci_ari_enabled(sd_device *dev) {
238 const char *a;
239
240 if (sd_device_get_sysattr_value(dev, "ari_enabled", &a) < 0)
241 return false;
242
243 return streq(a, "1");
244 }
245
246 static bool is_pci_bridge(sd_device *dev) {
247 const char *v, *p;
248
249 if (sd_device_get_sysattr_value(dev, "modalias", &v) < 0)
250 return false;
251
252 if (!startswith(v, "pci:"))
253 return false;
254
255 p = strrchr(v, 's');
256 if (!p)
257 return false;
258 if (p[1] != 'c')
259 return false;
260
261 /* PCI device subclass 04 corresponds to PCI bridge */
262 return strneq(p + 2, "04", 2);
263 }
264
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;
269 size_t l;
270 char *s;
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;
276 struct dirent *dent;
277 int r;
278
279 r = sd_device_get_sysname(names->pcidev, &sysname);
280 if (r < 0)
281 return r;
282
283 if (sscanf(sysname, "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
284 return -ENOENT;
285
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. */
291 func += slot * 8;
292
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. */
299 if (dev_port == 0 &&
300 sd_device_get_sysattr_value(dev, "type", &attr) >= 0) {
301 unsigned long type;
302
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);
307 }
308 }
309
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);
312
313 /* compose a name based on the raw kernel's PCI bus, slot numbers */
314 s = names->pci_path;
315 l = sizeof(names->pci_path);
316 if (domain > 0)
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);
321 if (port_name)
322 l = strpcpyf(&s, l, "n%s", port_name);
323 else if (dev_port > 0)
324 l = strpcpyf(&s, l, "d%lu", dev_port);
325 if (l == 0)
326 names->pci_path[0] = '\0';
327
328 /* ACPI _SUN — slot user number */
329 r = sd_device_new_from_subsystem_sysname(&pci, "subsystem", "pci");
330 if (r < 0)
331 return r;
332
333 r = sd_device_get_syspath(pci, &syspath);
334 if (r < 0)
335 return r;
336 if (!snprintf_ok(slots, sizeof slots, "%s/slots", syspath))
337 return -ENAMETOOLONG;
338
339 dir = opendir(slots);
340 if (!dir)
341 return -errno;
342
343 hotplug_slot_dev = names->pcidev;
344 while (hotplug_slot_dev) {
345 if (sd_device_get_sysname(hotplug_slot_dev, &sysname) < 0)
346 continue;
347
348 FOREACH_DIRENT_ALL(dent, dir, break) {
349 int i;
350 char str[PATH_MAX];
351 _cleanup_free_ char *address = NULL;
352
353 if (dot_or_dot_dot(dent->d_name))
354 continue;
355
356 r = safe_atoi(dent->d_name, &i);
357 if (r < 0 || i <= 0)
358 continue;
359
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)) {
364 hotplug_slot = i;
365
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
369 * collision. */
370 if (naming_scheme_has(NAMING_BRIDGE_NO_SLOT) && is_pci_bridge(hotplug_slot_dev))
371 hotplug_slot = 0;
372
373 break;
374 }
375 }
376 if (hotplug_slot >= 0)
377 break;
378 if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev, "pci", NULL, &hotplug_slot_dev) < 0)
379 break;
380 rewinddir(dir);
381 }
382
383 if (hotplug_slot > 0) {
384 s = names->pci_slot;
385 l = sizeof(names->pci_slot);
386 if (domain > 0)
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);
391 if (port_name)
392 l = strpcpyf(&s, l, "n%s", port_name);
393 else if (dev_port > 0)
394 l = strpcpyf(&s, l, "d%lu", dev_port);
395 if (l == 0)
396 names->pci_slot[0] = '\0';
397 }
398
399 return 0;
400 }
401
402 static int names_vio(sd_device *dev, struct netnames *names) {
403 sd_device *parent;
404 unsigned busid, slotid, ethid;
405 const char *syspath, *subsystem;
406 int r;
407
408 /* check if our direct parent is a VIO device with no other bus in-between */
409 r = sd_device_get_parent(dev, &parent);
410 if (r < 0)
411 return r;
412
413 r = sd_device_get_subsystem(parent, &subsystem);
414 if (r < 0)
415 return r;
416 if (!streq("vio", subsystem))
417 return -ENOENT;
418
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);
424 if (r < 0)
425 return r;
426
427 if (sscanf(syspath, "/sys/devices/vio/%4x%4x/net/eth%u", &busid, &slotid, &ethid) != 3)
428 return -EINVAL;
429
430 xsprintf(names->vio_slot, "v%u", slotid);
431 names->type = NET_VIO;
432 return 0;
433 }
434
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"
438
439 static int names_platform(sd_device *dev, struct netnames *names, bool test) {
440 sd_device *parent;
441 char vendor[5];
442 unsigned model, instance, ethid;
443 const char *syspath, *pattern, *validchars, *subsystem;
444 int r;
445
446 /* check if our direct parent is a platform device with no other bus in-between */
447 r = sd_device_get_parent(dev, &parent);
448 if (r < 0)
449 return r;
450
451 r = sd_device_get_subsystem(parent, &subsystem);
452 if (r < 0)
453 return r;
454
455 if (!streq("platform", subsystem))
456 return -ENOENT;
457
458 r = sd_device_get_syspath(dev, &syspath);
459 if (r < 0)
460 return r;
461
462 /* syspath is too short, to have a valid ACPI instance */
463 if (strlen(syspath) < sizeof _PLATFORM_TEST)
464 return -EINVAL;
465
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;
470 } else {
471 pattern = _PLATFORM_PATTERN3;
472 validchars = UPPERCASE_LETTERS;
473 }
474
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.
478 */
479
480 DISABLE_WARNING_FORMAT_NONLITERAL;
481 if (sscanf(syspath, pattern, vendor, &model, &instance, &ethid) != 4)
482 return -EINVAL;
483 REENABLE_WARNING;
484
485 if (!in_charset(vendor, validchars))
486 return -ENOENT;
487
488 ascii_strlower(vendor);
489
490 xsprintf(names->platform_path, "a%s%xi%u", vendor, model, instance);
491 names->type = NET_PLATFORM;
492 return 0;
493 }
494
495 static int names_pci(sd_device *dev, struct netnames *names) {
496 sd_device *parent;
497 struct netnames vf_names = {};
498 struct virtfn_info vf_info = {};
499 const char *subsystem;
500 int r;
501
502 assert(dev);
503 assert(names);
504
505 r = sd_device_get_parent(dev, &parent);
506 if (r < 0)
507 return r;
508 /* skip virtio subsystem if present */
509 parent = skip_virtio(parent);
510
511 if (!parent)
512 return -ENOENT;
513
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;
519 } else {
520 r = sd_device_get_parent_with_subsystem_devtype(dev, "pci", NULL, &names->pcidev);
521 if (r < 0)
522 return r;
523 }
524
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);
544 } else {
545 dev_pci_onboard(dev, names);
546 dev_pci_slot(dev, names);
547 }
548
549 return 0;
550 }
551
552 static int names_usb(sd_device *dev, struct netnames *names) {
553 sd_device *usbdev;
554 char name[256], *ports, *config, *interf, *s;
555 const char *sysname;
556 size_t l;
557 int r;
558
559 assert(dev);
560 assert(names);
561
562 r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &usbdev);
563 if (r < 0)
564 return r;
565
566 r = sd_device_get_sysname(usbdev, &sysname);
567 if (r < 0)
568 return r;
569
570 /* get USB port number chain, configuration, interface */
571 strscpy(name, sizeof(name), sysname);
572 s = strchr(name, '-');
573 if (!s)
574 return -EINVAL;
575 ports = s+1;
576
577 s = strchr(ports, ':');
578 if (!s)
579 return -EINVAL;
580 s[0] = '\0';
581 config = s+1;
582
583 s = strchr(config, '.');
584 if (!s)
585 return -EINVAL;
586 s[0] = '\0';
587 interf = s+1;
588
589 /* prefix every port number in the chain with "u" */
590 s = ports;
591 while ((s = strchr(s, '.')))
592 s[0] = 'u';
593 s = names->usb_ports;
594 l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
595
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);
599
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);
603 if (l == 0)
604 return -ENAMETOOLONG;
605
606 names->type = NET_USB;
607 return 0;
608 }
609
610 static int names_bcma(sd_device *dev, struct netnames *names) {
611 sd_device *bcmadev;
612 unsigned core;
613 const char *sysname;
614 int r;
615
616 assert(dev);
617 assert(names);
618
619 r = sd_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL, &bcmadev);
620 if (r < 0)
621 return r;
622
623 r = sd_device_get_sysname(bcmadev, &sysname);
624 if (r < 0)
625 return r;
626
627 /* bus num:core num */
628 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
629 return -EINVAL;
630 /* suppress the common core == 0 */
631 if (core > 0)
632 xsprintf(names->bcma_core, "b%u", core);
633
634 names->type = NET_BCMA;
635 return 0;
636 }
637
638 static int names_ccw(sd_device *dev, struct netnames *names) {
639 sd_device *cdev;
640 const char *bus_id, *subsys;
641 size_t bus_id_len;
642 size_t bus_id_start;
643 int r;
644
645 assert(dev);
646 assert(names);
647
648 /* Retrieve the associated CCW device */
649 r = sd_device_get_parent(dev, &cdev);
650 if (r < 0)
651 return r;
652
653 /* skip virtio subsystem if present */
654 cdev = skip_virtio(cdev);
655 if (!cdev)
656 return -ENOENT;
657
658 r = sd_device_get_subsystem(cdev, &subsys);
659 if (r < 0)
660 return r;
661
662 /* Network devices are either single or grouped CCW devices */
663 if (!STR_IN_SET(subsys, "ccwgroup", "ccw"))
664 return -ENOENT;
665
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.
669 */
670 r = sd_device_get_sysname(cdev, &bus_id);
671 if (r < 0)
672 return r;
673
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...
676 */
677 bus_id_len = strlen(bus_id);
678 if (!IN_SET(bus_id_len, 8, 9))
679 return -EINVAL;
680
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.
685 */
686 bus_id_start = strspn(bus_id, ".0");
687 bus_id += bus_id_start < bus_id_len ? bus_id_start : bus_id_len - 1;
688
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;
692
693 return 0;
694 }
695
696 static int names_mac(sd_device *dev, struct netnames *names) {
697 const char *s;
698 unsigned long i;
699 unsigned a1, a2, a3, a4, a5, a6;
700 int r;
701
702 /* Some kinds of devices tend to have hardware addresses
703 * that are impossible to use in an iface name.
704 */
705 r = sd_device_get_sysattr_value(dev, "type", &s);
706 if (r < 0)
707 return r;
708
709 i = strtoul(s, NULL, 0);
710 switch (i) {
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.
713 */
714 case ARPHRD_INFINIBAND:
715 return -EINVAL;
716 default:
717 break;
718 }
719
720 /* check for NET_ADDR_PERM, skip random MAC addresses */
721 r = sd_device_get_sysattr_value(dev, "addr_assign_type", &s);
722 if (r < 0)
723 return r;
724 i = strtoul(s, NULL, 0);
725 if (i != 0)
726 return 0;
727
728 r = sd_device_get_sysattr_value(dev, "address", &s);
729 if (r < 0)
730 return r;
731 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
732 return -EINVAL;
733
734 /* skip empty MAC addresses */
735 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
736 return -EINVAL;
737
738 names->mac[0] = a1;
739 names->mac[1] = a2;
740 names->mac[2] = a3;
741 names->mac[3] = a4;
742 names->mac[4] = a5;
743 names->mac[5] = a6;
744 names->mac_valid = true;
745 return 0;
746 }
747
748 static int names_netdevsim(sd_device *dev, struct netnames *names) {
749 sd_device *netdevsimdev;
750 const char *sysname;
751 unsigned addr;
752 const char *port_name = NULL;
753 int r;
754 bool ok;
755
756 if (!naming_scheme_has(NAMING_NETDEVSIM))
757 return 0;
758
759 assert(dev);
760 assert(names);
761
762 r = sd_device_get_parent_with_subsystem_devtype(dev, "netdevsim", NULL, &netdevsimdev);
763 if (r < 0)
764 return r;
765 r = sd_device_get_sysname(netdevsimdev, &sysname);
766 if (r < 0)
767 return r;
768
769 if (sscanf(sysname, "netdevsim%u", &addr) != 1)
770 return -EINVAL;
771
772 r = sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
773 if (r < 0)
774 return r;
775
776 ok = snprintf_ok(names->netdevsim_path, sizeof(names->netdevsim_path), "i%un%s", addr, port_name);
777 if (!ok)
778 return -ENOBUFS;
779
780 names->type = NET_NETDEVSIM;
781
782 return 0;
783 }
784
785 /* IEEE Organizationally Unique Identifier vendor string */
786 static int ieee_oui(sd_device *dev, struct netnames *names, bool test) {
787 char str[32];
788
789 if (!names->mac_valid)
790 return -ENOENT;
791 /* skip commonly misused 00:00:00 (Xerox) prefix */
792 if (memcmp(names->mac, "\0\0\0", 3) == 0)
793 return -EINVAL;
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],
796 names->mac[5]);
797 udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
798 return 0;
799 }
800
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 = {};
804 unsigned long i;
805 int r;
806
807 /* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
808 r = sd_device_get_sysattr_value(dev, "type", &s);
809 if (r < 0)
810 return r;
811
812 i = strtoul(s, NULL, 0);
813 switch (i) {
814 case ARPHRD_ETHER:
815 prefix = "en";
816 break;
817 case ARPHRD_INFINIBAND:
818 if (naming_scheme_has(NAMING_INFINIBAND))
819 prefix = "ib";
820 else
821 return 0;
822 break;
823 case ARPHRD_SLIP:
824 prefix = "sl";
825 break;
826 default:
827 return 0;
828 }
829
830 /* skip stacked devices, like VLANs, ... */
831 r = sd_device_get_sysattr_value(dev, "ifindex", &s);
832 if (r < 0)
833 return r;
834 r = sd_device_get_sysattr_value(dev, "iflink", &p);
835 if (r < 0)
836 return r;
837 if (!streq(s, p))
838 return 0;
839
840 if (sd_device_get_devtype(dev, &devtype) >= 0) {
841 if (streq("wlan", devtype))
842 prefix = "wl";
843 else if (streq("wwan", devtype))
844 prefix = "ww";
845 }
846
847 udev_builtin_add_property(dev, test, "ID_NET_NAMING_SCHEME", naming_scheme()->name);
848
849 r = names_mac(dev, &names);
850 if (r >= 0 && names.mac_valid) {
851 char str[ALTIFNAMSIZ];
852
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);
857
858 ieee_oui(dev, &names, test);
859 }
860
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];
864
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);
867 return 0;
868 }
869
870 /* get ibmveth/ibmvnic slot-based names. */
871 if (names_vio(dev, &names) >= 0 && names.type == NET_VIO) {
872 char str[ALTIFNAMSIZ];
873
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);
876 return 0;
877 }
878
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];
882
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);
885 return 0;
886 }
887
888 /* get netdevsim path names */
889 if (names_netdevsim(dev, &names) >= 0 && names.type == NET_NETDEVSIM) {
890 char str[ALTIFNAMSIZ];
891
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);
894
895 return 0;
896 }
897
898 /* get PCI based path names, we compose only PCI based paths */
899 if (names_pci(dev, &names) < 0)
900 return 0;
901
902 /* plain PCI device */
903 if (names.type == NET_PCI) {
904 char str[ALTIFNAMSIZ];
905
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);
909
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);
915
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);
919
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);
923 return 0;
924 }
925
926 /* USB device */
927 if (names_usb(dev, &names) >= 0 && names.type == NET_USB) {
928 char str[ALTIFNAMSIZ];
929
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);
933
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);
937 return 0;
938 }
939
940 /* Broadcom bus */
941 if (names_bcma(dev, &names) >= 0 && names.type == NET_BCMA) {
942 char str[ALTIFNAMSIZ];
943
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);
947
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);
951 return 0;
952 }
953
954 return 0;
955 }
956
957 const UdevBuiltin udev_builtin_net_id = {
958 .name = "net_id",
959 .cmd = builtin_net_id,
960 .help = "Network device properties",
961 };