]> git.proxmox.com Git - systemd.git/blame - src/udev/udev-builtin-net_id.c
New upstream version 240
[systemd.git] / src / udev / udev-builtin-net_id.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
663996b3
MS
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:
aa27b158 13 * en — Ethernet
6e866b33 14 * ib — InfiniBand
aa27b158
MP
15 * sl — serial line IP (slip)
16 * wl — wlan
17 * ww — wwan
663996b3
MS
18 *
19 * Type of names:
aa27b158 20 * b<number> — BCMA bus core number
2897b343
MP
21 * c<bus_id> — bus id of a grouped CCW or CCW device,
22 * with all leading zeros stripped [s390]
8a584da2
MP
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
aa27b158 27 * x<MAC> — MAC address
8a584da2 28 * [P<domain>]p<bus>s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
aa27b158 29 * — PCI geographical location
14228c0d 30 * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
aa27b158 31 * — USB port number chain
81c58355
MB
32 * v<slot> - VIO slot number (IBM PowerVM)
33 * a<vendor><model>i<instance> — Platform bus ACPI instance id
663996b3
MS
34 *
35 * All multi-function PCI devices will carry the [f<function>] number in the
36 * device name, including the function 0 device.
37 *
6e866b33
MB
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 *
14228c0d
MB
41 * When using PCI geography, The PCI domain is only prepended when it is not 0.
42 *
663996b3
MS
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 *
db2df898 48 * PCI Ethernet card with firmware index "1":
663996b3
MS
49 * ID_NET_NAME_ONBOARD=eno1
50 * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
51 *
db2df898 52 * PCI Ethernet card in hotplug slot with firmware index number:
663996b3
MS
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 *
db2df898 58 * PCI Ethernet multi-function card with 2 ports:
663996b3
MS
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 *
6e866b33
MB
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 *
663996b3
MS
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
2897b343
MP
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
663996b3
MS
91 */
92
663996b3 93#include <errno.h>
db2df898 94#include <fcntl.h>
663996b3 95#include <net/if.h>
60f067b4 96#include <net/if_arp.h>
db2df898
MP
97#include <stdarg.h>
98#include <stdio.h>
99#include <stdlib.h>
100#include <string.h>
101#include <unistd.h>
663996b3
MS
102#include <linux/pci_regs.h>
103
6e866b33 104#include "alloc-util.h"
2897b343 105#include "dirent-util.h"
db2df898 106#include "fd-util.h"
663996b3 107#include "fileio.h"
b012e921
MB
108#include "fs-util.h"
109#include "parse-util.h"
6e866b33 110#include "proc-cmdline.h"
4c89c718 111#include "stdio-util.h"
db2df898 112#include "string-util.h"
6e866b33
MB
113#include "strv.h"
114#include "strxcpyx.h"
115#include "udev-builtin.h"
663996b3 116
4c89c718
MP
117#define ONBOARD_INDEX_MAX (16*1024-1)
118
6e866b33
MB
119/* So here's the deal: net_id is supposed to be an excercise in providing stable names for network devices. However, we
120 * also want to keep updating the naming scheme used in future versions of net_id. These two goals of course are
121 * contradictory: on one hand we want things to not change and on the other hand we want them to improve. Our way out
122 * of this dilemma is to introduce the "naming scheme" concept: each time we improve the naming logic we define a new
123 * flag for it. Then, we keep a list of schemes, each identified by a name associated with the flags it implements. Via
124 * a kernel command line and environment variable we then allow the user to pick the scheme they want us to follow:
125 * installers could "freeze" the used scheme at the moment of installation this way.
126 *
127 * Developers: each time you tweak the naming logic here, define a new flag below, and condition the tweak with
128 * it. Each time we do a release we'll then add a new scheme entry and include all newly defined flags.
129 *
130 * Note that this is only half a solution to the problem though: not only udev/net_id gets updated all the time, the
131 * kernel gets too. And thus a kernel that previously didn't expose some sysfs attribute we look for might eventually
132 * do, and thus affect our naming scheme too. Thus, enforcing a naming scheme will make interfacing more stable across
133 * OS versions, but not fully stabilize them. */
134typedef enum NamingSchemeFlags {
135 /* First, the individual features */
136 NAMING_SR_IOV_V = 1 << 0, /* Use "v" suffix for SR-IOV, see 609948c7043a40008b8299529c978ed8e11de8f6*/
137 NAMING_NPAR_ARI = 1 << 1, /* Use NPAR "ARI", see 6bc04997b6eab35d1cb9fa73889892702c27be09 */
138 NAMING_INFINIBAND = 1 << 2, /* Use "ib" prefix for infiniband, see 938d30aa98df887797c9e05074a562ddacdcdf5e */
139 NAMING_ZERO_ACPI_INDEX = 1 << 3, /* Allow zero acpi_index field, see d81186ef4f6a888a70f20a1e73a812d6acb9e22f */
140
141 /* And now the masks that combine the features above */
142 NAMING_V238 = 0,
143 NAMING_V239 = NAMING_V238|NAMING_SR_IOV_V|NAMING_NPAR_ARI,
144 NAMING_V240 = NAMING_V239|NAMING_INFINIBAND|NAMING_ZERO_ACPI_INDEX,
145
146 _NAMING_SCHEME_FLAGS_INVALID = -1,
147} NamingSchemeFlags;
148
149typedef struct NamingScheme {
150 const char *name;
151 NamingSchemeFlags flags;
152} NamingScheme;
153
154static const NamingScheme naming_schemes[] = {
155 { "v238", NAMING_V238 },
156 { "v239", NAMING_V239 },
157 { "v240", NAMING_V240 },
158 /* … add more schemes here, as the logic to name devices is updated … */
159};
160
663996b3
MS
161enum netname_type{
162 NET_UNDEF,
163 NET_PCI,
164 NET_USB,
165 NET_BCMA,
60f067b4 166 NET_VIRTIO,
2897b343 167 NET_CCW,
81c58355
MB
168 NET_VIO,
169 NET_PLATFORM,
663996b3
MS
170};
171
172struct netnames {
173 enum netname_type type;
174
175 uint8_t mac[6];
176 bool mac_valid;
177
6e866b33 178 sd_device *pcidev;
663996b3
MS
179 char pci_slot[IFNAMSIZ];
180 char pci_path[IFNAMSIZ];
181 char pci_onboard[IFNAMSIZ];
182 const char *pci_onboard_label;
183
184 char usb_ports[IFNAMSIZ];
663996b3 185 char bcma_core[IFNAMSIZ];
2897b343 186 char ccw_busid[IFNAMSIZ];
81c58355
MB
187 char vio_slot[IFNAMSIZ];
188 char platform_path[IFNAMSIZ];
663996b3
MS
189};
190
b012e921 191struct virtfn_info {
6e866b33 192 sd_device *physfn_pcidev;
b012e921
MB
193 char suffix[IFNAMSIZ];
194};
195
6e866b33
MB
196static const NamingScheme* naming_scheme_from_name(const char *name) {
197 size_t i;
198
199 if (streq(name, "latest"))
200 return naming_schemes + ELEMENTSOF(naming_schemes) - 1;
201
202 for (i = 0; i < ELEMENTSOF(naming_schemes); i++)
203 if (streq(naming_schemes[i].name, name))
204 return naming_schemes + i;
205
206 return NULL;
207}
208
209static const NamingScheme* naming_scheme(void) {
210 static const NamingScheme *cache = NULL;
211 _cleanup_free_ char *buffer = NULL;
212 const char *e, *k;
213
214 if (cache)
215 return cache;
216
217 /* Acquire setting from the kernel command line */
218 (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer);
219
220 /* Also acquire it from an env var */
221 e = getenv("NET_NAMING_SCHEME");
222 if (e) {
223 if (*e == ':') {
224 /* If prefixed with ':' the kernel cmdline takes precedence */
225 k = buffer ?: e + 1;
226 } else
227 k = e; /* Otherwise the env var takes precedence */
228 } else
229 k = buffer;
230
231 if (k) {
232 cache = naming_scheme_from_name(k);
233 if (cache) {
234 log_info("Using interface naming scheme '%s'.", cache->name);
235 return cache;
236 }
237
238 log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
239 }
240
241 cache = naming_scheme_from_name(DEFAULT_NET_NAMING_SCHEME);
242 assert(cache);
243 log_info("Using default interface naming scheme '%s'.", cache->name);
244
245 return cache;
246}
247
248static bool naming_scheme_has(NamingSchemeFlags flags) {
249 return FLAGS_SET(naming_scheme()->flags, flags);
250}
251
2897b343 252/* skip intermediate virtio devices */
6e866b33
MB
253static sd_device *skip_virtio(sd_device *dev) {
254 sd_device *parent;
2897b343
MP
255
256 /* there can only ever be one virtio bus per parent device, so we can
6e866b33
MB
257 * safely ignore any virtio buses. see
258 * http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html */
259 for (parent = dev; parent; ) {
260 const char *subsystem;
261
262 if (sd_device_get_subsystem(parent, &subsystem) < 0)
263 break;
264
265 if (!streq(subsystem, "virtio"))
266 break;
267
268 if (sd_device_get_parent(parent, &parent) < 0)
269 return NULL;
270 }
271
2897b343
MP
272 return parent;
273}
274
6e866b33
MB
275static int get_virtfn_info(sd_device *dev, struct netnames *names, struct virtfn_info *ret) {
276 _cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL;
277 const char *physfn_link_file, *syspath;
b012e921
MB
278 _cleanup_free_ char *physfn_pci_syspath = NULL;
279 _cleanup_free_ char *virtfn_pci_syspath = NULL;
280 struct dirent *dent;
281 _cleanup_closedir_ DIR *dir = NULL;
6e866b33 282 char suffix[IFNAMSIZ];
b012e921
MB
283 int r;
284
6e866b33
MB
285 assert(dev);
286 assert(names);
287 assert(ret);
288
289 r = sd_device_get_syspath(names->pcidev, &syspath);
290 if (r < 0)
291 return r;
292
b012e921 293 /* Check if this is a virtual function. */
6e866b33 294 physfn_link_file = strjoina(syspath, "/physfn");
b012e921
MB
295 r = chase_symlinks(physfn_link_file, NULL, 0, &physfn_pci_syspath);
296 if (r < 0)
297 return r;
298
299 /* Get physical function's pci device. */
6e866b33
MB
300 r = sd_device_new_from_syspath(&physfn_pcidev, physfn_pci_syspath);
301 if (r < 0)
302 return r;
b012e921
MB
303
304 /* Find the virtual function number by finding the right virtfn link. */
305 dir = opendir(physfn_pci_syspath);
6e866b33
MB
306 if (!dir)
307 return -errno;
308
b012e921
MB
309 FOREACH_DIRENT_ALL(dent, dir, break) {
310 _cleanup_free_ char *virtfn_link_file = NULL;
6e866b33 311
b012e921
MB
312 if (!startswith(dent->d_name, "virtfn"))
313 continue;
6e866b33 314
b012e921 315 virtfn_link_file = strjoin(physfn_pci_syspath, "/", dent->d_name);
6e866b33
MB
316 if (!virtfn_link_file)
317 return -ENOMEM;
318
b012e921
MB
319 if (chase_symlinks(virtfn_link_file, NULL, 0, &virtfn_pci_syspath) < 0)
320 continue;
6e866b33
MB
321
322 if (streq(syspath, virtfn_pci_syspath)) {
323 if (!snprintf_ok(suffix, sizeof(suffix), "v%s", &dent->d_name[6]))
324 return -ENOENT;
325
b012e921
MB
326 break;
327 }
328 }
6e866b33
MB
329 if (isempty(suffix))
330 return -ENOENT;
331
332 ret->physfn_pcidev = TAKE_PTR(physfn_pcidev);
333 strncpy(ret->suffix, suffix, sizeof(ret->suffix));
b012e921 334
6e866b33 335 return 0;
b012e921
MB
336}
337
663996b3 338/* retrieve on-board index number and label from firmware */
6e866b33
MB
339static int dev_pci_onboard(sd_device *dev, struct netnames *names) {
340 unsigned long idx, dev_port = 0;
341 const char *attr, *port_name = NULL;
e3bff60a
MP
342 size_t l;
343 char *s;
6e866b33 344 int r;
663996b3 345
aa27b158 346 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
6e866b33
MB
347 if (sd_device_get_sysattr_value(names->pcidev, "acpi_index", &attr) < 0) {
348 /* SMBIOS type 41 — Onboard Devices Extended Information */
349 r = sd_device_get_sysattr_value(names->pcidev, "index", &attr);
350 if (r < 0)
351 return r;
352 }
e3bff60a 353
6e866b33
MB
354 r = safe_atolu(attr, &idx);
355 if (r < 0)
356 return r;
357 if (idx == 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX))
663996b3 358 return -EINVAL;
e3bff60a 359
4c89c718
MP
360 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
361 * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
362 * cut-off, which is somewhere beyond the realistic number of physical network interface a system might
363 * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
364 if (idx > ONBOARD_INDEX_MAX)
365 return -ENOENT;
366
e3bff60a 367 /* kernel provided port index for multiple ports on a single PCI function */
6e866b33
MB
368 if (sd_device_get_sysattr_value(dev, "dev_port", &attr) >= 0)
369 dev_port = strtoul(attr, NULL, 10);
e3bff60a 370
8a584da2 371 /* kernel provided front panel port name for multiple port PCI device */
6e866b33 372 (void) sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
8a584da2 373
e3bff60a
MP
374 s = names->pci_onboard;
375 l = sizeof(names->pci_onboard);
6e866b33 376 l = strpcpyf(&s, l, "o%lu", idx);
8a584da2
MP
377 if (port_name)
378 l = strpcpyf(&s, l, "n%s", port_name);
379 else if (dev_port > 0)
6e866b33 380 l = strpcpyf(&s, l, "d%lu", dev_port);
e3bff60a
MP
381 if (l == 0)
382 names->pci_onboard[0] = '\0';
663996b3 383
6e866b33
MB
384 if (sd_device_get_sysattr_value(names->pcidev, "label", &names->pci_onboard_label) < 0)
385 names->pci_onboard_label = NULL;
e3bff60a 386
663996b3
MS
387 return 0;
388}
389
390/* read the 256 bytes PCI configuration space to check the multi-function bit */
6e866b33 391static bool is_pci_multifunction(sd_device *dev) {
86f210e9 392 _cleanup_close_ int fd = -1;
6e866b33 393 const char *filename, *syspath;
5eef597e 394 uint8_t config[64];
663996b3 395
6e866b33
MB
396 if (sd_device_get_syspath(dev, &syspath) < 0)
397 return false;
398
399 filename = strjoina(syspath, "/config");
86f210e9
MP
400 fd = open(filename, O_RDONLY | O_CLOEXEC);
401 if (fd < 0)
5eef597e 402 return false;
86f210e9 403 if (read(fd, &config, sizeof(config)) != sizeof(config))
5eef597e 404 return false;
663996b3
MS
405
406 /* bit 0-6 header type, bit 7 multi/single function device */
6e866b33 407 return config[PCI_HEADER_TYPE] & 0x80;
663996b3
MS
408}
409
6e866b33
MB
410static bool is_pci_ari_enabled(sd_device *dev) {
411 const char *a;
412
413 if (sd_device_get_sysattr_value(dev, "ari_enabled", &a) < 0)
414 return false;
415
416 return streq(a, "1");
b012e921
MB
417}
418
6e866b33
MB
419static int dev_pci_slot(sd_device *dev, struct netnames *names) {
420 unsigned long dev_port = 0;
421 unsigned domain, bus, slot, func, hotplug_slot = 0;
663996b3
MS
422 size_t l;
423 char *s;
6e866b33
MB
424 const char *sysname, *attr, *port_name = NULL, *syspath;
425 _cleanup_(sd_device_unrefp) sd_device *pci = NULL;
426 sd_device *hotplug_slot_dev;
8a584da2 427 char slots[PATH_MAX];
60f067b4 428 _cleanup_closedir_ DIR *dir = NULL;
663996b3 429 struct dirent *dent;
6e866b33 430 int r;
663996b3 431
6e866b33
MB
432 r = sd_device_get_sysname(names->pcidev, &sysname);
433 if (r < 0)
434 return r;
435
436 if (sscanf(sysname, "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
663996b3 437 return -ENOENT;
6e866b33
MB
438
439 if (naming_scheme_has(NAMING_NPAR_ARI) &&
440 is_pci_ari_enabled(names->pcidev))
b012e921
MB
441 /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
442 * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
443 * where the slot makes up the upper 5 bits. */
444 func += slot * 8;
663996b3 445
e3bff60a 446 /* kernel provided port index for multiple ports on a single PCI function */
6e866b33
MB
447 if (sd_device_get_sysattr_value(dev, "dev_port", &attr) >= 0) {
448 dev_port = strtoul(attr, NULL, 10);
449 /* With older kernels IP-over-InfiniBand network interfaces sometimes erroneously
450 * provide the port number in the 'dev_id' sysfs attribute instead of 'dev_port',
451 * which thus stays initialized as 0. */
452 if (dev_port == 0 &&
453 sd_device_get_sysattr_value(dev, "type", &attr) >= 0) {
454 unsigned long type;
455
456 type = strtoul(attr, NULL, 10);
457 if (type == ARPHRD_INFINIBAND &&
458 sd_device_get_sysattr_value(dev, "dev_id", &attr) >= 0)
459 dev_port = strtoul(attr, NULL, 16);
460 }
461 }
663996b3 462
8a584da2 463 /* kernel provided front panel port name for multiple port PCI device */
6e866b33 464 (void) sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);
8a584da2 465
663996b3
MS
466 /* compose a name based on the raw kernel's PCI bus, slot numbers */
467 s = names->pci_path;
14228c0d
MB
468 l = sizeof(names->pci_path);
469 if (domain > 0)
e735f4d4
MP
470 l = strpcpyf(&s, l, "P%u", domain);
471 l = strpcpyf(&s, l, "p%us%u", bus, slot);
663996b3 472 if (func > 0 || is_pci_multifunction(names->pcidev))
e735f4d4 473 l = strpcpyf(&s, l, "f%u", func);
8a584da2
MP
474 if (port_name)
475 l = strpcpyf(&s, l, "n%s", port_name);
476 else if (dev_port > 0)
6e866b33 477 l = strpcpyf(&s, l, "d%lu", dev_port);
663996b3
MS
478 if (l == 0)
479 names->pci_path[0] = '\0';
480
aa27b158 481 /* ACPI _SUN — slot user number */
6e866b33
MB
482 r = sd_device_new_from_subsystem_sysname(&pci, "subsystem", "pci");
483 if (r < 0)
484 return r;
98393f85 485
6e866b33
MB
486 r = sd_device_get_syspath(pci, &syspath);
487 if (r < 0)
488 return r;
489 if (!snprintf_ok(slots, sizeof slots, "%s/slots", syspath))
98393f85 490 return -ENAMETOOLONG;
8a584da2 491
663996b3 492 dir = opendir(slots);
98393f85
MB
493 if (!dir)
494 return -errno;
663996b3 495
b012e921
MB
496 hotplug_slot_dev = names->pcidev;
497 while (hotplug_slot_dev) {
6e866b33
MB
498 if (sd_device_get_sysname(hotplug_slot_dev, &sysname) < 0)
499 continue;
500
b012e921
MB
501 FOREACH_DIRENT_ALL(dent, dir, break) {
502 unsigned i;
b012e921
MB
503 char str[PATH_MAX];
504 _cleanup_free_ char *address = NULL;
505
506 if (dent->d_name[0] == '.')
507 continue;
508 r = safe_atou_full(dent->d_name, 10, &i);
6e866b33 509 if (r < 0 || i <= 0)
b012e921
MB
510 continue;
511
6e866b33 512 /* match slot address with device by stripping the function */
b012e921 513 if (snprintf_ok(str, sizeof str, "%s/%s/address", slots, dent->d_name) &&
6e866b33
MB
514 read_one_line_file(str, &address) >= 0 &&
515 startswith(sysname, address)) {
516 hotplug_slot = i;
b012e921 517 break;
6e866b33 518 }
b012e921 519 }
663996b3
MS
520 if (hotplug_slot > 0)
521 break;
6e866b33
MB
522 if (sd_device_get_parent_with_subsystem_devtype(hotplug_slot_dev, "pci", NULL, &hotplug_slot_dev) < 0)
523 break;
b012e921 524 rewinddir(dir);
663996b3 525 }
663996b3
MS
526
527 if (hotplug_slot > 0) {
528 s = names->pci_slot;
14228c0d
MB
529 l = sizeof(names->pci_slot);
530 if (domain > 0)
531 l = strpcpyf(&s, l, "P%d", domain);
532 l = strpcpyf(&s, l, "s%d", hotplug_slot);
663996b3
MS
533 if (func > 0 || is_pci_multifunction(names->pcidev))
534 l = strpcpyf(&s, l, "f%d", func);
8a584da2
MP
535 if (port_name)
536 l = strpcpyf(&s, l, "n%s", port_name);
537 else if (dev_port > 0)
6e866b33 538 l = strpcpyf(&s, l, "d%lu", dev_port);
663996b3 539 if (l == 0)
e3bff60a 540 names->pci_slot[0] = '\0';
663996b3 541 }
98393f85
MB
542
543 return 0;
663996b3
MS
544}
545
6e866b33
MB
546static int names_vio(sd_device *dev, struct netnames *names) {
547 sd_device *parent;
81c58355 548 unsigned busid, slotid, ethid;
6e866b33
MB
549 const char *syspath, *subsystem;
550 int r;
81c58355
MB
551
552 /* check if our direct parent is a VIO device with no other bus in-between */
6e866b33
MB
553 r = sd_device_get_parent(dev, &parent);
554 if (r < 0)
555 return r;
81c58355 556
6e866b33
MB
557 r = sd_device_get_subsystem(parent, &subsystem);
558 if (r < 0)
559 return r;
560 if (!streq("vio", subsystem))
561 return -ENOENT;
81c58355
MB
562
563 /* The devices' $DEVPATH number is tied to (virtual) hardware (slot id
564 * selected in the HMC), thus this provides a reliable naming (e.g.
565 * "/devices/vio/30000002/net/eth1"); we ignore the bus number, as
566 * there should only ever be one bus, and then remove leading zeros. */
6e866b33
MB
567 r = sd_device_get_syspath(dev, &syspath);
568 if (r < 0)
569 return r;
81c58355
MB
570
571 if (sscanf(syspath, "/sys/devices/vio/%4x%4x/net/eth%u", &busid, &slotid, &ethid) != 3)
572 return -EINVAL;
573
574 xsprintf(names->vio_slot, "v%u", slotid);
575 names->type = NET_VIO;
576 return 0;
577}
578
579#define _PLATFORM_TEST "/sys/devices/platform/vvvvPPPP"
580#define _PLATFORM_PATTERN4 "/sys/devices/platform/%4s%4x:%2x/net/eth%u"
581#define _PLATFORM_PATTERN3 "/sys/devices/platform/%3s%4x:%2x/net/eth%u"
582
6e866b33
MB
583static int names_platform(sd_device *dev, struct netnames *names, bool test) {
584 sd_device *parent;
81c58355
MB
585 char vendor[5];
586 unsigned model, instance, ethid;
6e866b33
MB
587 const char *syspath, *pattern, *validchars, *subsystem;
588 int r;
81c58355
MB
589
590 /* check if our direct parent is a platform device with no other bus in-between */
6e866b33
MB
591 r = sd_device_get_parent(dev, &parent);
592 if (r < 0)
593 return r;
81c58355 594
6e866b33
MB
595 r = sd_device_get_subsystem(parent, &subsystem);
596 if (r < 0)
597 return r;
598
599 if (!streq("platform", subsystem))
81c58355
MB
600 return -ENOENT;
601
6e866b33
MB
602 r = sd_device_get_syspath(dev, &syspath);
603 if (r < 0)
604 return r;
81c58355
MB
605
606 /* syspath is too short, to have a valid ACPI instance */
607 if (strlen(syspath) < sizeof _PLATFORM_TEST)
608 return -EINVAL;
609
610 /* Vendor ID can be either PNP ID (3 chars A-Z) or ACPI ID (4 chars A-Z and numerals) */
611 if (syspath[sizeof _PLATFORM_TEST - 1] == ':') {
612 pattern = _PLATFORM_PATTERN4;
613 validchars = UPPERCASE_LETTERS DIGITS;
614 } else {
615 pattern = _PLATFORM_PATTERN3;
616 validchars = UPPERCASE_LETTERS;
617 }
618
619 /* Platform devices are named after ACPI table match, and instance id
620 * eg. "/sys/devices/platform/HISI00C2:00");
621 * The Vendor (3 or 4 char), followed by hexdecimal model number : instance id.
622 */
623
624#pragma GCC diagnostic push
625#pragma GCC diagnostic ignored "-Wformat-nonliteral"
626 if (sscanf(syspath, pattern, vendor, &model, &instance, &ethid) != 4)
627 return -EINVAL;
628#pragma GCC diagnostic pop
629
630 if (!in_charset(vendor, validchars))
631 return -ENOENT;
632
633 ascii_strlower(vendor);
634
635 xsprintf(names->platform_path, "a%s%xi%u", vendor, model, instance);
636 names->type = NET_PLATFORM;
637 return 0;
638}
639
6e866b33
MB
640static int names_pci(sd_device *dev, struct netnames *names) {
641 sd_device *parent;
b012e921
MB
642 struct netnames vf_names = {};
643 struct virtfn_info vf_info = {};
6e866b33
MB
644 const char *subsystem;
645 int r;
663996b3 646
86f210e9
MP
647 assert(dev);
648 assert(names);
649
6e866b33
MB
650 r = sd_device_get_parent(dev, &parent);
651 if (r < 0)
652 return r;
2897b343
MP
653 /* skip virtio subsystem if present */
654 parent = skip_virtio(parent);
d9dfd233 655
663996b3
MS
656 if (!parent)
657 return -ENOENT;
d9dfd233 658
663996b3 659 /* check if our direct parent is a PCI device with no other bus in-between */
6e866b33
MB
660 if (sd_device_get_subsystem(parent, &subsystem) >= 0 &&
661 streq("pci", subsystem)) {
663996b3
MS
662 names->type = NET_PCI;
663 names->pcidev = parent;
664 } else {
6e866b33
MB
665 r = sd_device_get_parent_with_subsystem_devtype(dev, "pci", NULL, &names->pcidev);
666 if (r < 0)
667 return r;
663996b3 668 }
b012e921 669
6e866b33
MB
670 if (naming_scheme_has(NAMING_SR_IOV_V) &&
671 get_virtfn_info(dev, names, &vf_info) >= 0) {
b012e921
MB
672 /* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
673 vf_names.pcidev = vf_info.physfn_pcidev;
674 dev_pci_onboard(dev, &vf_names);
675 dev_pci_slot(dev, &vf_names);
676 if (vf_names.pci_onboard[0])
677 if (strlen(vf_names.pci_onboard) + strlen(vf_info.suffix) < sizeof(names->pci_onboard))
678 strscpyl(names->pci_onboard, sizeof(names->pci_onboard),
679 vf_names.pci_onboard, vf_info.suffix, NULL);
680 if (vf_names.pci_slot[0])
681 if (strlen(vf_names.pci_slot) + strlen(vf_info.suffix) < sizeof(names->pci_slot))
682 strscpyl(names->pci_slot, sizeof(names->pci_slot),
683 vf_names.pci_slot, vf_info.suffix, NULL);
684 if (vf_names.pci_path[0])
685 if (strlen(vf_names.pci_path) + strlen(vf_info.suffix) < sizeof(names->pci_path))
686 strscpyl(names->pci_path, sizeof(names->pci_path),
687 vf_names.pci_path, vf_info.suffix, NULL);
6e866b33 688 sd_device_unref(vf_info.physfn_pcidev);
b012e921
MB
689 } else {
690 dev_pci_onboard(dev, names);
691 dev_pci_slot(dev, names);
692 }
6e866b33 693
663996b3
MS
694 return 0;
695}
696
6e866b33
MB
697static int names_usb(sd_device *dev, struct netnames *names) {
698 sd_device *usbdev;
699 char name[256], *ports, *config, *interf, *s;
700 const char *sysname;
663996b3 701 size_t l;
6e866b33 702 int r;
663996b3 703
86f210e9
MP
704 assert(dev);
705 assert(names);
706
6e866b33
MB
707 r = sd_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface", &usbdev);
708 if (r < 0)
709 return r;
710
711 r = sd_device_get_sysname(usbdev, &sysname);
712 if (r < 0)
713 return r;
663996b3
MS
714
715 /* get USB port number chain, configuration, interface */
6e866b33 716 strscpy(name, sizeof(name), sysname);
663996b3
MS
717 s = strchr(name, '-');
718 if (!s)
719 return -EINVAL;
720 ports = s+1;
721
722 s = strchr(ports, ':');
723 if (!s)
724 return -EINVAL;
725 s[0] = '\0';
726 config = s+1;
727
728 s = strchr(config, '.');
729 if (!s)
730 return -EINVAL;
731 s[0] = '\0';
732 interf = s+1;
733
e735f4d4 734 /* prefix every port number in the chain with "u" */
663996b3
MS
735 s = ports;
736 while ((s = strchr(s, '.')))
737 s[0] = 'u';
738 s = names->usb_ports;
739 l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
740
741 /* append USB config number, suppress the common config == 1 */
742 if (!streq(config, "1"))
743 l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
744
745 /* append USB interface number, suppress the interface == 0 */
746 if (!streq(interf, "0"))
747 l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
748 if (l == 0)
749 return -ENAMETOOLONG;
750
751 names->type = NET_USB;
752 return 0;
753}
754
6e866b33
MB
755static int names_bcma(sd_device *dev, struct netnames *names) {
756 sd_device *bcmadev;
757 unsigned core;
758 const char *sysname;
759 int r;
663996b3 760
86f210e9
MP
761 assert(dev);
762 assert(names);
763
6e866b33
MB
764 r = sd_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL, &bcmadev);
765 if (r < 0)
766 return r;
767
768 r = sd_device_get_sysname(bcmadev, &sysname);
769 if (r < 0)
770 return r;
663996b3
MS
771
772 /* bus num:core num */
6e866b33 773 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
663996b3
MS
774 return -EINVAL;
775 /* suppress the common core == 0 */
776 if (core > 0)
4c89c718 777 xsprintf(names->bcma_core, "b%u", core);
663996b3
MS
778
779 names->type = NET_BCMA;
780 return 0;
781}
782
6e866b33
MB
783static int names_ccw(sd_device *dev, struct netnames *names) {
784 sd_device *cdev;
2897b343 785 const char *bus_id, *subsys;
60f067b4 786 size_t bus_id_len;
2897b343 787 size_t bus_id_start;
6e866b33 788 int r;
60f067b4 789
86f210e9
MP
790 assert(dev);
791 assert(names);
792
60f067b4 793 /* Retrieve the associated CCW device */
6e866b33
MB
794 r = sd_device_get_parent(dev, &cdev);
795 if (r < 0)
796 return r;
797
2897b343
MP
798 /* skip virtio subsystem if present */
799 cdev = skip_virtio(cdev);
60f067b4
JS
800 if (!cdev)
801 return -ENOENT;
802
6e866b33
MB
803 r = sd_device_get_subsystem(cdev, &subsys);
804 if (r < 0)
805 return r;
806
2897b343 807 /* Network devices are either single or grouped CCW devices */
6e866b33 808 if (!STR_IN_SET(subsys, "ccwgroup", "ccw"))
60f067b4
JS
809 return -ENOENT;
810
2897b343 811 /* Retrieve bus-ID of the CCW device. The bus-ID uniquely
60f067b4
JS
812 * identifies the network device on the Linux on System z channel
813 * subsystem. Note that the bus-ID contains lowercase characters.
814 */
6e866b33
MB
815 r = sd_device_get_sysname(cdev, &bus_id);
816 if (r < 0)
817 return r;
60f067b4
JS
818
819 /* Check the length of the bus-ID. Rely on that the kernel provides
820 * a correct bus-ID; alternatively, improve this check and parse and
821 * verify each bus-ID part...
822 */
823 bus_id_len = strlen(bus_id);
1d42b86d 824 if (!IN_SET(bus_id_len, 8, 9))
60f067b4
JS
825 return -EINVAL;
826
4c89c718
MP
827 /* Strip leading zeros from the bus id for aesthetic purposes. This
828 * keeps the ccw names stable, yet much shorter in general case of
829 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
2897b343 830 * not prepended when it is zero. Preserve the last 0 for 0.0.0000.
4c89c718 831 */
2897b343
MP
832 bus_id_start = strspn(bus_id, ".0");
833 bus_id += bus_id_start < bus_id_len ? bus_id_start : bus_id_len - 1;
4c89c718 834
60f067b4 835 /* Store the CCW bus-ID for use as network device name */
98393f85 836 if (snprintf_ok(names->ccw_busid, sizeof(names->ccw_busid), "c%s", bus_id))
2897b343 837 names->type = NET_CCW;
98393f85 838
60f067b4
JS
839 return 0;
840}
841
6e866b33 842static int names_mac(sd_device *dev, struct netnames *names) {
663996b3 843 const char *s;
6e866b33
MB
844 unsigned long i;
845 unsigned a1, a2, a3, a4, a5, a6;
846 int r;
847
848 /* Some kinds of devices tend to have hardware addresses
849 * that are impossible to use in an iface name.
850 */
851 r = sd_device_get_sysattr_value(dev, "type", &s);
852 if (r < 0)
853 return r;
854
855 i = strtoul(s, NULL, 0);
856 switch (i) {
857 /* The persistent part of a hardware address of an InfiniBand NIC
858 * is 8 bytes long. We cannot fit this much in an iface name.
859 */
860 case ARPHRD_INFINIBAND:
861 return -EINVAL;
862 default:
863 break;
864 }
663996b3
MS
865
866 /* check for NET_ADDR_PERM, skip random MAC addresses */
6e866b33
MB
867 r = sd_device_get_sysattr_value(dev, "addr_assign_type", &s);
868 if (r < 0)
869 return r;
663996b3
MS
870 i = strtoul(s, NULL, 0);
871 if (i != 0)
872 return 0;
873
6e866b33
MB
874 r = sd_device_get_sysattr_value(dev, "address", &s);
875 if (r < 0)
876 return r;
663996b3
MS
877 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
878 return -EINVAL;
879
880 /* skip empty MAC addresses */
881 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
882 return -EINVAL;
883
884 names->mac[0] = a1;
885 names->mac[1] = a2;
886 names->mac[2] = a3;
887 names->mac[3] = a4;
888 names->mac[4] = a5;
889 names->mac[5] = a6;
890 names->mac_valid = true;
891 return 0;
892}
893
894/* IEEE Organizationally Unique Identifier vendor string */
6e866b33 895static int ieee_oui(sd_device *dev, struct netnames *names, bool test) {
663996b3
MS
896 char str[32];
897
898 if (!names->mac_valid)
899 return -ENOENT;
900 /* skip commonly misused 00:00:00 (Xerox) prefix */
901 if (memcmp(names->mac, "\0\0\0", 3) == 0)
902 return -EINVAL;
4c89c718
MP
903 xsprintf(str, "OUI:%02X%02X%02X%02X%02X%02X", names->mac[0],
904 names->mac[1], names->mac[2], names->mac[3], names->mac[4],
905 names->mac[5]);
14228c0d 906 udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
663996b3
MS
907 return 0;
908}
909
6e866b33
MB
910static int builtin_net_id(sd_device *dev, int argc, char *argv[], bool test) {
911 const char *s, *p, *devtype, *prefix = "en";
663996b3 912 struct netnames names = {};
6e866b33
MB
913 unsigned long i;
914 int r;
915
916 /* handle only ARPHRD_ETHER, ARPHRD_SLIP and ARPHRD_INFINIBAND devices */
917 r = sd_device_get_sysattr_value(dev, "type", &s);
918 if (r < 0)
919 return r;
663996b3 920
663996b3 921 i = strtoul(s, NULL, 0);
60f067b4
JS
922 switch (i) {
923 case ARPHRD_ETHER:
924 prefix = "en";
925 break;
6e866b33
MB
926 case ARPHRD_INFINIBAND:
927 if (naming_scheme_has(NAMING_INFINIBAND))
928 prefix = "ib";
929 else
930 return 0;
931 break;
60f067b4
JS
932 case ARPHRD_SLIP:
933 prefix = "sl";
934 break;
935 default:
663996b3 936 return 0;
60f067b4 937 }
663996b3
MS
938
939 /* skip stacked devices, like VLANs, ... */
6e866b33
MB
940 r = sd_device_get_sysattr_value(dev, "ifindex", &s);
941 if (r < 0)
942 return r;
943 r = sd_device_get_sysattr_value(dev, "iflink", &p);
944 if (r < 0)
945 return r;
663996b3
MS
946 if (!streq(s, p))
947 return 0;
948
6e866b33 949 if (sd_device_get_devtype(dev, &devtype) >= 0) {
663996b3
MS
950 if (streq("wlan", devtype))
951 prefix = "wl";
952 else if (streq("wwan", devtype))
953 prefix = "ww";
954 }
955
6e866b33
MB
956 udev_builtin_add_property(dev, test, "ID_NET_NAMING_SCHEME", naming_scheme()->name);
957
958 r = names_mac(dev, &names);
959 if (r >= 0 && names.mac_valid) {
663996b3
MS
960 char str[IFNAMSIZ];
961
4c89c718 962 xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix,
663996b3
MS
963 names.mac[0], names.mac[1], names.mac[2],
964 names.mac[3], names.mac[4], names.mac[5]);
965 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
966
967 ieee_oui(dev, &names, test);
968 }
969
60f067b4 970 /* get path names for Linux on System z network devices */
6e866b33 971 if (names_ccw(dev, &names) >= 0 && names.type == NET_CCW) {
60f067b4
JS
972 char str[IFNAMSIZ];
973
98393f85 974 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.ccw_busid))
60f067b4 975 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
6e866b33 976 return 0;
60f067b4
JS
977 }
978
81c58355 979 /* get ibmveth/ibmvnic slot-based names. */
6e866b33 980 if (names_vio(dev, &names) >= 0 && names.type == NET_VIO) {
81c58355
MB
981 char str[IFNAMSIZ];
982
98393f85 983 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.vio_slot))
81c58355 984 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
6e866b33 985 return 0;
81c58355
MB
986 }
987
988 /* get ACPI path names for ARM64 platform devices */
6e866b33 989 if (names_platform(dev, &names, test) >= 0 && names.type == NET_PLATFORM) {
81c58355
MB
990 char str[IFNAMSIZ];
991
98393f85 992 if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.platform_path))
81c58355 993 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
6e866b33 994 return 0;
81c58355
MB
995 }
996
663996b3 997 /* get PCI based path names, we compose only PCI based paths */
6e866b33
MB
998 if (names_pci(dev, &names) < 0)
999 return 0;
663996b3
MS
1000
1001 /* plain PCI device */
1002 if (names.type == NET_PCI) {
1003 char str[IFNAMSIZ];
1004
98393f85
MB
1005 if (names.pci_onboard[0] &&
1006 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_onboard))
1007 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
663996b3 1008
98393f85
MB
1009 if (names.pci_onboard_label &&
1010 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_onboard_label))
1011 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
663996b3 1012
98393f85
MB
1013 if (names.pci_path[0] &&
1014 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_path))
1015 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
663996b3 1016
98393f85
MB
1017 if (names.pci_slot[0] &&
1018 snprintf_ok(str, sizeof str, "%s%s", prefix, names.pci_slot))
1019 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
6e866b33 1020 return 0;
663996b3
MS
1021 }
1022
1023 /* USB device */
6e866b33 1024 if (names_usb(dev, &names) >= 0 && names.type == NET_USB) {
663996b3
MS
1025 char str[IFNAMSIZ];
1026
98393f85
MB
1027 if (names.pci_path[0] &&
1028 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.usb_ports))
1029 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
663996b3 1030
98393f85
MB
1031 if (names.pci_slot[0] &&
1032 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.usb_ports))
1033 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
6e866b33 1034 return 0;
663996b3
MS
1035 }
1036
1037 /* Broadcom bus */
6e866b33 1038 if (names_bcma(dev, &names) >= 0 && names.type == NET_BCMA) {
663996b3
MS
1039 char str[IFNAMSIZ];
1040
98393f85
MB
1041 if (names.pci_path[0] &&
1042 snprintf_ok(str, sizeof str, "%s%s%s", prefix, names.pci_path, names.bcma_core))
1043 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
663996b3 1044
98393f85
MB
1045 if (names.pci_slot[0] &&
1046 snprintf(str, sizeof str, "%s%s%s", prefix, names.pci_slot, names.bcma_core))
1047 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
6e866b33 1048 return 0;
663996b3 1049 }
6e866b33
MB
1050
1051 return 0;
663996b3
MS
1052}
1053
1054const struct udev_builtin udev_builtin_net_id = {
1055 .name = "net_id",
1056 .cmd = builtin_net_id,
e735f4d4 1057 .help = "Network device properties",
663996b3 1058};