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