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