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