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