2 This file is part of systemd.
4 Copyright 2012 Kay Sievers <kay@vrfy.org>
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.
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.
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/>.
21 * eno<index> -- ethernet on-board
22 * ID_NET_NAME_FIRMWARE=eno1
24 * enp<pci bus number>s<slot>f<function> -- physical location/path
25 * ID_NET_NAME_PATH=enp19s0f0
27 * enm<MAC address> -- MAC address
28 * ID_NET_NAME_MAC=enxf0def180d479
40 static int dev_pci(struct udev_device *dev, const char *prefix, bool test) {
41 struct udev_device *d;
48 /* skip other buses than direct PCI parents */
49 d = udev_device_get_parent(dev);
50 if (!d || !streq("pci", udev_device_get_subsystem(d)))
53 /* find SMBIOS type 41 entries for on-board devices */
54 index = udev_device_get_sysattr_value(d, "index");
58 idx = strtoul(index, NULL, 0);
63 snprintf(s, sizeof(s), "%so%d", prefix, idx);
64 udev_builtin_add_property(dev, test, "ID_NET_NAME_FIRMWARE", s);
66 label = udev_device_get_sysattr_value(d, "label");
68 udev_builtin_add_property(dev, test, "ID_NET_LABEL_FIRMWARE", label);
72 /* compose a name based on the PCI bus location */
73 if (sscanf(udev_device_get_sysname(d), "0000:%x:%x.%d", &bus, &slot, &func) == 3) {
76 snprintf(str, sizeof(str), "%sp%ds%df%d", prefix, bus, slot, func);
77 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
84 static int dev_mac(struct udev_device *dev, const char *prefix, bool test) {
87 unsigned int a1, a2, a3, a4, a5, a6;
91 /* check for NET_ADDR_PERM, skip random MAC addresses */
92 s = udev_device_get_sysattr_value(dev, "addr_assign_type");
95 i = strtoul(s, NULL, 0);
99 s = udev_device_get_sysattr_value(dev, "address");
102 if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
105 /* skip empty MAC addresses */
106 if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
109 /* add IEEE Organizationally Unique Identifier */
110 snprintf(str, sizeof(str), "OUI:%X%X%X", a1, a2, a3);
111 udev_builtin_hwdb_lookup(dev, str, test);
113 snprintf(str, sizeof(str), "%sx%x%x%x%x%x%x", prefix, a1, a2, a3, a4, a5, a6);
114 err = udev_builtin_add_property(dev, test, "ID_NET_NAME_MAC", str);
120 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
124 const char *prefix = "en";
126 /* handle only ARPHRD_ETHER devices */
127 s = udev_device_get_sysattr_value(dev, "type");
130 i = strtoul(s, NULL, 0);
134 devtype = udev_device_get_devtype(dev);
136 if (streq("wlan", devtype))
138 else if (streq("wwan", devtype))
142 dev_pci(dev, prefix, test);
143 dev_mac(dev, prefix, test);
147 const struct udev_builtin udev_builtin_net_id = {
149 .cmd = builtin_net_id,
150 .help = "network device properties",