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