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