chiark / gitweb /
build-sys: add a makefile target to run all tests through valgrind
[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 };
105
106 struct netnames {
107         enum netname_type type;
108
109         uint8_t mac[6];
110         bool mac_valid;
111
112         struct udev_device *pcidev;
113         char pci_slot[IFNAMSIZ];
114         char pci_path[IFNAMSIZ];
115         char pci_onboard[IFNAMSIZ];
116         const char *pci_onboard_label;
117
118         char usb_ports[IFNAMSIZ];
119
120         char bcma_core[IFNAMSIZ];
121 };
122
123 /* retrieve on-board index number and label from firmware */
124 static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
125         const char *index;
126         int idx;
127
128         /* ACPI _DSM  -- device specific method for naming a PCI or PCI Express device */
129         index = udev_device_get_sysattr_value(names->pcidev, "acpi_index");
130         /* SMBIOS type 41 -- Onboard Devices Extended Information */
131         if (!index)
132                 index = udev_device_get_sysattr_value(names->pcidev, "index");
133         if (!index)
134                 return -ENOENT;
135         idx = strtoul(index, NULL, 0);
136         if (idx <= 0)
137                 return -EINVAL;
138         snprintf(names->pci_onboard, sizeof(names->pci_onboard), "o%d", idx);
139
140         names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label");
141         return 0;
142 }
143
144 /* read the 256 bytes PCI configuration space to check the multi-function bit */
145 static bool is_pci_multifunction(struct udev_device *dev) {
146         char filename[256];
147         FILE *f = NULL;
148         char config[64];
149         bool multi = false;
150
151         snprintf(filename, sizeof(filename), "%s/config", udev_device_get_syspath(dev));
152         f = fopen(filename, "re");
153         if (!f)
154                 goto out;
155         if (fread(&config, sizeof(config), 1, f) != 1)
156                 goto out;
157
158         /* bit 0-6 header type, bit 7 multi/single function device */
159         if ((config[PCI_HEADER_TYPE] & 0x80) != 0)
160                 multi = true;
161 out:
162         if(f)
163                 fclose(f);
164         return multi;
165 }
166
167 static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
168         struct udev *udev = udev_device_get_udev(names->pcidev);
169         unsigned int domain;
170         unsigned int bus;
171         unsigned int slot;
172         unsigned int func;
173         unsigned int dev_id = 0;
174         size_t l;
175         char *s;
176         const char *attr;
177         struct udev_device *pci = NULL;
178         char slots[256];
179         DIR *dir;
180         struct dirent *dent;
181         char str[256];
182         int hotplug_slot = 0;
183         int err = 0;
184
185         if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%d", &domain, &bus, &slot, &func) != 4)
186                 return -ENOENT;
187
188         /* kernel provided multi-device index */
189         attr = udev_device_get_sysattr_value(dev, "dev_id");
190         if (attr)
191                 dev_id = strtol(attr, NULL, 16);
192
193         /* compose a name based on the raw kernel's PCI bus, slot numbers */
194         s = names->pci_path;
195         l = sizeof(names->pci_path);
196         if (domain > 0)
197                 l = strpcpyf(&s, l, "P%d", domain);
198         l = strpcpyf(&s, l, "p%ds%d", bus, slot);
199         if (func > 0 || is_pci_multifunction(names->pcidev))
200                 l = strpcpyf(&s, l, "f%d", func);
201         if (dev_id > 0)
202                 l = strpcpyf(&s, l, "d%d", dev_id);
203         if (l == 0)
204                 names->pci_path[0] = '\0';
205
206         /* ACPI _SUN  -- slot user number */
207         pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
208         if (!pci) {
209                 err = -ENOENT;
210                 goto out;
211         }
212         snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
213         dir = opendir(slots);
214         if (!dir) {
215                 err = -errno;
216                 goto out;
217         }
218
219         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
220                 int i;
221                 char *rest;
222                 char *address;
223
224                 if (dent->d_name[0] == '.')
225                         continue;
226                 i = strtol(dent->d_name, &rest, 10);
227                 if (rest[0] != '\0')
228                         continue;
229                 if (i < 1)
230                         continue;
231                 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
232                 if (read_one_line_file(str, &address) >= 0) {
233                         /* match slot address with device by stripping the function */
234                         if (strneq(address, udev_device_get_sysname(names->pcidev), strlen(address)))
235                                 hotplug_slot = i;
236                         free(address);
237                 }
238
239                 if (hotplug_slot > 0)
240                         break;
241         }
242         closedir(dir);
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%*d:%d", &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%d", core);
349
350         names->type = NET_BCMA;
351         return 0;
352 }
353
354 static int names_mac(struct udev_device *dev, struct netnames *names) {
355         const char *s;
356         unsigned int i;
357         unsigned int a1, a2, a3, a4, a5, a6;
358
359         /* check for NET_ADDR_PERM, skip random MAC addresses */
360         s = udev_device_get_sysattr_value(dev, "addr_assign_type");
361         if (!s)
362                 return EXIT_FAILURE;
363         i = strtoul(s, NULL, 0);
364         if (i != 0)
365                 return 0;
366
367         s = udev_device_get_sysattr_value(dev, "address");
368         if (!s)
369                 return -ENOENT;
370         if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
371                 return -EINVAL;
372
373         /* skip empty MAC addresses */
374         if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
375                 return -EINVAL;
376
377         names->mac[0] = a1;
378         names->mac[1] = a2;
379         names->mac[2] = a3;
380         names->mac[3] = a4;
381         names->mac[4] = a5;
382         names->mac[5] = a6;
383         names->mac_valid = true;
384         return 0;
385 }
386
387 /* IEEE Organizationally Unique Identifier vendor string */
388 static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) {
389         char str[32];
390
391         if (!names->mac_valid)
392                 return -ENOENT;
393         /* skip commonly misused 00:00:00 (Xerox) prefix */
394         if (memcmp(names->mac, "\0\0\0", 3) == 0)
395                 return -EINVAL;
396         snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X",
397                  names->mac[0], names->mac[1], names->mac[2],
398                  names->mac[3], names->mac[4], names->mac[5]);
399         udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
400         return 0;
401 }
402
403 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
404         const char *s;
405         const char *p;
406         unsigned int i;
407         const char *devtype;
408         const char *prefix = "en";
409         struct netnames names = {};
410         int err;
411
412         /* handle only ARPHRD_ETHER devices */
413         s = udev_device_get_sysattr_value(dev, "type");
414         if (!s)
415                 return EXIT_FAILURE;
416         i = strtoul(s, NULL, 0);
417         if (i != 1)
418                 return 0;
419
420         /* skip stacked devices, like VLANs, ... */
421         s = udev_device_get_sysattr_value(dev, "ifindex");
422         if (!s)
423                 return EXIT_FAILURE;
424         p = udev_device_get_sysattr_value(dev, "iflink");
425         if (!p)
426                 return EXIT_FAILURE;
427         if (!streq(s, p))
428                 return 0;
429
430         devtype = udev_device_get_devtype(dev);
431         if (devtype) {
432                 if (streq("wlan", devtype))
433                         prefix = "wl";
434                 else if (streq("wwan", devtype))
435                         prefix = "ww";
436         }
437
438         err = names_mac(dev, &names);
439         if (err >= 0 && names.mac_valid) {
440                 char str[IFNAMSIZ];
441
442                 snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix,
443                          names.mac[0], names.mac[1], names.mac[2],
444                          names.mac[3], names.mac[4], names.mac[5]);
445                 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
446
447                 ieee_oui(dev, &names, test);
448         }
449
450         /* get PCI based path names, we compose only PCI based paths */
451         err = names_pci(dev, &names);
452         if (err < 0)
453                 goto out;
454
455         /* plain PCI device */
456         if (names.type == NET_PCI) {
457                 char str[IFNAMSIZ];
458
459                 if (names.pci_onboard[0])
460                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str))
461                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
462
463                 if (names.pci_onboard_label)
464                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str))
465                                 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
466
467                 if (names.pci_path[0])
468                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (int)sizeof(str))
469                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
470
471                 if (names.pci_slot[0])
472                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_slot) < (int)sizeof(str))
473                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
474                 goto out;
475         }
476
477         /* USB device */
478         err = names_usb(dev, &names);
479         if (err >= 0 && names.type == NET_USB) {
480                 char str[IFNAMSIZ];
481
482                 if (names.pci_path[0])
483                         if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (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%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str))
488                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
489                 goto out;
490         }
491
492         /* Broadcom bus */
493         err = names_bcma(dev, &names);
494         if (err >= 0 && names.type == NET_BCMA) {
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.bcma_core) < (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.bcma_core) < (int)sizeof(str))
503                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
504                 goto out;
505         }
506
507 out:
508         return EXIT_SUCCESS;
509 }
510
511 const struct udev_builtin udev_builtin_net_id = {
512         .name = "net_id",
513         .cmd = builtin_net_id,
514         .help = "network device properties",
515 };