]>
Commit | Line | Data |
---|---|---|
a660c63c KS |
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 | ||
d23965a6 | 20 | /* |
ad37f393 | 21 | * Predictable network interface device names based on: |
472780d8 KS |
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 | * | |
ad37f393 | 27 | * Two character prefixes based on the type of interface: |
0035597a KS |
28 | * en -- ethernet |
29 | * wl -- wlan | |
30 | * ww -- wwan | |
d23965a6 | 31 | * |
ad37f393 | 32 | * Type of names: |
1328f66a KS |
33 | * o<index> -- on-board device index number |
34 | * s<slot>[f<function>][d<dev_id>] -- hotplug slot index number | |
35 | * x<MAC> -- MAC address | |
36 | * p<bus>s<slot>[f<function>][d<dev_id>] -- PCI geographical location | |
37 | * p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>] | |
38 | * -- USB port number chain | |
472780d8 | 39 | * |
ad37f393 | 40 | * All multi-function PCI devices will carry the [f<function>] number in the |
472780d8 | 41 | * device name, including the function 0 device. |
d23965a6 | 42 | * |
0d6ce923 KS |
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. | |
ad37f393 | 47 | * |
decd634e | 48 | * PCI ethernet card with firmware index "1": |
0035597a | 49 | * ID_NET_NAME_ONBOARD=eno1 |
f610d6de KS |
50 | * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1 |
51 | * | |
ad37f393 | 52 | * PCI ethernet card in hotplug slot with firmware index number: |
f610d6de KS |
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 | |
de892aea | 56 | * ID_NET_NAME_SLOT=ens1 |
f610d6de | 57 | * |
decd634e KS |
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 | * | |
f610d6de KS |
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 | |
d23965a6 KS |
80 | */ |
81 | ||
a660c63c KS |
82 | #include <stdio.h> |
83 | #include <stdlib.h> | |
84 | #include <stdarg.h> | |
85 | #include <unistd.h> | |
86 | #include <string.h> | |
87 | #include <errno.h> | |
02609440 | 88 | #include <net/if.h> |
de892aea | 89 | #include <linux/pci_regs.h> |
a660c63c KS |
90 | |
91 | #include "udev.h" | |
92 | ||
02609440 KS |
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 | ||
0035597a | 115 | /* retrieve on-board index number and label from firmware */ |
02609440 | 116 | static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) { |
d23965a6 | 117 | const char *index; |
0035597a | 118 | int idx; |
a660c63c | 119 | |
0035597a | 120 | /* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */ |
02609440 | 121 | index = udev_device_get_sysattr_value(names->pcidev, "acpi_index"); |
01d183dd KS |
122 | /* SMBIOS type 41 -- Onboard Devices Extended Information */ |
123 | if (!index) | |
02609440 | 124 | index = udev_device_get_sysattr_value(names->pcidev, "index"); |
0035597a KS |
125 | if (!index) |
126 | return -ENOENT; | |
127 | idx = strtoul(index, NULL, 0); | |
128 | if (idx <= 0) | |
129 | return -EINVAL; | |
02609440 | 130 | snprintf(names->pci_onboard, sizeof(names->pci_onboard), "o%d", idx); |
d23965a6 | 131 | |
02609440 | 132 | names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label"); |
0035597a KS |
133 | return 0; |
134 | } | |
d23965a6 | 135 | |
137661d8 | 136 | /* read the 256 bytes PCI configuration space to check the multi-function bit */ |
1328f66a | 137 | static bool is_pci_multifunction(struct udev_device *dev) { |
de892aea KS |
138 | char filename[256]; |
139 | FILE *f; | |
02609440 | 140 | char config[64]; |
1328f66a | 141 | bool multi = false; |
de892aea KS |
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 */ | |
1328f66a KS |
151 | if ((config[PCI_HEADER_TYPE] & 0x80) != 0) |
152 | multi = true; | |
de892aea KS |
153 | out: |
154 | fclose(f); | |
1328f66a | 155 | return multi; |
de892aea KS |
156 | } |
157 | ||
02609440 KS |
158 | static int dev_pci_slot(struct udev_device *dev, struct netnames *names) { |
159 | struct udev *udev = udev_device_get_udev(names->pcidev); | |
0035597a KS |
160 | unsigned int bus; |
161 | unsigned int slot; | |
162 | unsigned int func; | |
1328f66a KS |
163 | unsigned int dev_id = 0; |
164 | size_t l; | |
165 | char *s; | |
166 | const char *attr; | |
0035597a KS |
167 | struct udev_device *pci = NULL; |
168 | char slots[256]; | |
169 | DIR *dir; | |
170 | struct dirent *dent; | |
171 | char str[256]; | |
172 | int hotplug_slot = 0; | |
173 | int err = 0; | |
174 | ||
02609440 | 175 | if (sscanf(udev_device_get_sysname(names->pcidev), "0000:%x:%x.%d", &bus, &slot, &func) != 3) |
0035597a | 176 | return -ENOENT; |
1328f66a KS |
177 | |
178 | /* kernel provided multi-device index */ | |
179 | attr = udev_device_get_sysattr_value(dev, "dev_id"); | |
180 | if (attr) | |
181 | dev_id = strtol(attr, NULL, 16); | |
182 | ||
183 | /* compose a name based on the raw kernel's PCI bus, slot numbers */ | |
184 | s = names->pci_path; | |
185 | l = util_strpcpyf(&s, sizeof(names->pci_path), "p%ds%d", bus, slot); | |
186 | if (func > 0 || is_pci_multifunction(names->pcidev)) | |
187 | l = util_strpcpyf(&s, l, "f%d", func); | |
188 | if (dev_id > 0) | |
189 | l = util_strpcpyf(&s, l, "d%d", dev_id); | |
190 | if (l == 0) | |
191 | names->pci_path[0] = '\0'; | |
d23965a6 | 192 | |
0035597a KS |
193 | /* ACPI _SUN -- slot user number */ |
194 | pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci"); | |
195 | if (!pci) { | |
196 | err = -ENOENT; | |
197 | goto out; | |
198 | } | |
199 | snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci)); | |
200 | dir = opendir(slots); | |
201 | if (!dir) { | |
202 | err = -errno; | |
203 | goto out; | |
d23965a6 KS |
204 | } |
205 | ||
0035597a KS |
206 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { |
207 | int i; | |
208 | char *rest; | |
209 | char *address; | |
210 | ||
211 | if (dent->d_name[0] == '.') | |
212 | continue; | |
213 | i = strtol(dent->d_name, &rest, 10); | |
214 | if (rest[0] != '\0') | |
215 | continue; | |
216 | if (i < 1) | |
217 | continue; | |
218 | snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name); | |
219 | if (read_one_line_file(str, &address) >= 0) { | |
220 | /* match slot address with device by stripping the function */ | |
02609440 | 221 | if (strncmp(address, udev_device_get_sysname(names->pcidev), strlen(address)) == 0) |
0035597a KS |
222 | hotplug_slot = i; |
223 | free(address); | |
224 | } | |
225 | ||
226 | if (hotplug_slot > 0) | |
227 | break; | |
228 | } | |
229 | closedir(dir); | |
d23965a6 | 230 | |
0035597a | 231 | if (hotplug_slot > 0) { |
1328f66a KS |
232 | s = names->pci_slot; |
233 | l = util_strpcpyf(&s, sizeof(names->pci_slot), "s%d", hotplug_slot); | |
234 | if (func > 0 || is_pci_multifunction(names->pcidev)) | |
235 | l = util_strpcpyf(&s, l, "f%d", func); | |
236 | if (dev_id > 0) | |
237 | l = util_strpcpyf(&s, l, "d%d", dev_id); | |
238 | if (l == 0) | |
239 | names->pci_path[0] = '\0'; | |
d23965a6 | 240 | } |
0035597a KS |
241 | out: |
242 | udev_device_unref(pci); | |
243 | return err; | |
244 | } | |
245 | ||
02609440 | 246 | static int names_pci(struct udev_device *dev, struct netnames *names) { |
5b8180d3 | 247 | struct udev_device *parent; |
0035597a | 248 | |
5b8180d3 | 249 | parent = udev_device_get_parent(dev); |
02609440 KS |
250 | if (!parent) |
251 | return -ENOENT; | |
252 | /* check if our direct parent is a PCI device with no other bus in-between */ | |
253 | if (streq("pci", udev_device_get_subsystem(parent))) { | |
254 | names->type = NET_PCI; | |
255 | names->pcidev = parent; | |
256 | } else { | |
257 | names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL); | |
258 | if (!names->pcidev) | |
259 | return -ENOENT; | |
260 | } | |
261 | dev_pci_onboard(dev, names); | |
262 | dev_pci_slot(dev, names); | |
263 | return 0; | |
264 | } | |
265 | ||
266 | static int names_usb(struct udev_device *dev, struct netnames *names) { | |
267 | char name[256]; | |
268 | char *ports; | |
269 | char *config; | |
270 | char *interf; | |
271 | size_t l; | |
272 | char *s; | |
273 | ||
274 | names->usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface"); | |
275 | if (!names->usbdev) | |
0035597a KS |
276 | return -ENOENT; |
277 | ||
02609440 KS |
278 | /* get USB port number chain, configuration, interface */ |
279 | util_strscpy(name, sizeof(name), udev_device_get_sysname(names->usbdev)); | |
280 | s = strchr(name, '-'); | |
281 | if (!s) | |
282 | return -EINVAL; | |
283 | ports = s+1; | |
284 | ||
285 | s = strchr(ports, ':'); | |
286 | if (!s) | |
287 | return -EINVAL; | |
288 | s[0] = '\0'; | |
289 | config = s+1; | |
290 | ||
291 | s = strchr(config, '.'); | |
292 | if (!s) | |
293 | return -EINVAL; | |
294 | s[0] = '\0'; | |
295 | interf = s+1; | |
296 | ||
297 | /* prefix every port number in the chain with "u"*/ | |
298 | s = ports; | |
299 | while ((s = strchr(s, '.'))) | |
300 | s[0] = 'u'; | |
301 | s = names->usb_ports; | |
302 | l = util_strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL); | |
303 | ||
304 | /* append USB config number, suppress the common config == 1 */ | |
305 | if (!streq(config, "1")) | |
306 | l = util_strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL); | |
307 | ||
308 | /* append USB interface number, suppress the interface == 0 */ | |
309 | if (!streq(interf, "0")) | |
310 | l = util_strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL); | |
311 | if (l == 0) | |
312 | return -ENAMETOOLONG; | |
313 | ||
314 | names->type = NET_USB; | |
d23965a6 KS |
315 | return 0; |
316 | } | |
317 | ||
02609440 | 318 | static int names_mac(struct udev_device *dev, struct netnames *names) { |
d23965a6 KS |
319 | const char *s; |
320 | unsigned int i; | |
321 | unsigned int a1, a2, a3, a4, a5, a6; | |
d23965a6 KS |
322 | |
323 | /* check for NET_ADDR_PERM, skip random MAC addresses */ | |
324 | s = udev_device_get_sysattr_value(dev, "addr_assign_type"); | |
325 | if (!s) | |
326 | return EXIT_FAILURE; | |
327 | i = strtoul(s, NULL, 0); | |
328 | if (i != 0) | |
329 | return 0; | |
330 | ||
331 | s = udev_device_get_sysattr_value(dev, "address"); | |
332 | if (!s) | |
333 | return -ENOENT; | |
334 | if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6) | |
335 | return -EINVAL; | |
336 | ||
337 | /* skip empty MAC addresses */ | |
338 | if (a1 + a2 + a3 + a4 + a5 + a6 == 0) | |
a660c63c KS |
339 | return -EINVAL; |
340 | ||
02609440 KS |
341 | names->mac[0] = a1; |
342 | names->mac[1] = a2; | |
343 | names->mac[2] = a3; | |
344 | names->mac[3] = a4; | |
345 | names->mac[4] = a5; | |
346 | names->mac[5] = a6; | |
347 | names->mac_valid = true; | |
348 | return 0; | |
349 | } | |
d23965a6 | 350 | |
02609440 KS |
351 | /* IEEE Organizationally Unique Identifier vendor string */ |
352 | static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) { | |
971e7fb6 | 353 | char str[32]; |
02609440 | 354 | |
971e7fb6 | 355 | if (!names->mac_valid) |
02609440 KS |
356 | return -ENOENT; |
357 | /* skip commonly misused 00:00:00 (Xerox) prefix */ | |
358 | if (memcmp(names->mac, "\0\0\0", 3) == 0) | |
359 | return -EINVAL; | |
360 | snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X", | |
361 | names->mac[0], names->mac[1], names->mac[2], | |
362 | names->mac[3], names->mac[4], names->mac[5]); | |
363 | udev_builtin_hwdb_lookup(dev, str, test); | |
364 | return 0; | |
a660c63c KS |
365 | } |
366 | ||
367 | static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) { | |
d23965a6 KS |
368 | const char *s; |
369 | unsigned int i; | |
370 | const char *devtype; | |
371 | const char *prefix = "en"; | |
02609440 KS |
372 | struct netnames names; |
373 | int err; | |
d23965a6 KS |
374 | |
375 | /* handle only ARPHRD_ETHER devices */ | |
376 | s = udev_device_get_sysattr_value(dev, "type"); | |
377 | if (!s) | |
378 | return EXIT_FAILURE; | |
379 | i = strtoul(s, NULL, 0); | |
380 | if (i != 1) | |
381 | return 0; | |
382 | ||
383 | devtype = udev_device_get_devtype(dev); | |
384 | if (devtype) { | |
385 | if (streq("wlan", devtype)) | |
386 | prefix = "wl"; | |
387 | else if (streq("wwan", devtype)) | |
388 | prefix = "ww"; | |
389 | } | |
390 | ||
02609440 KS |
391 | zero(names); |
392 | err = names_mac(dev, &names); | |
393 | if (err >= 0 && names.mac_valid) { | |
394 | char str[IFNAMSIZ]; | |
395 | ||
396 | snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix, | |
397 | names.mac[0], names.mac[1], names.mac[2], | |
398 | names.mac[3], names.mac[4], names.mac[5]); | |
399 | udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str); | |
400 | ||
401 | ieee_oui(dev, &names, test); | |
402 | } | |
403 | ||
404 | /* get PCI based path names, we compose only PCI based paths */ | |
405 | err = names_pci(dev, &names); | |
406 | if (err < 0) | |
407 | goto out; | |
408 | ||
409 | /* plain PCI device */ | |
410 | if (names.type == NET_PCI) { | |
411 | char str[IFNAMSIZ]; | |
412 | ||
413 | if (names.pci_onboard[0]) | |
414 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str)) | |
415 | udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str); | |
416 | ||
417 | if (names.pci_onboard_label) | |
418 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str)) | |
419 | udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str); | |
420 | ||
421 | if (names.pci_path[0]) | |
422 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (int)sizeof(str)) | |
423 | udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); | |
424 | ||
425 | if (names.pci_slot[0]) | |
426 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_slot) < (int)sizeof(str)) | |
427 | udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); | |
428 | goto out; | |
429 | } | |
430 | ||
431 | /* USB device */ | |
432 | err = names_usb(dev, &names); | |
433 | if (err >= 0 && names.type == NET_USB) { | |
434 | char str[IFNAMSIZ]; | |
435 | ||
436 | if (names.pci_path[0]) | |
437 | if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (int)sizeof(str)) | |
438 | udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); | |
439 | ||
440 | if (names.pci_slot[0]) | |
441 | if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str)) | |
442 | udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); | |
443 | } | |
444 | out: | |
a660c63c KS |
445 | return EXIT_SUCCESS; |
446 | } | |
447 | ||
448 | const struct udev_builtin udev_builtin_net_id = { | |
449 | .name = "net_id", | |
450 | .cmd = builtin_net_id, | |
451 | .help = "network device properties", | |
452 | }; |