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