blob: 6bebcbfa62e561a0a20a33a79ac5205d4001b0e7 [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"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080041#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080043#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080045#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080046#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047#include "ap/hostapd.h"
48#include "ap/ap_config.h"
49#include "ap/ieee802_1x.h"
50#include "ap/wpa_auth.h"
Hai Shalom899fcc72020-10-19 14:38:18 -070051#include "ap/pmksa_cache_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052#include "ap/ieee802_11.h"
53#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054#include "ap/wps_hostapd.h"
55#include "ap/ctrl_iface_ap.h"
56#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080057#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080058#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070059#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080060#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070061#include "ap/neighbor_db.h"
62#include "ap/rrm.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070063#include "ap/dpp_hostapd.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070064#include "ap/dfs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065#include "wps/wps_defs.h"
66#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080067#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070068#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069#include "ctrl_iface.h"
70
71
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080072#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
73
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080074#ifdef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080075#define HOSTAPD_CTRL_IFACE_PORT 8877
76#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
77#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
78#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
79#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080
81static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020082 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083 const char *buf, size_t len);
84
85
86static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080087 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -070088 socklen_t fromlen, const char *input)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089{
Roshan Pius3a1667e2018-07-03 15:17:14 -070090 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091}
92
93
94static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080095 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096 socklen_t fromlen)
97{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080098 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099}
100
101
102static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800103 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 socklen_t fromlen,
105 char *level)
106{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800107 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108}
109
110
111static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
112 const char *txtaddr)
113{
114 u8 addr[ETH_ALEN];
115 struct sta_info *sta;
116
117 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
118
119 if (hwaddr_aton(txtaddr, addr))
120 return -1;
121
122 sta = ap_get_sta(hapd, addr);
123 if (sta)
124 return 0;
125
126 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
127 "notification", MAC2STR(addr));
128 sta = ap_sta_add(hapd, addr);
129 if (sta == NULL)
130 return -1;
131
132 hostapd_new_assoc_sta(hapd, sta, 0);
133 return 0;
134}
135
136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137#ifdef NEED_AP_MLME
138static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
139 const char *txtaddr)
140{
141 u8 addr[ETH_ALEN];
142 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
143
144 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
145
146 if (hwaddr_aton(txtaddr, addr) ||
147 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
148 return -1;
149
150 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
151
152 return 0;
153}
154#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156
157#ifdef CONFIG_WPS
158static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
159{
160 char *pin = os_strchr(txt, ' ');
161 char *timeout_txt;
162 int timeout;
163 u8 addr_buf[ETH_ALEN], *addr = NULL;
164 char *pos;
165
166 if (pin == NULL)
167 return -1;
168 *pin++ = '\0';
169
170 timeout_txt = os_strchr(pin, ' ');
171 if (timeout_txt) {
172 *timeout_txt++ = '\0';
173 timeout = atoi(timeout_txt);
174 pos = os_strchr(timeout_txt, ' ');
175 if (pos) {
176 *pos++ = '\0';
177 if (hwaddr_aton(pos, addr_buf) == 0)
178 addr = addr_buf;
179 }
180 } else
181 timeout = 0;
182
183 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
184}
185
186
187static int hostapd_ctrl_iface_wps_check_pin(
188 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
189{
190 char pin[9];
191 size_t len;
192 char *pos;
193 int ret;
194
195 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
196 (u8 *) cmd, os_strlen(cmd));
197 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
198 if (*pos < '0' || *pos > '9')
199 continue;
200 pin[len++] = *pos;
201 if (len == 9) {
202 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
203 return -1;
204 }
205 }
206 if (len != 4 && len != 8) {
207 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
208 return -1;
209 }
210 pin[len] = '\0';
211
212 if (len == 8) {
213 unsigned int pin_val;
214 pin_val = atoi(pin);
215 if (!wps_pin_valid(pin_val)) {
216 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
217 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800218 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 return -1;
220 return ret;
221 }
222 }
223
224 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800225 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 return -1;
227
228 return ret;
229}
230
231
Dmitry Shmidt04949592012-07-19 12:16:46 -0700232#ifdef CONFIG_WPS_NFC
233static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
234 char *pos)
235{
236 size_t len;
237 struct wpabuf *buf;
238 int ret;
239
240 len = os_strlen(pos);
241 if (len & 0x01)
242 return -1;
243 len /= 2;
244
245 buf = wpabuf_alloc(len);
246 if (buf == NULL)
247 return -1;
248 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
249 wpabuf_free(buf);
250 return -1;
251 }
252
253 ret = hostapd_wps_nfc_tag_read(hapd, buf);
254 wpabuf_free(buf);
255
256 return ret;
257}
258
259
260static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
261 char *cmd, char *reply,
262 size_t max_len)
263{
264 int ndef;
265 struct wpabuf *buf;
266 int res;
267
268 if (os_strcmp(cmd, "WPS") == 0)
269 ndef = 0;
270 else if (os_strcmp(cmd, "NDEF") == 0)
271 ndef = 1;
272 else
273 return -1;
274
275 buf = hostapd_wps_nfc_config_token(hapd, ndef);
276 if (buf == NULL)
277 return -1;
278
279 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
280 wpabuf_len(buf));
281 reply[res++] = '\n';
282 reply[res] = '\0';
283
284 wpabuf_free(buf);
285
286 return res;
287}
288
289
290static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
291 char *reply, size_t max_len,
292 int ndef)
293{
294 struct wpabuf *buf;
295 int res;
296
297 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
298 if (buf == NULL)
299 return -1;
300
301 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
302 wpabuf_len(buf));
303 reply[res++] = '\n';
304 reply[res] = '\0';
305
306 wpabuf_free(buf);
307
308 return res;
309}
310
311
312static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
313 char *cmd, char *reply,
314 size_t max_len)
315{
316 if (os_strcmp(cmd, "WPS") == 0)
317 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
318 max_len, 0);
319
320 if (os_strcmp(cmd, "NDEF") == 0)
321 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
322 max_len, 1);
323
324 if (os_strcmp(cmd, "enable") == 0)
325 return hostapd_wps_nfc_token_enable(hapd);
326
327 if (os_strcmp(cmd, "disable") == 0) {
328 hostapd_wps_nfc_token_disable(hapd);
329 return 0;
330 }
331
332 return -1;
333}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800334
335
336static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
337 char *cmd, char *reply,
338 size_t max_len)
339{
340 struct wpabuf *buf;
341 int res;
342 char *pos;
343 int ndef;
344
345 pos = os_strchr(cmd, ' ');
346 if (pos == NULL)
347 return -1;
348 *pos++ = '\0';
349
350 if (os_strcmp(cmd, "WPS") == 0)
351 ndef = 0;
352 else if (os_strcmp(cmd, "NDEF") == 0)
353 ndef = 1;
354 else
355 return -1;
356
357 if (os_strcmp(pos, "WPS-CR") == 0)
358 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
359 else
360 buf = NULL;
361 if (buf == NULL)
362 return -1;
363
364 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
365 wpabuf_len(buf));
366 reply[res++] = '\n';
367 reply[res] = '\0';
368
369 wpabuf_free(buf);
370
371 return res;
372}
373
374
375static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
376 char *cmd)
377{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800378 size_t len;
379 struct wpabuf *req, *sel;
380 int ret;
381 char *pos, *role, *type, *pos2;
382
383 role = cmd;
384 pos = os_strchr(role, ' ');
385 if (pos == NULL)
386 return -1;
387 *pos++ = '\0';
388
389 type = pos;
390 pos = os_strchr(type, ' ');
391 if (pos == NULL)
392 return -1;
393 *pos++ = '\0';
394
395 pos2 = os_strchr(pos, ' ');
396 if (pos2 == NULL)
397 return -1;
398 *pos2++ = '\0';
399
400 len = os_strlen(pos);
401 if (len & 0x01)
402 return -1;
403 len /= 2;
404
405 req = wpabuf_alloc(len);
406 if (req == NULL)
407 return -1;
408 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
409 wpabuf_free(req);
410 return -1;
411 }
412
413 len = os_strlen(pos2);
414 if (len & 0x01) {
415 wpabuf_free(req);
416 return -1;
417 }
418 len /= 2;
419
420 sel = wpabuf_alloc(len);
421 if (sel == NULL) {
422 wpabuf_free(req);
423 return -1;
424 }
425 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
426 wpabuf_free(req);
427 wpabuf_free(sel);
428 return -1;
429 }
430
431 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
432 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
433 } else {
434 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
435 "reported: role=%s type=%s", role, type);
436 ret = -1;
437 }
438 wpabuf_free(req);
439 wpabuf_free(sel);
440
441 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800442}
443
Dmitry Shmidt04949592012-07-19 12:16:46 -0700444#endif /* CONFIG_WPS_NFC */
445
446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
448 char *buf, size_t buflen)
449{
450 int timeout = 300;
451 char *pos;
452 const char *pin_txt;
453
454 pos = os_strchr(txt, ' ');
455 if (pos)
456 *pos++ = '\0';
457
458 if (os_strcmp(txt, "disable") == 0) {
459 hostapd_wps_ap_pin_disable(hapd);
460 return os_snprintf(buf, buflen, "OK\n");
461 }
462
463 if (os_strcmp(txt, "random") == 0) {
464 if (pos)
465 timeout = atoi(pos);
466 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
467 if (pin_txt == NULL)
468 return -1;
469 return os_snprintf(buf, buflen, "%s", pin_txt);
470 }
471
472 if (os_strcmp(txt, "get") == 0) {
473 pin_txt = hostapd_wps_ap_pin_get(hapd);
474 if (pin_txt == NULL)
475 return -1;
476 return os_snprintf(buf, buflen, "%s", pin_txt);
477 }
478
479 if (os_strcmp(txt, "set") == 0) {
480 char *pin;
481 if (pos == NULL)
482 return -1;
483 pin = pos;
484 pos = os_strchr(pos, ' ');
485 if (pos) {
486 *pos++ = '\0';
487 timeout = atoi(pos);
488 }
489 if (os_strlen(pin) > buflen)
490 return -1;
491 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
492 return -1;
493 return os_snprintf(buf, buflen, "%s", pin);
494 }
495
496 return -1;
497}
498
499
500static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
501{
502 char *pos;
503 char *ssid, *auth, *encr = NULL, *key = NULL;
504
505 ssid = txt;
506 pos = os_strchr(txt, ' ');
507 if (!pos)
508 return -1;
509 *pos++ = '\0';
510
511 auth = pos;
512 pos = os_strchr(pos, ' ');
513 if (pos) {
514 *pos++ = '\0';
515 encr = pos;
516 pos = os_strchr(pos, ' ');
517 if (pos) {
518 *pos++ = '\0';
519 key = pos;
520 }
521 }
522
523 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
524}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700525
526
527static const char * pbc_status_str(enum pbc_status status)
528{
529 switch (status) {
530 case WPS_PBC_STATUS_DISABLE:
531 return "Disabled";
532 case WPS_PBC_STATUS_ACTIVE:
533 return "Active";
534 case WPS_PBC_STATUS_TIMEOUT:
535 return "Timed-out";
536 case WPS_PBC_STATUS_OVERLAP:
537 return "Overlap";
538 default:
539 return "Unknown";
540 }
541}
542
543
544static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
545 char *buf, size_t buflen)
546{
547 int ret;
548 char *pos, *end;
549
550 pos = buf;
551 end = buf + buflen;
552
553 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
554 pbc_status_str(hapd->wps_stats.pbc_status));
555
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800556 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700557 return pos - buf;
558 pos += ret;
559
560 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
561 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
562 "Success":
563 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
564 "Failed" : "None")));
565
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800566 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700567 return pos - buf;
568 pos += ret;
569
570 /* If status == Failure - Add possible Reasons */
571 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
572 hapd->wps_stats.failure_reason > 0) {
573 ret = os_snprintf(pos, end - pos,
574 "Failure Reason: %s\n",
575 wps_ei_str(hapd->wps_stats.failure_reason));
576
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800577 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700578 return pos - buf;
579 pos += ret;
580 }
581
582 if (hapd->wps_stats.status) {
583 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
584 MAC2STR(hapd->wps_stats.peer_addr));
585
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800586 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700587 return pos - buf;
588 pos += ret;
589 }
590
591 return pos - buf;
592}
593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594#endif /* CONFIG_WPS */
595
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800596#ifdef CONFIG_HS20
597
598static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
599 const char *cmd)
600{
601 u8 addr[ETH_ALEN];
602 const char *url;
603
604 if (hwaddr_aton(cmd, addr))
605 return -1;
606 url = cmd + 17;
607 if (*url == '\0') {
608 url = NULL;
609 } else {
610 if (*url != ' ')
611 return -1;
612 url++;
613 if (*url == '\0')
614 url = NULL;
615 }
616
617 return hs20_send_wnm_notification(hapd, addr, 1, url);
618}
619
620
621static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
622 const char *cmd)
623{
624 u8 addr[ETH_ALEN];
625 int code, reauth_delay, ret;
626 const char *pos;
627 size_t url_len;
628 struct wpabuf *req;
629
630 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
631 if (hwaddr_aton(cmd, addr))
632 return -1;
633
634 pos = os_strchr(cmd, ' ');
635 if (pos == NULL)
636 return -1;
637 pos++;
638 code = atoi(pos);
639
640 pos = os_strchr(pos, ' ');
641 if (pos == NULL)
642 return -1;
643 pos++;
644 reauth_delay = atoi(pos);
645
646 url_len = 0;
647 pos = os_strchr(pos, ' ');
648 if (pos) {
649 pos++;
650 url_len = os_strlen(pos);
651 }
652
653 req = wpabuf_alloc(4 + url_len);
654 if (req == NULL)
655 return -1;
656 wpabuf_put_u8(req, code);
657 wpabuf_put_le16(req, reauth_delay);
658 wpabuf_put_u8(req, url_len);
659 if (pos)
660 wpabuf_put_data(req, pos, url_len);
661
662 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
663 " to indicate imminent deauthentication (code=%d "
664 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
665 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
666 wpabuf_free(req);
667 return ret;
668}
669
670#endif /* CONFIG_HS20 */
671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672
Dmitry Shmidt051af732013-10-22 13:52:46 -0700673#ifdef CONFIG_INTERWORKING
674
675static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
676 const char *cmd)
677{
678 u8 qos_map_set[16 + 2 * 21], count = 0;
679 const char *pos = cmd;
680 int val, ret;
681
682 for (;;) {
683 if (count == sizeof(qos_map_set)) {
684 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
685 return -1;
686 }
687
688 val = atoi(pos);
689 if (val < 0 || val > 255) {
690 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
691 return -1;
692 }
693
694 qos_map_set[count++] = val;
695 pos = os_strchr(pos, ',');
696 if (!pos)
697 break;
698 pos++;
699 }
700
701 if (count < 16 || count & 1) {
702 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
703 return -1;
704 }
705
706 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
707 if (ret) {
708 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
709 return -1;
710 }
711
712 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
713 hapd->conf->qos_map_set_len = count;
714
715 return 0;
716}
717
718
719static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
720 const char *cmd)
721{
722 u8 addr[ETH_ALEN];
723 struct sta_info *sta;
724 struct wpabuf *buf;
725 u8 *qos_map_set = hapd->conf->qos_map_set;
726 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
727 int ret;
728
729 if (!qos_map_set_len) {
730 wpa_printf(MSG_INFO, "QoS Map Set is not set");
731 return -1;
732 }
733
734 if (hwaddr_aton(cmd, addr))
735 return -1;
736
737 sta = ap_get_sta(hapd, addr);
738 if (sta == NULL) {
739 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
740 "for QoS Map Configuration message",
741 MAC2STR(addr));
742 return -1;
743 }
744
745 if (!sta->qos_map_enabled) {
746 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
747 "support for QoS Map", MAC2STR(addr));
748 return -1;
749 }
750
751 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
752 if (buf == NULL)
753 return -1;
754
755 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
756 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
757
758 /* QoS Map Set Element */
759 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
760 wpabuf_put_u8(buf, qos_map_set_len);
761 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
762
763 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
764 wpabuf_head(buf), wpabuf_len(buf));
765 wpabuf_free(buf);
766
767 return ret;
768}
769
770#endif /* CONFIG_INTERWORKING */
771
772
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700773#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800774
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800775static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
776 const char *cmd)
777{
778 u8 addr[ETH_ALEN];
779 struct sta_info *sta;
780 const char *pos;
781 unsigned int auto_report, timeout;
782
783 if (hwaddr_aton(cmd, addr)) {
784 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
785 return -1;
786 }
787
788 sta = ap_get_sta(hapd, addr);
789 if (!sta) {
790 wpa_printf(MSG_DEBUG, "Station " MACSTR
791 " not found for Collocated Interference Request",
792 MAC2STR(addr));
793 return -1;
794 }
795
796 pos = cmd + 17;
797 if (*pos != ' ')
798 return -1;
799 pos++;
800 auto_report = atoi(pos);
801 pos = os_strchr(pos, ' ');
802 if (!pos)
803 return -1;
804 pos++;
805 timeout = atoi(pos);
806
807 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
808}
809
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700810#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800811
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800813static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
814 char *buf, size_t buflen)
815{
816 int ret = 0;
817 char *pos, *end;
818
819 pos = buf;
820 end = buf + buflen;
821
822 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
823
824 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
825 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
826 if (os_snprintf_error(end - pos, ret))
827 return pos - buf;
828 pos += ret;
829 }
830 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
831 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
832 if (os_snprintf_error(end - pos, ret))
833 return pos - buf;
834 pos += ret;
835 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800836#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800837 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
838 ret = os_snprintf(pos, end - pos, "FT-PSK ");
839 if (os_snprintf_error(end - pos, ret))
840 return pos - buf;
841 pos += ret;
842 }
843 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
844 ret = os_snprintf(pos, end - pos, "FT-EAP ");
845 if (os_snprintf_error(end - pos, ret))
846 return pos - buf;
847 pos += ret;
848 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700849#ifdef CONFIG_SHA384
850 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
851 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
852 if (os_snprintf_error(end - pos, ret))
853 return pos - buf;
854 pos += ret;
855 }
856#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800857#ifdef CONFIG_SAE
858 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
859 ret = os_snprintf(pos, end - pos, "FT-SAE ");
860 if (os_snprintf_error(end - pos, ret))
861 return pos - buf;
862 pos += ret;
863 }
864#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800865#ifdef CONFIG_FILS
866 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
867 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
868 if (os_snprintf_error(end - pos, ret))
869 return pos - buf;
870 pos += ret;
871 }
872 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
873 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
874 if (os_snprintf_error(end - pos, ret))
875 return pos - buf;
876 pos += ret;
877 }
878#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800879#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800880 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
881 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
882 if (os_snprintf_error(end - pos, ret))
883 return pos - buf;
884 pos += ret;
885 }
886 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
887 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
888 if (os_snprintf_error(end - pos, ret))
889 return pos - buf;
890 pos += ret;
891 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800892#ifdef CONFIG_SAE
893 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
894 ret = os_snprintf(pos, end - pos, "SAE ");
895 if (os_snprintf_error(end - pos, ret))
896 return pos - buf;
897 pos += ret;
898 }
899#endif /* CONFIG_SAE */
900 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
901 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
902 if (os_snprintf_error(end - pos, ret))
903 return pos - buf;
904 pos += ret;
905 }
906 if (hapd->conf->wpa_key_mgmt &
907 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
908 ret = os_snprintf(pos, end - pos,
909 "WPA-EAP-SUITE-B-192 ");
910 if (os_snprintf_error(end - pos, ret))
911 return pos - buf;
912 pos += ret;
913 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800914#ifdef CONFIG_FILS
915 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
916 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
917 if (os_snprintf_error(end - pos, ret))
918 return pos - buf;
919 pos += ret;
920 }
921 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
922 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
923 if (os_snprintf_error(end - pos, ret))
924 return pos - buf;
925 pos += ret;
926 }
927#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800928
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700929#ifdef CONFIG_OWE
930 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
931 ret = os_snprintf(pos, end - pos, "OWE ");
932 if (os_snprintf_error(end - pos, ret))
933 return pos - buf;
934 pos += ret;
935 }
936#endif /* CONFIG_OWE */
937
938#ifdef CONFIG_DPP
939 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
940 ret = os_snprintf(pos, end - pos, "DPP ");
941 if (os_snprintf_error(end - pos, ret))
942 return pos - buf;
943 pos += ret;
944 }
945#endif /* CONFIG_DPP */
946
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800947 if (pos > buf && *(pos - 1) == ' ') {
948 *(pos - 1) = '\0';
949 pos--;
950 }
951
952 return pos - buf;
953}
954
955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
957 char *buf, size_t buflen)
958{
959 int ret;
960 char *pos, *end;
961
962 pos = buf;
963 end = buf + buflen;
964
965 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
966 "ssid=%s\n",
967 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700968 wpa_ssid_txt(hapd->conf->ssid.ssid,
969 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800970 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 return pos - buf;
972 pos += ret;
973
974#ifdef CONFIG_WPS
975 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
976 hapd->conf->wps_state == 0 ? "disabled" :
977 (hapd->conf->wps_state == 1 ? "not configured" :
978 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800979 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 return pos - buf;
981 pos += ret;
982
983 if (hapd->conf->wps_state && hapd->conf->wpa &&
984 hapd->conf->ssid.wpa_passphrase) {
985 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
986 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800987 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 return pos - buf;
989 pos += ret;
990 }
991
992 if (hapd->conf->wps_state && hapd->conf->wpa &&
993 hapd->conf->ssid.wpa_psk &&
994 hapd->conf->ssid.wpa_psk->group) {
995 char hex[PMK_LEN * 2 + 1];
996 wpa_snprintf_hex(hex, sizeof(hex),
997 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
998 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800999 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 return pos - buf;
1001 pos += ret;
1002 }
Hai Shalom60840252021-02-19 19:02:11 -08001003
1004 if (hapd->conf->multi_ap) {
1005 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1006
1007 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1008 hapd->conf->multi_ap);
1009 if (os_snprintf_error(end - pos, ret))
1010 return pos - buf;
1011 pos += ret;
1012
1013 if (ssid->ssid_len) {
1014 ret = os_snprintf(pos, end - pos,
1015 "multi_ap_backhaul_ssid=%s\n",
1016 wpa_ssid_txt(ssid->ssid,
1017 ssid->ssid_len));
1018 if (os_snprintf_error(end - pos, ret))
1019 return pos - buf;
1020 pos += ret;
1021 }
1022
1023 if (hapd->conf->wps_state && hapd->conf->wpa &&
1024 ssid->wpa_passphrase) {
1025 ret = os_snprintf(pos, end - pos,
1026 "multi_ap_backhaul_wpa_passphrase=%s\n",
1027 ssid->wpa_passphrase);
1028 if (os_snprintf_error(end - pos, ret))
1029 return pos - buf;
1030 pos += ret;
1031 }
1032
1033 if (hapd->conf->wps_state && hapd->conf->wpa &&
1034 ssid->wpa_psk &&
1035 ssid->wpa_psk->group) {
1036 char hex[PMK_LEN * 2 + 1];
1037
1038 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1039 PMK_LEN);
1040 ret = os_snprintf(pos, end - pos,
1041 "multi_ap_backhaul_wpa_psk=%s\n",
1042 hex);
1043 forced_memzero(hex, sizeof(hex));
1044 if (os_snprintf_error(end - pos, ret))
1045 return pos - buf;
1046 pos += ret;
1047 }
1048 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049#endif /* CONFIG_WPS */
1050
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001051 if (hapd->conf->wpa) {
1052 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1053 if (os_snprintf_error(end - pos, ret))
1054 return pos - buf;
1055 pos += ret;
1056 }
1057
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1059 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001060 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 return pos - buf;
1062 pos += ret;
1063
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001064 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065
1066 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001067 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 return pos - buf;
1069 pos += ret;
1070 }
1071
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001072 if (hapd->conf->wpa) {
1073 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1074 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001075 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 return pos - buf;
1077 pos += ret;
1078 }
1079
1080 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1081 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001082 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 return pos - buf;
1084 pos += ret;
1085
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001086 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1087 " ");
1088 if (ret < 0)
1089 return pos - buf;
1090 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091
1092 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001093 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 return pos - buf;
1095 pos += ret;
1096 }
1097
1098 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1099 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001100 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 return pos - buf;
1102 pos += ret;
1103
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001104 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001105 " ");
1106 if (ret < 0)
1107 return pos - buf;
1108 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109
1110 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001111 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 return pos - buf;
1113 pos += ret;
1114 }
1115
Hai Shalomfdcde762020-04-02 11:19:20 -07001116 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1117 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1118 hapd->conf->wpa_deny_ptk0_rekey);
1119 if (os_snprintf_error(end - pos, ret))
1120 return pos - buf;
1121 pos += ret;
1122 }
1123
1124 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1125 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1126 hapd->conf->extended_key_id);
1127 if (os_snprintf_error(end - pos, ret))
1128 return pos - buf;
1129 pos += ret;
1130 }
1131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 return pos - buf;
1133}
1134
1135
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001136static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
Hai Shalom60840252021-02-19 19:02:11 -08001137 const char *bands)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001138{
1139 union wpa_event_data event;
Hai Shalom60840252021-02-19 19:02:11 -08001140 u32 setband_mask = WPA_SETBAND_AUTO;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001141
Hai Shalom60840252021-02-19 19:02:11 -08001142 /*
1143 * For example:
1144 * SET setband 2G,6G
1145 * SET setband 5G
1146 * SET setband AUTO
1147 */
1148 if (!os_strstr(bands, "AUTO")) {
1149 if (os_strstr(bands, "5G"))
1150 setband_mask |= WPA_SETBAND_5G;
1151 if (os_strstr(bands, "6G"))
1152 setband_mask |= WPA_SETBAND_6G;
1153 if (os_strstr(bands, "2G"))
1154 setband_mask |= WPA_SETBAND_2G;
1155 if (setband_mask == WPA_SETBAND_AUTO)
1156 return -1;
1157 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001158
Hai Shalom60840252021-02-19 19:02:11 -08001159 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001160 os_memset(&event, 0, sizeof(event));
1161 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1162 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1163 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1164 }
1165
1166 return 0;
1167}
1168
1169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1171{
1172 char *value;
1173 int ret = 0;
1174
1175 value = os_strchr(cmd, ' ');
1176 if (value == NULL)
1177 return -1;
1178 *value++ = '\0';
1179
1180 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1181 if (0) {
1182#ifdef CONFIG_WPS_TESTING
1183 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1184 long int val;
1185 val = strtol(value, NULL, 0);
1186 if (val < 0 || val > 0xff) {
1187 ret = -1;
1188 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1189 "wps_version_number %ld", val);
1190 } else {
1191 wps_version_number = val;
1192 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1193 "version %u.%u",
1194 (wps_version_number & 0xf0) >> 4,
1195 wps_version_number & 0x0f);
1196 hostapd_wps_update_ie(hapd);
1197 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001198 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1199 wps_testing_stub_cred = atoi(value);
1200 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1201 wps_testing_stub_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001202 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1203 wps_corrupt_pkhash = atoi(value);
1204 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1205 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001207#ifdef CONFIG_TESTING_OPTIONS
1208 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1209 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001210 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1211 hapd->ext_eapol_frame_io = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001212 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1213 hapd->force_backlog_bytes = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001214#ifdef CONFIG_DPP
1215 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1216 os_free(hapd->dpp_config_obj_override);
1217 hapd->dpp_config_obj_override = os_strdup(value);
1218 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1219 os_free(hapd->dpp_discovery_override);
1220 hapd->dpp_discovery_override = os_strdup(value);
1221 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1222 os_free(hapd->dpp_groups_override);
1223 hapd->dpp_groups_override = os_strdup(value);
1224 } else if (os_strcasecmp(cmd,
1225 "dpp_ignore_netaccesskey_mismatch") == 0) {
1226 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001227 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1228 dpp_test = atoi(value);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001229 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1230 dpp_version_override = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001231#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001232#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001233#ifdef CONFIG_MBO
1234 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1235 int val;
1236
1237 if (!hapd->conf->mbo_enabled)
1238 return -1;
1239
1240 val = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001241 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001242 return -1;
1243
1244 hapd->mbo_assoc_disallow = val;
1245 ieee802_11_update_beacons(hapd->iface);
1246
1247 /*
1248 * TODO: Need to configure drivers that do AP MLME offload with
1249 * disallowing station logic.
1250 */
1251#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001252#ifdef CONFIG_DPP
1253 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1254 os_free(hapd->dpp_configurator_params);
1255 hapd->dpp_configurator_params = os_strdup(value);
Sunil Ravia04bd252022-05-02 22:54:18 -07001256#ifdef CONFIG_DPP2
1257 dpp_controller_set_params(hapd->iface->interfaces->dpp, value);
1258#endif /* CONFIG_DPP2 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001259 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1260 hapd->dpp_init_max_tries = atoi(value);
1261 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1262 hapd->dpp_init_retry_time = atoi(value);
1263 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1264 hapd->dpp_resp_wait_time = atoi(value);
1265 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1266 hapd->dpp_resp_max_tries = atoi(value);
1267 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1268 hapd->dpp_resp_retry_time = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001269#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001270 } else if (os_strcasecmp(cmd, "setband") == 0) {
1271 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001273 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001274 if (ret)
1275 return ret;
1276
1277 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001278 hostapd_disassoc_deny_mac(hapd);
1279 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1280 hostapd_disassoc_accept_mac(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001281 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1282 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1283 hapd->parameter_set_count++;
1284 if (ieee802_11_update_beacons(hapd->iface))
1285 wpa_printf(MSG_DEBUG,
1286 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001287 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1288 os_strcmp(cmd, "sae_password") == 0 ||
1289 os_strcmp(cmd, "sae_pwe") == 0) {
1290 if (hapd->started)
1291 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001292 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1293 wpa_auth_set_transition_disable(hapd->wpa_auth,
1294 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001295 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001296
1297#ifdef CONFIG_TESTING_OPTIONS
1298 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1299 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1300 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001301 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1302 wpa_auth_set_ocv_override_freq(
1303 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1304 atoi(value));
1305 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1306 wpa_auth_set_ocv_override_freq(
1307 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1308 atoi(value));
1309 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1310 wpa_auth_set_ocv_override_freq(
1311 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1312 atoi(value));
1313 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1314 wpa_auth_set_ocv_override_freq(
1315 hapd->wpa_auth,
1316 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001317 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1318 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1319 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1320 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001321#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 }
1323
1324 return ret;
1325}
1326
1327
1328static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1329 char *buf, size_t buflen)
1330{
1331 int res;
1332
1333 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1334
1335 if (os_strcmp(cmd, "version") == 0) {
1336 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001337 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338 return -1;
1339 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001340 } else if (os_strcmp(cmd, "tls_library") == 0) {
1341 res = tls_get_library_version(buf, buflen);
1342 if (os_snprintf_error(buflen, res))
1343 return -1;
1344 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 }
1346
1347 return -1;
1348}
1349
1350
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001351static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1352{
1353 if (hostapd_enable_iface(iface) < 0) {
1354 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1355 return -1;
1356 }
1357 return 0;
1358}
1359
1360
1361static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1362{
1363 if (hostapd_reload_iface(iface) < 0) {
1364 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1365 return -1;
1366 }
1367 return 0;
1368}
1369
1370
1371static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1372{
1373 if (hostapd_disable_iface(iface) < 0) {
1374 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1375 return -1;
1376 }
1377 return 0;
1378}
1379
1380
Hai Shalom74f70d42019-02-11 14:42:39 -08001381static int
1382hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1383 struct sta_info *sta, void *ctx)
1384{
1385 struct hostapd_wpa_psk *psk;
1386 const u8 *pmk;
1387 int pmk_len;
1388 int pmk_match;
1389 int sta_match;
1390 int bss_match;
1391 int reason;
1392
1393 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1394
1395 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1396 pmk_match = PMK_LEN == pmk_len &&
1397 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1398 sta_match = psk->group == 0 &&
1399 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1400 bss_match = psk->group == 1;
1401
1402 if (pmk_match && (sta_match || bss_match))
1403 return 0;
1404 }
1405
1406 wpa_printf(MSG_INFO, "STA " MACSTR
1407 " PSK/passphrase no longer valid - disconnect",
1408 MAC2STR(sta->addr));
1409 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1410 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1411 ap_sta_deauthenticate(hapd, sta, reason);
1412
1413 return 0;
1414}
1415
1416
1417static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1418{
1419 struct hostapd_bss_config *conf = hapd->conf;
1420 int err;
1421
1422 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1423
1424 err = hostapd_setup_wpa_psk(conf);
1425 if (err < 0) {
1426 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1427 err);
1428 return -1;
1429 }
1430
1431 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1432 NULL);
1433
1434 return 0;
1435}
1436
1437
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001438#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001439
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001440static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1441{
1442 union wpa_event_data data;
1443 char *pos, *param;
1444 enum wpa_event_type event;
1445
1446 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1447
1448 os_memset(&data, 0, sizeof(data));
1449
1450 param = os_strchr(cmd, ' ');
1451 if (param == NULL)
1452 return -1;
1453 *param++ = '\0';
1454
1455 if (os_strcmp(cmd, "DETECTED") == 0)
1456 event = EVENT_DFS_RADAR_DETECTED;
1457 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1458 event = EVENT_DFS_CAC_FINISHED;
1459 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1460 event = EVENT_DFS_CAC_ABORTED;
1461 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1462 event = EVENT_DFS_NOP_FINISHED;
1463 else {
1464 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1465 cmd);
1466 return -1;
1467 }
1468
1469 pos = os_strstr(param, "freq=");
1470 if (pos)
1471 data.dfs_event.freq = atoi(pos + 5);
1472
1473 pos = os_strstr(param, "ht_enabled=1");
1474 if (pos)
1475 data.dfs_event.ht_enabled = 1;
1476
1477 pos = os_strstr(param, "chan_offset=");
1478 if (pos)
1479 data.dfs_event.chan_offset = atoi(pos + 12);
1480
1481 pos = os_strstr(param, "chan_width=");
1482 if (pos)
1483 data.dfs_event.chan_width = atoi(pos + 11);
1484
1485 pos = os_strstr(param, "cf1=");
1486 if (pos)
1487 data.dfs_event.cf1 = atoi(pos + 4);
1488
1489 pos = os_strstr(param, "cf2=");
1490 if (pos)
1491 data.dfs_event.cf2 = atoi(pos + 4);
1492
1493 wpa_supplicant_event(hapd, event, &data);
1494
1495 return 0;
1496}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001497
1498
1499static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1500{
1501 size_t len;
1502 u8 *buf;
1503 int res;
1504
1505 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1506
1507 len = os_strlen(cmd);
1508 if (len & 1)
1509 return -1;
1510 len /= 2;
1511
1512 buf = os_malloc(len);
1513 if (buf == NULL)
1514 return -1;
1515
1516 if (hexstr2bin(cmd, buf, len) < 0) {
1517 os_free(buf);
1518 return -1;
1519 }
1520
Hai Shalomfdcde762020-04-02 11:19:20 -07001521 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001522 os_free(buf);
1523 return res;
1524}
1525
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001526
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001527static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1528 char *cmd)
1529{
1530 char *pos, *param;
1531 size_t len;
1532 u8 *buf;
1533 int stype = 0, ok = 0;
1534 union wpa_event_data event;
1535
1536 if (!hapd->ext_mgmt_frame_handling)
1537 return -1;
1538
1539 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1540
1541 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1542
1543 pos = cmd;
1544 param = os_strstr(pos, "stype=");
1545 if (param) {
1546 param += 6;
1547 stype = atoi(param);
1548 }
1549
1550 param = os_strstr(pos, " ok=");
1551 if (param) {
1552 param += 4;
1553 ok = atoi(param);
1554 }
1555
1556 param = os_strstr(pos, " buf=");
1557 if (!param)
1558 return -1;
1559 param += 5;
1560
1561 len = os_strlen(param);
1562 if (len & 1)
1563 return -1;
1564 len /= 2;
1565
1566 buf = os_malloc(len);
1567 if (!buf || hexstr2bin(param, buf, len) < 0) {
1568 os_free(buf);
1569 return -1;
1570 }
1571
1572 os_memset(&event, 0, sizeof(event));
1573 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1574 event.tx_status.data = buf;
1575 event.tx_status.data_len = len;
1576 event.tx_status.stype = stype;
1577 event.tx_status.ack = ok;
1578 hapd->ext_mgmt_frame_handling = 0;
1579 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1580 hapd->ext_mgmt_frame_handling = 1;
1581
1582 os_free(buf);
1583
1584 return 0;
1585}
1586
1587
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001588static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1589 char *cmd)
1590{
1591 char *pos, *param;
1592 size_t len;
1593 u8 *buf;
1594 int freq = 0, datarate = 0, ssi_signal = 0;
1595 union wpa_event_data event;
1596
1597 if (!hapd->ext_mgmt_frame_handling)
1598 return -1;
1599
1600 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1601
1602 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1603
1604 pos = cmd;
1605 param = os_strstr(pos, "freq=");
1606 if (param) {
1607 param += 5;
1608 freq = atoi(param);
1609 }
1610
1611 param = os_strstr(pos, " datarate=");
1612 if (param) {
1613 param += 10;
1614 datarate = atoi(param);
1615 }
1616
1617 param = os_strstr(pos, " ssi_signal=");
1618 if (param) {
1619 param += 12;
1620 ssi_signal = atoi(param);
1621 }
1622
1623 param = os_strstr(pos, " frame=");
1624 if (param == NULL)
1625 return -1;
1626 param += 7;
1627
1628 len = os_strlen(param);
1629 if (len & 1)
1630 return -1;
1631 len /= 2;
1632
1633 buf = os_malloc(len);
1634 if (buf == NULL)
1635 return -1;
1636
1637 if (hexstr2bin(param, buf, len) < 0) {
1638 os_free(buf);
1639 return -1;
1640 }
1641
1642 os_memset(&event, 0, sizeof(event));
1643 event.rx_mgmt.freq = freq;
1644 event.rx_mgmt.frame = buf;
1645 event.rx_mgmt.frame_len = len;
1646 event.rx_mgmt.ssi_signal = ssi_signal;
1647 event.rx_mgmt.datarate = datarate;
1648 hapd->ext_mgmt_frame_handling = 0;
1649 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1650 hapd->ext_mgmt_frame_handling = 1;
1651
1652 os_free(buf);
1653
1654 return 0;
1655}
1656
1657
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001658static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1659{
1660 char *pos;
1661 u8 src[ETH_ALEN], *buf;
1662 int used;
1663 size_t len;
1664
1665 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1666
1667 pos = cmd;
1668 used = hwaddr_aton2(pos, src);
1669 if (used < 0)
1670 return -1;
1671 pos += used;
1672 while (*pos == ' ')
1673 pos++;
1674
1675 len = os_strlen(pos);
1676 if (len & 1)
1677 return -1;
1678 len /= 2;
1679
1680 buf = os_malloc(len);
1681 if (buf == NULL)
1682 return -1;
1683
1684 if (hexstr2bin(pos, buf, len) < 0) {
1685 os_free(buf);
1686 return -1;
1687 }
1688
Sunil8cd6f4d2022-06-28 18:40:46 +00001689 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001690 os_free(buf);
1691
1692 return 0;
1693}
1694
1695
Hai Shaloma20dcd72022-02-04 13:43:00 -08001696static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1697{
1698 char *pos, *pos2;
1699 u8 dst[ETH_ALEN], *buf;
1700 int used, ret;
1701 size_t len;
1702 unsigned int prev;
1703 int encrypt = 0;
1704
1705 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1706
1707 pos = cmd;
1708 used = hwaddr_aton2(pos, dst);
1709 if (used < 0)
1710 return -1;
1711 pos += used;
1712 while (*pos == ' ')
1713 pos++;
1714
1715 pos2 = os_strchr(pos, ' ');
1716 if (pos2) {
1717 len = pos2 - pos;
1718 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1719 } else {
1720 len = os_strlen(pos);
1721 }
1722 if (len & 1)
1723 return -1;
1724 len /= 2;
1725
1726 buf = os_malloc(len);
1727 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1728 os_free(buf);
1729 return -1;
1730 }
1731
1732 prev = hapd->ext_eapol_frame_io;
1733 hapd->ext_eapol_frame_io = 0;
1734 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1735 hapd->ext_eapol_frame_io = prev;
1736 os_free(buf);
1737
1738 return ret;
1739}
1740
1741
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001742static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1743{
1744 size_t i;
1745 u32 sum = 0;
1746 const u16 *pos = buf;
1747
1748 for (i = 0; i < len / 2; i++)
1749 sum += *pos++;
1750
1751 while (sum >> 16)
1752 sum = (sum & 0xffff) + (sum >> 16);
1753
1754 return sum ^ 0xffff;
1755}
1756
1757
1758#define HWSIM_PACKETLEN 1500
1759#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1760
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001761static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1762 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001763{
1764 struct hostapd_data *hapd = ctx;
1765 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001766 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001767 const u8 *pos;
1768 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001769 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001770
Hai Shalom81f62d82019-07-22 12:10:00 -07001771 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1772 wpa_printf(MSG_DEBUG,
1773 "test data: RX - ignore unexpected length %d",
1774 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001775 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001776 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001777
1778 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001779 os_memcpy(&ip, eth + 1, sizeof(ip));
1780 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001781
Hai Shalomfdcde762020-04-02 11:19:20 -07001782 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1783 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001784 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07001785 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001786 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001787 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001788
Hai Shalomfdcde762020-04-02 11:19:20 -07001789 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001790 if (*pos != (u8) i) {
1791 wpa_printf(MSG_DEBUG,
1792 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001793 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001794 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001795 pos++;
1796 }
1797
Hai Shalom81f62d82019-07-22 12:10:00 -07001798 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001799 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1800 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001801 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1802 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001803}
1804
1805
1806static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1807 char *cmd)
1808{
1809 int enabled = atoi(cmd);
1810 char *pos;
1811 const char *ifname;
1812
1813 if (!enabled) {
1814 if (hapd->l2_test) {
1815 l2_packet_deinit(hapd->l2_test);
1816 hapd->l2_test = NULL;
1817 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1818 "test data: Disabled");
1819 }
1820 return 0;
1821 }
1822
1823 if (hapd->l2_test)
1824 return 0;
1825
1826 pos = os_strstr(cmd, " ifname=");
1827 if (pos)
1828 ifname = pos + 8;
1829 else
1830 ifname = hapd->conf->iface;
1831
1832 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1833 ETHERTYPE_IP, hostapd_data_test_rx,
1834 hapd, 1);
1835 if (hapd->l2_test == NULL)
1836 return -1;
1837
1838 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1839
1840 return 0;
1841}
1842
1843
1844static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1845{
1846 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001847 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001848 int used;
1849 long int val;
1850 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001851 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001852 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001853 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001854 u8 *dpos;
1855 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001856 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001857
1858 if (hapd->l2_test == NULL)
1859 return -1;
1860
Hai Shalom81f62d82019-07-22 12:10:00 -07001861 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001862
1863 pos = cmd;
1864 used = hwaddr_aton2(pos, dst);
1865 if (used < 0)
1866 return -1;
1867 pos += used;
1868 while (*pos == ' ')
1869 pos++;
1870 used = hwaddr_aton2(pos, src);
1871 if (used < 0)
1872 return -1;
1873 pos += used;
1874
Hai Shalom81f62d82019-07-22 12:10:00 -07001875 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001876 if (val < 0 || val > 0xff)
1877 return -1;
1878 tos = val;
1879
Hai Shalom81f62d82019-07-22 12:10:00 -07001880 pos = os_strstr(pos2, " len=");
1881 if (pos) {
1882 i = atoi(pos + 5);
1883 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
1884 return -1;
1885 send_len = i;
1886 }
1887
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001888 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001889 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1890 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1891 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07001892 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07001894 ip->ip_hl = 5;
1895 ip->ip_v = 4;
1896 ip->ip_ttl = 64;
1897 ip->ip_tos = tos;
1898 ip->ip_len = htons(send_len);
1899 ip->ip_p = 1;
1900 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1901 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1902 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001903 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07001904 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001905 *dpos++ = i;
1906
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001907 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07001908 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001909 return -1;
1910
1911 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1912 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1913
1914 return 0;
1915}
1916
1917
1918static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1919 char *cmd)
1920{
1921 u8 *buf;
1922 struct ether_header *eth;
1923 struct l2_packet_data *l2 = NULL;
1924 size_t len;
1925 u16 ethertype;
1926 int res = -1;
1927 const char *ifname = hapd->conf->iface;
1928
1929 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1930 cmd += 7;
1931 ifname = cmd;
1932 cmd = os_strchr(cmd, ' ');
1933 if (cmd == NULL)
1934 return -1;
1935 *cmd++ = '\0';
1936 }
1937
1938 len = os_strlen(cmd);
1939 if (len & 1 || len < ETH_HLEN * 2)
1940 return -1;
1941 len /= 2;
1942
1943 buf = os_malloc(len);
1944 if (buf == NULL)
1945 return -1;
1946
1947 if (hexstr2bin(cmd, buf, len) < 0)
1948 goto done;
1949
1950 eth = (struct ether_header *) buf;
1951 ethertype = ntohs(eth->ether_type);
1952
1953 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1954 hostapd_data_test_rx, hapd, 1);
1955 if (l2 == NULL)
1956 goto done;
1957
1958 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1959 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1960done:
1961 if (l2)
1962 l2_packet_deinit(l2);
1963 os_free(buf);
1964
1965 return res < 0 ? -1 : 0;
1966}
1967
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001968
1969static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1970{
1971#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001972 char *pos;
1973
1974 wpa_trace_fail_after = atoi(cmd);
1975 pos = os_strchr(cmd, ':');
1976 if (pos) {
1977 pos++;
1978 os_strlcpy(wpa_trace_fail_func, pos,
1979 sizeof(wpa_trace_fail_func));
1980 } else {
1981 wpa_trace_fail_after = 0;
1982 }
1983
1984 return 0;
1985#else /* WPA_TRACE_BFD */
1986 return -1;
1987#endif /* WPA_TRACE_BFD */
1988}
1989
1990
1991static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1992 char *buf, size_t buflen)
1993{
1994#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001995 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1996 wpa_trace_fail_func);
1997#else /* WPA_TRACE_BFD */
1998 return -1;
1999#endif /* WPA_TRACE_BFD */
2000}
2001
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002002
2003static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2004{
2005#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002006 char *pos;
2007
2008 wpa_trace_test_fail_after = atoi(cmd);
2009 pos = os_strchr(cmd, ':');
2010 if (pos) {
2011 pos++;
2012 os_strlcpy(wpa_trace_test_fail_func, pos,
2013 sizeof(wpa_trace_test_fail_func));
2014 } else {
2015 wpa_trace_test_fail_after = 0;
2016 }
2017
2018 return 0;
2019#else /* WPA_TRACE_BFD */
2020 return -1;
2021#endif /* WPA_TRACE_BFD */
2022}
2023
2024
2025static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2026 char *buf, size_t buflen)
2027{
2028#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002029 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2030 wpa_trace_test_fail_func);
2031#else /* WPA_TRACE_BFD */
2032 return -1;
2033#endif /* WPA_TRACE_BFD */
2034}
2035
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002036
2037static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2038{
2039 struct sta_info *sta;
2040 u8 addr[ETH_ALEN];
2041 u8 zero[WPA_TK_MAX_LEN];
2042
2043 os_memset(zero, 0, sizeof(zero));
2044
2045 if (hwaddr_aton(cmd, addr))
2046 return -1;
2047
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002048 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2049 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2050 return -1;
2051
2052 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2053
2054 /* First, use a zero key to avoid any possible duplicate key
2055 * avoidance in the driver. */
2056 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2057 hapd->last_bigtk_alg,
2058 broadcast_ether_addr,
2059 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2060 zero, hapd->last_bigtk_len,
2061 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2062 return -1;
2063
2064 /* Set the previously configured key to reset its TSC */
2065 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2066 hapd->last_bigtk_alg,
2067 broadcast_ether_addr,
2068 hapd->last_bigtk_key_idx, 0, 1, NULL,
2069 0, hapd->last_bigtk,
2070 hapd->last_bigtk_len,
2071 KEY_FLAG_GROUP_TX_DEFAULT);
2072 }
2073
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002074 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2075 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2076 return -1;
2077
2078 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2079
2080 /* First, use a zero key to avoid any possible duplicate key
2081 * avoidance in the driver. */
2082 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2083 hapd->last_igtk_alg,
2084 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002085 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2086 zero, hapd->last_igtk_len,
2087 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002088 return -1;
2089
2090 /* Set the previously configured key to reset its TSC */
2091 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2092 hapd->last_igtk_alg,
2093 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002094 hapd->last_igtk_key_idx, 0, 1, NULL,
2095 0, hapd->last_igtk,
2096 hapd->last_igtk_len,
2097 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002098 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002099
2100 if (is_broadcast_ether_addr(addr)) {
2101 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2102 return -1;
2103
2104 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2105
2106 /* First, use a zero key to avoid any possible duplicate key
2107 * avoidance in the driver. */
2108 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2109 hapd->last_gtk_alg,
2110 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002111 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2112 zero, hapd->last_gtk_len,
2113 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002114 return -1;
2115
2116 /* Set the previously configured key to reset its TSC */
2117 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2118 hapd->last_gtk_alg,
2119 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002120 hapd->last_gtk_key_idx, 0, 1, NULL,
2121 0, hapd->last_gtk,
2122 hapd->last_gtk_len,
2123 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002124 }
2125
2126 sta = ap_get_sta(hapd, addr);
2127 if (!sta)
2128 return -1;
2129
2130 if (sta->last_tk_alg == WPA_ALG_NONE)
2131 return -1;
2132
2133 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2134 MAC2STR(sta->addr));
2135
2136 /* First, use a zero key to avoid any possible duplicate key avoidance
2137 * in the driver. */
2138 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002139 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2140 zero, sta->last_tk_len,
2141 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002142 return -1;
2143
2144 /* Set the previously configured key to reset its TSC/RSC */
2145 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002146 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2147 0, sta->last_tk, sta->last_tk_len,
2148 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002149}
2150
2151
2152static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2153{
2154 u8 addr[ETH_ALEN];
2155 const char *pos = cmd;
2156 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002157 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002158 int idx, set_tx;
2159 u8 seq[6], key[WPA_TK_MAX_LEN];
2160 size_t key_len;
2161
Hai Shalomfdcde762020-04-02 11:19:20 -07002162 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002163
2164 alg = atoi(pos);
2165 pos = os_strchr(pos, ' ');
2166 if (!pos)
2167 return -1;
2168 pos++;
2169 if (hwaddr_aton(pos, addr))
2170 return -1;
2171 pos += 17;
2172 if (*pos != ' ')
2173 return -1;
2174 pos++;
2175 idx = atoi(pos);
2176 pos = os_strchr(pos, ' ');
2177 if (!pos)
2178 return -1;
2179 pos++;
2180 set_tx = atoi(pos);
2181 pos = os_strchr(pos, ' ');
2182 if (!pos)
2183 return -1;
2184 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002185 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002186 return -1;
2187 pos += 2 * 6;
2188 if (*pos != ' ')
2189 return -1;
2190 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002191 if (!os_strchr(pos, ' '))
2192 return -1;
2193 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002194 if (hexstr2bin(pos, key, key_len) < 0)
2195 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002196 pos += 2 * key_len;
2197 if (*pos != ' ')
2198 return -1;
2199
2200 pos++;
2201 key_flag = atoi(pos);
2202 pos = os_strchr(pos, ' ');
2203 if (pos)
2204 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002205
2206 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002207 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2208 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002209}
2210
2211
2212static void restore_tk(void *ctx1, void *ctx2)
2213{
2214 struct hostapd_data *hapd = ctx1;
2215 struct sta_info *sta = ctx2;
2216
2217 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2218 MAC2STR(sta->addr));
2219 /* This does not really restore the TSC properly, so this will result
2220 * in replay protection issues for now since there is no clean way of
2221 * preventing encryption of a single EAPOL frame. */
2222 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002223 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2224 sta->last_tk, sta->last_tk_len,
2225 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002226}
2227
2228
2229static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2230{
2231 struct sta_info *sta;
2232 u8 addr[ETH_ALEN];
2233 int plain = os_strstr(cmd, "plaintext") != NULL;
2234
2235 if (hwaddr_aton(cmd, addr))
2236 return -1;
2237
2238 sta = ap_get_sta(hapd, addr);
2239 if (!sta || !sta->wpa_sm)
2240 return -1;
2241
2242 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2243 plain = 0; /* no need for special processing */
2244 if (plain) {
2245 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2246 MAC2STR(sta->addr));
2247 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002248 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2249 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002250 }
2251
2252 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2253 return wpa_auth_resend_m1(sta->wpa_sm,
2254 os_strstr(cmd, "change-anonce") != NULL,
2255 plain ? restore_tk : NULL, hapd, sta);
2256}
2257
2258
2259static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2260{
2261 struct sta_info *sta;
2262 u8 addr[ETH_ALEN];
2263 int plain = os_strstr(cmd, "plaintext") != NULL;
2264
2265 if (hwaddr_aton(cmd, addr))
2266 return -1;
2267
2268 sta = ap_get_sta(hapd, addr);
2269 if (!sta || !sta->wpa_sm)
2270 return -1;
2271
2272 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2273 plain = 0; /* no need for special processing */
2274 if (plain) {
2275 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2276 MAC2STR(sta->addr));
2277 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002278 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2279 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002280 }
2281
2282 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2283 return wpa_auth_resend_m3(sta->wpa_sm,
2284 plain ? restore_tk : NULL, hapd, sta);
2285}
2286
2287
2288static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2289 const char *cmd)
2290{
2291 struct sta_info *sta;
2292 u8 addr[ETH_ALEN];
2293 int plain = os_strstr(cmd, "plaintext") != NULL;
2294
2295 if (hwaddr_aton(cmd, addr))
2296 return -1;
2297
2298 sta = ap_get_sta(hapd, addr);
2299 if (!sta || !sta->wpa_sm)
2300 return -1;
2301
2302 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2303 plain = 0; /* no need for special processing */
2304 if (plain) {
2305 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2306 MAC2STR(sta->addr));
2307 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002308 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2309 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002310 }
2311
2312 wpa_printf(MSG_INFO,
2313 "TESTING: Send group M1 for the same GTK and zero RSC to "
2314 MACSTR, MAC2STR(sta->addr));
2315 return wpa_auth_resend_group_m1(sta->wpa_sm,
2316 plain ? restore_tk : NULL, hapd, sta);
2317}
2318
Hai Shalomfdcde762020-04-02 11:19:20 -07002319
Hai Shaloma20dcd72022-02-04 13:43:00 -08002320static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2321{
2322 struct sta_info *sta;
2323 u8 addr[ETH_ALEN];
2324
2325 if (hwaddr_aton(cmd, addr))
2326 return -1;
2327
2328 sta = ap_get_sta(hapd, addr);
2329 if (!sta || !sta->wpa_sm)
2330 return -1;
2331
2332 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2333}
2334
2335
Hai Shalom899fcc72020-10-19 14:38:18 -07002336static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2337 char *buf, size_t buflen)
2338{
2339 struct rsn_pmksa_cache_entry *pmksa;
2340
2341 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2342 if (!pmksa)
2343 return -1;
2344
2345 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2346}
2347
2348
Hai Shalomfdcde762020-04-02 11:19:20 -07002349static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2350 char *buf, size_t buflen)
2351{
2352 struct sta_info *sta;
2353 u8 addr[ETH_ALEN];
2354 const u8 *pmk;
2355 int pmk_len;
2356
2357 if (hwaddr_aton(cmd, addr))
2358 return -1;
2359
2360 sta = ap_get_sta(hapd, addr);
2361 if (!sta || !sta->wpa_sm) {
2362 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2363 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002364 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002365 }
2366 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002367 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002368 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2369 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002370 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002371 }
2372
2373 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2374}
2375
Hai Shaloma20dcd72022-02-04 13:43:00 -08002376
2377static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2378 const char *cmd)
2379{
2380 u16 type;
2381 char *pos, *end;
2382 u8 match[10];
2383 size_t match_len;
2384 bool multicast = false;
2385
2386 type = strtol(cmd, &pos, 16);
2387 if (*pos != ' ')
2388 return -1;
2389 pos++;
2390 end = os_strchr(pos, ' ');
2391 if (end) {
2392 match_len = end - pos;
2393 multicast = os_strstr(end, "multicast") != NULL;
2394 } else {
2395 match_len = os_strlen(pos) / 2;
2396 }
2397 if (hexstr2bin(pos, match, match_len))
2398 return -1;
2399
2400 return hostapd_drv_register_frame(hapd, type, match, match_len,
2401 multicast);
2402}
2403
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002404#endif /* CONFIG_TESTING_OPTIONS */
2405
2406
Hai Shalomb755a2a2020-04-23 21:49:02 -07002407#ifdef NEED_AP_MLME
Hai Shalomfdcde762020-04-02 11:19:20 -07002408static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params)
2409{
2410 switch (params->bandwidth) {
2411 case 0:
2412 /* bandwidth not specified: use 20 MHz by default */
2413 /* fall-through */
2414 case 20:
2415 if (params->center_freq1 &&
2416 params->center_freq1 != params->freq)
2417 return -1;
2418
2419 if (params->center_freq2 || params->sec_channel_offset)
2420 return -1;
2421 break;
2422 case 40:
2423 if (params->center_freq2 || !params->sec_channel_offset)
2424 return -1;
2425
2426 if (!params->center_freq1)
2427 break;
2428 switch (params->sec_channel_offset) {
2429 case 1:
2430 if (params->freq + 10 != params->center_freq1)
2431 return -1;
2432 break;
2433 case -1:
2434 if (params->freq - 10 != params->center_freq1)
2435 return -1;
2436 break;
2437 default:
2438 return -1;
2439 }
2440 break;
2441 case 80:
2442 if (!params->center_freq1 || !params->sec_channel_offset)
2443 return 1;
2444
2445 switch (params->sec_channel_offset) {
2446 case 1:
2447 if (params->freq - 10 != params->center_freq1 &&
2448 params->freq + 30 != params->center_freq1)
2449 return 1;
2450 break;
2451 case -1:
2452 if (params->freq + 10 != params->center_freq1 &&
2453 params->freq - 30 != params->center_freq1)
2454 return -1;
2455 break;
2456 default:
2457 return -1;
2458 }
2459
2460 /* Adjacent and overlapped are not allowed for 80+80 */
2461 if (params->center_freq2 &&
2462 params->center_freq1 - params->center_freq2 <= 80 &&
2463 params->center_freq2 - params->center_freq1 <= 80)
2464 return 1;
2465 break;
2466 case 160:
2467 if (!params->center_freq1 || params->center_freq2 ||
2468 !params->sec_channel_offset)
2469 return -1;
2470
2471 switch (params->sec_channel_offset) {
2472 case 1:
2473 if (params->freq + 70 != params->center_freq1 &&
2474 params->freq + 30 != params->center_freq1 &&
2475 params->freq - 10 != params->center_freq1 &&
2476 params->freq - 50 != params->center_freq1)
2477 return -1;
2478 break;
2479 case -1:
2480 if (params->freq + 50 != params->center_freq1 &&
2481 params->freq + 10 != params->center_freq1 &&
2482 params->freq - 30 != params->center_freq1 &&
2483 params->freq - 70 != params->center_freq1)
2484 return -1;
2485 break;
2486 default:
2487 return -1;
2488 }
2489 break;
2490 default:
2491 return -1;
2492 }
2493
2494 return 0;
2495}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002496#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002497
2498
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002499static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2500 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002501{
2502#ifdef NEED_AP_MLME
2503 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002504 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002505 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002506 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002507 int bandwidth;
2508 u8 chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002509
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002510 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002511 if (ret)
2512 return ret;
2513
Hai Shalomfdcde762020-04-02 11:19:20 -07002514 ret = hostapd_ctrl_check_freq_params(&settings.freq_params);
2515 if (ret) {
2516 wpa_printf(MSG_INFO,
2517 "chanswitch: invalid frequency settings provided");
2518 return ret;
2519 }
2520
2521 switch (settings.freq_params.bandwidth) {
2522 case 40:
2523 bandwidth = CHAN_WIDTH_40;
2524 break;
2525 case 80:
2526 if (settings.freq_params.center_freq2)
2527 bandwidth = CHAN_WIDTH_80P80;
2528 else
2529 bandwidth = CHAN_WIDTH_80;
2530 break;
2531 case 160:
2532 bandwidth = CHAN_WIDTH_160;
2533 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002534 case 320:
2535 bandwidth = CHAN_WIDTH_320;
2536 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002537 default:
2538 bandwidth = CHAN_WIDTH_20;
2539 break;
2540 }
2541
2542 if (settings.freq_params.center_freq1)
2543 dfs_range += hostapd_is_dfs_overlap(
2544 iface, bandwidth, settings.freq_params.center_freq1);
2545 else
2546 dfs_range += hostapd_is_dfs_overlap(
2547 iface, bandwidth, settings.freq_params.freq);
2548
2549 if (settings.freq_params.center_freq2)
2550 dfs_range += hostapd_is_dfs_overlap(
2551 iface, bandwidth, settings.freq_params.center_freq2);
2552
2553 if (dfs_range) {
2554 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2555 if (ret == NUM_HOSTAPD_MODES) {
2556 wpa_printf(MSG_ERROR,
2557 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2558 settings.freq_params.freq,
2559 settings.freq_params.sec_channel_offset,
2560 settings.freq_params.bandwidth);
2561 return -1;
2562 }
2563
2564 settings.freq_params.channel = chan;
2565
2566 wpa_printf(MSG_DEBUG,
2567 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2568 settings.freq_params.channel,
2569 settings.freq_params.freq,
2570 settings.freq_params.sec_channel_offset,
2571 settings.freq_params.bandwidth,
2572 settings.freq_params.center_freq1);
2573
2574 /* Perform CAC and switch channel */
2575 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2576 return 0;
2577 }
2578
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002579 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002580
Sunil Ravia04bd252022-05-02 22:54:18 -07002581 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002582 hostapd_chan_switch_config(iface->bss[i],
2583 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002584
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002585 ret = hostapd_switch_channel(iface->bss[i], &settings);
2586 if (ret) {
2587 /* FIX: What do we do if CSA fails in the middle of
2588 * submitting multi-BSS CSA requests? */
2589 return ret;
2590 }
2591 }
2592
2593 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002594#else /* NEED_AP_MLME */
2595 return -1;
2596#endif /* NEED_AP_MLME */
2597}
2598
2599
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002600static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2601 int reply_size, const char *param)
2602{
2603#ifdef RADIUS_SERVER
2604 if (os_strcmp(param, "radius_server") == 0) {
2605 return radius_server_get_mib(hapd->radius_srv, reply,
2606 reply_size);
2607 }
2608#endif /* RADIUS_SERVER */
2609 return -1;
2610}
2611
2612
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002613static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2614 char *buf, size_t buflen)
2615{
2616 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08002617 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002618 u8 *data = NULL;
2619 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08002620 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002621 struct wpabuf *reply;
2622 size_t data_len = 0;
2623
Hai Shalom60840252021-02-19 19:02:11 -08002624 /**
2625 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2626 * [nested=<0|1>]
2627 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002628 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002629 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002630 return -EINVAL;
2631
2632 subcmd = strtoul(pos, &pos, 10);
2633
2634 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002635 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002636 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08002637
2638 temp = os_strchr(pos, ' ');
2639 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002640 }
2641
2642 if (data_len) {
2643 data_len /= 2;
2644 data = os_malloc(data_len);
2645 if (!data)
2646 return -ENOBUFS;
2647
2648 if (hexstr2bin(pos, data, data_len)) {
2649 wpa_printf(MSG_DEBUG,
2650 "Vendor command: wrong parameter format");
2651 os_free(data);
2652 return -EINVAL;
2653 }
2654 }
2655
Hai Shalom60840252021-02-19 19:02:11 -08002656 pos = os_strstr(cmd, "nested=");
2657 if (pos)
2658 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2659 NESTED_ATTR_NOT_USED;
2660
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002661 reply = wpabuf_alloc((buflen - 1) / 2);
2662 if (!reply) {
2663 os_free(data);
2664 return -ENOBUFS;
2665 }
2666
2667 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08002668 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002669
2670 if (ret == 0)
2671 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2672 wpabuf_len(reply));
2673
2674 wpabuf_free(reply);
2675 os_free(data);
2676
2677 return ret;
2678}
2679
2680
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002681static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2682 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002684 u8 addr[ETH_ALEN];
2685 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002686
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002687 if (hwaddr_aton(cmd, addr))
2688 return -1;
2689
2690 sta = ap_get_sta(hapd, addr);
2691 if (!sta || !sta->eapol_sm)
2692 return -1;
2693
2694 eapol_auth_reauthenticate(sta->eapol_sm);
2695 return 0;
2696}
2697
2698
2699static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2700{
2701 u8 addr[ETH_ALEN];
2702 struct sta_info *sta;
2703 char *pos = cmd, *param;
2704
2705 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2706 return -1;
2707 pos += 18;
2708 param = pos;
2709 pos = os_strchr(pos, ' ');
2710 if (!pos)
2711 return -1;
2712 *pos++ = '\0';
2713
2714 sta = ap_get_sta(hapd, addr);
2715 if (!sta || !sta->eapol_sm)
2716 return -1;
2717
2718 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2719}
2720
2721
2722static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2723 char *buf, size_t buflen)
2724{
2725 char *pos, *end, *stamp;
2726 int ret;
2727
2728 /* cmd: "LOG_LEVEL [<level>]" */
2729 if (*cmd == '\0') {
2730 pos = buf;
2731 end = buf + buflen;
2732 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2733 "Timestamp: %d\n",
2734 debug_level_str(wpa_debug_level),
2735 wpa_debug_timestamp);
2736 if (os_snprintf_error(end - pos, ret))
2737 ret = 0;
2738
2739 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002742 while (*cmd == ' ')
2743 cmd++;
2744
2745 stamp = os_strchr(cmd, ' ');
2746 if (stamp) {
2747 *stamp++ = '\0';
2748 while (*stamp == ' ') {
2749 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 }
2752
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002753 if (os_strlen(cmd)) {
2754 int level = str_to_debug_level(cmd);
2755 if (level < 0)
2756 return -1;
2757 wpa_debug_level = level;
2758 }
2759
2760 if (stamp && os_strlen(stamp))
2761 wpa_debug_timestamp = atoi(stamp);
2762
2763 os_memcpy(buf, "OK\n", 3);
2764 return 3;
2765}
2766
2767
2768#ifdef NEED_AP_MLME
2769static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2770 char *buf, size_t buflen)
2771{
2772 struct hostapd_iface *iface = hapd->iface;
2773 char *pos, *end;
2774 struct hostapd_sta_info *info;
2775 struct os_reltime now;
2776
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002777 if (!iface->num_sta_seen)
2778 return 0;
2779
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002780 sta_track_expire(iface, 0);
2781
2782 pos = buf;
2783 end = buf + buflen;
2784
2785 os_get_reltime(&now);
2786 dl_list_for_each_reverse(info, &iface->sta_seen,
2787 struct hostapd_sta_info, list) {
2788 struct os_reltime age;
2789 int ret;
2790
2791 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002792 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2793 MAC2STR(info->addr), (unsigned int) age.sec,
2794 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002795 if (os_snprintf_error(end - pos, ret))
2796 break;
2797 pos += ret;
2798 }
2799
2800 return pos - buf;
2801}
2802#endif /* NEED_AP_MLME */
2803
2804
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002805static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2806 const char *cmd)
2807{
2808 u8 addr[ETH_ALEN];
2809
2810 if (hwaddr_aton(cmd, addr)) {
2811 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2812 return -1;
2813 }
2814
2815 return hostapd_send_lci_req(hapd, addr);
2816}
2817
2818
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002819static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002820{
2821 u8 addr[ETH_ALEN];
2822 char *token, *context = NULL;
2823 int random_interval, min_ap;
2824 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2825 unsigned int n_responders;
2826
2827 token = str_token(cmd, " ", &context);
2828 if (!token || hwaddr_aton(token, addr)) {
2829 wpa_printf(MSG_INFO,
2830 "CTRL: REQ_RANGE - Bad destination address");
2831 return -1;
2832 }
2833
2834 token = str_token(cmd, " ", &context);
2835 if (!token)
2836 return -1;
2837
2838 random_interval = atoi(token);
2839 if (random_interval < 0 || random_interval > 0xffff)
2840 return -1;
2841
2842 token = str_token(cmd, " ", &context);
2843 if (!token)
2844 return -1;
2845
2846 min_ap = atoi(token);
2847 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2848 return -1;
2849
2850 n_responders = 0;
2851 while ((token = str_token(cmd, " ", &context))) {
2852 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2853 wpa_printf(MSG_INFO,
2854 "CTRL: REQ_RANGE: Too many responders");
2855 return -1;
2856 }
2857
2858 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2859 wpa_printf(MSG_INFO,
2860 "CTRL: REQ_RANGE: Bad responder address");
2861 return -1;
2862 }
2863
2864 n_responders++;
2865 }
2866
2867 if (!n_responders) {
2868 wpa_printf(MSG_INFO,
2869 "CTRL: REQ_RANGE - No FTM responder address");
2870 return -1;
2871 }
2872
2873 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2874 responders, n_responders);
2875}
2876
2877
Dmitry Shmidt29333592017-01-09 12:27:11 -08002878static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2879 const char *cmd, char *reply,
2880 size_t reply_size)
2881{
2882 u8 addr[ETH_ALEN];
2883 const char *pos;
2884 struct wpabuf *req;
2885 int ret;
2886 u8 req_mode = 0;
2887
2888 if (hwaddr_aton(cmd, addr))
2889 return -1;
2890 pos = os_strchr(cmd, ' ');
2891 if (!pos)
2892 return -1;
2893 pos++;
2894 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2895 int val = hex2byte(pos + 9);
2896
2897 if (val < 0)
2898 return -1;
2899 req_mode = val;
2900 pos += 11;
2901 pos = os_strchr(pos, ' ');
2902 if (!pos)
2903 return -1;
2904 pos++;
2905 }
2906 req = wpabuf_parse_bin(pos);
2907 if (!req)
2908 return -1;
2909
2910 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2911 wpabuf_free(req);
2912 if (ret >= 0)
2913 ret = os_snprintf(reply, reply_size, "%d", ret);
2914 return ret;
2915}
2916
2917
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002918static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
2919 char *buf, size_t buflen)
2920{
2921 if (!(hapd->conf->radio_measurements[0] &
2922 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2923 wpa_printf(MSG_ERROR,
2924 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
2925 return -1;
2926 }
2927
2928 return hostapd_neighbor_show(hapd, buf, buflen);
2929}
2930
2931
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002932static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2933{
2934 struct wpa_ssid_value ssid;
2935 u8 bssid[ETH_ALEN];
2936 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002937 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002938 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002939 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002940 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002941
2942 if (!(hapd->conf->radio_measurements[0] &
2943 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2944 wpa_printf(MSG_ERROR,
2945 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2946 return -1;
2947 }
2948
2949 if (hwaddr_aton(buf, bssid)) {
2950 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2951 return -1;
2952 }
2953
2954 tmp = os_strstr(buf, "ssid=");
2955 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2956 wpa_printf(MSG_ERROR,
2957 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2958 return -1;
2959 }
2960 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2961 if (!buf)
2962 return -1;
2963
2964 tmp = os_strstr(buf, "nr=");
2965 if (!tmp) {
2966 wpa_printf(MSG_ERROR,
2967 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2968 return -1;
2969 }
2970
2971 buf = os_strchr(tmp, ' ');
2972 if (buf)
2973 *buf++ = '\0';
2974
2975 nr = wpabuf_parse_bin(tmp + 3);
2976 if (!nr) {
2977 wpa_printf(MSG_ERROR,
2978 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2979 return -1;
2980 }
2981
2982 if (!buf)
2983 goto set;
2984
2985 tmp = os_strstr(buf, "lci=");
2986 if (tmp) {
2987 buf = os_strchr(tmp, ' ');
2988 if (buf)
2989 *buf++ = '\0';
2990 lci = wpabuf_parse_bin(tmp + 4);
2991 if (!lci) {
2992 wpa_printf(MSG_ERROR,
2993 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08002994 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002995 }
2996 }
2997
2998 if (!buf)
2999 goto set;
3000
3001 tmp = os_strstr(buf, "civic=");
3002 if (tmp) {
3003 buf = os_strchr(tmp, ' ');
3004 if (buf)
3005 *buf++ = '\0';
3006 civic = wpabuf_parse_bin(tmp + 6);
3007 if (!civic) {
3008 wpa_printf(MSG_ERROR,
3009 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003010 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003011 }
3012 }
3013
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003014 if (!buf)
3015 goto set;
3016
3017 if (os_strstr(buf, "stat"))
3018 stationary = 1;
3019
Hai Shaloma20dcd72022-02-04 13:43:00 -08003020 tmp = os_strstr(buf, "bss_parameter=");
3021 if (tmp) {
3022 bss_parameters = atoi(tmp + 14);
3023 if (bss_parameters < 0 || bss_parameters > 0xff) {
3024 wpa_printf(MSG_ERROR,
3025 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3026 goto fail;
3027 }
3028 }
3029
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003030set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003031 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003032 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003033
Hai Shaloma20dcd72022-02-04 13:43:00 -08003034fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003035 wpabuf_free(nr);
3036 wpabuf_free(lci);
3037 wpabuf_free(civic);
3038
3039 return ret;
3040}
3041
3042
3043static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3044 char *buf)
3045{
3046 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003047 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003048 u8 bssid[ETH_ALEN];
3049 char *tmp;
3050
3051 if (hwaddr_aton(buf, bssid)) {
3052 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3053 return -1;
3054 }
3055
3056 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003057 if (tmp) {
3058 ssidp = &ssid;
3059 if (ssid_parse(tmp + 5, &ssid)) {
3060 wpa_printf(MSG_ERROR,
3061 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3062 return -1;
3063 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003064 }
3065
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003066 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003067}
3068
3069
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003070static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3071 size_t buflen)
3072{
3073 int ret, i;
3074 char *pos, *end;
3075
3076 ret = os_snprintf(buf, buflen, "%016llX:\n",
3077 (long long unsigned) iface->drv_flags);
3078 if (os_snprintf_error(buflen, ret))
3079 return -1;
3080
3081 pos = buf + ret;
3082 end = buf + buflen;
3083
3084 for (i = 0; i < 64; i++) {
3085 if (iface->drv_flags & (1LLU << i)) {
3086 ret = os_snprintf(pos, end - pos, "%s\n",
3087 driver_flag_to_string(1LLU << i));
3088 if (os_snprintf_error(end - pos, ret))
3089 return -1;
3090 pos += ret;
3091 }
3092 }
3093
3094 return pos - buf;
3095}
3096
3097
Hai Shalomb755a2a2020-04-23 21:49:02 -07003098static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3099 size_t buflen)
3100{
3101 int ret, i;
3102 char *pos, *end;
3103
3104 ret = os_snprintf(buf, buflen, "%016llX:\n",
3105 (long long unsigned) iface->drv_flags2);
3106 if (os_snprintf_error(buflen, ret))
3107 return -1;
3108
3109 pos = buf + ret;
3110 end = buf + buflen;
3111
3112 for (i = 0; i < 64; i++) {
3113 if (iface->drv_flags2 & (1LLU << i)) {
3114 ret = os_snprintf(pos, end - pos, "%s\n",
3115 driver_flag2_to_string(1LLU << i));
3116 if (os_snprintf_error(end - pos, ret))
3117 return -1;
3118 pos += ret;
3119 }
3120 }
3121
3122 return pos - buf;
3123}
3124
3125
Hai Shalom021b0b52019-04-10 11:17:58 -07003126static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3127 const char *field, char *buf,
3128 size_t buflen)
3129{
3130 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3131
3132#ifdef CONFIG_DPP
3133 if (os_strcmp(field, "dpp") == 0) {
3134 int res;
3135
Hai Shaloma20dcd72022-02-04 13:43:00 -08003136#ifdef CONFIG_DPP3
3137 res = os_snprintf(buf, buflen, "DPP=3");
3138#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003139 res = os_snprintf(buf, buflen, "DPP=2");
3140#else /* CONFIG_DPP2 */
3141 res = os_snprintf(buf, buflen, "DPP=1");
3142#endif /* CONFIG_DPP2 */
3143 if (os_snprintf_error(buflen, res))
3144 return -1;
3145 return res;
3146 }
3147#endif /* CONFIG_DPP */
3148
3149 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3150 field);
3151
3152 return -1;
3153}
3154
3155
Hai Shaloma20dcd72022-02-04 13:43:00 -08003156#ifdef ANDROID
3157static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3158 char *buf, size_t buflen)
3159{
3160 int ret;
3161
3162 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3163 if (ret == 0) {
3164 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3165 if (os_snprintf_error(buflen, ret))
3166 ret = -1;
3167 }
3168 return ret;
3169}
3170#endif /* ANDROID */
3171
3172
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003173static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3174 char *buf, char *reply,
3175 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003176 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003177 socklen_t fromlen)
3178{
3179 int reply_len, res;
3180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 os_memcpy(reply, "OK\n", 3);
3182 reply_len = 3;
3183
3184 if (os_strcmp(buf, "PING") == 0) {
3185 os_memcpy(reply, "PONG\n", 5);
3186 reply_len = 5;
3187 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3188 if (wpa_debug_reopen_file() < 0)
3189 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003190 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3191 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003192 } else if (os_strcmp(buf, "STATUS") == 0) {
3193 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3194 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003195 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3196 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197 } else if (os_strcmp(buf, "MIB") == 0) {
3198 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3199 if (reply_len >= 0) {
3200 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3201 reply_size - reply_len);
3202 if (res < 0)
3203 reply_len = -1;
3204 else
3205 reply_len += res;
3206 }
3207 if (reply_len >= 0) {
3208 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3209 reply_size - reply_len);
3210 if (res < 0)
3211 reply_len = -1;
3212 else
3213 reply_len += res;
3214 }
3215#ifndef CONFIG_NO_RADIUS
3216 if (reply_len >= 0) {
3217 res = radius_client_get_mib(hapd->radius,
3218 reply + reply_len,
3219 reply_size - reply_len);
3220 if (res < 0)
3221 reply_len = -1;
3222 else
3223 reply_len += res;
3224 }
3225#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003226 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3227 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3228 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3230 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3231 reply_size);
3232 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3233 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3234 reply_size);
3235 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3236 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3237 reply_size);
3238 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003239 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3240 reply_len = -1;
3241 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3242 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 reply_len = -1;
3244 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003245 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246 reply_len = -1;
3247 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003248 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 buf + 6))
3250 reply_len = -1;
3251 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3252 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3253 reply_len = -1;
3254 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3255 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3256 reply_len = -1;
3257 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3258 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3259 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003260#ifdef CONFIG_TAXONOMY
3261 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3262 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3263 reply, reply_size);
3264#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003265 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3266 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3267 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003268 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3269 if (hostapd_ctrl_iface_stop_ap(hapd))
3270 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271#ifdef NEED_AP_MLME
3272 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3273 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3274 reply_len = -1;
3275#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276#ifdef CONFIG_WPS
3277 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3278 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3279 reply_len = -1;
3280 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3281 reply_len = hostapd_ctrl_iface_wps_check_pin(
3282 hapd, buf + 14, reply, reply_size);
3283 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3284 if (hostapd_wps_button_pushed(hapd, NULL))
3285 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003286 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3287 if (hostapd_wps_cancel(hapd))
3288 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3290 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3291 reply, reply_size);
3292 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3293 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3294 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003295 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3296 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3297 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003298#ifdef CONFIG_WPS_NFC
3299 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3300 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3301 reply_len = -1;
3302 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3303 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3304 hapd, buf + 21, reply, reply_size);
3305 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3306 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3307 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003308 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3309 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3310 hapd, buf + 21, reply, reply_size);
3311 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3312 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3313 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003314#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003315#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07003316#ifdef CONFIG_INTERWORKING
3317 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3318 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3319 reply_len = -1;
3320 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3321 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3322 reply_len = -1;
3323#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003324#ifdef CONFIG_HS20
3325 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3326 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3327 reply_len = -1;
3328 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3329 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3330 reply_len = -1;
3331#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003332#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003333 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3334 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3335 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003336 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3337 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3338 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003339 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3340 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3341 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003342 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3343 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3344 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003345#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003346 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3347 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3348 reply_size);
3349 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3350 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3351 reply_len = -1;
3352 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3353 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3354 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003355 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3356 if (hostapd_ctrl_iface_enable(hapd->iface))
3357 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003358 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3359 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3360 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003361 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3362 if (hostapd_ctrl_iface_reload(hapd->iface))
3363 reply_len = -1;
3364 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3365 if (hostapd_ctrl_iface_disable(hapd->iface))
3366 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003367 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3368 if (ieee802_11_set_beacon(hapd))
3369 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003370#ifdef CONFIG_TESTING_OPTIONS
3371 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3372 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3373 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003374 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3375 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3376 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003377 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3378 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3379 buf + 23) < 0)
3380 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003381 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3382 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3383 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003384 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3385 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3386 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003387 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
3388 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
3389 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003390 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3391 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3392 reply_len = -1;
3393 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3394 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3395 reply_len = -1;
3396 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3397 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3398 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003399 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3400 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3401 reply_len = -1;
3402 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3403 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3404 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003405 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3406 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3407 reply_len = -1;
3408 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3409 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003410 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3411 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3412 reply_len = -1;
3413 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3414 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3415 reply_len = -1;
3416 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3417 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3418 reply_len = -1;
3419 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3420 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3421 reply_len = -1;
3422 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3423 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3424 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003425 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
3426 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
3427 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003428 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3429 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3430 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003431 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3432 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3433 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003434 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
3435 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
3436 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003437#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003438 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003439 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003440 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003441 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3442 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3443 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003444 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3445 ieee802_1x_erp_flush(hapd);
3446#ifdef RADIUS_SERVER
3447 radius_server_erp_flush(hapd->radius_srv);
3448#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003449 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3450 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3451 reply_len = -1;
3452 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3453 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3454 reply_len = -1;
3455 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3456 reply_len = hostapd_ctrl_iface_log_level(
3457 hapd, buf + 9, reply, reply_size);
3458#ifdef NEED_AP_MLME
3459 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3460 reply_len = hostapd_ctrl_iface_track_sta_list(
3461 hapd, reply, reply_size);
3462#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07003463 } else if (os_strcmp(buf, "PMKSA") == 0) {
3464 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3465 reply_size);
3466 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3467 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003468 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3469 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3470 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003471 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3472 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3473 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003474 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3475 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3476 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003477 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3478 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3479 reply_len = -1;
3480 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3481 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3482 reply_len = -1;
3483 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3484 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3485 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003486 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3487 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3488 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003489 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3490 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3491 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07003492 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
3493 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
3494 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003495 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3496 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003497 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3498 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003499 if (hostapd_ctrl_iface_acl_add_mac(
3500 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003501 &hapd->conf->num_accept_mac, buf + 19) ||
3502 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003503 reply_len = -1;
3504 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003505 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003506 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003507 &hapd->conf->num_accept_mac, buf + 19) ||
3508 hostapd_set_acl(hapd) ||
3509 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07003510 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003511 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3512 reply_len = hostapd_ctrl_iface_acl_show_mac(
3513 hapd->conf->accept_mac,
3514 hapd->conf->num_accept_mac, reply, reply_size);
3515 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3516 hostapd_ctrl_iface_acl_clear_list(
3517 &hapd->conf->accept_mac,
3518 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003519 if (hostapd_set_acl(hapd) ||
3520 hostapd_disassoc_accept_mac(hapd))
3521 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003522 }
3523 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3524 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003525 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003526 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003527 &hapd->conf->num_deny_mac, buf + 17) ||
3528 hostapd_set_acl(hapd) ||
3529 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003530 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003531 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003532 if (hostapd_ctrl_iface_acl_del_mac(
3533 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003534 &hapd->conf->num_deny_mac, buf + 17) ||
3535 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003536 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003537 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3538 reply_len = hostapd_ctrl_iface_acl_show_mac(
3539 hapd->conf->deny_mac,
3540 hapd->conf->num_deny_mac, reply, reply_size);
3541 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3542 hostapd_ctrl_iface_acl_clear_list(
3543 &hapd->conf->deny_mac,
3544 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003545 if (hostapd_set_acl(hapd))
3546 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003547 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003548#ifdef CONFIG_DPP
3549 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3550 res = hostapd_dpp_qr_code(hapd, buf + 12);
3551 if (res < 0) {
3552 reply_len = -1;
3553 } else {
3554 reply_len = os_snprintf(reply, reply_size, "%d", res);
3555 if (os_snprintf_error(reply_size, reply_len))
3556 reply_len = -1;
3557 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003558 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3559 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3560 if (res < 0) {
3561 reply_len = -1;
3562 } else {
3563 reply_len = os_snprintf(reply, reply_size, "%d", res);
3564 if (os_snprintf_error(reply_size, reply_len))
3565 reply_len = -1;
3566 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003567 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3568 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3569 if (res < 0) {
3570 reply_len = -1;
3571 } else {
3572 reply_len = os_snprintf(reply, reply_size, "%d", res);
3573 if (os_snprintf_error(reply_size, reply_len))
3574 reply_len = -1;
3575 }
3576 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3577 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3578 if (res < 0) {
3579 reply_len = -1;
3580 } else {
3581 reply_len = os_snprintf(reply, reply_size, "%d", res);
3582 if (os_snprintf_error(reply_size, reply_len))
3583 reply_len = -1;
3584 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003585 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003586 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003587 if (res < 0) {
3588 reply_len = -1;
3589 } else {
3590 reply_len = os_snprintf(reply, reply_size, "%d", res);
3591 if (os_snprintf_error(reply_size, reply_len))
3592 reply_len = -1;
3593 }
3594 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003595 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3596 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003597 reply_len = -1;
3598 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3599 const char *uri;
3600
Hai Shalom021b0b52019-04-10 11:17:58 -07003601 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3602 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003603 if (!uri) {
3604 reply_len = -1;
3605 } else {
3606 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3607 if (os_snprintf_error(reply_size, reply_len))
3608 reply_len = -1;
3609 }
3610 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003611 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3612 atoi(buf + 19),
3613 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07003614 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3615 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3616 atoi(buf + 18),
3617 os_strchr(buf + 18, ' ')) < 0)
3618 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003619 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3620 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3621 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003622 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3623 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3624 reply_len = -1;
3625 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3626 hostapd_dpp_stop(hapd);
3627 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003628 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003629 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3630 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003631 if (res < 0) {
3632 reply_len = -1;
3633 } else {
3634 reply_len = os_snprintf(reply, reply_size, "%d", res);
3635 if (os_snprintf_error(reply_size, reply_len))
3636 reply_len = -1;
3637 }
Sunil Ravia04bd252022-05-02 22:54:18 -07003638 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
3639 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
3640 buf + 20) < 0)
3641 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003642 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003643 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3644 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003645 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003646 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003647 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003648 reply_len = -1;
3649 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003650 reply_len = dpp_configurator_get_key_id(
3651 hapd->iface->interfaces->dpp,
3652 atoi(buf + 25),
3653 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003654 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3655 res = hostapd_dpp_pkex_add(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 }
3663 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3664 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3665 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003666#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07003667 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
3668 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
3669 reply_len = -1;
3670 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
3671 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
3672 reply_len = -1;
3673 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
3674 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003675 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
3676 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
3677 reply_len = -1;
3678 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
3679 hostapd_dpp_chirp_stop(hapd);
3680#endif /* CONFIG_DPP2 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003681#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003682#ifdef RADIUS_SERVER
3683 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3684 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3685 reply_len = -1;
3686#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07003687 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3688 reply_len = hostapd_ctrl_iface_get_capability(
3689 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08003690#ifdef CONFIG_PASN
3691 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
3692 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
3693#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003694#ifdef ANDROID
3695 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
3696 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
3697 reply_size);
3698#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 } else {
3700 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3701 reply_len = 16;
3702 }
3703
3704 if (reply_len < 0) {
3705 os_memcpy(reply, "FAIL\n", 5);
3706 reply_len = 5;
3707 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003708
3709 return reply_len;
3710}
3711
3712
3713static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3714 void *sock_ctx)
3715{
3716 struct hostapd_data *hapd = eloop_ctx;
3717 char buf[4096];
3718 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003719 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003720 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003721 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003722 const int reply_size = 4096;
3723 int reply_len;
3724 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003725#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07003726 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003727#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003728
3729 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3730 (struct sockaddr *) &from, &fromlen);
3731 if (res < 0) {
3732 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3733 strerror(errno));
3734 return;
3735 }
3736 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003737
3738 reply = os_malloc(reply_size);
3739 if (reply == NULL) {
3740 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3741 fromlen) < 0) {
3742 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3743 strerror(errno));
3744 }
3745 return;
3746 }
3747
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003748#ifdef CONFIG_CTRL_IFACE_UDP
3749 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3750 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07003751 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
3752 hapd->ctrl_iface_cookie,
3753 CTRL_IFACE_COOKIE_LEN);
3754 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003755 goto done;
3756 }
3757
3758 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07003759 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003760 wpa_printf(MSG_DEBUG,
3761 "CTRL: No cookie in the request - drop request");
3762 os_free(reply);
3763 return;
3764 }
3765
Hai Shalomfdcde762020-04-02 11:19:20 -07003766 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
3767 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003768 wpa_printf(MSG_DEBUG,
3769 "CTRL: Invalid cookie in the request - drop request");
3770 os_free(reply);
3771 return;
3772 }
3773
Hai Shalomfdcde762020-04-02 11:19:20 -07003774 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003775 while (*pos == ' ')
3776 pos++;
3777#endif /* CONFIG_CTRL_IFACE_UDP */
3778
3779 if (os_strcmp(pos, "PING") == 0)
3780 level = MSG_EXCESSIVE;
3781 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3782
3783 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003784 reply, reply_size,
3785 &from, fromlen);
3786
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003787#ifdef CONFIG_CTRL_IFACE_UDP
3788done:
3789#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003790 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3791 fromlen) < 0) {
3792 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3793 strerror(errno));
3794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795 os_free(reply);
3796}
3797
3798
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003799#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3801{
3802 char *buf;
3803 size_t len;
3804
3805 if (hapd->conf->ctrl_interface == NULL)
3806 return NULL;
3807
3808 len = os_strlen(hapd->conf->ctrl_interface) +
3809 os_strlen(hapd->conf->iface) + 2;
3810 buf = os_malloc(len);
3811 if (buf == NULL)
3812 return NULL;
3813
3814 os_snprintf(buf, len, "%s/%s",
3815 hapd->conf->ctrl_interface, hapd->conf->iface);
3816 buf[len - 1] = '\0';
3817 return buf;
3818}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003819#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820
3821
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07003822static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3823 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 const char *txt, size_t len)
3825{
3826 struct hostapd_data *hapd = ctx;
3827 if (hapd == NULL)
3828 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003829 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003830}
3831
3832
3833int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3834{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003835#ifdef CONFIG_CTRL_IFACE_UDP
3836 int port = HOSTAPD_CTRL_IFACE_PORT;
3837 char p[32] = { 0 };
3838 char port_str[40], *tmp;
3839 char *pos;
3840 struct addrinfo hints = { 0 }, *res, *saveres;
3841 int n;
3842
3843 if (hapd->ctrl_sock > -1) {
3844 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3845 return 0;
3846 }
3847
3848 if (hapd->conf->ctrl_interface == NULL)
3849 return 0;
3850
3851 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3852 if (pos) {
3853 pos += 4;
3854 port = atoi(pos);
3855 if (port <= 0) {
3856 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3857 goto fail;
3858 }
3859 }
3860
3861 dl_list_init(&hapd->ctrl_dst);
3862 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003863 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003864
3865#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3866 hints.ai_flags = AI_PASSIVE;
3867#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3868
3869#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3870 hints.ai_family = AF_INET6;
3871#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3872 hints.ai_family = AF_INET;
3873#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3874 hints.ai_socktype = SOCK_DGRAM;
3875
3876try_again:
3877 os_snprintf(p, sizeof(p), "%d", port);
3878 n = getaddrinfo(NULL, p, &hints, &res);
3879 if (n) {
3880 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3881 goto fail;
3882 }
3883
3884 saveres = res;
3885 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3886 res->ai_protocol);
3887 if (hapd->ctrl_sock < 0) {
3888 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3889 goto fail;
3890 }
3891
3892 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3893 port--;
3894 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
3895 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
3896 goto try_again;
3897 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3898 goto fail;
3899 }
3900
3901 freeaddrinfo(saveres);
3902
3903 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3904 tmp = os_strdup(port_str);
3905 if (tmp) {
3906 os_free(hapd->conf->ctrl_interface);
3907 hapd->conf->ctrl_interface = tmp;
3908 }
3909 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3910
3911 if (eloop_register_read_sock(hapd->ctrl_sock,
3912 hostapd_ctrl_iface_receive, hapd, NULL) <
3913 0) {
3914 hostapd_ctrl_iface_deinit(hapd);
3915 return -1;
3916 }
3917
3918 hapd->msg_ctx = hapd;
3919 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3920
3921 return 0;
3922
3923fail:
3924 if (hapd->ctrl_sock >= 0)
3925 close(hapd->ctrl_sock);
3926 return -1;
3927#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 struct sockaddr_un addr;
3929 int s = -1;
3930 char *fname = NULL;
3931
Dmitry Shmidt04949592012-07-19 12:16:46 -07003932 if (hapd->ctrl_sock > -1) {
3933 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3934 return 0;
3935 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003937 dl_list_init(&hapd->ctrl_dst);
3938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 if (hapd->conf->ctrl_interface == NULL)
3940 return 0;
3941
3942 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3943 if (errno == EEXIST) {
3944 wpa_printf(MSG_DEBUG, "Using existing control "
3945 "interface directory.");
3946 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003947 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3948 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949 goto fail;
3950 }
3951 }
3952
3953 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08003954 lchown(hapd->conf->ctrl_interface, -1,
3955 hapd->conf->ctrl_interface_gid) < 0) {
3956 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003957 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958 return -1;
3959 }
3960
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003961 if (!hapd->conf->ctrl_interface_gid_set &&
3962 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08003963 lchown(hapd->conf->ctrl_interface, -1,
3964 hapd->iface->interfaces->ctrl_iface_group) < 0) {
3965 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003966 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003967 return -1;
3968 }
3969
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003970#ifdef ANDROID
3971 /*
3972 * Android is using umask 0077 which would leave the control interface
3973 * directory without group access. This breaks things since Wi-Fi
3974 * framework assumes that this directory can be accessed by other
3975 * applications in the wifi group. Fix this by adding group access even
3976 * if umask value would prevent this.
3977 */
3978 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3979 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
3980 strerror(errno));
3981 /* Try to continue anyway */
3982 }
3983#endif /* ANDROID */
3984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
3986 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
3987 goto fail;
3988
3989 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3990 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003991 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992 goto fail;
3993 }
3994
3995 os_memset(&addr, 0, sizeof(addr));
3996#ifdef __FreeBSD__
3997 addr.sun_len = sizeof(addr);
3998#endif /* __FreeBSD__ */
3999 addr.sun_family = AF_UNIX;
4000 fname = hostapd_ctrl_iface_path(hapd);
4001 if (fname == NULL)
4002 goto fail;
4003 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4004 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4005 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4006 strerror(errno));
4007 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4008 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4009 " allow connections - assuming it was left"
4010 "over from forced program termination");
4011 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004012 wpa_printf(MSG_ERROR,
4013 "Could not unlink existing ctrl_iface socket '%s': %s",
4014 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 goto fail;
4016 }
4017 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4018 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004019 wpa_printf(MSG_ERROR,
4020 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4021 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022 goto fail;
4023 }
4024 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4025 "ctrl_iface socket '%s'", fname);
4026 } else {
4027 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4028 "be in use - cannot override it");
4029 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4030 "not used anymore", fname);
4031 os_free(fname);
4032 fname = NULL;
4033 goto fail;
4034 }
4035 }
4036
4037 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004038 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4039 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004040 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 goto fail;
4042 }
4043
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004044 if (!hapd->conf->ctrl_interface_gid_set &&
4045 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004046 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4047 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004048 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004049 goto fail;
4050 }
4051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004053 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4054 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 goto fail;
4056 }
4057 os_free(fname);
4058
4059 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004060 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4061 NULL) < 0) {
4062 hostapd_ctrl_iface_deinit(hapd);
4063 return -1;
4064 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004065 hapd->msg_ctx = hapd;
4066 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4067
4068 return 0;
4069
4070fail:
4071 if (s >= 0)
4072 close(s);
4073 if (fname) {
4074 unlink(fname);
4075 os_free(fname);
4076 }
4077 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004078#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079}
4080
4081
4082void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4083{
4084 struct wpa_ctrl_dst *dst, *prev;
4085
4086 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004087#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004089#endif /* !CONFIG_CTRL_IFACE_UDP */
4090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 eloop_unregister_read_sock(hapd->ctrl_sock);
4092 close(hapd->ctrl_sock);
4093 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004094#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004095 fname = hostapd_ctrl_iface_path(hapd);
4096 if (fname)
4097 unlink(fname);
4098 os_free(fname);
4099
4100 if (hapd->conf->ctrl_interface &&
4101 rmdir(hapd->conf->ctrl_interface) < 0) {
4102 if (errno == ENOTEMPTY) {
4103 wpa_printf(MSG_DEBUG, "Control interface "
4104 "directory not empty - leaving it "
4105 "behind");
4106 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004107 wpa_printf(MSG_ERROR,
4108 "rmdir[ctrl_interface=%s]: %s",
4109 hapd->conf->ctrl_interface,
4110 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111 }
4112 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004113#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 }
4115
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004116 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4117 list)
4118 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004119
4120#ifdef CONFIG_TESTING_OPTIONS
4121 l2_packet_deinit(hapd->l2_test);
4122 hapd->l2_test = NULL;
4123#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124}
4125
4126
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004127static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4128 char *buf)
4129{
4130 if (hostapd_add_iface(interfaces, buf) < 0) {
4131 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4132 return -1;
4133 }
4134 return 0;
4135}
4136
4137
4138static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4139 char *buf)
4140{
4141 if (hostapd_remove_iface(interfaces, buf) < 0) {
4142 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4143 return -1;
4144 }
4145 return 0;
4146}
4147
4148
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004149static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004150 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004151 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004152{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004153 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4154 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004155}
4156
4157
4158static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004159 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004160 socklen_t fromlen)
4161{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004162 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004163}
4164
4165
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004166static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4167{
4168#ifdef CONFIG_WPS_TESTING
4169 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004170 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004171 wps_corrupt_pkhash = 0;
4172#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004173
4174#ifdef CONFIG_TESTING_OPTIONS
4175#ifdef CONFIG_DPP
4176 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004177#ifdef CONFIG_DPP3
4178 dpp_version_override = 3;
4179#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004180 dpp_version_override = 2;
4181#else /* CONFIG_DPP2 */
4182 dpp_version_override = 1;
4183#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004184#endif /* CONFIG_DPP */
4185#endif /* CONFIG_TESTING_OPTIONS */
4186
4187#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004188 dpp_global_clear(interfaces->dpp);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004189#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004190}
4191
4192
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004193#ifdef CONFIG_FST
4194
4195static int
4196hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4197 const char *cmd)
4198{
4199 char ifname[IFNAMSIZ + 1];
4200 struct fst_iface_cfg cfg;
4201 struct hostapd_data *hapd;
4202 struct fst_wpa_obj iface_obj;
4203
4204 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4205 hapd = hostapd_get_iface(interfaces, ifname);
4206 if (hapd) {
4207 if (hapd->iface->fst) {
4208 wpa_printf(MSG_INFO, "FST: Already attached");
4209 return -1;
4210 }
4211 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4212 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4213 &iface_obj, &cfg);
4214 if (hapd->iface->fst)
4215 return 0;
4216 }
4217 }
4218
4219 return -EINVAL;
4220}
4221
4222
4223static int
4224hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4225 const char *cmd)
4226{
4227 char ifname[IFNAMSIZ + 1];
4228 struct hostapd_data * hapd;
4229
4230 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4231 hapd = hostapd_get_iface(interfaces, ifname);
4232 if (hapd) {
4233 if (!fst_iface_detach(ifname)) {
4234 hapd->iface->fst = NULL;
4235 hapd->iface->fst_ies = NULL;
4236 return 0;
4237 }
4238 }
4239 }
4240
4241 return -EINVAL;
4242}
4243
4244#endif /* CONFIG_FST */
4245
4246
4247static struct hostapd_data *
4248hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4249 const char *ifname)
4250{
4251 size_t i, j;
4252
4253 for (i = 0; i < interfaces->count; i++) {
4254 struct hostapd_iface *iface = interfaces->iface[i];
4255
4256 for (j = 0; j < iface->num_bss; j++) {
4257 struct hostapd_data *hapd;
4258
4259 hapd = iface->bss[j];
4260 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4261 return hapd;
4262 }
4263 }
4264
4265 return NULL;
4266}
4267
4268
4269static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4270 struct hostapd_data *dst_hapd,
4271 const char *param)
4272{
4273 int res;
4274 char *value;
4275
4276 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4277 if (!value) {
4278 wpa_printf(MSG_ERROR,
4279 "DUP: cannot allocate buffer to stringify %s",
4280 param);
4281 goto error_return;
4282 }
4283
4284 if (os_strcmp(param, "wpa") == 0) {
4285 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4286 src_hapd->conf->wpa);
4287 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4288 src_hapd->conf->wpa_key_mgmt) {
4289 res = hostapd_ctrl_iface_get_key_mgmt(
4290 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4291 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4292 goto error_stringify;
4293 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4294 src_hapd->conf->wpa_pairwise) {
4295 res = wpa_write_ciphers(value,
4296 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4297 src_hapd->conf->wpa_pairwise, " ");
4298 if (res < 0)
4299 goto error_stringify;
4300 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4301 src_hapd->conf->rsn_pairwise) {
4302 res = wpa_write_ciphers(value,
4303 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4304 src_hapd->conf->rsn_pairwise, " ");
4305 if (res < 0)
4306 goto error_stringify;
4307 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4308 src_hapd->conf->ssid.wpa_passphrase) {
4309 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4310 src_hapd->conf->ssid.wpa_passphrase);
4311 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4312 src_hapd->conf->ssid.wpa_psk_set) {
4313 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4314 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4315 } else {
4316 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4317 goto error_return;
4318 }
4319
4320 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4321 os_free(value);
4322 return res;
4323
4324error_stringify:
4325 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4326error_return:
4327 os_free(value);
4328 return -1;
4329}
4330
4331
4332static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004333hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4334 const char *input,
4335 char *reply, int reply_size)
4336{
4337 size_t i, j;
4338 int res;
4339 char *pos, *end;
4340 struct hostapd_iface *iface;
4341 int show_ctrl = 0;
4342
4343 if (input)
4344 show_ctrl = !!os_strstr(input, "ctrl");
4345
4346 pos = reply;
4347 end = reply + reply_size;
4348
4349 for (i = 0; i < interfaces->count; i++) {
4350 iface = interfaces->iface[i];
4351
4352 for (j = 0; j < iface->num_bss; j++) {
4353 struct hostapd_bss_config *conf;
4354
4355 conf = iface->conf->bss[j];
4356 if (show_ctrl)
4357 res = os_snprintf(pos, end - pos,
4358 "%s ctrl_iface=%s\n",
4359 conf->iface,
4360 conf->ctrl_interface ?
4361 conf->ctrl_interface : "N/A");
4362 else
4363 res = os_snprintf(pos, end - pos, "%s\n",
4364 conf->iface);
4365 if (os_snprintf_error(end - pos, res)) {
4366 *pos = '\0';
4367 return pos - reply;
4368 }
4369 pos += res;
4370 }
4371 }
4372
4373 return pos - reply;
4374}
4375
4376
4377static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004378hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4379 char *cmd)
4380{
4381 char *p_start = cmd, *p_end;
4382 struct hostapd_data *src_hapd, *dst_hapd;
4383
4384 /* cmd: "<src ifname> <dst ifname> <variable name> */
4385
4386 p_end = os_strchr(p_start, ' ');
4387 if (!p_end) {
4388 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4389 cmd);
4390 return -1;
4391 }
4392
4393 *p_end = '\0';
4394 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4395 if (!src_hapd) {
4396 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4397 p_start);
4398 return -1;
4399 }
4400
4401 p_start = p_end + 1;
4402 p_end = os_strchr(p_start, ' ');
4403 if (!p_end) {
4404 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4405 cmd);
4406 return -1;
4407 }
4408
4409 *p_end = '\0';
4410 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4411 if (!dst_hapd) {
4412 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4413 p_start);
4414 return -1;
4415 }
4416
4417 p_start = p_end + 1;
4418 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4419}
4420
4421
4422static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4423 const char *ifname,
4424 char *buf, char *reply,
4425 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004426 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004427 socklen_t fromlen)
4428{
4429 struct hostapd_data *hapd;
4430
4431 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4432 if (hapd == NULL) {
4433 int res;
4434
4435 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4436 if (os_snprintf_error(reply_size, res))
4437 return -1;
4438 return res;
4439 }
4440
4441 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4442 from, fromlen);
4443}
4444
4445
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004446static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4447 void *sock_ctx)
4448{
Hai Shalomfdcde762020-04-02 11:19:20 -07004449 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004450 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004451 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004452 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004453 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004454 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004455 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004456 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004457#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004458 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004459#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004460
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004461 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004462 (struct sockaddr *) &from, &fromlen);
4463 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004464 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4465 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004466 return;
4467 }
4468 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004469 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004470
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004471 reply = os_malloc(reply_size);
4472 if (reply == NULL) {
4473 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4474 fromlen) < 0) {
4475 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4476 strerror(errno));
4477 }
4478 return;
4479 }
4480
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004481 os_memcpy(reply, "OK\n", 3);
4482 reply_len = 3;
4483
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004484#ifdef CONFIG_CTRL_IFACE_UDP
4485 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4486 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004487 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4488 interfaces->ctrl_iface_cookie,
4489 CTRL_IFACE_COOKIE_LEN);
4490 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004491 goto send_reply;
4492 }
4493
4494 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004495 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004496 wpa_printf(MSG_DEBUG,
4497 "CTRL: No cookie in the request - drop request");
4498 os_free(reply);
4499 return;
4500 }
4501
Hai Shalomfdcde762020-04-02 11:19:20 -07004502 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4503 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004504 wpa_printf(MSG_DEBUG,
4505 "CTRL: Invalid cookie in the request - drop request");
4506 os_free(reply);
4507 return;
4508 }
4509
Hai Shalomfdcde762020-04-02 11:19:20 -07004510 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004511 while (*buf == ' ')
4512 buf++;
4513#endif /* CONFIG_CTRL_IFACE_UDP */
4514
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004515 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4516 char *pos = os_strchr(buf + 7, ' ');
4517
4518 if (pos) {
4519 *pos++ = '\0';
4520 reply_len = hostapd_global_ctrl_iface_ifname(
4521 interfaces, buf + 7, pos, reply, reply_size,
4522 &from, fromlen);
4523 goto send_reply;
4524 }
4525 }
4526
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004527 if (os_strcmp(buf, "PING") == 0) {
4528 os_memcpy(reply, "PONG\n", 5);
4529 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004530 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4531 if (wpa_debug_reopen_file() < 0)
4532 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004533 } else if (os_strcmp(buf, "FLUSH") == 0) {
4534 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004535 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4536 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4537 reply_len = -1;
4538 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4539 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4540 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004541 } else if (os_strcmp(buf, "ATTACH") == 0) {
4542 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004543 fromlen, NULL))
4544 reply_len = -1;
4545 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4546 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4547 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004548 reply_len = -1;
4549 } else if (os_strcmp(buf, "DETACH") == 0) {
4550 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4551 fromlen))
4552 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004553#ifdef CONFIG_MODULE_TESTS
4554 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004555 if (hapd_module_tests() < 0)
4556 reply_len = -1;
4557#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004558#ifdef CONFIG_FST
4559 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4560 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4561 reply_len = os_snprintf(reply, reply_size, "OK\n");
4562 else
4563 reply_len = -1;
4564 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4565 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4566 reply_len = os_snprintf(reply, reply_size, "OK\n");
4567 else
4568 reply_len = -1;
4569 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4570 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4571#endif /* CONFIG_FST */
4572 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4573 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4574 buf + 12))
4575 reply_len = os_snprintf(reply, reply_size, "OK\n");
4576 else
4577 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004578 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4579 reply_len = hostapd_global_ctrl_iface_interfaces(
4580 interfaces, buf + 10, reply, sizeof(buffer));
4581 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4582 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004583 } else {
4584 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4585 "ignored");
4586 reply_len = -1;
4587 }
4588
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004589send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004590 if (reply_len < 0) {
4591 os_memcpy(reply, "FAIL\n", 5);
4592 reply_len = 5;
4593 }
4594
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004595 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4596 fromlen) < 0) {
4597 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4598 strerror(errno));
4599 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004600 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004601}
4602
4603
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004604#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004605static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4606{
4607 char *buf;
4608 size_t len;
4609
4610 if (interface->global_iface_path == NULL)
4611 return NULL;
4612
4613 len = os_strlen(interface->global_iface_path) +
4614 os_strlen(interface->global_iface_name) + 2;
4615 buf = os_malloc(len);
4616 if (buf == NULL)
4617 return NULL;
4618
4619 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4620 interface->global_iface_name);
4621 buf[len - 1] = '\0';
4622 return buf;
4623}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004624#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004625
4626
4627int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4628{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004629#ifdef CONFIG_CTRL_IFACE_UDP
4630 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4631 char p[32] = { 0 };
4632 char *pos;
4633 struct addrinfo hints = { 0 }, *res, *saveres;
4634 int n;
4635
4636 if (interface->global_ctrl_sock > -1) {
4637 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4638 return 0;
4639 }
4640
4641 if (interface->global_iface_path == NULL)
4642 return 0;
4643
4644 pos = os_strstr(interface->global_iface_path, "udp:");
4645 if (pos) {
4646 pos += 4;
4647 port = atoi(pos);
4648 if (port <= 0) {
4649 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4650 goto fail;
4651 }
4652 }
4653
Hai Shalomfdcde762020-04-02 11:19:20 -07004654 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004655
4656#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4657 hints.ai_flags = AI_PASSIVE;
4658#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4659
4660#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4661 hints.ai_family = AF_INET6;
4662#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4663 hints.ai_family = AF_INET;
4664#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4665 hints.ai_socktype = SOCK_DGRAM;
4666
4667try_again:
4668 os_snprintf(p, sizeof(p), "%d", port);
4669 n = getaddrinfo(NULL, p, &hints, &res);
4670 if (n) {
4671 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4672 goto fail;
4673 }
4674
4675 saveres = res;
4676 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4677 res->ai_protocol);
4678 if (interface->global_ctrl_sock < 0) {
4679 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4680 goto fail;
4681 }
4682
4683 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4684 0) {
4685 port++;
4686 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4687 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4688 goto try_again;
4689 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4690 goto fail;
4691 }
4692
4693 freeaddrinfo(saveres);
4694
4695 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4696
4697 if (eloop_register_read_sock(interface->global_ctrl_sock,
4698 hostapd_global_ctrl_iface_receive,
4699 interface, NULL) < 0) {
4700 hostapd_global_ctrl_iface_deinit(interface);
4701 return -1;
4702 }
4703
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004704 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4705
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004706 return 0;
4707
4708fail:
4709 if (interface->global_ctrl_sock >= 0)
4710 close(interface->global_ctrl_sock);
4711 return -1;
4712#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004713 struct sockaddr_un addr;
4714 int s = -1;
4715 char *fname = NULL;
4716
4717 if (interface->global_iface_path == NULL) {
4718 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4719 return 0;
4720 }
4721
4722 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4723 if (errno == EEXIST) {
4724 wpa_printf(MSG_DEBUG, "Using existing control "
4725 "interface directory.");
4726 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004727 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4728 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004729 goto fail;
4730 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004731 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004732 lchown(interface->global_iface_path, -1,
4733 interface->ctrl_iface_group) < 0) {
4734 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004735 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004736 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004737 }
4738
4739 if (os_strlen(interface->global_iface_path) + 1 +
4740 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4741 goto fail;
4742
4743 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4744 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004745 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004746 goto fail;
4747 }
4748
4749 os_memset(&addr, 0, sizeof(addr));
4750#ifdef __FreeBSD__
4751 addr.sun_len = sizeof(addr);
4752#endif /* __FreeBSD__ */
4753 addr.sun_family = AF_UNIX;
4754 fname = hostapd_global_ctrl_iface_path(interface);
4755 if (fname == NULL)
4756 goto fail;
4757 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4758 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4759 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4760 strerror(errno));
4761 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4762 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4763 " allow connections - assuming it was left"
4764 "over from forced program termination");
4765 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004766 wpa_printf(MSG_ERROR,
4767 "Could not unlink existing ctrl_iface socket '%s': %s",
4768 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004769 goto fail;
4770 }
4771 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4772 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004773 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4774 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004775 goto fail;
4776 }
4777 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4778 "ctrl_iface socket '%s'", fname);
4779 } else {
4780 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4781 "be in use - cannot override it");
4782 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4783 "not used anymore", fname);
4784 os_free(fname);
4785 fname = NULL;
4786 goto fail;
4787 }
4788 }
4789
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004790 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004791 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4792 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004793 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004794 goto fail;
4795 }
4796
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004797 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004798 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4799 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004800 goto fail;
4801 }
4802 os_free(fname);
4803
4804 interface->global_ctrl_sock = s;
4805 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4806 interface, NULL);
4807
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004808 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4809
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004810 return 0;
4811
4812fail:
4813 if (s >= 0)
4814 close(s);
4815 if (fname) {
4816 unlink(fname);
4817 os_free(fname);
4818 }
4819 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004820#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004821}
4822
4823
4824void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4825{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004826#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004827 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004828#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004829 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004830
4831 if (interfaces->global_ctrl_sock > -1) {
4832 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4833 close(interfaces->global_ctrl_sock);
4834 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004835#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004836 fname = hostapd_global_ctrl_iface_path(interfaces);
4837 if (fname) {
4838 unlink(fname);
4839 os_free(fname);
4840 }
4841
4842 if (interfaces->global_iface_path &&
4843 rmdir(interfaces->global_iface_path) < 0) {
4844 if (errno == ENOTEMPTY) {
4845 wpa_printf(MSG_DEBUG, "Control interface "
4846 "directory not empty - leaving it "
4847 "behind");
4848 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004849 wpa_printf(MSG_ERROR,
4850 "rmdir[ctrl_interface=%s]: %s",
4851 interfaces->global_iface_path,
4852 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004853 }
4854 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004855#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004856 }
4857
4858 os_free(interfaces->global_iface_path);
4859 interfaces->global_iface_path = NULL;
4860
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004861 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4862 struct wpa_ctrl_dst, list)
4863 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004864}
4865
4866
Roshan Pius3a1667e2018-07-03 15:17:14 -07004867static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4868 const char *buf)
4869{
4870 /* Enable Probe Request events based on explicit request.
4871 * Other events are enabled by default.
4872 */
4873 if (str_starts(buf, RX_PROBE_REQUEST))
4874 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4875 return 1;
4876}
4877
4878
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004879static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
4880 const char *ifname, int level,
4881 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882{
4883 struct wpa_ctrl_dst *dst, *next;
4884 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004885 int idx, res;
4886 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 char levelstr[10];
4888
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004889 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004890 return;
4891
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004892 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4893 if (os_snprintf_error(sizeof(levelstr), res))
4894 return;
4895 idx = 0;
4896 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004897 io[idx].iov_base = "IFNAME=";
4898 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004899 idx++;
4900 io[idx].iov_base = (char *) ifname;
4901 io[idx].iov_len = os_strlen(ifname);
4902 idx++;
4903 io[idx].iov_base = " ";
4904 io[idx].iov_len = 1;
4905 idx++;
4906 }
4907 io[idx].iov_base = levelstr;
4908 io[idx].iov_len = os_strlen(levelstr);
4909 idx++;
4910 io[idx].iov_base = (char *) buf;
4911 io[idx].iov_len = len;
4912 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913 os_memset(&msg, 0, sizeof(msg));
4914 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004915 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916
4917 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004918 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004919 if ((level >= dst->debug_level) &&
4920 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004921 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4922 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923 msg.msg_name = &dst->addr;
4924 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004925 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 int _errno = errno;
4927 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4928 "%d - %s",
4929 idx, errno, strerror(errno));
4930 dst->errors++;
4931 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004932 ctrl_iface_detach(ctrl_dst,
4933 &dst->addr,
4934 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 }
4936 } else
4937 dst->errors = 0;
4938 }
4939 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 }
4941}
4942
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004943
4944static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
4945 enum wpa_msg_type type,
4946 const char *buf, size_t len)
4947{
4948 if (type != WPA_MSG_NO_GLOBAL) {
4949 hostapd_ctrl_iface_send_internal(
4950 hapd->iface->interfaces->global_ctrl_sock,
4951 &hapd->iface->interfaces->global_ctrl_dst,
4952 type != WPA_MSG_PER_INTERFACE ?
4953 NULL : hapd->conf->iface,
4954 level, buf, len);
4955 }
4956
4957 if (type != WPA_MSG_ONLY_GLOBAL) {
4958 hostapd_ctrl_iface_send_internal(
4959 hapd->ctrl_sock, &hapd->ctrl_dst,
4960 NULL, level, buf, len);
4961 }
4962}
4963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964#endif /* CONFIG_NATIVE_WINDOWS */