Debian Codegraph @

src/network/networkd-wifi.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/nl80211.h>
4 #include <net/ethernet.h>
5
6 #include "sd-netlink.h"
7
8 #include "alloc-util.h"
9 #include "ether-addr-util.h"
10 #include "networkd-link.h"
11 #include "networkd-manager.h"
12 #include "networkd-wifi.h"
13 #include "networkd-wiphy.h"
14 #include "set.h"
15 #include "string-util.h"
16 #include "wifi-util.h"
17
18 int link_get_wlan_interface(Link *link) {
19 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
20 int r;
21
22 assert(link);
23
24 r = sd_genl_message_new(link->manager->genl, NL80211_GENL_NAME, NL80211_CMD_GET_INTERFACE, &req);
25 if (r < 0)
26 return log_link_debug_errno(link, r, "Failed to create generic netlink message: %m");
27
28 r = sd_netlink_message_append_u32(req, NL80211_ATTR_IFINDEX, link->ifindex);
29 if (r < 0)
30 return log_link_debug_errno(link, r, "Could not append NL80211_ATTR_IFINDEX attribute: %m");
31
32 r = sd_netlink_call(link->manager->genl, req, 0, &reply);
33 if (r < 0)
34 return log_link_debug_errno(link, r, "Failed to request information about wlan interface: %m");
35 if (!reply) {
36 log_link_debug(link, "No reply received to request for information about wifi interface, ignoring.");
37 return 0;
38 }
39
40 return manager_genl_process_nl80211_config(link->manager->genl, reply, link->manager);
41 }
42
43 int manager_genl_process_nl80211_config(sd_netlink *genl, sd_netlink_message *message, Manager *manager) {
44 _cleanup_free_ char *ssid = NULL;
45 uint32_t ifindex, wlan_iftype;
46 const char *family, *ifname;
47 uint8_t cmd;
48 size_t len;
49 Link *link;
50 int r;
51
52 assert(genl);
53 assert(message);
54 assert(manager);
55
56 if (sd_netlink_message_is_error(message)) {
57 r = sd_netlink_message_get_errno(message);
58 if (r < 0)
59 log_message_warning_errno(message, r, "nl80211: received error message, ignoring");
60
61 return 0;
62 }
63
64 r = sd_genl_message_get_family_name(genl, message, &family);
65 if (r < 0) {
66 log_debug_errno(r, "nl80211: failed to determine genl family, ignoring: %m");
67 return 0;
68 }
69 if (!streq(family, NL80211_GENL_NAME)) {
70 log_debug("nl80211: received message of unexpected genl family '%s', ignoring.", family);
71 return 0;
72 }
73
74 r = sd_genl_message_get_command(genl, message, &cmd);
75 if (r < 0) {
76 log_debug_errno(r, "nl80211: failed to determine genl message command, ignoring: %m");
77 return 0;
78 }
79 if (IN_SET(cmd, NL80211_CMD_NEW_WIPHY, NL80211_CMD_DEL_WIPHY))
80 return manager_genl_process_nl80211_wiphy(genl, message, manager);
81 if (!IN_SET(cmd, NL80211_CMD_SET_INTERFACE, NL80211_CMD_NEW_INTERFACE, NL80211_CMD_DEL_INTERFACE)) {
82 log_debug("nl80211: ignoring nl80211 %s(%u) message.",
83 strna(nl80211_cmd_to_string(cmd)), cmd);
84 return 0;
85 }
86
87 r = sd_netlink_message_read_u32(message, NL80211_ATTR_IFINDEX, &ifindex);
88 if (r < 0) {
89 log_debug_errno(r, "nl80211: received %s(%u) message without valid ifindex, ignoring: %m",
90 strna(nl80211_cmd_to_string(cmd)), cmd);
91 return 0;
92 }
93
94 r = link_get_by_index(manager, ifindex, &link);
95 if (r < 0) {
96 log_debug_errno(r, "nl80211: received %s(%u) message for link '%"PRIu32"' we don't know about, ignoring.",
97 strna(nl80211_cmd_to_string(cmd)), cmd, ifindex);
98
99 /* The NL80211_CMD_NEW_INTERFACE message might arrive before RTM_NEWLINK, in which case a
100 * link will not have been created yet. Store the interface index such that the wireless
101 * properties of the link (such as wireless interface type) are queried again after the link
102 * is created.
103 */
104 if (cmd == NL80211_CMD_NEW_INTERFACE) {
105 r = set_ensure_put(&manager->new_wlan_ifindices, NULL, INT_TO_PTR(ifindex));
106 if (r < 0)
107 log_warning_errno(r, "Failed to add new wireless interface index to set, ignoring: %m");
108 } else if (cmd == NL80211_CMD_DEL_INTERFACE)
109 set_remove(manager->new_wlan_ifindices, INT_TO_PTR(ifindex));
110
111 return 0;
112 }
113
114 r = sd_netlink_message_read_string(message, NL80211_ATTR_IFNAME, &ifname);
115 if (r < 0) {
116 log_link_debug_errno(link, r, "nl80211: received %s(%u) message without valid interface name, ignoring: %m",
117 strna(nl80211_cmd_to_string(cmd)), cmd);
118 return 0;
119 }
120
121 if (!streq(ifname, link->ifname)) {
122 log_link_debug(link, "nl80211: received %s(%u) message with invalid interface name '%s', ignoring.",
123 strna(nl80211_cmd_to_string(cmd)), cmd, ifname);
124 return 0;
125 }
126
127 r = sd_netlink_message_read_u32(message, NL80211_ATTR_IFTYPE, &wlan_iftype);
128 if (r < 0) {
129 log_link_debug_errno(link, r, "nl80211: received %s(%u) message without valid wlan interface type, ignoring: %m",
130 strna(nl80211_cmd_to_string(cmd)), cmd);
131 return 0;
132 }
133
134 r = sd_netlink_message_read_data(message, NL80211_ATTR_SSID, &len, (void**) &ssid);
135 if (r < 0 && r != -ENODATA) {
136 log_link_debug_errno(link, r, "nl80211: received %s(%u) message without valid SSID, ignoring: %m",
137 strna(nl80211_cmd_to_string(cmd)), cmd);
138 return 0;
139 }
140 if (r >= 0) {
141 if (len == 0) {
142 log_link_debug(link, "nl80211: received SSID has zero length, ignoring it.");
143 ssid = mfree(ssid);
144 } else if (strlen_ptr(ssid) != len) {
145 log_link_debug(link, "nl80211: received SSID contains NUL characters, ignoring it.");
146 ssid = mfree(ssid);
147 }
148 }
149
150 log_link_debug(link, "nl80211: received %s(%u) message: iftype=%s, ssid=%s",
151 strna(nl80211_cmd_to_string(cmd)), cmd,
152 strna(nl80211_iftype_to_string(wlan_iftype)), strna(ssid));
153
154 switch (cmd) {
155 case NL80211_CMD_SET_INTERFACE:
156 case NL80211_CMD_NEW_INTERFACE:
157 link->wlan_iftype = wlan_iftype;
158 free_and_replace(link->ssid, ssid);
159 break;
160
161 case NL80211_CMD_DEL_INTERFACE:
162 link->wlan_iftype = NL80211_IFTYPE_UNSPECIFIED;
163 link->ssid = mfree(link->ssid);
164 break;
165
166 default:
167 assert_not_reached();
168 }
169
170 return 0;
171 }
172
173 int manager_genl_process_nl80211_mlme(sd_netlink *genl, sd_netlink_message *message, Manager *manager) {
174 const char *family;
175 uint32_t ifindex;
176 uint8_t cmd;
177 Link *link;
178 int r;
179
180 assert(genl);
181 assert(message);
182 assert(manager);
183
184 if (sd_netlink_message_is_error(message)) {
185 r = sd_netlink_message_get_errno(message);
186 if (r < 0)
187 log_message_warning_errno(message, r, "nl80211: received error message, ignoring");
188
189 return 0;
190 }
191
192 r = sd_genl_message_get_family_name(genl, message, &family);
193 if (r < 0) {
194 log_debug_errno(r, "nl80211: failed to determine genl family, ignoring: %m");
195 return 0;
196 }
197 if (!streq(family, NL80211_GENL_NAME)) {
198 log_debug("nl80211: Received message of unexpected genl family '%s', ignoring.", family);
199 return 0;
200 }
201
202 r = sd_genl_message_get_command(genl, message, &cmd);
203 if (r < 0) {
204 log_debug_errno(r, "nl80211: failed to determine genl message command, ignoring: %m");
205 return 0;
206 }
207
208 r = sd_netlink_message_read_u32(message, NL80211_ATTR_IFINDEX, &ifindex);
209 if (r < 0) {
210 log_debug_errno(r, "nl80211: received %s(%u) message without valid ifindex, ignoring: %m",
211 strna(nl80211_cmd_to_string(cmd)), cmd);
212 return 0;
213 }
214
215 r = link_get_by_index(manager, ifindex, &link);
216 if (r < 0) {
217 log_debug_errno(r, "nl80211: received %s(%u) message for link '%"PRIu32"' we don't know about, ignoring.",
218 strna(nl80211_cmd_to_string(cmd)), cmd, ifindex);
219 return 0;
220 }
221
222 switch (cmd) {
223 case NL80211_CMD_NEW_STATION:
224 case NL80211_CMD_DEL_STATION: {
225 struct ether_addr bssid;
226
227 r = sd_netlink_message_read_ether_addr(message, NL80211_ATTR_MAC, &bssid);
228 if (r < 0) {
229 log_link_debug_errno(link, r, "nl80211: received %s(%u) message without valid BSSID, ignoring: %m",
230 strna(nl80211_cmd_to_string(cmd)), cmd);
231 return 0;
232 }
233
234 log_link_debug(link, "nl80211: received %s(%u) message: bssid=%s",
235 strna(nl80211_cmd_to_string(cmd)), cmd, ETHER_ADDR_TO_STR(&bssid));
236
237 if (cmd == NL80211_CMD_DEL_STATION) {
238 link->bssid = ETHER_ADDR_NULL;
239 return 0;
240 }
241
242 link->bssid = bssid;
243
244 if (manager->enumerating &&
245 link->wlan_iftype == NL80211_IFTYPE_STATION && link->ssid)
246 log_link_info(link, "Connected WiFi access point: %s (%s)",
247 link->ssid, ETHER_ADDR_TO_STR(&link->bssid));
248 break;
249 }
250 case NL80211_CMD_CONNECT: {
251 struct ether_addr bssid;
252 uint16_t status_code;
253
254 r = sd_netlink_message_read_ether_addr(message, NL80211_ATTR_MAC, &bssid);
255 if (r < 0 && r != -ENODATA) {
256 log_link_debug_errno(link, r, "nl80211: received %s(%u) message without valid BSSID, ignoring: %m",
257 strna(nl80211_cmd_to_string(cmd)), cmd);
258 return 0;
259 }
260
261 r = sd_netlink_message_read_u16(message, NL80211_ATTR_STATUS_CODE, &status_code);
262 if (r < 0) {
263 log_link_debug_errno(link, r, "nl80211: received %s(%u) message without valid status code, ignoring: %m",
264 strna(nl80211_cmd_to_string(cmd)), cmd);
265 return 0;
266 }
267
268 log_link_debug(link, "nl80211: received %s(%u) message: status=%u, bssid=%s",
269 strna(nl80211_cmd_to_string(cmd)), cmd, status_code, ETHER_ADDR_TO_STR(&bssid));
270
271 if (status_code != 0)
272 return 0;
273
274 link->bssid = bssid;
275
276 if (!manager->enumerating) {
277 r = link_get_wlan_interface(link);
278 if (r < 0) {
279 log_link_warning_errno(link, r, "Failed to update wireless LAN interface: %m");
280 link_enter_failed(link);
281 return 0;
282 }
283 }
284
285 if (link->wlan_iftype == NL80211_IFTYPE_STATION && link->ssid)
286 log_link_info(link, "Connected WiFi access point: %s (%s)",
287 link->ssid, ETHER_ADDR_TO_STR(&link->bssid));
288
289 /* Sometimes, RTM_NEWLINK message with carrier is received earlier than NL80211_CMD_CONNECT.
290 * To make SSID= or other WiFi related settings in [Match] section work, let's try to
291 * reconfigure the interface. */
292 if (link->ssid && link_has_carrier(link)) {
293 r = link_reconfigure_impl(link, /* flags= */ 0);
294 if (r < 0) {
295 log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
296 link_enter_failed(link);
297 return 0;
298 }
299 }
300 break;
301 }
302 case NL80211_CMD_DISCONNECT:
303 log_link_debug(link, "nl80211: received %s(%u) message.",
304 strna(nl80211_cmd_to_string(cmd)), cmd);
305
306 link->bssid = ETHER_ADDR_NULL;
307 free_and_replace(link->previous_ssid, link->ssid);
308 break;
309
310 case NL80211_CMD_START_AP: {
311 log_link_debug(link, "nl80211: received %s(%u) message.",
312 strna(nl80211_cmd_to_string(cmd)), cmd);
313
314 /* No need to reconfigure during enumeration */
315 if (manager->enumerating)
316 break;
317
318 /* If there is no carrier, let the link get configured on
319 * carrier gain instead */
320 if (!link_has_carrier(link))
321 break;
322
323 /* AP start event may indicate different properties (e.g. SSID) */
324 r = link_get_wlan_interface(link);
325 if (r < 0) {
326 log_link_warning_errno(link, r, "Failed to update wireless LAN interface: %m");
327 link_enter_failed(link);
328 return 0;
329 }
330
331 /* If necessary, reconfigure based on those new properties */
332 r = link_reconfigure_impl(link, /* flags= */ 0);
333 if (r < 0) {
334 log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
335 link_enter_failed(link);
336 return 0;
337 }
338
339 break;
340 }
341
342 default:
343 log_link_debug(link, "nl80211: received %s(%u) message.",
344 strna(nl80211_cmd_to_string(cmd)), cmd);
345 }
346
347 return 0;
348 }