chiark / gitweb /
sd-network: expose both admin and operational state directly
[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  *   sl -- serial line IP (slip)
32  *   wl -- wlan
33  *   ww -- wwan
34  *
35  * Type of names:
36  *   b<number>                             -- BCMA bus core number
37  *   ccw<name>                             -- CCW bus group name
38  *   o<index>                              -- on-board device index number
39  *   s<slot>[f<function>][d<dev_id>]       -- hotplug slot index number
40  *   x<MAC>                                -- MAC address
41  *   [P<domain>]p<bus>s<slot>[f<function>][d<dev_id>]
42  *                                         -- PCI geographical location
43  *   [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
44  *                                         -- USB port number chain
45  *
46  * All multi-function PCI devices will carry the [f<function>] number in the
47  * device name, including the function 0 device.
48  *
49  * When using PCI geography, The PCI domain is only prepended when it is not 0.
50  *
51  * For USB devices the full chain of port numbers of hubs is composed. If the
52  * name gets longer than the maximum number of 15 characters, the name is not
53  * exported.
54  * The usual USB configuration == 1 and interface == 0 values are suppressed.
55  *
56  * PCI ethernet card with firmware index "1":
57  *   ID_NET_NAME_ONBOARD=eno1
58  *   ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
59  *
60  * PCI ethernet card in hotplug slot with firmware index number:
61  *   /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
62  *   ID_NET_NAME_MAC=enx000000000466
63  *   ID_NET_NAME_PATH=enp5s0
64  *   ID_NET_NAME_SLOT=ens1
65  *
66  * PCI ethernet multi-function card with 2 ports:
67  *   /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0/net/enp2s0f0
68  *   ID_NET_NAME_MAC=enx78e7d1ea46da
69  *   ID_NET_NAME_PATH=enp2s0f0
70  *   /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.1/net/enp2s0f1
71  *   ID_NET_NAME_MAC=enx78e7d1ea46dc
72  *   ID_NET_NAME_PATH=enp2s0f1
73  *
74  * PCI wlan card:
75  *   /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
76  *   ID_NET_NAME_MAC=wlx0024d7e31130
77  *   ID_NET_NAME_PATH=wlp3s0
78  *
79  * USB built-in 3G modem:
80  *   /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
81  *   ID_NET_NAME_MAC=wwx028037ec0200
82  *   ID_NET_NAME_PATH=wwp0s29u1u4i6
83  *
84  * USB Android phone:
85  *   /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
86  *   ID_NET_NAME_MAC=enxd626b3450fb5
87  *   ID_NET_NAME_PATH=enp0s29u1u2
88  */
89
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <stdarg.h>
93 #include <unistd.h>
94 #include <string.h>
95 #include <errno.h>
96 #include <net/if.h>
97 #include <net/if_arp.h>
98 #include <linux/pci_regs.h>
99
100 #include "udev.h"
101 #include "fileio.h"
102
103 enum netname_type{
104         NET_UNDEF,
105         NET_PCI,
106         NET_USB,
107         NET_BCMA,
108         NET_VIRTIO,
109         NET_CCWGROUP,
110 };
111
112 struct netnames {
113         enum netname_type type;
114
115         uint8_t mac[6];
116         bool mac_valid;
117
118         struct udev_device *pcidev;
119         char pci_slot[IFNAMSIZ];
120         char pci_path[IFNAMSIZ];
121         char pci_onboard[IFNAMSIZ];
122         const char *pci_onboard_label;
123
124         char usb_ports[IFNAMSIZ];
125         char bcma_core[IFNAMSIZ];
126         char ccw_group[IFNAMSIZ];
127 };
128
129 /* retrieve on-board index number and label from firmware */
130 static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
131         const char *index;
132         int idx;
133
134         /* ACPI _DSM  -- device specific method for naming a PCI or PCI Express device */
135         index = udev_device_get_sysattr_value(names->pcidev, "acpi_index");
136         /* SMBIOS type 41 -- Onboard Devices Extended Information */
137         if (!index)
138                 index = udev_device_get_sysattr_value(names->pcidev, "index");
139         if (!index)
140                 return -ENOENT;
141         idx = strtoul(index, NULL, 0);
142         if (idx <= 0)
143                 return -EINVAL;
144         snprintf(names->pci_onboard, sizeof(names->pci_onboard), "o%d", idx);
145
146         names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label");
147         return 0;
148 }
149
150 /* read the 256 bytes PCI configuration space to check the multi-function bit */
151 static bool is_pci_multifunction(struct udev_device *dev) {
152         char filename[256];
153         FILE *f = NULL;
154         char config[64];
155         bool multi = false;
156
157         snprintf(filename, sizeof(filename), "%s/config", udev_device_get_syspath(dev));
158         f = fopen(filename, "re");
159         if (!f)
160                 goto out;
161         if (fread(&config, sizeof(config), 1, f) != 1)
162                 goto out;
163
164         /* bit 0-6 header type, bit 7 multi/single function device */
165         if ((config[PCI_HEADER_TYPE] & 0x80) != 0)
166                 multi = true;
167 out:
168         if (f)
169                 fclose(f);
170         return multi;
171 }
172
173 static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
174         struct udev *udev = udev_device_get_udev(names->pcidev);
175         unsigned domain, bus, slot, func, dev_id = 0;
176         size_t l;
177         char *s;
178         const char *attr;
179         struct udev_device *pci = NULL;
180         char slots[256], str[256];
181         _cleanup_closedir_ DIR *dir = NULL;
182         struct dirent *dent;
183         int hotplug_slot = 0, err = 0;
184
185         if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%u", &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
243         if (hotplug_slot > 0) {
244                 s = names->pci_slot;
245                 l = sizeof(names->pci_slot);
246                 if (domain > 0)
247                         l = strpcpyf(&s, l, "P%d", domain);
248                 l = strpcpyf(&s, l, "s%d", hotplug_slot);
249                 if (func > 0 || is_pci_multifunction(names->pcidev))
250                         l = strpcpyf(&s, l, "f%d", func);
251                 if (dev_id > 0)
252                         l = strpcpyf(&s, l, "d%d", dev_id);
253                 if (l == 0)
254                         names->pci_path[0] = '\0';
255         }
256 out:
257         udev_device_unref(pci);
258         return err;
259 }
260
261 static int names_pci(struct udev_device *dev, struct netnames *names) {
262         struct udev_device *parent;
263
264         parent = udev_device_get_parent(dev);
265         if (!parent)
266                 return -ENOENT;
267         /* check if our direct parent is a PCI device with no other bus in-between */
268         if (streq_ptr("pci", udev_device_get_subsystem(parent))) {
269                 names->type = NET_PCI;
270                 names->pcidev = parent;
271         } else {
272                 names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
273                 if (!names->pcidev)
274                         return -ENOENT;
275         }
276         dev_pci_onboard(dev, names);
277         dev_pci_slot(dev, names);
278         return 0;
279 }
280
281 static int names_usb(struct udev_device *dev, struct netnames *names) {
282         struct udev_device *usbdev;
283         char name[256];
284         char *ports;
285         char *config;
286         char *interf;
287         size_t l;
288         char *s;
289
290         usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
291         if (!usbdev)
292                 return -ENOENT;
293
294         /* get USB port number chain, configuration, interface */
295         strscpy(name, sizeof(name), udev_device_get_sysname(usbdev));
296         s = strchr(name, '-');
297         if (!s)
298                 return -EINVAL;
299         ports = s+1;
300
301         s = strchr(ports, ':');
302         if (!s)
303                 return -EINVAL;
304         s[0] = '\0';
305         config = s+1;
306
307         s = strchr(config, '.');
308         if (!s)
309                 return -EINVAL;
310         s[0] = '\0';
311         interf = s+1;
312
313         /* prefix every port number in the chain with "u"*/
314         s = ports;
315         while ((s = strchr(s, '.')))
316                 s[0] = 'u';
317         s = names->usb_ports;
318         l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
319
320         /* append USB config number, suppress the common config == 1 */
321         if (!streq(config, "1"))
322                 l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
323
324         /* append USB interface number, suppress the interface == 0 */
325         if (!streq(interf, "0"))
326                 l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
327         if (l == 0)
328                 return -ENAMETOOLONG;
329
330         names->type = NET_USB;
331         return 0;
332 }
333
334 static int names_bcma(struct udev_device *dev, struct netnames *names) {
335         struct udev_device *bcmadev;
336         unsigned int core;
337
338         bcmadev = udev_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL);
339         if (!bcmadev)
340                 return -ENOENT;
341
342         /* bus num:core num */
343         if (sscanf(udev_device_get_sysname(bcmadev), "bcma%*u:%u", &core) != 1)
344                 return -EINVAL;
345         /* suppress the common core == 0 */
346         if (core > 0)
347                 snprintf(names->bcma_core, sizeof(names->bcma_core), "b%u", core);
348
349         names->type = NET_BCMA;
350         return 0;
351 }
352
353 static int names_ccw(struct  udev_device *dev, struct netnames *names) {
354         struct udev_device *cdev;
355         const char *bus_id;
356         size_t bus_id_len;
357         int rc;
358
359         /* Retrieve the associated CCW device */
360         cdev = udev_device_get_parent(dev);
361         if (!cdev)
362                 return -ENOENT;
363
364         /* Network devices are always grouped CCW devices */
365         if (!streq_ptr("ccwgroup", udev_device_get_subsystem(cdev)))
366                 return -ENOENT;
367
368         /* Retrieve bus-ID of the grouped CCW device.  The bus-ID uniquely
369          * identifies the network device on the Linux on System z channel
370          * subsystem.  Note that the bus-ID contains lowercase characters.
371          */
372         bus_id = udev_device_get_sysname(cdev);
373         if (!bus_id)
374                 return -ENOENT;
375
376         /* Check the length of the bus-ID.  Rely on that the kernel provides
377          * a correct bus-ID; alternatively, improve this check and parse and
378          * verify each bus-ID part...
379          */
380         bus_id_len = strlen(bus_id);
381         if (!bus_id_len || bus_id_len < 8 || bus_id_len > 9)
382                 return -EINVAL;
383
384         /* Store the CCW bus-ID for use as network device name */
385         rc = snprintf(names->ccw_group, sizeof(names->ccw_group), "ccw%s", bus_id);
386         if (rc >= 0 && rc < (int)sizeof(names->ccw_group))
387                 names->type = NET_CCWGROUP;
388         return 0;
389 }
390
391 static int names_mac(struct udev_device *dev, struct netnames *names) {
392         const char *s;
393         unsigned int i;
394         unsigned int a1, a2, a3, a4, a5, a6;
395
396         /* check for NET_ADDR_PERM, skip random MAC addresses */
397         s = udev_device_get_sysattr_value(dev, "addr_assign_type");
398         if (!s)
399                 return EXIT_FAILURE;
400         i = strtoul(s, NULL, 0);
401         if (i != 0)
402                 return 0;
403
404         s = udev_device_get_sysattr_value(dev, "address");
405         if (!s)
406                 return -ENOENT;
407         if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
408                 return -EINVAL;
409
410         /* skip empty MAC addresses */
411         if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
412                 return -EINVAL;
413
414         names->mac[0] = a1;
415         names->mac[1] = a2;
416         names->mac[2] = a3;
417         names->mac[3] = a4;
418         names->mac[4] = a5;
419         names->mac[5] = a6;
420         names->mac_valid = true;
421         return 0;
422 }
423
424 /* IEEE Organizationally Unique Identifier vendor string */
425 static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) {
426         char str[32];
427
428         if (!names->mac_valid)
429                 return -ENOENT;
430         /* skip commonly misused 00:00:00 (Xerox) prefix */
431         if (memcmp(names->mac, "\0\0\0", 3) == 0)
432                 return -EINVAL;
433         snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X",
434                  names->mac[0], names->mac[1], names->mac[2],
435                  names->mac[3], names->mac[4], names->mac[5]);
436         udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
437         return 0;
438 }
439
440 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
441         const char *s;
442         const char *p;
443         unsigned int i;
444         const char *devtype;
445         const char *prefix = "en";
446         struct netnames names = {};
447         int err;
448
449         /* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */
450         s = udev_device_get_sysattr_value(dev, "type");
451         if (!s)
452                 return EXIT_FAILURE;
453         i = strtoul(s, NULL, 0);
454         switch (i) {
455         case ARPHRD_ETHER:
456                 prefix = "en";
457                 break;
458         case ARPHRD_SLIP:
459                 prefix = "sl";
460                 break;
461         default:
462                 return 0;
463         }
464
465         /* skip stacked devices, like VLANs, ... */
466         s = udev_device_get_sysattr_value(dev, "ifindex");
467         if (!s)
468                 return EXIT_FAILURE;
469         p = udev_device_get_sysattr_value(dev, "iflink");
470         if (!p)
471                 return EXIT_FAILURE;
472         if (!streq(s, p))
473                 return 0;
474
475         devtype = udev_device_get_devtype(dev);
476         if (devtype) {
477                 if (streq("wlan", devtype))
478                         prefix = "wl";
479                 else if (streq("wwan", devtype))
480                         prefix = "ww";
481         }
482
483         err = names_mac(dev, &names);
484         if (err >= 0 && names.mac_valid) {
485                 char str[IFNAMSIZ];
486
487                 snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix,
488                          names.mac[0], names.mac[1], names.mac[2],
489                          names.mac[3], names.mac[4], names.mac[5]);
490                 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
491
492                 ieee_oui(dev, &names, test);
493         }
494
495         /* get path names for Linux on System z network devices */
496         err = names_ccw(dev, &names);
497         if (err >= 0 && names.type == NET_CCWGROUP) {
498                 char str[IFNAMSIZ];
499
500                 if (snprintf(str, sizeof(str), "%s%s", prefix, names.ccw_group) < (int)sizeof(str))
501                         udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
502                 goto out;
503         }
504
505         /* get PCI based path names, we compose only PCI based paths */
506         err = names_pci(dev, &names);
507         if (err < 0)
508                 goto out;
509
510         /* plain PCI device */
511         if (names.type == NET_PCI) {
512                 char str[IFNAMSIZ];
513
514                 if (names.pci_onboard[0])
515                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str))
516                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
517
518                 if (names.pci_onboard_label)
519                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str))
520                                 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
521
522                 if (names.pci_path[0])
523                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (int)sizeof(str))
524                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
525
526                 if (names.pci_slot[0])
527                         if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_slot) < (int)sizeof(str))
528                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
529                 goto out;
530         }
531
532         /* USB device */
533         err = names_usb(dev, &names);
534         if (err >= 0 && names.type == NET_USB) {
535                 char str[IFNAMSIZ];
536
537                 if (names.pci_path[0])
538                         if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (int)sizeof(str))
539                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
540
541                 if (names.pci_slot[0])
542                         if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str))
543                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
544                 goto out;
545         }
546
547         /* Broadcom bus */
548         err = names_bcma(dev, &names);
549         if (err >= 0 && names.type == NET_BCMA) {
550                 char str[IFNAMSIZ];
551
552                 if (names.pci_path[0])
553                         if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.bcma_core) < (int)sizeof(str))
554                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
555
556                 if (names.pci_slot[0])
557                         if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.bcma_core) < (int)sizeof(str))
558                                 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
559                 goto out;
560         }
561 out:
562         return EXIT_SUCCESS;
563 }
564
565 const struct udev_builtin udev_builtin_net_id = {
566         .name = "net_id",
567         .cmd = builtin_net_id,
568         .help = "network device properties",
569 };