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