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