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>[d<dev_port>] -- on-board device index number
39 * s<slot>[f<function>][d<dev_port>] -- hotplug slot index number
40 * x<MAC> -- MAC address
41 * [P<domain>]p<bus>s<slot>[f<function>][d<dev_port>]
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 <fcntl.h>
95 #include <string.h>
96 #include <errno.h>
97 #include <net/if.h>
98 #include <net/if_arp.h>
99 #include <linux/pci_regs.h>
100
101 #include "udev.h"
102 #include "fileio.h"
103
104 enum netname_type{
105 NET_UNDEF,
106 NET_PCI,
107 NET_USB,
108 NET_BCMA,
109 NET_VIRTIO,
110 NET_CCWGROUP,
111 };
112
113 struct netnames {
114 enum netname_type type;
115
116 uint8_t mac[6];
117 bool mac_valid;
118
119 struct udev_device *pcidev;
120 char pci_slot[IFNAMSIZ];
121 char pci_path[IFNAMSIZ];
122 char pci_onboard[IFNAMSIZ];
123 const char *pci_onboard_label;
124
125 char usb_ports[IFNAMSIZ];
126 char bcma_core[IFNAMSIZ];
127 char ccw_group[IFNAMSIZ];
128 };
129
130 /* retrieve on-board index number and label from firmware */
dev_pci_onboard(struct udev_device * dev,struct netnames * names)131 static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
132 unsigned dev_port = 0;
133 size_t l;
134 char *s;
135 const char *attr;
136 int idx;
137
138 /* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */
139 attr = udev_device_get_sysattr_value(names->pcidev, "acpi_index");
140 /* SMBIOS type 41 -- Onboard Devices Extended Information */
141 if (!attr)
142 attr = udev_device_get_sysattr_value(names->pcidev, "index");
143 if (!attr)
144 return -ENOENT;
145
146 idx = strtoul(attr, NULL, 0);
147 if (idx <= 0)
148 return -EINVAL;
149
150 /* kernel provided port index for multiple ports on a single PCI function */
151 attr = udev_device_get_sysattr_value(dev, "dev_port");
152 if (attr)
153 dev_port = strtol(attr, NULL, 10);
154
155 s = names->pci_onboard;
156 l = sizeof(names->pci_onboard);
157 l = strpcpyf(&s, l, "o%d", idx);
158 if (dev_port > 0)
159 l = strpcpyf(&s, l, "d%d", dev_port);
160 if (l == 0)
161 names->pci_onboard[0] = '\0';
162
163 names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label");
164
165 return 0;
166 }
167
168 /* read the 256 bytes PCI configuration space to check the multi-function bit */
is_pci_multifunction(struct udev_device * dev)169 static bool is_pci_multifunction(struct udev_device *dev) {
170 _cleanup_close_ int fd = -1;
171 const char *filename;
172 uint8_t config[64];
173
174 filename = strjoina(udev_device_get_syspath(dev), "/config");
175 fd = open(filename, O_RDONLY | O_CLOEXEC);
176 if (fd < 0)
177 return false;
178 if (read(fd, &config, sizeof(config)) != sizeof(config))
179 return false;
180
181 /* bit 0-6 header type, bit 7 multi/single function device */
182 if ((config[PCI_HEADER_TYPE] & 0x80) != 0)
183 return true;
184
185 return false;
186 }
187
dev_pci_slot(struct udev_device * dev,struct netnames * names)188 static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
189 struct udev *udev = udev_device_get_udev(names->pcidev);
190 unsigned domain, bus, slot, func, dev_port = 0;
191 size_t l;
192 char *s;
193 const char *attr;
194 struct udev_device *pci = NULL;
195 char slots[256], str[256];
196 _cleanup_closedir_ DIR *dir = NULL;
197 struct dirent *dent;
198 int hotplug_slot = 0, err = 0;
199
200 if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
201 return -ENOENT;
202
203 /* kernel provided port index for multiple ports on a single PCI function */
204 attr = udev_device_get_sysattr_value(dev, "dev_port");
205 if (attr)
206 dev_port = strtol(attr, NULL, 10);
207
208 /* compose a name based on the raw kernel's PCI bus, slot numbers */
209 s = names->pci_path;
210 l = sizeof(names->pci_path);
211 if (domain > 0)
212 l = strpcpyf(&s, l, "P%u", domain);
213 l = strpcpyf(&s, l, "p%us%u", bus, slot);
214 if (func > 0 || is_pci_multifunction(names->pcidev))
215 l = strpcpyf(&s, l, "f%u", func);
216 if (dev_port > 0)
217 l = strpcpyf(&s, l, "d%u", dev_port);
218 if (l == 0)
219 names->pci_path[0] = '\0';
220
221 /* ACPI _SUN -- slot user number */
222 pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
223 if (!pci) {
224 err = -ENOENT;
225 goto out;
226 }
227 snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
228 dir = opendir(slots);
229 if (!dir) {
230 err = -errno;
231 goto out;
232 }
233
234 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
235 int i;
236 char *rest;
237 char *address;
238
239 if (dent->d_name[0] == '.')
240 continue;
241 i = strtol(dent->d_name, &rest, 10);
242 if (rest[0] != '\0')
243 continue;
244 if (i < 1)
245 continue;
246 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
247 if (read_one_line_file(str, &address) >= 0) {
248 /* match slot address with device by stripping the function */
249 if (strneq(address, udev_device_get_sysname(names->pcidev), strlen(address)))
250 hotplug_slot = i;
251 free(address);
252 }
253
254 if (hotplug_slot > 0)
255 break;
256 }
257
258 if (hotplug_slot > 0) {
259 s = names->pci_slot;
260 l = sizeof(names->pci_slot);
261 if (domain > 0)
262 l = strpcpyf(&s, l, "P%d", domain);
263 l = strpcpyf(&s, l, "s%d", hotplug_slot);
264 if (func > 0 || is_pci_multifunction(names->pcidev))
265 l = strpcpyf(&s, l, "f%d", func);
266 if (dev_port > 0)
267 l = strpcpyf(&s, l, "d%d", dev_port);
268 if (l == 0)
269 names->pci_slot[0] = '\0';
270 }
271 out:
272 udev_device_unref(pci);
273 return err;
274 }
275
names_pci(struct udev_device * dev,struct netnames * names)276 static int names_pci(struct udev_device *dev, struct netnames *names) {
277 struct udev_device *parent;
278
279 parent = udev_device_get_parent(dev);
280 if (!parent)
281 return -ENOENT;
282 /* check if our direct parent is a PCI device with no other bus in-between */
283 if (streq_ptr("pci", udev_device_get_subsystem(parent))) {
284 names->type = NET_PCI;
285 names->pcidev = parent;
286 } else {
287 names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
288 if (!names->pcidev)
289 return -ENOENT;
290 }
291 dev_pci_onboard(dev, names);
292 dev_pci_slot(dev, names);
293 return 0;
294 }
295
names_usb(struct udev_device * dev,struct netnames * names)296 static int names_usb(struct udev_device *dev, struct netnames *names) {
297 struct udev_device *usbdev;
298 char name[256];
299 char *ports;
300 char *config;
301 char *interf;
302 size_t l;
303 char *s;
304
305 usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
306 if (!usbdev)
307 return -ENOENT;
308
309 /* get USB port number chain, configuration, interface */
310 strscpy(name, sizeof(name), udev_device_get_sysname(usbdev));
311 s = strchr(name, '-');
312 if (!s)
313 return -EINVAL;
314 ports = s+1;
315
316 s = strchr(ports, ':');
317 if (!s)
318 return -EINVAL;
319 s[0] = '\0';
320 config = s+1;
321
322 s = strchr(config, '.');
323 if (!s)
324 return -EINVAL;
325 s[0] = '\0';
326 interf = s+1;
327
328 /* prefix every port number in the chain with "u" */
329 s = ports;
330 while ((s = strchr(s, '.')))
331 s[0] = 'u';
332 s = names->usb_ports;
333 l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
334
335 /* append USB config number, suppress the common config == 1 */
336 if (!streq(config, "1"))
337 l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
338
339 /* append USB interface number, suppress the interface == 0 */
340 if (!streq(interf, "0"))
341 l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
342 if (l == 0)
343 return -ENAMETOOLONG;
344
345 names->type = NET_USB;
346 return 0;
347 }
348
names_bcma(struct udev_device * dev,struct netnames * names)349 static int names_bcma(struct udev_device *dev, struct netnames *names) {
350 struct udev_device *bcmadev;
351 unsigned int core;
352
353 bcmadev = udev_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL);
354 if (!bcmadev)
355 return -ENOENT;
356
357 /* bus num:core num */
358 if (sscanf(udev_device_get_sysname(bcmadev), "bcma%*u:%u", &core) != 1)
359 return -EINVAL;
360 /* suppress the common core == 0 */
361 if (core > 0)
362 snprintf(names->bcma_core, sizeof(names->bcma_core), "b%u", core);
363
364 names->type = NET_BCMA;
365 return 0;
366 }
367
names_ccw(struct udev_device * dev,struct netnames * names)368 static int names_ccw(struct udev_device *dev, struct netnames *names) {
369 struct udev_device *cdev;
370 const char *bus_id;
371 size_t bus_id_len;
372 int rc;
373
374 /* Retrieve the associated CCW device */
375 cdev = udev_device_get_parent(dev);
376 if (!cdev)
377 return -ENOENT;
378
379 /* Network devices are always grouped CCW devices */
380 if (!streq_ptr("ccwgroup", udev_device_get_subsystem(cdev)))
381 return -ENOENT;
382
383 /* Retrieve bus-ID of the grouped CCW device. The bus-ID uniquely
384 * identifies the network device on the Linux on System z channel
385 * subsystem. Note that the bus-ID contains lowercase characters.
386 */
387 bus_id = udev_device_get_sysname(cdev);
388 if (!bus_id)
389 return -ENOENT;
390
391 /* Check the length of the bus-ID. Rely on that the kernel provides
392 * a correct bus-ID; alternatively, improve this check and parse and
393 * verify each bus-ID part...
394 */
395 bus_id_len = strlen(bus_id);
396 if (!bus_id_len || bus_id_len < 8 || bus_id_len > 9)
397 return -EINVAL;
398
399 /* Store the CCW bus-ID for use as network device name */
400 rc = snprintf(names->ccw_group, sizeof(names->ccw_group), "ccw%s", bus_id);
401 if (rc >= 0 && rc < (int)sizeof(names->ccw_group))
402 names->type = NET_CCWGROUP;
403 return 0;
404 }
405
names_mac(struct udev_device * dev,struct netnames * names)406 static int names_mac(struct udev_device *dev, struct netnames *names) {
407 const char *s;
408 unsigned int i;
409 unsigned int a1, a2, a3, a4, a5, a6;
410
411 /* check for NET_ADDR_PERM, skip random MAC addresses */
412 s = udev_device_get_sysattr_value(dev, "addr_assign_type");
413 if (!s)
414 return EXIT_FAILURE;
415 i = strtoul(s, NULL, 0);
416 if (i != 0)
417 return 0;
418
419 s = udev_device_get_sysattr_value(dev, "address");
420 if (!s)
421 return -ENOENT;
422 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
423 return -EINVAL;
424
425 /* skip empty MAC addresses */
426 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
427 return -EINVAL;
428
429 names->mac[0] = a1;
430 names->mac[1] = a2;
431 names->mac[2] = a3;
432 names->mac[3] = a4;
433 names->mac[4] = a5;
434 names->mac[5] = a6;
435 names->mac_valid = true;
436 return 0;
437 }
438
439 /* IEEE Organizationally Unique Identifier vendor string */
ieee_oui(struct udev_device * dev,struct netnames * names,bool test)440 static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) {
441 char str[32];
442
443 if (!names->mac_valid)
444 return -ENOENT;
445 /* skip commonly misused 00:00:00 (Xerox) prefix */
446 if (memcmp(names->mac, "\0\0\0", 3) == 0)
447 return -EINVAL;
448 snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X",
449 names->mac[0], names->mac[1], names->mac[2],
450 names->mac[3], names->mac[4], names->mac[5]);
451 udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test);
452 return 0;
453 }
454
builtin_net_id(struct udev_device * dev,int argc,char * argv[],bool test)455 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
456 const char *s;
457 const char *p;
458 unsigned int i;
459 const char *devtype;
460 const char *prefix = "en";
461 struct netnames names = {};
462 int err;
463
464 /* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */
465 s = udev_device_get_sysattr_value(dev, "type");
466 if (!s)
467 return EXIT_FAILURE;
468 i = strtoul(s, NULL, 0);
469 switch (i) {
470 case ARPHRD_ETHER:
471 prefix = "en";
472 break;
473 case ARPHRD_SLIP:
474 prefix = "sl";
475 break;
476 default:
477 return 0;
478 }
479
480 /* skip stacked devices, like VLANs, ... */
481 s = udev_device_get_sysattr_value(dev, "ifindex");
482 if (!s)
483 return EXIT_FAILURE;
484 p = udev_device_get_sysattr_value(dev, "iflink");
485 if (!p)
486 return EXIT_FAILURE;
487 if (!streq(s, p))
488 return 0;
489
490 devtype = udev_device_get_devtype(dev);
491 if (devtype) {
492 if (streq("wlan", devtype))
493 prefix = "wl";
494 else if (streq("wwan", devtype))
495 prefix = "ww";
496 }
497
498 err = names_mac(dev, &names);
499 if (err >= 0 && names.mac_valid) {
500 char str[IFNAMSIZ];
501
502 snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix,
503 names.mac[0], names.mac[1], names.mac[2],
504 names.mac[3], names.mac[4], names.mac[5]);
505 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
506
507 ieee_oui(dev, &names, test);
508 }
509
510 /* get path names for Linux on System z network devices */
511 err = names_ccw(dev, &names);
512 if (err >= 0 && names.type == NET_CCWGROUP) {
513 char str[IFNAMSIZ];
514
515 if (snprintf(str, sizeof(str), "%s%s", prefix, names.ccw_group) < (int)sizeof(str))
516 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
517 goto out;
518 }
519
520 /* get PCI based path names, we compose only PCI based paths */
521 err = names_pci(dev, &names);
522 if (err < 0)
523 goto out;
524
525 /* plain PCI device */
526 if (names.type == NET_PCI) {
527 char str[IFNAMSIZ];
528
529 if (names.pci_onboard[0])
530 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str))
531 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
532
533 if (names.pci_onboard_label)
534 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str))
535 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
536
537 if (names.pci_path[0])
538 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (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", prefix, names.pci_slot) < (int)sizeof(str))
543 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
544 goto out;
545 }
546
547 /* USB device */
548 err = names_usb(dev, &names);
549 if (err >= 0 && names.type == NET_USB) {
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.usb_ports) < (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.usb_ports) < (int)sizeof(str))
558 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
559 goto out;
560 }
561
562 /* Broadcom bus */
563 err = names_bcma(dev, &names);
564 if (err >= 0 && names.type == NET_BCMA) {
565 char str[IFNAMSIZ];
566
567 if (names.pci_path[0])
568 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.bcma_core) < (int)sizeof(str))
569 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
570
571 if (names.pci_slot[0])
572 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.bcma_core) < (int)sizeof(str))
573 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
574 goto out;
575 }
576 out:
577 return EXIT_SUCCESS;
578 }
579
580 const struct udev_builtin udev_builtin_net_id = {
581 .name = "net_id",
582 .cmd = builtin_net_id,
583 .help = "Network device properties",
584 };
585