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