]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-net_id.c
udevadm: do not free node on success
[thirdparty/systemd.git] / src / udev / udev-builtin-net_id.c
CommitLineData
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 *
25da63b9
KS
27 * http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames
28 *
ad37f393 29 * Two character prefixes based on the type of interface:
0035597a
KS
30 * en -- ethernet
31 * wl -- wlan
32 * ww -- wwan
d23965a6 33 *
ad37f393 34 * Type of names:
1328f66a
KS
35 * o<index> -- on-board device index number
36 * s<slot>[f<function>][d<dev_id>] -- hotplug slot index number
37 * x<MAC> -- MAC address
38 * p<bus>s<slot>[f<function>][d<dev_id>] -- PCI geographical location
39 * p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
40 * -- USB port number chain
472780d8 41 *
ad37f393 42 * All multi-function PCI devices will carry the [f<function>] number in the
472780d8 43 * device name, including the function 0 device.
d23965a6 44 *
0d6ce923
KS
45 * For USB devices the full chain of port numbers of hubs is composed. If the
46 * name gets longer than the maximum number of 15 characters, the name is not
47 * exported.
48 * The usual USB configuration == 1 and interface == 0 values are suppressed.
ad37f393 49 *
decd634e 50 * PCI ethernet card with firmware index "1":
0035597a 51 * ID_NET_NAME_ONBOARD=eno1
f610d6de
KS
52 * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
53 *
ad37f393 54 * PCI ethernet card in hotplug slot with firmware index number:
f610d6de
KS
55 * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
56 * ID_NET_NAME_MAC=enx000000000466
57 * ID_NET_NAME_PATH=enp5s0
de892aea 58 * ID_NET_NAME_SLOT=ens1
f610d6de 59 *
decd634e
KS
60 * PCI ethernet multi-function card with 2 ports:
61 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0/net/enp2s0f0
62 * ID_NET_NAME_MAC=enx78e7d1ea46da
63 * ID_NET_NAME_PATH=enp2s0f0
64 * /sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.1/net/enp2s0f1
65 * ID_NET_NAME_MAC=enx78e7d1ea46dc
66 * ID_NET_NAME_PATH=enp2s0f1
67 *
f610d6de
KS
68 * PCI wlan card:
69 * /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
70 * ID_NET_NAME_MAC=wlx0024d7e31130
71 * ID_NET_NAME_PATH=wlp3s0
72 *
73 * USB built-in 3G modem:
74 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
75 * ID_NET_NAME_MAC=wwx028037ec0200
76 * ID_NET_NAME_PATH=wwp0s29u1u4i6
77 *
78 * USB Android phone:
79 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
80 * ID_NET_NAME_MAC=enxd626b3450fb5
81 * ID_NET_NAME_PATH=enp0s29u1u2
d23965a6
KS
82 */
83
a660c63c
KS
84#include <stdio.h>
85#include <stdlib.h>
86#include <stdarg.h>
87#include <unistd.h>
88#include <string.h>
89#include <errno.h>
02609440 90#include <net/if.h>
de892aea 91#include <linux/pci_regs.h>
a660c63c
KS
92
93#include "udev.h"
a5c32cff 94#include "fileio.h"
a660c63c 95
02609440
KS
96enum netname_type{
97 NET_UNDEF,
98 NET_PCI,
99 NET_USB,
984c4348 100 NET_BCMA,
02609440
KS
101};
102
103struct netnames {
104 enum netname_type type;
105
106 uint8_t mac[6];
107 bool mac_valid;
108
109 struct udev_device *pcidev;
110 char pci_slot[IFNAMSIZ];
111 char pci_path[IFNAMSIZ];
112 char pci_onboard[IFNAMSIZ];
113 const char *pci_onboard_label;
114
02609440 115 char usb_ports[IFNAMSIZ];
984c4348
KS
116
117 char bcma_core[IFNAMSIZ];
02609440
KS
118};
119
0035597a 120/* retrieve on-board index number and label from firmware */
02609440 121static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
d23965a6 122 const char *index;
0035597a 123 int idx;
a660c63c 124
0035597a 125 /* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */
02609440 126 index = udev_device_get_sysattr_value(names->pcidev, "acpi_index");
01d183dd
KS
127 /* SMBIOS type 41 -- Onboard Devices Extended Information */
128 if (!index)
02609440 129 index = udev_device_get_sysattr_value(names->pcidev, "index");
0035597a
KS
130 if (!index)
131 return -ENOENT;
132 idx = strtoul(index, NULL, 0);
133 if (idx <= 0)
134 return -EINVAL;
02609440 135 snprintf(names->pci_onboard, sizeof(names->pci_onboard), "o%d", idx);
d23965a6 136
02609440 137 names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label");
0035597a
KS
138 return 0;
139}
d23965a6 140
137661d8 141/* read the 256 bytes PCI configuration space to check the multi-function bit */
1328f66a 142static bool is_pci_multifunction(struct udev_device *dev) {
de892aea 143 char filename[256];
3c123e08 144 FILE *f = NULL;
02609440 145 char config[64];
1328f66a 146 bool multi = false;
de892aea
KS
147
148 snprintf(filename, sizeof(filename), "%s/config", udev_device_get_syspath(dev));
149 f = fopen(filename, "re");
150 if (!f)
151 goto out;
152 if (fread(&config, sizeof(config), 1, f) != 1)
153 goto out;
154
155 /* bit 0-6 header type, bit 7 multi/single function device */
1328f66a
KS
156 if ((config[PCI_HEADER_TYPE] & 0x80) != 0)
157 multi = true;
de892aea 158out:
3c123e08
LN
159 if(f)
160 fclose(f);
1328f66a 161 return multi;
de892aea
KS
162}
163
02609440
KS
164static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
165 struct udev *udev = udev_device_get_udev(names->pcidev);
0035597a
KS
166 unsigned int bus;
167 unsigned int slot;
168 unsigned int func;
1328f66a
KS
169 unsigned int dev_id = 0;
170 size_t l;
171 char *s;
172 const char *attr;
0035597a
KS
173 struct udev_device *pci = NULL;
174 char slots[256];
175 DIR *dir;
176 struct dirent *dent;
177 char str[256];
178 int hotplug_slot = 0;
179 int err = 0;
180
02609440 181 if (sscanf(udev_device_get_sysname(names->pcidev), "0000:%x:%x.%d", &bus, &slot, &func) != 3)
0035597a 182 return -ENOENT;
1328f66a
KS
183
184 /* kernel provided multi-device index */
185 attr = udev_device_get_sysattr_value(dev, "dev_id");
186 if (attr)
187 dev_id = strtol(attr, NULL, 16);
188
189 /* compose a name based on the raw kernel's PCI bus, slot numbers */
190 s = names->pci_path;
d5a89d7d 191 l = strpcpyf(&s, sizeof(names->pci_path), "p%ds%d", bus, slot);
1328f66a 192 if (func > 0 || is_pci_multifunction(names->pcidev))
d5a89d7d 193 l = strpcpyf(&s, l, "f%d", func);
1328f66a 194 if (dev_id > 0)
d5a89d7d 195 l = strpcpyf(&s, l, "d%d", dev_id);
1328f66a
KS
196 if (l == 0)
197 names->pci_path[0] = '\0';
d23965a6 198
0035597a
KS
199 /* ACPI _SUN -- slot user number */
200 pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci");
201 if (!pci) {
202 err = -ENOENT;
203 goto out;
204 }
205 snprintf(slots, sizeof(slots), "%s/slots", udev_device_get_syspath(pci));
206 dir = opendir(slots);
207 if (!dir) {
208 err = -errno;
209 goto out;
d23965a6
KS
210 }
211
0035597a
KS
212 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
213 int i;
214 char *rest;
215 char *address;
216
217 if (dent->d_name[0] == '.')
218 continue;
219 i = strtol(dent->d_name, &rest, 10);
220 if (rest[0] != '\0')
221 continue;
222 if (i < 1)
223 continue;
224 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
225 if (read_one_line_file(str, &address) >= 0) {
226 /* match slot address with device by stripping the function */
641906e9 227 if (strneq(address, udev_device_get_sysname(names->pcidev), strlen(address)))
0035597a
KS
228 hotplug_slot = i;
229 free(address);
230 }
231
232 if (hotplug_slot > 0)
233 break;
234 }
235 closedir(dir);
d23965a6 236
0035597a 237 if (hotplug_slot > 0) {
1328f66a 238 s = names->pci_slot;
d5a89d7d 239 l = strpcpyf(&s, sizeof(names->pci_slot), "s%d", hotplug_slot);
1328f66a 240 if (func > 0 || is_pci_multifunction(names->pcidev))
d5a89d7d 241 l = strpcpyf(&s, l, "f%d", func);
1328f66a 242 if (dev_id > 0)
d5a89d7d 243 l = strpcpyf(&s, l, "d%d", dev_id);
1328f66a
KS
244 if (l == 0)
245 names->pci_path[0] = '\0';
d23965a6 246 }
0035597a
KS
247out:
248 udev_device_unref(pci);
249 return err;
250}
251
02609440 252static int names_pci(struct udev_device *dev, struct netnames *names) {
5b8180d3 253 struct udev_device *parent;
0035597a 254
5b8180d3 255 parent = udev_device_get_parent(dev);
02609440
KS
256 if (!parent)
257 return -ENOENT;
258 /* check if our direct parent is a PCI device with no other bus in-between */
bb26309d 259 if (streq_ptr("pci", udev_device_get_subsystem(parent))) {
02609440
KS
260 names->type = NET_PCI;
261 names->pcidev = parent;
262 } else {
263 names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
264 if (!names->pcidev)
265 return -ENOENT;
266 }
267 dev_pci_onboard(dev, names);
268 dev_pci_slot(dev, names);
269 return 0;
270}
271
272static int names_usb(struct udev_device *dev, struct netnames *names) {
984c4348 273 struct udev_device *usbdev;
02609440
KS
274 char name[256];
275 char *ports;
276 char *config;
277 char *interf;
278 size_t l;
279 char *s;
280
984c4348
KS
281 usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
282 if (!usbdev)
0035597a
KS
283 return -ENOENT;
284
02609440 285 /* get USB port number chain, configuration, interface */
984c4348 286 strscpy(name, sizeof(name), udev_device_get_sysname(usbdev));
02609440
KS
287 s = strchr(name, '-');
288 if (!s)
289 return -EINVAL;
290 ports = s+1;
291
292 s = strchr(ports, ':');
293 if (!s)
294 return -EINVAL;
295 s[0] = '\0';
296 config = s+1;
297
298 s = strchr(config, '.');
299 if (!s)
300 return -EINVAL;
301 s[0] = '\0';
302 interf = s+1;
303
304 /* prefix every port number in the chain with "u"*/
305 s = ports;
306 while ((s = strchr(s, '.')))
307 s[0] = 'u';
308 s = names->usb_ports;
d5a89d7d 309 l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL);
02609440
KS
310
311 /* append USB config number, suppress the common config == 1 */
312 if (!streq(config, "1"))
d5a89d7d 313 l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL);
02609440
KS
314
315 /* append USB interface number, suppress the interface == 0 */
316 if (!streq(interf, "0"))
d5a89d7d 317 l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL);
02609440
KS
318 if (l == 0)
319 return -ENAMETOOLONG;
320
321 names->type = NET_USB;
d23965a6
KS
322 return 0;
323}
324
984c4348
KS
325static int names_bcma(struct udev_device *dev, struct netnames *names) {
326 struct udev_device *bcmadev;
327 unsigned int core;
328
329 bcmadev = udev_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL);
330 if (!bcmadev)
331 return -ENOENT;
332
f4ddacbd 333 /* bus num:core num */
984c4348
KS
334 if (sscanf(udev_device_get_sysname(bcmadev), "bcma%*d:%d", &core) != 1)
335 return -EINVAL;
f4ddacbd
KS
336 /* suppress the common core == 0 */
337 if (core > 0)
338 snprintf(names->bcma_core, sizeof(names->bcma_core), "b%d", core);
339
984c4348
KS
340 names->type = NET_BCMA;
341 return 0;
342}
343
02609440 344static int names_mac(struct udev_device *dev, struct netnames *names) {
d23965a6
KS
345 const char *s;
346 unsigned int i;
347 unsigned int a1, a2, a3, a4, a5, a6;
d23965a6
KS
348
349 /* check for NET_ADDR_PERM, skip random MAC addresses */
350 s = udev_device_get_sysattr_value(dev, "addr_assign_type");
351 if (!s)
352 return EXIT_FAILURE;
353 i = strtoul(s, NULL, 0);
354 if (i != 0)
355 return 0;
356
357 s = udev_device_get_sysattr_value(dev, "address");
358 if (!s)
359 return -ENOENT;
360 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
361 return -EINVAL;
362
363 /* skip empty MAC addresses */
364 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
a660c63c
KS
365 return -EINVAL;
366
02609440
KS
367 names->mac[0] = a1;
368 names->mac[1] = a2;
369 names->mac[2] = a3;
370 names->mac[3] = a4;
371 names->mac[4] = a5;
372 names->mac[5] = a6;
373 names->mac_valid = true;
374 return 0;
375}
d23965a6 376
02609440
KS
377/* IEEE Organizationally Unique Identifier vendor string */
378static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) {
971e7fb6 379 char str[32];
02609440 380
971e7fb6 381 if (!names->mac_valid)
02609440
KS
382 return -ENOENT;
383 /* skip commonly misused 00:00:00 (Xerox) prefix */
384 if (memcmp(names->mac, "\0\0\0", 3) == 0)
385 return -EINVAL;
386 snprintf(str, sizeof(str), "OUI:%02X%02X%02X%02X%02X%02X",
387 names->mac[0], names->mac[1], names->mac[2],
388 names->mac[3], names->mac[4], names->mac[5]);
389 udev_builtin_hwdb_lookup(dev, str, test);
390 return 0;
a660c63c
KS
391}
392
393static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
d23965a6 394 const char *s;
72bc96f0 395 const char *p;
d23965a6
KS
396 unsigned int i;
397 const char *devtype;
398 const char *prefix = "en";
02609440
KS
399 struct netnames names;
400 int err;
d23965a6
KS
401
402 /* handle only ARPHRD_ETHER devices */
403 s = udev_device_get_sysattr_value(dev, "type");
404 if (!s)
405 return EXIT_FAILURE;
406 i = strtoul(s, NULL, 0);
407 if (i != 1)
408 return 0;
409
72bc96f0
KS
410 /* skip stacked devices, like VLANs, ... */
411 s = udev_device_get_sysattr_value(dev, "ifindex");
412 if (!s)
413 return EXIT_FAILURE;
414 p = udev_device_get_sysattr_value(dev, "iflink");
415 if (!p)
416 return EXIT_FAILURE;
090be865 417 if (!streq(s, p))
72bc96f0
KS
418 return 0;
419
d23965a6
KS
420 devtype = udev_device_get_devtype(dev);
421 if (devtype) {
422 if (streq("wlan", devtype))
423 prefix = "wl";
424 else if (streq("wwan", devtype))
425 prefix = "ww";
426 }
427
02609440
KS
428 zero(names);
429 err = names_mac(dev, &names);
430 if (err >= 0 && names.mac_valid) {
431 char str[IFNAMSIZ];
432
433 snprintf(str, sizeof(str), "%sx%02x%02x%02x%02x%02x%02x", prefix,
434 names.mac[0], names.mac[1], names.mac[2],
435 names.mac[3], names.mac[4], names.mac[5]);
436 udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
437
438 ieee_oui(dev, &names, test);
439 }
440
441 /* get PCI based path names, we compose only PCI based paths */
442 err = names_pci(dev, &names);
443 if (err < 0)
444 goto out;
445
446 /* plain PCI device */
447 if (names.type == NET_PCI) {
448 char str[IFNAMSIZ];
449
450 if (names.pci_onboard[0])
451 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str))
452 udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str);
453
454 if (names.pci_onboard_label)
455 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str))
456 udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str);
457
458 if (names.pci_path[0])
459 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (int)sizeof(str))
460 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
461
462 if (names.pci_slot[0])
463 if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_slot) < (int)sizeof(str))
464 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
465 goto out;
466 }
467
468 /* USB device */
469 err = names_usb(dev, &names);
470 if (err >= 0 && names.type == NET_USB) {
471 char str[IFNAMSIZ];
472
473 if (names.pci_path[0])
474 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (int)sizeof(str))
475 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
476
477 if (names.pci_slot[0])
478 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str))
479 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
984c4348
KS
480 goto out;
481 }
482
483 /* Broadcom bus */
484 err = names_bcma(dev, &names);
485 if (err >= 0 && names.type == NET_BCMA) {
486 char str[IFNAMSIZ];
487
488 if (names.pci_path[0])
489 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.bcma_core) < (int)sizeof(str))
490 udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
491
492 if (names.pci_slot[0])
493 if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.bcma_core) < (int)sizeof(str))
494 udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
495 goto out;
02609440 496 }
984c4348 497
02609440 498out:
a660c63c
KS
499 return EXIT_SUCCESS;
500}
501
502const struct udev_builtin udev_builtin_net_id = {
503 .name = "net_id",
504 .cmd = builtin_net_id,
505 .help = "network device properties",
506};