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