]>
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 | * | |
25da63b9 KS |
27 | * http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames |
28 | * | |
ad37f393 | 29 | * Two character prefixes based on the type of interface: |
ccddd104 ZJS |
30 | * en — Ethernet |
31 | * sl — serial line IP (slip) | |
32 | * wl — wlan | |
33 | * ww — wwan | |
d23965a6 | 34 | * |
ad37f393 | 35 | * Type of names: |
ccddd104 ZJS |
36 | * b<number> — BCMA bus core number |
37 | * c<bus_id> — CCW bus group name, without leading zeros [s390] | |
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 | |
3058e017 | 41 | * [P<domain>]p<bus>s<slot>[f<function>][d<dev_port>] |
ccddd104 | 42 | * — PCI geographical location |
214daa72 | 43 | * [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>] |
ccddd104 | 44 | * — USB port number chain |
472780d8 | 45 | * |
ad37f393 | 46 | * All multi-function PCI devices will carry the [f<function>] number in the |
472780d8 | 47 | * device name, including the function 0 device. |
d23965a6 | 48 | * |
214daa72 SM |
49 | * When using PCI geography, The PCI domain is only prepended when it is not 0. |
50 | * | |
0d6ce923 KS |
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. | |
ad37f393 | 55 | * |
a8eaaee7 | 56 | * PCI Ethernet card with firmware index "1": |
0035597a | 57 | * ID_NET_NAME_ONBOARD=eno1 |
f610d6de KS |
58 | * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1 |
59 | * | |
a8eaaee7 | 60 | * PCI Ethernet card in hotplug slot with firmware index number: |
f610d6de KS |
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 | |
de892aea | 64 | * ID_NET_NAME_SLOT=ens1 |
f610d6de | 65 | * |
a8eaaee7 | 66 | * PCI Ethernet multi-function card with 2 ports: |
decd634e KS |
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 | * | |
f610d6de KS |
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 | |
d23965a6 KS |
88 | */ |
89 | ||
a660c63c | 90 | #include <errno.h> |
07630cea | 91 | #include <fcntl.h> |
02609440 | 92 | #include <net/if.h> |
19aa72f7 | 93 | #include <net/if_arp.h> |
07630cea LP |
94 | #include <stdarg.h> |
95 | #include <stdio.h> | |
96 | #include <stdlib.h> | |
97 | #include <string.h> | |
98 | #include <unistd.h> | |
de892aea | 99 | #include <linux/pci_regs.h> |
a660c63c | 100 | |
3ffd4af2 | 101 | #include "fd-util.h" |
a5c32cff | 102 | #include "fileio.h" |
d054f0a4 | 103 | #include "stdio-util.h" |
07630cea LP |
104 | #include "string-util.h" |
105 | #include "udev.h" | |
a660c63c | 106 | |
6c1e69f9 LP |
107 | #define ONBOARD_INDEX_MAX (16*1024-1) |
108 | ||
02609440 KS |
109 | enum netname_type{ |
110 | NET_UNDEF, | |
111 | NET_PCI, | |
112 | NET_USB, | |
984c4348 | 113 | NET_BCMA, |
e3d56334 | 114 | NET_VIRTIO, |
e0d4a0ac | 115 | NET_CCWGROUP, |
02609440 KS |
116 | }; |
117 | ||
118 | struct netnames { | |
119 | enum netname_type type; | |
120 | ||
121 | uint8_t mac[6]; | |
122 | bool mac_valid; | |
123 | ||
124 | struct udev_device *pcidev; | |
125 | char pci_slot[IFNAMSIZ]; | |
126 | char pci_path[IFNAMSIZ]; | |
127 | char pci_onboard[IFNAMSIZ]; | |
128 | const char *pci_onboard_label; | |
129 | ||
02609440 | 130 | char usb_ports[IFNAMSIZ]; |
984c4348 | 131 | char bcma_core[IFNAMSIZ]; |
d4b687c9 | 132 | char ccw_group[IFNAMSIZ]; |
02609440 KS |
133 | }; |
134 | ||
0035597a | 135 | /* retrieve on-board index number and label from firmware */ |
02609440 | 136 | static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) { |
c0a43734 TG |
137 | unsigned dev_port = 0; |
138 | size_t l; | |
139 | char *s; | |
140 | const char *attr; | |
0035597a | 141 | int idx; |
a660c63c | 142 | |
ccddd104 | 143 | /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */ |
c0a43734 | 144 | attr = udev_device_get_sysattr_value(names->pcidev, "acpi_index"); |
ccddd104 | 145 | /* SMBIOS type 41 — Onboard Devices Extended Information */ |
c0a43734 TG |
146 | if (!attr) |
147 | attr = udev_device_get_sysattr_value(names->pcidev, "index"); | |
148 | if (!attr) | |
0035597a | 149 | return -ENOENT; |
c0a43734 TG |
150 | |
151 | idx = strtoul(attr, NULL, 0); | |
0035597a KS |
152 | if (idx <= 0) |
153 | return -EINVAL; | |
c0a43734 | 154 | |
6c1e69f9 LP |
155 | /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for |
156 | * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary | |
157 | * cut-off, which is somewhere beyond the realistic number of physical network interface a system might | |
158 | * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */ | |
159 | if (idx > ONBOARD_INDEX_MAX) | |
160 | return -ENOENT; | |
161 | ||
309b578d | 162 | /* kernel provided port index for multiple ports on a single PCI function */ |
c0a43734 TG |
163 | attr = udev_device_get_sysattr_value(dev, "dev_port"); |
164 | if (attr) | |
165 | dev_port = strtol(attr, NULL, 10); | |
166 | ||
167 | s = names->pci_onboard; | |
168 | l = sizeof(names->pci_onboard); | |
169 | l = strpcpyf(&s, l, "o%d", idx); | |
170 | if (dev_port > 0) | |
171 | l = strpcpyf(&s, l, "d%d", dev_port); | |
172 | if (l == 0) | |
173 | names->pci_onboard[0] = '\0'; | |
d23965a6 | 174 | |
02609440 | 175 | names->pci_onboard_label = udev_device_get_sysattr_value(names->pcidev, "label"); |
c0a43734 | 176 | |
0035597a KS |
177 | return 0; |
178 | } | |
d23965a6 | 179 | |
137661d8 | 180 | /* read the 256 bytes PCI configuration space to check the multi-function bit */ |
1328f66a | 181 | static bool is_pci_multifunction(struct udev_device *dev) { |
0454229c | 182 | _cleanup_close_ int fd = -1; |
1cb5d1f3 LP |
183 | const char *filename; |
184 | uint8_t config[64]; | |
de892aea | 185 | |
63c372cb | 186 | filename = strjoina(udev_device_get_syspath(dev), "/config"); |
0454229c JM |
187 | fd = open(filename, O_RDONLY | O_CLOEXEC); |
188 | if (fd < 0) | |
1cb5d1f3 | 189 | return false; |
0454229c | 190 | if (read(fd, &config, sizeof(config)) != sizeof(config)) |
1cb5d1f3 | 191 | return false; |
de892aea KS |
192 | |
193 | /* bit 0-6 header type, bit 7 multi/single function device */ | |
1328f66a | 194 | if ((config[PCI_HEADER_TYPE] & 0x80) != 0) |
1cb5d1f3 LP |
195 | return true; |
196 | ||
197 | return false; | |
de892aea KS |
198 | } |
199 | ||
02609440 KS |
200 | static int dev_pci_slot(struct udev_device *dev, struct netnames *names) { |
201 | struct udev *udev = udev_device_get_udev(names->pcidev); | |
3058e017 | 202 | unsigned domain, bus, slot, func, dev_port = 0; |
1328f66a KS |
203 | size_t l; |
204 | char *s; | |
205 | const char *attr; | |
0035597a | 206 | struct udev_device *pci = NULL; |
b5dd8148 ZJS |
207 | char slots[256], str[256]; |
208 | _cleanup_closedir_ DIR *dir = NULL; | |
0035597a | 209 | struct dirent *dent; |
b5dd8148 | 210 | int hotplug_slot = 0, err = 0; |
0035597a | 211 | |
b5dd8148 | 212 | if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4) |
0035597a | 213 | return -ENOENT; |
1328f66a | 214 | |
309b578d | 215 | /* kernel provided port index for multiple ports on a single PCI function */ |
3058e017 | 216 | attr = udev_device_get_sysattr_value(dev, "dev_port"); |
1328f66a | 217 | if (attr) |
3058e017 | 218 | dev_port = strtol(attr, NULL, 10); |
1328f66a KS |
219 | |
220 | /* compose a name based on the raw kernel's PCI bus, slot numbers */ | |
221 | s = names->pci_path; | |
214daa72 SM |
222 | l = sizeof(names->pci_path); |
223 | if (domain > 0) | |
1fa2f38f ZJS |
224 | l = strpcpyf(&s, l, "P%u", domain); |
225 | l = strpcpyf(&s, l, "p%us%u", bus, slot); | |
1328f66a | 226 | if (func > 0 || is_pci_multifunction(names->pcidev)) |
1fa2f38f | 227 | l = strpcpyf(&s, l, "f%u", func); |
3058e017 | 228 | if (dev_port > 0) |
1fa2f38f | 229 | l = strpcpyf(&s, l, "d%u", dev_port); |
1328f66a KS |
230 | if (l == 0) |
231 | names->pci_path[0] = '\0'; | |
d23965a6 | 232 | |
ccddd104 | 233 | /* ACPI _SUN — slot user number */ |
0035597a KS |
234 | pci = udev_device_new_from_subsystem_sysname(udev, "subsystem", "pci"); |
235 | if (!pci) { | |
236 | err = -ENOENT; | |
237 | goto out; | |
238 | } | |
d054f0a4 | 239 | xsprintf(slots, "%s/slots", udev_device_get_syspath(pci)); |
0035597a KS |
240 | dir = opendir(slots); |
241 | if (!dir) { | |
242 | err = -errno; | |
243 | goto out; | |
d23965a6 KS |
244 | } |
245 | ||
0035597a KS |
246 | for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) { |
247 | int i; | |
248 | char *rest; | |
249 | char *address; | |
250 | ||
251 | if (dent->d_name[0] == '.') | |
252 | continue; | |
253 | i = strtol(dent->d_name, &rest, 10); | |
254 | if (rest[0] != '\0') | |
255 | continue; | |
256 | if (i < 1) | |
257 | continue; | |
d054f0a4 | 258 | xsprintf(str, "%s/%s/address", slots, dent->d_name); |
0035597a KS |
259 | if (read_one_line_file(str, &address) >= 0) { |
260 | /* match slot address with device by stripping the function */ | |
641906e9 | 261 | if (strneq(address, udev_device_get_sysname(names->pcidev), strlen(address))) |
0035597a KS |
262 | hotplug_slot = i; |
263 | free(address); | |
264 | } | |
265 | ||
266 | if (hotplug_slot > 0) | |
267 | break; | |
268 | } | |
d23965a6 | 269 | |
0035597a | 270 | if (hotplug_slot > 0) { |
1328f66a | 271 | s = names->pci_slot; |
214daa72 SM |
272 | l = sizeof(names->pci_slot); |
273 | if (domain > 0) | |
274 | l = strpcpyf(&s, l, "P%d", domain); | |
275 | l = strpcpyf(&s, l, "s%d", hotplug_slot); | |
1328f66a | 276 | if (func > 0 || is_pci_multifunction(names->pcidev)) |
d5a89d7d | 277 | l = strpcpyf(&s, l, "f%d", func); |
3058e017 TLSC |
278 | if (dev_port > 0) |
279 | l = strpcpyf(&s, l, "d%d", dev_port); | |
1328f66a | 280 | if (l == 0) |
16f948cb | 281 | names->pci_slot[0] = '\0'; |
d23965a6 | 282 | } |
0035597a KS |
283 | out: |
284 | udev_device_unref(pci); | |
285 | return err; | |
286 | } | |
287 | ||
02609440 | 288 | static int names_pci(struct udev_device *dev, struct netnames *names) { |
5b8180d3 | 289 | struct udev_device *parent; |
0035597a | 290 | |
3b64e4d4 TG |
291 | assert(dev); |
292 | assert(names); | |
293 | ||
5b8180d3 | 294 | parent = udev_device_get_parent(dev); |
54683f0f TG |
295 | |
296 | /* there can only ever be one virtio bus per parent device, so we can | |
297 | safely ignore any virtio buses. see | |
298 | <http://lists.linuxfoundation.org/pipermail/virtualization/2015-August/030331.html> */ | |
299 | while (parent && streq_ptr("virtio", udev_device_get_subsystem(parent))) | |
300 | parent = udev_device_get_parent(parent); | |
301 | ||
02609440 KS |
302 | if (!parent) |
303 | return -ENOENT; | |
54683f0f | 304 | |
02609440 | 305 | /* check if our direct parent is a PCI device with no other bus in-between */ |
bb26309d | 306 | if (streq_ptr("pci", udev_device_get_subsystem(parent))) { |
02609440 KS |
307 | names->type = NET_PCI; |
308 | names->pcidev = parent; | |
309 | } else { | |
310 | names->pcidev = udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL); | |
311 | if (!names->pcidev) | |
312 | return -ENOENT; | |
313 | } | |
314 | dev_pci_onboard(dev, names); | |
315 | dev_pci_slot(dev, names); | |
316 | return 0; | |
317 | } | |
318 | ||
319 | static int names_usb(struct udev_device *dev, struct netnames *names) { | |
984c4348 | 320 | struct udev_device *usbdev; |
02609440 KS |
321 | char name[256]; |
322 | char *ports; | |
323 | char *config; | |
324 | char *interf; | |
325 | size_t l; | |
326 | char *s; | |
327 | ||
3b64e4d4 TG |
328 | assert(dev); |
329 | assert(names); | |
330 | ||
984c4348 KS |
331 | usbdev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface"); |
332 | if (!usbdev) | |
0035597a KS |
333 | return -ENOENT; |
334 | ||
02609440 | 335 | /* get USB port number chain, configuration, interface */ |
984c4348 | 336 | strscpy(name, sizeof(name), udev_device_get_sysname(usbdev)); |
02609440 KS |
337 | s = strchr(name, '-'); |
338 | if (!s) | |
339 | return -EINVAL; | |
340 | ports = s+1; | |
341 | ||
342 | s = strchr(ports, ':'); | |
343 | if (!s) | |
344 | return -EINVAL; | |
345 | s[0] = '\0'; | |
346 | config = s+1; | |
347 | ||
348 | s = strchr(config, '.'); | |
349 | if (!s) | |
350 | return -EINVAL; | |
351 | s[0] = '\0'; | |
352 | interf = s+1; | |
353 | ||
f7340ab2 | 354 | /* prefix every port number in the chain with "u" */ |
02609440 KS |
355 | s = ports; |
356 | while ((s = strchr(s, '.'))) | |
357 | s[0] = 'u'; | |
358 | s = names->usb_ports; | |
d5a89d7d | 359 | l = strpcpyl(&s, sizeof(names->usb_ports), "u", ports, NULL); |
02609440 KS |
360 | |
361 | /* append USB config number, suppress the common config == 1 */ | |
362 | if (!streq(config, "1")) | |
d5a89d7d | 363 | l = strpcpyl(&s, sizeof(names->usb_ports), "c", config, NULL); |
02609440 KS |
364 | |
365 | /* append USB interface number, suppress the interface == 0 */ | |
366 | if (!streq(interf, "0")) | |
d5a89d7d | 367 | l = strpcpyl(&s, sizeof(names->usb_ports), "i", interf, NULL); |
02609440 KS |
368 | if (l == 0) |
369 | return -ENAMETOOLONG; | |
370 | ||
371 | names->type = NET_USB; | |
d23965a6 KS |
372 | return 0; |
373 | } | |
374 | ||
984c4348 KS |
375 | static int names_bcma(struct udev_device *dev, struct netnames *names) { |
376 | struct udev_device *bcmadev; | |
377 | unsigned int core; | |
378 | ||
3b64e4d4 TG |
379 | assert(dev); |
380 | assert(names); | |
381 | ||
984c4348 KS |
382 | bcmadev = udev_device_get_parent_with_subsystem_devtype(dev, "bcma", NULL); |
383 | if (!bcmadev) | |
384 | return -ENOENT; | |
385 | ||
f4ddacbd | 386 | /* bus num:core num */ |
b5dd8148 | 387 | if (sscanf(udev_device_get_sysname(bcmadev), "bcma%*u:%u", &core) != 1) |
984c4348 | 388 | return -EINVAL; |
f4ddacbd KS |
389 | /* suppress the common core == 0 */ |
390 | if (core > 0) | |
d054f0a4 | 391 | xsprintf(names->bcma_core, "b%u", core); |
f4ddacbd | 392 | |
984c4348 KS |
393 | names->type = NET_BCMA; |
394 | return 0; | |
395 | } | |
396 | ||
e0d4a0ac HB |
397 | static int names_ccw(struct udev_device *dev, struct netnames *names) { |
398 | struct udev_device *cdev; | |
399 | const char *bus_id; | |
400 | size_t bus_id_len; | |
401 | int rc; | |
402 | ||
3b64e4d4 TG |
403 | assert(dev); |
404 | assert(names); | |
405 | ||
e0d4a0ac HB |
406 | /* Retrieve the associated CCW device */ |
407 | cdev = udev_device_get_parent(dev); | |
408 | if (!cdev) | |
409 | return -ENOENT; | |
410 | ||
411 | /* Network devices are always grouped CCW devices */ | |
412 | if (!streq_ptr("ccwgroup", udev_device_get_subsystem(cdev))) | |
413 | return -ENOENT; | |
414 | ||
415 | /* Retrieve bus-ID of the grouped CCW device. The bus-ID uniquely | |
416 | * identifies the network device on the Linux on System z channel | |
417 | * subsystem. Note that the bus-ID contains lowercase characters. | |
418 | */ | |
419 | bus_id = udev_device_get_sysname(cdev); | |
420 | if (!bus_id) | |
421 | return -ENOENT; | |
422 | ||
423 | /* Check the length of the bus-ID. Rely on that the kernel provides | |
424 | * a correct bus-ID; alternatively, improve this check and parse and | |
425 | * verify each bus-ID part... | |
426 | */ | |
427 | bus_id_len = strlen(bus_id); | |
428 | if (!bus_id_len || bus_id_len < 8 || bus_id_len > 9) | |
429 | return -EINVAL; | |
430 | ||
0037a669 DJL |
431 | /* Strip leading zeros from the bus id for aesthetic purposes. This |
432 | * keeps the ccw names stable, yet much shorter in general case of | |
433 | * bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is | |
434 | * not prepended when it is zero. | |
435 | */ | |
436 | bus_id += strspn(bus_id, ".0"); | |
437 | ||
e0d4a0ac | 438 | /* Store the CCW bus-ID for use as network device name */ |
0037a669 | 439 | rc = snprintf(names->ccw_group, sizeof(names->ccw_group), "c%s", bus_id); |
d4b687c9 | 440 | if (rc >= 0 && rc < (int)sizeof(names->ccw_group)) |
e0d4a0ac HB |
441 | names->type = NET_CCWGROUP; |
442 | return 0; | |
443 | } | |
444 | ||
02609440 | 445 | static int names_mac(struct udev_device *dev, struct netnames *names) { |
d23965a6 KS |
446 | const char *s; |
447 | unsigned int i; | |
448 | unsigned int a1, a2, a3, a4, a5, a6; | |
d23965a6 KS |
449 | |
450 | /* check for NET_ADDR_PERM, skip random MAC addresses */ | |
451 | s = udev_device_get_sysattr_value(dev, "addr_assign_type"); | |
452 | if (!s) | |
453 | return EXIT_FAILURE; | |
454 | i = strtoul(s, NULL, 0); | |
455 | if (i != 0) | |
456 | return 0; | |
457 | ||
458 | s = udev_device_get_sysattr_value(dev, "address"); | |
459 | if (!s) | |
460 | return -ENOENT; | |
461 | if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6) | |
462 | return -EINVAL; | |
463 | ||
464 | /* skip empty MAC addresses */ | |
465 | if (a1 + a2 + a3 + a4 + a5 + a6 == 0) | |
a660c63c KS |
466 | return -EINVAL; |
467 | ||
02609440 KS |
468 | names->mac[0] = a1; |
469 | names->mac[1] = a2; | |
470 | names->mac[2] = a3; | |
471 | names->mac[3] = a4; | |
472 | names->mac[4] = a5; | |
473 | names->mac[5] = a6; | |
474 | names->mac_valid = true; | |
475 | return 0; | |
476 | } | |
d23965a6 | 477 | |
02609440 KS |
478 | /* IEEE Organizationally Unique Identifier vendor string */ |
479 | static int ieee_oui(struct udev_device *dev, struct netnames *names, bool test) { | |
971e7fb6 | 480 | char str[32]; |
02609440 | 481 | |
971e7fb6 | 482 | if (!names->mac_valid) |
02609440 KS |
483 | return -ENOENT; |
484 | /* skip commonly misused 00:00:00 (Xerox) prefix */ | |
485 | if (memcmp(names->mac, "\0\0\0", 3) == 0) | |
486 | return -EINVAL; | |
d054f0a4 DM |
487 | xsprintf(str, "OUI:%02X%02X%02X%02X%02X%02X", names->mac[0], |
488 | names->mac[1], names->mac[2], names->mac[3], names->mac[4], | |
489 | names->mac[5]); | |
a4bbef09 | 490 | udev_builtin_hwdb_lookup(dev, NULL, str, NULL, test); |
02609440 | 491 | return 0; |
a660c63c KS |
492 | } |
493 | ||
494 | static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) { | |
d23965a6 | 495 | const char *s; |
72bc96f0 | 496 | const char *p; |
d23965a6 KS |
497 | unsigned int i; |
498 | const char *devtype; | |
499 | const char *prefix = "en"; | |
b92bea5d | 500 | struct netnames names = {}; |
02609440 | 501 | int err; |
d23965a6 | 502 | |
e0d4a0ac | 503 | /* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */ |
d23965a6 KS |
504 | s = udev_device_get_sysattr_value(dev, "type"); |
505 | if (!s) | |
506 | return EXIT_FAILURE; | |
507 | i = strtoul(s, NULL, 0); | |
e0d4a0ac | 508 | switch (i) { |
19aa72f7 | 509 | case ARPHRD_ETHER: |
e0d4a0ac HB |
510 | prefix = "en"; |
511 | break; | |
19aa72f7 | 512 | case ARPHRD_SLIP: |
e0d4a0ac HB |
513 | prefix = "sl"; |
514 | break; | |
515 | default: | |
d23965a6 | 516 | return 0; |
e0d4a0ac | 517 | } |
d23965a6 | 518 | |
72bc96f0 KS |
519 | /* skip stacked devices, like VLANs, ... */ |
520 | s = udev_device_get_sysattr_value(dev, "ifindex"); | |
521 | if (!s) | |
522 | return EXIT_FAILURE; | |
523 | p = udev_device_get_sysattr_value(dev, "iflink"); | |
524 | if (!p) | |
525 | return EXIT_FAILURE; | |
090be865 | 526 | if (!streq(s, p)) |
72bc96f0 KS |
527 | return 0; |
528 | ||
d23965a6 KS |
529 | devtype = udev_device_get_devtype(dev); |
530 | if (devtype) { | |
531 | if (streq("wlan", devtype)) | |
532 | prefix = "wl"; | |
533 | else if (streq("wwan", devtype)) | |
534 | prefix = "ww"; | |
535 | } | |
536 | ||
02609440 KS |
537 | err = names_mac(dev, &names); |
538 | if (err >= 0 && names.mac_valid) { | |
539 | char str[IFNAMSIZ]; | |
540 | ||
d054f0a4 | 541 | xsprintf(str, "%sx%02x%02x%02x%02x%02x%02x", prefix, |
02609440 KS |
542 | names.mac[0], names.mac[1], names.mac[2], |
543 | names.mac[3], names.mac[4], names.mac[5]); | |
544 | udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str); | |
545 | ||
546 | ieee_oui(dev, &names, test); | |
547 | } | |
548 | ||
e0d4a0ac HB |
549 | /* get path names for Linux on System z network devices */ |
550 | err = names_ccw(dev, &names); | |
551 | if (err >= 0 && names.type == NET_CCWGROUP) { | |
552 | char str[IFNAMSIZ]; | |
553 | ||
d4b687c9 | 554 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.ccw_group) < (int)sizeof(str)) |
e0d4a0ac HB |
555 | udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); |
556 | goto out; | |
557 | } | |
558 | ||
02609440 KS |
559 | /* get PCI based path names, we compose only PCI based paths */ |
560 | err = names_pci(dev, &names); | |
561 | if (err < 0) | |
562 | goto out; | |
563 | ||
564 | /* plain PCI device */ | |
565 | if (names.type == NET_PCI) { | |
566 | char str[IFNAMSIZ]; | |
567 | ||
568 | if (names.pci_onboard[0]) | |
569 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard) < (int)sizeof(str)) | |
570 | udev_builtin_add_property(dev, test, "ID_NET_NAME_ONBOARD", str); | |
571 | ||
572 | if (names.pci_onboard_label) | |
573 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_onboard_label) < (int)sizeof(str)) | |
574 | udev_builtin_add_property(dev, test, "ID_NET_LABEL_ONBOARD", str); | |
575 | ||
576 | if (names.pci_path[0]) | |
577 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_path) < (int)sizeof(str)) | |
578 | udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); | |
579 | ||
580 | if (names.pci_slot[0]) | |
581 | if (snprintf(str, sizeof(str), "%s%s", prefix, names.pci_slot) < (int)sizeof(str)) | |
582 | udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); | |
583 | goto out; | |
584 | } | |
585 | ||
586 | /* USB device */ | |
587 | err = names_usb(dev, &names); | |
588 | if (err >= 0 && names.type == NET_USB) { | |
589 | char str[IFNAMSIZ]; | |
590 | ||
591 | if (names.pci_path[0]) | |
592 | if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (int)sizeof(str)) | |
593 | udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); | |
594 | ||
595 | if (names.pci_slot[0]) | |
596 | if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str)) | |
597 | udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); | |
984c4348 KS |
598 | goto out; |
599 | } | |
600 | ||
601 | /* Broadcom bus */ | |
602 | err = names_bcma(dev, &names); | |
603 | if (err >= 0 && names.type == NET_BCMA) { | |
604 | char str[IFNAMSIZ]; | |
605 | ||
606 | if (names.pci_path[0]) | |
607 | if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.bcma_core) < (int)sizeof(str)) | |
608 | udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str); | |
609 | ||
610 | if (names.pci_slot[0]) | |
611 | if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.bcma_core) < (int)sizeof(str)) | |
612 | udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str); | |
613 | goto out; | |
02609440 KS |
614 | } |
615 | out: | |
a660c63c KS |
616 | return EXIT_SUCCESS; |
617 | } | |
618 | ||
619 | const struct udev_builtin udev_builtin_net_id = { | |
620 | .name = "net_id", | |
621 | .cmd = builtin_net_id, | |
5ac0162c | 622 | .help = "Network device properties", |
a660c63c | 623 | }; |