chiark / gitweb /
bc6ca212db1441c228c9f56763bdb087e23158d5
[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  * prefixes:
22  *   en -- ethernet
23  *   wl -- wlan
24  *   ww -- wwan
25  *
26  * types:
27  *   o<index>                   -- on-board device index
28  *   s<slot>[f<function>]       -- hotplug slot number
29  *   x<MAC>                     -- MAC address
30  *   p<bus>s<slot>[f<function>] -- PCI/physical location
31  *
32  * example:
33  *   ID_NET_NAME_ONBOARD=eno1
34  *   ID_NET_NAME_SLOT=ens1
35  *   ID_NET_NAME_SLOT=ens2f0
36  *   ID_NET_NAME_SLOT=ens2f1
37  *   ID_NET_NAME_MAC=enxf0def180d479
38  *   ID_NET_NAME_PATH=enp0s25
39  *   ID_NET_NAME_PATH=enp19s3f0
40  *   ID_NET_NAME_PATH=enp19s3f1
41  */
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <linux/pci_regs.h>
50
51 #include "udev.h"
52
53 /* retrieve on-board index number and label from firmware */
54 static int dev_pci_onboard(struct udev_device *dev, struct udev_device *parent, const char *prefix, bool test) {
55         const char *index;
56         int idx;
57         const char *label;
58         char s[16];
59         int err;
60
61         /* ACPI _DSM  -- device specific method for naming a PCI or PCI Express device */
62         index = udev_device_get_sysattr_value(parent, "acpi_index");
63         /* SMBIOS type 41 -- Onboard Devices Extended Information */
64         if (!index)
65                 index = udev_device_get_sysattr_value(parent, "index");
66         if (!index)
67                 return -ENOENT;
68         idx = strtoul(index, NULL, 0);
69         if (idx <= 0)
70                 return -EINVAL;
71         snprintf(s, sizeof(s), "%so%d", prefix, idx);
72         err = udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", s);
73         if (err < 0)
74                 return err;
75
76         label = udev_device_get_sysattr_value(parent, "label");
77         if (label) {
78                 err = udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", label);
79                 if (err < 0)
80                         return err;
81         }
82         return 0;
83 }
84
85 static bool is_pci_singlefunction(struct udev_device *dev) {
86         char filename[256];
87         FILE *f;
88         char config[256];
89         bool single = false;
90
91         snprintf(filename, sizeof(filename), "%s/config", udev_device_get_syspath(dev));
92         f = fopen(filename, "re");
93         if (!f)
94                 goto out;
95         if (fread(&config, sizeof(config), 1, f) != 1)
96                 goto out;
97
98         /* bit 0-6 header type, bit 7 multi/single function device */
99         if ((config[PCI_HEADER_TYPE] & 0x80) == 0)
100                 single = true;
101 out:
102         fclose(f);
103         return single;
104 }
105
106 static int dev_pci_slot(struct udev_device *dev, struct udev_device *parent, const char *prefix, bool test) {
107         struct udev *udev = udev_device_get_udev(dev);
108         unsigned int bus;
109         unsigned int slot;
110         unsigned int func;
111         struct udev_device *pci = NULL;
112         char slots[256];
113         DIR *dir;
114         struct dirent *dent;
115         char str[256];
116         int hotplug_slot = 0;
117         int err = 0;
118
119         /* compose a name based on the raw kernel's PCI bus, slot numbers */
120         if (sscanf(udev_device_get_sysname(parent), "0000:%x:%x.%d", &bus, &slot, &func) != 3)
121                 return -ENOENT;
122         if (func == 0 && is_pci_singlefunction(parent))
123                 snprintf(str, sizeof(str), "%sp%ds%d", prefix, bus, slot);
124         else
125                 snprintf(str, sizeof(str), "%sp%ds%df%d", prefix, bus, slot, func);
126         err = udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
127         if (err < 0)
128                 return err;
129
130         /* ACPI _SUN  -- slot user number */
131         pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
132         if (!pci) {
133                 err = -ENOENT;
134                 goto out;
135         }
136         snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
137         dir = opendir(slots);
138         if (!dir) {
139                 err = -errno;
140                 goto out;
141         }
142
143         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
144                 int i;
145                 char *rest;
146                 char *address;
147
148                 if (dent->d_name[0] == '.')
149                         continue;
150                 i = strtol(dent->d_name, &rest, 10);
151                 if (rest[0] != '\0')
152                         continue;
153                 if (i < 1)
154                         continue;
155                 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
156                 if (read_one_line_file(str, &address) >= 0) {
157                         /* match slot address with device by stripping the function */
158                         if (strncmp(address, udev_device_get_sysname(parent), strlen(address)) == 0)
159                                 hotplug_slot = i;
160                         free(address);
161                 }
162
163                 if (hotplug_slot > 0)
164                         break;
165         }
166         closedir(dir);
167
168         if (hotplug_slot > 0) {
169                 if (func == 0 && is_pci_singlefunction(parent))
170                         snprintf(str, sizeof(str), "%ss%d", prefix, hotplug_slot);
171                 else
172                         snprintf(str, sizeof(str), "%ss%df%d", prefix, hotplug_slot, func);
173                 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
174         }
175 out:
176         udev_device_unref(pci);
177         return err;
178 }
179
180 static int dev_pci(struct udev_device *dev, const char *prefix, bool test) {
181         struct udev_device *parent;
182
183         /* skip other buses than direct PCI parents */
184         parent = udev_device_get_parent(dev);
185         if (!parent || !streq("pci", udev_device_get_subsystem(parent)))
186                 return -ENOENT;
187
188         dev_pci_onboard(dev, parent, prefix, test);
189         dev_pci_slot(dev, parent, prefix, test);
190         return 0;
191 }
192
193 static int dev_mac(struct udev_device *dev, const char *prefix, bool test) {
194         const char *s;
195         unsigned int i;
196         unsigned int a1, a2, a3, a4, a5, a6;
197         char str[16];
198
199         /* check for NET_ADDR_PERM, skip random MAC addresses */
200         s = udev_device_get_sysattr_value(dev, "addr_assign_type");
201         if (!s)
202                 return EXIT_FAILURE;
203         i = strtoul(s, NULL, 0);
204         if (i != 0)
205                 return 0;
206
207         s = udev_device_get_sysattr_value(dev, "address");
208         if (!s)
209                 return -ENOENT;
210         if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
211                 return -EINVAL;
212
213         /* skip empty MAC addresses */
214         if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
215                 return -EINVAL;
216
217         /* add IEEE Organizationally Unique Identifier */
218         snprintf(str, sizeof(str), "OUI:%X%X%X", a1, a2, a3);
219         udev_builtin_hwdb_lookup(dev, str, test);
220
221         snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix, a1, a2, a3, a4, a5, a6);
222         return udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
223 }
224
225 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
226         const char *s;
227         unsigned int i;
228         const char *devtype;
229         const char *prefix = "en";
230
231         /* handle only ARPHRD_ETHER devices */
232         s = udev_device_get_sysattr_value(dev, "type");
233         if (!s)
234                 return EXIT_FAILURE;
235         i = strtoul(s, NULL, 0);
236         if (i != 1)
237                 return 0;
238
239         devtype = udev_device_get_devtype(dev);
240         if (devtype) {
241                 if (streq("wlan", devtype))
242                         prefix = "wl";
243                 else if (streq("wwan", devtype))
244                         prefix = "ww";
245         }
246
247         dev_pci(dev, prefix, test);
248         dev_mac(dev, prefix, test);
249         return EXIT_SUCCESS;
250 }
251
252 const struct udev_builtin udev_builtin_net_id = {
253         .name = "net_id",
254         .cmd = builtin_net_id,
255         .help = "network device properties",
256 };