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