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