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