]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * hostapd / UNIX domain socket -based control interface | |
c6f2eceb | 3 | * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi> |
6fc6879b | 4 | * |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
6fc6879b JM |
7 | */ |
8 | ||
6226e38d | 9 | #include "utils/includes.h" |
6fc6879b JM |
10 | |
11 | #ifndef CONFIG_NATIVE_WINDOWS | |
12 | ||
4a6cc862 JM |
13 | #ifdef CONFIG_TESTING_OPTIONS |
14 | #include <net/ethernet.h> | |
15 | #include <netinet/ip.h> | |
16 | #endif /* CONFIG_TESTING_OPTIONS */ | |
17 | ||
6fc6879b JM |
18 | #include <sys/un.h> |
19 | #include <sys/stat.h> | |
75864b7f | 20 | #include <stddef.h> |
6fc6879b | 21 | |
56885eec JD |
22 | #ifdef CONFIG_CTRL_IFACE_UDP |
23 | #include <netdb.h> | |
24 | #endif /* CONFIG_CTRL_IFACE_UDP */ | |
25 | ||
6226e38d JM |
26 | #include "utils/common.h" |
27 | #include "utils/eloop.h" | |
50a17a76 | 28 | #include "utils/module_tests.h" |
acec8d32 | 29 | #include "common/version.h" |
81f4f619 | 30 | #include "common/ieee802_11_defs.h" |
89b781bc | 31 | #include "common/ctrl_iface_common.h" |
4b07484c | 32 | #ifdef CONFIG_DPP |
60239f60 | 33 | #include "common/dpp.h" |
4b07484c | 34 | #endif /* CONFIG_DPP */ |
cc79e06f | 35 | #include "common/wpa_ctrl.h" |
a1651451 | 36 | #include "crypto/tls.h" |
1057d78e | 37 | #include "drivers/driver.h" |
cfb5c08f | 38 | #include "eapol_auth/eapol_auth_sm.h" |
6fc6879b | 39 | #include "radius/radius_client.h" |
4c03a2b3 | 40 | #include "radius/radius_server.h" |
4a6cc862 | 41 | #include "l2_packet/l2_packet.h" |
1057d78e | 42 | #include "ap/hostapd.h" |
6226e38d | 43 | #include "ap/ap_config.h" |
1057d78e | 44 | #include "ap/ieee802_1x.h" |
6226e38d | 45 | #include "ap/wpa_auth.h" |
1057d78e JM |
46 | #include "ap/ieee802_11.h" |
47 | #include "ap/sta_info.h" | |
32da61d9 | 48 | #include "ap/wps_hostapd.h" |
0e2d35c6 | 49 | #include "ap/ctrl_iface_ap.h" |
51e2a27a | 50 | #include "ap/ap_drv_ops.h" |
3fb17a95 | 51 | #include "ap/hs20.h" |
2025cad9 | 52 | #include "ap/wnm_ap.h" |
901d1fe1 | 53 | #include "ap/wpa_auth.h" |
9ff8dda1 | 54 | #include "ap/beacon.h" |
9b4b2264 | 55 | #include "ap/neighbor_db.h" |
f4f185a2 | 56 | #include "ap/rrm.h" |
9c2b8204 | 57 | #include "ap/dpp_hostapd.h" |
b4e34f2f | 58 | #include "wps/wps_defs.h" |
3981cb3c | 59 | #include "wps/wps.h" |
8e9a8b0f | 60 | #include "fst/fst_ctrl_iface.h" |
31b79e11 | 61 | #include "config_file.h" |
6fc6879b | 62 | #include "ctrl_iface.h" |
6fc6879b JM |
63 | |
64 | ||
748febf3 AN |
65 | #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256 |
66 | ||
56885eec JD |
67 | #ifdef CONFIG_CTRL_IFACE_UDP |
68 | #define COOKIE_LEN 8 | |
69 | static unsigned char cookie[COOKIE_LEN]; | |
70 | static unsigned char gcookie[COOKIE_LEN]; | |
71 | #define HOSTAPD_CTRL_IFACE_PORT 8877 | |
72 | #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50 | |
73 | #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878 | |
74 | #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50 | |
75 | #endif /* CONFIG_CTRL_IFACE_UDP */ | |
6fc6879b | 76 | |
42d16805 | 77 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
ee1e3f57 | 78 | enum wpa_msg_type type, |
42d16805 JM |
79 | const char *buf, size_t len); |
80 | ||
81 | ||
6fc6879b | 82 | static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd, |
acf57fae | 83 | struct sockaddr_storage *from, |
cc79e06f | 84 | socklen_t fromlen, const char *input) |
6fc6879b | 85 | { |
cc79e06f | 86 | return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input); |
6fc6879b JM |
87 | } |
88 | ||
89 | ||
90 | static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd, | |
acf57fae | 91 | struct sockaddr_storage *from, |
6fc6879b JM |
92 | socklen_t fromlen) |
93 | { | |
89b781bc | 94 | return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen); |
6fc6879b JM |
95 | } |
96 | ||
97 | ||
98 | static int hostapd_ctrl_iface_level(struct hostapd_data *hapd, | |
acf57fae | 99 | struct sockaddr_storage *from, |
6fc6879b JM |
100 | socklen_t fromlen, |
101 | char *level) | |
102 | { | |
89b781bc | 103 | return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level); |
6fc6879b JM |
104 | } |
105 | ||
106 | ||
6fc6879b JM |
107 | static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd, |
108 | const char *txtaddr) | |
109 | { | |
110 | u8 addr[ETH_ALEN]; | |
111 | struct sta_info *sta; | |
112 | ||
113 | wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr); | |
114 | ||
115 | if (hwaddr_aton(txtaddr, addr)) | |
116 | return -1; | |
117 | ||
118 | sta = ap_get_sta(hapd, addr); | |
119 | if (sta) | |
120 | return 0; | |
121 | ||
122 | wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface " | |
123 | "notification", MAC2STR(addr)); | |
124 | sta = ap_sta_add(hapd, addr); | |
125 | if (sta == NULL) | |
126 | return -1; | |
127 | ||
128 | hostapd_new_assoc_sta(hapd, sta, 0); | |
6fc6879b JM |
129 | return 0; |
130 | } | |
131 | ||
132 | ||
88b4b424 | 133 | #ifdef CONFIG_IEEE80211W |
fe6bdb77 | 134 | #ifdef NEED_AP_MLME |
88b4b424 JM |
135 | static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd, |
136 | const char *txtaddr) | |
137 | { | |
138 | u8 addr[ETH_ALEN]; | |
139 | u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN]; | |
140 | ||
141 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr); | |
142 | ||
f5455a2d JM |
143 | if (hwaddr_aton(txtaddr, addr) || |
144 | os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) | |
88b4b424 JM |
145 | return -1; |
146 | ||
88b4b424 JM |
147 | ieee802_11_send_sa_query_req(hapd, addr, trans_id); |
148 | ||
149 | return 0; | |
150 | } | |
fe6bdb77 | 151 | #endif /* NEED_AP_MLME */ |
88b4b424 JM |
152 | #endif /* CONFIG_IEEE80211W */ |
153 | ||
154 | ||
ad08c363 JM |
155 | #ifdef CONFIG_WPS |
156 | static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt) | |
157 | { | |
158 | char *pin = os_strchr(txt, ' '); | |
077a781f JM |
159 | char *timeout_txt; |
160 | int timeout; | |
31fcea93 JM |
161 | u8 addr_buf[ETH_ALEN], *addr = NULL; |
162 | char *pos; | |
077a781f | 163 | |
ad08c363 JM |
164 | if (pin == NULL) |
165 | return -1; | |
166 | *pin++ = '\0'; | |
077a781f JM |
167 | |
168 | timeout_txt = os_strchr(pin, ' '); | |
169 | if (timeout_txt) { | |
170 | *timeout_txt++ = '\0'; | |
171 | timeout = atoi(timeout_txt); | |
31fcea93 JM |
172 | pos = os_strchr(timeout_txt, ' '); |
173 | if (pos) { | |
174 | *pos++ = '\0'; | |
175 | if (hwaddr_aton(pos, addr_buf) == 0) | |
176 | addr = addr_buf; | |
177 | } | |
077a781f JM |
178 | } else |
179 | timeout = 0; | |
180 | ||
31fcea93 | 181 | return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout); |
ad08c363 | 182 | } |
46bdb83a MH |
183 | |
184 | ||
3981cb3c JM |
185 | static int hostapd_ctrl_iface_wps_check_pin( |
186 | struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen) | |
187 | { | |
188 | char pin[9]; | |
189 | size_t len; | |
190 | char *pos; | |
191 | int ret; | |
192 | ||
193 | wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN", | |
194 | (u8 *) cmd, os_strlen(cmd)); | |
195 | for (pos = cmd, len = 0; *pos != '\0'; pos++) { | |
196 | if (*pos < '0' || *pos > '9') | |
197 | continue; | |
198 | pin[len++] = *pos; | |
199 | if (len == 9) { | |
200 | wpa_printf(MSG_DEBUG, "WPS: Too long PIN"); | |
201 | return -1; | |
202 | } | |
203 | } | |
204 | if (len != 4 && len != 8) { | |
205 | wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len); | |
206 | return -1; | |
207 | } | |
208 | pin[len] = '\0'; | |
209 | ||
210 | if (len == 8) { | |
211 | unsigned int pin_val; | |
212 | pin_val = atoi(pin); | |
213 | if (!wps_pin_valid(pin_val)) { | |
214 | wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit"); | |
215 | ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n"); | |
d85e1fc8 | 216 | if (os_snprintf_error(buflen, ret)) |
3981cb3c JM |
217 | return -1; |
218 | return ret; | |
219 | } | |
220 | } | |
221 | ||
222 | ret = os_snprintf(buf, buflen, "%s", pin); | |
d85e1fc8 | 223 | if (os_snprintf_error(buflen, ret)) |
3981cb3c JM |
224 | return -1; |
225 | ||
226 | return ret; | |
227 | } | |
228 | ||
229 | ||
bb45b6d7 JM |
230 | #ifdef CONFIG_WPS_NFC |
231 | static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd, | |
232 | char *pos) | |
233 | { | |
234 | size_t len; | |
235 | struct wpabuf *buf; | |
236 | int ret; | |
237 | ||
238 | len = os_strlen(pos); | |
239 | if (len & 0x01) | |
240 | return -1; | |
241 | len /= 2; | |
242 | ||
243 | buf = wpabuf_alloc(len); | |
244 | if (buf == NULL) | |
245 | return -1; | |
246 | if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) { | |
247 | wpabuf_free(buf); | |
248 | return -1; | |
249 | } | |
250 | ||
251 | ret = hostapd_wps_nfc_tag_read(hapd, buf); | |
252 | wpabuf_free(buf); | |
253 | ||
254 | return ret; | |
255 | } | |
3cf7a59d JM |
256 | |
257 | ||
258 | static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd, | |
259 | char *cmd, char *reply, | |
260 | size_t max_len) | |
261 | { | |
262 | int ndef; | |
263 | struct wpabuf *buf; | |
264 | int res; | |
265 | ||
266 | if (os_strcmp(cmd, "WPS") == 0) | |
267 | ndef = 0; | |
268 | else if (os_strcmp(cmd, "NDEF") == 0) | |
269 | ndef = 1; | |
270 | else | |
271 | return -1; | |
272 | ||
273 | buf = hostapd_wps_nfc_config_token(hapd, ndef); | |
274 | if (buf == NULL) | |
275 | return -1; | |
276 | ||
277 | res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), | |
278 | wpabuf_len(buf)); | |
279 | reply[res++] = '\n'; | |
280 | reply[res] = '\0'; | |
281 | ||
282 | wpabuf_free(buf); | |
283 | ||
284 | return res; | |
285 | } | |
ffdaa05a JM |
286 | |
287 | ||
288 | static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd, | |
289 | char *reply, size_t max_len, | |
290 | int ndef) | |
291 | { | |
292 | struct wpabuf *buf; | |
293 | int res; | |
294 | ||
295 | buf = hostapd_wps_nfc_token_gen(hapd, ndef); | |
296 | if (buf == NULL) | |
297 | return -1; | |
298 | ||
299 | res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), | |
300 | wpabuf_len(buf)); | |
301 | reply[res++] = '\n'; | |
302 | reply[res] = '\0'; | |
303 | ||
304 | wpabuf_free(buf); | |
305 | ||
306 | return res; | |
307 | } | |
308 | ||
309 | ||
310 | static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd, | |
311 | char *cmd, char *reply, | |
312 | size_t max_len) | |
313 | { | |
314 | if (os_strcmp(cmd, "WPS") == 0) | |
315 | return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply, | |
316 | max_len, 0); | |
317 | ||
318 | if (os_strcmp(cmd, "NDEF") == 0) | |
319 | return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply, | |
320 | max_len, 1); | |
321 | ||
322 | if (os_strcmp(cmd, "enable") == 0) | |
323 | return hostapd_wps_nfc_token_enable(hapd); | |
324 | ||
325 | if (os_strcmp(cmd, "disable") == 0) { | |
326 | hostapd_wps_nfc_token_disable(hapd); | |
327 | return 0; | |
328 | } | |
329 | ||
330 | return -1; | |
331 | } | |
6772a90a JM |
332 | |
333 | ||
334 | static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd, | |
335 | char *cmd, char *reply, | |
336 | size_t max_len) | |
337 | { | |
338 | struct wpabuf *buf; | |
339 | int res; | |
340 | char *pos; | |
341 | int ndef; | |
342 | ||
343 | pos = os_strchr(cmd, ' '); | |
344 | if (pos == NULL) | |
345 | return -1; | |
346 | *pos++ = '\0'; | |
347 | ||
348 | if (os_strcmp(cmd, "WPS") == 0) | |
349 | ndef = 0; | |
350 | else if (os_strcmp(cmd, "NDEF") == 0) | |
351 | ndef = 1; | |
352 | else | |
353 | return -1; | |
354 | ||
355 | if (os_strcmp(pos, "WPS-CR") == 0) | |
356 | buf = hostapd_wps_nfc_hs_cr(hapd, ndef); | |
357 | else | |
358 | buf = NULL; | |
359 | if (buf == NULL) | |
360 | return -1; | |
361 | ||
362 | res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf), | |
363 | wpabuf_len(buf)); | |
364 | reply[res++] = '\n'; | |
365 | reply[res] = '\0'; | |
366 | ||
367 | wpabuf_free(buf); | |
368 | ||
369 | return res; | |
370 | } | |
371 | ||
e4758827 JM |
372 | |
373 | static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd, | |
374 | char *cmd) | |
375 | { | |
3189ca05 JM |
376 | size_t len; |
377 | struct wpabuf *req, *sel; | |
378 | int ret; | |
379 | char *pos, *role, *type, *pos2; | |
380 | ||
381 | role = cmd; | |
382 | pos = os_strchr(role, ' '); | |
383 | if (pos == NULL) | |
384 | return -1; | |
385 | *pos++ = '\0'; | |
386 | ||
387 | type = pos; | |
388 | pos = os_strchr(type, ' '); | |
389 | if (pos == NULL) | |
390 | return -1; | |
391 | *pos++ = '\0'; | |
392 | ||
393 | pos2 = os_strchr(pos, ' '); | |
394 | if (pos2 == NULL) | |
395 | return -1; | |
396 | *pos2++ = '\0'; | |
397 | ||
398 | len = os_strlen(pos); | |
399 | if (len & 0x01) | |
400 | return -1; | |
401 | len /= 2; | |
402 | ||
403 | req = wpabuf_alloc(len); | |
404 | if (req == NULL) | |
405 | return -1; | |
406 | if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) { | |
407 | wpabuf_free(req); | |
408 | return -1; | |
409 | } | |
410 | ||
411 | len = os_strlen(pos2); | |
412 | if (len & 0x01) { | |
413 | wpabuf_free(req); | |
414 | return -1; | |
415 | } | |
416 | len /= 2; | |
417 | ||
418 | sel = wpabuf_alloc(len); | |
419 | if (sel == NULL) { | |
420 | wpabuf_free(req); | |
421 | return -1; | |
422 | } | |
423 | if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) { | |
424 | wpabuf_free(req); | |
425 | wpabuf_free(sel); | |
426 | return -1; | |
427 | } | |
428 | ||
429 | if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) { | |
430 | ret = hostapd_wps_nfc_report_handover(hapd, req, sel); | |
431 | } else { | |
432 | wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover " | |
433 | "reported: role=%s type=%s", role, type); | |
434 | ret = -1; | |
435 | } | |
436 | wpabuf_free(req); | |
437 | wpabuf_free(sel); | |
438 | ||
439 | return ret; | |
e4758827 JM |
440 | } |
441 | ||
bb45b6d7 JM |
442 | #endif /* CONFIG_WPS_NFC */ |
443 | ||
444 | ||
5a1cc30f JM |
445 | static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt, |
446 | char *buf, size_t buflen) | |
447 | { | |
448 | int timeout = 300; | |
449 | char *pos; | |
450 | const char *pin_txt; | |
451 | ||
452 | pos = os_strchr(txt, ' '); | |
453 | if (pos) | |
454 | *pos++ = '\0'; | |
455 | ||
456 | if (os_strcmp(txt, "disable") == 0) { | |
457 | hostapd_wps_ap_pin_disable(hapd); | |
458 | return os_snprintf(buf, buflen, "OK\n"); | |
459 | } | |
460 | ||
461 | if (os_strcmp(txt, "random") == 0) { | |
462 | if (pos) | |
463 | timeout = atoi(pos); | |
464 | pin_txt = hostapd_wps_ap_pin_random(hapd, timeout); | |
465 | if (pin_txt == NULL) | |
466 | return -1; | |
467 | return os_snprintf(buf, buflen, "%s", pin_txt); | |
468 | } | |
469 | ||
470 | if (os_strcmp(txt, "get") == 0) { | |
471 | pin_txt = hostapd_wps_ap_pin_get(hapd); | |
472 | if (pin_txt == NULL) | |
473 | return -1; | |
474 | return os_snprintf(buf, buflen, "%s", pin_txt); | |
475 | } | |
476 | ||
477 | if (os_strcmp(txt, "set") == 0) { | |
478 | char *pin; | |
479 | if (pos == NULL) | |
480 | return -1; | |
481 | pin = pos; | |
482 | pos = os_strchr(pos, ' '); | |
483 | if (pos) { | |
484 | *pos++ = '\0'; | |
485 | timeout = atoi(pos); | |
486 | } | |
487 | if (os_strlen(pin) > buflen) | |
488 | return -1; | |
489 | if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0) | |
490 | return -1; | |
491 | return os_snprintf(buf, buflen, "%s", pin); | |
492 | } | |
493 | ||
494 | return -1; | |
495 | } | |
450eddcf JM |
496 | |
497 | ||
498 | static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt) | |
499 | { | |
500 | char *pos; | |
501 | char *ssid, *auth, *encr = NULL, *key = NULL; | |
502 | ||
503 | ssid = txt; | |
504 | pos = os_strchr(txt, ' '); | |
505 | if (!pos) | |
506 | return -1; | |
507 | *pos++ = '\0'; | |
508 | ||
509 | auth = pos; | |
510 | pos = os_strchr(pos, ' '); | |
511 | if (pos) { | |
512 | *pos++ = '\0'; | |
513 | encr = pos; | |
514 | pos = os_strchr(pos, ' '); | |
515 | if (pos) { | |
516 | *pos++ = '\0'; | |
517 | key = pos; | |
518 | } | |
519 | } | |
520 | ||
521 | return hostapd_wps_config_ap(hapd, ssid, auth, encr, key); | |
522 | } | |
3351a384 JM |
523 | |
524 | ||
525 | static const char * pbc_status_str(enum pbc_status status) | |
526 | { | |
527 | switch (status) { | |
528 | case WPS_PBC_STATUS_DISABLE: | |
529 | return "Disabled"; | |
530 | case WPS_PBC_STATUS_ACTIVE: | |
531 | return "Active"; | |
532 | case WPS_PBC_STATUS_TIMEOUT: | |
533 | return "Timed-out"; | |
534 | case WPS_PBC_STATUS_OVERLAP: | |
535 | return "Overlap"; | |
536 | default: | |
537 | return "Unknown"; | |
538 | } | |
539 | } | |
540 | ||
541 | ||
542 | static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd, | |
543 | char *buf, size_t buflen) | |
544 | { | |
545 | int ret; | |
546 | char *pos, *end; | |
547 | ||
548 | pos = buf; | |
549 | end = buf + buflen; | |
550 | ||
551 | ret = os_snprintf(pos, end - pos, "PBC Status: %s\n", | |
552 | pbc_status_str(hapd->wps_stats.pbc_status)); | |
553 | ||
d85e1fc8 | 554 | if (os_snprintf_error(end - pos, ret)) |
3351a384 JM |
555 | return pos - buf; |
556 | pos += ret; | |
557 | ||
558 | ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n", | |
559 | (hapd->wps_stats.status == WPS_STATUS_SUCCESS ? | |
560 | "Success": | |
561 | (hapd->wps_stats.status == WPS_STATUS_FAILURE ? | |
562 | "Failed" : "None"))); | |
563 | ||
d85e1fc8 | 564 | if (os_snprintf_error(end - pos, ret)) |
3351a384 JM |
565 | return pos - buf; |
566 | pos += ret; | |
567 | ||
568 | /* If status == Failure - Add possible Reasons */ | |
569 | if(hapd->wps_stats.status == WPS_STATUS_FAILURE && | |
570 | hapd->wps_stats.failure_reason > 0) { | |
571 | ret = os_snprintf(pos, end - pos, | |
572 | "Failure Reason: %s\n", | |
573 | wps_ei_str(hapd->wps_stats.failure_reason)); | |
574 | ||
d85e1fc8 | 575 | if (os_snprintf_error(end - pos, ret)) |
3351a384 JM |
576 | return pos - buf; |
577 | pos += ret; | |
578 | } | |
579 | ||
580 | if (hapd->wps_stats.status) { | |
581 | ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n", | |
582 | MAC2STR(hapd->wps_stats.peer_addr)); | |
583 | ||
d85e1fc8 | 584 | if (os_snprintf_error(end - pos, ret)) |
3351a384 JM |
585 | return pos - buf; |
586 | pos += ret; | |
587 | } | |
588 | ||
589 | return pos - buf; | |
590 | } | |
591 | ||
ad08c363 JM |
592 | #endif /* CONFIG_WPS */ |
593 | ||
3fb17a95 JM |
594 | #ifdef CONFIG_HS20 |
595 | ||
596 | static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd, | |
597 | const char *cmd) | |
598 | { | |
599 | u8 addr[ETH_ALEN]; | |
600 | const char *url; | |
601 | ||
602 | if (hwaddr_aton(cmd, addr)) | |
603 | return -1; | |
604 | url = cmd + 17; | |
605 | if (*url == '\0') { | |
606 | url = NULL; | |
607 | } else { | |
608 | if (*url != ' ') | |
609 | return -1; | |
610 | url++; | |
611 | if (*url == '\0') | |
612 | url = NULL; | |
613 | } | |
614 | ||
615 | return hs20_send_wnm_notification(hapd, addr, 1, url); | |
616 | } | |
617 | ||
8e1146d9 JM |
618 | |
619 | static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd, | |
620 | const char *cmd) | |
621 | { | |
622 | u8 addr[ETH_ALEN]; | |
623 | int code, reauth_delay, ret; | |
624 | const char *pos; | |
625 | size_t url_len; | |
626 | struct wpabuf *req; | |
627 | ||
628 | /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */ | |
629 | if (hwaddr_aton(cmd, addr)) | |
630 | return -1; | |
631 | ||
632 | pos = os_strchr(cmd, ' '); | |
633 | if (pos == NULL) | |
634 | return -1; | |
635 | pos++; | |
636 | code = atoi(pos); | |
637 | ||
638 | pos = os_strchr(pos, ' '); | |
639 | if (pos == NULL) | |
640 | return -1; | |
641 | pos++; | |
642 | reauth_delay = atoi(pos); | |
643 | ||
644 | url_len = 0; | |
645 | pos = os_strchr(pos, ' '); | |
646 | if (pos) { | |
647 | pos++; | |
648 | url_len = os_strlen(pos); | |
649 | } | |
650 | ||
651 | req = wpabuf_alloc(4 + url_len); | |
652 | if (req == NULL) | |
653 | return -1; | |
654 | wpabuf_put_u8(req, code); | |
655 | wpabuf_put_le16(req, reauth_delay); | |
656 | wpabuf_put_u8(req, url_len); | |
657 | if (pos) | |
658 | wpabuf_put_data(req, pos, url_len); | |
659 | ||
660 | wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR | |
661 | " to indicate imminent deauthentication (code=%d " | |
662 | "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay); | |
663 | ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req); | |
664 | wpabuf_free(req); | |
665 | return ret; | |
666 | } | |
667 | ||
3fb17a95 JM |
668 | #endif /* CONFIG_HS20 */ |
669 | ||
ad08c363 | 670 | |
c551700f KP |
671 | #ifdef CONFIG_INTERWORKING |
672 | ||
673 | static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd, | |
674 | const char *cmd) | |
675 | { | |
676 | u8 qos_map_set[16 + 2 * 21], count = 0; | |
677 | const char *pos = cmd; | |
678 | int val, ret; | |
679 | ||
680 | for (;;) { | |
681 | if (count == sizeof(qos_map_set)) { | |
682 | wpa_printf(MSG_ERROR, "Too many qos_map_set parameters"); | |
683 | return -1; | |
684 | } | |
685 | ||
686 | val = atoi(pos); | |
687 | if (val < 0 || val > 255) { | |
688 | wpa_printf(MSG_INFO, "Invalid QoS Map Set"); | |
689 | return -1; | |
690 | } | |
691 | ||
692 | qos_map_set[count++] = val; | |
693 | pos = os_strchr(pos, ','); | |
694 | if (!pos) | |
695 | break; | |
696 | pos++; | |
697 | } | |
698 | ||
699 | if (count < 16 || count & 1) { | |
700 | wpa_printf(MSG_INFO, "Invalid QoS Map Set"); | |
701 | return -1; | |
702 | } | |
703 | ||
704 | ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count); | |
705 | if (ret) { | |
706 | wpa_printf(MSG_INFO, "Failed to set QoS Map Set"); | |
707 | return -1; | |
708 | } | |
709 | ||
710 | os_memcpy(hapd->conf->qos_map_set, qos_map_set, count); | |
711 | hapd->conf->qos_map_set_len = count; | |
712 | ||
713 | return 0; | |
714 | } | |
715 | ||
716 | ||
717 | static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd, | |
718 | const char *cmd) | |
719 | { | |
720 | u8 addr[ETH_ALEN]; | |
721 | struct sta_info *sta; | |
722 | struct wpabuf *buf; | |
723 | u8 *qos_map_set = hapd->conf->qos_map_set; | |
724 | u8 qos_map_set_len = hapd->conf->qos_map_set_len; | |
725 | int ret; | |
726 | ||
727 | if (!qos_map_set_len) { | |
728 | wpa_printf(MSG_INFO, "QoS Map Set is not set"); | |
729 | return -1; | |
730 | } | |
731 | ||
732 | if (hwaddr_aton(cmd, addr)) | |
733 | return -1; | |
734 | ||
735 | sta = ap_get_sta(hapd, addr); | |
736 | if (sta == NULL) { | |
737 | wpa_printf(MSG_DEBUG, "Station " MACSTR " not found " | |
738 | "for QoS Map Configuration message", | |
739 | MAC2STR(addr)); | |
740 | return -1; | |
741 | } | |
742 | ||
743 | if (!sta->qos_map_enabled) { | |
744 | wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate " | |
745 | "support for QoS Map", MAC2STR(addr)); | |
746 | return -1; | |
747 | } | |
748 | ||
749 | buf = wpabuf_alloc(2 + 2 + qos_map_set_len); | |
750 | if (buf == NULL) | |
751 | return -1; | |
752 | ||
753 | wpabuf_put_u8(buf, WLAN_ACTION_QOS); | |
754 | wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG); | |
755 | ||
756 | /* QoS Map Set Element */ | |
757 | wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET); | |
758 | wpabuf_put_u8(buf, qos_map_set_len); | |
759 | wpabuf_put_data(buf, qos_map_set, qos_map_set_len); | |
760 | ||
761 | ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr, | |
762 | wpabuf_head(buf), wpabuf_len(buf)); | |
763 | wpabuf_free(buf); | |
764 | ||
765 | return ret; | |
766 | } | |
767 | ||
768 | #endif /* CONFIG_INTERWORKING */ | |
769 | ||
770 | ||
b5bf84ba | 771 | #ifdef CONFIG_WNM_AP |
2049a875 JM |
772 | |
773 | static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd, | |
774 | const char *cmd) | |
775 | { | |
776 | u8 addr[ETH_ALEN]; | |
2049a875 | 777 | int disassoc_timer; |
8d321a7d | 778 | struct sta_info *sta; |
2049a875 JM |
779 | |
780 | if (hwaddr_aton(cmd, addr)) | |
781 | return -1; | |
782 | if (cmd[17] != ' ') | |
783 | return -1; | |
784 | disassoc_timer = atoi(cmd + 17); | |
785 | ||
8d321a7d JM |
786 | sta = ap_get_sta(hapd, addr); |
787 | if (sta == NULL) { | |
788 | wpa_printf(MSG_DEBUG, "Station " MACSTR | |
789 | " not found for disassociation imminent message", | |
790 | MAC2STR(addr)); | |
2049a875 JM |
791 | return -1; |
792 | } | |
793 | ||
8d321a7d | 794 | return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer); |
2049a875 JM |
795 | } |
796 | ||
797 | ||
71269b37 JM |
798 | static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd, |
799 | const char *cmd) | |
800 | { | |
801 | u8 addr[ETH_ALEN]; | |
d5b559b6 | 802 | const char *url, *timerstr; |
d5b559b6 | 803 | int disassoc_timer; |
2025cad9 | 804 | struct sta_info *sta; |
71269b37 JM |
805 | |
806 | if (hwaddr_aton(cmd, addr)) | |
807 | return -1; | |
d5b559b6 | 808 | |
2025cad9 JM |
809 | sta = ap_get_sta(hapd, addr); |
810 | if (sta == NULL) { | |
811 | wpa_printf(MSG_DEBUG, "Station " MACSTR | |
812 | " not found for ESS disassociation imminent message", | |
813 | MAC2STR(addr)); | |
814 | return -1; | |
815 | } | |
816 | ||
d5b559b6 KP |
817 | timerstr = cmd + 17; |
818 | if (*timerstr != ' ') | |
819 | return -1; | |
820 | timerstr++; | |
821 | disassoc_timer = atoi(timerstr); | |
822 | if (disassoc_timer < 0 || disassoc_timer > 65535) | |
823 | return -1; | |
824 | ||
825 | url = os_strchr(timerstr, ' '); | |
eb4737f6 | 826 | if (url == NULL) |
71269b37 JM |
827 | return -1; |
828 | url++; | |
71269b37 | 829 | |
2025cad9 | 830 | return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer); |
71269b37 JM |
831 | } |
832 | ||
a30dff07 JM |
833 | |
834 | static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd, | |
835 | const char *cmd) | |
836 | { | |
837 | u8 addr[ETH_ALEN]; | |
838 | const char *pos, *end; | |
839 | int disassoc_timer = 0; | |
840 | struct sta_info *sta; | |
841 | u8 req_mode = 0, valid_int = 0x01; | |
842 | u8 bss_term_dur[12]; | |
843 | char *url = NULL; | |
844 | int ret; | |
845 | u8 nei_rep[1000]; | |
e044a9d1 | 846 | int nei_len; |
c0e2a172 AS |
847 | u8 mbo[10]; |
848 | size_t mbo_len = 0; | |
a30dff07 JM |
849 | |
850 | if (hwaddr_aton(cmd, addr)) { | |
851 | wpa_printf(MSG_DEBUG, "Invalid STA MAC address"); | |
852 | return -1; | |
853 | } | |
854 | ||
855 | sta = ap_get_sta(hapd, addr); | |
856 | if (sta == NULL) { | |
857 | wpa_printf(MSG_DEBUG, "Station " MACSTR | |
858 | " not found for BSS TM Request message", | |
859 | MAC2STR(addr)); | |
860 | return -1; | |
861 | } | |
862 | ||
863 | pos = os_strstr(cmd, " disassoc_timer="); | |
864 | if (pos) { | |
865 | pos += 16; | |
866 | disassoc_timer = atoi(pos); | |
867 | if (disassoc_timer < 0 || disassoc_timer > 65535) { | |
868 | wpa_printf(MSG_DEBUG, "Invalid disassoc_timer"); | |
869 | return -1; | |
870 | } | |
871 | } | |
872 | ||
873 | pos = os_strstr(cmd, " valid_int="); | |
874 | if (pos) { | |
875 | pos += 11; | |
876 | valid_int = atoi(pos); | |
877 | } | |
878 | ||
879 | pos = os_strstr(cmd, " bss_term="); | |
880 | if (pos) { | |
881 | pos += 10; | |
882 | req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED; | |
883 | /* TODO: TSF configurable/learnable */ | |
884 | bss_term_dur[0] = 4; /* Subelement ID */ | |
885 | bss_term_dur[1] = 10; /* Length */ | |
886 | os_memset(bss_term_dur, 2, 8); | |
887 | end = os_strchr(pos, ','); | |
888 | if (end == NULL) { | |
889 | wpa_printf(MSG_DEBUG, "Invalid bss_term data"); | |
890 | return -1; | |
891 | } | |
892 | end++; | |
893 | WPA_PUT_LE16(&bss_term_dur[10], atoi(end)); | |
894 | } | |
895 | ||
e044a9d1 AS |
896 | nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep, |
897 | sizeof(nei_rep)); | |
898 | if (nei_len < 0) | |
899 | return -1; | |
a30dff07 JM |
900 | |
901 | pos = os_strstr(cmd, " url="); | |
902 | if (pos) { | |
903 | size_t len; | |
904 | pos += 5; | |
905 | end = os_strchr(pos, ' '); | |
906 | if (end) | |
907 | len = end - pos; | |
908 | else | |
909 | len = os_strlen(pos); | |
910 | url = os_malloc(len + 1); | |
911 | if (url == NULL) | |
912 | return -1; | |
913 | os_memcpy(url, pos, len); | |
914 | url[len] = '\0'; | |
915 | req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT; | |
916 | } | |
917 | ||
918 | if (os_strstr(cmd, " pref=1")) | |
919 | req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED; | |
920 | if (os_strstr(cmd, " abridged=1")) | |
921 | req_mode |= WNM_BSS_TM_REQ_ABRIDGED; | |
922 | if (os_strstr(cmd, " disassoc_imminent=1")) | |
923 | req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT; | |
924 | ||
c0e2a172 AS |
925 | #ifdef CONFIG_MBO |
926 | pos = os_strstr(cmd, "mbo="); | |
927 | if (pos) { | |
928 | unsigned int mbo_reason, cell_pref, reassoc_delay; | |
929 | u8 *mbo_pos = mbo; | |
930 | ||
931 | ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason, | |
932 | &reassoc_delay, &cell_pref); | |
933 | if (ret != 3) { | |
934 | wpa_printf(MSG_DEBUG, | |
935 | "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>"); | |
fd5f29e7 JM |
936 | ret = -1; |
937 | goto fail; | |
c0e2a172 AS |
938 | } |
939 | ||
940 | if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) { | |
941 | wpa_printf(MSG_DEBUG, | |
942 | "Invalid MBO transition reason code %u", | |
943 | mbo_reason); | |
fd5f29e7 JM |
944 | ret = -1; |
945 | goto fail; | |
c0e2a172 AS |
946 | } |
947 | ||
948 | /* Valid values for Cellular preference are: 0, 1, 255 */ | |
949 | if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) { | |
950 | wpa_printf(MSG_DEBUG, | |
951 | "Invalid MBO cellular capability %u", | |
952 | cell_pref); | |
fd5f29e7 JM |
953 | ret = -1; |
954 | goto fail; | |
c0e2a172 AS |
955 | } |
956 | ||
957 | if (reassoc_delay > 65535 || | |
958 | (reassoc_delay && | |
959 | !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) { | |
960 | wpa_printf(MSG_DEBUG, | |
961 | "MBO: Assoc retry delay is only valid in disassoc imminent mode"); | |
fd5f29e7 JM |
962 | ret = -1; |
963 | goto fail; | |
c0e2a172 AS |
964 | } |
965 | ||
966 | *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON; | |
967 | *mbo_pos++ = 1; | |
968 | *mbo_pos++ = mbo_reason; | |
969 | *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF; | |
970 | *mbo_pos++ = 1; | |
971 | *mbo_pos++ = cell_pref; | |
972 | ||
973 | if (reassoc_delay) { | |
974 | *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY; | |
975 | *mbo_pos++ = 2; | |
976 | WPA_PUT_LE16(mbo_pos, reassoc_delay); | |
977 | mbo_pos += 2; | |
978 | } | |
979 | ||
980 | mbo_len = mbo_pos - mbo; | |
981 | } | |
982 | #endif /* CONFIG_MBO */ | |
983 | ||
a30dff07 JM |
984 | ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer, |
985 | valid_int, bss_term_dur, url, | |
e044a9d1 AS |
986 | nei_len ? nei_rep : NULL, nei_len, |
987 | mbo_len ? mbo : NULL, mbo_len); | |
d5bd9413 | 988 | #ifdef CONFIG_MBO |
fd5f29e7 | 989 | fail: |
d5bd9413 | 990 | #endif /* CONFIG_MBO */ |
a30dff07 JM |
991 | os_free(url); |
992 | return ret; | |
993 | } | |
994 | ||
d514b502 JM |
995 | |
996 | static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd, | |
997 | const char *cmd) | |
998 | { | |
999 | u8 addr[ETH_ALEN]; | |
1000 | struct sta_info *sta; | |
1001 | const char *pos; | |
1002 | unsigned int auto_report, timeout; | |
1003 | ||
1004 | if (hwaddr_aton(cmd, addr)) { | |
1005 | wpa_printf(MSG_DEBUG, "Invalid STA MAC address"); | |
1006 | return -1; | |
1007 | } | |
1008 | ||
1009 | sta = ap_get_sta(hapd, addr); | |
1010 | if (!sta) { | |
1011 | wpa_printf(MSG_DEBUG, "Station " MACSTR | |
1012 | " not found for Collocated Interference Request", | |
1013 | MAC2STR(addr)); | |
1014 | return -1; | |
1015 | } | |
1016 | ||
1017 | pos = cmd + 17; | |
1018 | if (*pos != ' ') | |
1019 | return -1; | |
1020 | pos++; | |
1021 | auto_report = atoi(pos); | |
1022 | pos = os_strchr(pos, ' '); | |
1023 | if (!pos) | |
1024 | return -1; | |
1025 | pos++; | |
1026 | timeout = atoi(pos); | |
1027 | ||
1028 | return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout); | |
1029 | } | |
1030 | ||
b5bf84ba | 1031 | #endif /* CONFIG_WNM_AP */ |
2049a875 | 1032 | |
71269b37 | 1033 | |
c497a024 AN |
1034 | static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd, |
1035 | char *buf, size_t buflen) | |
1036 | { | |
1037 | int ret = 0; | |
1038 | char *pos, *end; | |
1039 | ||
1040 | pos = buf; | |
1041 | end = buf + buflen; | |
1042 | ||
1043 | WPA_ASSERT(hapd->conf->wpa_key_mgmt); | |
1044 | ||
1045 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) { | |
1046 | ret = os_snprintf(pos, end - pos, "WPA-PSK "); | |
1047 | if (os_snprintf_error(end - pos, ret)) | |
1048 | return pos - buf; | |
1049 | pos += ret; | |
1050 | } | |
1051 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) { | |
1052 | ret = os_snprintf(pos, end - pos, "WPA-EAP "); | |
1053 | if (os_snprintf_error(end - pos, ret)) | |
1054 | return pos - buf; | |
1055 | pos += ret; | |
1056 | } | |
d503eeea | 1057 | #ifdef CONFIG_IEEE80211R_AP |
c497a024 AN |
1058 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) { |
1059 | ret = os_snprintf(pos, end - pos, "FT-PSK "); | |
1060 | if (os_snprintf_error(end - pos, ret)) | |
1061 | return pos - buf; | |
1062 | pos += ret; | |
1063 | } | |
1064 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) { | |
1065 | ret = os_snprintf(pos, end - pos, "FT-EAP "); | |
1066 | if (os_snprintf_error(end - pos, ret)) | |
1067 | return pos - buf; | |
1068 | pos += ret; | |
1069 | } | |
c6f2eceb JM |
1070 | #ifdef CONFIG_SHA384 |
1071 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) { | |
1072 | ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 "); | |
1073 | if (os_snprintf_error(end - pos, ret)) | |
1074 | return pos - buf; | |
1075 | pos += ret; | |
1076 | } | |
1077 | #endif /* CONFIG_SHA384 */ | |
c497a024 AN |
1078 | #ifdef CONFIG_SAE |
1079 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) { | |
1080 | ret = os_snprintf(pos, end - pos, "FT-SAE "); | |
1081 | if (os_snprintf_error(end - pos, ret)) | |
1082 | return pos - buf; | |
1083 | pos += ret; | |
1084 | } | |
1085 | #endif /* CONFIG_SAE */ | |
903ecbe8 JM |
1086 | #ifdef CONFIG_FILS |
1087 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) { | |
1088 | ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 "); | |
1089 | if (os_snprintf_error(end - pos, ret)) | |
1090 | return pos - buf; | |
1091 | pos += ret; | |
1092 | } | |
1093 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) { | |
1094 | ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 "); | |
1095 | if (os_snprintf_error(end - pos, ret)) | |
1096 | return pos - buf; | |
1097 | pos += ret; | |
1098 | } | |
1099 | #endif /* CONFIG_FILS */ | |
d503eeea | 1100 | #endif /* CONFIG_IEEE80211R_AP */ |
c497a024 AN |
1101 | #ifdef CONFIG_IEEE80211W |
1102 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) { | |
1103 | ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 "); | |
1104 | if (os_snprintf_error(end - pos, ret)) | |
1105 | return pos - buf; | |
1106 | pos += ret; | |
1107 | } | |
1108 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) { | |
1109 | ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 "); | |
1110 | if (os_snprintf_error(end - pos, ret)) | |
1111 | return pos - buf; | |
1112 | pos += ret; | |
1113 | } | |
1114 | #endif /* CONFIG_IEEE80211W */ | |
1115 | #ifdef CONFIG_SAE | |
1116 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) { | |
1117 | ret = os_snprintf(pos, end - pos, "SAE "); | |
1118 | if (os_snprintf_error(end - pos, ret)) | |
1119 | return pos - buf; | |
1120 | pos += ret; | |
1121 | } | |
1122 | #endif /* CONFIG_SAE */ | |
1123 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) { | |
1124 | ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B "); | |
1125 | if (os_snprintf_error(end - pos, ret)) | |
1126 | return pos - buf; | |
1127 | pos += ret; | |
1128 | } | |
1129 | if (hapd->conf->wpa_key_mgmt & | |
1130 | WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) { | |
1131 | ret = os_snprintf(pos, end - pos, | |
1132 | "WPA-EAP-SUITE-B-192 "); | |
1133 | if (os_snprintf_error(end - pos, ret)) | |
1134 | return pos - buf; | |
1135 | pos += ret; | |
1136 | } | |
903ecbe8 JM |
1137 | #ifdef CONFIG_FILS |
1138 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) { | |
1139 | ret = os_snprintf(pos, end - pos, "FILS-SHA256 "); | |
1140 | if (os_snprintf_error(end - pos, ret)) | |
1141 | return pos - buf; | |
1142 | pos += ret; | |
1143 | } | |
1144 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) { | |
1145 | ret = os_snprintf(pos, end - pos, "FILS-SHA384 "); | |
1146 | if (os_snprintf_error(end - pos, ret)) | |
1147 | return pos - buf; | |
1148 | pos += ret; | |
1149 | } | |
1150 | #endif /* CONFIG_FILS */ | |
c497a024 | 1151 | |
a1ea1b45 JM |
1152 | #ifdef CONFIG_OWE |
1153 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) { | |
1154 | ret = os_snprintf(pos, end - pos, "OWE "); | |
1155 | if (os_snprintf_error(end - pos, ret)) | |
1156 | return pos - buf; | |
1157 | pos += ret; | |
1158 | } | |
1159 | #endif /* CONFIG_OWE */ | |
1160 | ||
567da5bb JM |
1161 | #ifdef CONFIG_DPP |
1162 | if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) { | |
1163 | ret = os_snprintf(pos, end - pos, "DPP "); | |
1164 | if (os_snprintf_error(end - pos, ret)) | |
1165 | return pos - buf; | |
1166 | pos += ret; | |
1167 | } | |
1168 | #endif /* CONFIG_DPP */ | |
1169 | ||
c497a024 AN |
1170 | if (pos > buf && *(pos - 1) == ' ') { |
1171 | *(pos - 1) = '\0'; | |
1172 | pos--; | |
1173 | } | |
1174 | ||
1175 | return pos - buf; | |
1176 | } | |
1177 | ||
1178 | ||
403b96fe JM |
1179 | static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd, |
1180 | char *buf, size_t buflen) | |
1181 | { | |
1182 | int ret; | |
1183 | char *pos, *end; | |
1184 | ||
1185 | pos = buf; | |
1186 | end = buf + buflen; | |
1187 | ||
1188 | ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n" | |
1189 | "ssid=%s\n", | |
1190 | MAC2STR(hapd->own_addr), | |
986de33d JM |
1191 | wpa_ssid_txt(hapd->conf->ssid.ssid, |
1192 | hapd->conf->ssid.ssid_len)); | |
d85e1fc8 | 1193 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1194 | return pos - buf; |
1195 | pos += ret; | |
1196 | ||
1197 | #ifdef CONFIG_WPS | |
1198 | ret = os_snprintf(pos, end - pos, "wps_state=%s\n", | |
1199 | hapd->conf->wps_state == 0 ? "disabled" : | |
1200 | (hapd->conf->wps_state == 1 ? "not configured" : | |
1201 | "configured")); | |
d85e1fc8 | 1202 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1203 | return pos - buf; |
1204 | pos += ret; | |
1205 | ||
088a2255 | 1206 | if (hapd->conf->wps_state && hapd->conf->wpa && |
403b96fe JM |
1207 | hapd->conf->ssid.wpa_passphrase) { |
1208 | ret = os_snprintf(pos, end - pos, "passphrase=%s\n", | |
1209 | hapd->conf->ssid.wpa_passphrase); | |
d85e1fc8 | 1210 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1211 | return pos - buf; |
1212 | pos += ret; | |
1213 | } | |
1214 | ||
088a2255 JM |
1215 | if (hapd->conf->wps_state && hapd->conf->wpa && |
1216 | hapd->conf->ssid.wpa_psk && | |
403b96fe JM |
1217 | hapd->conf->ssid.wpa_psk->group) { |
1218 | char hex[PMK_LEN * 2 + 1]; | |
1219 | wpa_snprintf_hex(hex, sizeof(hex), | |
1220 | hapd->conf->ssid.wpa_psk->psk, PMK_LEN); | |
1221 | ret = os_snprintf(pos, end - pos, "psk=%s\n", hex); | |
d85e1fc8 | 1222 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1223 | return pos - buf; |
1224 | pos += ret; | |
1225 | } | |
1226 | #endif /* CONFIG_WPS */ | |
1227 | ||
de7581ef MSS |
1228 | if (hapd->conf->wpa) { |
1229 | ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa); | |
1230 | if (os_snprintf_error(end - pos, ret)) | |
1231 | return pos - buf; | |
1232 | pos += ret; | |
1233 | } | |
1234 | ||
403b96fe JM |
1235 | if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) { |
1236 | ret = os_snprintf(pos, end - pos, "key_mgmt="); | |
d85e1fc8 | 1237 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1238 | return pos - buf; |
1239 | pos += ret; | |
1240 | ||
c497a024 | 1241 | pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos); |
403b96fe JM |
1242 | |
1243 | ret = os_snprintf(pos, end - pos, "\n"); | |
d85e1fc8 | 1244 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1245 | return pos - buf; |
1246 | pos += ret; | |
1247 | } | |
1248 | ||
0282a8c4 JM |
1249 | if (hapd->conf->wpa) { |
1250 | ret = os_snprintf(pos, end - pos, "group_cipher=%s\n", | |
1251 | wpa_cipher_txt(hapd->conf->wpa_group)); | |
d85e1fc8 | 1252 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1253 | return pos - buf; |
1254 | pos += ret; | |
1255 | } | |
1256 | ||
1257 | if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) { | |
1258 | ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher="); | |
d85e1fc8 | 1259 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1260 | return pos - buf; |
1261 | pos += ret; | |
1262 | ||
0282a8c4 JM |
1263 | ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise, |
1264 | " "); | |
1265 | if (ret < 0) | |
1266 | return pos - buf; | |
1267 | pos += ret; | |
403b96fe JM |
1268 | |
1269 | ret = os_snprintf(pos, end - pos, "\n"); | |
d85e1fc8 | 1270 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1271 | return pos - buf; |
1272 | pos += ret; | |
1273 | } | |
1274 | ||
1275 | if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) { | |
1276 | ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher="); | |
d85e1fc8 | 1277 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1278 | return pos - buf; |
1279 | pos += ret; | |
1280 | ||
10e7948f | 1281 | ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise, |
0282a8c4 JM |
1282 | " "); |
1283 | if (ret < 0) | |
1284 | return pos - buf; | |
1285 | pos += ret; | |
403b96fe JM |
1286 | |
1287 | ret = os_snprintf(pos, end - pos, "\n"); | |
d85e1fc8 | 1288 | if (os_snprintf_error(end - pos, ret)) |
403b96fe JM |
1289 | return pos - buf; |
1290 | pos += ret; | |
1291 | } | |
1292 | ||
1293 | return pos - buf; | |
1294 | } | |
1295 | ||
1296 | ||
3988046d T |
1297 | static void hostapd_disassoc_accept_mac(struct hostapd_data *hapd) |
1298 | { | |
1299 | struct sta_info *sta; | |
1300 | struct vlan_description vlan_id; | |
1301 | ||
1302 | if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED) | |
1303 | return; | |
1304 | ||
1305 | for (sta = hapd->sta_list; sta; sta = sta->next) { | |
1306 | if (!hostapd_maclist_found(hapd->conf->accept_mac, | |
1307 | hapd->conf->num_accept_mac, | |
1308 | sta->addr, &vlan_id) || | |
1309 | (vlan_id.notempty && | |
1310 | vlan_compare(&vlan_id, sta->vlan_desc))) | |
1311 | ap_sta_disconnect(hapd, sta, sta->addr, | |
1312 | WLAN_REASON_UNSPECIFIED); | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | ||
1317 | static void hostapd_disassoc_deny_mac(struct hostapd_data *hapd) | |
1318 | { | |
1319 | struct sta_info *sta; | |
1320 | struct vlan_description vlan_id; | |
1321 | ||
1322 | for (sta = hapd->sta_list; sta; sta = sta->next) { | |
1323 | if (hostapd_maclist_found(hapd->conf->deny_mac, | |
1324 | hapd->conf->num_deny_mac, sta->addr, | |
1325 | &vlan_id) && | |
1326 | (!vlan_id.notempty || | |
1327 | !vlan_compare(&vlan_id, sta->vlan_desc))) | |
1328 | ap_sta_disconnect(hapd, sta, sta->addr, | |
1329 | WLAN_REASON_UNSPECIFIED); | |
1330 | } | |
1331 | } | |
1332 | ||
2c8a4eef | 1333 | static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd) |
b4e34f2f JM |
1334 | { |
1335 | char *value; | |
1336 | int ret = 0; | |
1337 | ||
1338 | value = os_strchr(cmd, ' '); | |
1339 | if (value == NULL) | |
1340 | return -1; | |
1341 | *value++ = '\0'; | |
1342 | ||
1343 | wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value); | |
1344 | if (0) { | |
1345 | #ifdef CONFIG_WPS_TESTING | |
1346 | } else if (os_strcasecmp(cmd, "wps_version_number") == 0) { | |
1347 | long int val; | |
1348 | val = strtol(value, NULL, 0); | |
1349 | if (val < 0 || val > 0xff) { | |
1350 | ret = -1; | |
1351 | wpa_printf(MSG_DEBUG, "WPS: Invalid " | |
1352 | "wps_version_number %ld", val); | |
1353 | } else { | |
1354 | wps_version_number = val; | |
1355 | wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS " | |
1356 | "version %u.%u", | |
1357 | (wps_version_number & 0xf0) >> 4, | |
1358 | wps_version_number & 0x0f); | |
2c8a4eef | 1359 | hostapd_wps_update_ie(hapd); |
b4e34f2f JM |
1360 | } |
1361 | } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) { | |
1362 | wps_testing_dummy_cred = atoi(value); | |
1363 | wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d", | |
1364 | wps_testing_dummy_cred); | |
91226e0d JM |
1365 | } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) { |
1366 | wps_corrupt_pkhash = atoi(value); | |
1367 | wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d", | |
1368 | wps_corrupt_pkhash); | |
b4e34f2f | 1369 | #endif /* CONFIG_WPS_TESTING */ |
93827f45 JM |
1370 | #ifdef CONFIG_TESTING_OPTIONS |
1371 | } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) { | |
1372 | hapd->ext_mgmt_frame_handling = atoi(value); | |
9d4ff04a JM |
1373 | } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) { |
1374 | hapd->ext_eapol_frame_io = atoi(value); | |
9c2b8204 JM |
1375 | #ifdef CONFIG_DPP |
1376 | } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) { | |
1377 | os_free(hapd->dpp_config_obj_override); | |
1378 | hapd->dpp_config_obj_override = os_strdup(value); | |
1379 | } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) { | |
1380 | os_free(hapd->dpp_discovery_override); | |
1381 | hapd->dpp_discovery_override = os_strdup(value); | |
1382 | } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) { | |
1383 | os_free(hapd->dpp_groups_override); | |
1384 | hapd->dpp_groups_override = os_strdup(value); | |
9c2b8204 JM |
1385 | } else if (os_strcasecmp(cmd, |
1386 | "dpp_ignore_netaccesskey_mismatch") == 0) { | |
1387 | hapd->dpp_ignore_netaccesskey_mismatch = atoi(value); | |
60239f60 JM |
1388 | } else if (os_strcasecmp(cmd, "dpp_test") == 0) { |
1389 | dpp_test = atoi(value); | |
9c2b8204 | 1390 | #endif /* CONFIG_DPP */ |
93827f45 | 1391 | #endif /* CONFIG_TESTING_OPTIONS */ |
fb9a1c3e AS |
1392 | #ifdef CONFIG_MBO |
1393 | } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) { | |
1394 | int val; | |
1395 | ||
1396 | if (!hapd->conf->mbo_enabled) | |
1397 | return -1; | |
1398 | ||
1399 | val = atoi(value); | |
1400 | if (val < 0 || val > 1) | |
1401 | return -1; | |
1402 | ||
1403 | hapd->mbo_assoc_disallow = val; | |
1404 | ieee802_11_update_beacons(hapd->iface); | |
1405 | ||
1406 | /* | |
1407 | * TODO: Need to configure drivers that do AP MLME offload with | |
1408 | * disallowing station logic. | |
1409 | */ | |
1410 | #endif /* CONFIG_MBO */ | |
6b140f0f JM |
1411 | #ifdef CONFIG_DPP |
1412 | } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) { | |
1413 | os_free(hapd->dpp_configurator_params); | |
1414 | hapd->dpp_configurator_params = os_strdup(value); | |
1415 | #endif /* CONFIG_DPP */ | |
b4e34f2f | 1416 | } else { |
31b79e11 | 1417 | ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value); |
e2364d16 ET |
1418 | if (ret) |
1419 | return ret; | |
1420 | ||
1421 | if (os_strcasecmp(cmd, "deny_mac_file") == 0) { | |
3988046d T |
1422 | hostapd_disassoc_deny_mac(hapd); |
1423 | } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) { | |
1424 | hostapd_disassoc_accept_mac(hapd); | |
86a6f5f8 BL |
1425 | } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 || |
1426 | os_strncmp(cmd, "wmm_ac_", 7) == 0) { | |
1427 | hapd->parameter_set_count++; | |
1428 | if (ieee802_11_update_beacons(hapd->iface)) | |
1429 | wpa_printf(MSG_DEBUG, | |
1430 | "Failed to update beacons with WMM parameters"); | |
e2364d16 | 1431 | } |
b4e34f2f JM |
1432 | } |
1433 | ||
1434 | return ret; | |
1435 | } | |
1436 | ||
1437 | ||
acec8d32 JM |
1438 | static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd, |
1439 | char *buf, size_t buflen) | |
1440 | { | |
1441 | int res; | |
1442 | ||
1443 | wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd); | |
1444 | ||
1445 | if (os_strcmp(cmd, "version") == 0) { | |
1446 | res = os_snprintf(buf, buflen, "%s", VERSION_STR); | |
d85e1fc8 | 1447 | if (os_snprintf_error(buflen, res)) |
a1651451 JM |
1448 | return -1; |
1449 | return res; | |
1450 | } else if (os_strcmp(cmd, "tls_library") == 0) { | |
1451 | res = tls_get_library_version(buf, buflen); | |
1452 | if (os_snprintf_error(buflen, res)) | |
acec8d32 JM |
1453 | return -1; |
1454 | return res; | |
1455 | } | |
1456 | ||
1457 | return -1; | |
1458 | } | |
1459 | ||
1460 | ||
75545652 SP |
1461 | static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface) |
1462 | { | |
1463 | if (hostapd_enable_iface(iface) < 0) { | |
1464 | wpa_printf(MSG_ERROR, "Enabling of interface failed"); | |
1465 | return -1; | |
1466 | } | |
1467 | return 0; | |
1468 | } | |
1469 | ||
1470 | ||
1471 | static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface) | |
1472 | { | |
1473 | if (hostapd_reload_iface(iface) < 0) { | |
1474 | wpa_printf(MSG_ERROR, "Reloading of interface failed"); | |
1475 | return -1; | |
1476 | } | |
1477 | return 0; | |
1478 | } | |
1479 | ||
1480 | ||
1481 | static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface) | |
1482 | { | |
1483 | if (hostapd_disable_iface(iface) < 0) { | |
1484 | wpa_printf(MSG_ERROR, "Disabling of interface failed"); | |
1485 | return -1; | |
1486 | } | |
1487 | return 0; | |
1488 | } | |
1489 | ||
1490 | ||
83c86081 MK |
1491 | static int |
1492 | hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd, | |
1493 | struct sta_info *sta, void *ctx) | |
1494 | { | |
1495 | struct hostapd_wpa_psk *psk; | |
1496 | const u8 *pmk; | |
1497 | int pmk_len; | |
1498 | int pmk_match; | |
1499 | int sta_match; | |
1500 | int bss_match; | |
1501 | int reason; | |
1502 | ||
1503 | pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len); | |
1504 | ||
1505 | for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) { | |
1506 | pmk_match = PMK_LEN == pmk_len && | |
1507 | os_memcmp(psk->psk, pmk, pmk_len) == 0; | |
1508 | sta_match = psk->group == 0 && | |
1509 | os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0; | |
1510 | bss_match = psk->group == 1; | |
1511 | ||
1512 | if (pmk_match && (sta_match || bss_match)) | |
1513 | return 0; | |
1514 | } | |
1515 | ||
1516 | wpa_printf(MSG_INFO, "STA " MACSTR | |
1517 | " PSK/passphrase no longer valid - disconnect", | |
1518 | MAC2STR(sta->addr)); | |
1519 | reason = WLAN_REASON_PREV_AUTH_NOT_VALID; | |
1520 | hostapd_drv_sta_deauth(hapd, sta->addr, reason); | |
1521 | ap_sta_deauthenticate(hapd, sta, reason); | |
1522 | ||
1523 | return 0; | |
1524 | } | |
1525 | ||
1526 | ||
1527 | static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd) | |
1528 | { | |
1529 | struct hostapd_bss_config *conf = hapd->conf; | |
1530 | int err; | |
1531 | ||
1532 | hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk); | |
1533 | ||
1534 | err = hostapd_setup_wpa_psk(conf); | |
1535 | if (err < 0) { | |
1536 | wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d", | |
1537 | err); | |
1538 | return -1; | |
1539 | } | |
1540 | ||
1541 | ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter, | |
1542 | NULL); | |
1543 | ||
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | ||
ddf55174 | 1548 | #ifdef CONFIG_TESTING_OPTIONS |
93827f45 | 1549 | |
ddf55174 JM |
1550 | static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd) |
1551 | { | |
1552 | union wpa_event_data data; | |
1553 | char *pos, *param; | |
1554 | enum wpa_event_type event; | |
1555 | ||
1556 | wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd); | |
1557 | ||
1558 | os_memset(&data, 0, sizeof(data)); | |
1559 | ||
1560 | param = os_strchr(cmd, ' '); | |
1561 | if (param == NULL) | |
1562 | return -1; | |
1563 | *param++ = '\0'; | |
1564 | ||
1565 | if (os_strcmp(cmd, "DETECTED") == 0) | |
1566 | event = EVENT_DFS_RADAR_DETECTED; | |
1567 | else if (os_strcmp(cmd, "CAC-FINISHED") == 0) | |
1568 | event = EVENT_DFS_CAC_FINISHED; | |
1569 | else if (os_strcmp(cmd, "CAC-ABORTED") == 0) | |
1570 | event = EVENT_DFS_CAC_ABORTED; | |
1571 | else if (os_strcmp(cmd, "NOP-FINISHED") == 0) | |
1572 | event = EVENT_DFS_NOP_FINISHED; | |
1573 | else { | |
1574 | wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s", | |
1575 | cmd); | |
1576 | return -1; | |
1577 | } | |
1578 | ||
1579 | pos = os_strstr(param, "freq="); | |
1580 | if (pos) | |
1581 | data.dfs_event.freq = atoi(pos + 5); | |
1582 | ||
1583 | pos = os_strstr(param, "ht_enabled=1"); | |
1584 | if (pos) | |
1585 | data.dfs_event.ht_enabled = 1; | |
1586 | ||
1587 | pos = os_strstr(param, "chan_offset="); | |
1588 | if (pos) | |
1589 | data.dfs_event.chan_offset = atoi(pos + 12); | |
1590 | ||
1591 | pos = os_strstr(param, "chan_width="); | |
1592 | if (pos) | |
1593 | data.dfs_event.chan_width = atoi(pos + 11); | |
1594 | ||
1595 | pos = os_strstr(param, "cf1="); | |
1596 | if (pos) | |
1597 | data.dfs_event.cf1 = atoi(pos + 4); | |
1598 | ||
1599 | pos = os_strstr(param, "cf2="); | |
1600 | if (pos) | |
1601 | data.dfs_event.cf2 = atoi(pos + 4); | |
1602 | ||
1603 | wpa_supplicant_event(hapd, event, &data); | |
1604 | ||
1605 | return 0; | |
1606 | } | |
93827f45 JM |
1607 | |
1608 | ||
1609 | static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd) | |
1610 | { | |
1611 | size_t len; | |
1612 | u8 *buf; | |
1613 | int res; | |
1614 | ||
1615 | wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd); | |
1616 | ||
1617 | len = os_strlen(cmd); | |
1618 | if (len & 1) | |
1619 | return -1; | |
1620 | len /= 2; | |
1621 | ||
1622 | buf = os_malloc(len); | |
1623 | if (buf == NULL) | |
1624 | return -1; | |
1625 | ||
1626 | if (hexstr2bin(cmd, buf, len) < 0) { | |
1627 | os_free(buf); | |
1628 | return -1; | |
1629 | } | |
1630 | ||
1631 | res = hostapd_drv_send_mlme(hapd, buf, len, 0); | |
1632 | os_free(buf); | |
1633 | return res; | |
1634 | } | |
1635 | ||
9d4ff04a | 1636 | |
df949062 JM |
1637 | static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd, |
1638 | char *cmd) | |
1639 | { | |
1640 | char *pos, *param; | |
1641 | size_t len; | |
1642 | u8 *buf; | |
1643 | int stype = 0, ok = 0; | |
1644 | union wpa_event_data event; | |
1645 | ||
1646 | if (!hapd->ext_mgmt_frame_handling) | |
1647 | return -1; | |
1648 | ||
1649 | /* stype=<val> ok=<0/1> buf=<frame hexdump> */ | |
1650 | ||
1651 | wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd); | |
1652 | ||
1653 | pos = cmd; | |
1654 | param = os_strstr(pos, "stype="); | |
1655 | if (param) { | |
1656 | param += 6; | |
1657 | stype = atoi(param); | |
1658 | } | |
1659 | ||
1660 | param = os_strstr(pos, " ok="); | |
1661 | if (param) { | |
1662 | param += 4; | |
1663 | ok = atoi(param); | |
1664 | } | |
1665 | ||
1666 | param = os_strstr(pos, " buf="); | |
1667 | if (!param) | |
1668 | return -1; | |
1669 | param += 5; | |
1670 | ||
1671 | len = os_strlen(param); | |
1672 | if (len & 1) | |
1673 | return -1; | |
1674 | len /= 2; | |
1675 | ||
1676 | buf = os_malloc(len); | |
1677 | if (!buf || hexstr2bin(param, buf, len) < 0) { | |
1678 | os_free(buf); | |
1679 | return -1; | |
1680 | } | |
1681 | ||
1682 | os_memset(&event, 0, sizeof(event)); | |
1683 | event.tx_status.type = WLAN_FC_TYPE_MGMT; | |
1684 | event.tx_status.data = buf; | |
1685 | event.tx_status.data_len = len; | |
1686 | event.tx_status.stype = stype; | |
1687 | event.tx_status.ack = ok; | |
1688 | hapd->ext_mgmt_frame_handling = 0; | |
1689 | wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event); | |
1690 | hapd->ext_mgmt_frame_handling = 1; | |
1691 | ||
1692 | os_free(buf); | |
1693 | ||
1694 | return 0; | |
1695 | } | |
1696 | ||
1697 | ||
47d74bf0 JM |
1698 | static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd, |
1699 | char *cmd) | |
1700 | { | |
1701 | char *pos, *param; | |
1702 | size_t len; | |
1703 | u8 *buf; | |
1704 | int freq = 0, datarate = 0, ssi_signal = 0; | |
1705 | union wpa_event_data event; | |
1706 | ||
1707 | if (!hapd->ext_mgmt_frame_handling) | |
1708 | return -1; | |
1709 | ||
1710 | /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */ | |
1711 | ||
1712 | wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd); | |
1713 | ||
1714 | pos = cmd; | |
1715 | param = os_strstr(pos, "freq="); | |
1716 | if (param) { | |
1717 | param += 5; | |
1718 | freq = atoi(param); | |
1719 | } | |
1720 | ||
1721 | param = os_strstr(pos, " datarate="); | |
1722 | if (param) { | |
1723 | param += 10; | |
1724 | datarate = atoi(param); | |
1725 | } | |
1726 | ||
1727 | param = os_strstr(pos, " ssi_signal="); | |
1728 | if (param) { | |
1729 | param += 12; | |
1730 | ssi_signal = atoi(param); | |
1731 | } | |
1732 | ||
1733 | param = os_strstr(pos, " frame="); | |
1734 | if (param == NULL) | |
1735 | return -1; | |
1736 | param += 7; | |
1737 | ||
1738 | len = os_strlen(param); | |
1739 | if (len & 1) | |
1740 | return -1; | |
1741 | len /= 2; | |
1742 | ||
1743 | buf = os_malloc(len); | |
1744 | if (buf == NULL) | |
1745 | return -1; | |
1746 | ||
1747 | if (hexstr2bin(param, buf, len) < 0) { | |
1748 | os_free(buf); | |
1749 | return -1; | |
1750 | } | |
1751 | ||
1752 | os_memset(&event, 0, sizeof(event)); | |
1753 | event.rx_mgmt.freq = freq; | |
1754 | event.rx_mgmt.frame = buf; | |
1755 | event.rx_mgmt.frame_len = len; | |
1756 | event.rx_mgmt.ssi_signal = ssi_signal; | |
1757 | event.rx_mgmt.datarate = datarate; | |
1758 | hapd->ext_mgmt_frame_handling = 0; | |
1759 | wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event); | |
1760 | hapd->ext_mgmt_frame_handling = 1; | |
1761 | ||
1762 | os_free(buf); | |
1763 | ||
1764 | return 0; | |
1765 | } | |
1766 | ||
1767 | ||
9d4ff04a JM |
1768 | static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd) |
1769 | { | |
1770 | char *pos; | |
1771 | u8 src[ETH_ALEN], *buf; | |
1772 | int used; | |
1773 | size_t len; | |
1774 | ||
1775 | wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd); | |
1776 | ||
1777 | pos = cmd; | |
1778 | used = hwaddr_aton2(pos, src); | |
1779 | if (used < 0) | |
1780 | return -1; | |
1781 | pos += used; | |
1782 | while (*pos == ' ') | |
1783 | pos++; | |
1784 | ||
1785 | len = os_strlen(pos); | |
1786 | if (len & 1) | |
1787 | return -1; | |
1788 | len /= 2; | |
1789 | ||
1790 | buf = os_malloc(len); | |
1791 | if (buf == NULL) | |
1792 | return -1; | |
1793 | ||
1794 | if (hexstr2bin(pos, buf, len) < 0) { | |
1795 | os_free(buf); | |
1796 | return -1; | |
1797 | } | |
1798 | ||
1799 | ieee802_1x_receive(hapd, src, buf, len); | |
1800 | os_free(buf); | |
1801 | ||
1802 | return 0; | |
1803 | } | |
1804 | ||
4a6cc862 JM |
1805 | |
1806 | static u16 ipv4_hdr_checksum(const void *buf, size_t len) | |
1807 | { | |
1808 | size_t i; | |
1809 | u32 sum = 0; | |
1810 | const u16 *pos = buf; | |
1811 | ||
1812 | for (i = 0; i < len / 2; i++) | |
1813 | sum += *pos++; | |
1814 | ||
1815 | while (sum >> 16) | |
1816 | sum = (sum & 0xffff) + (sum >> 16); | |
1817 | ||
1818 | return sum ^ 0xffff; | |
1819 | } | |
1820 | ||
1821 | ||
1822 | #define HWSIM_PACKETLEN 1500 | |
1823 | #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header)) | |
1824 | ||
f367c08d JM |
1825 | static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, |
1826 | size_t len) | |
4a6cc862 JM |
1827 | { |
1828 | struct hostapd_data *hapd = ctx; | |
1829 | const struct ether_header *eth; | |
75352270 | 1830 | struct iphdr ip; |
4a6cc862 JM |
1831 | const u8 *pos; |
1832 | unsigned int i; | |
1833 | ||
1834 | if (len != HWSIM_PACKETLEN) | |
1835 | return; | |
1836 | ||
1837 | eth = (const struct ether_header *) buf; | |
75352270 JM |
1838 | os_memcpy(&ip, eth + 1, sizeof(ip)); |
1839 | pos = &buf[sizeof(*eth) + sizeof(ip)]; | |
4a6cc862 | 1840 | |
75352270 JM |
1841 | if (ip.ihl != 5 || ip.version != 4 || |
1842 | ntohs(ip.tot_len) != HWSIM_IP_LEN) | |
4a6cc862 JM |
1843 | return; |
1844 | ||
75352270 | 1845 | for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) { |
4a6cc862 JM |
1846 | if (*pos != (u8) i) |
1847 | return; | |
1848 | pos++; | |
1849 | } | |
1850 | ||
1851 | wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR, | |
1852 | MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost)); | |
1853 | } | |
1854 | ||
1855 | ||
1856 | static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd, | |
1857 | char *cmd) | |
1858 | { | |
1859 | int enabled = atoi(cmd); | |
527d2378 JM |
1860 | char *pos; |
1861 | const char *ifname; | |
4a6cc862 JM |
1862 | |
1863 | if (!enabled) { | |
1864 | if (hapd->l2_test) { | |
1865 | l2_packet_deinit(hapd->l2_test); | |
1866 | hapd->l2_test = NULL; | |
1867 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, | |
1868 | "test data: Disabled"); | |
1869 | } | |
1870 | return 0; | |
1871 | } | |
1872 | ||
1873 | if (hapd->l2_test) | |
1874 | return 0; | |
1875 | ||
527d2378 JM |
1876 | pos = os_strstr(cmd, " ifname="); |
1877 | if (pos) | |
1878 | ifname = pos + 8; | |
1879 | else | |
1880 | ifname = hapd->conf->iface; | |
1881 | ||
1882 | hapd->l2_test = l2_packet_init(ifname, hapd->own_addr, | |
4a6cc862 JM |
1883 | ETHERTYPE_IP, hostapd_data_test_rx, |
1884 | hapd, 1); | |
1885 | if (hapd->l2_test == NULL) | |
1886 | return -1; | |
1887 | ||
1888 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled"); | |
1889 | ||
1890 | return 0; | |
1891 | } | |
1892 | ||
1893 | ||
1894 | static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd) | |
1895 | { | |
1896 | u8 dst[ETH_ALEN], src[ETH_ALEN]; | |
1897 | char *pos; | |
1898 | int used; | |
1899 | long int val; | |
1900 | u8 tos; | |
75352270 | 1901 | u8 buf[2 + HWSIM_PACKETLEN]; |
4a6cc862 JM |
1902 | struct ether_header *eth; |
1903 | struct iphdr *ip; | |
1904 | u8 *dpos; | |
1905 | unsigned int i; | |
1906 | ||
1907 | if (hapd->l2_test == NULL) | |
1908 | return -1; | |
1909 | ||
1910 | /* format: <dst> <src> <tos> */ | |
1911 | ||
1912 | pos = cmd; | |
1913 | used = hwaddr_aton2(pos, dst); | |
1914 | if (used < 0) | |
1915 | return -1; | |
1916 | pos += used; | |
1917 | while (*pos == ' ') | |
1918 | pos++; | |
1919 | used = hwaddr_aton2(pos, src); | |
1920 | if (used < 0) | |
1921 | return -1; | |
1922 | pos += used; | |
1923 | ||
1924 | val = strtol(pos, NULL, 0); | |
1925 | if (val < 0 || val > 0xff) | |
1926 | return -1; | |
1927 | tos = val; | |
1928 | ||
75352270 | 1929 | eth = (struct ether_header *) &buf[2]; |
4a6cc862 JM |
1930 | os_memcpy(eth->ether_dhost, dst, ETH_ALEN); |
1931 | os_memcpy(eth->ether_shost, src, ETH_ALEN); | |
1932 | eth->ether_type = htons(ETHERTYPE_IP); | |
1933 | ip = (struct iphdr *) (eth + 1); | |
1934 | os_memset(ip, 0, sizeof(*ip)); | |
1935 | ip->ihl = 5; | |
1936 | ip->version = 4; | |
1937 | ip->ttl = 64; | |
1938 | ip->tos = tos; | |
1939 | ip->tot_len = htons(HWSIM_IP_LEN); | |
1940 | ip->protocol = 1; | |
66f1e078 JM |
1941 | ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1); |
1942 | ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2); | |
4a6cc862 JM |
1943 | ip->check = ipv4_hdr_checksum(ip, sizeof(*ip)); |
1944 | dpos = (u8 *) (ip + 1); | |
1945 | for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) | |
1946 | *dpos++ = i; | |
1947 | ||
75352270 | 1948 | if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2], |
4a6cc862 JM |
1949 | HWSIM_PACKETLEN) < 0) |
1950 | return -1; | |
1951 | ||
1952 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR | |
1953 | " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos); | |
1954 | ||
1955 | return 0; | |
1956 | } | |
1957 | ||
85660d31 JM |
1958 | |
1959 | static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd, | |
1960 | char *cmd) | |
1961 | { | |
1962 | u8 *buf; | |
1963 | struct ether_header *eth; | |
1964 | struct l2_packet_data *l2 = NULL; | |
1965 | size_t len; | |
1966 | u16 ethertype; | |
1967 | int res = -1; | |
1968 | const char *ifname = hapd->conf->iface; | |
1969 | ||
1970 | if (os_strncmp(cmd, "ifname=", 7) == 0) { | |
1971 | cmd += 7; | |
1972 | ifname = cmd; | |
1973 | cmd = os_strchr(cmd, ' '); | |
1974 | if (cmd == NULL) | |
1975 | return -1; | |
1976 | *cmd++ = '\0'; | |
1977 | } | |
1978 | ||
1979 | len = os_strlen(cmd); | |
1980 | if (len & 1 || len < ETH_HLEN * 2) | |
1981 | return -1; | |
1982 | len /= 2; | |
1983 | ||
1984 | buf = os_malloc(len); | |
1985 | if (buf == NULL) | |
1986 | return -1; | |
1987 | ||
1988 | if (hexstr2bin(cmd, buf, len) < 0) | |
1989 | goto done; | |
1990 | ||
1991 | eth = (struct ether_header *) buf; | |
1992 | ethertype = ntohs(eth->ether_type); | |
1993 | ||
1994 | l2 = l2_packet_init(ifname, hapd->own_addr, ethertype, | |
1995 | hostapd_data_test_rx, hapd, 1); | |
1996 | if (l2 == NULL) | |
1997 | goto done; | |
1998 | ||
1999 | res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len); | |
2000 | wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res); | |
2001 | done: | |
2002 | if (l2) | |
2003 | l2_packet_deinit(l2); | |
2004 | os_free(buf); | |
2005 | ||
2006 | return res < 0 ? -1 : 0; | |
2007 | } | |
2008 | ||
a156ffda JM |
2009 | |
2010 | static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd) | |
2011 | { | |
2012 | #ifdef WPA_TRACE_BFD | |
a156ffda JM |
2013 | char *pos; |
2014 | ||
2015 | wpa_trace_fail_after = atoi(cmd); | |
2016 | pos = os_strchr(cmd, ':'); | |
2017 | if (pos) { | |
2018 | pos++; | |
2019 | os_strlcpy(wpa_trace_fail_func, pos, | |
2020 | sizeof(wpa_trace_fail_func)); | |
2021 | } else { | |
2022 | wpa_trace_fail_after = 0; | |
2023 | } | |
2024 | ||
2025 | return 0; | |
2026 | #else /* WPA_TRACE_BFD */ | |
2027 | return -1; | |
2028 | #endif /* WPA_TRACE_BFD */ | |
2029 | } | |
2030 | ||
2031 | ||
2032 | static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd, | |
2033 | char *buf, size_t buflen) | |
2034 | { | |
2035 | #ifdef WPA_TRACE_BFD | |
a156ffda JM |
2036 | return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after, |
2037 | wpa_trace_fail_func); | |
2038 | #else /* WPA_TRACE_BFD */ | |
2039 | return -1; | |
2040 | #endif /* WPA_TRACE_BFD */ | |
2041 | } | |
2042 | ||
2da52565 JM |
2043 | |
2044 | static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd) | |
2045 | { | |
2046 | #ifdef WPA_TRACE_BFD | |
2da52565 JM |
2047 | char *pos; |
2048 | ||
2049 | wpa_trace_test_fail_after = atoi(cmd); | |
2050 | pos = os_strchr(cmd, ':'); | |
2051 | if (pos) { | |
2052 | pos++; | |
2053 | os_strlcpy(wpa_trace_test_fail_func, pos, | |
2054 | sizeof(wpa_trace_test_fail_func)); | |
2055 | } else { | |
2056 | wpa_trace_test_fail_after = 0; | |
2057 | } | |
2058 | ||
2059 | return 0; | |
2060 | #else /* WPA_TRACE_BFD */ | |
2061 | return -1; | |
2062 | #endif /* WPA_TRACE_BFD */ | |
2063 | } | |
2064 | ||
2065 | ||
2066 | static int hostapd_ctrl_get_fail(struct hostapd_data *hapd, | |
2067 | char *buf, size_t buflen) | |
2068 | { | |
2069 | #ifdef WPA_TRACE_BFD | |
2da52565 JM |
2070 | return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after, |
2071 | wpa_trace_test_fail_func); | |
2072 | #else /* WPA_TRACE_BFD */ | |
2073 | return -1; | |
2074 | #endif /* WPA_TRACE_BFD */ | |
2075 | } | |
2076 | ||
16579769 JM |
2077 | |
2078 | static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd) | |
2079 | { | |
2080 | struct sta_info *sta; | |
2081 | u8 addr[ETH_ALEN]; | |
2082 | u8 zero[WPA_TK_MAX_LEN]; | |
2083 | ||
2084 | os_memset(zero, 0, sizeof(zero)); | |
2085 | ||
2086 | if (hwaddr_aton(cmd, addr)) | |
2087 | return -1; | |
2088 | ||
2089 | #ifdef CONFIG_IEEE80211W | |
2090 | if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) { | |
2091 | if (hapd->last_igtk_alg == WPA_ALG_NONE) | |
2092 | return -1; | |
2093 | ||
2094 | wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK"); | |
2095 | ||
2096 | /* First, use a zero key to avoid any possible duplicate key | |
2097 | * avoidance in the driver. */ | |
2098 | if (hostapd_drv_set_key(hapd->conf->iface, hapd, | |
2099 | hapd->last_igtk_alg, | |
2100 | broadcast_ether_addr, | |
2101 | hapd->last_igtk_key_idx, 1, NULL, 0, | |
2102 | zero, hapd->last_igtk_len) < 0) | |
2103 | return -1; | |
2104 | ||
2105 | /* Set the previously configured key to reset its TSC */ | |
2106 | return hostapd_drv_set_key(hapd->conf->iface, hapd, | |
2107 | hapd->last_igtk_alg, | |
2108 | broadcast_ether_addr, | |
2109 | hapd->last_igtk_key_idx, 1, NULL, 0, | |
2110 | hapd->last_igtk, | |
2111 | hapd->last_igtk_len); | |
2112 | } | |
2113 | #endif /* CONFIG_IEEE80211W */ | |
2114 | ||
2115 | if (is_broadcast_ether_addr(addr)) { | |
2116 | if (hapd->last_gtk_alg == WPA_ALG_NONE) | |
2117 | return -1; | |
2118 | ||
2119 | wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK"); | |
2120 | ||
2121 | /* First, use a zero key to avoid any possible duplicate key | |
2122 | * avoidance in the driver. */ | |
2123 | if (hostapd_drv_set_key(hapd->conf->iface, hapd, | |
2124 | hapd->last_gtk_alg, | |
2125 | broadcast_ether_addr, | |
2126 | hapd->last_gtk_key_idx, 1, NULL, 0, | |
2127 | zero, hapd->last_gtk_len) < 0) | |
2128 | return -1; | |
2129 | ||
2130 | /* Set the previously configured key to reset its TSC */ | |
2131 | return hostapd_drv_set_key(hapd->conf->iface, hapd, | |
2132 | hapd->last_gtk_alg, | |
2133 | broadcast_ether_addr, | |
2134 | hapd->last_gtk_key_idx, 1, NULL, 0, | |
2135 | hapd->last_gtk, hapd->last_gtk_len); | |
2136 | } | |
2137 | ||
2138 | sta = ap_get_sta(hapd, addr); | |
2139 | if (!sta) | |
2140 | return -1; | |
2141 | ||
2142 | if (sta->last_tk_alg == WPA_ALG_NONE) | |
2143 | return -1; | |
2144 | ||
2145 | wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR, | |
2146 | MAC2STR(sta->addr)); | |
2147 | ||
2148 | /* First, use a zero key to avoid any possible duplicate key avoidance | |
2149 | * in the driver. */ | |
2150 | if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, | |
2151 | sta->addr, sta->last_tk_key_idx, 1, NULL, 0, | |
2152 | zero, sta->last_tk_len) < 0) | |
2153 | return -1; | |
2154 | ||
2155 | /* Set the previously configured key to reset its TSC/RSC */ | |
2156 | return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, | |
2157 | sta->addr, sta->last_tk_key_idx, 1, NULL, 0, | |
2158 | sta->last_tk, sta->last_tk_len); | |
2159 | } | |
2160 | ||
6bc2f00f | 2161 | |
143b81ba JM |
2162 | static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd) |
2163 | { | |
2164 | u8 addr[ETH_ALEN]; | |
2165 | const char *pos = cmd; | |
2166 | enum wpa_alg alg; | |
2167 | int idx, set_tx; | |
2168 | u8 seq[6], key[WPA_TK_MAX_LEN]; | |
2169 | size_t key_len; | |
2170 | ||
2171 | /* parameters: alg addr idx set_tx seq key */ | |
2172 | ||
2173 | alg = atoi(pos); | |
2174 | pos = os_strchr(pos, ' '); | |
2175 | if (!pos) | |
2176 | return -1; | |
2177 | pos++; | |
2178 | if (hwaddr_aton(pos, addr)) | |
2179 | return -1; | |
2180 | pos += 17; | |
2181 | if (*pos != ' ') | |
2182 | return -1; | |
2183 | pos++; | |
2184 | idx = atoi(pos); | |
2185 | pos = os_strchr(pos, ' '); | |
2186 | if (!pos) | |
2187 | return -1; | |
2188 | pos++; | |
2189 | set_tx = atoi(pos); | |
2190 | pos = os_strchr(pos, ' '); | |
2191 | if (!pos) | |
2192 | return -1; | |
2193 | pos++; | |
10c83475 | 2194 | if (hexstr2bin(pos, seq, sizeof(seq)) < 0) |
143b81ba JM |
2195 | return -1; |
2196 | pos += 2 * 6; | |
2197 | if (*pos != ' ') | |
2198 | return -1; | |
2199 | pos++; | |
2200 | key_len = os_strlen(pos) / 2; | |
2201 | if (hexstr2bin(pos, key, key_len) < 0) | |
2202 | return -1; | |
2203 | ||
2204 | wpa_printf(MSG_INFO, "TESTING: Set key"); | |
2205 | return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, | |
2206 | set_tx, seq, 6, key, key_len); | |
2207 | } | |
2208 | ||
2209 | ||
3d695328 JM |
2210 | static void restore_tk(void *ctx1, void *ctx2) |
2211 | { | |
2212 | struct hostapd_data *hapd = ctx1; | |
2213 | struct sta_info *sta = ctx2; | |
2214 | ||
2215 | wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR, | |
2216 | MAC2STR(sta->addr)); | |
2217 | /* This does not really restore the TSC properly, so this will result | |
2218 | * in replay protection issues for now since there is no clean way of | |
2219 | * preventing encryption of a single EAPOL frame. */ | |
2220 | hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg, | |
2221 | sta->addr, sta->last_tk_key_idx, 1, NULL, 0, | |
2222 | sta->last_tk, sta->last_tk_len); | |
2223 | } | |
2224 | ||
2225 | ||
d8afdb21 JM |
2226 | static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd) |
2227 | { | |
2228 | struct sta_info *sta; | |
2229 | u8 addr[ETH_ALEN]; | |
3d695328 | 2230 | int plain = os_strstr(cmd, "plaintext") != NULL; |
d8afdb21 JM |
2231 | |
2232 | if (hwaddr_aton(cmd, addr)) | |
2233 | return -1; | |
2234 | ||
2235 | sta = ap_get_sta(hapd, addr); | |
2236 | if (!sta || !sta->wpa_sm) | |
2237 | return -1; | |
2238 | ||
3d695328 JM |
2239 | if (plain && sta->last_tk_alg == WPA_ALG_NONE) |
2240 | plain = 0; /* no need for special processing */ | |
2241 | if (plain) { | |
2242 | wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, | |
2243 | MAC2STR(sta->addr)); | |
2244 | hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, | |
2245 | sta->addr, sta->last_tk_key_idx, 0, NULL, 0, | |
2246 | NULL, 0); | |
2247 | } | |
2248 | ||
d8afdb21 JM |
2249 | wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr)); |
2250 | return wpa_auth_resend_m1(sta->wpa_sm, | |
3d695328 JM |
2251 | os_strstr(cmd, "change-anonce") != NULL, |
2252 | plain ? restore_tk : NULL, hapd, sta); | |
d8afdb21 JM |
2253 | } |
2254 | ||
2255 | ||
2256 | static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd) | |
2257 | { | |
2258 | struct sta_info *sta; | |
2259 | u8 addr[ETH_ALEN]; | |
3d695328 | 2260 | int plain = os_strstr(cmd, "plaintext") != NULL; |
d8afdb21 JM |
2261 | |
2262 | if (hwaddr_aton(cmd, addr)) | |
2263 | return -1; | |
2264 | ||
2265 | sta = ap_get_sta(hapd, addr); | |
2266 | if (!sta || !sta->wpa_sm) | |
2267 | return -1; | |
2268 | ||
3d695328 JM |
2269 | if (plain && sta->last_tk_alg == WPA_ALG_NONE) |
2270 | plain = 0; /* no need for special processing */ | |
2271 | if (plain) { | |
2272 | wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, | |
2273 | MAC2STR(sta->addr)); | |
2274 | hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, | |
2275 | sta->addr, sta->last_tk_key_idx, 0, NULL, 0, | |
2276 | NULL, 0); | |
2277 | } | |
2278 | ||
d8afdb21 | 2279 | wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr)); |
3d695328 JM |
2280 | return wpa_auth_resend_m3(sta->wpa_sm, |
2281 | plain ? restore_tk : NULL, hapd, sta); | |
d8afdb21 JM |
2282 | } |
2283 | ||
2284 | ||
6bc2f00f JM |
2285 | static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd, |
2286 | const char *cmd) | |
2287 | { | |
2288 | struct sta_info *sta; | |
2289 | u8 addr[ETH_ALEN]; | |
3d695328 | 2290 | int plain = os_strstr(cmd, "plaintext") != NULL; |
6bc2f00f JM |
2291 | |
2292 | if (hwaddr_aton(cmd, addr)) | |
2293 | return -1; | |
2294 | ||
2295 | sta = ap_get_sta(hapd, addr); | |
2296 | if (!sta || !sta->wpa_sm) | |
2297 | return -1; | |
2298 | ||
3d695328 JM |
2299 | if (plain && sta->last_tk_alg == WPA_ALG_NONE) |
2300 | plain = 0; /* no need for special processing */ | |
2301 | if (plain) { | |
2302 | wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR, | |
2303 | MAC2STR(sta->addr)); | |
2304 | hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE, | |
2305 | sta->addr, sta->last_tk_key_idx, 0, NULL, 0, | |
2306 | NULL, 0); | |
2307 | } | |
2308 | ||
6bc2f00f JM |
2309 | wpa_printf(MSG_INFO, |
2310 | "TESTING: Send group M1 for the same GTK and zero RSC to " | |
2311 | MACSTR, MAC2STR(sta->addr)); | |
3d695328 JM |
2312 | return wpa_auth_resend_group_m1(sta->wpa_sm, |
2313 | plain ? restore_tk : NULL, hapd, sta); | |
6bc2f00f JM |
2314 | } |
2315 | ||
ddf55174 JM |
2316 | #endif /* CONFIG_TESTING_OPTIONS */ |
2317 | ||
2318 | ||
ccac7c61 MK |
2319 | static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface, |
2320 | char *pos) | |
334bf36a AO |
2321 | { |
2322 | #ifdef NEED_AP_MLME | |
2323 | struct csa_settings settings; | |
ccac7c61 MK |
2324 | int ret; |
2325 | unsigned int i; | |
334bf36a | 2326 | |
ccac7c61 | 2327 | ret = hostapd_parse_csa_settings(pos, &settings); |
334bf36a AO |
2328 | if (ret) |
2329 | return ret; | |
2330 | ||
ccac7c61 | 2331 | for (i = 0; i < iface->num_bss; i++) { |
bda9c085 SM |
2332 | |
2333 | /* Save CHAN_SWITCH VHT config */ | |
2334 | hostapd_chan_switch_vht_config( | |
2335 | iface->bss[i], settings.freq_params.vht_enabled); | |
2336 | ||
ccac7c61 MK |
2337 | ret = hostapd_switch_channel(iface->bss[i], &settings); |
2338 | if (ret) { | |
2339 | /* FIX: What do we do if CSA fails in the middle of | |
2340 | * submitting multi-BSS CSA requests? */ | |
2341 | return ret; | |
2342 | } | |
2343 | } | |
2344 | ||
2345 | return 0; | |
334bf36a AO |
2346 | #else /* NEED_AP_MLME */ |
2347 | return -1; | |
2348 | #endif /* NEED_AP_MLME */ | |
2349 | } | |
2350 | ||
2351 | ||
4c03a2b3 JM |
2352 | static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply, |
2353 | int reply_size, const char *param) | |
2354 | { | |
2355 | #ifdef RADIUS_SERVER | |
2356 | if (os_strcmp(param, "radius_server") == 0) { | |
2357 | return radius_server_get_mib(hapd->radius_srv, reply, | |
2358 | reply_size); | |
2359 | } | |
2360 | #endif /* RADIUS_SERVER */ | |
2361 | return -1; | |
2362 | } | |
2363 | ||
2364 | ||
3ae8b7b7 AS |
2365 | static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd, |
2366 | char *buf, size_t buflen) | |
2367 | { | |
2368 | int ret; | |
2369 | char *pos; | |
2370 | u8 *data = NULL; | |
2371 | unsigned int vendor_id, subcmd; | |
2372 | struct wpabuf *reply; | |
2373 | size_t data_len = 0; | |
2374 | ||
2375 | /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */ | |
2376 | vendor_id = strtoul(cmd, &pos, 16); | |
640b0b93 | 2377 | if (!isblank((unsigned char) *pos)) |
3ae8b7b7 AS |
2378 | return -EINVAL; |
2379 | ||
2380 | subcmd = strtoul(pos, &pos, 10); | |
2381 | ||
2382 | if (*pos != '\0') { | |
640b0b93 | 2383 | if (!isblank((unsigned char) *pos++)) |
3ae8b7b7 AS |
2384 | return -EINVAL; |
2385 | data_len = os_strlen(pos); | |
2386 | } | |
2387 | ||
2388 | if (data_len) { | |
2389 | data_len /= 2; | |
2390 | data = os_malloc(data_len); | |
2391 | if (!data) | |
2392 | return -ENOBUFS; | |
2393 | ||
2394 | if (hexstr2bin(pos, data, data_len)) { | |
2395 | wpa_printf(MSG_DEBUG, | |
2396 | "Vendor command: wrong parameter format"); | |
2397 | os_free(data); | |
2398 | return -EINVAL; | |
2399 | } | |
2400 | } | |
2401 | ||
2402 | reply = wpabuf_alloc((buflen - 1) / 2); | |
2403 | if (!reply) { | |
2404 | os_free(data); | |
2405 | return -ENOBUFS; | |
2406 | } | |
2407 | ||
2408 | ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len, | |
2409 | reply); | |
2410 | ||
2411 | if (ret == 0) | |
2412 | ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply), | |
2413 | wpabuf_len(reply)); | |
2414 | ||
2415 | wpabuf_free(reply); | |
2416 | os_free(data); | |
2417 | ||
2418 | return ret; | |
2419 | } | |
2420 | ||
2421 | ||
cfb5c08f JM |
2422 | static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd, |
2423 | const char *cmd) | |
2424 | { | |
2425 | u8 addr[ETH_ALEN]; | |
2426 | struct sta_info *sta; | |
2427 | ||
2428 | if (hwaddr_aton(cmd, addr)) | |
2429 | return -1; | |
2430 | ||
2431 | sta = ap_get_sta(hapd, addr); | |
2432 | if (!sta || !sta->eapol_sm) | |
2433 | return -1; | |
2434 | ||
2435 | eapol_auth_reauthenticate(sta->eapol_sm); | |
2436 | return 0; | |
2437 | } | |
2438 | ||
2439 | ||
3722c0f4 JM |
2440 | static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd) |
2441 | { | |
2442 | u8 addr[ETH_ALEN]; | |
2443 | struct sta_info *sta; | |
2444 | char *pos = cmd, *param; | |
2445 | ||
2446 | if (hwaddr_aton(pos, addr) || pos[17] != ' ') | |
2447 | return -1; | |
2448 | pos += 18; | |
2449 | param = pos; | |
2450 | pos = os_strchr(pos, ' '); | |
2451 | if (!pos) | |
2452 | return -1; | |
2453 | *pos++ = '\0'; | |
2454 | ||
2455 | sta = ap_get_sta(hapd, addr); | |
2456 | if (!sta || !sta->eapol_sm) | |
2457 | return -1; | |
2458 | ||
2459 | return eapol_auth_set_conf(sta->eapol_sm, param, pos); | |
2460 | } | |
2461 | ||
2462 | ||
5c4f0511 SD |
2463 | static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd, |
2464 | char *buf, size_t buflen) | |
2465 | { | |
2466 | char *pos, *end, *stamp; | |
2467 | int ret; | |
2468 | ||
2469 | /* cmd: "LOG_LEVEL [<level>]" */ | |
2470 | if (*cmd == '\0') { | |
2471 | pos = buf; | |
2472 | end = buf + buflen; | |
2473 | ret = os_snprintf(pos, end - pos, "Current level: %s\n" | |
2474 | "Timestamp: %d\n", | |
2475 | debug_level_str(wpa_debug_level), | |
2476 | wpa_debug_timestamp); | |
2477 | if (os_snprintf_error(end - pos, ret)) | |
2478 | ret = 0; | |
2479 | ||
2480 | return ret; | |
2481 | } | |
2482 | ||
2483 | while (*cmd == ' ') | |
2484 | cmd++; | |
2485 | ||
2486 | stamp = os_strchr(cmd, ' '); | |
2487 | if (stamp) { | |
2488 | *stamp++ = '\0'; | |
2489 | while (*stamp == ' ') { | |
2490 | stamp++; | |
2491 | } | |
2492 | } | |
2493 | ||
137b2939 | 2494 | if (os_strlen(cmd)) { |
5c4f0511 SD |
2495 | int level = str_to_debug_level(cmd); |
2496 | if (level < 0) | |
2497 | return -1; | |
2498 | wpa_debug_level = level; | |
2499 | } | |
2500 | ||
2501 | if (stamp && os_strlen(stamp)) | |
2502 | wpa_debug_timestamp = atoi(stamp); | |
2503 | ||
2504 | os_memcpy(buf, "OK\n", 3); | |
2505 | return 3; | |
2506 | } | |
2507 | ||
2508 | ||
a65a9b8d JM |
2509 | #ifdef NEED_AP_MLME |
2510 | static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd, | |
2511 | char *buf, size_t buflen) | |
2512 | { | |
2513 | struct hostapd_iface *iface = hapd->iface; | |
2514 | char *pos, *end; | |
2515 | struct hostapd_sta_info *info; | |
2516 | struct os_reltime now; | |
2517 | ||
30e0745b JM |
2518 | if (!iface->num_sta_seen) |
2519 | return 0; | |
2520 | ||
a65a9b8d JM |
2521 | sta_track_expire(iface, 0); |
2522 | ||
2523 | pos = buf; | |
2524 | end = buf + buflen; | |
2525 | ||
2526 | os_get_reltime(&now); | |
2527 | dl_list_for_each_reverse(info, &iface->sta_seen, | |
2528 | struct hostapd_sta_info, list) { | |
2529 | struct os_reltime age; | |
2530 | int ret; | |
2531 | ||
2532 | os_reltime_sub(&now, &info->last_seen, &age); | |
a818425d KM |
2533 | ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n", |
2534 | MAC2STR(info->addr), (unsigned int) age.sec, | |
2535 | info->ssi_signal); | |
a65a9b8d JM |
2536 | if (os_snprintf_error(end - pos, ret)) |
2537 | break; | |
2538 | pos += ret; | |
2539 | } | |
2540 | ||
2541 | return pos - buf; | |
2542 | } | |
2543 | #endif /* NEED_AP_MLME */ | |
2544 | ||
2545 | ||
f4f185a2 DS |
2546 | static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd, |
2547 | const char *cmd) | |
2548 | { | |
2549 | u8 addr[ETH_ALEN]; | |
2550 | ||
2551 | if (hwaddr_aton(cmd, addr)) { | |
2552 | wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address"); | |
2553 | return -1; | |
2554 | } | |
2555 | ||
2556 | return hostapd_send_lci_req(hapd, addr); | |
2557 | } | |
2558 | ||
2559 | ||
f367c08d | 2560 | static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd) |
220754c5 DS |
2561 | { |
2562 | u8 addr[ETH_ALEN]; | |
2563 | char *token, *context = NULL; | |
2564 | int random_interval, min_ap; | |
2565 | u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS]; | |
2566 | unsigned int n_responders; | |
2567 | ||
2568 | token = str_token(cmd, " ", &context); | |
2569 | if (!token || hwaddr_aton(token, addr)) { | |
2570 | wpa_printf(MSG_INFO, | |
2571 | "CTRL: REQ_RANGE - Bad destination address"); | |
2572 | return -1; | |
2573 | } | |
2574 | ||
2575 | token = str_token(cmd, " ", &context); | |
2576 | if (!token) | |
2577 | return -1; | |
2578 | ||
2579 | random_interval = atoi(token); | |
2580 | if (random_interval < 0 || random_interval > 0xffff) | |
2581 | return -1; | |
2582 | ||
2583 | token = str_token(cmd, " ", &context); | |
2584 | if (!token) | |
2585 | return -1; | |
2586 | ||
2587 | min_ap = atoi(token); | |
2588 | if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP) | |
2589 | return -1; | |
2590 | ||
2591 | n_responders = 0; | |
2592 | while ((token = str_token(cmd, " ", &context))) { | |
2593 | if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) { | |
2594 | wpa_printf(MSG_INFO, | |
2595 | "CTRL: REQ_RANGE: Too many responders"); | |
2596 | return -1; | |
2597 | } | |
2598 | ||
2599 | if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) { | |
2600 | wpa_printf(MSG_INFO, | |
2601 | "CTRL: REQ_RANGE: Bad responder address"); | |
2602 | return -1; | |
2603 | } | |
2604 | ||
2605 | n_responders++; | |
2606 | } | |
2607 | ||
2608 | if (!n_responders) { | |
2609 | wpa_printf(MSG_INFO, | |
2610 | "CTRL: REQ_RANGE - No FTM responder address"); | |
2611 | return -1; | |
2612 | } | |
2613 | ||
2614 | return hostapd_send_range_req(hapd, addr, random_interval, min_ap, | |
2615 | responders, n_responders); | |
2616 | } | |
2617 | ||
2618 | ||
90d9d7c3 JM |
2619 | static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd, |
2620 | const char *cmd, char *reply, | |
2621 | size_t reply_size) | |
2622 | { | |
2623 | u8 addr[ETH_ALEN]; | |
2624 | const char *pos; | |
2625 | struct wpabuf *req; | |
2626 | int ret; | |
2627 | u8 req_mode = 0; | |
2628 | ||
2629 | if (hwaddr_aton(cmd, addr)) | |
2630 | return -1; | |
2631 | pos = os_strchr(cmd, ' '); | |
2632 | if (!pos) | |
2633 | return -1; | |
2634 | pos++; | |
2635 | if (os_strncmp(pos, "req_mode=", 9) == 0) { | |
2636 | int val = hex2byte(pos + 9); | |
2637 | ||
2638 | if (val < 0) | |
2639 | return -1; | |
2640 | req_mode = val; | |
2641 | pos += 11; | |
2642 | pos = os_strchr(pos, ' '); | |
2643 | if (!pos) | |
2644 | return -1; | |
2645 | pos++; | |
2646 | } | |
2647 | req = wpabuf_parse_bin(pos); | |
2648 | if (!req) | |
2649 | return -1; | |
2650 | ||
2651 | ret = hostapd_send_beacon_req(hapd, addr, req_mode, req); | |
2652 | wpabuf_free(req); | |
2653 | if (ret >= 0) | |
2654 | ret = os_snprintf(reply, reply_size, "%d", ret); | |
2655 | return ret; | |
2656 | } | |
2657 | ||
2658 | ||
9b4b2264 DS |
2659 | static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf) |
2660 | { | |
2661 | struct wpa_ssid_value ssid; | |
2662 | u8 bssid[ETH_ALEN]; | |
2663 | struct wpabuf *nr, *lci = NULL, *civic = NULL; | |
451a27b1 | 2664 | int stationary = 0; |
9b4b2264 DS |
2665 | char *tmp; |
2666 | int ret; | |
2667 | ||
2668 | if (!(hapd->conf->radio_measurements[0] & | |
2669 | WLAN_RRM_CAPS_NEIGHBOR_REPORT)) { | |
2670 | wpa_printf(MSG_ERROR, | |
2671 | "CTRL: SET_NEIGHBOR: Neighbor report is not enabled"); | |
2672 | return -1; | |
2673 | } | |
2674 | ||
2675 | if (hwaddr_aton(buf, bssid)) { | |
2676 | wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID"); | |
2677 | return -1; | |
2678 | } | |
2679 | ||
2680 | tmp = os_strstr(buf, "ssid="); | |
2681 | if (!tmp || ssid_parse(tmp + 5, &ssid)) { | |
2682 | wpa_printf(MSG_ERROR, | |
2683 | "CTRL: SET_NEIGHBOR: Bad or missing SSID"); | |
2684 | return -1; | |
2685 | } | |
2686 | buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' '); | |
2687 | if (!buf) | |
2688 | return -1; | |
2689 | ||
2690 | tmp = os_strstr(buf, "nr="); | |
2691 | if (!tmp) { | |
2692 | wpa_printf(MSG_ERROR, | |
2693 | "CTRL: SET_NEIGHBOR: Missing Neighbor Report element"); | |
2694 | return -1; | |
2695 | } | |
2696 | ||
2697 | buf = os_strchr(tmp, ' '); | |
2698 | if (buf) | |
2699 | *buf++ = '\0'; | |
2700 | ||
2701 | nr = wpabuf_parse_bin(tmp + 3); | |
2702 | if (!nr) { | |
2703 | wpa_printf(MSG_ERROR, | |
2704 | "CTRL: SET_NEIGHBOR: Bad Neighbor Report element"); | |
2705 | return -1; | |
2706 | } | |
2707 | ||
2708 | if (!buf) | |
2709 | goto set; | |
2710 | ||
2711 | tmp = os_strstr(buf, "lci="); | |
2712 | if (tmp) { | |
2713 | buf = os_strchr(tmp, ' '); | |
2714 | if (buf) | |
2715 | *buf++ = '\0'; | |
2716 | lci = wpabuf_parse_bin(tmp + 4); | |
2717 | if (!lci) { | |
2718 | wpa_printf(MSG_ERROR, | |
2719 | "CTRL: SET_NEIGHBOR: Bad LCI subelement"); | |
2720 | wpabuf_free(nr); | |
2721 | return -1; | |
2722 | } | |
2723 | } | |
2724 | ||
2725 | if (!buf) | |
2726 | goto set; | |
2727 | ||
2728 | tmp = os_strstr(buf, "civic="); | |
2729 | if (tmp) { | |
2730 | buf = os_strchr(tmp, ' '); | |
2731 | if (buf) | |
2732 | *buf++ = '\0'; | |
2733 | civic = wpabuf_parse_bin(tmp + 6); | |
2734 | if (!civic) { | |
2735 | wpa_printf(MSG_ERROR, | |
2736 | "CTRL: SET_NEIGHBOR: Bad civic subelement"); | |
2737 | wpabuf_free(nr); | |
2738 | wpabuf_free(lci); | |
2739 | return -1; | |
2740 | } | |
2741 | } | |
2742 | ||
451a27b1 DS |
2743 | if (!buf) |
2744 | goto set; | |
2745 | ||
2746 | if (os_strstr(buf, "stat")) | |
2747 | stationary = 1; | |
2748 | ||
9b4b2264 | 2749 | set: |
451a27b1 DS |
2750 | ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic, |
2751 | stationary); | |
9b4b2264 DS |
2752 | |
2753 | wpabuf_free(nr); | |
2754 | wpabuf_free(lci); | |
2755 | wpabuf_free(civic); | |
2756 | ||
2757 | return ret; | |
2758 | } | |
2759 | ||
2760 | ||
2761 | static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd, | |
2762 | char *buf) | |
2763 | { | |
2764 | struct wpa_ssid_value ssid; | |
2765 | u8 bssid[ETH_ALEN]; | |
2766 | char *tmp; | |
2767 | ||
2768 | if (hwaddr_aton(buf, bssid)) { | |
2769 | wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID"); | |
2770 | return -1; | |
2771 | } | |
2772 | ||
2773 | tmp = os_strstr(buf, "ssid="); | |
2774 | if (!tmp || ssid_parse(tmp + 5, &ssid)) { | |
2775 | wpa_printf(MSG_ERROR, | |
2776 | "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID"); | |
2777 | return -1; | |
2778 | } | |
2779 | ||
2780 | return hostapd_neighbor_remove(hapd, bssid, &ssid); | |
2781 | } | |
2782 | ||
2783 | ||
4d7aab78 EL |
2784 | static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf, |
2785 | size_t buflen) | |
2786 | { | |
2787 | int ret, i; | |
2788 | char *pos, *end; | |
2789 | ||
2790 | ret = os_snprintf(buf, buflen, "%016llX:\n", | |
2791 | (long long unsigned) iface->drv_flags); | |
2792 | if (os_snprintf_error(buflen, ret)) | |
2793 | return -1; | |
2794 | ||
2795 | pos = buf + ret; | |
2796 | end = buf + buflen; | |
2797 | ||
2798 | for (i = 0; i < 64; i++) { | |
2799 | if (iface->drv_flags & (1LLU << i)) { | |
2800 | ret = os_snprintf(pos, end - pos, "%s\n", | |
2801 | driver_flag_to_string(1LLU << i)); | |
2802 | if (os_snprintf_error(end - pos, ret)) | |
2803 | return -1; | |
2804 | pos += ret; | |
2805 | } | |
2806 | } | |
2807 | ||
2808 | return pos - buf; | |
2809 | } | |
2810 | ||
2811 | ||
3988046d T |
2812 | static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num, |
2813 | const char *txtaddr) | |
2814 | { | |
2815 | u8 addr[ETH_ALEN]; | |
2816 | struct vlan_description vlan_id; | |
2817 | ||
2818 | if (!(*num)) | |
2819 | return 0; | |
2820 | ||
2821 | if (hwaddr_aton(txtaddr, addr)) | |
2822 | return -1; | |
2823 | ||
2824 | if (hostapd_maclist_found(*acl, *num, addr, &vlan_id)) | |
2825 | hostapd_remove_acl_mac(acl, num, addr); | |
2826 | ||
2827 | return 0; | |
2828 | } | |
2829 | ||
2830 | ||
2831 | static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl, | |
2832 | int *num) | |
2833 | { | |
2834 | while (*num) | |
2835 | hostapd_remove_acl_mac(acl, num, (*acl)[0].addr); | |
2836 | } | |
2837 | ||
2838 | ||
2839 | static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num, | |
2840 | char *buf, size_t buflen) | |
2841 | { | |
2842 | int i = 0, len = 0, ret = 0; | |
2843 | ||
2844 | if (!acl) | |
2845 | return 0; | |
2846 | ||
2847 | while (i < num) { | |
2848 | ret = os_snprintf(buf + len, buflen - len, | |
2849 | MACSTR " VLAN_ID=%d\n", | |
2850 | MAC2STR(acl[i].addr), | |
2851 | acl[i].vlan_id.untagged); | |
2852 | if (ret < 0 || (size_t) ret >= buflen - len) | |
2853 | return len; | |
2854 | i++; | |
2855 | len += ret; | |
2856 | } | |
2857 | return len; | |
2858 | } | |
2859 | ||
2860 | ||
2861 | static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num, | |
2862 | const char *cmd) | |
2863 | { | |
2864 | u8 addr[ETH_ALEN]; | |
2865 | struct vlan_description vlan_id; | |
2866 | int ret = 0, vlanid = 0; | |
2867 | const char *pos; | |
2868 | ||
2869 | if (hwaddr_aton(cmd, addr)) | |
2870 | return -1; | |
2871 | ||
2872 | pos = os_strstr(cmd, "VLAN_ID="); | |
2873 | if (pos) | |
2874 | vlanid = atoi(pos + 8); | |
2875 | ||
2876 | if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) { | |
2877 | ret = hostapd_add_acl_maclist(acl, num, vlanid, addr); | |
2878 | if (ret != -1 && *acl) | |
2879 | qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp); | |
2880 | } | |
2881 | ||
2882 | return ret < 0 ? -1 : 0; | |
2883 | } | |
2884 | ||
2885 | ||
585478e2 AN |
2886 | static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd, |
2887 | char *buf, char *reply, | |
2888 | int reply_size, | |
acf57fae | 2889 | struct sockaddr_storage *from, |
585478e2 | 2890 | socklen_t fromlen) |
6fc6879b | 2891 | { |
585478e2 | 2892 | int reply_len, res; |
6fc6879b JM |
2893 | |
2894 | os_memcpy(reply, "OK\n", 3); | |
2895 | reply_len = 3; | |
2896 | ||
2897 | if (os_strcmp(buf, "PING") == 0) { | |
2898 | os_memcpy(reply, "PONG\n", 5); | |
2899 | reply_len = 5; | |
b41a47c0 BG |
2900 | } else if (os_strncmp(buf, "RELOG", 5) == 0) { |
2901 | if (wpa_debug_reopen_file() < 0) | |
2902 | reply_len = -1; | |
f6de7253 JM |
2903 | } else if (os_strncmp(buf, "NOTE ", 5) == 0) { |
2904 | wpa_printf(MSG_INFO, "NOTE: %s", buf + 5); | |
5ae6449c JM |
2905 | } else if (os_strcmp(buf, "STATUS") == 0) { |
2906 | reply_len = hostapd_ctrl_iface_status(hapd, reply, | |
2907 | reply_size); | |
f0cbb986 JM |
2908 | } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) { |
2909 | reply_len = hostapd_drv_status(hapd, reply, reply_size); | |
6fc6879b JM |
2910 | } else if (os_strcmp(buf, "MIB") == 0) { |
2911 | reply_len = ieee802_11_get_mib(hapd, reply, reply_size); | |
2912 | if (reply_len >= 0) { | |
2913 | res = wpa_get_mib(hapd->wpa_auth, reply + reply_len, | |
2914 | reply_size - reply_len); | |
2915 | if (res < 0) | |
2916 | reply_len = -1; | |
2917 | else | |
2918 | reply_len += res; | |
2919 | } | |
2920 | if (reply_len >= 0) { | |
2921 | res = ieee802_1x_get_mib(hapd, reply + reply_len, | |
2922 | reply_size - reply_len); | |
2923 | if (res < 0) | |
2924 | reply_len = -1; | |
2925 | else | |
2926 | reply_len += res; | |
2927 | } | |
74784010 | 2928 | #ifndef CONFIG_NO_RADIUS |
6fc6879b JM |
2929 | if (reply_len >= 0) { |
2930 | res = radius_client_get_mib(hapd->radius, | |
2931 | reply + reply_len, | |
2932 | reply_size - reply_len); | |
2933 | if (res < 0) | |
2934 | reply_len = -1; | |
2935 | else | |
2936 | reply_len += res; | |
2937 | } | |
74784010 | 2938 | #endif /* CONFIG_NO_RADIUS */ |
4c03a2b3 JM |
2939 | } else if (os_strncmp(buf, "MIB ", 4) == 0) { |
2940 | reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size, | |
2941 | buf + 4); | |
6fc6879b JM |
2942 | } else if (os_strcmp(buf, "STA-FIRST") == 0) { |
2943 | reply_len = hostapd_ctrl_iface_sta_first(hapd, reply, | |
2944 | reply_size); | |
2945 | } else if (os_strncmp(buf, "STA ", 4) == 0) { | |
2946 | reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply, | |
2947 | reply_size); | |
2948 | } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) { | |
2949 | reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply, | |
2950 | reply_size); | |
2951 | } else if (os_strcmp(buf, "ATTACH") == 0) { | |
cc79e06f | 2952 | if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL)) |
2953 | reply_len = -1; | |
2954 | } else if (os_strncmp(buf, "ATTACH ", 7) == 0) { | |
2955 | if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7)) | |
6fc6879b JM |
2956 | reply_len = -1; |
2957 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
585478e2 | 2958 | if (hostapd_ctrl_iface_detach(hapd, from, fromlen)) |
6fc6879b JM |
2959 | reply_len = -1; |
2960 | } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { | |
585478e2 | 2961 | if (hostapd_ctrl_iface_level(hapd, from, fromlen, |
6fc6879b JM |
2962 | buf + 6)) |
2963 | reply_len = -1; | |
2964 | } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) { | |
2965 | if (hostapd_ctrl_iface_new_sta(hapd, buf + 8)) | |
2966 | reply_len = -1; | |
90a3206a JM |
2967 | } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) { |
2968 | if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15)) | |
2969 | reply_len = -1; | |
2970 | } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) { | |
2971 | if (hostapd_ctrl_iface_disassociate(hapd, buf + 13)) | |
2972 | reply_len = -1; | |
04059ab8 DG |
2973 | #ifdef CONFIG_TAXONOMY |
2974 | } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) { | |
2975 | reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10, | |
2976 | reply, reply_size); | |
2977 | #endif /* CONFIG_TAXONOMY */ | |
1854eeca JM |
2978 | } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) { |
2979 | if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9)) | |
2980 | reply_len = -1; | |
99650cad JM |
2981 | } else if (os_strcmp(buf, "STOP_AP") == 0) { |
2982 | if (hostapd_ctrl_iface_stop_ap(hapd)) | |
2983 | reply_len = -1; | |
88b4b424 | 2984 | #ifdef CONFIG_IEEE80211W |
fe6bdb77 | 2985 | #ifdef NEED_AP_MLME |
88b4b424 JM |
2986 | } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) { |
2987 | if (hostapd_ctrl_iface_sa_query(hapd, buf + 9)) | |
2988 | reply_len = -1; | |
fe6bdb77 | 2989 | #endif /* NEED_AP_MLME */ |
88b4b424 | 2990 | #endif /* CONFIG_IEEE80211W */ |
ad08c363 JM |
2991 | #ifdef CONFIG_WPS |
2992 | } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) { | |
2993 | if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8)) | |
2994 | reply_len = -1; | |
3981cb3c JM |
2995 | } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) { |
2996 | reply_len = hostapd_ctrl_iface_wps_check_pin( | |
2997 | hapd, buf + 14, reply, reply_size); | |
ad08c363 | 2998 | } else if (os_strcmp(buf, "WPS_PBC") == 0) { |
d601247c | 2999 | if (hostapd_wps_button_pushed(hapd, NULL)) |
ad08c363 | 3000 | reply_len = -1; |
4c374cde AS |
3001 | } else if (os_strcmp(buf, "WPS_CANCEL") == 0) { |
3002 | if (hostapd_wps_cancel(hapd)) | |
3003 | reply_len = -1; | |
5a1cc30f JM |
3004 | } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) { |
3005 | reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11, | |
3006 | reply, reply_size); | |
450eddcf JM |
3007 | } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) { |
3008 | if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0) | |
3009 | reply_len = -1; | |
3351a384 JM |
3010 | } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) { |
3011 | reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply, | |
3012 | reply_size); | |
bb45b6d7 JM |
3013 | #ifdef CONFIG_WPS_NFC |
3014 | } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) { | |
3015 | if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17)) | |
3016 | reply_len = -1; | |
3cf7a59d JM |
3017 | } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) { |
3018 | reply_len = hostapd_ctrl_iface_wps_nfc_config_token( | |
3019 | hapd, buf + 21, reply, reply_size); | |
ffdaa05a JM |
3020 | } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) { |
3021 | reply_len = hostapd_ctrl_iface_wps_nfc_token( | |
3022 | hapd, buf + 14, reply, reply_size); | |
6772a90a JM |
3023 | } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) { |
3024 | reply_len = hostapd_ctrl_iface_nfc_get_handover_sel( | |
3025 | hapd, buf + 21, reply, reply_size); | |
e4758827 JM |
3026 | } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) { |
3027 | if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20)) | |
3028 | reply_len = -1; | |
bb45b6d7 | 3029 | #endif /* CONFIG_WPS_NFC */ |
ad08c363 | 3030 | #endif /* CONFIG_WPS */ |
c551700f KP |
3031 | #ifdef CONFIG_INTERWORKING |
3032 | } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) { | |
3033 | if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16)) | |
3034 | reply_len = -1; | |
3035 | } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) { | |
3036 | if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18)) | |
3037 | reply_len = -1; | |
3038 | #endif /* CONFIG_INTERWORKING */ | |
3fb17a95 JM |
3039 | #ifdef CONFIG_HS20 |
3040 | } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) { | |
3041 | if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15)) | |
3042 | reply_len = -1; | |
8e1146d9 JM |
3043 | } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) { |
3044 | if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16)) | |
3045 | reply_len = -1; | |
3fb17a95 | 3046 | #endif /* CONFIG_HS20 */ |
b5bf84ba | 3047 | #ifdef CONFIG_WNM_AP |
2049a875 JM |
3048 | } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) { |
3049 | if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18)) | |
3050 | reply_len = -1; | |
71269b37 JM |
3051 | } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) { |
3052 | if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13)) | |
3053 | reply_len = -1; | |
a30dff07 JM |
3054 | } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) { |
3055 | if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11)) | |
3056 | reply_len = -1; | |
d514b502 JM |
3057 | } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) { |
3058 | if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15)) | |
3059 | reply_len = -1; | |
b5bf84ba | 3060 | #endif /* CONFIG_WNM_AP */ |
403b96fe JM |
3061 | } else if (os_strcmp(buf, "GET_CONFIG") == 0) { |
3062 | reply_len = hostapd_ctrl_iface_get_config(hapd, reply, | |
3063 | reply_size); | |
b4e34f2f JM |
3064 | } else if (os_strncmp(buf, "SET ", 4) == 0) { |
3065 | if (hostapd_ctrl_iface_set(hapd, buf + 4)) | |
3066 | reply_len = -1; | |
acec8d32 JM |
3067 | } else if (os_strncmp(buf, "GET ", 4) == 0) { |
3068 | reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply, | |
3069 | reply_size); | |
75545652 SP |
3070 | } else if (os_strncmp(buf, "ENABLE", 6) == 0) { |
3071 | if (hostapd_ctrl_iface_enable(hapd->iface)) | |
3072 | reply_len = -1; | |
83c86081 MK |
3073 | } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) { |
3074 | if (hostapd_ctrl_iface_reload_wpa_psk(hapd)) | |
3075 | reply_len = -1; | |
75545652 SP |
3076 | } else if (os_strncmp(buf, "RELOAD", 6) == 0) { |
3077 | if (hostapd_ctrl_iface_reload(hapd->iface)) | |
3078 | reply_len = -1; | |
3079 | } else if (os_strncmp(buf, "DISABLE", 7) == 0) { | |
3080 | if (hostapd_ctrl_iface_disable(hapd->iface)) | |
3081 | reply_len = -1; | |
9ff8dda1 JM |
3082 | } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) { |
3083 | if (ieee802_11_set_beacon(hapd)) | |
3084 | reply_len = -1; | |
ddf55174 JM |
3085 | #ifdef CONFIG_TESTING_OPTIONS |
3086 | } else if (os_strncmp(buf, "RADAR ", 6) == 0) { | |
3087 | if (hostapd_ctrl_iface_radar(hapd, buf + 6)) | |
3088 | reply_len = -1; | |
93827f45 JM |
3089 | } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) { |
3090 | if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8)) | |
3091 | reply_len = -1; | |
df949062 JM |
3092 | } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) { |
3093 | if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd, | |
3094 | buf + 23) < 0) | |
3095 | reply_len = -1; | |
47d74bf0 JM |
3096 | } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) { |
3097 | if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0) | |
3098 | reply_len = -1; | |
9d4ff04a JM |
3099 | } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) { |
3100 | if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0) | |
3101 | reply_len = -1; | |
4a6cc862 JM |
3102 | } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) { |
3103 | if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0) | |
3104 | reply_len = -1; | |
3105 | } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) { | |
3106 | if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0) | |
3107 | reply_len = -1; | |
85660d31 JM |
3108 | } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) { |
3109 | if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0) | |
3110 | reply_len = -1; | |
a156ffda JM |
3111 | } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) { |
3112 | if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0) | |
3113 | reply_len = -1; | |
3114 | } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) { | |
3115 | reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply, | |
3116 | reply_size); | |
2da52565 JM |
3117 | } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) { |
3118 | if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0) | |
3119 | reply_len = -1; | |
3120 | } else if (os_strcmp(buf, "GET_FAIL") == 0) { | |
3121 | reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size); | |
16579769 JM |
3122 | } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) { |
3123 | if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0) | |
3124 | reply_len = -1; | |
143b81ba JM |
3125 | } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) { |
3126 | if (hostapd_ctrl_set_key(hapd, buf + 8) < 0) | |
3127 | reply_len = -1; | |
d8afdb21 JM |
3128 | } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) { |
3129 | if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0) | |
3130 | reply_len = -1; | |
3131 | } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) { | |
3132 | if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0) | |
3133 | reply_len = -1; | |
6bc2f00f JM |
3134 | } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) { |
3135 | if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0) | |
3136 | reply_len = -1; | |
92662fb2 JB |
3137 | } else if (os_strcmp(buf, "REKEY_GTK") == 0) { |
3138 | if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0) | |
3139 | reply_len = -1; | |
ddf55174 | 3140 | #endif /* CONFIG_TESTING_OPTIONS */ |
334bf36a | 3141 | } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) { |
ccac7c61 | 3142 | if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12)) |
334bf36a | 3143 | reply_len = -1; |
3ae8b7b7 AS |
3144 | } else if (os_strncmp(buf, "VENDOR ", 7) == 0) { |
3145 | reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply, | |
3146 | reply_size); | |
2c6411ed JM |
3147 | } else if (os_strcmp(buf, "ERP_FLUSH") == 0) { |
3148 | ieee802_1x_erp_flush(hapd); | |
3149 | #ifdef RADIUS_SERVER | |
3150 | radius_server_erp_flush(hapd->radius_srv); | |
3151 | #endif /* RADIUS_SERVER */ | |
cfb5c08f JM |
3152 | } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) { |
3153 | if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13)) | |
3154 | reply_len = -1; | |
3722c0f4 JM |
3155 | } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) { |
3156 | if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10)) | |
3157 | reply_len = -1; | |
5c4f0511 SD |
3158 | } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) { |
3159 | reply_len = hostapd_ctrl_iface_log_level( | |
3160 | hapd, buf + 9, reply, reply_size); | |
a65a9b8d JM |
3161 | #ifdef NEED_AP_MLME |
3162 | } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) { | |
3163 | reply_len = hostapd_ctrl_iface_track_sta_list( | |
3164 | hapd, reply, reply_size); | |
3165 | #endif /* NEED_AP_MLME */ | |
b8daac18 MH |
3166 | } else if (os_strcmp(buf, "PMKSA") == 0) { |
3167 | reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply, | |
3168 | reply_size); | |
4c522c77 MH |
3169 | } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) { |
3170 | hostapd_ctrl_iface_pmksa_flush(hapd); | |
0c52953b JM |
3171 | } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) { |
3172 | if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0) | |
3173 | reply_len = -1; | |
9b4b2264 DS |
3174 | } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) { |
3175 | if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13)) | |
3176 | reply_len = -1; | |
3177 | } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) { | |
3178 | if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16)) | |
3179 | reply_len = -1; | |
f4f185a2 DS |
3180 | } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) { |
3181 | if (hostapd_ctrl_iface_req_lci(hapd, buf + 8)) | |
3182 | reply_len = -1; | |
220754c5 DS |
3183 | } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) { |
3184 | if (hostapd_ctrl_iface_req_range(hapd, buf + 10)) | |
3185 | reply_len = -1; | |
90d9d7c3 JM |
3186 | } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) { |
3187 | reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11, | |
3188 | reply, reply_size); | |
4d7aab78 EL |
3189 | } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) { |
3190 | reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply, | |
3191 | reply_size); | |
45997cce JM |
3192 | } else if (os_strcmp(buf, "TERMINATE") == 0) { |
3193 | eloop_terminate(); | |
3988046d T |
3194 | } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) { |
3195 | if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) { | |
3196 | if (!hostapd_ctrl_iface_acl_add_mac( | |
3197 | &hapd->conf->accept_mac, | |
3198 | &hapd->conf->num_accept_mac, buf + 19)) | |
3199 | hostapd_disassoc_accept_mac(hapd); | |
3200 | else | |
3201 | reply_len = -1; | |
3202 | } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) { | |
3203 | hostapd_ctrl_iface_acl_del_mac( | |
3204 | &hapd->conf->accept_mac, | |
3205 | &hapd->conf->num_accept_mac, buf + 19); | |
3206 | } else if (os_strcmp(buf + 11, "SHOW") == 0) { | |
3207 | reply_len = hostapd_ctrl_iface_acl_show_mac( | |
3208 | hapd->conf->accept_mac, | |
3209 | hapd->conf->num_accept_mac, reply, reply_size); | |
3210 | } else if (os_strcmp(buf + 11, "CLEAR") == 0) { | |
3211 | hostapd_ctrl_iface_acl_clear_list( | |
3212 | &hapd->conf->accept_mac, | |
3213 | &hapd->conf->num_accept_mac); | |
3214 | } | |
3215 | } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) { | |
3216 | if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) { | |
3217 | if (!hostapd_ctrl_iface_acl_add_mac( | |
3218 | &hapd->conf->deny_mac, | |
3219 | &hapd->conf->num_deny_mac, buf + 17)) | |
3220 | hostapd_disassoc_deny_mac(hapd); | |
3221 | } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) { | |
3222 | hostapd_ctrl_iface_acl_del_mac( | |
3223 | &hapd->conf->deny_mac, | |
3224 | &hapd->conf->num_deny_mac, buf + 17); | |
3225 | } else if (os_strcmp(buf + 9, "SHOW") == 0) { | |
3226 | reply_len = hostapd_ctrl_iface_acl_show_mac( | |
3227 | hapd->conf->deny_mac, | |
3228 | hapd->conf->num_deny_mac, reply, reply_size); | |
3229 | } else if (os_strcmp(buf + 9, "CLEAR") == 0) { | |
3230 | hostapd_ctrl_iface_acl_clear_list( | |
3231 | &hapd->conf->deny_mac, | |
3232 | &hapd->conf->num_deny_mac); | |
3233 | } | |
9c2b8204 JM |
3234 | #ifdef CONFIG_DPP |
3235 | } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) { | |
3236 | res = hostapd_dpp_qr_code(hapd, buf + 12); | |
3237 | if (res < 0) { | |
3238 | reply_len = -1; | |
3239 | } else { | |
3240 | reply_len = os_snprintf(reply, reply_size, "%d", res); | |
3241 | if (os_snprintf_error(reply_size, reply_len)) | |
3242 | reply_len = -1; | |
3243 | } | |
3244 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) { | |
3245 | res = hostapd_dpp_bootstrap_gen(hapd, buf + 18); | |
3246 | if (res < 0) { | |
3247 | reply_len = -1; | |
3248 | } else { | |
3249 | reply_len = os_snprintf(reply, reply_size, "%d", res); | |
3250 | if (os_snprintf_error(reply_size, reply_len)) | |
3251 | reply_len = -1; | |
3252 | } | |
3253 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) { | |
3254 | if (hostapd_dpp_bootstrap_remove(hapd, buf + 21) < 0) | |
3255 | reply_len = -1; | |
3256 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) { | |
3257 | const char *uri; | |
3258 | ||
3259 | uri = hostapd_dpp_bootstrap_get_uri(hapd, atoi(buf + 22)); | |
3260 | if (!uri) { | |
3261 | reply_len = -1; | |
3262 | } else { | |
3263 | reply_len = os_snprintf(reply, reply_size, "%s", uri); | |
3264 | if (os_snprintf_error(reply_size, reply_len)) | |
3265 | reply_len = -1; | |
3266 | } | |
a86fb43c JM |
3267 | } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) { |
3268 | reply_len = hostapd_dpp_bootstrap_info(hapd, atoi(buf + 19), | |
3269 | reply, reply_size); | |
9c2b8204 JM |
3270 | } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) { |
3271 | if (hostapd_dpp_auth_init(hapd, buf + 13) < 0) | |
3272 | reply_len = -1; | |
1fafdf11 JM |
3273 | } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) { |
3274 | if (hostapd_dpp_listen(hapd, buf + 11) < 0) | |
3275 | reply_len = -1; | |
3276 | } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) { | |
c1d37739 | 3277 | hostapd_dpp_stop(hapd); |
1fafdf11 | 3278 | hostapd_dpp_listen_stop(hapd); |
2605405a JM |
3279 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) { |
3280 | res = hostapd_dpp_configurator_add(hapd, buf + 20); | |
3281 | if (res < 0) { | |
3282 | reply_len = -1; | |
3283 | } else { | |
3284 | reply_len = os_snprintf(reply, reply_size, "%d", res); | |
3285 | if (os_snprintf_error(reply_size, reply_len)) | |
3286 | reply_len = -1; | |
3287 | } | |
3288 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) { | |
3289 | if (hostapd_dpp_configurator_remove(hapd, buf + 24) < 0) | |
3290 | reply_len = -1; | |
a2588be8 | 3291 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) { |
53d5de6f | 3292 | if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0) |
a2588be8 | 3293 | reply_len = -1; |
8179ae3a PK |
3294 | } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) { |
3295 | reply_len = hostapd_dpp_configurator_get_key(hapd, | |
3296 | atoi(buf + 25), | |
3297 | reply, reply_size); | |
efeada91 JM |
3298 | } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) { |
3299 | res = hostapd_dpp_pkex_add(hapd, buf + 12); | |
3300 | if (res < 0) { | |
3301 | reply_len = -1; | |
3302 | } else { | |
3303 | reply_len = os_snprintf(reply, reply_size, "%d", res); | |
3304 | if (os_snprintf_error(reply_size, reply_len)) | |
3305 | reply_len = -1; | |
3306 | } | |
3307 | } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) { | |
3308 | if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0) | |
3309 | reply_len = -1; | |
9c2b8204 | 3310 | #endif /* CONFIG_DPP */ |
abed6136 JM |
3311 | #ifdef RADIUS_SERVER |
3312 | } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) { | |
3313 | if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0) | |
3314 | reply_len = -1; | |
3315 | #endif /* RADIUS_SERVER */ | |
6fc6879b JM |
3316 | } else { |
3317 | os_memcpy(reply, "UNKNOWN COMMAND\n", 16); | |
3318 | reply_len = 16; | |
3319 | } | |
3320 | ||
3321 | if (reply_len < 0) { | |
3322 | os_memcpy(reply, "FAIL\n", 5); | |
3323 | reply_len = 5; | |
3324 | } | |
585478e2 AN |
3325 | |
3326 | return reply_len; | |
3327 | } | |
3328 | ||
3329 | ||
3330 | static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx, | |
3331 | void *sock_ctx) | |
3332 | { | |
3333 | struct hostapd_data *hapd = eloop_ctx; | |
3334 | char buf[4096]; | |
3335 | int res; | |
acf57fae | 3336 | struct sockaddr_storage from; |
585478e2 | 3337 | socklen_t fromlen = sizeof(from); |
56885eec | 3338 | char *reply, *pos = buf; |
585478e2 AN |
3339 | const int reply_size = 4096; |
3340 | int reply_len; | |
3341 | int level = MSG_DEBUG; | |
56885eec JD |
3342 | #ifdef CONFIG_CTRL_IFACE_UDP |
3343 | unsigned char lcookie[COOKIE_LEN]; | |
3344 | #endif /* CONFIG_CTRL_IFACE_UDP */ | |
585478e2 AN |
3345 | |
3346 | res = recvfrom(sock, buf, sizeof(buf) - 1, 0, | |
3347 | (struct sockaddr *) &from, &fromlen); | |
3348 | if (res < 0) { | |
3349 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", | |
3350 | strerror(errno)); | |
3351 | return; | |
3352 | } | |
3353 | buf[res] = '\0'; | |
585478e2 AN |
3354 | |
3355 | reply = os_malloc(reply_size); | |
3356 | if (reply == NULL) { | |
3357 | if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, | |
3358 | fromlen) < 0) { | |
3359 | wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", | |
3360 | strerror(errno)); | |
3361 | } | |
3362 | return; | |
3363 | } | |
3364 | ||
56885eec JD |
3365 | #ifdef CONFIG_CTRL_IFACE_UDP |
3366 | if (os_strcmp(buf, "GET_COOKIE") == 0) { | |
3367 | os_memcpy(reply, "COOKIE=", 7); | |
3368 | wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1, | |
3369 | cookie, COOKIE_LEN); | |
3370 | reply_len = 7 + 2 * COOKIE_LEN; | |
3371 | goto done; | |
3372 | } | |
3373 | ||
3374 | if (os_strncmp(buf, "COOKIE=", 7) != 0 || | |
3375 | hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) { | |
3376 | wpa_printf(MSG_DEBUG, | |
3377 | "CTRL: No cookie in the request - drop request"); | |
3378 | os_free(reply); | |
3379 | return; | |
3380 | } | |
3381 | ||
3382 | if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) { | |
3383 | wpa_printf(MSG_DEBUG, | |
3384 | "CTRL: Invalid cookie in the request - drop request"); | |
3385 | os_free(reply); | |
3386 | return; | |
3387 | } | |
3388 | ||
3389 | pos = buf + 7 + 2 * COOKIE_LEN; | |
3390 | while (*pos == ' ') | |
3391 | pos++; | |
3392 | #endif /* CONFIG_CTRL_IFACE_UDP */ | |
3393 | ||
3394 | if (os_strcmp(pos, "PING") == 0) | |
3395 | level = MSG_EXCESSIVE; | |
3396 | wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res); | |
3397 | ||
3398 | reply_len = hostapd_ctrl_iface_receive_process(hapd, pos, | |
585478e2 AN |
3399 | reply, reply_size, |
3400 | &from, fromlen); | |
3401 | ||
56885eec JD |
3402 | #ifdef CONFIG_CTRL_IFACE_UDP |
3403 | done: | |
3404 | #endif /* CONFIG_CTRL_IFACE_UDP */ | |
8e3e0b3f JM |
3405 | if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, |
3406 | fromlen) < 0) { | |
3407 | wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", | |
3408 | strerror(errno)); | |
3409 | } | |
6fc6879b JM |
3410 | os_free(reply); |
3411 | } | |
3412 | ||
3413 | ||
56885eec | 3414 | #ifndef CONFIG_CTRL_IFACE_UDP |
6fc6879b JM |
3415 | static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd) |
3416 | { | |
3417 | char *buf; | |
3418 | size_t len; | |
3419 | ||
3420 | if (hapd->conf->ctrl_interface == NULL) | |
3421 | return NULL; | |
3422 | ||
3423 | len = os_strlen(hapd->conf->ctrl_interface) + | |
3424 | os_strlen(hapd->conf->iface) + 2; | |
3425 | buf = os_malloc(len); | |
3426 | if (buf == NULL) | |
3427 | return NULL; | |
3428 | ||
3429 | os_snprintf(buf, len, "%s/%s", | |
3430 | hapd->conf->ctrl_interface, hapd->conf->iface); | |
3431 | buf[len - 1] = '\0'; | |
3432 | return buf; | |
3433 | } | |
56885eec | 3434 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
6fc6879b JM |
3435 | |
3436 | ||
995a3a06 JM |
3437 | static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, |
3438 | enum wpa_msg_type type, | |
42d16805 JM |
3439 | const char *txt, size_t len) |
3440 | { | |
3441 | struct hostapd_data *hapd = ctx; | |
3442 | if (hapd == NULL) | |
3443 | return; | |
ee1e3f57 | 3444 | hostapd_ctrl_iface_send(hapd, level, type, txt, len); |
42d16805 JM |
3445 | } |
3446 | ||
3447 | ||
6fc6879b JM |
3448 | int hostapd_ctrl_iface_init(struct hostapd_data *hapd) |
3449 | { | |
56885eec JD |
3450 | #ifdef CONFIG_CTRL_IFACE_UDP |
3451 | int port = HOSTAPD_CTRL_IFACE_PORT; | |
3452 | char p[32] = { 0 }; | |
180e5b96 | 3453 | char port_str[40], *tmp; |
b9066c63 | 3454 | char *pos; |
56885eec JD |
3455 | struct addrinfo hints = { 0 }, *res, *saveres; |
3456 | int n; | |
3457 | ||
3458 | if (hapd->ctrl_sock > -1) { | |
3459 | wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); | |
3460 | return 0; | |
3461 | } | |
3462 | ||
3463 | if (hapd->conf->ctrl_interface == NULL) | |
3464 | return 0; | |
3465 | ||
b9066c63 JD |
3466 | pos = os_strstr(hapd->conf->ctrl_interface, "udp:"); |
3467 | if (pos) { | |
3468 | pos += 4; | |
3469 | port = atoi(pos); | |
3470 | if (port <= 0) { | |
3471 | wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port"); | |
3472 | goto fail; | |
3473 | } | |
3474 | } | |
3475 | ||
56885eec JD |
3476 | dl_list_init(&hapd->ctrl_dst); |
3477 | hapd->ctrl_sock = -1; | |
3478 | os_get_random(cookie, COOKIE_LEN); | |
3479 | ||
3480 | #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE | |
3481 | hints.ai_flags = AI_PASSIVE; | |
3482 | #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ | |
3483 | ||
3484 | #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 | |
3485 | hints.ai_family = AF_INET6; | |
3486 | #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ | |
3487 | hints.ai_family = AF_INET; | |
3488 | #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ | |
3489 | hints.ai_socktype = SOCK_DGRAM; | |
3490 | ||
3491 | try_again: | |
3492 | os_snprintf(p, sizeof(p), "%d", port); | |
3493 | n = getaddrinfo(NULL, p, &hints, &res); | |
3494 | if (n) { | |
3495 | wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n)); | |
3496 | goto fail; | |
3497 | } | |
3498 | ||
3499 | saveres = res; | |
3500 | hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype, | |
3501 | res->ai_protocol); | |
3502 | if (hapd->ctrl_sock < 0) { | |
3503 | wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno)); | |
3504 | goto fail; | |
3505 | } | |
3506 | ||
3507 | if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) { | |
3508 | port--; | |
3509 | if ((HOSTAPD_CTRL_IFACE_PORT - port) < | |
b9066c63 | 3510 | HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos) |
56885eec JD |
3511 | goto try_again; |
3512 | wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno)); | |
3513 | goto fail; | |
3514 | } | |
3515 | ||
3516 | freeaddrinfo(saveres); | |
3517 | ||
180e5b96 JD |
3518 | os_snprintf(port_str, sizeof(port_str), "udp:%d", port); |
3519 | tmp = os_strdup(port_str); | |
3520 | if (tmp) { | |
3521 | os_free(hapd->conf->ctrl_interface); | |
3522 | hapd->conf->ctrl_interface = tmp; | |
3523 | } | |
56885eec JD |
3524 | wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port); |
3525 | ||
3526 | if (eloop_register_read_sock(hapd->ctrl_sock, | |
3527 | hostapd_ctrl_iface_receive, hapd, NULL) < | |
3528 | 0) { | |
3529 | hostapd_ctrl_iface_deinit(hapd); | |
3530 | return -1; | |
3531 | } | |
3532 | ||
3533 | hapd->msg_ctx = hapd; | |
3534 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); | |
3535 | ||
3536 | return 0; | |
3537 | ||
3538 | fail: | |
3539 | if (hapd->ctrl_sock >= 0) | |
3540 | close(hapd->ctrl_sock); | |
3541 | return -1; | |
3542 | #else /* CONFIG_CTRL_IFACE_UDP */ | |
6fc6879b JM |
3543 | struct sockaddr_un addr; |
3544 | int s = -1; | |
3545 | char *fname = NULL; | |
3546 | ||
9e7d033e SP |
3547 | if (hapd->ctrl_sock > -1) { |
3548 | wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); | |
3549 | return 0; | |
3550 | } | |
6fc6879b | 3551 | |
89b781bc JD |
3552 | dl_list_init(&hapd->ctrl_dst); |
3553 | ||
6fc6879b JM |
3554 | if (hapd->conf->ctrl_interface == NULL) |
3555 | return 0; | |
3556 | ||
3557 | if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { | |
3558 | if (errno == EEXIST) { | |
3559 | wpa_printf(MSG_DEBUG, "Using existing control " | |
3560 | "interface directory."); | |
3561 | } else { | |
a193231d JM |
3562 | wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s", |
3563 | strerror(errno)); | |
6fc6879b JM |
3564 | goto fail; |
3565 | } | |
3566 | } | |
3567 | ||
3568 | if (hapd->conf->ctrl_interface_gid_set && | |
02f52ab6 JM |
3569 | lchown(hapd->conf->ctrl_interface, -1, |
3570 | hapd->conf->ctrl_interface_gid) < 0) { | |
3571 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", | |
a193231d | 3572 | strerror(errno)); |
6fc6879b JM |
3573 | return -1; |
3574 | } | |
3575 | ||
187f87f0 JM |
3576 | if (!hapd->conf->ctrl_interface_gid_set && |
3577 | hapd->iface->interfaces->ctrl_iface_group && | |
02f52ab6 JM |
3578 | lchown(hapd->conf->ctrl_interface, -1, |
3579 | hapd->iface->interfaces->ctrl_iface_group) < 0) { | |
3580 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", | |
a193231d | 3581 | strerror(errno)); |
187f87f0 JM |
3582 | return -1; |
3583 | } | |
3584 | ||
5bbf590a PF |
3585 | #ifdef ANDROID |
3586 | /* | |
3587 | * Android is using umask 0077 which would leave the control interface | |
3588 | * directory without group access. This breaks things since Wi-Fi | |
3589 | * framework assumes that this directory can be accessed by other | |
3590 | * applications in the wifi group. Fix this by adding group access even | |
3591 | * if umask value would prevent this. | |
3592 | */ | |
3593 | if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) { | |
3594 | wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s", | |
3595 | strerror(errno)); | |
3596 | /* Try to continue anyway */ | |
3597 | } | |
3598 | #endif /* ANDROID */ | |
3599 | ||
6fc6879b JM |
3600 | if (os_strlen(hapd->conf->ctrl_interface) + 1 + |
3601 | os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path)) | |
3602 | goto fail; | |
3603 | ||
3604 | s = socket(PF_UNIX, SOCK_DGRAM, 0); | |
3605 | if (s < 0) { | |
a193231d | 3606 | wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
6fc6879b JM |
3607 | goto fail; |
3608 | } | |
3609 | ||
3610 | os_memset(&addr, 0, sizeof(addr)); | |
75864b7f JM |
3611 | #ifdef __FreeBSD__ |
3612 | addr.sun_len = sizeof(addr); | |
3613 | #endif /* __FreeBSD__ */ | |
6fc6879b JM |
3614 | addr.sun_family = AF_UNIX; |
3615 | fname = hostapd_ctrl_iface_path(hapd); | |
3616 | if (fname == NULL) | |
3617 | goto fail; | |
3618 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); | |
3619 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
617d1555 JM |
3620 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", |
3621 | strerror(errno)); | |
3622 | if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
3623 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
3624 | " allow connections - assuming it was left" | |
3625 | "over from forced program termination"); | |
3626 | if (unlink(fname) < 0) { | |
a193231d JM |
3627 | wpa_printf(MSG_ERROR, |
3628 | "Could not unlink existing ctrl_iface socket '%s': %s", | |
3629 | fname, strerror(errno)); | |
617d1555 JM |
3630 | goto fail; |
3631 | } | |
3632 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < | |
3633 | 0) { | |
a193231d JM |
3634 | wpa_printf(MSG_ERROR, |
3635 | "hostapd-ctrl-iface: bind(PF_UNIX): %s", | |
3636 | strerror(errno)); | |
617d1555 JM |
3637 | goto fail; |
3638 | } | |
3639 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
3640 | "ctrl_iface socket '%s'", fname); | |
3641 | } else { | |
3642 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
3643 | "be in use - cannot override it"); | |
3644 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
3645 | "not used anymore", fname); | |
3646 | os_free(fname); | |
3647 | fname = NULL; | |
3648 | goto fail; | |
3649 | } | |
6fc6879b JM |
3650 | } |
3651 | ||
3652 | if (hapd->conf->ctrl_interface_gid_set && | |
02f52ab6 JM |
3653 | lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) { |
3654 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s", | |
a193231d | 3655 | strerror(errno)); |
6fc6879b JM |
3656 | goto fail; |
3657 | } | |
3658 | ||
187f87f0 JM |
3659 | if (!hapd->conf->ctrl_interface_gid_set && |
3660 | hapd->iface->interfaces->ctrl_iface_group && | |
02f52ab6 JM |
3661 | lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) { |
3662 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s", | |
a193231d | 3663 | strerror(errno)); |
187f87f0 JM |
3664 | goto fail; |
3665 | } | |
3666 | ||
6fc6879b | 3667 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { |
a193231d JM |
3668 | wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s", |
3669 | strerror(errno)); | |
6fc6879b JM |
3670 | goto fail; |
3671 | } | |
3672 | os_free(fname); | |
3673 | ||
3674 | hapd->ctrl_sock = s; | |
86a7fbb2 JM |
3675 | if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd, |
3676 | NULL) < 0) { | |
3677 | hostapd_ctrl_iface_deinit(hapd); | |
3678 | return -1; | |
3679 | } | |
4f760fcc | 3680 | hapd->msg_ctx = hapd; |
42d16805 | 3681 | wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb); |
6fc6879b JM |
3682 | |
3683 | return 0; | |
3684 | ||
3685 | fail: | |
3686 | if (s >= 0) | |
3687 | close(s); | |
3688 | if (fname) { | |
3689 | unlink(fname); | |
3690 | os_free(fname); | |
3691 | } | |
3692 | return -1; | |
56885eec | 3693 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
6fc6879b JM |
3694 | } |
3695 | ||
3696 | ||
3697 | void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd) | |
3698 | { | |
3699 | struct wpa_ctrl_dst *dst, *prev; | |
3700 | ||
3701 | if (hapd->ctrl_sock > -1) { | |
56885eec | 3702 | #ifndef CONFIG_CTRL_IFACE_UDP |
6fc6879b | 3703 | char *fname; |
56885eec JD |
3704 | #endif /* !CONFIG_CTRL_IFACE_UDP */ |
3705 | ||
6fc6879b JM |
3706 | eloop_unregister_read_sock(hapd->ctrl_sock); |
3707 | close(hapd->ctrl_sock); | |
3708 | hapd->ctrl_sock = -1; | |
56885eec | 3709 | #ifndef CONFIG_CTRL_IFACE_UDP |
6fc6879b JM |
3710 | fname = hostapd_ctrl_iface_path(hapd); |
3711 | if (fname) | |
3712 | unlink(fname); | |
3713 | os_free(fname); | |
3714 | ||
3715 | if (hapd->conf->ctrl_interface && | |
3716 | rmdir(hapd->conf->ctrl_interface) < 0) { | |
3717 | if (errno == ENOTEMPTY) { | |
3718 | wpa_printf(MSG_DEBUG, "Control interface " | |
3719 | "directory not empty - leaving it " | |
3720 | "behind"); | |
3721 | } else { | |
2c6f8cf6 JM |
3722 | wpa_printf(MSG_ERROR, |
3723 | "rmdir[ctrl_interface=%s]: %s", | |
3724 | hapd->conf->ctrl_interface, | |
3725 | strerror(errno)); | |
6fc6879b JM |
3726 | } |
3727 | } | |
56885eec | 3728 | #endif /* !CONFIG_CTRL_IFACE_UDP */ |
6fc6879b JM |
3729 | } |
3730 | ||
89b781bc JD |
3731 | dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst, |
3732 | list) | |
3733 | os_free(dst); | |
4a6cc862 JM |
3734 | |
3735 | #ifdef CONFIG_TESTING_OPTIONS | |
3736 | l2_packet_deinit(hapd->l2_test); | |
3737 | hapd->l2_test = NULL; | |
3738 | #endif /* CONFIG_TESTING_OPTIONS */ | |
6fc6879b JM |
3739 | } |
3740 | ||
3741 | ||
06bb8c62 SP |
3742 | static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces, |
3743 | char *buf) | |
3744 | { | |
3745 | if (hostapd_add_iface(interfaces, buf) < 0) { | |
3746 | wpa_printf(MSG_ERROR, "Adding interface %s failed", buf); | |
3747 | return -1; | |
3748 | } | |
3749 | return 0; | |
3750 | } | |
3751 | ||
3752 | ||
3753 | static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces, | |
3754 | char *buf) | |
3755 | { | |
3756 | if (hostapd_remove_iface(interfaces, buf) < 0) { | |
3757 | wpa_printf(MSG_ERROR, "Removing interface %s failed", buf); | |
3758 | return -1; | |
3759 | } | |
3760 | return 0; | |
3761 | } | |
3762 | ||
3763 | ||
ee1e3f57 | 3764 | static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces, |
acf57fae | 3765 | struct sockaddr_storage *from, |
cc79e06f | 3766 | socklen_t fromlen, char *input) |
ee1e3f57 | 3767 | { |
cc79e06f | 3768 | return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen, |
3769 | input); | |
ee1e3f57 AN |
3770 | } |
3771 | ||
3772 | ||
3773 | static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces, | |
acf57fae | 3774 | struct sockaddr_storage *from, |
ee1e3f57 AN |
3775 | socklen_t fromlen) |
3776 | { | |
89b781bc | 3777 | return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen); |
ee1e3f57 AN |
3778 | } |
3779 | ||
3780 | ||
91226e0d JM |
3781 | static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces) |
3782 | { | |
3783 | #ifdef CONFIG_WPS_TESTING | |
3784 | wps_version_number = 0x20; | |
3785 | wps_testing_dummy_cred = 0; | |
3786 | wps_corrupt_pkhash = 0; | |
3787 | #endif /* CONFIG_WPS_TESTING */ | |
60239f60 JM |
3788 | |
3789 | #ifdef CONFIG_TESTING_OPTIONS | |
3790 | #ifdef CONFIG_DPP | |
3791 | dpp_test = DPP_TEST_DISABLED; | |
3792 | #endif /* CONFIG_DPP */ | |
3793 | #endif /* CONFIG_TESTING_OPTIONS */ | |
3794 | ||
7eb6bfb4 JM |
3795 | #ifdef CONFIG_DPP |
3796 | hostapd_dpp_deinit_global(interfaces); | |
3797 | #endif /* CONFIG_DPP */ | |
91226e0d JM |
3798 | } |
3799 | ||
3800 | ||
8e9a8b0f AN |
3801 | #ifdef CONFIG_FST |
3802 | ||
3803 | static int | |
3804 | hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces, | |
3805 | const char *cmd) | |
3806 | { | |
3807 | char ifname[IFNAMSIZ + 1]; | |
3808 | struct fst_iface_cfg cfg; | |
3809 | struct hostapd_data *hapd; | |
3810 | struct fst_wpa_obj iface_obj; | |
3811 | ||
3812 | if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) { | |
3813 | hapd = hostapd_get_iface(interfaces, ifname); | |
3814 | if (hapd) { | |
ff7a4bd1 JM |
3815 | if (hapd->iface->fst) { |
3816 | wpa_printf(MSG_INFO, "FST: Already attached"); | |
3817 | return -1; | |
3818 | } | |
8e9a8b0f AN |
3819 | fst_hostapd_fill_iface_obj(hapd, &iface_obj); |
3820 | hapd->iface->fst = fst_attach(ifname, hapd->own_addr, | |
3821 | &iface_obj, &cfg); | |
3822 | if (hapd->iface->fst) | |
3823 | return 0; | |
3824 | } | |
3825 | } | |
3826 | ||
ceb8a79c | 3827 | return -EINVAL; |
8e9a8b0f AN |
3828 | } |
3829 | ||
3830 | ||
3831 | static int | |
3832 | hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces, | |
3833 | const char *cmd) | |
3834 | { | |
3835 | char ifname[IFNAMSIZ + 1]; | |
3836 | struct hostapd_data * hapd; | |
3837 | ||
3838 | if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) { | |
3839 | hapd = hostapd_get_iface(interfaces, ifname); | |
3840 | if (hapd) { | |
3841 | if (!fst_iface_detach(ifname)) { | |
3842 | hapd->iface->fst = NULL; | |
780753fa | 3843 | hapd->iface->fst_ies = NULL; |
8e9a8b0f AN |
3844 | return 0; |
3845 | } | |
3846 | } | |
3847 | } | |
3848 | ||
ceb8a79c | 3849 | return -EINVAL; |
8e9a8b0f AN |
3850 | } |
3851 | ||
3852 | #endif /* CONFIG_FST */ | |
3853 | ||
b3281452 AN |
3854 | |
3855 | static struct hostapd_data * | |
3856 | hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces, | |
3857 | const char *ifname) | |
6990d41a AN |
3858 | { |
3859 | size_t i, j; | |
6990d41a | 3860 | |
b3281452 | 3861 | for (i = 0; i < interfaces->count; i++) { |
6990d41a AN |
3862 | struct hostapd_iface *iface = interfaces->iface[i]; |
3863 | ||
3864 | for (j = 0; j < iface->num_bss; j++) { | |
b3281452 AN |
3865 | struct hostapd_data *hapd; |
3866 | ||
6990d41a AN |
3867 | hapd = iface->bss[j]; |
3868 | if (os_strcmp(ifname, hapd->conf->iface) == 0) | |
b3281452 | 3869 | return hapd; |
6990d41a AN |
3870 | } |
3871 | } | |
3872 | ||
b3281452 AN |
3873 | return NULL; |
3874 | } | |
3875 | ||
3876 | ||
748febf3 AN |
3877 | static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd, |
3878 | struct hostapd_data *dst_hapd, | |
63e169e1 | 3879 | const char *param) |
748febf3 AN |
3880 | { |
3881 | int res; | |
3882 | char *value; | |
3883 | ||
3884 | value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN); | |
3885 | if (!value) { | |
3886 | wpa_printf(MSG_ERROR, | |
3887 | "DUP: cannot allocate buffer to stringify %s", | |
3888 | param); | |
3889 | goto error_return; | |
3890 | } | |
3891 | ||
3892 | if (os_strcmp(param, "wpa") == 0) { | |
3893 | os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d", | |
3894 | src_hapd->conf->wpa); | |
3895 | } else if (os_strcmp(param, "wpa_key_mgmt") == 0 && | |
3896 | src_hapd->conf->wpa_key_mgmt) { | |
3897 | res = hostapd_ctrl_iface_get_key_mgmt( | |
3898 | src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN); | |
3899 | if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res)) | |
3900 | goto error_stringify; | |
3901 | } else if (os_strcmp(param, "wpa_pairwise") == 0 && | |
3902 | src_hapd->conf->wpa_pairwise) { | |
3903 | res = wpa_write_ciphers(value, | |
3904 | value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN, | |
3905 | src_hapd->conf->wpa_pairwise, " "); | |
3906 | if (res < 0) | |
3907 | goto error_stringify; | |
3908 | } else if (os_strcmp(param, "rsn_pairwise") == 0 && | |
3909 | src_hapd->conf->rsn_pairwise) { | |
3910 | res = wpa_write_ciphers(value, | |
3911 | value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN, | |
3912 | src_hapd->conf->rsn_pairwise, " "); | |
3913 | if (res < 0) | |
3914 | goto error_stringify; | |
3915 | } else if (os_strcmp(param, "wpa_passphrase") == 0 && | |
3916 | src_hapd->conf->ssid.wpa_passphrase) { | |
3917 | os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s", | |
3918 | src_hapd->conf->ssid.wpa_passphrase); | |
3919 | } else if (os_strcmp(param, "wpa_psk") == 0 && | |
3920 | src_hapd->conf->ssid.wpa_psk_set) { | |
3921 | wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, | |
3922 | src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN); | |
3923 | } else { | |
3924 | wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param); | |
3925 | goto error_return; | |
3926 | } | |
3927 | ||
3928 | res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value); | |
3929 | os_free(value); | |
3930 | return res; | |
3931 | ||
3932 | error_stringify: | |
3933 | wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param); | |
3934 | error_return: | |
3935 | os_free(value); | |
3936 | return -1; | |
3937 | } | |
3938 | ||
3939 | ||
618f5d01 JD |
3940 | static int |
3941 | hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces, | |
3942 | const char *input, | |
3943 | char *reply, int reply_size) | |
3944 | { | |
3945 | size_t i, j; | |
3946 | int res; | |
3947 | char *pos, *end; | |
3948 | struct hostapd_iface *iface; | |
3949 | int show_ctrl = 0; | |
3950 | ||
3951 | if (input) | |
3952 | show_ctrl = !!os_strstr(input, "ctrl"); | |
3953 | ||
3954 | pos = reply; | |
3955 | end = reply + reply_size; | |
3956 | ||
3957 | for (i = 0; i < interfaces->count; i++) { | |
3958 | iface = interfaces->iface[i]; | |
3959 | ||
3960 | for (j = 0; j < iface->num_bss; j++) { | |
3961 | struct hostapd_bss_config *conf; | |
3962 | ||
3963 | conf = iface->conf->bss[j]; | |
3964 | if (show_ctrl) | |
3965 | res = os_snprintf(pos, end - pos, | |
3966 | "%s ctrl_iface=%s\n", | |
3967 | conf->iface, | |
3968 | conf->ctrl_interface ? | |
3969 | conf->ctrl_interface : "N/A"); | |
3970 | else | |
3971 | res = os_snprintf(pos, end - pos, "%s\n", | |
3972 | conf->iface); | |
3973 | if (os_snprintf_error(end - pos, res)) { | |
3974 | *pos = '\0'; | |
3975 | return pos - reply; | |
3976 | } | |
3977 | pos += res; | |
3978 | } | |
3979 | } | |
3980 | ||
3981 | return pos - reply; | |
3982 | } | |
3983 | ||
3984 | ||
748febf3 AN |
3985 | static int |
3986 | hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces, | |
3987 | char *cmd) | |
3988 | { | |
3989 | char *p_start = cmd, *p_end; | |
3990 | struct hostapd_data *src_hapd, *dst_hapd; | |
3991 | ||
3992 | /* cmd: "<src ifname> <dst ifname> <variable name> */ | |
3993 | ||
3994 | p_end = os_strchr(p_start, ' '); | |
3995 | if (!p_end) { | |
3996 | wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'", | |
3997 | cmd); | |
3998 | return -1; | |
3999 | } | |
4000 | ||
4001 | *p_end = '\0'; | |
4002 | src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start); | |
4003 | if (!src_hapd) { | |
4004 | wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'", | |
4005 | p_start); | |
4006 | return -1; | |
4007 | } | |
4008 | ||
4009 | p_start = p_end + 1; | |
4010 | p_end = os_strchr(p_start, ' '); | |
4011 | if (!p_end) { | |
4012 | wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'", | |
4013 | cmd); | |
4014 | return -1; | |
4015 | } | |
4016 | ||
4017 | *p_end = '\0'; | |
4018 | dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start); | |
4019 | if (!dst_hapd) { | |
4020 | wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'", | |
4021 | p_start); | |
4022 | return -1; | |
4023 | } | |
4024 | ||
4025 | p_start = p_end + 1; | |
4026 | return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start); | |
4027 | } | |
4028 | ||
4029 | ||
b3281452 AN |
4030 | static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces, |
4031 | const char *ifname, | |
4032 | char *buf, char *reply, | |
4033 | int reply_size, | |
acf57fae | 4034 | struct sockaddr_storage *from, |
b3281452 AN |
4035 | socklen_t fromlen) |
4036 | { | |
4037 | struct hostapd_data *hapd; | |
4038 | ||
4039 | hapd = hostapd_interfaces_get_hapd(interfaces, ifname); | |
6990d41a AN |
4040 | if (hapd == NULL) { |
4041 | int res; | |
4042 | ||
4043 | res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n"); | |
4044 | if (os_snprintf_error(reply_size, res)) | |
4045 | return -1; | |
4046 | return res; | |
4047 | } | |
4048 | ||
4049 | return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size, | |
4050 | from, fromlen); | |
4051 | } | |
4052 | ||
8e9a8b0f | 4053 | |
c90fd485 SP |
4054 | static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx, |
4055 | void *sock_ctx) | |
4056 | { | |
06bb8c62 | 4057 | void *interfaces = eloop_ctx; |
56885eec | 4058 | char buffer[256], *buf = buffer; |
c90fd485 | 4059 | int res; |
acf57fae | 4060 | struct sockaddr_storage from; |
c90fd485 | 4061 | socklen_t fromlen = sizeof(from); |
ee1e3f57 | 4062 | char *reply; |
c90fd485 | 4063 | int reply_len; |
ee1e3f57 | 4064 | const int reply_size = 4096; |
56885eec JD |
4065 | #ifdef CONFIG_CTRL_IFACE_UDP |
4066 | unsigned char lcookie[COOKIE_LEN]; | |
4067 | #endif /* CONFIG_CTRL_IFACE_UDP */ | |
c90fd485 | 4068 | |
56885eec | 4069 | res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, |
c90fd485 SP |
4070 | (struct sockaddr *) &from, &fromlen); |
4071 | if (res < 0) { | |
a193231d JM |
4072 | wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", |
4073 | strerror(errno)); | |
c90fd485 SP |
4074 | return; |
4075 | } | |
4076 | buf[res] = '\0'; | |
3f134b43 | 4077 | wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf); |
c90fd485 | 4078 | |
ee1e3f57 AN |
4079 | reply = os_malloc(reply_size); |
4080 | if (reply == NULL) { | |
4081 | if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, | |
4082 | fromlen) < 0) { | |
4083 | wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", | |
4084 | strerror(errno)); | |
4085 | } | |
4086 | return; | |
4087 | } | |
4088 | ||
c90fd485 SP |
4089 | os_memcpy(reply, "OK\n", 3); |
4090 | reply_len = 3; | |
4091 | ||
56885eec JD |
4092 | #ifdef CONFIG_CTRL_IFACE_UDP |
4093 | if (os_strcmp(buf, "GET_COOKIE") == 0) { | |
4094 | os_memcpy(reply, "COOKIE=", 7); | |
4095 | wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1, | |
4096 | gcookie, COOKIE_LEN); | |
4097 | reply_len = 7 + 2 * COOKIE_LEN; | |
4098 | goto send_reply; | |
4099 | } | |
4100 | ||
4101 | if (os_strncmp(buf, "COOKIE=", 7) != 0 || | |
4102 | hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) { | |
4103 | wpa_printf(MSG_DEBUG, | |
4104 | "CTRL: No cookie in the request - drop request"); | |
4105 | os_free(reply); | |
4106 | return; | |
4107 | } | |
4108 | ||
4109 | if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) { | |
4110 | wpa_printf(MSG_DEBUG, | |
4111 | "CTRL: Invalid cookie in the request - drop request"); | |
4112 | os_free(reply); | |
4113 | return; | |
4114 | } | |
4115 | ||
4116 | buf += 7 + 2 * COOKIE_LEN; | |
4117 | while (*buf == ' ') | |
4118 | buf++; | |
4119 | #endif /* CONFIG_CTRL_IFACE_UDP */ | |
4120 | ||
6990d41a AN |
4121 | if (os_strncmp(buf, "IFNAME=", 7) == 0) { |
4122 | char *pos = os_strchr(buf + 7, ' '); | |
4123 | ||
4124 | if (pos) { | |
4125 | *pos++ = '\0'; | |
4126 | reply_len = hostapd_global_ctrl_iface_ifname( | |
4127 | interfaces, buf + 7, pos, reply, reply_size, | |
4128 | &from, fromlen); | |
4129 | goto send_reply; | |
4130 | } | |
4131 | } | |
4132 | ||
c90fd485 SP |
4133 | if (os_strcmp(buf, "PING") == 0) { |
4134 | os_memcpy(reply, "PONG\n", 5); | |
4135 | reply_len = 5; | |
3f134b43 JM |
4136 | } else if (os_strncmp(buf, "RELOG", 5) == 0) { |
4137 | if (wpa_debug_reopen_file() < 0) | |
4138 | reply_len = -1; | |
91226e0d JM |
4139 | } else if (os_strcmp(buf, "FLUSH") == 0) { |
4140 | hostapd_ctrl_iface_flush(interfaces); | |
06bb8c62 SP |
4141 | } else if (os_strncmp(buf, "ADD ", 4) == 0) { |
4142 | if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0) | |
4143 | reply_len = -1; | |
4144 | } else if (os_strncmp(buf, "REMOVE ", 7) == 0) { | |
4145 | if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0) | |
4146 | reply_len = -1; | |
ee1e3f57 AN |
4147 | } else if (os_strcmp(buf, "ATTACH") == 0) { |
4148 | if (hostapd_global_ctrl_iface_attach(interfaces, &from, | |
cc79e06f | 4149 | fromlen, NULL)) |
4150 | reply_len = -1; | |
4151 | } else if (os_strncmp(buf, "ATTACH ", 7) == 0) { | |
4152 | if (hostapd_global_ctrl_iface_attach(interfaces, &from, | |
4153 | fromlen, buf + 7)) | |
ee1e3f57 AN |
4154 | reply_len = -1; |
4155 | } else if (os_strcmp(buf, "DETACH") == 0) { | |
4156 | if (hostapd_global_ctrl_iface_detach(interfaces, &from, | |
4157 | fromlen)) | |
4158 | reply_len = -1; | |
ea449b5b JM |
4159 | #ifdef CONFIG_MODULE_TESTS |
4160 | } else if (os_strcmp(buf, "MODULE_TESTS") == 0) { | |
ea449b5b JM |
4161 | if (hapd_module_tests() < 0) |
4162 | reply_len = -1; | |
4163 | #endif /* CONFIG_MODULE_TESTS */ | |
8e9a8b0f AN |
4164 | #ifdef CONFIG_FST |
4165 | } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) { | |
4166 | if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11)) | |
4167 | reply_len = os_snprintf(reply, reply_size, "OK\n"); | |
4168 | else | |
4169 | reply_len = -1; | |
4170 | } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) { | |
4171 | if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11)) | |
4172 | reply_len = os_snprintf(reply, reply_size, "OK\n"); | |
4173 | else | |
4174 | reply_len = -1; | |
4175 | } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) { | |
4176 | reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size); | |
4177 | #endif /* CONFIG_FST */ | |
748febf3 AN |
4178 | } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) { |
4179 | if (!hostapd_global_ctrl_iface_dup_network(interfaces, | |
4180 | buf + 12)) | |
4181 | reply_len = os_snprintf(reply, reply_size, "OK\n"); | |
4182 | else | |
4183 | reply_len = -1; | |
618f5d01 JD |
4184 | } else if (os_strncmp(buf, "INTERFACES", 10) == 0) { |
4185 | reply_len = hostapd_global_ctrl_iface_interfaces( | |
4186 | interfaces, buf + 10, reply, sizeof(buffer)); | |
3e671718 JD |
4187 | } else if (os_strcmp(buf, "TERMINATE") == 0) { |
4188 | eloop_terminate(); | |
c90fd485 SP |
4189 | } else { |
4190 | wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command " | |
4191 | "ignored"); | |
4192 | reply_len = -1; | |
4193 | } | |
4194 | ||
6990d41a | 4195 | send_reply: |
c90fd485 SP |
4196 | if (reply_len < 0) { |
4197 | os_memcpy(reply, "FAIL\n", 5); | |
4198 | reply_len = 5; | |
4199 | } | |
4200 | ||
8e3e0b3f JM |
4201 | if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, |
4202 | fromlen) < 0) { | |
4203 | wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s", | |
4204 | strerror(errno)); | |
4205 | } | |
ee1e3f57 | 4206 | os_free(reply); |
c90fd485 SP |
4207 | } |
4208 | ||
4209 | ||
56885eec | 4210 | #ifndef CONFIG_CTRL_IFACE_UDP |
c90fd485 SP |
4211 | static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface) |
4212 | { | |
4213 | char *buf; | |
4214 | size_t len; | |
4215 | ||
4216 | if (interface->global_iface_path == NULL) | |
4217 | return NULL; | |
4218 | ||
4219 | len = os_strlen(interface->global_iface_path) + | |
4220 | os_strlen(interface->global_iface_name) + 2; | |
4221 | buf = os_malloc(len); | |
4222 | if (buf == NULL) | |
4223 | return NULL; | |
4224 | ||
4225 | os_snprintf(buf, len, "%s/%s", interface->global_iface_path, | |
4226 | interface->global_iface_name); | |
4227 | buf[len - 1] = '\0'; | |
4228 | return buf; | |
4229 | } | |
56885eec | 4230 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
c90fd485 SP |
4231 | |
4232 | ||
4233 | int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface) | |
4234 | { | |
56885eec JD |
4235 | #ifdef CONFIG_CTRL_IFACE_UDP |
4236 | int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT; | |
4237 | char p[32] = { 0 }; | |
b9066c63 | 4238 | char *pos; |
56885eec JD |
4239 | struct addrinfo hints = { 0 }, *res, *saveres; |
4240 | int n; | |
4241 | ||
4242 | if (interface->global_ctrl_sock > -1) { | |
4243 | wpa_printf(MSG_DEBUG, "ctrl_iface already exists!"); | |
4244 | return 0; | |
4245 | } | |
4246 | ||
4247 | if (interface->global_iface_path == NULL) | |
4248 | return 0; | |
4249 | ||
b9066c63 JD |
4250 | pos = os_strstr(interface->global_iface_path, "udp:"); |
4251 | if (pos) { | |
4252 | pos += 4; | |
4253 | port = atoi(pos); | |
4254 | if (port <= 0) { | |
4255 | wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port"); | |
4256 | goto fail; | |
4257 | } | |
4258 | } | |
4259 | ||
56885eec JD |
4260 | os_get_random(gcookie, COOKIE_LEN); |
4261 | ||
4262 | #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE | |
4263 | hints.ai_flags = AI_PASSIVE; | |
4264 | #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ | |
4265 | ||
4266 | #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 | |
4267 | hints.ai_family = AF_INET6; | |
4268 | #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ | |
4269 | hints.ai_family = AF_INET; | |
4270 | #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ | |
4271 | hints.ai_socktype = SOCK_DGRAM; | |
4272 | ||
4273 | try_again: | |
4274 | os_snprintf(p, sizeof(p), "%d", port); | |
4275 | n = getaddrinfo(NULL, p, &hints, &res); | |
4276 | if (n) { | |
4277 | wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n)); | |
4278 | goto fail; | |
4279 | } | |
4280 | ||
4281 | saveres = res; | |
4282 | interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype, | |
4283 | res->ai_protocol); | |
4284 | if (interface->global_ctrl_sock < 0) { | |
4285 | wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno)); | |
4286 | goto fail; | |
4287 | } | |
4288 | ||
4289 | if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) < | |
4290 | 0) { | |
4291 | port++; | |
4292 | if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) < | |
b9066c63 | 4293 | HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos) |
56885eec JD |
4294 | goto try_again; |
4295 | wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno)); | |
4296 | goto fail; | |
4297 | } | |
4298 | ||
4299 | freeaddrinfo(saveres); | |
4300 | ||
4301 | wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port); | |
4302 | ||
4303 | if (eloop_register_read_sock(interface->global_ctrl_sock, | |
4304 | hostapd_global_ctrl_iface_receive, | |
4305 | interface, NULL) < 0) { | |
4306 | hostapd_global_ctrl_iface_deinit(interface); | |
4307 | return -1; | |
4308 | } | |
4309 | ||
4310 | return 0; | |
4311 | ||
4312 | fail: | |
4313 | if (interface->global_ctrl_sock >= 0) | |
4314 | close(interface->global_ctrl_sock); | |
4315 | return -1; | |
4316 | #else /* CONFIG_CTRL_IFACE_UDP */ | |
c90fd485 SP |
4317 | struct sockaddr_un addr; |
4318 | int s = -1; | |
4319 | char *fname = NULL; | |
4320 | ||
4321 | if (interface->global_iface_path == NULL) { | |
4322 | wpa_printf(MSG_DEBUG, "ctrl_iface not configured!"); | |
4323 | return 0; | |
4324 | } | |
4325 | ||
4326 | if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) { | |
4327 | if (errno == EEXIST) { | |
4328 | wpa_printf(MSG_DEBUG, "Using existing control " | |
4329 | "interface directory."); | |
4330 | } else { | |
a193231d JM |
4331 | wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s", |
4332 | strerror(errno)); | |
c90fd485 SP |
4333 | goto fail; |
4334 | } | |
187f87f0 | 4335 | } else if (interface->ctrl_iface_group && |
02f52ab6 JM |
4336 | lchown(interface->global_iface_path, -1, |
4337 | interface->ctrl_iface_group) < 0) { | |
4338 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", | |
a193231d | 4339 | strerror(errno)); |
187f87f0 | 4340 | goto fail; |
c90fd485 SP |
4341 | } |
4342 | ||
4343 | if (os_strlen(interface->global_iface_path) + 1 + | |
4344 | os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path)) | |
4345 | goto fail; | |
4346 | ||
4347 | s = socket(PF_UNIX, SOCK_DGRAM, 0); | |
4348 | if (s < 0) { | |
a193231d | 4349 | wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno)); |
c90fd485 SP |
4350 | goto fail; |
4351 | } | |
4352 | ||
4353 | os_memset(&addr, 0, sizeof(addr)); | |
4354 | #ifdef __FreeBSD__ | |
4355 | addr.sun_len = sizeof(addr); | |
4356 | #endif /* __FreeBSD__ */ | |
4357 | addr.sun_family = AF_UNIX; | |
4358 | fname = hostapd_global_ctrl_iface_path(interface); | |
4359 | if (fname == NULL) | |
4360 | goto fail; | |
4361 | os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path)); | |
4362 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
4363 | wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s", | |
4364 | strerror(errno)); | |
4365 | if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
4366 | wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not" | |
4367 | " allow connections - assuming it was left" | |
4368 | "over from forced program termination"); | |
4369 | if (unlink(fname) < 0) { | |
a193231d JM |
4370 | wpa_printf(MSG_ERROR, |
4371 | "Could not unlink existing ctrl_iface socket '%s': %s", | |
4372 | fname, strerror(errno)); | |
c90fd485 SP |
4373 | goto fail; |
4374 | } | |
4375 | if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < | |
4376 | 0) { | |
a193231d JM |
4377 | wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s", |
4378 | strerror(errno)); | |
c90fd485 SP |
4379 | goto fail; |
4380 | } | |
4381 | wpa_printf(MSG_DEBUG, "Successfully replaced leftover " | |
4382 | "ctrl_iface socket '%s'", fname); | |
4383 | } else { | |
4384 | wpa_printf(MSG_INFO, "ctrl_iface exists and seems to " | |
4385 | "be in use - cannot override it"); | |
4386 | wpa_printf(MSG_INFO, "Delete '%s' manually if it is " | |
4387 | "not used anymore", fname); | |
4388 | os_free(fname); | |
4389 | fname = NULL; | |
4390 | goto fail; | |
4391 | } | |
4392 | } | |
4393 | ||
187f87f0 | 4394 | if (interface->ctrl_iface_group && |
02f52ab6 JM |
4395 | lchown(fname, -1, interface->ctrl_iface_group) < 0) { |
4396 | wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s", | |
a193231d | 4397 | strerror(errno)); |
187f87f0 JM |
4398 | goto fail; |
4399 | } | |
4400 | ||
c90fd485 | 4401 | if (chmod(fname, S_IRWXU | S_IRWXG) < 0) { |
a193231d JM |
4402 | wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s", |
4403 | strerror(errno)); | |
c90fd485 SP |
4404 | goto fail; |
4405 | } | |
4406 | os_free(fname); | |
4407 | ||
4408 | interface->global_ctrl_sock = s; | |
4409 | eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive, | |
4410 | interface, NULL); | |
4411 | ||
4412 | return 0; | |
4413 | ||
4414 | fail: | |
4415 | if (s >= 0) | |
4416 | close(s); | |
4417 | if (fname) { | |
4418 | unlink(fname); | |
4419 | os_free(fname); | |
4420 | } | |
4421 | return -1; | |
56885eec | 4422 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
c90fd485 SP |
4423 | } |
4424 | ||
4425 | ||
4426 | void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces) | |
4427 | { | |
56885eec | 4428 | #ifndef CONFIG_CTRL_IFACE_UDP |
c90fd485 | 4429 | char *fname = NULL; |
56885eec | 4430 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
ee1e3f57 | 4431 | struct wpa_ctrl_dst *dst, *prev; |
c90fd485 SP |
4432 | |
4433 | if (interfaces->global_ctrl_sock > -1) { | |
4434 | eloop_unregister_read_sock(interfaces->global_ctrl_sock); | |
4435 | close(interfaces->global_ctrl_sock); | |
4436 | interfaces->global_ctrl_sock = -1; | |
56885eec | 4437 | #ifndef CONFIG_CTRL_IFACE_UDP |
c90fd485 SP |
4438 | fname = hostapd_global_ctrl_iface_path(interfaces); |
4439 | if (fname) { | |
4440 | unlink(fname); | |
4441 | os_free(fname); | |
4442 | } | |
4443 | ||
4444 | if (interfaces->global_iface_path && | |
4445 | rmdir(interfaces->global_iface_path) < 0) { | |
4446 | if (errno == ENOTEMPTY) { | |
4447 | wpa_printf(MSG_DEBUG, "Control interface " | |
4448 | "directory not empty - leaving it " | |
4449 | "behind"); | |
4450 | } else { | |
2c6f8cf6 JM |
4451 | wpa_printf(MSG_ERROR, |
4452 | "rmdir[ctrl_interface=%s]: %s", | |
4453 | interfaces->global_iface_path, | |
4454 | strerror(errno)); | |
c90fd485 SP |
4455 | } |
4456 | } | |
56885eec | 4457 | #endif /* CONFIG_CTRL_IFACE_UDP */ |
ee1e3f57 AN |
4458 | } |
4459 | ||
4460 | os_free(interfaces->global_iface_path); | |
4461 | interfaces->global_iface_path = NULL; | |
4462 | ||
89b781bc JD |
4463 | dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst, |
4464 | struct wpa_ctrl_dst, list) | |
4465 | os_free(dst); | |
c90fd485 SP |
4466 | } |
4467 | ||
4468 | ||
cc79e06f | 4469 | static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst, |
4470 | const char *buf) | |
4471 | { | |
4472 | /* Enable Probe Request events based on explicit request. | |
4473 | * Other events are enabled by default. | |
4474 | */ | |
4475 | if (str_starts(buf, RX_PROBE_REQUEST)) | |
4476 | return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST); | |
4477 | return 1; | |
4478 | } | |
4479 | ||
4480 | ||
42d16805 | 4481 | static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level, |
ee1e3f57 | 4482 | enum wpa_msg_type type, |
42d16805 | 4483 | const char *buf, size_t len) |
6fc6879b JM |
4484 | { |
4485 | struct wpa_ctrl_dst *dst, *next; | |
89b781bc | 4486 | struct dl_list *ctrl_dst; |
6fc6879b JM |
4487 | struct msghdr msg; |
4488 | int idx; | |
4489 | struct iovec io[2]; | |
4490 | char levelstr[10]; | |
ee1e3f57 | 4491 | int s; |
6fc6879b | 4492 | |
ee1e3f57 AN |
4493 | if (type != WPA_MSG_ONLY_GLOBAL) { |
4494 | s = hapd->ctrl_sock; | |
89b781bc | 4495 | ctrl_dst = &hapd->ctrl_dst; |
ee1e3f57 AN |
4496 | } else { |
4497 | s = hapd->iface->interfaces->global_ctrl_sock; | |
89b781bc | 4498 | ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst; |
ee1e3f57 AN |
4499 | } |
4500 | ||
89b781bc | 4501 | if (s < 0 || dl_list_empty(ctrl_dst)) |
6fc6879b JM |
4502 | return; |
4503 | ||
4504 | os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); | |
4505 | io[0].iov_base = levelstr; | |
4506 | io[0].iov_len = os_strlen(levelstr); | |
42d16805 | 4507 | io[1].iov_base = (char *) buf; |
6fc6879b JM |
4508 | io[1].iov_len = len; |
4509 | os_memset(&msg, 0, sizeof(msg)); | |
4510 | msg.msg_iov = io; | |
4511 | msg.msg_iovlen = 2; | |
4512 | ||
4513 | idx = 0; | |
89b781bc | 4514 | dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) { |
cc79e06f | 4515 | if ((level >= dst->debug_level) && |
4516 | hostapd_ctrl_check_event_enabled(dst, buf)) { | |
acf57fae JD |
4517 | sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send", |
4518 | &dst->addr, dst->addrlen); | |
6fc6879b JM |
4519 | msg.msg_name = &dst->addr; |
4520 | msg.msg_namelen = dst->addrlen; | |
ee1e3f57 | 4521 | if (sendmsg(s, &msg, 0) < 0) { |
c5aaa015 JM |
4522 | int _errno = errno; |
4523 | wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: " | |
4524 | "%d - %s", | |
4525 | idx, errno, strerror(errno)); | |
6fc6879b | 4526 | dst->errors++; |
c5aaa015 | 4527 | if (dst->errors > 10 || _errno == ENOENT) { |
ee1e3f57 AN |
4528 | if (type != WPA_MSG_ONLY_GLOBAL) |
4529 | hostapd_ctrl_iface_detach( | |
4530 | hapd, &dst->addr, | |
4531 | dst->addrlen); | |
4532 | else | |
4533 | hostapd_global_ctrl_iface_detach( | |
4534 | hapd->iface->interfaces, | |
4535 | &dst->addr, | |
4536 | dst->addrlen); | |
6fc6879b JM |
4537 | } |
4538 | } else | |
4539 | dst->errors = 0; | |
4540 | } | |
4541 | idx++; | |
6fc6879b JM |
4542 | } |
4543 | } | |
4544 | ||
4545 | #endif /* CONFIG_NATIVE_WINDOWS */ |