chiark / gitweb /
72237bf31a1e0ea81f08d3b9e65fc23eaa8418aa
[elogind.git] / src / udev / udev-builtin-net_id.c
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
20 /*
21  * eno<index> -- ethernet on-board
22  * ID_NET_NAME_FIRMWARE=eno1
23  *
24  * enp<pci bus number>s<slot>f<function> -- physical location/path
25  * ID_NET_NAME_PATH=enp19s0f0
26  *
27  * enm<MAC address> -- MAC address
28  * ID_NET_NAME_MAC=enxf0def180d479
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37
38 #include "udev.h"
39
40 static int dev_pci(struct udev_device *dev, const char *prefix, bool test) {
41         struct udev_device *d;
42         unsigned int bus;
43         unsigned int slot;
44         unsigned int func;
45         const char *index;
46         int err;
47
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)))
51                 return -ENOENT;
52
53         /* find SMBIOS type 41 entries for on-board devices */
54         index = udev_device_get_sysattr_value(d, "index");
55         if (index) {
56                 unsigned int idx;
57
58                 idx = strtoul(index, NULL, 0);
59                 if (idx > 0) {
60                         const char *label;
61                         char s[16];
62
63                         snprintf(s, sizeof(s), "%so%d", prefix, idx);
64                         udev_builtin_add_property(dev, test, "ID_NET_NAME_FIRMWARE", s);
65
66                         label = udev_device_get_sysattr_value(d, "label");
67                         if (label)
68                                 udev_builtin_add_property(dev, test, "ID_NET_LABEL_FIRMWARE", label);
69                 }
70         }
71
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) {
74                 char str[16];
75
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);
78                 if (err < 0)
79                         return err;
80         }
81         return 0;
82 }
83
84 static int dev_mac(struct udev_device *dev, const char *prefix, bool test) {
85         const char *s;
86         unsigned int i;
87         unsigned int a1, a2, a3, a4, a5, a6;
88         char str[16];
89         int err;
90
91         /* check for NET_ADDR_PERM, skip random MAC addresses */
92         s = udev_device_get_sysattr_value(dev, "addr_assign_type");
93         if (!s)
94                 return EXIT_FAILURE;
95         i = strtoul(s, NULL, 0);
96         if (i != 0)
97                 return 0;
98
99         s = udev_device_get_sysattr_value(dev, "address");
100         if (!s)
101                 return -ENOENT;
102         if (sscanf(s, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6)
103                 return -EINVAL;
104
105         /* skip empty MAC addresses */
106         if (a1 + a2 + a3 + a4 + a5 + a6 == 0)
107                 return -EINVAL;
108
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);
112
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);
115         if (err < 0)
116                 return err;
117         return 0;
118 }
119
120 static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool test) {
121         const char *s;
122         unsigned int i;
123         const char *devtype;
124         const char *prefix = "en";
125
126         /* handle only ARPHRD_ETHER devices */
127         s = udev_device_get_sysattr_value(dev, "type");
128         if (!s)
129                 return EXIT_FAILURE;
130         i = strtoul(s, NULL, 0);
131         if (i != 1)
132                 return 0;
133
134         devtype = udev_device_get_devtype(dev);
135         if (devtype) {
136                 if (streq("wlan", devtype))
137                         prefix = "wl";
138                 else if (streq("wwan", devtype))
139                         prefix = "ww";
140         }
141
142         dev_pci(dev, prefix, test);
143         dev_mac(dev, prefix, test);
144         return EXIT_SUCCESS;
145 }
146
147 const struct udev_builtin udev_builtin_net_id = {
148         .name = "net_id",
149         .cmd = builtin_net_id,
150         .help = "network device properties",
151 };