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