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