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