]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-net_id.c
Merge pull request #16504 from poettering/read-file-ipc
[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/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 his 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 int dev_pci_slot(sd_device *dev, struct netnames *names) {
247 unsigned long dev_port = 0;
248 unsigned domain, bus, slot, func, hotplug_slot = 0;
249 size_t l;
250 char *s;
251 const char *sysname, *attr, *port_name = NULL, *syspath;
252 _cleanup_(sd_device_unrefp) sd_device *pci = NULL;
253 sd_device *hotplug_slot_dev;
254 char slots[PATH_MAX];
255 _cleanup_closedir_ DIR *dir = NULL;
256 struct dirent *dent;
257 int r;
258
259 r = sd_device_get_sysname(names->pcidev, &sysname);
260 if (r < 0)
261 return r;
262
263 if (sscanf(sysname, "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
264 return -ENOENT;
265
266 if (naming_scheme_has(NAMING_NPAR_ARI) &&
267 is_pci_ari_enabled(names->pcidev))
268 /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
269 * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
270 * where the slot makes up the upper 5 bits. */
271 func += slot * 8;
272
273 /* kernel provided port index for multiple ports on a single PCI function */
274 if (sd_device_get_sysattr_value(dev, "dev_port", &attr) >= 0) {
275 dev_port = strtoul(attr, NULL, 10);
276 /* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously
277 * provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
278 * which thus stays initialized as 0. */
279 if (dev_port == 0 &&
280 sd_device_get_sysattr_value(dev, "type", &attr) >= 0) {
281 unsigned long type;
282
283 type = strtoul(attr, NULL, 10);
284 if (type == ARPHRD_INFINIBAND &&
285 sd_device_get_sysattr_value(dev, "dev_id", &attr) >= 0)
286 dev_port = strtoul(attr, NULL, 16);
287 }
288 }
289
290 /* kernel provided front panel port name for multi-port PCI device */
291 (void) sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
292
293 /* compose a name based on the raw kernel's PCI bus, slot numbers */
294 s = names->pci_path;
295 l = sizeof(names->pci_path);
296 if (domain > 0)
297 l = strpcpyf(&s, l, "P%u", domain);
298 l = strpcpyf(&s, l, "p%us%u", bus, slot);
299 if (func > 0 || is_pci_multifunction(names->pcidev))
300 l = strpcpyf(&s, l, "f%u", func);
301 if (port_name)
302 l = strpcpyf(&s, l, "n%s", port_name);
303 else if (dev_port > 0)
304 l = strpcpyf(&s, l, "d%lu", dev_port);
305 if (l == 0)
306 names->pci_path[0] = '\0';
307
308 /* ACPI _SUN — slot user number */
309 r = sd_device_new_from_subsystem_sysname(&pci, "subsystem", "pci");
310 if (r < 0)
311 return r;
312
313 r = sd_device_get_syspath(pci, &syspath);
314 if (r < 0)
315 return r;
316 if (!snprintf_ok(slots, sizeof slots, "%s/slots", syspath))
317 return -ENAMETOOLONG;
318
319 dir = opendir(slots);
320 if (!dir)
321 return -errno;
322
323 hotplug_slot_dev = names->pcidev;
324 while (hotplug_slot_dev) {
325 if (sd_device_get_sysname(hotplug_slot_dev, &sysname) < 0)
326 continue;
327
328 FOREACH_DIRENT_ALL(dent, dir, break) {
329 unsigned i;
330 char str[PATH_MAX];
331 _cleanup_free_ char *address = NULL;
332
333 if (dot_or_dot_dot(dent->d_name))
334 continue;
335
336 r = safe_atou_full(dent->d_name, 10, &i);
337 if (r < 0 || i <= 0)
338 continue;
339
340 /* match slot address with device by stripping the function */
341 if (snprintf_ok(str, sizeof str, "%s/%s/address", slots, dent->d_name) &&
342 read_one_line_file(str, &address) >= 0 &&
343 startswith(sysname, address)) {
344 hotplug_slot = i;
345 break;
346 }
347 }
348 if (hotplug_slot > 0)
349 break;
350 if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev, "pci", NULL, &hotplug_slot_dev) < 0)
351 break;
352 rewinddir(dir);
353 }
354
355 if (hotplug_slot > 0) {
356 s = names->pci_slot;
357 l = sizeof(names->pci_slot);
358 if (domain > 0)
359 l = strpcpyf(&s, l, "P%d", domain);
360 l = strpcpyf(&s, l, "s%d", hotplug_slot);
361 if (func > 0 || is_pci_multifunction(names->pcidev))
362 l = strpcpyf(&s, l, "f%d", func);
363 if (port_name)
364 l = strpcpyf(&s, l, "n%s", port_name);
365 else if (dev_port > 0)
366 l = strpcpyf(&s, l, "d%lu", dev_port);
367 if (l == 0)
368 names->pci_slot[0] = '\0';
369 }
370
371 return 0;
372 }
373
374 static int names_vio(sd_device *dev, struct netnames *names) {
375 sd_device *parent;
376 unsigned busid, slotid, ethid;
377 const char *syspath, *subsystem;
378 int r;
379
380 /* check if our direct parent is a VIO device with no other bus in-between */
381 r = sd_device_get_parent(dev, &parent);
382 if (r < 0)
383 return r;
384
385 r = sd_device_get_subsystem(parent, &subsystem);
386 if (r < 0)
387 return r;
388 if (!streq("vio", subsystem))
389 return -ENOENT;
390
391 /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
392 * selected in the HMC), thus this provides a reliable naming (e.g.
393 * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
394 * there should only ever be one bus, and then remove leading zeros. */
395 r = sd_device_get_syspath(dev, &syspath);
396 if (r < 0)
397 return r;
398
399 if (sscanf(syspath, "/sys/devices/vio/%4x%4x/net/eth%u", &busid, &slotid, &ethid) != 3)
400 return -EINVAL;
401
402 xsprintf(names->vio_slot, "v%u", slotid);
403 names->type = NET_VIO;
404 return 0;
405 }
406
407 #define _PLATFORM_TEST "/sys/devices/platform/vvvvPPPP"
408 #define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
409 #define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
410
411 static int names_platform(sd_device *dev, struct netnames *names, bool test) {
412 sd_device *parent;
413 char vendor[5];
414 unsigned model, instance, ethid;
415 const char *syspath, *pattern, *validchars, *subsystem;
416 int r;
417
418 /* check if our direct parent is a platform device with no other bus in-between */
419 r = sd_device_get_parent(dev, &parent);
420 if (r < 0)
421 return r;
422
423 r = sd_device_get_subsystem(parent, &subsystem);
424 if (r < 0)
425 return r;
426
427 if (!streq("platform", subsystem))
428 return -ENOENT;
429
430 r = sd_device_get_syspath(dev, &syspath);
431 if (r < 0)
432 return r;
433
434 /* syspath is too short, to have a valid ACPI instance */
435 if (strlen(syspath) < sizeof _PLATFORM_TEST)
436 return -EINVAL;
437
438 /* Vendor ID can be either PNP ID (3 chars A-Z) or ACPI ID (4 chars A-Z and numerals) */
439 if (syspath[sizeof _PLATFORM_TEST - 1] == ':') {
440 pattern = _PLATFORM_PATTERN4;
441 validchars = UPPERCASE_LETTERS DIGITS;
442 } else {
443 pattern = _PLATFORM_PATTERN3;
444 validchars = UPPERCASE_LETTERS;
445 }
446
447 /* Platform devices are named after ACPI table match, and instance id
448 * eg. "/sys/devices/platform/HISI00C2:00");
449 * The Vendor (3 or 4 char), followed by hexdecimal model number : instance id.
450 */
451
452 DISABLE_WARNING_FORMAT_NONLITERAL;
453 if (sscanf(syspath, pattern, vendor, &model, &instance, &ethid) != 4)
454 return -EINVAL;
455 REENABLE_WARNING;
456
457 if (!in_charset(vendor, validchars))
458 return -ENOENT;
459
460 ascii_strlower(vendor);
461
462 xsprintf(names->platform_path, "a%s%xi%u", vendor, model, instance);
463 names->type = NET_PLATFORM;
464 return 0;
465 }
466
467 static int names_pci(sd_device *dev, struct netnames *names) {
468 sd_device *parent;
469 struct netnames vf_names = {};
470 struct virtfn_info vf_info = {};
471 const char *subsystem;
472 int r;
473
474 assert(dev);
475 assert(names);
476
477 r = sd_device_get_parent(dev, &parent);
478 if (r < 0)
479 return r;
480 /* skip virtio subsystem if present */
481 parent = skip_virtio(parent);
482
483 if (!parent)
484 return -ENOENT;
485
486 /* check if our direct parent is a PCI device with no other bus in-between */
487 if (sd_device_get_subsystem(parent, &subsystem) >= 0 &&
488 streq("pci", subsystem)) {
489 names->type = NET_PCI;
490 names->pcidev = parent;
491 } else {
492 r = sd_device_get_parent_with_subsystem_devtype(dev, "pci", NULL, &names->pcidev);
493 if (r < 0)
494 return r;
495 }
496
497 if (naming_scheme_has(NAMING_SR_IOV_V) &&
498 get_virtfn_info(dev, names, &vf_info) >= 0) {
499 /* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
500 vf_names.pcidev = vf_info.physfn_pcidev;
501 dev_pci_onboard(dev, &vf_names);
502 dev_pci_slot(dev, &vf_names);
503 if (vf_names.pci_onboard[0])
504 if (strlen(vf_names.pci_onboard) + strlen(vf_info.suffix) < sizeof(names->pci_onboard))
505 strscpyl(names->pci_onboard, sizeof(names->pci_onboard),
506 vf_names.pci_onboard, vf_info.suffix, NULL);
507 if (vf_names.pci_slot[0])
508 if (strlen(vf_names.pci_slot) + strlen(vf_info.suffix) < sizeof(names->pci_slot))
509 strscpyl(names->pci_slot, sizeof(names->pci_slot),
510 vf_names.pci_slot, vf_info.suffix, NULL);
511 if (vf_names.pci_path[0])
512 if (strlen(vf_names.pci_path) + strlen(vf_info.suffix) < sizeof(names->pci_path))
513 strscpyl(names->pci_path, sizeof(names->pci_path),
514 vf_names.pci_path, vf_info.suffix, NULL);
515 sd_device_unref(vf_info.physfn_pcidev);
516 } else {
517 dev_pci_onboard(dev, names);
518 dev_pci_slot(dev, names);
519 }
520
521 return 0;
522 }
523
524 static int names_usb(sd_device *dev, struct netnames *names) {
525 sd_device *usbdev;
526 char name[256], *ports, *config, *interf, *s;
527 const char *sysname;
528 size_t l;
529 int r;
530
531 assert(dev);
532 assert(names);
533
534 r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &usbdev);
535 if (r < 0)
536 return r;
537
538 r = sd_device_get_sysname(usbdev, &sysname);
539 if (r < 0)
540 return r;
541
542 /* get USB port number chain, configuration, interface */
543 strscpy(name, sizeof(name), sysname);
544 s = strchr(name, '-');
545 if (!s)
546 return -EINVAL;
547 ports = s+1;
548
549 s = strchr(ports, ':');
550 if (!s)
551 return -EINVAL;
552 s[0] = '\0';
553 config = s+1;
554
555 s = strchr(config, '.');
556 if (!s)
557 return -EINVAL;
558 s[0] = '\0';
559 interf = s+1;
560
561 /* prefix every port number in the chain with "u" */
562 s = ports;
563 while ((s = strchr(s, '.')))
564 s[0] = 'u';
565 s = names->usb_ports;
566 l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
567
568 /* append USB config number, suppress the common config == 1 */
569 if (!streq(config, "1"))
570 l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
571
572 /* append USB interface number, suppress the interface == 0 */
573 if (!streq(interf, "0"))
574 l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
575 if (l == 0)
576 return -ENAMETOOLONG;
577
578 names->type = NET_USB;
579 return 0;
580 }
581
582 static int names_bcma(sd_device *dev, struct netnames *names) {
583 sd_device *bcmadev;
584 unsigned core;
585 const char *sysname;
586 int r;
587
588 assert(dev);
589 assert(names);
590
591 r = sd_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL, &bcmadev);
592 if (r < 0)
593 return r;
594
595 r = sd_device_get_sysname(bcmadev, &sysname);
596 if (r < 0)
597 return r;
598
599 /* bus num:core num */
600 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
601 return -EINVAL;
602 /* suppress the common core == 0 */
603 if (core > 0)
604 xsprintf(names->bcma_core, "b%u", core);
605
606 names->type = NET_BCMA;
607 return 0;
608 }
609
610 static int names_ccw(sd_device *dev, struct netnames *names) {
611 sd_device *cdev;
612 const char *bus_id, *subsys;
613 size_t bus_id_len;
614 size_t bus_id_start;
615 int r;
616
617 assert(dev);
618 assert(names);
619
620 /* Retrieve the associated CCW device */
621 r = sd_device_get_parent(dev, &cdev);
622 if (r < 0)
623 return r;
624
625 /* skip virtio subsystem if present */
626 cdev = skip_virtio(cdev);
627 if (!cdev)
628 return -ENOENT;
629
630 r = sd_device_get_subsystem(cdev, &subsys);
631 if (r < 0)
632 return r;
633
634 /* Network devices are either single or grouped CCW devices */
635 if (!STR_IN_SET(subsys, "ccwgroup", "ccw"))
636 return -ENOENT;
637
638 /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
639 * identifies the network device on the Linux on System z channel
640 * subsystem. Note that the bus-ID contains lowercase characters.
641 */
642 r = sd_device_get_sysname(cdev, &bus_id);
643 if (r < 0)
644 return r;
645
646 /* Check the length of the bus-ID. Rely on the fact that the kernel provides a correct bus-ID;
647 * alternatively, improve this check and parse and verify each bus-ID part...
648 */
649 bus_id_len = strlen(bus_id);
650 if (!IN_SET(bus_id_len, 8, 9))
651 return -EINVAL;
652
653 /* Strip leading zeros from the bus id for aesthetic purposes. This
654 * keeps the ccw names stable, yet much shorter in general case of
655 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
656 * not prepended when it is zero. Preserve the last 0 for 0.0.0000.
657 */
658 bus_id_start = strspn(bus_id, ".0");
659 bus_id += bus_id_start < bus_id_len ? bus_id_start : bus_id_len - 1;
660
661 /* Store the CCW bus-ID for use as network device name */
662 if (snprintf_ok(names->ccw_busid, sizeof(names->ccw_busid), "c%s", bus_id))
663 names->type = NET_CCW;
664
665 return 0;
666 }
667
668 static int names_mac(sd_device *dev, struct netnames *names) {
669 const char *s;
670 unsigned long i;
671 unsigned a1, a2, a3, a4, a5, a6;
672 int r;
673
674 /* Some kinds of devices tend to have hardware addresses
675 * that are impossible to use in an iface name.
676 */
677 r = sd_device_get_sysattr_value(dev, "type", &s);
678 if (r < 0)
679 return r;
680
681 i = strtoul(s, NULL, 0);
682 switch (i) {
683 /* The persistent part of a hardware address of an InfiniBand NIC
684 * is 8 bytes long. We cannot fit this much in an iface name.
685 */
686 case ARPHRD_INFINIBAND:
687 return -EINVAL;
688 default:
689 break;
690 }
691
692 /* check for NET_ADDR_PERM, skip random MAC addresses */
693 r = sd_device_get_sysattr_value(dev, "addr_assign_type", &s);
694 if (r < 0)
695 return r;
696 i = strtoul(s, NULL, 0);
697 if (i != 0)
698 return 0;
699
700 r = sd_device_get_sysattr_value(dev, "address", &s);
701 if (r < 0)
702 return r;
703 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
704 return -EINVAL;
705
706 /* skip empty MAC addresses */
707 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
708 return -EINVAL;
709
710 names->mac[0] = a1;
711 names->mac[1] = a2;
712 names->mac[2] = a3;
713 names->mac[3] = a4;
714 names->mac[4] = a5;
715 names->mac[5] = a6;
716 names->mac_valid = true;
717 return 0;
718 }
719
720 static int names_netdevsim(sd_device *dev, struct netnames *names) {
721 sd_device *netdevsimdev;
722 const char *sysname;
723 unsigned addr;
724 const char *port_name = NULL;
725 int r;
726 bool ok;
727
728 if (!naming_scheme_has(NAMING_NETDEVSIM))
729 return 0;
730
731 assert(dev);
732 assert(names);
733
734 r = sd_device_get_parent_with_subsystem_devtype(dev, "netdevsim", NULL, &netdevsimdev);
735 if (r < 0)
736 return r;
737 r = sd_device_get_sysname(netdevsimdev, &sysname);
738 if (r < 0)
739 return r;
740
741 if (sscanf(sysname, "netdevsim%u", &addr) != 1)
742 return -EINVAL;
743
744 r = sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
745 if (r < 0)
746 return r;
747
748 ok = snprintf_ok(names->netdevsim_path, sizeof(names->netdevsim_path), "i%un%s", addr, port_name);
749 if (!ok)
750 return -ENOBUFS;
751
752 names->type = NET_NETDEVSIM;
753
754 return 0;
755 }
756
757 /* IEEE Organizationally Unique Identifier vendor string */
758 static int ieee_oui(sd_device *dev, struct netnames *names, bool test) {
759 char str[32];
760
761 if (!names->mac_valid)
762 return -ENOENT;
763 /* skip commonly misused 00:00:00 (Xerox) prefix */
764 if (memcmp(names->mac, "\0\0\0", 3) == 0)
765 return -EINVAL;
766 xsprintf(str, "OUI:%02X%02X%02X%02X%02X%02X", names->mac[0],
767 names->mac[1], names->mac[2], names->mac[3], names->mac[4],
768 names->mac[5]);
769 udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
770 return 0;
771 }
772
773 static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
774 const char *s, *p, *devtype, *prefix = "en";
775 struct netnames names = {};
776 unsigned long i;
777 int r;
778
779 /* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
780 r = sd_device_get_sysattr_value(dev, "type", &s);
781 if (r < 0)
782 return r;
783
784 i = strtoul(s, NULL, 0);
785 switch (i) {
786 case ARPHRD_ETHER:
787 prefix = "en";
788 break;
789 case ARPHRD_INFINIBAND:
790 if (naming_scheme_has(NAMING_INFINIBAND))
791 prefix = "ib";
792 else
793 return 0;
794 break;
795 case ARPHRD_SLIP:
796 prefix = "sl";
797 break;
798 default:
799 return 0;
800 }
801
802 /* skip stacked devices, like VLANs, ... */
803 r = sd_device_get_sysattr_value(dev, "ifindex", &s);
804 if (r < 0)
805 return r;
806 r = sd_device_get_sysattr_value(dev, "iflink", &p);
807 if (r < 0)
808 return r;
809 if (!streq(s, p))
810 return 0;
811
812 if (sd_device_get_devtype(dev, &devtype) >= 0) {
813 if (streq("wlan", devtype))
814 prefix = "wl";
815 else if (streq("wwan", devtype))
816 prefix = "ww";
817 }
818
819 udev_builtin_add_property(dev, test, "ID_NET_NAMING_SCHEME", naming_scheme()->name);
820
821 r = names_mac(dev, &names);
822 if (r >= 0 && names.mac_valid) {
823 char str[ALTIFNAMSIZ];
824
825 xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix,
826 names.mac[0], names.mac[1], names.mac[2],
827 names.mac[3], names.mac[4], names.mac[5]);
828 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
829
830 ieee_oui(dev, &names, test);
831 }
832
833 /* get path names for Linux on System z network devices */
834 if (names_ccw(dev, &names) >= 0 && names.type == NET_CCW) {
835 char str[ALTIFNAMSIZ];
836
837 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.ccw_busid))
838 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
839 return 0;
840 }
841
842 /* get ibmveth/ibmvnic slot-based names. */
843 if (names_vio(dev, &names) >= 0 && names.type == NET_VIO) {
844 char str[ALTIFNAMSIZ];
845
846 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.vio_slot))
847 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
848 return 0;
849 }
850
851 /* get ACPI path names for ARM64 platform devices */
852 if (names_platform(dev, &names, test) >= 0 && names.type == NET_PLATFORM) {
853 char str[ALTIFNAMSIZ];
854
855 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.platform_path))
856 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
857 return 0;
858 }
859
860 /* get netdevsim path names */
861 if (names_netdevsim(dev, &names) >= 0 && names.type == NET_NETDEVSIM) {
862 char str[ALTIFNAMSIZ];
863
864 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.netdevsim_path))
865 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
866
867 return 0;
868 }
869
870 /* get PCI based path names, we compose only PCI based paths */
871 if (names_pci(dev, &names) < 0)
872 return 0;
873
874 /* plain PCI device */
875 if (names.type == NET_PCI) {
876 char str[ALTIFNAMSIZ];
877
878 if (names.pci_onboard[0] &&
879 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_onboard))
880 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
881
882 if (names.pci_onboard_label &&
883 snprintf_ok(str, sizeof str, "%s%s",
884 naming_scheme_has(NAMING_LABEL_NOPREFIX) ? "" : prefix,
885 names.pci_onboard_label))
886 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
887
888 if (names.pci_path[0] &&
889 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_path))
890 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
891
892 if (names.pci_slot[0] &&
893 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_slot))
894 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
895 return 0;
896 }
897
898 /* USB device */
899 if (names_usb(dev, &names) >= 0 && names.type == NET_USB) {
900 char str[ALTIFNAMSIZ];
901
902 if (names.pci_path[0] &&
903 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.usb_ports))
904 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
905
906 if (names.pci_slot[0] &&
907 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.usb_ports))
908 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
909 return 0;
910 }
911
912 /* Broadcom bus */
913 if (names_bcma(dev, &names) >= 0 && names.type == NET_BCMA) {
914 char str[ALTIFNAMSIZ];
915
916 if (names.pci_path[0] &&
917 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.bcma_core))
918 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
919
920 if (names.pci_slot[0] &&
921 snprintf(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.bcma_core))
922 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
923 return 0;
924 }
925
926 return 0;
927 }
928
929 const UdevBuiltin udev_builtin_net_id = {
930 .name = "net_id",
931 .cmd = builtin_net_id,
932 .help = "Network device properties",
933 };