blob: ea9c444436984ecadfcc3f446a80498beef79db7 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / UNIX domain socket -based control interface
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080013#ifdef CONFIG_TESTING_OPTIONS
Hai Shalomfdcde762020-04-02 11:19:20 -070014#ifdef __NetBSD__
15#include <net/if_ether.h>
16#else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080017#include <net/ethernet.h>
Hai Shalomfdcde762020-04-02 11:19:20 -070018#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080019#include <netinet/ip.h>
20#endif /* CONFIG_TESTING_OPTIONS */
21
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <sys/un.h>
23#include <sys/stat.h>
24#include <stddef.h>
25
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080026#ifdef CONFIG_CTRL_IFACE_UDP
27#include <netdb.h>
28#endif /* CONFIG_CTRL_IFACE_UDP */
29
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "utils/common.h"
31#include "utils/eloop.h"
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070032#include "utils/module_tests.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "common/version.h"
34#include "common/ieee802_11_defs.h"
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080035#include "common/ctrl_iface_common.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070036#ifdef CONFIG_DPP
37#include "common/dpp.h"
38#endif /* CONFIG_DPP */
39#include "common/wpa_ctrl.h"
Hai Shalom60840252021-02-19 19:02:11 -080040#include "common/ptksa_cache.h"
Sunil Ravi036cec52023-03-29 11:35:17 -070041#include "common/hw_features_common.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080042#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080044#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080046#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080047#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048#include "ap/hostapd.h"
49#include "ap/ap_config.h"
50#include "ap/ieee802_1x.h"
51#include "ap/wpa_auth.h"
Hai Shalom899fcc72020-10-19 14:38:18 -070052#include "ap/pmksa_cache_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053#include "ap/ieee802_11.h"
54#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055#include "ap/wps_hostapd.h"
56#include "ap/ctrl_iface_ap.h"
57#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080058#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080059#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070060#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080061#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070062#include "ap/neighbor_db.h"
63#include "ap/rrm.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070064#include "ap/dpp_hostapd.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070065#include "ap/dfs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066#include "wps/wps_defs.h"
67#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080068#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070069#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070#include "ctrl_iface.h"
71
72
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080073#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
74
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080075#ifdef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080076#define HOSTAPD_CTRL_IFACE_PORT 8877
77#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
78#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
79#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
80#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081
82static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020083 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 const char *buf, size_t len);
85
86
87static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080088 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -070089 socklen_t fromlen, const char *input)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090{
Roshan Pius3a1667e2018-07-03 15:17:14 -070091 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092}
93
94
95static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080096 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 socklen_t fromlen)
98{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080099 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100}
101
102
103static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800104 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 socklen_t fromlen,
106 char *level)
107{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800108 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109}
110
111
112static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
113 const char *txtaddr)
114{
115 u8 addr[ETH_ALEN];
116 struct sta_info *sta;
117
118 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
119
120 if (hwaddr_aton(txtaddr, addr))
121 return -1;
122
123 sta = ap_get_sta(hapd, addr);
124 if (sta)
125 return 0;
126
127 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
128 "notification", MAC2STR(addr));
129 sta = ap_sta_add(hapd, addr);
130 if (sta == NULL)
131 return -1;
132
133 hostapd_new_assoc_sta(hapd, sta, 0);
134 return 0;
135}
136
137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138#ifdef NEED_AP_MLME
139static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
140 const char *txtaddr)
141{
142 u8 addr[ETH_ALEN];
143 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
144
145 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
146
147 if (hwaddr_aton(txtaddr, addr) ||
148 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
149 return -1;
150
151 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
152
153 return 0;
154}
155#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156
157
158#ifdef CONFIG_WPS
159static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
160{
161 char *pin = os_strchr(txt, ' ');
162 char *timeout_txt;
163 int timeout;
164 u8 addr_buf[ETH_ALEN], *addr = NULL;
165 char *pos;
166
167 if (pin == NULL)
168 return -1;
169 *pin++ = '\0';
170
171 timeout_txt = os_strchr(pin, ' ');
172 if (timeout_txt) {
173 *timeout_txt++ = '\0';
174 timeout = atoi(timeout_txt);
175 pos = os_strchr(timeout_txt, ' ');
176 if (pos) {
177 *pos++ = '\0';
178 if (hwaddr_aton(pos, addr_buf) == 0)
179 addr = addr_buf;
180 }
181 } else
182 timeout = 0;
183
184 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
185}
186
187
188static int hostapd_ctrl_iface_wps_check_pin(
189 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
190{
191 char pin[9];
192 size_t len;
193 char *pos;
194 int ret;
195
196 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
197 (u8 *) cmd, os_strlen(cmd));
198 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
199 if (*pos < '0' || *pos > '9')
200 continue;
201 pin[len++] = *pos;
202 if (len == 9) {
203 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
204 return -1;
205 }
206 }
207 if (len != 4 && len != 8) {
208 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
209 return -1;
210 }
211 pin[len] = '\0';
212
213 if (len == 8) {
214 unsigned int pin_val;
215 pin_val = atoi(pin);
216 if (!wps_pin_valid(pin_val)) {
217 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
218 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800219 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 return -1;
221 return ret;
222 }
223 }
224
225 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800226 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 return -1;
228
229 return ret;
230}
231
232
Dmitry Shmidt04949592012-07-19 12:16:46 -0700233#ifdef CONFIG_WPS_NFC
234static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
235 char *pos)
236{
237 size_t len;
238 struct wpabuf *buf;
239 int ret;
240
241 len = os_strlen(pos);
242 if (len & 0x01)
243 return -1;
244 len /= 2;
245
246 buf = wpabuf_alloc(len);
247 if (buf == NULL)
248 return -1;
249 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
250 wpabuf_free(buf);
251 return -1;
252 }
253
254 ret = hostapd_wps_nfc_tag_read(hapd, buf);
255 wpabuf_free(buf);
256
257 return ret;
258}
259
260
261static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
262 char *cmd, char *reply,
263 size_t max_len)
264{
265 int ndef;
266 struct wpabuf *buf;
267 int res;
268
269 if (os_strcmp(cmd, "WPS") == 0)
270 ndef = 0;
271 else if (os_strcmp(cmd, "NDEF") == 0)
272 ndef = 1;
273 else
274 return -1;
275
276 buf = hostapd_wps_nfc_config_token(hapd, ndef);
277 if (buf == NULL)
278 return -1;
279
280 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
281 wpabuf_len(buf));
282 reply[res++] = '\n';
283 reply[res] = '\0';
284
285 wpabuf_free(buf);
286
287 return res;
288}
289
290
291static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
292 char *reply, size_t max_len,
293 int ndef)
294{
295 struct wpabuf *buf;
296 int res;
297
298 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
299 if (buf == NULL)
300 return -1;
301
302 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
303 wpabuf_len(buf));
304 reply[res++] = '\n';
305 reply[res] = '\0';
306
307 wpabuf_free(buf);
308
309 return res;
310}
311
312
313static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
314 char *cmd, char *reply,
315 size_t max_len)
316{
317 if (os_strcmp(cmd, "WPS") == 0)
318 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
319 max_len, 0);
320
321 if (os_strcmp(cmd, "NDEF") == 0)
322 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
323 max_len, 1);
324
325 if (os_strcmp(cmd, "enable") == 0)
326 return hostapd_wps_nfc_token_enable(hapd);
327
328 if (os_strcmp(cmd, "disable") == 0) {
329 hostapd_wps_nfc_token_disable(hapd);
330 return 0;
331 }
332
333 return -1;
334}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800335
336
337static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
338 char *cmd, char *reply,
339 size_t max_len)
340{
341 struct wpabuf *buf;
342 int res;
343 char *pos;
344 int ndef;
345
346 pos = os_strchr(cmd, ' ');
347 if (pos == NULL)
348 return -1;
349 *pos++ = '\0';
350
351 if (os_strcmp(cmd, "WPS") == 0)
352 ndef = 0;
353 else if (os_strcmp(cmd, "NDEF") == 0)
354 ndef = 1;
355 else
356 return -1;
357
358 if (os_strcmp(pos, "WPS-CR") == 0)
359 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
360 else
361 buf = NULL;
362 if (buf == NULL)
363 return -1;
364
365 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
366 wpabuf_len(buf));
367 reply[res++] = '\n';
368 reply[res] = '\0';
369
370 wpabuf_free(buf);
371
372 return res;
373}
374
375
376static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
377 char *cmd)
378{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800379 size_t len;
380 struct wpabuf *req, *sel;
381 int ret;
382 char *pos, *role, *type, *pos2;
383
384 role = cmd;
385 pos = os_strchr(role, ' ');
386 if (pos == NULL)
387 return -1;
388 *pos++ = '\0';
389
390 type = pos;
391 pos = os_strchr(type, ' ');
392 if (pos == NULL)
393 return -1;
394 *pos++ = '\0';
395
396 pos2 = os_strchr(pos, ' ');
397 if (pos2 == NULL)
398 return -1;
399 *pos2++ = '\0';
400
401 len = os_strlen(pos);
402 if (len & 0x01)
403 return -1;
404 len /= 2;
405
406 req = wpabuf_alloc(len);
407 if (req == NULL)
408 return -1;
409 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
410 wpabuf_free(req);
411 return -1;
412 }
413
414 len = os_strlen(pos2);
415 if (len & 0x01) {
416 wpabuf_free(req);
417 return -1;
418 }
419 len /= 2;
420
421 sel = wpabuf_alloc(len);
422 if (sel == NULL) {
423 wpabuf_free(req);
424 return -1;
425 }
426 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
427 wpabuf_free(req);
428 wpabuf_free(sel);
429 return -1;
430 }
431
432 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
433 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
434 } else {
435 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
436 "reported: role=%s type=%s", role, type);
437 ret = -1;
438 }
439 wpabuf_free(req);
440 wpabuf_free(sel);
441
442 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800443}
444
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445#endif /* CONFIG_WPS_NFC */
446
447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
449 char *buf, size_t buflen)
450{
451 int timeout = 300;
452 char *pos;
453 const char *pin_txt;
454
455 pos = os_strchr(txt, ' ');
456 if (pos)
457 *pos++ = '\0';
458
459 if (os_strcmp(txt, "disable") == 0) {
460 hostapd_wps_ap_pin_disable(hapd);
461 return os_snprintf(buf, buflen, "OK\n");
462 }
463
464 if (os_strcmp(txt, "random") == 0) {
465 if (pos)
466 timeout = atoi(pos);
467 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
468 if (pin_txt == NULL)
469 return -1;
470 return os_snprintf(buf, buflen, "%s", pin_txt);
471 }
472
473 if (os_strcmp(txt, "get") == 0) {
474 pin_txt = hostapd_wps_ap_pin_get(hapd);
475 if (pin_txt == NULL)
476 return -1;
477 return os_snprintf(buf, buflen, "%s", pin_txt);
478 }
479
480 if (os_strcmp(txt, "set") == 0) {
481 char *pin;
482 if (pos == NULL)
483 return -1;
484 pin = pos;
485 pos = os_strchr(pos, ' ');
486 if (pos) {
487 *pos++ = '\0';
488 timeout = atoi(pos);
489 }
490 if (os_strlen(pin) > buflen)
491 return -1;
492 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
493 return -1;
494 return os_snprintf(buf, buflen, "%s", pin);
495 }
496
497 return -1;
498}
499
500
501static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
502{
503 char *pos;
504 char *ssid, *auth, *encr = NULL, *key = NULL;
505
506 ssid = txt;
507 pos = os_strchr(txt, ' ');
508 if (!pos)
509 return -1;
510 *pos++ = '\0';
511
512 auth = pos;
513 pos = os_strchr(pos, ' ');
514 if (pos) {
515 *pos++ = '\0';
516 encr = pos;
517 pos = os_strchr(pos, ' ');
518 if (pos) {
519 *pos++ = '\0';
520 key = pos;
521 }
522 }
523
524 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
525}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700526
527
528static const char * pbc_status_str(enum pbc_status status)
529{
530 switch (status) {
531 case WPS_PBC_STATUS_DISABLE:
532 return "Disabled";
533 case WPS_PBC_STATUS_ACTIVE:
534 return "Active";
535 case WPS_PBC_STATUS_TIMEOUT:
536 return "Timed-out";
537 case WPS_PBC_STATUS_OVERLAP:
538 return "Overlap";
539 default:
540 return "Unknown";
541 }
542}
543
544
545static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
546 char *buf, size_t buflen)
547{
548 int ret;
549 char *pos, *end;
550
551 pos = buf;
552 end = buf + buflen;
553
554 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
555 pbc_status_str(hapd->wps_stats.pbc_status));
556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800557 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700558 return pos - buf;
559 pos += ret;
560
561 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
562 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
563 "Success":
564 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
565 "Failed" : "None")));
566
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800567 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700568 return pos - buf;
569 pos += ret;
570
571 /* If status == Failure - Add possible Reasons */
572 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
573 hapd->wps_stats.failure_reason > 0) {
574 ret = os_snprintf(pos, end - pos,
575 "Failure Reason: %s\n",
576 wps_ei_str(hapd->wps_stats.failure_reason));
577
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800578 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700579 return pos - buf;
580 pos += ret;
581 }
582
583 if (hapd->wps_stats.status) {
584 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
585 MAC2STR(hapd->wps_stats.peer_addr));
586
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800587 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700588 return pos - buf;
589 pos += ret;
590 }
591
592 return pos - buf;
593}
594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595#endif /* CONFIG_WPS */
596
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800597#ifdef CONFIG_HS20
598
599static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
600 const char *cmd)
601{
602 u8 addr[ETH_ALEN];
603 const char *url;
604
605 if (hwaddr_aton(cmd, addr))
606 return -1;
607 url = cmd + 17;
608 if (*url == '\0') {
609 url = NULL;
610 } else {
611 if (*url != ' ')
612 return -1;
613 url++;
614 if (*url == '\0')
615 url = NULL;
616 }
617
618 return hs20_send_wnm_notification(hapd, addr, 1, url);
619}
620
621
622static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
623 const char *cmd)
624{
625 u8 addr[ETH_ALEN];
626 int code, reauth_delay, ret;
627 const char *pos;
628 size_t url_len;
629 struct wpabuf *req;
630
631 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
632 if (hwaddr_aton(cmd, addr))
633 return -1;
634
635 pos = os_strchr(cmd, ' ');
636 if (pos == NULL)
637 return -1;
638 pos++;
639 code = atoi(pos);
640
641 pos = os_strchr(pos, ' ');
642 if (pos == NULL)
643 return -1;
644 pos++;
645 reauth_delay = atoi(pos);
646
647 url_len = 0;
648 pos = os_strchr(pos, ' ');
649 if (pos) {
650 pos++;
651 url_len = os_strlen(pos);
652 }
653
654 req = wpabuf_alloc(4 + url_len);
655 if (req == NULL)
656 return -1;
657 wpabuf_put_u8(req, code);
658 wpabuf_put_le16(req, reauth_delay);
659 wpabuf_put_u8(req, url_len);
660 if (pos)
661 wpabuf_put_data(req, pos, url_len);
662
663 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
664 " to indicate imminent deauthentication (code=%d "
665 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
666 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
667 wpabuf_free(req);
668 return ret;
669}
670
671#endif /* CONFIG_HS20 */
672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673
Dmitry Shmidt051af732013-10-22 13:52:46 -0700674#ifdef CONFIG_INTERWORKING
675
676static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
677 const char *cmd)
678{
679 u8 qos_map_set[16 + 2 * 21], count = 0;
680 const char *pos = cmd;
681 int val, ret;
682
683 for (;;) {
684 if (count == sizeof(qos_map_set)) {
685 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
686 return -1;
687 }
688
689 val = atoi(pos);
690 if (val < 0 || val > 255) {
691 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
692 return -1;
693 }
694
695 qos_map_set[count++] = val;
696 pos = os_strchr(pos, ',');
697 if (!pos)
698 break;
699 pos++;
700 }
701
702 if (count < 16 || count & 1) {
703 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
704 return -1;
705 }
706
707 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
708 if (ret) {
709 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
710 return -1;
711 }
712
713 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
714 hapd->conf->qos_map_set_len = count;
715
716 return 0;
717}
718
719
720static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
721 const char *cmd)
722{
723 u8 addr[ETH_ALEN];
724 struct sta_info *sta;
725 struct wpabuf *buf;
726 u8 *qos_map_set = hapd->conf->qos_map_set;
727 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
728 int ret;
729
730 if (!qos_map_set_len) {
731 wpa_printf(MSG_INFO, "QoS Map Set is not set");
732 return -1;
733 }
734
735 if (hwaddr_aton(cmd, addr))
736 return -1;
737
738 sta = ap_get_sta(hapd, addr);
739 if (sta == NULL) {
740 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
741 "for QoS Map Configuration message",
742 MAC2STR(addr));
743 return -1;
744 }
745
746 if (!sta->qos_map_enabled) {
747 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
748 "support for QoS Map", MAC2STR(addr));
749 return -1;
750 }
751
752 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
753 if (buf == NULL)
754 return -1;
755
756 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
757 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
758
759 /* QoS Map Set Element */
760 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
761 wpabuf_put_u8(buf, qos_map_set_len);
762 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
763
764 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
765 wpabuf_head(buf), wpabuf_len(buf));
766 wpabuf_free(buf);
767
768 return ret;
769}
770
771#endif /* CONFIG_INTERWORKING */
772
773
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700774#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800775
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800776static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
777 const char *cmd)
778{
779 u8 addr[ETH_ALEN];
780 struct sta_info *sta;
781 const char *pos;
782 unsigned int auto_report, timeout;
783
784 if (hwaddr_aton(cmd, addr)) {
785 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
786 return -1;
787 }
788
789 sta = ap_get_sta(hapd, addr);
790 if (!sta) {
791 wpa_printf(MSG_DEBUG, "Station " MACSTR
792 " not found for Collocated Interference Request",
793 MAC2STR(addr));
794 return -1;
795 }
796
797 pos = cmd + 17;
798 if (*pos != ' ')
799 return -1;
800 pos++;
801 auto_report = atoi(pos);
802 pos = os_strchr(pos, ' ');
803 if (!pos)
804 return -1;
805 pos++;
806 timeout = atoi(pos);
807
808 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
809}
810
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700811#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800812
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800813
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800814static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
815 char *buf, size_t buflen)
816{
817 int ret = 0;
818 char *pos, *end;
819
820 pos = buf;
821 end = buf + buflen;
822
823 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
824
825 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
826 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
827 if (os_snprintf_error(end - pos, ret))
828 return pos - buf;
829 pos += ret;
830 }
831 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
832 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
833 if (os_snprintf_error(end - pos, ret))
834 return pos - buf;
835 pos += ret;
836 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800837#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800838 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
839 ret = os_snprintf(pos, end - pos, "FT-PSK ");
840 if (os_snprintf_error(end - pos, ret))
841 return pos - buf;
842 pos += ret;
843 }
844 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
845 ret = os_snprintf(pos, end - pos, "FT-EAP ");
846 if (os_snprintf_error(end - pos, ret))
847 return pos - buf;
848 pos += ret;
849 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700850#ifdef CONFIG_SHA384
851 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
852 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
853 if (os_snprintf_error(end - pos, ret))
854 return pos - buf;
855 pos += ret;
856 }
857#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800858#ifdef CONFIG_SAE
859 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
860 ret = os_snprintf(pos, end - pos, "FT-SAE ");
861 if (os_snprintf_error(end - pos, ret))
862 return pos - buf;
863 pos += ret;
864 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700865 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
866 ret = os_snprintf(pos, end - pos, "FT-SAE-EXT-KEY ");
867 if (os_snprintf_error(end - pos, ret))
868 return pos - buf;
869 pos += ret;
870 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800871#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800872#ifdef CONFIG_FILS
873 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
874 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
875 if (os_snprintf_error(end - pos, ret))
876 return pos - buf;
877 pos += ret;
878 }
879 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
880 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
881 if (os_snprintf_error(end - pos, ret))
882 return pos - buf;
883 pos += ret;
884 }
885#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800886#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800887 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
888 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
889 if (os_snprintf_error(end - pos, ret))
890 return pos - buf;
891 pos += ret;
892 }
893 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
894 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
895 if (os_snprintf_error(end - pos, ret))
896 return pos - buf;
897 pos += ret;
898 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800899#ifdef CONFIG_SAE
900 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
901 ret = os_snprintf(pos, end - pos, "SAE ");
902 if (os_snprintf_error(end - pos, ret))
903 return pos - buf;
904 pos += ret;
905 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700906 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
907 ret = os_snprintf(pos, end - pos, "SAE-EXT-KEY ");
908 if (os_snprintf_error(end - pos, ret))
909 return pos - buf;
910 pos += ret;
911 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800912#endif /* CONFIG_SAE */
913 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
914 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
915 if (os_snprintf_error(end - pos, ret))
916 return pos - buf;
917 pos += ret;
918 }
919 if (hapd->conf->wpa_key_mgmt &
920 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
921 ret = os_snprintf(pos, end - pos,
922 "WPA-EAP-SUITE-B-192 ");
923 if (os_snprintf_error(end - pos, ret))
924 return pos - buf;
925 pos += ret;
926 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800927#ifdef CONFIG_FILS
928 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
929 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
930 if (os_snprintf_error(end - pos, ret))
931 return pos - buf;
932 pos += ret;
933 }
934 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
935 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
936 if (os_snprintf_error(end - pos, ret))
937 return pos - buf;
938 pos += ret;
939 }
940#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800941
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700942#ifdef CONFIG_OWE
943 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
944 ret = os_snprintf(pos, end - pos, "OWE ");
945 if (os_snprintf_error(end - pos, ret))
946 return pos - buf;
947 pos += ret;
948 }
949#endif /* CONFIG_OWE */
950
951#ifdef CONFIG_DPP
952 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
953 ret = os_snprintf(pos, end - pos, "DPP ");
954 if (os_snprintf_error(end - pos, ret))
955 return pos - buf;
956 pos += ret;
957 }
958#endif /* CONFIG_DPP */
959
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800960 if (pos > buf && *(pos - 1) == ' ') {
961 *(pos - 1) = '\0';
962 pos--;
963 }
964
965 return pos - buf;
966}
967
968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
970 char *buf, size_t buflen)
971{
972 int ret;
973 char *pos, *end;
974
975 pos = buf;
976 end = buf + buflen;
977
978 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
979 "ssid=%s\n",
980 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700981 wpa_ssid_txt(hapd->conf->ssid.ssid,
982 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800983 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 return pos - buf;
985 pos += ret;
986
Sunil Ravi77d572f2023-01-17 23:58:31 +0000987 if ((hapd->conf->config_id)) {
988 ret = os_snprintf(pos, end - pos, "config_id=%s\n",
989 hapd->conf->config_id);
990 if (os_snprintf_error(end - pos, ret))
991 return pos - buf;
992 pos += ret;
993 }
994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995#ifdef CONFIG_WPS
996 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
997 hapd->conf->wps_state == 0 ? "disabled" :
998 (hapd->conf->wps_state == 1 ? "not configured" :
999 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001000 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 return pos - buf;
1002 pos += ret;
1003
1004 if (hapd->conf->wps_state && hapd->conf->wpa &&
1005 hapd->conf->ssid.wpa_passphrase) {
1006 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1007 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001008 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 return pos - buf;
1010 pos += ret;
1011 }
1012
1013 if (hapd->conf->wps_state && hapd->conf->wpa &&
1014 hapd->conf->ssid.wpa_psk &&
1015 hapd->conf->ssid.wpa_psk->group) {
1016 char hex[PMK_LEN * 2 + 1];
1017 wpa_snprintf_hex(hex, sizeof(hex),
1018 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1019 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001020 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 return pos - buf;
1022 pos += ret;
1023 }
Hai Shalom60840252021-02-19 19:02:11 -08001024
1025 if (hapd->conf->multi_ap) {
1026 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1027
1028 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1029 hapd->conf->multi_ap);
1030 if (os_snprintf_error(end - pos, ret))
1031 return pos - buf;
1032 pos += ret;
1033
1034 if (ssid->ssid_len) {
1035 ret = os_snprintf(pos, end - pos,
1036 "multi_ap_backhaul_ssid=%s\n",
1037 wpa_ssid_txt(ssid->ssid,
1038 ssid->ssid_len));
1039 if (os_snprintf_error(end - pos, ret))
1040 return pos - buf;
1041 pos += ret;
1042 }
1043
1044 if (hapd->conf->wps_state && hapd->conf->wpa &&
1045 ssid->wpa_passphrase) {
1046 ret = os_snprintf(pos, end - pos,
1047 "multi_ap_backhaul_wpa_passphrase=%s\n",
1048 ssid->wpa_passphrase);
1049 if (os_snprintf_error(end - pos, ret))
1050 return pos - buf;
1051 pos += ret;
1052 }
1053
1054 if (hapd->conf->wps_state && hapd->conf->wpa &&
1055 ssid->wpa_psk &&
1056 ssid->wpa_psk->group) {
1057 char hex[PMK_LEN * 2 + 1];
1058
1059 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1060 PMK_LEN);
1061 ret = os_snprintf(pos, end - pos,
1062 "multi_ap_backhaul_wpa_psk=%s\n",
1063 hex);
1064 forced_memzero(hex, sizeof(hex));
1065 if (os_snprintf_error(end - pos, ret))
1066 return pos - buf;
1067 pos += ret;
1068 }
1069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070#endif /* CONFIG_WPS */
1071
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001072 if (hapd->conf->wpa) {
1073 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1074 if (os_snprintf_error(end - pos, ret))
1075 return pos - buf;
1076 pos += ret;
1077 }
1078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1080 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001081 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 return pos - buf;
1083 pos += ret;
1084
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001085 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086
1087 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001088 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 return pos - buf;
1090 pos += ret;
1091 }
1092
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001093 if (hapd->conf->wpa) {
1094 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1095 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001096 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 return pos - buf;
1098 pos += ret;
1099 }
1100
1101 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1102 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001103 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 return pos - buf;
1105 pos += ret;
1106
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001107 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1108 " ");
1109 if (ret < 0)
1110 return pos - buf;
1111 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
1113 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001114 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 return pos - buf;
1116 pos += ret;
1117 }
1118
1119 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1120 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001121 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 return pos - buf;
1123 pos += ret;
1124
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001125 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001126 " ");
1127 if (ret < 0)
1128 return pos - buf;
1129 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130
1131 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001132 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 return pos - buf;
1134 pos += ret;
1135 }
1136
Hai Shalomfdcde762020-04-02 11:19:20 -07001137 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1138 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1139 hapd->conf->wpa_deny_ptk0_rekey);
1140 if (os_snprintf_error(end - pos, ret))
1141 return pos - buf;
1142 pos += ret;
1143 }
1144
1145 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1146 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1147 hapd->conf->extended_key_id);
1148 if (os_snprintf_error(end - pos, ret))
1149 return pos - buf;
1150 pos += ret;
1151 }
1152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 return pos - buf;
1154}
1155
1156
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001157static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
Hai Shalom60840252021-02-19 19:02:11 -08001158 const char *bands)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001159{
1160 union wpa_event_data event;
Hai Shalom60840252021-02-19 19:02:11 -08001161 u32 setband_mask = WPA_SETBAND_AUTO;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001162
Hai Shalom60840252021-02-19 19:02:11 -08001163 /*
1164 * For example:
1165 * SET setband 2G,6G
1166 * SET setband 5G
1167 * SET setband AUTO
1168 */
1169 if (!os_strstr(bands, "AUTO")) {
1170 if (os_strstr(bands, "5G"))
1171 setband_mask |= WPA_SETBAND_5G;
1172 if (os_strstr(bands, "6G"))
1173 setband_mask |= WPA_SETBAND_6G;
1174 if (os_strstr(bands, "2G"))
1175 setband_mask |= WPA_SETBAND_2G;
1176 if (setband_mask == WPA_SETBAND_AUTO)
1177 return -1;
1178 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001179
Hai Shalom60840252021-02-19 19:02:11 -08001180 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001181 os_memset(&event, 0, sizeof(event));
1182 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1183 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1184 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1185 }
1186
1187 return 0;
1188}
1189
1190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1192{
1193 char *value;
1194 int ret = 0;
1195
1196 value = os_strchr(cmd, ' ');
1197 if (value == NULL)
1198 return -1;
1199 *value++ = '\0';
1200
1201 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1202 if (0) {
1203#ifdef CONFIG_WPS_TESTING
1204 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1205 long int val;
1206 val = strtol(value, NULL, 0);
1207 if (val < 0 || val > 0xff) {
1208 ret = -1;
1209 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1210 "wps_version_number %ld", val);
1211 } else {
1212 wps_version_number = val;
1213 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1214 "version %u.%u",
1215 (wps_version_number & 0xf0) >> 4,
1216 wps_version_number & 0x0f);
1217 hostapd_wps_update_ie(hapd);
1218 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001219 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1220 wps_testing_stub_cred = atoi(value);
1221 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1222 wps_testing_stub_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001223 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1224 wps_corrupt_pkhash = atoi(value);
1225 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1226 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001228#ifdef CONFIG_TESTING_OPTIONS
1229 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1230 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001231 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1232 hapd->ext_eapol_frame_io = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001233 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1234 hapd->force_backlog_bytes = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001235#ifdef CONFIG_DPP
1236 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1237 os_free(hapd->dpp_config_obj_override);
1238 hapd->dpp_config_obj_override = os_strdup(value);
1239 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1240 os_free(hapd->dpp_discovery_override);
1241 hapd->dpp_discovery_override = os_strdup(value);
1242 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1243 os_free(hapd->dpp_groups_override);
1244 hapd->dpp_groups_override = os_strdup(value);
1245 } else if (os_strcasecmp(cmd,
1246 "dpp_ignore_netaccesskey_mismatch") == 0) {
1247 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001248 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1249 dpp_test = atoi(value);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001250 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1251 dpp_version_override = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001252#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001253#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001254#ifdef CONFIG_MBO
1255 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1256 int val;
1257
1258 if (!hapd->conf->mbo_enabled)
1259 return -1;
1260
1261 val = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001262 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001263 return -1;
1264
1265 hapd->mbo_assoc_disallow = val;
1266 ieee802_11_update_beacons(hapd->iface);
1267
1268 /*
1269 * TODO: Need to configure drivers that do AP MLME offload with
1270 * disallowing station logic.
1271 */
1272#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001273#ifdef CONFIG_DPP
1274 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1275 os_free(hapd->dpp_configurator_params);
1276 hapd->dpp_configurator_params = os_strdup(value);
Sunil Ravia04bd252022-05-02 22:54:18 -07001277#ifdef CONFIG_DPP2
1278 dpp_controller_set_params(hapd->iface->interfaces->dpp, value);
1279#endif /* CONFIG_DPP2 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001280 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1281 hapd->dpp_init_max_tries = atoi(value);
1282 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1283 hapd->dpp_init_retry_time = atoi(value);
1284 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1285 hapd->dpp_resp_wait_time = atoi(value);
1286 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1287 hapd->dpp_resp_max_tries = atoi(value);
1288 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1289 hapd->dpp_resp_retry_time = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001290#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001291 } else if (os_strcasecmp(cmd, "setband") == 0) {
1292 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001294 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001295 if (ret)
1296 return ret;
1297
1298 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001299 hostapd_disassoc_deny_mac(hapd);
1300 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1301 hostapd_disassoc_accept_mac(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001302 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1303 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1304 hapd->parameter_set_count++;
1305 if (ieee802_11_update_beacons(hapd->iface))
1306 wpa_printf(MSG_DEBUG,
1307 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001308 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1309 os_strcmp(cmd, "sae_password") == 0 ||
1310 os_strcmp(cmd, "sae_pwe") == 0) {
1311 if (hapd->started)
1312 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001313 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1314 wpa_auth_set_transition_disable(hapd->wpa_auth,
1315 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001316 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001317
1318#ifdef CONFIG_TESTING_OPTIONS
1319 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1320 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1321 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001322 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1323 wpa_auth_set_ocv_override_freq(
1324 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1325 atoi(value));
1326 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1327 wpa_auth_set_ocv_override_freq(
1328 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1329 atoi(value));
1330 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1331 wpa_auth_set_ocv_override_freq(
1332 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1333 atoi(value));
1334 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1335 wpa_auth_set_ocv_override_freq(
1336 hapd->wpa_auth,
1337 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001338 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1339 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1340 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1341 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001342#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 }
1344
1345 return ret;
1346}
1347
1348
1349static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1350 char *buf, size_t buflen)
1351{
1352 int res;
1353
1354 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1355
1356 if (os_strcmp(cmd, "version") == 0) {
1357 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001358 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 return -1;
1360 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001361 } else if (os_strcmp(cmd, "tls_library") == 0) {
1362 res = tls_get_library_version(buf, buflen);
1363 if (os_snprintf_error(buflen, res))
1364 return -1;
1365 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 }
1367
1368 return -1;
1369}
1370
1371
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001372static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1373{
1374 if (hostapd_enable_iface(iface) < 0) {
1375 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1376 return -1;
1377 }
1378 return 0;
1379}
1380
1381
1382static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1383{
1384 if (hostapd_reload_iface(iface) < 0) {
1385 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1386 return -1;
1387 }
1388 return 0;
1389}
1390
1391
Sunil Ravi77d572f2023-01-17 23:58:31 +00001392static int hostapd_ctrl_iface_reload_bss(struct hostapd_data *bss)
1393{
1394 if (hostapd_reload_bss_only(bss) < 0) {
1395 wpa_printf(MSG_ERROR, "Reloading of BSS failed");
1396 return -1;
1397 }
1398 return 0;
1399}
1400
1401
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001402static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1403{
1404 if (hostapd_disable_iface(iface) < 0) {
1405 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1406 return -1;
1407 }
1408 return 0;
1409}
1410
1411
Hai Shalom74f70d42019-02-11 14:42:39 -08001412static int
1413hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1414 struct sta_info *sta, void *ctx)
1415{
1416 struct hostapd_wpa_psk *psk;
1417 const u8 *pmk;
1418 int pmk_len;
1419 int pmk_match;
1420 int sta_match;
1421 int bss_match;
1422 int reason;
1423
1424 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1425
1426 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1427 pmk_match = PMK_LEN == pmk_len &&
1428 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1429 sta_match = psk->group == 0 &&
1430 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1431 bss_match = psk->group == 1;
1432
1433 if (pmk_match && (sta_match || bss_match))
1434 return 0;
1435 }
1436
1437 wpa_printf(MSG_INFO, "STA " MACSTR
1438 " PSK/passphrase no longer valid - disconnect",
1439 MAC2STR(sta->addr));
1440 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1441 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1442 ap_sta_deauthenticate(hapd, sta, reason);
1443
1444 return 0;
1445}
1446
1447
1448static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1449{
1450 struct hostapd_bss_config *conf = hapd->conf;
1451 int err;
1452
1453 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1454
1455 err = hostapd_setup_wpa_psk(conf);
1456 if (err < 0) {
1457 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1458 err);
1459 return -1;
1460 }
1461
1462 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1463 NULL);
1464
1465 return 0;
1466}
1467
1468
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001469#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001470
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001471static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1472{
1473 union wpa_event_data data;
1474 char *pos, *param;
1475 enum wpa_event_type event;
1476
1477 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1478
1479 os_memset(&data, 0, sizeof(data));
1480
1481 param = os_strchr(cmd, ' ');
1482 if (param == NULL)
1483 return -1;
1484 *param++ = '\0';
1485
1486 if (os_strcmp(cmd, "DETECTED") == 0)
1487 event = EVENT_DFS_RADAR_DETECTED;
1488 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1489 event = EVENT_DFS_CAC_FINISHED;
1490 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1491 event = EVENT_DFS_CAC_ABORTED;
1492 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1493 event = EVENT_DFS_NOP_FINISHED;
1494 else {
1495 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1496 cmd);
1497 return -1;
1498 }
1499
1500 pos = os_strstr(param, "freq=");
1501 if (pos)
1502 data.dfs_event.freq = atoi(pos + 5);
1503
1504 pos = os_strstr(param, "ht_enabled=1");
1505 if (pos)
1506 data.dfs_event.ht_enabled = 1;
1507
1508 pos = os_strstr(param, "chan_offset=");
1509 if (pos)
1510 data.dfs_event.chan_offset = atoi(pos + 12);
1511
1512 pos = os_strstr(param, "chan_width=");
1513 if (pos)
1514 data.dfs_event.chan_width = atoi(pos + 11);
1515
1516 pos = os_strstr(param, "cf1=");
1517 if (pos)
1518 data.dfs_event.cf1 = atoi(pos + 4);
1519
1520 pos = os_strstr(param, "cf2=");
1521 if (pos)
1522 data.dfs_event.cf2 = atoi(pos + 4);
1523
1524 wpa_supplicant_event(hapd, event, &data);
1525
1526 return 0;
1527}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001528
1529
1530static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1531{
1532 size_t len;
1533 u8 *buf;
1534 int res;
1535
1536 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1537
1538 len = os_strlen(cmd);
1539 if (len & 1)
1540 return -1;
1541 len /= 2;
1542
1543 buf = os_malloc(len);
1544 if (buf == NULL)
1545 return -1;
1546
1547 if (hexstr2bin(cmd, buf, len) < 0) {
1548 os_free(buf);
1549 return -1;
1550 }
1551
Hai Shalomfdcde762020-04-02 11:19:20 -07001552 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001553 os_free(buf);
1554 return res;
1555}
1556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001557
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001558static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1559 char *cmd)
1560{
1561 char *pos, *param;
1562 size_t len;
1563 u8 *buf;
1564 int stype = 0, ok = 0;
1565 union wpa_event_data event;
1566
1567 if (!hapd->ext_mgmt_frame_handling)
1568 return -1;
1569
1570 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1571
1572 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1573
1574 pos = cmd;
1575 param = os_strstr(pos, "stype=");
1576 if (param) {
1577 param += 6;
1578 stype = atoi(param);
1579 }
1580
1581 param = os_strstr(pos, " ok=");
1582 if (param) {
1583 param += 4;
1584 ok = atoi(param);
1585 }
1586
1587 param = os_strstr(pos, " buf=");
1588 if (!param)
1589 return -1;
1590 param += 5;
1591
1592 len = os_strlen(param);
1593 if (len & 1)
1594 return -1;
1595 len /= 2;
1596
1597 buf = os_malloc(len);
1598 if (!buf || hexstr2bin(param, buf, len) < 0) {
1599 os_free(buf);
1600 return -1;
1601 }
1602
1603 os_memset(&event, 0, sizeof(event));
1604 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1605 event.tx_status.data = buf;
1606 event.tx_status.data_len = len;
1607 event.tx_status.stype = stype;
1608 event.tx_status.ack = ok;
1609 hapd->ext_mgmt_frame_handling = 0;
1610 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1611 hapd->ext_mgmt_frame_handling = 1;
1612
1613 os_free(buf);
1614
1615 return 0;
1616}
1617
1618
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001619static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1620 char *cmd)
1621{
1622 char *pos, *param;
1623 size_t len;
1624 u8 *buf;
1625 int freq = 0, datarate = 0, ssi_signal = 0;
1626 union wpa_event_data event;
1627
1628 if (!hapd->ext_mgmt_frame_handling)
1629 return -1;
1630
1631 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1632
1633 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1634
1635 pos = cmd;
1636 param = os_strstr(pos, "freq=");
1637 if (param) {
1638 param += 5;
1639 freq = atoi(param);
1640 }
1641
1642 param = os_strstr(pos, " datarate=");
1643 if (param) {
1644 param += 10;
1645 datarate = atoi(param);
1646 }
1647
1648 param = os_strstr(pos, " ssi_signal=");
1649 if (param) {
1650 param += 12;
1651 ssi_signal = atoi(param);
1652 }
1653
1654 param = os_strstr(pos, " frame=");
1655 if (param == NULL)
1656 return -1;
1657 param += 7;
1658
1659 len = os_strlen(param);
1660 if (len & 1)
1661 return -1;
1662 len /= 2;
1663
1664 buf = os_malloc(len);
1665 if (buf == NULL)
1666 return -1;
1667
1668 if (hexstr2bin(param, buf, len) < 0) {
1669 os_free(buf);
1670 return -1;
1671 }
1672
1673 os_memset(&event, 0, sizeof(event));
1674 event.rx_mgmt.freq = freq;
1675 event.rx_mgmt.frame = buf;
1676 event.rx_mgmt.frame_len = len;
1677 event.rx_mgmt.ssi_signal = ssi_signal;
1678 event.rx_mgmt.datarate = datarate;
1679 hapd->ext_mgmt_frame_handling = 0;
1680 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1681 hapd->ext_mgmt_frame_handling = 1;
1682
1683 os_free(buf);
1684
1685 return 0;
1686}
1687
1688
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001689static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1690{
1691 char *pos;
1692 u8 src[ETH_ALEN], *buf;
1693 int used;
1694 size_t len;
1695
1696 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1697
1698 pos = cmd;
1699 used = hwaddr_aton2(pos, src);
1700 if (used < 0)
1701 return -1;
1702 pos += used;
1703 while (*pos == ' ')
1704 pos++;
1705
1706 len = os_strlen(pos);
1707 if (len & 1)
1708 return -1;
1709 len /= 2;
1710
1711 buf = os_malloc(len);
1712 if (buf == NULL)
1713 return -1;
1714
1715 if (hexstr2bin(pos, buf, len) < 0) {
1716 os_free(buf);
1717 return -1;
1718 }
1719
Sunil8cd6f4d2022-06-28 18:40:46 +00001720 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001721 os_free(buf);
1722
1723 return 0;
1724}
1725
1726
Hai Shaloma20dcd72022-02-04 13:43:00 -08001727static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1728{
1729 char *pos, *pos2;
1730 u8 dst[ETH_ALEN], *buf;
1731 int used, ret;
1732 size_t len;
1733 unsigned int prev;
1734 int encrypt = 0;
1735
1736 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1737
1738 pos = cmd;
1739 used = hwaddr_aton2(pos, dst);
1740 if (used < 0)
1741 return -1;
1742 pos += used;
1743 while (*pos == ' ')
1744 pos++;
1745
1746 pos2 = os_strchr(pos, ' ');
1747 if (pos2) {
1748 len = pos2 - pos;
1749 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1750 } else {
1751 len = os_strlen(pos);
1752 }
1753 if (len & 1)
1754 return -1;
1755 len /= 2;
1756
1757 buf = os_malloc(len);
1758 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1759 os_free(buf);
1760 return -1;
1761 }
1762
1763 prev = hapd->ext_eapol_frame_io;
1764 hapd->ext_eapol_frame_io = 0;
1765 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1766 hapd->ext_eapol_frame_io = prev;
1767 os_free(buf);
1768
1769 return ret;
1770}
1771
1772
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001773static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1774{
1775 size_t i;
1776 u32 sum = 0;
1777 const u16 *pos = buf;
1778
1779 for (i = 0; i < len / 2; i++)
1780 sum += *pos++;
1781
1782 while (sum >> 16)
1783 sum = (sum & 0xffff) + (sum >> 16);
1784
1785 return sum ^ 0xffff;
1786}
1787
1788
1789#define HWSIM_PACKETLEN 1500
1790#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1791
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001792static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1793 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001794{
1795 struct hostapd_data *hapd = ctx;
1796 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001797 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001798 const u8 *pos;
1799 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001800 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001801
Hai Shalom81f62d82019-07-22 12:10:00 -07001802 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1803 wpa_printf(MSG_DEBUG,
1804 "test data: RX - ignore unexpected length %d",
1805 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001806 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001807 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001808
1809 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001810 os_memcpy(&ip, eth + 1, sizeof(ip));
1811 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001812
Hai Shalomfdcde762020-04-02 11:19:20 -07001813 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1814 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001815 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07001816 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001817 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001818 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001819
Hai Shalomfdcde762020-04-02 11:19:20 -07001820 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001821 if (*pos != (u8) i) {
1822 wpa_printf(MSG_DEBUG,
1823 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001824 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001825 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001826 pos++;
1827 }
1828
Hai Shalom81f62d82019-07-22 12:10:00 -07001829 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001830 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1831 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001832 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1833 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001834}
1835
1836
1837static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1838 char *cmd)
1839{
1840 int enabled = atoi(cmd);
1841 char *pos;
1842 const char *ifname;
1843
1844 if (!enabled) {
1845 if (hapd->l2_test) {
1846 l2_packet_deinit(hapd->l2_test);
1847 hapd->l2_test = NULL;
1848 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1849 "test data: Disabled");
1850 }
1851 return 0;
1852 }
1853
1854 if (hapd->l2_test)
1855 return 0;
1856
1857 pos = os_strstr(cmd, " ifname=");
1858 if (pos)
1859 ifname = pos + 8;
1860 else
1861 ifname = hapd->conf->iface;
1862
1863 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1864 ETHERTYPE_IP, hostapd_data_test_rx,
1865 hapd, 1);
1866 if (hapd->l2_test == NULL)
1867 return -1;
1868
1869 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1870
1871 return 0;
1872}
1873
1874
1875static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1876{
1877 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001878 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001879 int used;
1880 long int val;
1881 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001883 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001884 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001885 u8 *dpos;
1886 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001887 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001888
1889 if (hapd->l2_test == NULL)
1890 return -1;
1891
Hai Shalom81f62d82019-07-22 12:10:00 -07001892 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893
1894 pos = cmd;
1895 used = hwaddr_aton2(pos, dst);
1896 if (used < 0)
1897 return -1;
1898 pos += used;
1899 while (*pos == ' ')
1900 pos++;
1901 used = hwaddr_aton2(pos, src);
1902 if (used < 0)
1903 return -1;
1904 pos += used;
1905
Hai Shalom81f62d82019-07-22 12:10:00 -07001906 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001907 if (val < 0 || val > 0xff)
1908 return -1;
1909 tos = val;
1910
Hai Shalom81f62d82019-07-22 12:10:00 -07001911 pos = os_strstr(pos2, " len=");
1912 if (pos) {
1913 i = atoi(pos + 5);
1914 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
1915 return -1;
1916 send_len = i;
1917 }
1918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001919 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001920 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1921 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1922 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07001923 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001924 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07001925 ip->ip_hl = 5;
1926 ip->ip_v = 4;
1927 ip->ip_ttl = 64;
1928 ip->ip_tos = tos;
1929 ip->ip_len = htons(send_len);
1930 ip->ip_p = 1;
1931 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1932 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1933 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001934 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07001935 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001936 *dpos++ = i;
1937
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001938 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07001939 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001940 return -1;
1941
1942 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1943 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1944
1945 return 0;
1946}
1947
1948
1949static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1950 char *cmd)
1951{
1952 u8 *buf;
1953 struct ether_header *eth;
1954 struct l2_packet_data *l2 = NULL;
1955 size_t len;
1956 u16 ethertype;
1957 int res = -1;
1958 const char *ifname = hapd->conf->iface;
1959
1960 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1961 cmd += 7;
1962 ifname = cmd;
1963 cmd = os_strchr(cmd, ' ');
1964 if (cmd == NULL)
1965 return -1;
1966 *cmd++ = '\0';
1967 }
1968
1969 len = os_strlen(cmd);
1970 if (len & 1 || len < ETH_HLEN * 2)
1971 return -1;
1972 len /= 2;
1973
1974 buf = os_malloc(len);
1975 if (buf == NULL)
1976 return -1;
1977
1978 if (hexstr2bin(cmd, buf, len) < 0)
1979 goto done;
1980
1981 eth = (struct ether_header *) buf;
1982 ethertype = ntohs(eth->ether_type);
1983
1984 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1985 hostapd_data_test_rx, hapd, 1);
1986 if (l2 == NULL)
1987 goto done;
1988
1989 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1990 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1991done:
1992 if (l2)
1993 l2_packet_deinit(l2);
1994 os_free(buf);
1995
1996 return res < 0 ? -1 : 0;
1997}
1998
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001999
2000static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
2001{
2002#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002003 char *pos;
2004
2005 wpa_trace_fail_after = atoi(cmd);
2006 pos = os_strchr(cmd, ':');
2007 if (pos) {
2008 pos++;
2009 os_strlcpy(wpa_trace_fail_func, pos,
2010 sizeof(wpa_trace_fail_func));
2011 } else {
2012 wpa_trace_fail_after = 0;
2013 }
2014
2015 return 0;
2016#else /* WPA_TRACE_BFD */
2017 return -1;
2018#endif /* WPA_TRACE_BFD */
2019}
2020
2021
2022static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2023 char *buf, size_t buflen)
2024{
2025#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002026 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2027 wpa_trace_fail_func);
2028#else /* WPA_TRACE_BFD */
2029 return -1;
2030#endif /* WPA_TRACE_BFD */
2031}
2032
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002033
2034static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2035{
2036#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002037 char *pos;
2038
2039 wpa_trace_test_fail_after = atoi(cmd);
2040 pos = os_strchr(cmd, ':');
2041 if (pos) {
2042 pos++;
2043 os_strlcpy(wpa_trace_test_fail_func, pos,
2044 sizeof(wpa_trace_test_fail_func));
2045 } else {
2046 wpa_trace_test_fail_after = 0;
2047 }
2048
2049 return 0;
2050#else /* WPA_TRACE_BFD */
2051 return -1;
2052#endif /* WPA_TRACE_BFD */
2053}
2054
2055
2056static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2057 char *buf, size_t buflen)
2058{
2059#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002060 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2061 wpa_trace_test_fail_func);
2062#else /* WPA_TRACE_BFD */
2063 return -1;
2064#endif /* WPA_TRACE_BFD */
2065}
2066
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002067
2068static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2069{
2070 struct sta_info *sta;
2071 u8 addr[ETH_ALEN];
2072 u8 zero[WPA_TK_MAX_LEN];
2073
2074 os_memset(zero, 0, sizeof(zero));
2075
2076 if (hwaddr_aton(cmd, addr))
2077 return -1;
2078
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002079 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2080 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2081 return -1;
2082
2083 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2084
2085 /* First, use a zero key to avoid any possible duplicate key
2086 * avoidance in the driver. */
2087 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2088 hapd->last_bigtk_alg,
2089 broadcast_ether_addr,
2090 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2091 zero, hapd->last_bigtk_len,
2092 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2093 return -1;
2094
2095 /* Set the previously configured key to reset its TSC */
2096 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2097 hapd->last_bigtk_alg,
2098 broadcast_ether_addr,
2099 hapd->last_bigtk_key_idx, 0, 1, NULL,
2100 0, hapd->last_bigtk,
2101 hapd->last_bigtk_len,
2102 KEY_FLAG_GROUP_TX_DEFAULT);
2103 }
2104
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002105 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2106 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2107 return -1;
2108
2109 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2110
2111 /* First, use a zero key to avoid any possible duplicate key
2112 * avoidance in the driver. */
2113 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2114 hapd->last_igtk_alg,
2115 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002116 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2117 zero, hapd->last_igtk_len,
2118 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002119 return -1;
2120
2121 /* Set the previously configured key to reset its TSC */
2122 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2123 hapd->last_igtk_alg,
2124 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002125 hapd->last_igtk_key_idx, 0, 1, NULL,
2126 0, hapd->last_igtk,
2127 hapd->last_igtk_len,
2128 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002129 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002130
2131 if (is_broadcast_ether_addr(addr)) {
2132 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2133 return -1;
2134
2135 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2136
2137 /* First, use a zero key to avoid any possible duplicate key
2138 * avoidance in the driver. */
2139 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2140 hapd->last_gtk_alg,
2141 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002142 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2143 zero, hapd->last_gtk_len,
2144 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002145 return -1;
2146
2147 /* Set the previously configured key to reset its TSC */
2148 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2149 hapd->last_gtk_alg,
2150 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002151 hapd->last_gtk_key_idx, 0, 1, NULL,
2152 0, hapd->last_gtk,
2153 hapd->last_gtk_len,
2154 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002155 }
2156
2157 sta = ap_get_sta(hapd, addr);
2158 if (!sta)
2159 return -1;
2160
2161 if (sta->last_tk_alg == WPA_ALG_NONE)
2162 return -1;
2163
2164 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2165 MAC2STR(sta->addr));
2166
2167 /* First, use a zero key to avoid any possible duplicate key avoidance
2168 * in the driver. */
2169 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002170 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2171 zero, sta->last_tk_len,
2172 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002173 return -1;
2174
2175 /* Set the previously configured key to reset its TSC/RSC */
2176 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002177 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2178 0, sta->last_tk, sta->last_tk_len,
2179 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002180}
2181
2182
2183static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2184{
2185 u8 addr[ETH_ALEN];
2186 const char *pos = cmd;
2187 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002188 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002189 int idx, set_tx;
2190 u8 seq[6], key[WPA_TK_MAX_LEN];
2191 size_t key_len;
2192
Hai Shalomfdcde762020-04-02 11:19:20 -07002193 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002194
2195 alg = atoi(pos);
2196 pos = os_strchr(pos, ' ');
2197 if (!pos)
2198 return -1;
2199 pos++;
2200 if (hwaddr_aton(pos, addr))
2201 return -1;
2202 pos += 17;
2203 if (*pos != ' ')
2204 return -1;
2205 pos++;
2206 idx = atoi(pos);
2207 pos = os_strchr(pos, ' ');
2208 if (!pos)
2209 return -1;
2210 pos++;
2211 set_tx = atoi(pos);
2212 pos = os_strchr(pos, ' ');
2213 if (!pos)
2214 return -1;
2215 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002216 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002217 return -1;
2218 pos += 2 * 6;
2219 if (*pos != ' ')
2220 return -1;
2221 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002222 if (!os_strchr(pos, ' '))
2223 return -1;
2224 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002225 if (hexstr2bin(pos, key, key_len) < 0)
2226 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002227 pos += 2 * key_len;
2228 if (*pos != ' ')
2229 return -1;
2230
2231 pos++;
2232 key_flag = atoi(pos);
2233 pos = os_strchr(pos, ' ');
2234 if (pos)
2235 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002236
2237 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002238 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2239 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002240}
2241
2242
2243static void restore_tk(void *ctx1, void *ctx2)
2244{
2245 struct hostapd_data *hapd = ctx1;
2246 struct sta_info *sta = ctx2;
2247
2248 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2249 MAC2STR(sta->addr));
2250 /* This does not really restore the TSC properly, so this will result
2251 * in replay protection issues for now since there is no clean way of
2252 * preventing encryption of a single EAPOL frame. */
2253 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002254 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2255 sta->last_tk, sta->last_tk_len,
2256 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002257}
2258
2259
2260static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2261{
2262 struct sta_info *sta;
2263 u8 addr[ETH_ALEN];
2264 int plain = os_strstr(cmd, "plaintext") != NULL;
2265
2266 if (hwaddr_aton(cmd, addr))
2267 return -1;
2268
2269 sta = ap_get_sta(hapd, addr);
2270 if (!sta || !sta->wpa_sm)
2271 return -1;
2272
2273 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2274 plain = 0; /* no need for special processing */
2275 if (plain) {
2276 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2277 MAC2STR(sta->addr));
2278 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002279 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2280 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002281 }
2282
2283 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2284 return wpa_auth_resend_m1(sta->wpa_sm,
2285 os_strstr(cmd, "change-anonce") != NULL,
2286 plain ? restore_tk : NULL, hapd, sta);
2287}
2288
2289
2290static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2291{
2292 struct sta_info *sta;
2293 u8 addr[ETH_ALEN];
2294 int plain = os_strstr(cmd, "plaintext") != NULL;
2295
2296 if (hwaddr_aton(cmd, addr))
2297 return -1;
2298
2299 sta = ap_get_sta(hapd, addr);
2300 if (!sta || !sta->wpa_sm)
2301 return -1;
2302
2303 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2304 plain = 0; /* no need for special processing */
2305 if (plain) {
2306 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2307 MAC2STR(sta->addr));
2308 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002309 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2310 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002311 }
2312
2313 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2314 return wpa_auth_resend_m3(sta->wpa_sm,
2315 plain ? restore_tk : NULL, hapd, sta);
2316}
2317
2318
2319static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2320 const char *cmd)
2321{
2322 struct sta_info *sta;
2323 u8 addr[ETH_ALEN];
2324 int plain = os_strstr(cmd, "plaintext") != NULL;
2325
2326 if (hwaddr_aton(cmd, addr))
2327 return -1;
2328
2329 sta = ap_get_sta(hapd, addr);
2330 if (!sta || !sta->wpa_sm)
2331 return -1;
2332
2333 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2334 plain = 0; /* no need for special processing */
2335 if (plain) {
2336 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2337 MAC2STR(sta->addr));
2338 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002339 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2340 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002341 }
2342
2343 wpa_printf(MSG_INFO,
2344 "TESTING: Send group M1 for the same GTK and zero RSC to "
2345 MACSTR, MAC2STR(sta->addr));
2346 return wpa_auth_resend_group_m1(sta->wpa_sm,
2347 plain ? restore_tk : NULL, hapd, sta);
2348}
2349
Hai Shalomfdcde762020-04-02 11:19:20 -07002350
Hai Shaloma20dcd72022-02-04 13:43:00 -08002351static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2352{
2353 struct sta_info *sta;
2354 u8 addr[ETH_ALEN];
2355
2356 if (hwaddr_aton(cmd, addr))
2357 return -1;
2358
2359 sta = ap_get_sta(hapd, addr);
2360 if (!sta || !sta->wpa_sm)
2361 return -1;
2362
2363 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2364}
2365
2366
Hai Shalom899fcc72020-10-19 14:38:18 -07002367static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2368 char *buf, size_t buflen)
2369{
2370 struct rsn_pmksa_cache_entry *pmksa;
2371
2372 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2373 if (!pmksa)
2374 return -1;
2375
2376 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2377}
2378
2379
Hai Shalomfdcde762020-04-02 11:19:20 -07002380static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2381 char *buf, size_t buflen)
2382{
2383 struct sta_info *sta;
2384 u8 addr[ETH_ALEN];
2385 const u8 *pmk;
2386 int pmk_len;
2387
2388 if (hwaddr_aton(cmd, addr))
2389 return -1;
2390
2391 sta = ap_get_sta(hapd, addr);
2392 if (!sta || !sta->wpa_sm) {
2393 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2394 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002395 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002396 }
2397 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002398 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002399 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2400 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002401 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002402 }
2403
2404 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2405}
2406
Hai Shaloma20dcd72022-02-04 13:43:00 -08002407
2408static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2409 const char *cmd)
2410{
2411 u16 type;
2412 char *pos, *end;
2413 u8 match[10];
2414 size_t match_len;
2415 bool multicast = false;
2416
2417 type = strtol(cmd, &pos, 16);
2418 if (*pos != ' ')
2419 return -1;
2420 pos++;
2421 end = os_strchr(pos, ' ');
2422 if (end) {
2423 match_len = end - pos;
2424 multicast = os_strstr(end, "multicast") != NULL;
2425 } else {
2426 match_len = os_strlen(pos) / 2;
2427 }
2428 if (hexstr2bin(pos, match, match_len))
2429 return -1;
2430
2431 return hostapd_drv_register_frame(hapd, type, match, match_len,
2432 multicast);
2433}
2434
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002435#endif /* CONFIG_TESTING_OPTIONS */
2436
2437
Hai Shalomb755a2a2020-04-23 21:49:02 -07002438#ifdef NEED_AP_MLME
Sunil Ravi036cec52023-03-29 11:35:17 -07002439static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params,
2440 u16 punct_bitmap)
Hai Shalomfdcde762020-04-02 11:19:20 -07002441{
Sunil Ravi036cec52023-03-29 11:35:17 -07002442 u32 start_freq;
2443
2444 if (is_6ghz_freq(params->freq)) {
2445 const int bw_idx[] = { 20, 40, 80, 160, 320 };
2446 int idx, bw;
2447
2448 /* The 6 GHz band requires HE to be enabled. */
2449 params->he_enabled = 1;
2450
2451 if (params->center_freq1) {
2452 if (params->freq == 5935)
2453 idx = (params->center_freq1 - 5925) / 5;
2454 else
2455 idx = (params->center_freq1 - 5950) / 5;
2456
2457 bw = center_idx_to_bw_6ghz(idx);
2458 if (bw < 0 || bw > (int) ARRAY_SIZE(bw_idx) ||
2459 bw_idx[bw] != params->bandwidth)
2460 return -1;
2461 }
2462 }
2463
Hai Shalomfdcde762020-04-02 11:19:20 -07002464 switch (params->bandwidth) {
2465 case 0:
2466 /* bandwidth not specified: use 20 MHz by default */
2467 /* fall-through */
2468 case 20:
2469 if (params->center_freq1 &&
2470 params->center_freq1 != params->freq)
2471 return -1;
2472
2473 if (params->center_freq2 || params->sec_channel_offset)
2474 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07002475
2476 if (punct_bitmap)
2477 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002478 break;
2479 case 40:
2480 if (params->center_freq2 || !params->sec_channel_offset)
2481 return -1;
2482
Sunil Ravi036cec52023-03-29 11:35:17 -07002483 if (punct_bitmap)
2484 return -1;
2485
Hai Shalomfdcde762020-04-02 11:19:20 -07002486 if (!params->center_freq1)
2487 break;
2488 switch (params->sec_channel_offset) {
2489 case 1:
2490 if (params->freq + 10 != params->center_freq1)
2491 return -1;
2492 break;
2493 case -1:
2494 if (params->freq - 10 != params->center_freq1)
2495 return -1;
2496 break;
2497 default:
2498 return -1;
2499 }
2500 break;
2501 case 80:
2502 if (!params->center_freq1 || !params->sec_channel_offset)
2503 return 1;
2504
2505 switch (params->sec_channel_offset) {
2506 case 1:
2507 if (params->freq - 10 != params->center_freq1 &&
2508 params->freq + 30 != params->center_freq1)
2509 return 1;
2510 break;
2511 case -1:
2512 if (params->freq + 10 != params->center_freq1 &&
2513 params->freq - 30 != params->center_freq1)
2514 return -1;
2515 break;
2516 default:
2517 return -1;
2518 }
2519
Sunil Ravi036cec52023-03-29 11:35:17 -07002520 if (params->center_freq2 && punct_bitmap)
2521 return -1;
2522
Hai Shalomfdcde762020-04-02 11:19:20 -07002523 /* Adjacent and overlapped are not allowed for 80+80 */
2524 if (params->center_freq2 &&
2525 params->center_freq1 - params->center_freq2 <= 80 &&
2526 params->center_freq2 - params->center_freq1 <= 80)
2527 return 1;
2528 break;
2529 case 160:
2530 if (!params->center_freq1 || params->center_freq2 ||
2531 !params->sec_channel_offset)
2532 return -1;
2533
2534 switch (params->sec_channel_offset) {
2535 case 1:
2536 if (params->freq + 70 != params->center_freq1 &&
2537 params->freq + 30 != params->center_freq1 &&
2538 params->freq - 10 != params->center_freq1 &&
2539 params->freq - 50 != params->center_freq1)
2540 return -1;
2541 break;
2542 case -1:
2543 if (params->freq + 50 != params->center_freq1 &&
2544 params->freq + 10 != params->center_freq1 &&
2545 params->freq - 30 != params->center_freq1 &&
2546 params->freq - 70 != params->center_freq1)
2547 return -1;
2548 break;
2549 default:
2550 return -1;
2551 }
2552 break;
2553 default:
2554 return -1;
2555 }
2556
Sunil Ravi036cec52023-03-29 11:35:17 -07002557 if (!punct_bitmap)
2558 return 0;
2559
2560 if (!params->eht_enabled) {
2561 wpa_printf(MSG_ERROR,
2562 "Preamble puncturing supported only in EHT");
2563 return -1;
2564 }
2565
2566 if (params->freq >= 2412 && params->freq <= 2484) {
2567 wpa_printf(MSG_ERROR,
2568 "Preamble puncturing is not supported in 2.4 GHz");
2569 return -1;
2570 }
2571
2572 start_freq = params->center_freq1 - (params->bandwidth / 2);
2573 if (!is_punct_bitmap_valid(params->bandwidth,
2574 (params->freq - start_freq) / 20,
2575 punct_bitmap)) {
2576 wpa_printf(MSG_ERROR, "Invalid preamble puncturing bitmap");
2577 return -1;
2578 }
2579
Hai Shalomfdcde762020-04-02 11:19:20 -07002580 return 0;
2581}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002582#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002583
2584
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002585static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2586 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002587{
2588#ifdef NEED_AP_MLME
2589 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002590 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002591 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002592 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002593 int bandwidth;
2594 u8 chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002595
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002596 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002597 if (ret)
2598 return ret;
2599
Sunil Ravi036cec52023-03-29 11:35:17 -07002600 ret = hostapd_ctrl_check_freq_params(&settings.freq_params,
2601 settings.punct_bitmap);
Hai Shalomfdcde762020-04-02 11:19:20 -07002602 if (ret) {
2603 wpa_printf(MSG_INFO,
2604 "chanswitch: invalid frequency settings provided");
2605 return ret;
2606 }
2607
2608 switch (settings.freq_params.bandwidth) {
2609 case 40:
2610 bandwidth = CHAN_WIDTH_40;
2611 break;
2612 case 80:
2613 if (settings.freq_params.center_freq2)
2614 bandwidth = CHAN_WIDTH_80P80;
2615 else
2616 bandwidth = CHAN_WIDTH_80;
2617 break;
2618 case 160:
2619 bandwidth = CHAN_WIDTH_160;
2620 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002621 case 320:
2622 bandwidth = CHAN_WIDTH_320;
2623 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002624 default:
2625 bandwidth = CHAN_WIDTH_20;
2626 break;
2627 }
2628
2629 if (settings.freq_params.center_freq1)
2630 dfs_range += hostapd_is_dfs_overlap(
2631 iface, bandwidth, settings.freq_params.center_freq1);
2632 else
2633 dfs_range += hostapd_is_dfs_overlap(
2634 iface, bandwidth, settings.freq_params.freq);
2635
2636 if (settings.freq_params.center_freq2)
2637 dfs_range += hostapd_is_dfs_overlap(
2638 iface, bandwidth, settings.freq_params.center_freq2);
2639
2640 if (dfs_range) {
2641 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2642 if (ret == NUM_HOSTAPD_MODES) {
2643 wpa_printf(MSG_ERROR,
2644 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2645 settings.freq_params.freq,
2646 settings.freq_params.sec_channel_offset,
2647 settings.freq_params.bandwidth);
2648 return -1;
2649 }
2650
2651 settings.freq_params.channel = chan;
2652
2653 wpa_printf(MSG_DEBUG,
2654 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2655 settings.freq_params.channel,
2656 settings.freq_params.freq,
2657 settings.freq_params.sec_channel_offset,
2658 settings.freq_params.bandwidth,
2659 settings.freq_params.center_freq1);
2660
2661 /* Perform CAC and switch channel */
2662 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2663 return 0;
2664 }
2665
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002666 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002667
Sunil Ravia04bd252022-05-02 22:54:18 -07002668 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002669 hostapd_chan_switch_config(iface->bss[i],
2670 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002671
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002672 ret = hostapd_switch_channel(iface->bss[i], &settings);
2673 if (ret) {
2674 /* FIX: What do we do if CSA fails in the middle of
2675 * submitting multi-BSS CSA requests? */
2676 return ret;
2677 }
2678 }
2679
2680 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002681#else /* NEED_AP_MLME */
2682 return -1;
2683#endif /* NEED_AP_MLME */
2684}
2685
2686
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002687static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2688 int reply_size, const char *param)
2689{
2690#ifdef RADIUS_SERVER
2691 if (os_strcmp(param, "radius_server") == 0) {
2692 return radius_server_get_mib(hapd->radius_srv, reply,
2693 reply_size);
2694 }
2695#endif /* RADIUS_SERVER */
2696 return -1;
2697}
2698
2699
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002700static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2701 char *buf, size_t buflen)
2702{
2703 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08002704 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002705 u8 *data = NULL;
2706 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08002707 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002708 struct wpabuf *reply;
2709 size_t data_len = 0;
2710
Hai Shalom60840252021-02-19 19:02:11 -08002711 /**
2712 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2713 * [nested=<0|1>]
2714 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002715 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002716 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002717 return -EINVAL;
2718
2719 subcmd = strtoul(pos, &pos, 10);
2720
2721 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002722 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002723 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08002724
2725 temp = os_strchr(pos, ' ');
2726 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002727 }
2728
2729 if (data_len) {
2730 data_len /= 2;
2731 data = os_malloc(data_len);
2732 if (!data)
2733 return -ENOBUFS;
2734
2735 if (hexstr2bin(pos, data, data_len)) {
2736 wpa_printf(MSG_DEBUG,
2737 "Vendor command: wrong parameter format");
2738 os_free(data);
2739 return -EINVAL;
2740 }
2741 }
2742
Hai Shalom60840252021-02-19 19:02:11 -08002743 pos = os_strstr(cmd, "nested=");
2744 if (pos)
2745 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2746 NESTED_ATTR_NOT_USED;
2747
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002748 reply = wpabuf_alloc((buflen - 1) / 2);
2749 if (!reply) {
2750 os_free(data);
2751 return -ENOBUFS;
2752 }
2753
2754 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08002755 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002756
2757 if (ret == 0)
2758 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2759 wpabuf_len(reply));
2760
2761 wpabuf_free(reply);
2762 os_free(data);
2763
2764 return ret;
2765}
2766
2767
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002768static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2769 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002771 u8 addr[ETH_ALEN];
2772 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002774 if (hwaddr_aton(cmd, addr))
2775 return -1;
2776
2777 sta = ap_get_sta(hapd, addr);
2778 if (!sta || !sta->eapol_sm)
2779 return -1;
2780
2781 eapol_auth_reauthenticate(sta->eapol_sm);
2782 return 0;
2783}
2784
2785
2786static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2787{
2788 u8 addr[ETH_ALEN];
2789 struct sta_info *sta;
2790 char *pos = cmd, *param;
2791
2792 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2793 return -1;
2794 pos += 18;
2795 param = pos;
2796 pos = os_strchr(pos, ' ');
2797 if (!pos)
2798 return -1;
2799 *pos++ = '\0';
2800
2801 sta = ap_get_sta(hapd, addr);
2802 if (!sta || !sta->eapol_sm)
2803 return -1;
2804
2805 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2806}
2807
2808
2809static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2810 char *buf, size_t buflen)
2811{
2812 char *pos, *end, *stamp;
2813 int ret;
2814
2815 /* cmd: "LOG_LEVEL [<level>]" */
2816 if (*cmd == '\0') {
2817 pos = buf;
2818 end = buf + buflen;
2819 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2820 "Timestamp: %d\n",
2821 debug_level_str(wpa_debug_level),
2822 wpa_debug_timestamp);
2823 if (os_snprintf_error(end - pos, ret))
2824 ret = 0;
2825
2826 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002829 while (*cmd == ' ')
2830 cmd++;
2831
2832 stamp = os_strchr(cmd, ' ');
2833 if (stamp) {
2834 *stamp++ = '\0';
2835 while (*stamp == ' ') {
2836 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002837 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838 }
2839
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002840 if (os_strlen(cmd)) {
2841 int level = str_to_debug_level(cmd);
2842 if (level < 0)
2843 return -1;
2844 wpa_debug_level = level;
2845 }
2846
2847 if (stamp && os_strlen(stamp))
2848 wpa_debug_timestamp = atoi(stamp);
2849
2850 os_memcpy(buf, "OK\n", 3);
2851 return 3;
2852}
2853
2854
2855#ifdef NEED_AP_MLME
2856static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2857 char *buf, size_t buflen)
2858{
2859 struct hostapd_iface *iface = hapd->iface;
2860 char *pos, *end;
2861 struct hostapd_sta_info *info;
2862 struct os_reltime now;
2863
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002864 if (!iface->num_sta_seen)
2865 return 0;
2866
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002867 sta_track_expire(iface, 0);
2868
2869 pos = buf;
2870 end = buf + buflen;
2871
2872 os_get_reltime(&now);
2873 dl_list_for_each_reverse(info, &iface->sta_seen,
2874 struct hostapd_sta_info, list) {
2875 struct os_reltime age;
2876 int ret;
2877
2878 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002879 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2880 MAC2STR(info->addr), (unsigned int) age.sec,
2881 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002882 if (os_snprintf_error(end - pos, ret))
2883 break;
2884 pos += ret;
2885 }
2886
2887 return pos - buf;
2888}
2889#endif /* NEED_AP_MLME */
2890
2891
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002892static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2893 const char *cmd)
2894{
2895 u8 addr[ETH_ALEN];
2896
2897 if (hwaddr_aton(cmd, addr)) {
2898 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2899 return -1;
2900 }
2901
2902 return hostapd_send_lci_req(hapd, addr);
2903}
2904
2905
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002906static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002907{
2908 u8 addr[ETH_ALEN];
2909 char *token, *context = NULL;
2910 int random_interval, min_ap;
2911 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2912 unsigned int n_responders;
2913
2914 token = str_token(cmd, " ", &context);
2915 if (!token || hwaddr_aton(token, addr)) {
2916 wpa_printf(MSG_INFO,
2917 "CTRL: REQ_RANGE - Bad destination address");
2918 return -1;
2919 }
2920
2921 token = str_token(cmd, " ", &context);
2922 if (!token)
2923 return -1;
2924
2925 random_interval = atoi(token);
2926 if (random_interval < 0 || random_interval > 0xffff)
2927 return -1;
2928
2929 token = str_token(cmd, " ", &context);
2930 if (!token)
2931 return -1;
2932
2933 min_ap = atoi(token);
2934 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2935 return -1;
2936
2937 n_responders = 0;
2938 while ((token = str_token(cmd, " ", &context))) {
2939 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2940 wpa_printf(MSG_INFO,
2941 "CTRL: REQ_RANGE: Too many responders");
2942 return -1;
2943 }
2944
2945 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2946 wpa_printf(MSG_INFO,
2947 "CTRL: REQ_RANGE: Bad responder address");
2948 return -1;
2949 }
2950
2951 n_responders++;
2952 }
2953
2954 if (!n_responders) {
2955 wpa_printf(MSG_INFO,
2956 "CTRL: REQ_RANGE - No FTM responder address");
2957 return -1;
2958 }
2959
2960 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2961 responders, n_responders);
2962}
2963
2964
Dmitry Shmidt29333592017-01-09 12:27:11 -08002965static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2966 const char *cmd, char *reply,
2967 size_t reply_size)
2968{
2969 u8 addr[ETH_ALEN];
2970 const char *pos;
2971 struct wpabuf *req;
2972 int ret;
2973 u8 req_mode = 0;
2974
2975 if (hwaddr_aton(cmd, addr))
2976 return -1;
2977 pos = os_strchr(cmd, ' ');
2978 if (!pos)
2979 return -1;
2980 pos++;
2981 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2982 int val = hex2byte(pos + 9);
2983
2984 if (val < 0)
2985 return -1;
2986 req_mode = val;
2987 pos += 11;
2988 pos = os_strchr(pos, ' ');
2989 if (!pos)
2990 return -1;
2991 pos++;
2992 }
2993 req = wpabuf_parse_bin(pos);
2994 if (!req)
2995 return -1;
2996
2997 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2998 wpabuf_free(req);
2999 if (ret >= 0)
3000 ret = os_snprintf(reply, reply_size, "%d", ret);
3001 return ret;
3002}
3003
3004
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003005static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3006 char *buf, size_t buflen)
3007{
3008 if (!(hapd->conf->radio_measurements[0] &
3009 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3010 wpa_printf(MSG_ERROR,
3011 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3012 return -1;
3013 }
3014
3015 return hostapd_neighbor_show(hapd, buf, buflen);
3016}
3017
3018
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003019static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3020{
3021 struct wpa_ssid_value ssid;
3022 u8 bssid[ETH_ALEN];
3023 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003024 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003025 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003026 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003027 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003028
3029 if (!(hapd->conf->radio_measurements[0] &
3030 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3031 wpa_printf(MSG_ERROR,
3032 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3033 return -1;
3034 }
3035
3036 if (hwaddr_aton(buf, bssid)) {
3037 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3038 return -1;
3039 }
3040
3041 tmp = os_strstr(buf, "ssid=");
3042 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3043 wpa_printf(MSG_ERROR,
3044 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3045 return -1;
3046 }
3047 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3048 if (!buf)
3049 return -1;
3050
3051 tmp = os_strstr(buf, "nr=");
3052 if (!tmp) {
3053 wpa_printf(MSG_ERROR,
3054 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3055 return -1;
3056 }
3057
3058 buf = os_strchr(tmp, ' ');
3059 if (buf)
3060 *buf++ = '\0';
3061
3062 nr = wpabuf_parse_bin(tmp + 3);
3063 if (!nr) {
3064 wpa_printf(MSG_ERROR,
3065 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3066 return -1;
3067 }
3068
3069 if (!buf)
3070 goto set;
3071
3072 tmp = os_strstr(buf, "lci=");
3073 if (tmp) {
3074 buf = os_strchr(tmp, ' ');
3075 if (buf)
3076 *buf++ = '\0';
3077 lci = wpabuf_parse_bin(tmp + 4);
3078 if (!lci) {
3079 wpa_printf(MSG_ERROR,
3080 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003081 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003082 }
3083 }
3084
3085 if (!buf)
3086 goto set;
3087
3088 tmp = os_strstr(buf, "civic=");
3089 if (tmp) {
3090 buf = os_strchr(tmp, ' ');
3091 if (buf)
3092 *buf++ = '\0';
3093 civic = wpabuf_parse_bin(tmp + 6);
3094 if (!civic) {
3095 wpa_printf(MSG_ERROR,
3096 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003097 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003098 }
3099 }
3100
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003101 if (!buf)
3102 goto set;
3103
3104 if (os_strstr(buf, "stat"))
3105 stationary = 1;
3106
Hai Shaloma20dcd72022-02-04 13:43:00 -08003107 tmp = os_strstr(buf, "bss_parameter=");
3108 if (tmp) {
3109 bss_parameters = atoi(tmp + 14);
3110 if (bss_parameters < 0 || bss_parameters > 0xff) {
3111 wpa_printf(MSG_ERROR,
3112 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3113 goto fail;
3114 }
3115 }
3116
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003117set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003118 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003119 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003120
Hai Shaloma20dcd72022-02-04 13:43:00 -08003121fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003122 wpabuf_free(nr);
3123 wpabuf_free(lci);
3124 wpabuf_free(civic);
3125
3126 return ret;
3127}
3128
3129
3130static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3131 char *buf)
3132{
3133 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003134 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003135 u8 bssid[ETH_ALEN];
3136 char *tmp;
3137
3138 if (hwaddr_aton(buf, bssid)) {
3139 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3140 return -1;
3141 }
3142
3143 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003144 if (tmp) {
3145 ssidp = &ssid;
3146 if (ssid_parse(tmp + 5, &ssid)) {
3147 wpa_printf(MSG_ERROR,
3148 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3149 return -1;
3150 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003151 }
3152
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003153 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003154}
3155
3156
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003157static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3158 size_t buflen)
3159{
3160 int ret, i;
3161 char *pos, *end;
3162
3163 ret = os_snprintf(buf, buflen, "%016llX:\n",
3164 (long long unsigned) iface->drv_flags);
3165 if (os_snprintf_error(buflen, ret))
3166 return -1;
3167
3168 pos = buf + ret;
3169 end = buf + buflen;
3170
3171 for (i = 0; i < 64; i++) {
3172 if (iface->drv_flags & (1LLU << i)) {
3173 ret = os_snprintf(pos, end - pos, "%s\n",
3174 driver_flag_to_string(1LLU << i));
3175 if (os_snprintf_error(end - pos, ret))
3176 return -1;
3177 pos += ret;
3178 }
3179 }
3180
3181 return pos - buf;
3182}
3183
3184
Hai Shalomb755a2a2020-04-23 21:49:02 -07003185static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3186 size_t buflen)
3187{
3188 int ret, i;
3189 char *pos, *end;
3190
3191 ret = os_snprintf(buf, buflen, "%016llX:\n",
3192 (long long unsigned) iface->drv_flags2);
3193 if (os_snprintf_error(buflen, ret))
3194 return -1;
3195
3196 pos = buf + ret;
3197 end = buf + buflen;
3198
3199 for (i = 0; i < 64; i++) {
3200 if (iface->drv_flags2 & (1LLU << i)) {
3201 ret = os_snprintf(pos, end - pos, "%s\n",
3202 driver_flag2_to_string(1LLU << i));
3203 if (os_snprintf_error(end - pos, ret))
3204 return -1;
3205 pos += ret;
3206 }
3207 }
3208
3209 return pos - buf;
3210}
3211
3212
Hai Shalom021b0b52019-04-10 11:17:58 -07003213static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3214 const char *field, char *buf,
3215 size_t buflen)
3216{
3217 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3218
3219#ifdef CONFIG_DPP
3220 if (os_strcmp(field, "dpp") == 0) {
3221 int res;
3222
Hai Shaloma20dcd72022-02-04 13:43:00 -08003223#ifdef CONFIG_DPP3
3224 res = os_snprintf(buf, buflen, "DPP=3");
3225#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003226 res = os_snprintf(buf, buflen, "DPP=2");
3227#else /* CONFIG_DPP2 */
3228 res = os_snprintf(buf, buflen, "DPP=1");
3229#endif /* CONFIG_DPP2 */
3230 if (os_snprintf_error(buflen, res))
3231 return -1;
3232 return res;
3233 }
3234#endif /* CONFIG_DPP */
3235
3236 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3237 field);
3238
3239 return -1;
3240}
3241
3242
Hai Shaloma20dcd72022-02-04 13:43:00 -08003243#ifdef ANDROID
3244static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3245 char *buf, size_t buflen)
3246{
3247 int ret;
3248
3249 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3250 if (ret == 0) {
3251 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3252 if (os_snprintf_error(buflen, ret))
3253 ret = -1;
3254 }
3255 return ret;
3256}
3257#endif /* ANDROID */
3258
3259
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003260static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3261 char *buf, char *reply,
3262 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003263 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003264 socklen_t fromlen)
3265{
3266 int reply_len, res;
3267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 os_memcpy(reply, "OK\n", 3);
3269 reply_len = 3;
3270
3271 if (os_strcmp(buf, "PING") == 0) {
3272 os_memcpy(reply, "PONG\n", 5);
3273 reply_len = 5;
3274 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3275 if (wpa_debug_reopen_file() < 0)
3276 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003277 } else if (os_strcmp(buf, "CLOSE_LOG") == 0) {
3278 wpa_debug_stop_log();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003279 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3280 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003281 } else if (os_strcmp(buf, "STATUS") == 0) {
3282 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3283 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003284 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3285 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286 } else if (os_strcmp(buf, "MIB") == 0) {
3287 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3288 if (reply_len >= 0) {
3289 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3290 reply_size - reply_len);
3291 if (res < 0)
3292 reply_len = -1;
3293 else
3294 reply_len += res;
3295 }
3296 if (reply_len >= 0) {
3297 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3298 reply_size - reply_len);
3299 if (res < 0)
3300 reply_len = -1;
3301 else
3302 reply_len += res;
3303 }
3304#ifndef CONFIG_NO_RADIUS
3305 if (reply_len >= 0) {
3306 res = radius_client_get_mib(hapd->radius,
3307 reply + reply_len,
3308 reply_size - reply_len);
3309 if (res < 0)
3310 reply_len = -1;
3311 else
3312 reply_len += res;
3313 }
3314#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003315 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3316 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3317 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3319 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3320 reply_size);
3321 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3322 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3323 reply_size);
3324 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3325 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3326 reply_size);
3327 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003328 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3329 reply_len = -1;
3330 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3331 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332 reply_len = -1;
3333 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003334 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003335 reply_len = -1;
3336 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003337 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338 buf + 6))
3339 reply_len = -1;
3340 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3341 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3342 reply_len = -1;
3343 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3344 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3345 reply_len = -1;
3346 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3347 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3348 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003349#ifdef CONFIG_TAXONOMY
3350 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3351 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3352 reply, reply_size);
3353#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003354 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3355 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3356 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003357 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3358 if (hostapd_ctrl_iface_stop_ap(hapd))
3359 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003360#ifdef NEED_AP_MLME
3361 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3362 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3363 reply_len = -1;
3364#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003365#ifdef CONFIG_WPS
3366 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3367 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3368 reply_len = -1;
3369 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3370 reply_len = hostapd_ctrl_iface_wps_check_pin(
3371 hapd, buf + 14, reply, reply_size);
3372 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3373 if (hostapd_wps_button_pushed(hapd, NULL))
3374 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003375 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3376 if (hostapd_wps_cancel(hapd))
3377 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3379 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3380 reply, reply_size);
3381 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3382 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3383 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003384 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3385 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3386 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003387#ifdef CONFIG_WPS_NFC
3388 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3389 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3390 reply_len = -1;
3391 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3392 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3393 hapd, buf + 21, reply, reply_size);
3394 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3395 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3396 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003397 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3398 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3399 hapd, buf + 21, reply, reply_size);
3400 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3401 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3402 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003403#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07003405#ifdef CONFIG_INTERWORKING
3406 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3407 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3408 reply_len = -1;
3409 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3410 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3411 reply_len = -1;
3412#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003413#ifdef CONFIG_HS20
3414 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3415 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3416 reply_len = -1;
3417 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3418 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3419 reply_len = -1;
3420#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003421#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003422 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3423 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3424 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003425 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3426 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3427 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003428 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3429 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3430 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003431 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3432 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3433 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003434#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003435 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3436 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3437 reply_size);
3438 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3439 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3440 reply_len = -1;
3441 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3442 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3443 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003444 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3445 if (hostapd_ctrl_iface_enable(hapd->iface))
3446 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003447 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3448 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3449 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003450 } else if (os_strcmp(buf, "RELOAD_BSS") == 0) {
3451 if (hostapd_ctrl_iface_reload_bss(hapd))
3452 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003453 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3454 if (hostapd_ctrl_iface_reload(hapd->iface))
3455 reply_len = -1;
3456 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3457 if (hostapd_ctrl_iface_disable(hapd->iface))
3458 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003459 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3460 if (ieee802_11_set_beacon(hapd))
3461 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003462#ifdef CONFIG_TESTING_OPTIONS
3463 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3464 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3465 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003466 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3467 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3468 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003469 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3470 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3471 buf + 23) < 0)
3472 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003473 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3474 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3475 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003476 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3477 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3478 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003479 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
3480 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
3481 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003482 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3483 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3484 reply_len = -1;
3485 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3486 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3487 reply_len = -1;
3488 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3489 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3490 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003491 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3492 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3493 reply_len = -1;
3494 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3495 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3496 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003497 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3498 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3499 reply_len = -1;
3500 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3501 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003502 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3503 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3504 reply_len = -1;
3505 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3506 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3507 reply_len = -1;
3508 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3509 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3510 reply_len = -1;
3511 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3512 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3513 reply_len = -1;
3514 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3515 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3516 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003517 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
3518 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
3519 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003520 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3521 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3522 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003523 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3524 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3525 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003526 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
3527 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
3528 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003529#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003530 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003531 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003532 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003533 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3534 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3535 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003536 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3537 ieee802_1x_erp_flush(hapd);
3538#ifdef RADIUS_SERVER
3539 radius_server_erp_flush(hapd->radius_srv);
3540#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003541 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3542 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3543 reply_len = -1;
3544 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3545 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3546 reply_len = -1;
3547 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3548 reply_len = hostapd_ctrl_iface_log_level(
3549 hapd, buf + 9, reply, reply_size);
3550#ifdef NEED_AP_MLME
3551 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3552 reply_len = hostapd_ctrl_iface_track_sta_list(
3553 hapd, reply, reply_size);
3554#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07003555 } else if (os_strcmp(buf, "PMKSA") == 0) {
3556 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3557 reply_size);
3558 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3559 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003560 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3561 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3562 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003563 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3564 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3565 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003566 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3567 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3568 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003569 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3570 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3571 reply_len = -1;
3572 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3573 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3574 reply_len = -1;
3575 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3576 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3577 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003578 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3579 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3580 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003581 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3582 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3583 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07003584 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
3585 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
3586 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003587 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3588 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003589 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3590 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003591 if (hostapd_ctrl_iface_acl_add_mac(
3592 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003593 &hapd->conf->num_accept_mac, buf + 19) ||
3594 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003595 reply_len = -1;
3596 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003597 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003598 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003599 &hapd->conf->num_accept_mac, buf + 19) ||
3600 hostapd_set_acl(hapd) ||
3601 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07003602 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003603 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3604 reply_len = hostapd_ctrl_iface_acl_show_mac(
3605 hapd->conf->accept_mac,
3606 hapd->conf->num_accept_mac, reply, reply_size);
3607 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3608 hostapd_ctrl_iface_acl_clear_list(
3609 &hapd->conf->accept_mac,
3610 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003611 if (hostapd_set_acl(hapd) ||
3612 hostapd_disassoc_accept_mac(hapd))
3613 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07003614 } else {
3615 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003616 }
3617 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3618 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003619 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003620 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003621 &hapd->conf->num_deny_mac, buf + 17) ||
3622 hostapd_set_acl(hapd) ||
3623 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003624 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003625 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003626 if (hostapd_ctrl_iface_acl_del_mac(
3627 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003628 &hapd->conf->num_deny_mac, buf + 17) ||
3629 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003630 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003631 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3632 reply_len = hostapd_ctrl_iface_acl_show_mac(
3633 hapd->conf->deny_mac,
3634 hapd->conf->num_deny_mac, reply, reply_size);
3635 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3636 hostapd_ctrl_iface_acl_clear_list(
3637 &hapd->conf->deny_mac,
3638 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003639 if (hostapd_set_acl(hapd))
3640 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07003641 } else {
3642 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003643 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003644#ifdef CONFIG_DPP
3645 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3646 res = hostapd_dpp_qr_code(hapd, buf + 12);
3647 if (res < 0) {
3648 reply_len = -1;
3649 } else {
3650 reply_len = os_snprintf(reply, reply_size, "%d", res);
3651 if (os_snprintf_error(reply_size, reply_len))
3652 reply_len = -1;
3653 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003654 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3655 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3656 if (res < 0) {
3657 reply_len = -1;
3658 } else {
3659 reply_len = os_snprintf(reply, reply_size, "%d", res);
3660 if (os_snprintf_error(reply_size, reply_len))
3661 reply_len = -1;
3662 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003663 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3664 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3665 if (res < 0) {
3666 reply_len = -1;
3667 } else {
3668 reply_len = os_snprintf(reply, reply_size, "%d", res);
3669 if (os_snprintf_error(reply_size, reply_len))
3670 reply_len = -1;
3671 }
3672 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3673 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3674 if (res < 0) {
3675 reply_len = -1;
3676 } else {
3677 reply_len = os_snprintf(reply, reply_size, "%d", res);
3678 if (os_snprintf_error(reply_size, reply_len))
3679 reply_len = -1;
3680 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003681 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003682 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003683 if (res < 0) {
3684 reply_len = -1;
3685 } else {
3686 reply_len = os_snprintf(reply, reply_size, "%d", res);
3687 if (os_snprintf_error(reply_size, reply_len))
3688 reply_len = -1;
3689 }
3690 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003691 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3692 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003693 reply_len = -1;
3694 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3695 const char *uri;
3696
Hai Shalom021b0b52019-04-10 11:17:58 -07003697 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3698 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003699 if (!uri) {
3700 reply_len = -1;
3701 } else {
3702 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3703 if (os_snprintf_error(reply_size, reply_len))
3704 reply_len = -1;
3705 }
3706 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003707 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3708 atoi(buf + 19),
3709 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07003710 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3711 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3712 atoi(buf + 18),
3713 os_strchr(buf + 18, ' ')) < 0)
3714 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003715 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3716 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3717 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003718 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3719 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3720 reply_len = -1;
3721 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3722 hostapd_dpp_stop(hapd);
3723 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003724 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003725 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3726 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003727 if (res < 0) {
3728 reply_len = -1;
3729 } else {
3730 reply_len = os_snprintf(reply, reply_size, "%d", res);
3731 if (os_snprintf_error(reply_size, reply_len))
3732 reply_len = -1;
3733 }
Sunil Ravia04bd252022-05-02 22:54:18 -07003734 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
3735 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
3736 buf + 20) < 0)
3737 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003738 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003739 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3740 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003741 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003742 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003743 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003744 reply_len = -1;
3745 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003746 reply_len = dpp_configurator_get_key_id(
3747 hapd->iface->interfaces->dpp,
3748 atoi(buf + 25),
3749 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003750 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3751 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3752 if (res < 0) {
3753 reply_len = -1;
3754 } else {
3755 reply_len = os_snprintf(reply, reply_size, "%d", res);
3756 if (os_snprintf_error(reply_size, reply_len))
3757 reply_len = -1;
3758 }
3759 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3760 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3761 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003762#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07003763 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
3764 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
3765 reply_len = -1;
3766 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
3767 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
3768 reply_len = -1;
3769 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
3770 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003771 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
3772 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
3773 reply_len = -1;
3774 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
3775 hostapd_dpp_chirp_stop(hapd);
Sunil Ravi89eba102022-09-13 21:04:37 -07003776 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
3777 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
3778 reply_len = -1;
3779 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
3780 hostapd_dpp_remove_controller(hapd, buf + 28);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003781#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07003782#ifdef CONFIG_DPP3
3783 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
3784 if (hostapd_dpp_push_button(hapd, NULL) < 0)
3785 reply_len = -1;
3786 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
3787 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
3788 reply_len = -1;
3789#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003790#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003791#ifdef RADIUS_SERVER
3792 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3793 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3794 reply_len = -1;
3795#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07003796 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3797 reply_len = hostapd_ctrl_iface_get_capability(
3798 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08003799#ifdef CONFIG_PASN
3800 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
3801 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
3802#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003803#ifdef ANDROID
3804 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
3805 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
3806 reply_size);
3807#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808 } else {
3809 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3810 reply_len = 16;
3811 }
3812
3813 if (reply_len < 0) {
3814 os_memcpy(reply, "FAIL\n", 5);
3815 reply_len = 5;
3816 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003817
3818 return reply_len;
3819}
3820
3821
3822static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3823 void *sock_ctx)
3824{
3825 struct hostapd_data *hapd = eloop_ctx;
3826 char buf[4096];
3827 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003828 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003829 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003830 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003831 const int reply_size = 4096;
3832 int reply_len;
3833 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003834#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07003835 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003836#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003837
3838 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3839 (struct sockaddr *) &from, &fromlen);
3840 if (res < 0) {
3841 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3842 strerror(errno));
3843 return;
3844 }
3845 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003846
3847 reply = os_malloc(reply_size);
3848 if (reply == NULL) {
3849 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3850 fromlen) < 0) {
3851 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3852 strerror(errno));
3853 }
3854 return;
3855 }
3856
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003857#ifdef CONFIG_CTRL_IFACE_UDP
3858 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3859 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07003860 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
3861 hapd->ctrl_iface_cookie,
3862 CTRL_IFACE_COOKIE_LEN);
3863 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003864 goto done;
3865 }
3866
3867 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07003868 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003869 wpa_printf(MSG_DEBUG,
3870 "CTRL: No cookie in the request - drop request");
3871 os_free(reply);
3872 return;
3873 }
3874
Hai Shalomfdcde762020-04-02 11:19:20 -07003875 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
3876 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003877 wpa_printf(MSG_DEBUG,
3878 "CTRL: Invalid cookie in the request - drop request");
3879 os_free(reply);
3880 return;
3881 }
3882
Hai Shalomfdcde762020-04-02 11:19:20 -07003883 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003884 while (*pos == ' ')
3885 pos++;
3886#endif /* CONFIG_CTRL_IFACE_UDP */
3887
3888 if (os_strcmp(pos, "PING") == 0)
3889 level = MSG_EXCESSIVE;
3890 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3891
3892 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003893 reply, reply_size,
3894 &from, fromlen);
3895
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003896#ifdef CONFIG_CTRL_IFACE_UDP
3897done:
3898#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003899 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3900 fromlen) < 0) {
3901 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3902 strerror(errno));
3903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904 os_free(reply);
3905}
3906
3907
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003908#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3910{
3911 char *buf;
3912 size_t len;
3913
3914 if (hapd->conf->ctrl_interface == NULL)
3915 return NULL;
3916
3917 len = os_strlen(hapd->conf->ctrl_interface) +
3918 os_strlen(hapd->conf->iface) + 2;
3919 buf = os_malloc(len);
3920 if (buf == NULL)
3921 return NULL;
3922
3923 os_snprintf(buf, len, "%s/%s",
3924 hapd->conf->ctrl_interface, hapd->conf->iface);
3925 buf[len - 1] = '\0';
3926 return buf;
3927}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003928#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003929
3930
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07003931static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3932 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 const char *txt, size_t len)
3934{
3935 struct hostapd_data *hapd = ctx;
3936 if (hapd == NULL)
3937 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003938 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939}
3940
3941
3942int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3943{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003944#ifdef CONFIG_CTRL_IFACE_UDP
3945 int port = HOSTAPD_CTRL_IFACE_PORT;
3946 char p[32] = { 0 };
3947 char port_str[40], *tmp;
3948 char *pos;
3949 struct addrinfo hints = { 0 }, *res, *saveres;
3950 int n;
3951
3952 if (hapd->ctrl_sock > -1) {
3953 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3954 return 0;
3955 }
3956
3957 if (hapd->conf->ctrl_interface == NULL)
3958 return 0;
3959
3960 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3961 if (pos) {
3962 pos += 4;
3963 port = atoi(pos);
3964 if (port <= 0) {
3965 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3966 goto fail;
3967 }
3968 }
3969
3970 dl_list_init(&hapd->ctrl_dst);
3971 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003972 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003973
3974#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3975 hints.ai_flags = AI_PASSIVE;
3976#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3977
3978#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3979 hints.ai_family = AF_INET6;
3980#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3981 hints.ai_family = AF_INET;
3982#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3983 hints.ai_socktype = SOCK_DGRAM;
3984
3985try_again:
3986 os_snprintf(p, sizeof(p), "%d", port);
3987 n = getaddrinfo(NULL, p, &hints, &res);
3988 if (n) {
3989 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3990 goto fail;
3991 }
3992
3993 saveres = res;
3994 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3995 res->ai_protocol);
3996 if (hapd->ctrl_sock < 0) {
3997 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3998 goto fail;
3999 }
4000
4001 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
4002 port--;
4003 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
4004 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
4005 goto try_again;
4006 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4007 goto fail;
4008 }
4009
4010 freeaddrinfo(saveres);
4011
4012 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
4013 tmp = os_strdup(port_str);
4014 if (tmp) {
4015 os_free(hapd->conf->ctrl_interface);
4016 hapd->conf->ctrl_interface = tmp;
4017 }
4018 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
4019
4020 if (eloop_register_read_sock(hapd->ctrl_sock,
4021 hostapd_ctrl_iface_receive, hapd, NULL) <
4022 0) {
4023 hostapd_ctrl_iface_deinit(hapd);
4024 return -1;
4025 }
4026
4027 hapd->msg_ctx = hapd;
4028 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4029
4030 return 0;
4031
4032fail:
4033 if (hapd->ctrl_sock >= 0)
4034 close(hapd->ctrl_sock);
4035 return -1;
4036#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004037 struct sockaddr_un addr;
4038 int s = -1;
4039 char *fname = NULL;
4040
Dmitry Shmidt04949592012-07-19 12:16:46 -07004041 if (hapd->ctrl_sock > -1) {
4042 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4043 return 0;
4044 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004046 dl_list_init(&hapd->ctrl_dst);
4047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004048 if (hapd->conf->ctrl_interface == NULL)
4049 return 0;
4050
4051 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4052 if (errno == EEXIST) {
4053 wpa_printf(MSG_DEBUG, "Using existing control "
4054 "interface directory.");
4055 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004056 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4057 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058 goto fail;
4059 }
4060 }
4061
4062 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004063 lchown(hapd->conf->ctrl_interface, -1,
4064 hapd->conf->ctrl_interface_gid) < 0) {
4065 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004066 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067 return -1;
4068 }
4069
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004070 if (!hapd->conf->ctrl_interface_gid_set &&
4071 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004072 lchown(hapd->conf->ctrl_interface, -1,
4073 hapd->iface->interfaces->ctrl_iface_group) < 0) {
4074 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004075 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004076 return -1;
4077 }
4078
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004079#ifdef ANDROID
4080 /*
4081 * Android is using umask 0077 which would leave the control interface
4082 * directory without group access. This breaks things since Wi-Fi
4083 * framework assumes that this directory can be accessed by other
4084 * applications in the wifi group. Fix this by adding group access even
4085 * if umask value would prevent this.
4086 */
4087 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4088 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4089 strerror(errno));
4090 /* Try to continue anyway */
4091 }
4092#endif /* ANDROID */
4093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4095 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4096 goto fail;
4097
4098 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4099 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004100 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004101 goto fail;
4102 }
4103
4104 os_memset(&addr, 0, sizeof(addr));
4105#ifdef __FreeBSD__
4106 addr.sun_len = sizeof(addr);
4107#endif /* __FreeBSD__ */
4108 addr.sun_family = AF_UNIX;
4109 fname = hostapd_ctrl_iface_path(hapd);
4110 if (fname == NULL)
4111 goto fail;
4112 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4113 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4114 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4115 strerror(errno));
4116 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4117 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4118 " allow connections - assuming it was left"
4119 "over from forced program termination");
4120 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004121 wpa_printf(MSG_ERROR,
4122 "Could not unlink existing ctrl_iface socket '%s': %s",
4123 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 goto fail;
4125 }
4126 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4127 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004128 wpa_printf(MSG_ERROR,
4129 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4130 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 goto fail;
4132 }
4133 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4134 "ctrl_iface socket '%s'", fname);
4135 } else {
4136 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4137 "be in use - cannot override it");
4138 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4139 "not used anymore", fname);
4140 os_free(fname);
4141 fname = NULL;
4142 goto fail;
4143 }
4144 }
4145
4146 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004147 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4148 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004149 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150 goto fail;
4151 }
4152
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004153 if (!hapd->conf->ctrl_interface_gid_set &&
4154 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004155 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4156 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004157 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004158 goto fail;
4159 }
4160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004162 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4163 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004164 goto fail;
4165 }
4166 os_free(fname);
4167
4168 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004169 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4170 NULL) < 0) {
4171 hostapd_ctrl_iface_deinit(hapd);
4172 return -1;
4173 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174 hapd->msg_ctx = hapd;
4175 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4176
4177 return 0;
4178
4179fail:
4180 if (s >= 0)
4181 close(s);
4182 if (fname) {
4183 unlink(fname);
4184 os_free(fname);
4185 }
4186 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004187#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188}
4189
4190
4191void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4192{
4193 struct wpa_ctrl_dst *dst, *prev;
4194
4195 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004196#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004198#endif /* !CONFIG_CTRL_IFACE_UDP */
4199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004200 eloop_unregister_read_sock(hapd->ctrl_sock);
4201 close(hapd->ctrl_sock);
4202 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004203#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004204 fname = hostapd_ctrl_iface_path(hapd);
4205 if (fname)
4206 unlink(fname);
4207 os_free(fname);
4208
4209 if (hapd->conf->ctrl_interface &&
4210 rmdir(hapd->conf->ctrl_interface) < 0) {
4211 if (errno == ENOTEMPTY) {
4212 wpa_printf(MSG_DEBUG, "Control interface "
4213 "directory not empty - leaving it "
4214 "behind");
4215 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004216 wpa_printf(MSG_ERROR,
4217 "rmdir[ctrl_interface=%s]: %s",
4218 hapd->conf->ctrl_interface,
4219 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 }
4221 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004222#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223 }
4224
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004225 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4226 list)
4227 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004228
4229#ifdef CONFIG_TESTING_OPTIONS
4230 l2_packet_deinit(hapd->l2_test);
4231 hapd->l2_test = NULL;
4232#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233}
4234
4235
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004236static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4237 char *buf)
4238{
4239 if (hostapd_add_iface(interfaces, buf) < 0) {
4240 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4241 return -1;
4242 }
4243 return 0;
4244}
4245
4246
4247static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4248 char *buf)
4249{
4250 if (hostapd_remove_iface(interfaces, buf) < 0) {
4251 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4252 return -1;
4253 }
4254 return 0;
4255}
4256
4257
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004258static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004259 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004260 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004261{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004262 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4263 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004264}
4265
4266
4267static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004268 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004269 socklen_t fromlen)
4270{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004271 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004272}
4273
4274
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004275static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4276{
4277#ifdef CONFIG_WPS_TESTING
4278 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004279 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004280 wps_corrupt_pkhash = 0;
4281#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004282
4283#ifdef CONFIG_TESTING_OPTIONS
4284#ifdef CONFIG_DPP
4285 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004286#ifdef CONFIG_DPP3
4287 dpp_version_override = 3;
4288#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004289 dpp_version_override = 2;
4290#else /* CONFIG_DPP2 */
4291 dpp_version_override = 1;
4292#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004293#endif /* CONFIG_DPP */
4294#endif /* CONFIG_TESTING_OPTIONS */
4295
4296#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004297 dpp_global_clear(interfaces->dpp);
Sunil Ravi89eba102022-09-13 21:04:37 -07004298#ifdef CONFIG_DPP3
4299 {
4300 int i;
4301
4302 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
4303 struct dpp_pb_info *info;
4304
4305 info = &interfaces->dpp_pb[i];
4306 info->rx_time.sec = 0;
4307 info->rx_time.usec = 0;
4308 }
4309 }
4310#endif /* CONFIG_DPP3 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004311#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004312}
4313
4314
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004315#ifdef CONFIG_FST
4316
4317static int
4318hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4319 const char *cmd)
4320{
4321 char ifname[IFNAMSIZ + 1];
4322 struct fst_iface_cfg cfg;
4323 struct hostapd_data *hapd;
4324 struct fst_wpa_obj iface_obj;
4325
4326 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4327 hapd = hostapd_get_iface(interfaces, ifname);
4328 if (hapd) {
4329 if (hapd->iface->fst) {
4330 wpa_printf(MSG_INFO, "FST: Already attached");
4331 return -1;
4332 }
4333 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4334 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4335 &iface_obj, &cfg);
4336 if (hapd->iface->fst)
4337 return 0;
4338 }
4339 }
4340
4341 return -EINVAL;
4342}
4343
4344
4345static int
4346hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4347 const char *cmd)
4348{
4349 char ifname[IFNAMSIZ + 1];
4350 struct hostapd_data * hapd;
4351
4352 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4353 hapd = hostapd_get_iface(interfaces, ifname);
4354 if (hapd) {
4355 if (!fst_iface_detach(ifname)) {
4356 hapd->iface->fst = NULL;
4357 hapd->iface->fst_ies = NULL;
4358 return 0;
4359 }
4360 }
4361 }
4362
4363 return -EINVAL;
4364}
4365
4366#endif /* CONFIG_FST */
4367
4368
4369static struct hostapd_data *
4370hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4371 const char *ifname)
4372{
4373 size_t i, j;
4374
4375 for (i = 0; i < interfaces->count; i++) {
4376 struct hostapd_iface *iface = interfaces->iface[i];
4377
4378 for (j = 0; j < iface->num_bss; j++) {
4379 struct hostapd_data *hapd;
4380
4381 hapd = iface->bss[j];
4382 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4383 return hapd;
4384 }
4385 }
4386
4387 return NULL;
4388}
4389
4390
4391static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4392 struct hostapd_data *dst_hapd,
4393 const char *param)
4394{
4395 int res;
4396 char *value;
4397
4398 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4399 if (!value) {
4400 wpa_printf(MSG_ERROR,
4401 "DUP: cannot allocate buffer to stringify %s",
4402 param);
4403 goto error_return;
4404 }
4405
4406 if (os_strcmp(param, "wpa") == 0) {
4407 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4408 src_hapd->conf->wpa);
4409 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4410 src_hapd->conf->wpa_key_mgmt) {
4411 res = hostapd_ctrl_iface_get_key_mgmt(
4412 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4413 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4414 goto error_stringify;
4415 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4416 src_hapd->conf->wpa_pairwise) {
4417 res = wpa_write_ciphers(value,
4418 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4419 src_hapd->conf->wpa_pairwise, " ");
4420 if (res < 0)
4421 goto error_stringify;
4422 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4423 src_hapd->conf->rsn_pairwise) {
4424 res = wpa_write_ciphers(value,
4425 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4426 src_hapd->conf->rsn_pairwise, " ");
4427 if (res < 0)
4428 goto error_stringify;
4429 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4430 src_hapd->conf->ssid.wpa_passphrase) {
4431 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4432 src_hapd->conf->ssid.wpa_passphrase);
4433 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4434 src_hapd->conf->ssid.wpa_psk_set) {
4435 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4436 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4437 } else {
4438 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4439 goto error_return;
4440 }
4441
4442 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4443 os_free(value);
4444 return res;
4445
4446error_stringify:
4447 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4448error_return:
4449 os_free(value);
4450 return -1;
4451}
4452
4453
4454static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004455hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4456 const char *input,
4457 char *reply, int reply_size)
4458{
4459 size_t i, j;
4460 int res;
4461 char *pos, *end;
4462 struct hostapd_iface *iface;
4463 int show_ctrl = 0;
4464
4465 if (input)
4466 show_ctrl = !!os_strstr(input, "ctrl");
4467
4468 pos = reply;
4469 end = reply + reply_size;
4470
4471 for (i = 0; i < interfaces->count; i++) {
4472 iface = interfaces->iface[i];
4473
4474 for (j = 0; j < iface->num_bss; j++) {
4475 struct hostapd_bss_config *conf;
4476
4477 conf = iface->conf->bss[j];
4478 if (show_ctrl)
4479 res = os_snprintf(pos, end - pos,
4480 "%s ctrl_iface=%s\n",
4481 conf->iface,
4482 conf->ctrl_interface ?
4483 conf->ctrl_interface : "N/A");
4484 else
4485 res = os_snprintf(pos, end - pos, "%s\n",
4486 conf->iface);
4487 if (os_snprintf_error(end - pos, res)) {
4488 *pos = '\0';
4489 return pos - reply;
4490 }
4491 pos += res;
4492 }
4493 }
4494
4495 return pos - reply;
4496}
4497
4498
4499static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004500hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4501 char *cmd)
4502{
4503 char *p_start = cmd, *p_end;
4504 struct hostapd_data *src_hapd, *dst_hapd;
4505
4506 /* cmd: "<src ifname> <dst ifname> <variable name> */
4507
4508 p_end = os_strchr(p_start, ' ');
4509 if (!p_end) {
4510 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4511 cmd);
4512 return -1;
4513 }
4514
4515 *p_end = '\0';
4516 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4517 if (!src_hapd) {
4518 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4519 p_start);
4520 return -1;
4521 }
4522
4523 p_start = p_end + 1;
4524 p_end = os_strchr(p_start, ' ');
4525 if (!p_end) {
4526 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4527 cmd);
4528 return -1;
4529 }
4530
4531 *p_end = '\0';
4532 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4533 if (!dst_hapd) {
4534 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4535 p_start);
4536 return -1;
4537 }
4538
4539 p_start = p_end + 1;
4540 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4541}
4542
4543
4544static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4545 const char *ifname,
4546 char *buf, char *reply,
4547 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004548 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004549 socklen_t fromlen)
4550{
4551 struct hostapd_data *hapd;
4552
4553 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4554 if (hapd == NULL) {
4555 int res;
4556
4557 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4558 if (os_snprintf_error(reply_size, res))
4559 return -1;
4560 return res;
4561 }
4562
4563 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4564 from, fromlen);
4565}
4566
4567
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004568static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4569 void *sock_ctx)
4570{
Hai Shalomfdcde762020-04-02 11:19:20 -07004571 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004572 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004573 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004574 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004575 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004576 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004577 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004578 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004579#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004580 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004581#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004582
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004583 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004584 (struct sockaddr *) &from, &fromlen);
4585 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004586 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4587 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004588 return;
4589 }
4590 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004591 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004592
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004593 reply = os_malloc(reply_size);
4594 if (reply == NULL) {
4595 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4596 fromlen) < 0) {
4597 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4598 strerror(errno));
4599 }
4600 return;
4601 }
4602
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004603 os_memcpy(reply, "OK\n", 3);
4604 reply_len = 3;
4605
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004606#ifdef CONFIG_CTRL_IFACE_UDP
4607 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4608 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004609 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4610 interfaces->ctrl_iface_cookie,
4611 CTRL_IFACE_COOKIE_LEN);
4612 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004613 goto send_reply;
4614 }
4615
4616 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004617 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004618 wpa_printf(MSG_DEBUG,
4619 "CTRL: No cookie in the request - drop request");
4620 os_free(reply);
4621 return;
4622 }
4623
Hai Shalomfdcde762020-04-02 11:19:20 -07004624 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4625 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004626 wpa_printf(MSG_DEBUG,
4627 "CTRL: Invalid cookie in the request - drop request");
4628 os_free(reply);
4629 return;
4630 }
4631
Hai Shalomfdcde762020-04-02 11:19:20 -07004632 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004633 while (*buf == ' ')
4634 buf++;
4635#endif /* CONFIG_CTRL_IFACE_UDP */
4636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004637 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4638 char *pos = os_strchr(buf + 7, ' ');
4639
4640 if (pos) {
4641 *pos++ = '\0';
4642 reply_len = hostapd_global_ctrl_iface_ifname(
4643 interfaces, buf + 7, pos, reply, reply_size,
4644 &from, fromlen);
4645 goto send_reply;
4646 }
4647 }
4648
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004649 if (os_strcmp(buf, "PING") == 0) {
4650 os_memcpy(reply, "PONG\n", 5);
4651 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004652 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4653 if (wpa_debug_reopen_file() < 0)
4654 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004655 } else if (os_strcmp(buf, "FLUSH") == 0) {
4656 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004657 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4658 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4659 reply_len = -1;
4660 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4661 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4662 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004663 } else if (os_strcmp(buf, "ATTACH") == 0) {
4664 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004665 fromlen, NULL))
4666 reply_len = -1;
4667 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4668 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4669 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004670 reply_len = -1;
4671 } else if (os_strcmp(buf, "DETACH") == 0) {
4672 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4673 fromlen))
4674 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004675#ifdef CONFIG_MODULE_TESTS
4676 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004677 if (hapd_module_tests() < 0)
4678 reply_len = -1;
4679#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004680#ifdef CONFIG_FST
4681 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4682 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4683 reply_len = os_snprintf(reply, reply_size, "OK\n");
4684 else
4685 reply_len = -1;
4686 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4687 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4688 reply_len = os_snprintf(reply, reply_size, "OK\n");
4689 else
4690 reply_len = -1;
4691 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4692 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4693#endif /* CONFIG_FST */
4694 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4695 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4696 buf + 12))
4697 reply_len = os_snprintf(reply, reply_size, "OK\n");
4698 else
4699 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004700 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4701 reply_len = hostapd_global_ctrl_iface_interfaces(
4702 interfaces, buf + 10, reply, sizeof(buffer));
4703 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4704 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004705 } else {
4706 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4707 "ignored");
4708 reply_len = -1;
4709 }
4710
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004711send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004712 if (reply_len < 0) {
4713 os_memcpy(reply, "FAIL\n", 5);
4714 reply_len = 5;
4715 }
4716
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004717 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4718 fromlen) < 0) {
4719 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4720 strerror(errno));
4721 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004722 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004723}
4724
4725
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004726#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004727static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4728{
4729 char *buf;
4730 size_t len;
4731
4732 if (interface->global_iface_path == NULL)
4733 return NULL;
4734
4735 len = os_strlen(interface->global_iface_path) +
4736 os_strlen(interface->global_iface_name) + 2;
4737 buf = os_malloc(len);
4738 if (buf == NULL)
4739 return NULL;
4740
4741 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4742 interface->global_iface_name);
4743 buf[len - 1] = '\0';
4744 return buf;
4745}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004746#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004747
4748
4749int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4750{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004751#ifdef CONFIG_CTRL_IFACE_UDP
4752 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4753 char p[32] = { 0 };
4754 char *pos;
4755 struct addrinfo hints = { 0 }, *res, *saveres;
4756 int n;
4757
4758 if (interface->global_ctrl_sock > -1) {
4759 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4760 return 0;
4761 }
4762
4763 if (interface->global_iface_path == NULL)
4764 return 0;
4765
4766 pos = os_strstr(interface->global_iface_path, "udp:");
4767 if (pos) {
4768 pos += 4;
4769 port = atoi(pos);
4770 if (port <= 0) {
4771 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4772 goto fail;
4773 }
4774 }
4775
Hai Shalomfdcde762020-04-02 11:19:20 -07004776 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004777
4778#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4779 hints.ai_flags = AI_PASSIVE;
4780#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4781
4782#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4783 hints.ai_family = AF_INET6;
4784#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4785 hints.ai_family = AF_INET;
4786#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4787 hints.ai_socktype = SOCK_DGRAM;
4788
4789try_again:
4790 os_snprintf(p, sizeof(p), "%d", port);
4791 n = getaddrinfo(NULL, p, &hints, &res);
4792 if (n) {
4793 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4794 goto fail;
4795 }
4796
4797 saveres = res;
4798 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4799 res->ai_protocol);
4800 if (interface->global_ctrl_sock < 0) {
4801 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4802 goto fail;
4803 }
4804
4805 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4806 0) {
4807 port++;
4808 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4809 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4810 goto try_again;
4811 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4812 goto fail;
4813 }
4814
4815 freeaddrinfo(saveres);
4816
4817 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4818
4819 if (eloop_register_read_sock(interface->global_ctrl_sock,
4820 hostapd_global_ctrl_iface_receive,
4821 interface, NULL) < 0) {
4822 hostapd_global_ctrl_iface_deinit(interface);
4823 return -1;
4824 }
4825
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004826 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4827
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004828 return 0;
4829
4830fail:
4831 if (interface->global_ctrl_sock >= 0)
4832 close(interface->global_ctrl_sock);
4833 return -1;
4834#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004835 struct sockaddr_un addr;
4836 int s = -1;
4837 char *fname = NULL;
4838
4839 if (interface->global_iface_path == NULL) {
4840 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4841 return 0;
4842 }
4843
4844 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4845 if (errno == EEXIST) {
4846 wpa_printf(MSG_DEBUG, "Using existing control "
4847 "interface directory.");
4848 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004849 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4850 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004851 goto fail;
4852 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004853 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004854 lchown(interface->global_iface_path, -1,
4855 interface->ctrl_iface_group) < 0) {
4856 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004857 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004858 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004859 }
4860
4861 if (os_strlen(interface->global_iface_path) + 1 +
4862 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4863 goto fail;
4864
4865 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4866 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004867 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004868 goto fail;
4869 }
4870
4871 os_memset(&addr, 0, sizeof(addr));
4872#ifdef __FreeBSD__
4873 addr.sun_len = sizeof(addr);
4874#endif /* __FreeBSD__ */
4875 addr.sun_family = AF_UNIX;
4876 fname = hostapd_global_ctrl_iface_path(interface);
4877 if (fname == NULL)
4878 goto fail;
4879 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4880 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4881 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4882 strerror(errno));
4883 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4884 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4885 " allow connections - assuming it was left"
4886 "over from forced program termination");
4887 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004888 wpa_printf(MSG_ERROR,
4889 "Could not unlink existing ctrl_iface socket '%s': %s",
4890 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004891 goto fail;
4892 }
4893 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4894 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004895 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4896 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004897 goto fail;
4898 }
4899 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4900 "ctrl_iface socket '%s'", fname);
4901 } else {
4902 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4903 "be in use - cannot override it");
4904 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4905 "not used anymore", fname);
4906 os_free(fname);
4907 fname = NULL;
4908 goto fail;
4909 }
4910 }
4911
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004912 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004913 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4914 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004915 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004916 goto fail;
4917 }
4918
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004919 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004920 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4921 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004922 goto fail;
4923 }
4924 os_free(fname);
4925
4926 interface->global_ctrl_sock = s;
4927 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4928 interface, NULL);
4929
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004930 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4931
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004932 return 0;
4933
4934fail:
4935 if (s >= 0)
4936 close(s);
4937 if (fname) {
4938 unlink(fname);
4939 os_free(fname);
4940 }
4941 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004942#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004943}
4944
4945
4946void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4947{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004948#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004949 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004950#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004951 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004952
4953 if (interfaces->global_ctrl_sock > -1) {
4954 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4955 close(interfaces->global_ctrl_sock);
4956 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004957#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004958 fname = hostapd_global_ctrl_iface_path(interfaces);
4959 if (fname) {
4960 unlink(fname);
4961 os_free(fname);
4962 }
4963
4964 if (interfaces->global_iface_path &&
4965 rmdir(interfaces->global_iface_path) < 0) {
4966 if (errno == ENOTEMPTY) {
4967 wpa_printf(MSG_DEBUG, "Control interface "
4968 "directory not empty - leaving it "
4969 "behind");
4970 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004971 wpa_printf(MSG_ERROR,
4972 "rmdir[ctrl_interface=%s]: %s",
4973 interfaces->global_iface_path,
4974 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004975 }
4976 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004977#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004978 }
4979
4980 os_free(interfaces->global_iface_path);
4981 interfaces->global_iface_path = NULL;
4982
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004983 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4984 struct wpa_ctrl_dst, list)
4985 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004986}
4987
4988
Roshan Pius3a1667e2018-07-03 15:17:14 -07004989static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4990 const char *buf)
4991{
4992 /* Enable Probe Request events based on explicit request.
4993 * Other events are enabled by default.
4994 */
4995 if (str_starts(buf, RX_PROBE_REQUEST))
4996 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4997 return 1;
4998}
4999
5000
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005001static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
5002 const char *ifname, int level,
5003 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004{
5005 struct wpa_ctrl_dst *dst, *next;
5006 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005007 int idx, res;
5008 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009 char levelstr[10];
5010
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005011 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005012 return;
5013
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005014 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
5015 if (os_snprintf_error(sizeof(levelstr), res))
5016 return;
5017 idx = 0;
5018 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005019 io[idx].iov_base = "IFNAME=";
5020 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005021 idx++;
5022 io[idx].iov_base = (char *) ifname;
5023 io[idx].iov_len = os_strlen(ifname);
5024 idx++;
5025 io[idx].iov_base = " ";
5026 io[idx].iov_len = 1;
5027 idx++;
5028 }
5029 io[idx].iov_base = levelstr;
5030 io[idx].iov_len = os_strlen(levelstr);
5031 idx++;
5032 io[idx].iov_base = (char *) buf;
5033 io[idx].iov_len = len;
5034 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035 os_memset(&msg, 0, sizeof(msg));
5036 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005037 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038
5039 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005040 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005041 if ((level >= dst->debug_level) &&
5042 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005043 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
5044 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005045 msg.msg_name = &dst->addr;
5046 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005047 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005048 int _errno = errno;
5049 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
5050 "%d - %s",
5051 idx, errno, strerror(errno));
5052 dst->errors++;
5053 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005054 ctrl_iface_detach(ctrl_dst,
5055 &dst->addr,
5056 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057 }
5058 } else
5059 dst->errors = 0;
5060 }
5061 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 }
5063}
5064
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005065
5066static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
5067 enum wpa_msg_type type,
5068 const char *buf, size_t len)
5069{
5070 if (type != WPA_MSG_NO_GLOBAL) {
5071 hostapd_ctrl_iface_send_internal(
5072 hapd->iface->interfaces->global_ctrl_sock,
5073 &hapd->iface->interfaces->global_ctrl_dst,
5074 type != WPA_MSG_PER_INTERFACE ?
5075 NULL : hapd->conf->iface,
5076 level, buf, len);
5077 }
5078
5079 if (type != WPA_MSG_ONLY_GLOBAL) {
5080 hostapd_ctrl_iface_send_internal(
5081 hapd->ctrl_sock, &hapd->ctrl_dst,
5082 NULL, level, buf, len);
5083 }
5084}
5085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086#endif /* CONFIG_NATIVE_WINDOWS */