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