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