1 /*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "supp_events.h"
8
9 #include "includes.h"
10 #include "common.h"
11 #include "common/ieee802_11_defs.h"
12 #include "wpa_supplicant_i.h"
13
14 #ifdef CONFIG_AP
15 #include "ap/sta_info.h"
16 #include "ap/ieee802_11.h"
17 #include "ap/hostapd.h"
18 #endif /* CONFIG_AP */
19
20 #include <zephyr/net/wifi_mgmt.h>
21
22 /* Re-defines MAC2STR with address of the element */
23 #define MACADDR2STR(a) &(a)[0], &(a)[1], &(a)[2], &(a)[3], &(a)[4], &(a)[5]
24
25 static const struct wpa_supp_event_info {
26 const char *event_str;
27 enum supplicant_event_num event;
28 } wpa_supp_event_info[] = {
29 { "CTRL-EVENT-CONNECTED", SUPPLICANT_EVENT_CONNECTED },
30 { "CTRL-EVENT-DISCONNECTED", SUPPLICANT_EVENT_DISCONNECTED },
31 { "CTRL-EVENT-ASSOC-REJECT", SUPPLICANT_EVENT_ASSOC_REJECT },
32 { "CTRL-EVENT-AUTH-REJECT", SUPPLICANT_EVENT_AUTH_REJECT },
33 { "CTRL-EVENT-SSID-TEMP-DISABLED", SUPPLICANT_EVENT_SSID_TEMP_DISABLED },
34 { "CTRL-EVENT-SSID-REENABLED", SUPPLICANT_EVENT_SSID_REENABLED },
35 { "CTRL-EVENT-BSS-ADDED", SUPPLICANT_EVENT_BSS_ADDED },
36 { "CTRL-EVENT-BSS-REMOVED", SUPPLICANT_EVENT_BSS_REMOVED },
37 { "CTRL-EVENT-TERMINATING", SUPPLICANT_EVENT_TERMINATING },
38 { "CTRL-EVENT-SCAN-STARTED", SUPPLICANT_EVENT_SCAN_STARTED },
39 { "CTRL-EVENT-SCAN-RESULTS", SUPPLICANT_EVENT_SCAN_RESULTS },
40 { "CTRL-EVENT-SCAN-FAILED", SUPPLICANT_EVENT_SCAN_FAILED },
41 { "CTRL-EVENT-NETWORK-NOT-FOUND", SUPPLICANT_EVENT_NETWORK_NOT_FOUND },
42 { "CTRL-EVENT-NETWORK-ADDED", SUPPLICANT_EVENT_NETWORK_ADDED },
43 { "CTRL-EVENT-NETWORK-REMOVED", SUPPLICANT_EVENT_NETWORK_REMOVED },
44 { "CTRL-EVENT-DSCP-POLICY", SUPPLICANT_EVENT_DSCP_POLICY },
45 { "CTRL-EVENT-REGDOM-CHANGE", SUPPLICANT_EVENT_REGDOM_CHANGE },
46 };
47
copy_mac_addr(const unsigned int * src,uint8_t * dst)48 static void copy_mac_addr(const unsigned int *src, uint8_t *dst)
49 {
50 int i;
51
52 for (i = 0; i < ETH_ALEN; i++) {
53 dst[i] = src[i];
54 }
55 }
56
wpas_to_wifi_mgmt_conn_status(int status)57 static enum wifi_conn_status wpas_to_wifi_mgmt_conn_status(int status)
58 {
59 switch (status) {
60 case WLAN_STATUS_SUCCESS:
61 return WIFI_STATUS_CONN_SUCCESS;
62 case WLAN_REASON_4WAY_HANDSHAKE_TIMEOUT:
63 return WIFI_STATUS_CONN_WRONG_PASSWORD;
64 /* Handle non-supplicant errors */
65 case -ETIMEDOUT:
66 return WIFI_STATUS_CONN_TIMEOUT;
67 default:
68 return WIFI_STATUS_CONN_FAIL;
69 }
70 }
71
wpas_to_wifi_mgmt_disconn_status(int status)72 static enum wifi_disconn_reason wpas_to_wifi_mgmt_disconn_status(int status)
73 {
74 switch (status) {
75 case WIFI_REASON_DISCONN_SUCCESS:
76 return WIFI_REASON_DISCONN_SUCCESS;
77 case WLAN_REASON_DEAUTH_LEAVING:
78 return WIFI_REASON_DISCONN_AP_LEAVING;
79 case WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY:
80 return WIFI_REASON_DISCONN_INACTIVITY;
81 case WLAN_REASON_UNSPECIFIED:
82 /* fall through */
83 default:
84 return WIFI_REASON_DISCONN_UNSPECIFIED;
85 }
86 }
87
supplicant_process_status(struct supplicant_int_event_data * event_data,char * supplicant_status)88 static int supplicant_process_status(struct supplicant_int_event_data *event_data,
89 char *supplicant_status)
90 {
91 int ret = 1; /* For cases where parsing is not being done*/
92 int i;
93 union supplicant_event_data *data;
94 unsigned int tmp_mac_addr[ETH_ALEN];
95 unsigned int event_prefix_len;
96 char *event_no_prefix;
97 struct wpa_supp_event_info event_info;
98
99 data = (union supplicant_event_data *)event_data->data;
100
101 for (i = 0; i < ARRAY_SIZE(wpa_supp_event_info); i++) {
102 if (strncmp(supplicant_status, wpa_supp_event_info[i].event_str,
103 strlen(wpa_supp_event_info[i].event_str)) == 0) {
104 event_info = wpa_supp_event_info[i];
105 break;
106 }
107 }
108
109 if (i >= ARRAY_SIZE(wpa_supp_event_info)) {
110 /* This is not a bug but rather implementation gap (intentional or not) */
111 wpa_printf(MSG_DEBUG, "Event not supported: %s", supplicant_status);
112 return -ENOTSUP;
113 }
114
115 /* Skip the event prefix and a space */
116 event_prefix_len = strlen(event_info.event_str) + 1;
117 event_no_prefix = supplicant_status + event_prefix_len;
118 event_data->event = event_info.event;
119
120 switch (event_data->event) {
121 case SUPPLICANT_EVENT_CONNECTED:
122 ret = sscanf(event_no_prefix, "- Connection to"
123 MACSTR, MACADDR2STR(tmp_mac_addr));
124 event_data->data_len = sizeof(data->connected);
125 copy_mac_addr(tmp_mac_addr, data->connected.bssid);
126 break;
127 case SUPPLICANT_EVENT_DISCONNECTED:
128 ret = sscanf(event_no_prefix,
129 MACSTR" reason=%d", MACADDR2STR(tmp_mac_addr),
130 &data->disconnected.reason_code);
131 event_data->data_len = sizeof(data->disconnected);
132 copy_mac_addr(tmp_mac_addr, data->disconnected.bssid);
133 break;
134 case SUPPLICANT_EVENT_ASSOC_REJECT:
135 /* TODO */
136 break;
137 case SUPPLICANT_EVENT_AUTH_REJECT:
138 ret = sscanf(event_no_prefix, MACSTR
139 " auth_type=%u auth_transaction=%u status_code=%u",
140 MACADDR2STR(tmp_mac_addr),
141 &data->auth_reject.auth_type,
142 &data->auth_reject.auth_transaction,
143 &data->auth_reject.status_code);
144 event_data->data_len = sizeof(data->auth_reject);
145 copy_mac_addr(tmp_mac_addr, data->auth_reject.bssid);
146 break;
147 case SUPPLICANT_EVENT_SSID_TEMP_DISABLED:
148 ret = sscanf(event_no_prefix,
149 "id=%d ssid=%s auth_failures=%u duration=%d reason=%s",
150 &data->temp_disabled.id, data->temp_disabled.ssid,
151 &data->temp_disabled.auth_failures,
152 &data->temp_disabled.duration,
153 data->temp_disabled.reason_code);
154 event_data->data_len = sizeof(data->temp_disabled);
155 break;
156 case SUPPLICANT_EVENT_SSID_REENABLED:
157 ret = sscanf(event_no_prefix,
158 "id=%d ssid=%s", &data->reenabled.id,
159 data->reenabled.ssid);
160 event_data->data_len = sizeof(data->reenabled);
161 break;
162 case SUPPLICANT_EVENT_BSS_ADDED:
163 ret = sscanf(event_no_prefix, "%u "MACSTR,
164 &data->bss_added.id,
165 MACADDR2STR(tmp_mac_addr));
166 copy_mac_addr(tmp_mac_addr, data->bss_added.bssid);
167 event_data->data_len = sizeof(data->bss_added);
168 break;
169 case SUPPLICANT_EVENT_BSS_REMOVED:
170 ret = sscanf(event_no_prefix,
171 "%u "MACSTR,
172 &data->bss_removed.id,
173 MACADDR2STR(tmp_mac_addr));
174 event_data->data_len = sizeof(data->bss_removed);
175 copy_mac_addr(tmp_mac_addr, data->bss_removed.bssid);
176 break;
177 case SUPPLICANT_EVENT_TERMINATING:
178 case SUPPLICANT_EVENT_SCAN_STARTED:
179 case SUPPLICANT_EVENT_SCAN_RESULTS:
180 case SUPPLICANT_EVENT_SCAN_FAILED:
181 case SUPPLICANT_EVENT_NETWORK_NOT_FOUND:
182 case SUPPLICANT_EVENT_NETWORK_ADDED:
183 case SUPPLICANT_EVENT_NETWORK_REMOVED:
184 strncpy(data->supplicant_event_str, event_info.event_str,
185 sizeof(data->supplicant_event_str) - 1);
186 event_data->data_len = strlen(data->supplicant_event_str) + 1;
187 case SUPPLICANT_EVENT_DSCP_POLICY:
188 /* TODO */
189 break;
190 default:
191 break;
192 }
193
194 if (ret <= 0) {
195 wpa_printf(MSG_ERROR, "%s Parse failed: %s",
196 event_info.event_str, strerror(errno));
197 }
198
199 return ret;
200 }
201
supplicant_send_wifi_mgmt_conn_event(void * ctx,int status_code)202 int supplicant_send_wifi_mgmt_conn_event(void *ctx, int status_code)
203 {
204 struct wpa_supplicant *wpa_s = ctx;
205 int status = wpas_to_wifi_mgmt_conn_status(status_code);
206 enum net_event_wifi_cmd event;
207
208 if (!wpa_s || !wpa_s->current_ssid) {
209 return -EINVAL;
210 }
211
212 if (wpa_s->current_ssid->mode == WPAS_MODE_AP) {
213 event = NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT;
214 } else {
215 event = NET_EVENT_WIFI_CMD_CONNECT_RESULT;
216 }
217
218 return supplicant_send_wifi_mgmt_event(wpa_s->ifname,
219 event,
220 (void *)&status,
221 sizeof(int));
222 }
223
supplicant_send_wifi_mgmt_disc_event(void * ctx,int reason_code)224 int supplicant_send_wifi_mgmt_disc_event(void *ctx, int reason_code)
225 {
226 struct wpa_supplicant *wpa_s = ctx;
227 int status = wpas_to_wifi_mgmt_disconn_status(reason_code);
228 enum net_event_wifi_cmd event;
229
230 if (!wpa_s || !wpa_s->current_ssid) {
231 return -EINVAL;
232 }
233
234 if (wpa_s->wpa_state >= WPA_COMPLETED) {
235 if (wpa_s->current_ssid->mode == WPAS_MODE_AP) {
236 event = NET_EVENT_WIFI_CMD_AP_DISABLE_RESULT;
237 } else {
238 event = NET_EVENT_WIFI_CMD_DISCONNECT_RESULT;
239 }
240 } else {
241 if (wpa_s->current_ssid->mode == WPAS_MODE_AP) {
242 event = NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT;
243 } else {
244 event = NET_EVENT_WIFI_CMD_CONNECT_RESULT;
245 }
246 }
247
248 return supplicant_send_wifi_mgmt_event(wpa_s->ifname,
249 event,
250 (void *)&status,
251 sizeof(int));
252 }
253
254 #ifdef CONFIG_AP
255 #ifdef CONFIG_WIFI_NM_HOSTAPD_AP
get_sta_link_mode(struct hostapd_iface * iface,struct sta_info * sta)256 static enum wifi_link_mode get_sta_link_mode(struct hostapd_iface *iface, struct sta_info *sta)
257 #else
258 static enum wifi_link_mode get_sta_link_mode(struct wpa_supplicant *wpa_s, struct sta_info *sta)
259 #endif
260 {
261 if (sta->flags & WLAN_STA_HE) {
262 return WIFI_6;
263 } else if (sta->flags & WLAN_STA_VHT) {
264 return WIFI_5;
265 } else if (sta->flags & WLAN_STA_HT) {
266 return WIFI_4;
267 #ifndef CONFIG_WIFI_NM_HOSTAPD_AP
268 } else if (sta->flags & WLAN_STA_NONERP) {
269 return WIFI_1;
270 } else if (wpa_s->assoc_freq > 4000) {
271 return WIFI_2;
272 } else if (wpa_s->assoc_freq > 2000) {
273 return WIFI_3;
274 #else
275 } else if ((sta->flags & WLAN_STA_NONERP) ||
276 (iface->current_mode->mode == HOSTAPD_MODE_IEEE80211B)) {
277 return WIFI_1;
278 } else if (iface->freq > 4000) {
279 return WIFI_2;
280 } else if (iface->freq > 2000) {
281 return WIFI_3;
282 #endif
283 } else {
284 return WIFI_LINK_MODE_UNKNOWN;
285 }
286 }
287
288 #ifdef CONFIG_WIFI_NM_HOSTAPD_AP
is_twt_capable(struct hostapd_iface * iface,struct sta_info * sta)289 static bool is_twt_capable(struct hostapd_iface *iface, struct sta_info *sta)
290 {
291 #ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_11AX
292 return hostapd_get_he_twt_responder(iface->bss[0], IEEE80211_MODE_AP);
293 #else
294 return false;
295 #endif
296 }
297 #else
is_twt_capable(struct wpa_supplicant * wpa_s,struct sta_info * sta)298 static bool is_twt_capable(struct wpa_supplicant *wpa_s, struct sta_info *sta)
299 {
300 #ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_11AX
301 return hostapd_get_he_twt_responder(wpa_s->ap_iface->bss[0], IEEE80211_MODE_AP);
302 #else
303 return false;
304 #endif
305 }
306 #endif
307
supplicant_send_wifi_mgmt_ap_status(void * ctx,enum net_event_wifi_cmd event,enum wifi_ap_status ap_status)308 int supplicant_send_wifi_mgmt_ap_status(void *ctx,
309 enum net_event_wifi_cmd event,
310 enum wifi_ap_status ap_status)
311 {
312 #ifdef CONFIG_WIFI_NM_HOSTAPD_AP
313 struct hostapd_iface *iface = ctx;
314 char *ifname = iface->conf->bss[0]->iface;
315 #else
316 struct wpa_supplicant *wpa_s = ctx;
317 char *ifname = wpa_s->ifname;
318 #endif
319 int status = ap_status;
320
321 return supplicant_send_wifi_mgmt_event(ifname,
322 event,
323 (void *)&status,
324 sizeof(int));
325 }
326
supplicant_send_wifi_mgmt_ap_sta_event(void * ctx,enum net_event_wifi_cmd event,void * data)327 int supplicant_send_wifi_mgmt_ap_sta_event(void *ctx,
328 enum net_event_wifi_cmd event,
329 void *data)
330 {
331 struct sta_info *sta = data;
332 #ifdef CONFIG_WIFI_NM_HOSTAPD_AP
333 struct hostapd_iface *ap_ctx = ctx;
334 char *ifname = ap_ctx->bss[0]->conf->iface;
335 #else
336 struct wpa_supplicant *ap_ctx = ctx;
337 char *ifname = ap_ctx->ifname;
338 #endif
339 struct wifi_ap_sta_info sta_info = { 0 };
340
341 if (!ap_ctx || !sta) {
342 return -EINVAL;
343 }
344
345 memcpy(sta_info.mac, sta->addr, sizeof(sta_info.mac));
346
347 if (event == NET_EVENT_WIFI_CMD_AP_STA_CONNECTED) {
348 sta_info.link_mode = get_sta_link_mode(ap_ctx, sta);
349 sta_info.twt_capable = is_twt_capable(ap_ctx, sta);
350 }
351
352 return supplicant_send_wifi_mgmt_event(ifname,
353 event,
354 (void *)&sta_info,
355 sizeof(sta_info));
356 }
357 #endif /* CONFIG_AP */
358
supplicant_send_wifi_mgmt_event(const char * ifname,enum net_event_wifi_cmd event,void * supplicant_status,size_t len)359 int supplicant_send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event,
360 void *supplicant_status, size_t len)
361 {
362 struct net_if *iface = net_if_get_by_index(net_if_get_by_name(ifname));
363 union supplicant_event_data data;
364 struct supplicant_int_event_data event_data;
365
366 if (!iface) {
367 wpa_printf(MSG_ERROR, "Could not find iface for %s", ifname);
368 return -ENODEV;
369 }
370
371 switch (event) {
372 case NET_EVENT_WIFI_CMD_CONNECT_RESULT:
373 wifi_mgmt_raise_connect_result_event(
374 iface,
375 *(int *)supplicant_status);
376 break;
377 case NET_EVENT_WIFI_CMD_DISCONNECT_RESULT:
378 wifi_mgmt_raise_disconnect_result_event(
379 iface,
380 *(int *)supplicant_status);
381 break;
382 #ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_ROAMING
383 case NET_EVENT_WIFI_CMD_SIGNAL_CHANGE:
384 net_mgmt_event_notify_with_info(NET_EVENT_WIFI_SIGNAL_CHANGE,
385 iface, NULL, 0);
386 break;
387 case NET_EVENT_WIFI_CMD_NEIGHBOR_REP_RECEIVED:
388 wifi_mgmt_raise_neighbor_rep_recv_event(
389 iface,
390 (char *)supplicant_status, len);
391 break;
392 case NET_EVENT_WIFI_CMD_NEIGHBOR_REP_COMPLETE:
393 net_mgmt_event_notify_with_info(NET_EVENT_WIFI_NEIGHBOR_REP_COMP,
394 iface, NULL, 0);
395 break;
396 #endif
397 #ifdef CONFIG_AP
398 case NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT:
399 wifi_mgmt_raise_ap_enable_result_event(iface,
400 *(int *)supplicant_status);
401 break;
402 case NET_EVENT_WIFI_CMD_AP_DISABLE_RESULT:
403 wifi_mgmt_raise_ap_disable_result_event(iface,
404 *(int *)supplicant_status);
405 break;
406 case NET_EVENT_WIFI_CMD_AP_STA_CONNECTED:
407 wifi_mgmt_raise_ap_sta_connected_event(iface,
408 (struct wifi_ap_sta_info *)supplicant_status);
409 break;
410 case NET_EVENT_WIFI_CMD_AP_STA_DISCONNECTED:
411 wifi_mgmt_raise_ap_sta_disconnected_event(iface,
412 (struct wifi_ap_sta_info *)supplicant_status);
413 break;
414 #endif /* CONFIG_AP */
415 case NET_EVENT_WIFI_CMD_SUPPLICANT:
416 event_data.data = &data;
417 if (supplicant_process_status(&event_data, (char *)supplicant_status) > 0) {
418 net_mgmt_event_notify_with_info(NET_EVENT_SUPPLICANT_INT_EVENT,
419 iface, &event_data, sizeof(event_data));
420 }
421 break;
422 default:
423 wpa_printf(MSG_ERROR, "Unsupported event %d", event);
424 return -EINVAL;
425 }
426
427 return 0;
428 }
429
supplicant_generate_state_event(const char * ifname,enum net_event_supplicant_cmd event,int status)430 int supplicant_generate_state_event(const char *ifname,
431 enum net_event_supplicant_cmd event,
432 int status)
433 {
434 struct net_if *iface;
435
436 iface = net_if_get_by_index(net_if_get_by_name(ifname));
437 if (!iface) {
438 wpa_printf(MSG_ERROR, "Could not find iface for %s", ifname);
439 return -ENODEV;
440 }
441
442 switch (event) {
443 case NET_EVENT_SUPPLICANT_CMD_READY:
444 net_mgmt_event_notify(NET_EVENT_SUPPLICANT_READY, iface);
445 break;
446 case NET_EVENT_SUPPLICANT_CMD_NOT_READY:
447 net_mgmt_event_notify(NET_EVENT_SUPPLICANT_NOT_READY, iface);
448 break;
449 case NET_EVENT_SUPPLICANT_CMD_IFACE_ADDED:
450 net_mgmt_event_notify(NET_EVENT_SUPPLICANT_IFACE_ADDED, iface);
451 break;
452 case NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVING:
453 net_mgmt_event_notify(NET_EVENT_SUPPLICANT_IFACE_REMOVING, iface);
454 break;
455 case NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVED:
456 net_mgmt_event_notify_with_info(NET_EVENT_SUPPLICANT_IFACE_REMOVED,
457 iface, &status, sizeof(status));
458 break;
459 default:
460 wpa_printf(MSG_ERROR, "Unsupported event %d", event);
461 return -EINVAL;
462 }
463
464 return 0;
465 }
466
467 #if defined(CONFIG_WIFI_NM_HOSTAPD_AP) && defined(CONFIG_WIFI_NM_WPA_SUPPLICANT_DPP)
hostapd_handle_dpp_event(void * ctx,char * buf,size_t len)468 void hostapd_handle_dpp_event(void *ctx, char *buf, size_t len)
469 {
470 struct hostapd_data *hapd = (struct hostapd_data *)ctx;
471
472 if (hapd == NULL) {
473 return;
474 }
475
476 struct hostapd_bss_config *conf = hapd->conf;
477
478 if (conf == NULL || !(conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP)) {
479 return;
480 }
481
482 /* check hostapd */
483 if (!strncmp(buf, DPP_EVENT_CONNECTOR, sizeof(DPP_EVENT_CONNECTOR) - 1)) {
484 if (conf->dpp_connector) {
485 os_free(conf->dpp_connector);
486 }
487
488 conf->dpp_connector = os_strdup(buf + sizeof(DPP_EVENT_CONNECTOR) - 1);
489 } else if (!strncmp(buf, DPP_EVENT_C_SIGN_KEY, sizeof(DPP_EVENT_C_SIGN_KEY) - 1)) {
490 if (conf->dpp_csign) {
491 wpabuf_free(conf->dpp_csign);
492 }
493
494 conf->dpp_csign = wpabuf_parse_bin(buf + sizeof(DPP_EVENT_C_SIGN_KEY) - 1);
495 } else if (!strncmp(buf, DPP_EVENT_NET_ACCESS_KEY, sizeof(DPP_EVENT_NET_ACCESS_KEY) - 1)) {
496 if (conf->dpp_netaccesskey) {
497 wpabuf_free(conf->dpp_netaccesskey);
498 }
499
500 conf->dpp_netaccesskey =
501 wpabuf_parse_bin(buf + sizeof(DPP_EVENT_NET_ACCESS_KEY) - 1);
502 }
503 }
504 #endif /* CONFIG_WIFI_NM_HOSTAPD_AP && CONFIG_WIFI_NM_WPA_SUPPLICANT_DPP */
505