]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-net_id.c
Merge pull request #4548 from keszybz/seccomp-help
[thirdparty/systemd.git] / src / udev / udev-builtin-net_id.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Kay Sievers <kay@vrfy.org>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 /*
21 * Predictable network interface device names based on:
22 * - firmware/bios-provided index numbers for on-board devices
23 * - firmware-provided pci-express hotplug slot index number
24 * - physical/geographical location of the hardware
25 * - the interface's MAC address
26 *
27 * http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames
28 *
29 * Two character prefixes based on the type of interface:
30 * en — Ethernet
31 * sl — serial line IP (slip)
32 * wl — wlan
33 * ww — wwan
34 *
35 * Type of names:
36 * b<number> — BCMA bus core number
37 * c<bus_id> — CCW bus group name, without leading zeros [s390]
38 * o<index>[n<phys_port_name>|d<dev_port>]
39 * — on-board device index number
40 * s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
41 * — hotplug slot index number
42 * x<MAC> — MAC address
43 * [P<domain>]p<bus>s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
44 * — PCI geographical location
45 * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
46 * — USB port number chain
47 *
48 * All multi-function PCI devices will carry the [f<function>] number in the
49 * device name, including the function 0 device.
50 *
51 * When using PCI geography, The PCI domain is only prepended when it is not 0.
52 *
53 * For USB devices the full chain of port numbers of hubs is composed. If the
54 * name gets longer than the maximum number of 15 characters, the name is not
55 * exported.
56 * The usual USB configuration == 1 and interface == 0 values are suppressed.
57 *
58 * PCI Ethernet card with firmware index "1":
59 * ID_NET_NAME_ONBOARD=eno1
60 * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
61 *
62 * PCI Ethernet card in hotplug slot with firmware index number:
63 * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
64 * ID_NET_NAME_MAC=enx000000000466
65 * ID_NET_NAME_PATH=enp5s0
66 * ID_NET_NAME_SLOT=ens1
67 *
68 * PCI Ethernet multi-function card with 2 ports:
69 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0/net/enp2s0f0
70 * ID_NET_NAME_MAC=enx78e7d1ea46da
71 * ID_NET_NAME_PATH=enp2s0f0
72 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.1/net/enp2s0f1
73 * ID_NET_NAME_MAC=enx78e7d1ea46dc
74 * ID_NET_NAME_PATH=enp2s0f1
75 *
76 * PCI wlan card:
77 * /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
78 * ID_NET_NAME_MAC=wlx0024d7e31130
79 * ID_NET_NAME_PATH=wlp3s0
80 *
81 * USB built-in 3G modem:
82 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
83 * ID_NET_NAME_MAC=wwx028037ec0200
84 * ID_NET_NAME_PATH=wwp0s29u1u4i6
85 *
86 * USB Android phone:
87 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
88 * ID_NET_NAME_MAC=enxd626b3450fb5
89 * ID_NET_NAME_PATH=enp0s29u1u2
90 */
91
92 #include <errno.h>
93 #include <fcntl.h>
94 #include <net/if.h>
95 #include <net/if_arp.h>
96 #include <stdarg.h>
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <linux/pci_regs.h>
102
103 #include "fd-util.h"
104 #include "fileio.h"
105 #include "stdio-util.h"
106 #include "string-util.h"
107 #include "udev.h"
108
109 #define ONBOARD_INDEX_MAX (16*1024-1)
110
111 enum netname_type{
112 NET_UNDEF,
113 NET_PCI,
114 NET_USB,
115 NET_BCMA,
116 NET_VIRTIO,
117 NET_CCWGROUP,
118 };
119
120 struct netnames {
121 enum netname_type type;
122
123 uint8_t mac[6];
124 bool mac_valid;
125
126 struct udev_device *pcidev;
127 char pci_slot[IFNAMSIZ];
128 char pci_path[IFNAMSIZ];
129 char pci_onboard[IFNAMSIZ];
130 const char *pci_onboard_label;
131
132 char usb_ports[IFNAMSIZ];
133 char bcma_core[IFNAMSIZ];
134 char ccw_group[IFNAMSIZ];
135 };
136
137 /* retrieve on-board index number and label from firmware */
138 static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
139 unsigned dev_port = 0;
140 size_t l;
141 char *s;
142 const char *attr, *port_name;
143 int idx;
144
145 /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
146 attr = udev_device_get_sysattr_value(names->pcidev, "acpi_index");
147 /* SMBIOS type 41 — Onboard Devices Extended Information */
148 if (!attr)
149 attr = udev_device_get_sysattr_value(names->pcidev, "index");
150 if (!attr)
151 return -ENOENT;
152
153 idx = strtoul(attr, NULL, 0);
154 if (idx <= 0)
155 return -EINVAL;
156
157 /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
158 * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
159 * cut-off, which is somewhere beyond the realistic number of physical network interface a system might
160 * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
161 if (idx > ONBOARD_INDEX_MAX)
162 return -ENOENT;
163
164 /* kernel provided port index for multiple ports on a single PCI function */
165 attr = udev_device_get_sysattr_value(dev, "dev_port");
166 if (attr)
167 dev_port = strtol(attr, NULL, 10);
168
169 /* kernel provided front panel port name for multiple port PCI device */
170 port_name = udev_device_get_sysattr_value(dev, "phys_port_name");
171
172 s = names->pci_onboard;
173 l = sizeof(names->pci_onboard);
174 l = strpcpyf(&s, l, "o%d", idx);
175 if (port_name)
176 l = strpcpyf(&s, l, "n%s", port_name);
177 else if (dev_port > 0)
178 l = strpcpyf(&s, l, "d%d", dev_port);
179 if (l == 0)
180 names->pci_onboard[0] = '\0';
181
182 names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label");
183
184 return 0;
185 }
186
187 /* read the 256 bytes PCI configuration space to check the multi-function bit */
188 static bool is_pci_multifunction(struct udev_device *dev) {
189 _cleanup_close_ int fd = -1;
190 const char *filename;
191 uint8_t config[64];
192
193 filename = strjoina(udev_device_get_syspath(dev), "/config");
194 fd = open(filename, O_RDONLY | O_CLOEXEC);
195 if (fd < 0)
196 return false;
197 if (read(fd, &config, sizeof(config)) != sizeof(config))
198 return false;
199
200 /* bit 0-6 header type, bit 7 multi/single function device */
201 if ((config[PCI_HEADER_TYPE] & 0x80) != 0)
202 return true;
203
204 return false;
205 }
206
207 static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
208 struct udev *udev = udev_device_get_udev(names->pcidev);
209 unsigned domain, bus, slot, func, dev_port = 0;
210 size_t l;
211 char *s;
212 const char *attr, *port_name;
213 struct udev_device *pci = NULL;
214 char slots[PATH_MAX];
215 _cleanup_closedir_ DIR *dir = NULL;
216 struct dirent *dent;
217 int hotplug_slot = 0, err = 0;
218
219 if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
220 return -ENOENT;
221
222 /* kernel provided port index for multiple ports on a single PCI function */
223 attr = udev_device_get_sysattr_value(dev, "dev_port");
224 if (attr)
225 dev_port = strtol(attr, NULL, 10);
226
227 /* kernel provided front panel port name for multiple port PCI device */
228 port_name = udev_device_get_sysattr_value(dev, "phys_port_name");
229
230 /* compose a name based on the raw kernel's PCI bus, slot numbers */
231 s = names->pci_path;
232 l = sizeof(names->pci_path);
233 if (domain > 0)
234 l = strpcpyf(&s, l, "P%u", domain);
235 l = strpcpyf(&s, l, "p%us%u", bus, slot);
236 if (func > 0 || is_pci_multifunction(names->pcidev))
237 l = strpcpyf(&s, l, "f%u", func);
238 if (port_name)
239 l = strpcpyf(&s, l, "n%s", port_name);
240 else if (dev_port > 0)
241 l = strpcpyf(&s, l, "d%u", dev_port);
242 if (l == 0)
243 names->pci_path[0] = '\0';
244
245 /* ACPI _SUN — slot user number */
246 pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
247 if (!pci) {
248 err = -ENOENT;
249 goto out;
250 }
251
252 snprintf(slots, sizeof slots, "%s/slots", udev_device_get_syspath(pci));
253 dir = opendir(slots);
254 if (!dir) {
255 err = -errno;
256 goto out;
257 }
258
259 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
260 int i;
261 char *rest, *address, str[PATH_MAX];
262
263 if (dent->d_name[0] == '.')
264 continue;
265 i = strtol(dent->d_name, &rest, 10);
266 if (rest[0] != '\0')
267 continue;
268 if (i < 1)
269 continue;
270
271 snprintf(str, sizeof str, "%s/%s/address", slots, dent->d_name);
272 if (read_one_line_file(str, &address) >= 0) {
273 /* match slot address with device by stripping the function */
274 if (strneq(address, udev_device_get_sysname(names->pcidev), strlen(address)))
275 hotplug_slot = i;
276 free(address);
277 }
278
279 if (hotplug_slot > 0)
280 break;
281 }
282
283 if (hotplug_slot > 0) {
284 s = names->pci_slot;
285 l = sizeof(names->pci_slot);
286 if (domain > 0)
287 l = strpcpyf(&s, l, "P%d", domain);
288 l = strpcpyf(&s, l, "s%d", hotplug_slot);
289 if (func > 0 || is_pci_multifunction(names->pcidev))
290 l = strpcpyf(&s, l, "f%d", func);
291 if (port_name)
292 l = strpcpyf(&s, l, "n%s", port_name);
293 else if (dev_port > 0)
294 l = strpcpyf(&s, l, "d%d", dev_port);
295 if (l == 0)
296 names->pci_slot[0] = '\0';
297 }
298 out:
299 udev_device_unref(pci);
300 return err;
301 }
302
303 static int names_pci(struct udev_device *dev, struct netnames *names) {
304 struct udev_device *parent;
305
306 assert(dev);
307 assert(names);
308
309 parent = udev_device_get_parent(dev);
310
311 /* there can only ever be one virtio bus per parent device, so we can
312 safely ignore any virtio buses. see
313 <http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html> */
314 while (parent && streq_ptr("virtio", udev_device_get_subsystem(parent)))
315 parent = udev_device_get_parent(parent);
316
317 if (!parent)
318 return -ENOENT;
319
320 /* check if our direct parent is a PCI device with no other bus in-between */
321 if (streq_ptr("pci", udev_device_get_subsystem(parent))) {
322 names->type = NET_PCI;
323 names->pcidev = parent;
324 } else {
325 names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
326 if (!names->pcidev)
327 return -ENOENT;
328 }
329 dev_pci_onboard(dev, names);
330 dev_pci_slot(dev, names);
331 return 0;
332 }
333
334 static int names_usb(struct udev_device *dev, struct netnames *names) {
335 struct udev_device *usbdev;
336 char name[256];
337 char *ports;
338 char *config;
339 char *interf;
340 size_t l;
341 char *s;
342
343 assert(dev);
344 assert(names);
345
346 usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
347 if (!usbdev)
348 return -ENOENT;
349
350 /* get USB port number chain, configuration, interface */
351 strscpy(name, sizeof(name), udev_device_get_sysname(usbdev));
352 s = strchr(name, '-');
353 if (!s)
354 return -EINVAL;
355 ports = s+1;
356
357 s = strchr(ports, ':');
358 if (!s)
359 return -EINVAL;
360 s[0] = '\0';
361 config = s+1;
362
363 s = strchr(config, '.');
364 if (!s)
365 return -EINVAL;
366 s[0] = '\0';
367 interf = s+1;
368
369 /* prefix every port number in the chain with "u" */
370 s = ports;
371 while ((s = strchr(s, '.')))
372 s[0] = 'u';
373 s = names->usb_ports;
374 l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
375
376 /* append USB config number, suppress the common config == 1 */
377 if (!streq(config, "1"))
378 l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
379
380 /* append USB interface number, suppress the interface == 0 */
381 if (!streq(interf, "0"))
382 l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
383 if (l == 0)
384 return -ENAMETOOLONG;
385
386 names->type = NET_USB;
387 return 0;
388 }
389
390 static int names_bcma(struct udev_device *dev, struct netnames *names) {
391 struct udev_device *bcmadev;
392 unsigned int core;
393
394 assert(dev);
395 assert(names);
396
397 bcmadev = udev_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL);
398 if (!bcmadev)
399 return -ENOENT;
400
401 /* bus num:core num */
402 if (sscanf(udev_device_get_sysname(bcmadev), "bcma%*u:%u", &core) != 1)
403 return -EINVAL;
404 /* suppress the common core == 0 */
405 if (core > 0)
406 xsprintf(names->bcma_core, "b%u", core);
407
408 names->type = NET_BCMA;
409 return 0;
410 }
411
412 static int names_ccw(struct udev_device *dev, struct netnames *names) {
413 struct udev_device *cdev;
414 const char *bus_id;
415 size_t bus_id_len;
416 int rc;
417
418 assert(dev);
419 assert(names);
420
421 /* Retrieve the associated CCW device */
422 cdev = udev_device_get_parent(dev);
423 if (!cdev)
424 return -ENOENT;
425
426 /* Network devices are always grouped CCW devices */
427 if (!streq_ptr("ccwgroup", udev_device_get_subsystem(cdev)))
428 return -ENOENT;
429
430 /* Retrieve bus-ID of the grouped CCW device. The bus-ID uniquely
431 * identifies the network device on the Linux on System z channel
432 * subsystem. Note that the bus-ID contains lowercase characters.
433 */
434 bus_id = udev_device_get_sysname(cdev);
435 if (!bus_id)
436 return -ENOENT;
437
438 /* Check the length of the bus-ID. Rely on that the kernel provides
439 * a correct bus-ID; alternatively, improve this check and parse and
440 * verify each bus-ID part...
441 */
442 bus_id_len = strlen(bus_id);
443 if (!bus_id_len || bus_id_len < 8 || bus_id_len > 9)
444 return -EINVAL;
445
446 /* Strip leading zeros from the bus id for aesthetic purposes. This
447 * keeps the ccw names stable, yet much shorter in general case of
448 * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
449 * not prepended when it is zero.
450 */
451 bus_id += strspn(bus_id, ".0");
452
453 /* Store the CCW bus-ID for use as network device name */
454 rc = snprintf(names->ccw_group, sizeof(names->ccw_group), "c%s", bus_id);
455 if (rc >= 0 && rc < (int)sizeof(names->ccw_group))
456 names->type = NET_CCWGROUP;
457 return 0;
458 }
459
460 static int names_mac(struct udev_device *dev, struct netnames *names) {
461 const char *s;
462 unsigned int i;
463 unsigned int a1, a2, a3, a4, a5, a6;
464
465 /* check for NET_ADDR_PERM, skip random MAC addresses */
466 s = udev_device_get_sysattr_value(dev, "addr_assign_type");
467 if (!s)
468 return EXIT_FAILURE;
469 i = strtoul(s, NULL, 0);
470 if (i != 0)
471 return 0;
472
473 s = udev_device_get_sysattr_value(dev, "address");
474 if (!s)
475 return -ENOENT;
476 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
477 return -EINVAL;
478
479 /* skip empty MAC addresses */
480 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
481 return -EINVAL;
482
483 names->mac[0] = a1;
484 names->mac[1] = a2;
485 names->mac[2] = a3;
486 names->mac[3] = a4;
487 names->mac[4] = a5;
488 names->mac[5] = a6;
489 names->mac_valid = true;
490 return 0;
491 }
492
493 /* IEEE Organizationally Unique Identifier vendor string */
494 static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) {
495 char str[32];
496
497 if (!names->mac_valid)
498 return -ENOENT;
499 /* skip commonly misused 00:00:00 (Xerox) prefix */
500 if (memcmp(names->mac, "\0\0\0", 3) == 0)
501 return -EINVAL;
502 xsprintf(str, "OUI:%02X%02X%02X%02X%02X%02X", names->mac[0],
503 names->mac[1], names->mac[2], names->mac[3], names->mac[4],
504 names->mac[5]);
505 udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
506 return 0;
507 }
508
509 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
510 const char *s;
511 const char *p;
512 unsigned int i;
513 const char *devtype;
514 const char *prefix = "en";
515 struct netnames names = {};
516 int err;
517
518 /* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */
519 s = udev_device_get_sysattr_value(dev, "type");
520 if (!s)
521 return EXIT_FAILURE;
522 i = strtoul(s, NULL, 0);
523 switch (i) {
524 case ARPHRD_ETHER:
525 prefix = "en";
526 break;
527 case ARPHRD_SLIP:
528 prefix = "sl";
529 break;
530 default:
531 return 0;
532 }
533
534 /* skip stacked devices, like VLANs, ... */
535 s = udev_device_get_sysattr_value(dev, "ifindex");
536 if (!s)
537 return EXIT_FAILURE;
538 p = udev_device_get_sysattr_value(dev, "iflink");
539 if (!p)
540 return EXIT_FAILURE;
541 if (!streq(s, p))
542 return 0;
543
544 devtype = udev_device_get_devtype(dev);
545 if (devtype) {
546 if (streq("wlan", devtype))
547 prefix = "wl";
548 else if (streq("wwan", devtype))
549 prefix = "ww";
550 }
551
552 err = names_mac(dev, &names);
553 if (err >= 0 && names.mac_valid) {
554 char str[IFNAMSIZ];
555
556 xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix,
557 names.mac[0], names.mac[1], names.mac[2],
558 names.mac[3], names.mac[4], names.mac[5]);
559 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
560
561 ieee_oui(dev, &names, test);
562 }
563
564 /* get path names for Linux on System z network devices */
565 err = names_ccw(dev, &names);
566 if (err >= 0 && names.type == NET_CCWGROUP) {
567 char str[IFNAMSIZ];
568
569 if (snprintf(str, sizeof(str), "%s%s", prefix, names.ccw_group) < (int)sizeof(str))
570 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
571 goto out;
572 }
573
574 /* get PCI based path names, we compose only PCI based paths */
575 err = names_pci(dev, &names);
576 if (err < 0)
577 goto out;
578
579 /* plain PCI device */
580 if (names.type == NET_PCI) {
581 char str[IFNAMSIZ];
582
583 if (names.pci_onboard[0])
584 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str))
585 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
586
587 if (names.pci_onboard_label)
588 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str))
589 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
590
591 if (names.pci_path[0])
592 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (int)sizeof(str))
593 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
594
595 if (names.pci_slot[0])
596 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_slot) < (int)sizeof(str))
597 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
598 goto out;
599 }
600
601 /* USB device */
602 err = names_usb(dev, &names);
603 if (err >= 0 && names.type == NET_USB) {
604 char str[IFNAMSIZ];
605
606 if (names.pci_path[0])
607 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (int)sizeof(str))
608 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
609
610 if (names.pci_slot[0])
611 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str))
612 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
613 goto out;
614 }
615
616 /* Broadcom bus */
617 err = names_bcma(dev, &names);
618 if (err >= 0 && names.type == NET_BCMA) {
619 char str[IFNAMSIZ];
620
621 if (names.pci_path[0])
622 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.bcma_core) < (int)sizeof(str))
623 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
624
625 if (names.pci_slot[0])
626 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.bcma_core) < (int)sizeof(str))
627 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
628 goto out;
629 }
630 out:
631 return EXIT_SUCCESS;
632 }
633
634 const struct udev_builtin udev_builtin_net_id = {
635 .name = "net_id",
636 .cmd = builtin_net_id,
637 .help = "Network device properties",
638 };