chiark / gitweb /
d8d45081776a9b0770d659fd1c3fe44c4a3f7fd7
[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>]       -- hotplug slot index number
35  *   x<MAC>                     -- MAC address
36  *   p<bus>s<slot>[f<function>] -- 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 };
98
99 struct netnames {
100         enum netname_type type;
101
102         uint8_t mac[6];
103         bool mac_valid;
104
105         struct udev_device *pcidev;
106         char pci_slot[IFNAMSIZ];
107         char pci_path[IFNAMSIZ];
108         char pci_onboard[IFNAMSIZ];
109         const char *pci_onboard_label;
110
111         struct udev_device *usbdev;
112         char usb_ports[IFNAMSIZ];
113 };
114
115 /* retrieve on-board index number and label from firmware */
116 static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
117         const char *index;
118         int idx;
119
120         /* ACPI _DSM  -- device specific method for naming a PCI or PCI Express device */
121         index = udev_device_get_sysattr_value(names->pcidev, "acpi_index");
122         /* SMBIOS type 41 -- Onboard Devices Extended Information */
123         if (!index)
124                 index = udev_device_get_sysattr_value(names->pcidev, "index");
125         if (!index)
126                 return -ENOENT;
127         idx = strtoul(index, NULL, 0);
128         if (idx <= 0)
129                 return -EINVAL;
130         snprintf(names->pci_onboard, sizeof(names->pci_onboard), "o%d", idx);
131
132         names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label");
133         return 0;
134 }
135
136 /* read the 256 bytes PCI configuration space to check the multi-function bit */
137 static bool is_pci_singlefunction(struct udev_device *dev) {
138         char filename[256];
139         FILE *f;
140         char config[64];
141         bool single = false;
142
143         snprintf(filename, sizeof(filename), "%s/config", udev_device_get_syspath(dev));
144         f = fopen(filename, "re");
145         if (!f)
146                 goto out;
147         if (fread(&config, sizeof(config), 1, f) != 1)
148                 goto out;
149
150         /* bit 0-6 header type, bit 7 multi/single function device */
151         if ((config[PCI_HEADER_TYPE] & 0x80) == 0)
152                 single = true;
153 out:
154         fclose(f);
155         return single;
156 }
157
158 static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
159         struct udev *udev = udev_device_get_udev(names->pcidev);
160         unsigned int bus;
161         unsigned int slot;
162         unsigned int func;
163         struct udev_device *pci = NULL;
164         char slots[256];
165         DIR *dir;
166         struct dirent *dent;
167         char str[256];
168         int hotplug_slot = 0;
169         int err = 0;
170
171         /* compose a name based on the raw kernel's PCI bus, slot numbers */
172         if (sscanf(udev_device_get_sysname(names->pcidev), "0000:%x:%x.%d", &bus, &slot, &func) != 3)
173                 return -ENOENT;
174         if (func == 0 && is_pci_singlefunction(names->pcidev))
175                 snprintf(names->pci_path, sizeof(names->pci_path), "p%ds%d", bus, slot);
176         else
177                 snprintf(names->pci_path, sizeof(names->pci_path), "p%ds%df%d", bus, slot, func);
178
179         /* ACPI _SUN  -- slot user number */
180         pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
181         if (!pci) {
182                 err = -ENOENT;
183                 goto out;
184         }
185         snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
186         dir = opendir(slots);
187         if (!dir) {
188                 err = -errno;
189                 goto out;
190         }
191
192         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
193                 int i;
194                 char *rest;
195                 char *address;
196
197                 if (dent->d_name[0] == '.')
198                         continue;
199                 i = strtol(dent->d_name, &rest, 10);
200                 if (rest[0] != '\0')
201                         continue;
202                 if (i < 1)
203                         continue;
204                 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
205                 if (read_one_line_file(str, &address) >= 0) {
206                         /* match slot address with device by stripping the function */
207                         if (strncmp(address, udev_device_get_sysname(names->pcidev), strlen(address)) == 0)
208                                 hotplug_slot = i;
209                         free(address);
210                 }
211
212                 if (hotplug_slot > 0)
213                         break;
214         }
215         closedir(dir);
216
217         if (hotplug_slot > 0) {
218                 if (func == 0 && is_pci_singlefunction(names->pcidev))
219                         snprintf(names->pci_slot, sizeof(names->pci_slot), "s%d", hotplug_slot);
220                 else
221                         snprintf(names->pci_slot, sizeof(names->pci_slot), "s%df%d", hotplug_slot, func);
222         }
223 out:
224         udev_device_unref(pci);
225         return err;
226 }
227
228 static int names_pci(struct udev_device *dev, struct netnames *names) {
229         struct udev_device *parent;
230
231         parent = udev_device_get_parent(dev);
232         if (!parent)
233                 return -ENOENT;
234         /* check if our direct parent is a PCI device with no other bus in-between */
235         if (streq("pci", udev_device_get_subsystem(parent))) {
236                 names->type = NET_PCI;
237                 names->pcidev = parent;
238         } else {
239                 names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
240                 if (!names->pcidev)
241                         return -ENOENT;
242         }
243         dev_pci_onboard(dev, names);
244         dev_pci_slot(dev, names);
245         return 0;
246 }
247
248 static int names_usb(struct udev_device *dev, struct netnames *names) {
249         char name[256];
250         char *ports;
251         char *config;
252         char *interf;
253         size_t l;
254         char *s;
255
256         names->usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
257         if (!names->usbdev)
258                 return -ENOENT;
259
260         /* get USB port number chain, configuration, interface */
261         util_strscpy(name, sizeof(name), udev_device_get_sysname(names->usbdev));
262         s = strchr(name, '-');
263         if (!s)
264                 return -EINVAL;
265         ports = s+1;
266
267         s = strchr(ports, ':');
268         if (!s)
269                 return -EINVAL;
270         s[0] = '\0';
271         config = s+1;
272
273         s = strchr(config, '.');
274         if (!s)
275                 return -EINVAL;
276         s[0] = '\0';
277         interf = s+1;
278
279         /* prefix every port number in the chain with "u"*/
280         s = ports;
281         while ((s = strchr(s, '.')))
282                 s[0] = 'u';
283         s = names->usb_ports;
284         l = util_strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
285
286         /* append USB config number, suppress the common config == 1 */
287         if (!streq(config, "1"))
288                 l = util_strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
289
290         /* append USB interface number, suppress the interface == 0 */
291         if (!streq(interf, "0"))
292                 l = util_strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
293         if (l == 0)
294                 return -ENAMETOOLONG;
295
296         names->type = NET_USB;
297         return 0;
298 }
299
300 static int names_mac(struct udev_device *dev, struct netnames *names) {
301         const char *s;
302         unsigned int i;
303         unsigned int a1, a2, a3, a4, a5, a6;
304
305         /* check for NET_ADDR_PERM, skip random MAC addresses */
306         s = udev_device_get_sysattr_value(dev, "addr_assign_type");
307         if (!s)
308                 return EXIT_FAILURE;
309         i = strtoul(s, NULL, 0);
310         if (i != 0)
311                 return 0;
312
313         s = udev_device_get_sysattr_value(dev, "address");
314         if (!s)
315                 return -ENOENT;
316         if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
317                 return -EINVAL;
318
319         /* skip empty MAC addresses */
320         if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
321                 return -EINVAL;
322
323         names->mac[0] = a1;
324         names->mac[1] = a2;
325         names->mac[2] = a3;
326         names->mac[3] = a4;
327         names->mac[4] = a5;
328         names->mac[5] = a6;
329         names->mac_valid = true;
330         return 0;
331 }
332
333 /* IEEE Organizationally Unique Identifier vendor string */
334 static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) {
335         char str[32];
336
337         if (!names->mac_valid)
338                 return -ENOENT;
339         /* skip commonly misused 00:00:00 (Xerox) prefix */
340         if (memcmp(names->mac, "\0\0\0", 3) == 0)
341                 return -EINVAL;
342         snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X",
343                  names->mac[0], names->mac[1], names->mac[2],
344                  names->mac[3], names->mac[4], names->mac[5]);
345         udev_builtin_hwdb_lookup(dev, str, test);
346         return 0;
347 }
348
349 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
350         const char *s;
351         unsigned int i;
352         const char *devtype;
353         const char *prefix = "en";
354         struct netnames names;
355         int err;
356
357         /* handle only ARPHRD_ETHER devices */
358         s = udev_device_get_sysattr_value(dev, "type");
359         if (!s)
360                 return EXIT_FAILURE;
361         i = strtoul(s, NULL, 0);
362         if (i != 1)
363                 return 0;
364
365         devtype = udev_device_get_devtype(dev);
366         if (devtype) {
367                 if (streq("wlan", devtype))
368                         prefix = "wl";
369                 else if (streq("wwan", devtype))
370                         prefix = "ww";
371         }
372
373         zero(names);
374         err = names_mac(dev, &names);
375         if (err >= 0 && names.mac_valid) {
376                 char str[IFNAMSIZ];
377
378                 snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix,
379                          names.mac[0], names.mac[1], names.mac[2],
380                          names.mac[3], names.mac[4], names.mac[5]);
381                 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
382
383                 ieee_oui(dev, &names, test);
384         }
385
386         /* get PCI based path names, we compose only PCI based paths */
387         err = names_pci(dev, &names);
388         if (err < 0)
389                 goto out;
390
391         /* plain PCI device */
392         if (names.type == NET_PCI) {
393                 char str[IFNAMSIZ];
394
395                 if (names.pci_onboard[0])
396                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str))
397                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
398
399                 if (names.pci_onboard_label)
400                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str))
401                                 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
402
403                 if (names.pci_path[0])
404                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (int)sizeof(str))
405                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
406
407                 if (names.pci_slot[0])
408                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_slot) < (int)sizeof(str))
409                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
410                 goto out;
411         }
412
413         /* USB device */
414         err = names_usb(dev, &names);
415         if (err >= 0 && names.type == NET_USB) {
416                 char str[IFNAMSIZ];
417
418                 if (names.pci_path[0])
419                         if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (int)sizeof(str))
420                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
421
422                 if (names.pci_slot[0])
423                         if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str))
424                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
425         }
426 out:
427         return EXIT_SUCCESS;
428 }
429
430 const struct udev_builtin udev_builtin_net_id = {
431         .name = "net_id",
432         .cmd = builtin_net_id,
433         .help = "network device properties",
434 };