blob: 7fb3fd397bd9eec6f8534e05c676e03bf6a49527 [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 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700864 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
865 ret = os_snprintf(pos, end - pos, "FT-SAE-EXT-KEY ");
866 if (os_snprintf_error(end - pos, ret))
867 return pos - buf;
868 pos += ret;
869 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800870#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800871#ifdef CONFIG_FILS
872 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
873 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
874 if (os_snprintf_error(end - pos, ret))
875 return pos - buf;
876 pos += ret;
877 }
878 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
879 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
880 if (os_snprintf_error(end - pos, ret))
881 return pos - buf;
882 pos += ret;
883 }
884#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800885#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800886 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
887 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
888 if (os_snprintf_error(end - pos, ret))
889 return pos - buf;
890 pos += ret;
891 }
892 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
893 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
894 if (os_snprintf_error(end - pos, ret))
895 return pos - buf;
896 pos += ret;
897 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800898#ifdef CONFIG_SAE
899 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
900 ret = os_snprintf(pos, end - pos, "SAE ");
901 if (os_snprintf_error(end - pos, ret))
902 return pos - buf;
903 pos += ret;
904 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700905 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
906 ret = os_snprintf(pos, end - pos, "SAE-EXT-KEY ");
907 if (os_snprintf_error(end - pos, ret))
908 return pos - buf;
909 pos += ret;
910 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800911#endif /* CONFIG_SAE */
912 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
913 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
914 if (os_snprintf_error(end - pos, ret))
915 return pos - buf;
916 pos += ret;
917 }
918 if (hapd->conf->wpa_key_mgmt &
919 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
920 ret = os_snprintf(pos, end - pos,
921 "WPA-EAP-SUITE-B-192 ");
922 if (os_snprintf_error(end - pos, ret))
923 return pos - buf;
924 pos += ret;
925 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800926#ifdef CONFIG_FILS
927 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
928 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
929 if (os_snprintf_error(end - pos, ret))
930 return pos - buf;
931 pos += ret;
932 }
933 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
934 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
935 if (os_snprintf_error(end - pos, ret))
936 return pos - buf;
937 pos += ret;
938 }
939#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800940
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700941#ifdef CONFIG_OWE
942 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
943 ret = os_snprintf(pos, end - pos, "OWE ");
944 if (os_snprintf_error(end - pos, ret))
945 return pos - buf;
946 pos += ret;
947 }
948#endif /* CONFIG_OWE */
949
950#ifdef CONFIG_DPP
951 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
952 ret = os_snprintf(pos, end - pos, "DPP ");
953 if (os_snprintf_error(end - pos, ret))
954 return pos - buf;
955 pos += ret;
956 }
957#endif /* CONFIG_DPP */
958
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800959 if (pos > buf && *(pos - 1) == ' ') {
960 *(pos - 1) = '\0';
961 pos--;
962 }
963
964 return pos - buf;
965}
966
967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
969 char *buf, size_t buflen)
970{
971 int ret;
972 char *pos, *end;
973
974 pos = buf;
975 end = buf + buflen;
976
977 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
978 "ssid=%s\n",
979 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 wpa_ssid_txt(hapd->conf->ssid.ssid,
981 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800982 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 return pos - buf;
984 pos += ret;
985
986#ifdef CONFIG_WPS
987 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
988 hapd->conf->wps_state == 0 ? "disabled" :
989 (hapd->conf->wps_state == 1 ? "not configured" :
990 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800991 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 return pos - buf;
993 pos += ret;
994
995 if (hapd->conf->wps_state && hapd->conf->wpa &&
996 hapd->conf->ssid.wpa_passphrase) {
997 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
998 hapd->conf->ssid.wpa_passphrase);
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 }
1003
1004 if (hapd->conf->wps_state && hapd->conf->wpa &&
1005 hapd->conf->ssid.wpa_psk &&
1006 hapd->conf->ssid.wpa_psk->group) {
1007 char hex[PMK_LEN * 2 + 1];
1008 wpa_snprintf_hex(hex, sizeof(hex),
1009 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1010 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001011 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012 return pos - buf;
1013 pos += ret;
1014 }
Hai Shalom60840252021-02-19 19:02:11 -08001015
1016 if (hapd->conf->multi_ap) {
1017 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1018
1019 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1020 hapd->conf->multi_ap);
1021 if (os_snprintf_error(end - pos, ret))
1022 return pos - buf;
1023 pos += ret;
1024
1025 if (ssid->ssid_len) {
1026 ret = os_snprintf(pos, end - pos,
1027 "multi_ap_backhaul_ssid=%s\n",
1028 wpa_ssid_txt(ssid->ssid,
1029 ssid->ssid_len));
1030 if (os_snprintf_error(end - pos, ret))
1031 return pos - buf;
1032 pos += ret;
1033 }
1034
1035 if (hapd->conf->wps_state && hapd->conf->wpa &&
1036 ssid->wpa_passphrase) {
1037 ret = os_snprintf(pos, end - pos,
1038 "multi_ap_backhaul_wpa_passphrase=%s\n",
1039 ssid->wpa_passphrase);
1040 if (os_snprintf_error(end - pos, ret))
1041 return pos - buf;
1042 pos += ret;
1043 }
1044
1045 if (hapd->conf->wps_state && hapd->conf->wpa &&
1046 ssid->wpa_psk &&
1047 ssid->wpa_psk->group) {
1048 char hex[PMK_LEN * 2 + 1];
1049
1050 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1051 PMK_LEN);
1052 ret = os_snprintf(pos, end - pos,
1053 "multi_ap_backhaul_wpa_psk=%s\n",
1054 hex);
1055 forced_memzero(hex, sizeof(hex));
1056 if (os_snprintf_error(end - pos, ret))
1057 return pos - buf;
1058 pos += ret;
1059 }
1060 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061#endif /* CONFIG_WPS */
1062
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001063 if (hapd->conf->wpa) {
1064 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1065 if (os_snprintf_error(end - pos, ret))
1066 return pos - buf;
1067 pos += ret;
1068 }
1069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1071 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001072 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 return pos - buf;
1074 pos += ret;
1075
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001076 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077
1078 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001079 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 return pos - buf;
1081 pos += ret;
1082 }
1083
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001084 if (hapd->conf->wpa) {
1085 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1086 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001087 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 return pos - buf;
1089 pos += ret;
1090 }
1091
1092 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1093 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001094 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 return pos - buf;
1096 pos += ret;
1097
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001098 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1099 " ");
1100 if (ret < 0)
1101 return pos - buf;
1102 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103
1104 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001105 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 return pos - buf;
1107 pos += ret;
1108 }
1109
1110 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1111 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001112 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 return pos - buf;
1114 pos += ret;
1115
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001116 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001117 " ");
1118 if (ret < 0)
1119 return pos - buf;
1120 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121
1122 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001123 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 return pos - buf;
1125 pos += ret;
1126 }
1127
Hai Shalomfdcde762020-04-02 11:19:20 -07001128 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1129 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1130 hapd->conf->wpa_deny_ptk0_rekey);
1131 if (os_snprintf_error(end - pos, ret))
1132 return pos - buf;
1133 pos += ret;
1134 }
1135
1136 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1137 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1138 hapd->conf->extended_key_id);
1139 if (os_snprintf_error(end - pos, ret))
1140 return pos - buf;
1141 pos += ret;
1142 }
1143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 return pos - buf;
1145}
1146
1147
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001148static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
Hai Shalom60840252021-02-19 19:02:11 -08001149 const char *bands)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001150{
1151 union wpa_event_data event;
Hai Shalom60840252021-02-19 19:02:11 -08001152 u32 setband_mask = WPA_SETBAND_AUTO;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001153
Hai Shalom60840252021-02-19 19:02:11 -08001154 /*
1155 * For example:
1156 * SET setband 2G,6G
1157 * SET setband 5G
1158 * SET setband AUTO
1159 */
1160 if (!os_strstr(bands, "AUTO")) {
1161 if (os_strstr(bands, "5G"))
1162 setband_mask |= WPA_SETBAND_5G;
1163 if (os_strstr(bands, "6G"))
1164 setband_mask |= WPA_SETBAND_6G;
1165 if (os_strstr(bands, "2G"))
1166 setband_mask |= WPA_SETBAND_2G;
1167 if (setband_mask == WPA_SETBAND_AUTO)
1168 return -1;
1169 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001170
Hai Shalom60840252021-02-19 19:02:11 -08001171 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001172 os_memset(&event, 0, sizeof(event));
1173 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1174 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1175 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1176 }
1177
1178 return 0;
1179}
1180
1181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1183{
1184 char *value;
1185 int ret = 0;
1186
1187 value = os_strchr(cmd, ' ');
1188 if (value == NULL)
1189 return -1;
1190 *value++ = '\0';
1191
1192 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1193 if (0) {
1194#ifdef CONFIG_WPS_TESTING
1195 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1196 long int val;
1197 val = strtol(value, NULL, 0);
1198 if (val < 0 || val > 0xff) {
1199 ret = -1;
1200 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1201 "wps_version_number %ld", val);
1202 } else {
1203 wps_version_number = val;
1204 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1205 "version %u.%u",
1206 (wps_version_number & 0xf0) >> 4,
1207 wps_version_number & 0x0f);
1208 hostapd_wps_update_ie(hapd);
1209 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001210 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1211 wps_testing_stub_cred = atoi(value);
1212 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1213 wps_testing_stub_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001214 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1215 wps_corrupt_pkhash = atoi(value);
1216 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1217 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001219#ifdef CONFIG_TESTING_OPTIONS
1220 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1221 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001222 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1223 hapd->ext_eapol_frame_io = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001224 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1225 hapd->force_backlog_bytes = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001226#ifdef CONFIG_DPP
1227 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1228 os_free(hapd->dpp_config_obj_override);
1229 hapd->dpp_config_obj_override = os_strdup(value);
1230 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1231 os_free(hapd->dpp_discovery_override);
1232 hapd->dpp_discovery_override = os_strdup(value);
1233 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1234 os_free(hapd->dpp_groups_override);
1235 hapd->dpp_groups_override = os_strdup(value);
1236 } else if (os_strcasecmp(cmd,
1237 "dpp_ignore_netaccesskey_mismatch") == 0) {
1238 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001239 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1240 dpp_test = atoi(value);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001241 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1242 dpp_version_override = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001243#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001244#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001245#ifdef CONFIG_MBO
1246 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1247 int val;
1248
1249 if (!hapd->conf->mbo_enabled)
1250 return -1;
1251
1252 val = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001253 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001254 return -1;
1255
1256 hapd->mbo_assoc_disallow = val;
1257 ieee802_11_update_beacons(hapd->iface);
1258
1259 /*
1260 * TODO: Need to configure drivers that do AP MLME offload with
1261 * disallowing station logic.
1262 */
1263#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001264#ifdef CONFIG_DPP
1265 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1266 os_free(hapd->dpp_configurator_params);
1267 hapd->dpp_configurator_params = os_strdup(value);
Sunil Ravia04bd252022-05-02 22:54:18 -07001268#ifdef CONFIG_DPP2
1269 dpp_controller_set_params(hapd->iface->interfaces->dpp, value);
1270#endif /* CONFIG_DPP2 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001271 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1272 hapd->dpp_init_max_tries = atoi(value);
1273 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1274 hapd->dpp_init_retry_time = atoi(value);
1275 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1276 hapd->dpp_resp_wait_time = atoi(value);
1277 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1278 hapd->dpp_resp_max_tries = atoi(value);
1279 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1280 hapd->dpp_resp_retry_time = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001281#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001282 } else if (os_strcasecmp(cmd, "setband") == 0) {
1283 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001285 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001286 if (ret)
1287 return ret;
1288
1289 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001290 hostapd_disassoc_deny_mac(hapd);
1291 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1292 hostapd_disassoc_accept_mac(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001293 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1294 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1295 hapd->parameter_set_count++;
1296 if (ieee802_11_update_beacons(hapd->iface))
1297 wpa_printf(MSG_DEBUG,
1298 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001299 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1300 os_strcmp(cmd, "sae_password") == 0 ||
1301 os_strcmp(cmd, "sae_pwe") == 0) {
1302 if (hapd->started)
1303 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001304 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1305 wpa_auth_set_transition_disable(hapd->wpa_auth,
1306 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001307 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001308
1309#ifdef CONFIG_TESTING_OPTIONS
1310 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1311 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1312 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001313 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1314 wpa_auth_set_ocv_override_freq(
1315 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1316 atoi(value));
1317 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1318 wpa_auth_set_ocv_override_freq(
1319 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1320 atoi(value));
1321 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1322 wpa_auth_set_ocv_override_freq(
1323 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1324 atoi(value));
1325 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1326 wpa_auth_set_ocv_override_freq(
1327 hapd->wpa_auth,
1328 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001329 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1330 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1331 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1332 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001333#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 }
1335
1336 return ret;
1337}
1338
1339
1340static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1341 char *buf, size_t buflen)
1342{
1343 int res;
1344
1345 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1346
1347 if (os_strcmp(cmd, "version") == 0) {
1348 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001349 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 return -1;
1351 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001352 } else if (os_strcmp(cmd, "tls_library") == 0) {
1353 res = tls_get_library_version(buf, buflen);
1354 if (os_snprintf_error(buflen, res))
1355 return -1;
1356 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 }
1358
1359 return -1;
1360}
1361
1362
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001363static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1364{
1365 if (hostapd_enable_iface(iface) < 0) {
1366 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1367 return -1;
1368 }
1369 return 0;
1370}
1371
1372
1373static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1374{
1375 if (hostapd_reload_iface(iface) < 0) {
1376 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1377 return -1;
1378 }
1379 return 0;
1380}
1381
1382
1383static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1384{
1385 if (hostapd_disable_iface(iface) < 0) {
1386 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1387 return -1;
1388 }
1389 return 0;
1390}
1391
1392
Hai Shalom74f70d42019-02-11 14:42:39 -08001393static int
1394hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1395 struct sta_info *sta, void *ctx)
1396{
1397 struct hostapd_wpa_psk *psk;
1398 const u8 *pmk;
1399 int pmk_len;
1400 int pmk_match;
1401 int sta_match;
1402 int bss_match;
1403 int reason;
1404
1405 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1406
1407 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1408 pmk_match = PMK_LEN == pmk_len &&
1409 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1410 sta_match = psk->group == 0 &&
1411 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1412 bss_match = psk->group == 1;
1413
1414 if (pmk_match && (sta_match || bss_match))
1415 return 0;
1416 }
1417
1418 wpa_printf(MSG_INFO, "STA " MACSTR
1419 " PSK/passphrase no longer valid - disconnect",
1420 MAC2STR(sta->addr));
1421 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1422 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1423 ap_sta_deauthenticate(hapd, sta, reason);
1424
1425 return 0;
1426}
1427
1428
1429static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1430{
1431 struct hostapd_bss_config *conf = hapd->conf;
1432 int err;
1433
1434 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1435
1436 err = hostapd_setup_wpa_psk(conf);
1437 if (err < 0) {
1438 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1439 err);
1440 return -1;
1441 }
1442
1443 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1444 NULL);
1445
1446 return 0;
1447}
1448
1449
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001450#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001451
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001452static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1453{
1454 union wpa_event_data data;
1455 char *pos, *param;
1456 enum wpa_event_type event;
1457
1458 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1459
1460 os_memset(&data, 0, sizeof(data));
1461
1462 param = os_strchr(cmd, ' ');
1463 if (param == NULL)
1464 return -1;
1465 *param++ = '\0';
1466
1467 if (os_strcmp(cmd, "DETECTED") == 0)
1468 event = EVENT_DFS_RADAR_DETECTED;
1469 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1470 event = EVENT_DFS_CAC_FINISHED;
1471 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1472 event = EVENT_DFS_CAC_ABORTED;
1473 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1474 event = EVENT_DFS_NOP_FINISHED;
1475 else {
1476 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1477 cmd);
1478 return -1;
1479 }
1480
1481 pos = os_strstr(param, "freq=");
1482 if (pos)
1483 data.dfs_event.freq = atoi(pos + 5);
1484
1485 pos = os_strstr(param, "ht_enabled=1");
1486 if (pos)
1487 data.dfs_event.ht_enabled = 1;
1488
1489 pos = os_strstr(param, "chan_offset=");
1490 if (pos)
1491 data.dfs_event.chan_offset = atoi(pos + 12);
1492
1493 pos = os_strstr(param, "chan_width=");
1494 if (pos)
1495 data.dfs_event.chan_width = atoi(pos + 11);
1496
1497 pos = os_strstr(param, "cf1=");
1498 if (pos)
1499 data.dfs_event.cf1 = atoi(pos + 4);
1500
1501 pos = os_strstr(param, "cf2=");
1502 if (pos)
1503 data.dfs_event.cf2 = atoi(pos + 4);
1504
1505 wpa_supplicant_event(hapd, event, &data);
1506
1507 return 0;
1508}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001509
1510
1511static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1512{
1513 size_t len;
1514 u8 *buf;
1515 int res;
1516
1517 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1518
1519 len = os_strlen(cmd);
1520 if (len & 1)
1521 return -1;
1522 len /= 2;
1523
1524 buf = os_malloc(len);
1525 if (buf == NULL)
1526 return -1;
1527
1528 if (hexstr2bin(cmd, buf, len) < 0) {
1529 os_free(buf);
1530 return -1;
1531 }
1532
Hai Shalomfdcde762020-04-02 11:19:20 -07001533 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001534 os_free(buf);
1535 return res;
1536}
1537
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001538
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001539static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1540 char *cmd)
1541{
1542 char *pos, *param;
1543 size_t len;
1544 u8 *buf;
1545 int stype = 0, ok = 0;
1546 union wpa_event_data event;
1547
1548 if (!hapd->ext_mgmt_frame_handling)
1549 return -1;
1550
1551 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1552
1553 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1554
1555 pos = cmd;
1556 param = os_strstr(pos, "stype=");
1557 if (param) {
1558 param += 6;
1559 stype = atoi(param);
1560 }
1561
1562 param = os_strstr(pos, " ok=");
1563 if (param) {
1564 param += 4;
1565 ok = atoi(param);
1566 }
1567
1568 param = os_strstr(pos, " buf=");
1569 if (!param)
1570 return -1;
1571 param += 5;
1572
1573 len = os_strlen(param);
1574 if (len & 1)
1575 return -1;
1576 len /= 2;
1577
1578 buf = os_malloc(len);
1579 if (!buf || hexstr2bin(param, buf, len) < 0) {
1580 os_free(buf);
1581 return -1;
1582 }
1583
1584 os_memset(&event, 0, sizeof(event));
1585 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1586 event.tx_status.data = buf;
1587 event.tx_status.data_len = len;
1588 event.tx_status.stype = stype;
1589 event.tx_status.ack = ok;
1590 hapd->ext_mgmt_frame_handling = 0;
1591 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1592 hapd->ext_mgmt_frame_handling = 1;
1593
1594 os_free(buf);
1595
1596 return 0;
1597}
1598
1599
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001600static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1601 char *cmd)
1602{
1603 char *pos, *param;
1604 size_t len;
1605 u8 *buf;
1606 int freq = 0, datarate = 0, ssi_signal = 0;
1607 union wpa_event_data event;
1608
1609 if (!hapd->ext_mgmt_frame_handling)
1610 return -1;
1611
1612 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1613
1614 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1615
1616 pos = cmd;
1617 param = os_strstr(pos, "freq=");
1618 if (param) {
1619 param += 5;
1620 freq = atoi(param);
1621 }
1622
1623 param = os_strstr(pos, " datarate=");
1624 if (param) {
1625 param += 10;
1626 datarate = atoi(param);
1627 }
1628
1629 param = os_strstr(pos, " ssi_signal=");
1630 if (param) {
1631 param += 12;
1632 ssi_signal = atoi(param);
1633 }
1634
1635 param = os_strstr(pos, " frame=");
1636 if (param == NULL)
1637 return -1;
1638 param += 7;
1639
1640 len = os_strlen(param);
1641 if (len & 1)
1642 return -1;
1643 len /= 2;
1644
1645 buf = os_malloc(len);
1646 if (buf == NULL)
1647 return -1;
1648
1649 if (hexstr2bin(param, buf, len) < 0) {
1650 os_free(buf);
1651 return -1;
1652 }
1653
1654 os_memset(&event, 0, sizeof(event));
1655 event.rx_mgmt.freq = freq;
1656 event.rx_mgmt.frame = buf;
1657 event.rx_mgmt.frame_len = len;
1658 event.rx_mgmt.ssi_signal = ssi_signal;
1659 event.rx_mgmt.datarate = datarate;
1660 hapd->ext_mgmt_frame_handling = 0;
1661 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1662 hapd->ext_mgmt_frame_handling = 1;
1663
1664 os_free(buf);
1665
1666 return 0;
1667}
1668
1669
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001670static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1671{
1672 char *pos;
1673 u8 src[ETH_ALEN], *buf;
1674 int used;
1675 size_t len;
1676
1677 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1678
1679 pos = cmd;
1680 used = hwaddr_aton2(pos, src);
1681 if (used < 0)
1682 return -1;
1683 pos += used;
1684 while (*pos == ' ')
1685 pos++;
1686
1687 len = os_strlen(pos);
1688 if (len & 1)
1689 return -1;
1690 len /= 2;
1691
1692 buf = os_malloc(len);
1693 if (buf == NULL)
1694 return -1;
1695
1696 if (hexstr2bin(pos, buf, len) < 0) {
1697 os_free(buf);
1698 return -1;
1699 }
1700
Sunil8cd6f4d2022-06-28 18:40:46 +00001701 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001702 os_free(buf);
1703
1704 return 0;
1705}
1706
1707
Hai Shaloma20dcd72022-02-04 13:43:00 -08001708static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1709{
1710 char *pos, *pos2;
1711 u8 dst[ETH_ALEN], *buf;
1712 int used, ret;
1713 size_t len;
1714 unsigned int prev;
1715 int encrypt = 0;
1716
1717 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1718
1719 pos = cmd;
1720 used = hwaddr_aton2(pos, dst);
1721 if (used < 0)
1722 return -1;
1723 pos += used;
1724 while (*pos == ' ')
1725 pos++;
1726
1727 pos2 = os_strchr(pos, ' ');
1728 if (pos2) {
1729 len = pos2 - pos;
1730 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1731 } else {
1732 len = os_strlen(pos);
1733 }
1734 if (len & 1)
1735 return -1;
1736 len /= 2;
1737
1738 buf = os_malloc(len);
1739 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1740 os_free(buf);
1741 return -1;
1742 }
1743
1744 prev = hapd->ext_eapol_frame_io;
1745 hapd->ext_eapol_frame_io = 0;
1746 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1747 hapd->ext_eapol_frame_io = prev;
1748 os_free(buf);
1749
1750 return ret;
1751}
1752
1753
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001754static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1755{
1756 size_t i;
1757 u32 sum = 0;
1758 const u16 *pos = buf;
1759
1760 for (i = 0; i < len / 2; i++)
1761 sum += *pos++;
1762
1763 while (sum >> 16)
1764 sum = (sum & 0xffff) + (sum >> 16);
1765
1766 return sum ^ 0xffff;
1767}
1768
1769
1770#define HWSIM_PACKETLEN 1500
1771#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1772
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001773static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1774 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001775{
1776 struct hostapd_data *hapd = ctx;
1777 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001778 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001779 const u8 *pos;
1780 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001781 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001782
Hai Shalom81f62d82019-07-22 12:10:00 -07001783 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1784 wpa_printf(MSG_DEBUG,
1785 "test data: RX - ignore unexpected length %d",
1786 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001787 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001788 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001789
1790 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001791 os_memcpy(&ip, eth + 1, sizeof(ip));
1792 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001793
Hai Shalomfdcde762020-04-02 11:19:20 -07001794 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1795 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001796 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07001797 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001798 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001799 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001800
Hai Shalomfdcde762020-04-02 11:19:20 -07001801 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001802 if (*pos != (u8) i) {
1803 wpa_printf(MSG_DEBUG,
1804 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001805 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001806 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001807 pos++;
1808 }
1809
Hai Shalom81f62d82019-07-22 12:10:00 -07001810 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001811 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1812 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001813 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1814 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001815}
1816
1817
1818static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1819 char *cmd)
1820{
1821 int enabled = atoi(cmd);
1822 char *pos;
1823 const char *ifname;
1824
1825 if (!enabled) {
1826 if (hapd->l2_test) {
1827 l2_packet_deinit(hapd->l2_test);
1828 hapd->l2_test = NULL;
1829 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1830 "test data: Disabled");
1831 }
1832 return 0;
1833 }
1834
1835 if (hapd->l2_test)
1836 return 0;
1837
1838 pos = os_strstr(cmd, " ifname=");
1839 if (pos)
1840 ifname = pos + 8;
1841 else
1842 ifname = hapd->conf->iface;
1843
1844 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1845 ETHERTYPE_IP, hostapd_data_test_rx,
1846 hapd, 1);
1847 if (hapd->l2_test == NULL)
1848 return -1;
1849
1850 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1851
1852 return 0;
1853}
1854
1855
1856static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1857{
1858 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001859 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001860 int used;
1861 long int val;
1862 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001863 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001864 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001865 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001866 u8 *dpos;
1867 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001868 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001869
1870 if (hapd->l2_test == NULL)
1871 return -1;
1872
Hai Shalom81f62d82019-07-22 12:10:00 -07001873 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001874
1875 pos = cmd;
1876 used = hwaddr_aton2(pos, dst);
1877 if (used < 0)
1878 return -1;
1879 pos += used;
1880 while (*pos == ' ')
1881 pos++;
1882 used = hwaddr_aton2(pos, src);
1883 if (used < 0)
1884 return -1;
1885 pos += used;
1886
Hai Shalom81f62d82019-07-22 12:10:00 -07001887 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001888 if (val < 0 || val > 0xff)
1889 return -1;
1890 tos = val;
1891
Hai Shalom81f62d82019-07-22 12:10:00 -07001892 pos = os_strstr(pos2, " len=");
1893 if (pos) {
1894 i = atoi(pos + 5);
1895 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
1896 return -1;
1897 send_len = i;
1898 }
1899
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001900 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001901 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1902 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1903 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07001904 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001905 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07001906 ip->ip_hl = 5;
1907 ip->ip_v = 4;
1908 ip->ip_ttl = 64;
1909 ip->ip_tos = tos;
1910 ip->ip_len = htons(send_len);
1911 ip->ip_p = 1;
1912 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1913 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1914 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001915 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07001916 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001917 *dpos++ = i;
1918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001919 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07001920 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001921 return -1;
1922
1923 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1924 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1925
1926 return 0;
1927}
1928
1929
1930static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1931 char *cmd)
1932{
1933 u8 *buf;
1934 struct ether_header *eth;
1935 struct l2_packet_data *l2 = NULL;
1936 size_t len;
1937 u16 ethertype;
1938 int res = -1;
1939 const char *ifname = hapd->conf->iface;
1940
1941 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1942 cmd += 7;
1943 ifname = cmd;
1944 cmd = os_strchr(cmd, ' ');
1945 if (cmd == NULL)
1946 return -1;
1947 *cmd++ = '\0';
1948 }
1949
1950 len = os_strlen(cmd);
1951 if (len & 1 || len < ETH_HLEN * 2)
1952 return -1;
1953 len /= 2;
1954
1955 buf = os_malloc(len);
1956 if (buf == NULL)
1957 return -1;
1958
1959 if (hexstr2bin(cmd, buf, len) < 0)
1960 goto done;
1961
1962 eth = (struct ether_header *) buf;
1963 ethertype = ntohs(eth->ether_type);
1964
1965 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1966 hostapd_data_test_rx, hapd, 1);
1967 if (l2 == NULL)
1968 goto done;
1969
1970 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1971 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1972done:
1973 if (l2)
1974 l2_packet_deinit(l2);
1975 os_free(buf);
1976
1977 return res < 0 ? -1 : 0;
1978}
1979
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001980
1981static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1982{
1983#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001984 char *pos;
1985
1986 wpa_trace_fail_after = atoi(cmd);
1987 pos = os_strchr(cmd, ':');
1988 if (pos) {
1989 pos++;
1990 os_strlcpy(wpa_trace_fail_func, pos,
1991 sizeof(wpa_trace_fail_func));
1992 } else {
1993 wpa_trace_fail_after = 0;
1994 }
1995
1996 return 0;
1997#else /* WPA_TRACE_BFD */
1998 return -1;
1999#endif /* WPA_TRACE_BFD */
2000}
2001
2002
2003static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2004 char *buf, size_t buflen)
2005{
2006#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002007 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2008 wpa_trace_fail_func);
2009#else /* WPA_TRACE_BFD */
2010 return -1;
2011#endif /* WPA_TRACE_BFD */
2012}
2013
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002014
2015static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2016{
2017#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002018 char *pos;
2019
2020 wpa_trace_test_fail_after = atoi(cmd);
2021 pos = os_strchr(cmd, ':');
2022 if (pos) {
2023 pos++;
2024 os_strlcpy(wpa_trace_test_fail_func, pos,
2025 sizeof(wpa_trace_test_fail_func));
2026 } else {
2027 wpa_trace_test_fail_after = 0;
2028 }
2029
2030 return 0;
2031#else /* WPA_TRACE_BFD */
2032 return -1;
2033#endif /* WPA_TRACE_BFD */
2034}
2035
2036
2037static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2038 char *buf, size_t buflen)
2039{
2040#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002041 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2042 wpa_trace_test_fail_func);
2043#else /* WPA_TRACE_BFD */
2044 return -1;
2045#endif /* WPA_TRACE_BFD */
2046}
2047
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002048
2049static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2050{
2051 struct sta_info *sta;
2052 u8 addr[ETH_ALEN];
2053 u8 zero[WPA_TK_MAX_LEN];
2054
2055 os_memset(zero, 0, sizeof(zero));
2056
2057 if (hwaddr_aton(cmd, addr))
2058 return -1;
2059
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002060 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2061 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2062 return -1;
2063
2064 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2065
2066 /* First, use a zero key to avoid any possible duplicate key
2067 * avoidance in the driver. */
2068 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2069 hapd->last_bigtk_alg,
2070 broadcast_ether_addr,
2071 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2072 zero, hapd->last_bigtk_len,
2073 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2074 return -1;
2075
2076 /* Set the previously configured key to reset its TSC */
2077 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2078 hapd->last_bigtk_alg,
2079 broadcast_ether_addr,
2080 hapd->last_bigtk_key_idx, 0, 1, NULL,
2081 0, hapd->last_bigtk,
2082 hapd->last_bigtk_len,
2083 KEY_FLAG_GROUP_TX_DEFAULT);
2084 }
2085
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002086 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2087 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2088 return -1;
2089
2090 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2091
2092 /* First, use a zero key to avoid any possible duplicate key
2093 * avoidance in the driver. */
2094 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2095 hapd->last_igtk_alg,
2096 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002097 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2098 zero, hapd->last_igtk_len,
2099 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002100 return -1;
2101
2102 /* Set the previously configured key to reset its TSC */
2103 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2104 hapd->last_igtk_alg,
2105 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002106 hapd->last_igtk_key_idx, 0, 1, NULL,
2107 0, hapd->last_igtk,
2108 hapd->last_igtk_len,
2109 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002110 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002111
2112 if (is_broadcast_ether_addr(addr)) {
2113 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2114 return -1;
2115
2116 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2117
2118 /* First, use a zero key to avoid any possible duplicate key
2119 * avoidance in the driver. */
2120 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2121 hapd->last_gtk_alg,
2122 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002123 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2124 zero, hapd->last_gtk_len,
2125 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002126 return -1;
2127
2128 /* Set the previously configured key to reset its TSC */
2129 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2130 hapd->last_gtk_alg,
2131 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002132 hapd->last_gtk_key_idx, 0, 1, NULL,
2133 0, hapd->last_gtk,
2134 hapd->last_gtk_len,
2135 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002136 }
2137
2138 sta = ap_get_sta(hapd, addr);
2139 if (!sta)
2140 return -1;
2141
2142 if (sta->last_tk_alg == WPA_ALG_NONE)
2143 return -1;
2144
2145 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2146 MAC2STR(sta->addr));
2147
2148 /* First, use a zero key to avoid any possible duplicate key avoidance
2149 * in the driver. */
2150 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002151 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2152 zero, sta->last_tk_len,
2153 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002154 return -1;
2155
2156 /* Set the previously configured key to reset its TSC/RSC */
2157 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002158 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2159 0, sta->last_tk, sta->last_tk_len,
2160 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002161}
2162
2163
2164static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2165{
2166 u8 addr[ETH_ALEN];
2167 const char *pos = cmd;
2168 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002169 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002170 int idx, set_tx;
2171 u8 seq[6], key[WPA_TK_MAX_LEN];
2172 size_t key_len;
2173
Hai Shalomfdcde762020-04-02 11:19:20 -07002174 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002175
2176 alg = atoi(pos);
2177 pos = os_strchr(pos, ' ');
2178 if (!pos)
2179 return -1;
2180 pos++;
2181 if (hwaddr_aton(pos, addr))
2182 return -1;
2183 pos += 17;
2184 if (*pos != ' ')
2185 return -1;
2186 pos++;
2187 idx = atoi(pos);
2188 pos = os_strchr(pos, ' ');
2189 if (!pos)
2190 return -1;
2191 pos++;
2192 set_tx = atoi(pos);
2193 pos = os_strchr(pos, ' ');
2194 if (!pos)
2195 return -1;
2196 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002197 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002198 return -1;
2199 pos += 2 * 6;
2200 if (*pos != ' ')
2201 return -1;
2202 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002203 if (!os_strchr(pos, ' '))
2204 return -1;
2205 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002206 if (hexstr2bin(pos, key, key_len) < 0)
2207 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002208 pos += 2 * key_len;
2209 if (*pos != ' ')
2210 return -1;
2211
2212 pos++;
2213 key_flag = atoi(pos);
2214 pos = os_strchr(pos, ' ');
2215 if (pos)
2216 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002217
2218 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002219 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2220 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002221}
2222
2223
2224static void restore_tk(void *ctx1, void *ctx2)
2225{
2226 struct hostapd_data *hapd = ctx1;
2227 struct sta_info *sta = ctx2;
2228
2229 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2230 MAC2STR(sta->addr));
2231 /* This does not really restore the TSC properly, so this will result
2232 * in replay protection issues for now since there is no clean way of
2233 * preventing encryption of a single EAPOL frame. */
2234 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002235 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2236 sta->last_tk, sta->last_tk_len,
2237 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002238}
2239
2240
2241static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2242{
2243 struct sta_info *sta;
2244 u8 addr[ETH_ALEN];
2245 int plain = os_strstr(cmd, "plaintext") != NULL;
2246
2247 if (hwaddr_aton(cmd, addr))
2248 return -1;
2249
2250 sta = ap_get_sta(hapd, addr);
2251 if (!sta || !sta->wpa_sm)
2252 return -1;
2253
2254 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2255 plain = 0; /* no need for special processing */
2256 if (plain) {
2257 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2258 MAC2STR(sta->addr));
2259 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002260 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2261 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002262 }
2263
2264 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2265 return wpa_auth_resend_m1(sta->wpa_sm,
2266 os_strstr(cmd, "change-anonce") != NULL,
2267 plain ? restore_tk : NULL, hapd, sta);
2268}
2269
2270
2271static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2272{
2273 struct sta_info *sta;
2274 u8 addr[ETH_ALEN];
2275 int plain = os_strstr(cmd, "plaintext") != NULL;
2276
2277 if (hwaddr_aton(cmd, addr))
2278 return -1;
2279
2280 sta = ap_get_sta(hapd, addr);
2281 if (!sta || !sta->wpa_sm)
2282 return -1;
2283
2284 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2285 plain = 0; /* no need for special processing */
2286 if (plain) {
2287 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2288 MAC2STR(sta->addr));
2289 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002290 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2291 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002292 }
2293
2294 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2295 return wpa_auth_resend_m3(sta->wpa_sm,
2296 plain ? restore_tk : NULL, hapd, sta);
2297}
2298
2299
2300static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2301 const char *cmd)
2302{
2303 struct sta_info *sta;
2304 u8 addr[ETH_ALEN];
2305 int plain = os_strstr(cmd, "plaintext") != NULL;
2306
2307 if (hwaddr_aton(cmd, addr))
2308 return -1;
2309
2310 sta = ap_get_sta(hapd, addr);
2311 if (!sta || !sta->wpa_sm)
2312 return -1;
2313
2314 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2315 plain = 0; /* no need for special processing */
2316 if (plain) {
2317 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2318 MAC2STR(sta->addr));
2319 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002320 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2321 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002322 }
2323
2324 wpa_printf(MSG_INFO,
2325 "TESTING: Send group M1 for the same GTK and zero RSC to "
2326 MACSTR, MAC2STR(sta->addr));
2327 return wpa_auth_resend_group_m1(sta->wpa_sm,
2328 plain ? restore_tk : NULL, hapd, sta);
2329}
2330
Hai Shalomfdcde762020-04-02 11:19:20 -07002331
Hai Shaloma20dcd72022-02-04 13:43:00 -08002332static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2333{
2334 struct sta_info *sta;
2335 u8 addr[ETH_ALEN];
2336
2337 if (hwaddr_aton(cmd, addr))
2338 return -1;
2339
2340 sta = ap_get_sta(hapd, addr);
2341 if (!sta || !sta->wpa_sm)
2342 return -1;
2343
2344 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2345}
2346
2347
Hai Shalom899fcc72020-10-19 14:38:18 -07002348static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2349 char *buf, size_t buflen)
2350{
2351 struct rsn_pmksa_cache_entry *pmksa;
2352
2353 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2354 if (!pmksa)
2355 return -1;
2356
2357 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2358}
2359
2360
Hai Shalomfdcde762020-04-02 11:19:20 -07002361static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2362 char *buf, size_t buflen)
2363{
2364 struct sta_info *sta;
2365 u8 addr[ETH_ALEN];
2366 const u8 *pmk;
2367 int pmk_len;
2368
2369 if (hwaddr_aton(cmd, addr))
2370 return -1;
2371
2372 sta = ap_get_sta(hapd, addr);
2373 if (!sta || !sta->wpa_sm) {
2374 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2375 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002376 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002377 }
2378 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002379 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002380 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2381 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002382 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002383 }
2384
2385 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2386}
2387
Hai Shaloma20dcd72022-02-04 13:43:00 -08002388
2389static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2390 const char *cmd)
2391{
2392 u16 type;
2393 char *pos, *end;
2394 u8 match[10];
2395 size_t match_len;
2396 bool multicast = false;
2397
2398 type = strtol(cmd, &pos, 16);
2399 if (*pos != ' ')
2400 return -1;
2401 pos++;
2402 end = os_strchr(pos, ' ');
2403 if (end) {
2404 match_len = end - pos;
2405 multicast = os_strstr(end, "multicast") != NULL;
2406 } else {
2407 match_len = os_strlen(pos) / 2;
2408 }
2409 if (hexstr2bin(pos, match, match_len))
2410 return -1;
2411
2412 return hostapd_drv_register_frame(hapd, type, match, match_len,
2413 multicast);
2414}
2415
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002416#endif /* CONFIG_TESTING_OPTIONS */
2417
2418
Hai Shalomb755a2a2020-04-23 21:49:02 -07002419#ifdef NEED_AP_MLME
Hai Shalomfdcde762020-04-02 11:19:20 -07002420static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params)
2421{
2422 switch (params->bandwidth) {
2423 case 0:
2424 /* bandwidth not specified: use 20 MHz by default */
2425 /* fall-through */
2426 case 20:
2427 if (params->center_freq1 &&
2428 params->center_freq1 != params->freq)
2429 return -1;
2430
2431 if (params->center_freq2 || params->sec_channel_offset)
2432 return -1;
2433 break;
2434 case 40:
2435 if (params->center_freq2 || !params->sec_channel_offset)
2436 return -1;
2437
2438 if (!params->center_freq1)
2439 break;
2440 switch (params->sec_channel_offset) {
2441 case 1:
2442 if (params->freq + 10 != params->center_freq1)
2443 return -1;
2444 break;
2445 case -1:
2446 if (params->freq - 10 != params->center_freq1)
2447 return -1;
2448 break;
2449 default:
2450 return -1;
2451 }
2452 break;
2453 case 80:
2454 if (!params->center_freq1 || !params->sec_channel_offset)
2455 return 1;
2456
2457 switch (params->sec_channel_offset) {
2458 case 1:
2459 if (params->freq - 10 != params->center_freq1 &&
2460 params->freq + 30 != params->center_freq1)
2461 return 1;
2462 break;
2463 case -1:
2464 if (params->freq + 10 != params->center_freq1 &&
2465 params->freq - 30 != params->center_freq1)
2466 return -1;
2467 break;
2468 default:
2469 return -1;
2470 }
2471
2472 /* Adjacent and overlapped are not allowed for 80+80 */
2473 if (params->center_freq2 &&
2474 params->center_freq1 - params->center_freq2 <= 80 &&
2475 params->center_freq2 - params->center_freq1 <= 80)
2476 return 1;
2477 break;
2478 case 160:
2479 if (!params->center_freq1 || params->center_freq2 ||
2480 !params->sec_channel_offset)
2481 return -1;
2482
2483 switch (params->sec_channel_offset) {
2484 case 1:
2485 if (params->freq + 70 != params->center_freq1 &&
2486 params->freq + 30 != params->center_freq1 &&
2487 params->freq - 10 != params->center_freq1 &&
2488 params->freq - 50 != params->center_freq1)
2489 return -1;
2490 break;
2491 case -1:
2492 if (params->freq + 50 != params->center_freq1 &&
2493 params->freq + 10 != params->center_freq1 &&
2494 params->freq - 30 != params->center_freq1 &&
2495 params->freq - 70 != params->center_freq1)
2496 return -1;
2497 break;
2498 default:
2499 return -1;
2500 }
2501 break;
2502 default:
2503 return -1;
2504 }
2505
2506 return 0;
2507}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002508#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002509
2510
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002511static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2512 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002513{
2514#ifdef NEED_AP_MLME
2515 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002516 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002517 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002518 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002519 int bandwidth;
2520 u8 chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002521
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002522 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002523 if (ret)
2524 return ret;
2525
Hai Shalomfdcde762020-04-02 11:19:20 -07002526 ret = hostapd_ctrl_check_freq_params(&settings.freq_params);
2527 if (ret) {
2528 wpa_printf(MSG_INFO,
2529 "chanswitch: invalid frequency settings provided");
2530 return ret;
2531 }
2532
2533 switch (settings.freq_params.bandwidth) {
2534 case 40:
2535 bandwidth = CHAN_WIDTH_40;
2536 break;
2537 case 80:
2538 if (settings.freq_params.center_freq2)
2539 bandwidth = CHAN_WIDTH_80P80;
2540 else
2541 bandwidth = CHAN_WIDTH_80;
2542 break;
2543 case 160:
2544 bandwidth = CHAN_WIDTH_160;
2545 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002546 case 320:
2547 bandwidth = CHAN_WIDTH_320;
2548 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002549 default:
2550 bandwidth = CHAN_WIDTH_20;
2551 break;
2552 }
2553
2554 if (settings.freq_params.center_freq1)
2555 dfs_range += hostapd_is_dfs_overlap(
2556 iface, bandwidth, settings.freq_params.center_freq1);
2557 else
2558 dfs_range += hostapd_is_dfs_overlap(
2559 iface, bandwidth, settings.freq_params.freq);
2560
2561 if (settings.freq_params.center_freq2)
2562 dfs_range += hostapd_is_dfs_overlap(
2563 iface, bandwidth, settings.freq_params.center_freq2);
2564
2565 if (dfs_range) {
2566 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2567 if (ret == NUM_HOSTAPD_MODES) {
2568 wpa_printf(MSG_ERROR,
2569 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2570 settings.freq_params.freq,
2571 settings.freq_params.sec_channel_offset,
2572 settings.freq_params.bandwidth);
2573 return -1;
2574 }
2575
2576 settings.freq_params.channel = chan;
2577
2578 wpa_printf(MSG_DEBUG,
2579 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2580 settings.freq_params.channel,
2581 settings.freq_params.freq,
2582 settings.freq_params.sec_channel_offset,
2583 settings.freq_params.bandwidth,
2584 settings.freq_params.center_freq1);
2585
2586 /* Perform CAC and switch channel */
2587 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2588 return 0;
2589 }
2590
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002591 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002592
Sunil Ravia04bd252022-05-02 22:54:18 -07002593 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002594 hostapd_chan_switch_config(iface->bss[i],
2595 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002596
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002597 ret = hostapd_switch_channel(iface->bss[i], &settings);
2598 if (ret) {
2599 /* FIX: What do we do if CSA fails in the middle of
2600 * submitting multi-BSS CSA requests? */
2601 return ret;
2602 }
2603 }
2604
2605 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002606#else /* NEED_AP_MLME */
2607 return -1;
2608#endif /* NEED_AP_MLME */
2609}
2610
2611
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002612static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2613 int reply_size, const char *param)
2614{
2615#ifdef RADIUS_SERVER
2616 if (os_strcmp(param, "radius_server") == 0) {
2617 return radius_server_get_mib(hapd->radius_srv, reply,
2618 reply_size);
2619 }
2620#endif /* RADIUS_SERVER */
2621 return -1;
2622}
2623
2624
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002625static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2626 char *buf, size_t buflen)
2627{
2628 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08002629 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002630 u8 *data = NULL;
2631 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08002632 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002633 struct wpabuf *reply;
2634 size_t data_len = 0;
2635
Hai Shalom60840252021-02-19 19:02:11 -08002636 /**
2637 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2638 * [nested=<0|1>]
2639 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002640 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002641 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002642 return -EINVAL;
2643
2644 subcmd = strtoul(pos, &pos, 10);
2645
2646 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002647 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002648 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08002649
2650 temp = os_strchr(pos, ' ');
2651 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002652 }
2653
2654 if (data_len) {
2655 data_len /= 2;
2656 data = os_malloc(data_len);
2657 if (!data)
2658 return -ENOBUFS;
2659
2660 if (hexstr2bin(pos, data, data_len)) {
2661 wpa_printf(MSG_DEBUG,
2662 "Vendor command: wrong parameter format");
2663 os_free(data);
2664 return -EINVAL;
2665 }
2666 }
2667
Hai Shalom60840252021-02-19 19:02:11 -08002668 pos = os_strstr(cmd, "nested=");
2669 if (pos)
2670 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2671 NESTED_ATTR_NOT_USED;
2672
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002673 reply = wpabuf_alloc((buflen - 1) / 2);
2674 if (!reply) {
2675 os_free(data);
2676 return -ENOBUFS;
2677 }
2678
2679 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08002680 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002681
2682 if (ret == 0)
2683 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2684 wpabuf_len(reply));
2685
2686 wpabuf_free(reply);
2687 os_free(data);
2688
2689 return ret;
2690}
2691
2692
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002693static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2694 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002696 u8 addr[ETH_ALEN];
2697 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002699 if (hwaddr_aton(cmd, addr))
2700 return -1;
2701
2702 sta = ap_get_sta(hapd, addr);
2703 if (!sta || !sta->eapol_sm)
2704 return -1;
2705
2706 eapol_auth_reauthenticate(sta->eapol_sm);
2707 return 0;
2708}
2709
2710
2711static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2712{
2713 u8 addr[ETH_ALEN];
2714 struct sta_info *sta;
2715 char *pos = cmd, *param;
2716
2717 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2718 return -1;
2719 pos += 18;
2720 param = pos;
2721 pos = os_strchr(pos, ' ');
2722 if (!pos)
2723 return -1;
2724 *pos++ = '\0';
2725
2726 sta = ap_get_sta(hapd, addr);
2727 if (!sta || !sta->eapol_sm)
2728 return -1;
2729
2730 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2731}
2732
2733
2734static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2735 char *buf, size_t buflen)
2736{
2737 char *pos, *end, *stamp;
2738 int ret;
2739
2740 /* cmd: "LOG_LEVEL [<level>]" */
2741 if (*cmd == '\0') {
2742 pos = buf;
2743 end = buf + buflen;
2744 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2745 "Timestamp: %d\n",
2746 debug_level_str(wpa_debug_level),
2747 wpa_debug_timestamp);
2748 if (os_snprintf_error(end - pos, ret))
2749 ret = 0;
2750
2751 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002754 while (*cmd == ' ')
2755 cmd++;
2756
2757 stamp = os_strchr(cmd, ' ');
2758 if (stamp) {
2759 *stamp++ = '\0';
2760 while (*stamp == ' ') {
2761 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002762 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 }
2764
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002765 if (os_strlen(cmd)) {
2766 int level = str_to_debug_level(cmd);
2767 if (level < 0)
2768 return -1;
2769 wpa_debug_level = level;
2770 }
2771
2772 if (stamp && os_strlen(stamp))
2773 wpa_debug_timestamp = atoi(stamp);
2774
2775 os_memcpy(buf, "OK\n", 3);
2776 return 3;
2777}
2778
2779
2780#ifdef NEED_AP_MLME
2781static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2782 char *buf, size_t buflen)
2783{
2784 struct hostapd_iface *iface = hapd->iface;
2785 char *pos, *end;
2786 struct hostapd_sta_info *info;
2787 struct os_reltime now;
2788
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002789 if (!iface->num_sta_seen)
2790 return 0;
2791
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002792 sta_track_expire(iface, 0);
2793
2794 pos = buf;
2795 end = buf + buflen;
2796
2797 os_get_reltime(&now);
2798 dl_list_for_each_reverse(info, &iface->sta_seen,
2799 struct hostapd_sta_info, list) {
2800 struct os_reltime age;
2801 int ret;
2802
2803 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002804 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2805 MAC2STR(info->addr), (unsigned int) age.sec,
2806 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002807 if (os_snprintf_error(end - pos, ret))
2808 break;
2809 pos += ret;
2810 }
2811
2812 return pos - buf;
2813}
2814#endif /* NEED_AP_MLME */
2815
2816
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002817static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2818 const char *cmd)
2819{
2820 u8 addr[ETH_ALEN];
2821
2822 if (hwaddr_aton(cmd, addr)) {
2823 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2824 return -1;
2825 }
2826
2827 return hostapd_send_lci_req(hapd, addr);
2828}
2829
2830
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002831static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002832{
2833 u8 addr[ETH_ALEN];
2834 char *token, *context = NULL;
2835 int random_interval, min_ap;
2836 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2837 unsigned int n_responders;
2838
2839 token = str_token(cmd, " ", &context);
2840 if (!token || hwaddr_aton(token, addr)) {
2841 wpa_printf(MSG_INFO,
2842 "CTRL: REQ_RANGE - Bad destination address");
2843 return -1;
2844 }
2845
2846 token = str_token(cmd, " ", &context);
2847 if (!token)
2848 return -1;
2849
2850 random_interval = atoi(token);
2851 if (random_interval < 0 || random_interval > 0xffff)
2852 return -1;
2853
2854 token = str_token(cmd, " ", &context);
2855 if (!token)
2856 return -1;
2857
2858 min_ap = atoi(token);
2859 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2860 return -1;
2861
2862 n_responders = 0;
2863 while ((token = str_token(cmd, " ", &context))) {
2864 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2865 wpa_printf(MSG_INFO,
2866 "CTRL: REQ_RANGE: Too many responders");
2867 return -1;
2868 }
2869
2870 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2871 wpa_printf(MSG_INFO,
2872 "CTRL: REQ_RANGE: Bad responder address");
2873 return -1;
2874 }
2875
2876 n_responders++;
2877 }
2878
2879 if (!n_responders) {
2880 wpa_printf(MSG_INFO,
2881 "CTRL: REQ_RANGE - No FTM responder address");
2882 return -1;
2883 }
2884
2885 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2886 responders, n_responders);
2887}
2888
2889
Dmitry Shmidt29333592017-01-09 12:27:11 -08002890static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2891 const char *cmd, char *reply,
2892 size_t reply_size)
2893{
2894 u8 addr[ETH_ALEN];
2895 const char *pos;
2896 struct wpabuf *req;
2897 int ret;
2898 u8 req_mode = 0;
2899
2900 if (hwaddr_aton(cmd, addr))
2901 return -1;
2902 pos = os_strchr(cmd, ' ');
2903 if (!pos)
2904 return -1;
2905 pos++;
2906 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2907 int val = hex2byte(pos + 9);
2908
2909 if (val < 0)
2910 return -1;
2911 req_mode = val;
2912 pos += 11;
2913 pos = os_strchr(pos, ' ');
2914 if (!pos)
2915 return -1;
2916 pos++;
2917 }
2918 req = wpabuf_parse_bin(pos);
2919 if (!req)
2920 return -1;
2921
2922 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2923 wpabuf_free(req);
2924 if (ret >= 0)
2925 ret = os_snprintf(reply, reply_size, "%d", ret);
2926 return ret;
2927}
2928
2929
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002930static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
2931 char *buf, size_t buflen)
2932{
2933 if (!(hapd->conf->radio_measurements[0] &
2934 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2935 wpa_printf(MSG_ERROR,
2936 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
2937 return -1;
2938 }
2939
2940 return hostapd_neighbor_show(hapd, buf, buflen);
2941}
2942
2943
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002944static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2945{
2946 struct wpa_ssid_value ssid;
2947 u8 bssid[ETH_ALEN];
2948 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002949 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002950 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002951 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002952 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002953
2954 if (!(hapd->conf->radio_measurements[0] &
2955 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2956 wpa_printf(MSG_ERROR,
2957 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2958 return -1;
2959 }
2960
2961 if (hwaddr_aton(buf, bssid)) {
2962 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2963 return -1;
2964 }
2965
2966 tmp = os_strstr(buf, "ssid=");
2967 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2968 wpa_printf(MSG_ERROR,
2969 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2970 return -1;
2971 }
2972 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2973 if (!buf)
2974 return -1;
2975
2976 tmp = os_strstr(buf, "nr=");
2977 if (!tmp) {
2978 wpa_printf(MSG_ERROR,
2979 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2980 return -1;
2981 }
2982
2983 buf = os_strchr(tmp, ' ');
2984 if (buf)
2985 *buf++ = '\0';
2986
2987 nr = wpabuf_parse_bin(tmp + 3);
2988 if (!nr) {
2989 wpa_printf(MSG_ERROR,
2990 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2991 return -1;
2992 }
2993
2994 if (!buf)
2995 goto set;
2996
2997 tmp = os_strstr(buf, "lci=");
2998 if (tmp) {
2999 buf = os_strchr(tmp, ' ');
3000 if (buf)
3001 *buf++ = '\0';
3002 lci = wpabuf_parse_bin(tmp + 4);
3003 if (!lci) {
3004 wpa_printf(MSG_ERROR,
3005 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003006 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003007 }
3008 }
3009
3010 if (!buf)
3011 goto set;
3012
3013 tmp = os_strstr(buf, "civic=");
3014 if (tmp) {
3015 buf = os_strchr(tmp, ' ');
3016 if (buf)
3017 *buf++ = '\0';
3018 civic = wpabuf_parse_bin(tmp + 6);
3019 if (!civic) {
3020 wpa_printf(MSG_ERROR,
3021 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003022 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003023 }
3024 }
3025
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003026 if (!buf)
3027 goto set;
3028
3029 if (os_strstr(buf, "stat"))
3030 stationary = 1;
3031
Hai Shaloma20dcd72022-02-04 13:43:00 -08003032 tmp = os_strstr(buf, "bss_parameter=");
3033 if (tmp) {
3034 bss_parameters = atoi(tmp + 14);
3035 if (bss_parameters < 0 || bss_parameters > 0xff) {
3036 wpa_printf(MSG_ERROR,
3037 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3038 goto fail;
3039 }
3040 }
3041
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003042set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003043 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003044 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003045
Hai Shaloma20dcd72022-02-04 13:43:00 -08003046fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003047 wpabuf_free(nr);
3048 wpabuf_free(lci);
3049 wpabuf_free(civic);
3050
3051 return ret;
3052}
3053
3054
3055static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3056 char *buf)
3057{
3058 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003059 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003060 u8 bssid[ETH_ALEN];
3061 char *tmp;
3062
3063 if (hwaddr_aton(buf, bssid)) {
3064 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3065 return -1;
3066 }
3067
3068 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003069 if (tmp) {
3070 ssidp = &ssid;
3071 if (ssid_parse(tmp + 5, &ssid)) {
3072 wpa_printf(MSG_ERROR,
3073 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3074 return -1;
3075 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003076 }
3077
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003078 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003079}
3080
3081
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003082static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3083 size_t buflen)
3084{
3085 int ret, i;
3086 char *pos, *end;
3087
3088 ret = os_snprintf(buf, buflen, "%016llX:\n",
3089 (long long unsigned) iface->drv_flags);
3090 if (os_snprintf_error(buflen, ret))
3091 return -1;
3092
3093 pos = buf + ret;
3094 end = buf + buflen;
3095
3096 for (i = 0; i < 64; i++) {
3097 if (iface->drv_flags & (1LLU << i)) {
3098 ret = os_snprintf(pos, end - pos, "%s\n",
3099 driver_flag_to_string(1LLU << i));
3100 if (os_snprintf_error(end - pos, ret))
3101 return -1;
3102 pos += ret;
3103 }
3104 }
3105
3106 return pos - buf;
3107}
3108
3109
Hai Shalomb755a2a2020-04-23 21:49:02 -07003110static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3111 size_t buflen)
3112{
3113 int ret, i;
3114 char *pos, *end;
3115
3116 ret = os_snprintf(buf, buflen, "%016llX:\n",
3117 (long long unsigned) iface->drv_flags2);
3118 if (os_snprintf_error(buflen, ret))
3119 return -1;
3120
3121 pos = buf + ret;
3122 end = buf + buflen;
3123
3124 for (i = 0; i < 64; i++) {
3125 if (iface->drv_flags2 & (1LLU << i)) {
3126 ret = os_snprintf(pos, end - pos, "%s\n",
3127 driver_flag2_to_string(1LLU << i));
3128 if (os_snprintf_error(end - pos, ret))
3129 return -1;
3130 pos += ret;
3131 }
3132 }
3133
3134 return pos - buf;
3135}
3136
3137
Hai Shalom021b0b52019-04-10 11:17:58 -07003138static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3139 const char *field, char *buf,
3140 size_t buflen)
3141{
3142 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3143
3144#ifdef CONFIG_DPP
3145 if (os_strcmp(field, "dpp") == 0) {
3146 int res;
3147
Hai Shaloma20dcd72022-02-04 13:43:00 -08003148#ifdef CONFIG_DPP3
3149 res = os_snprintf(buf, buflen, "DPP=3");
3150#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003151 res = os_snprintf(buf, buflen, "DPP=2");
3152#else /* CONFIG_DPP2 */
3153 res = os_snprintf(buf, buflen, "DPP=1");
3154#endif /* CONFIG_DPP2 */
3155 if (os_snprintf_error(buflen, res))
3156 return -1;
3157 return res;
3158 }
3159#endif /* CONFIG_DPP */
3160
3161 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3162 field);
3163
3164 return -1;
3165}
3166
3167
Hai Shaloma20dcd72022-02-04 13:43:00 -08003168#ifdef ANDROID
3169static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3170 char *buf, size_t buflen)
3171{
3172 int ret;
3173
3174 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3175 if (ret == 0) {
3176 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3177 if (os_snprintf_error(buflen, ret))
3178 ret = -1;
3179 }
3180 return ret;
3181}
3182#endif /* ANDROID */
3183
3184
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003185static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3186 char *buf, char *reply,
3187 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003188 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003189 socklen_t fromlen)
3190{
3191 int reply_len, res;
3192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003193 os_memcpy(reply, "OK\n", 3);
3194 reply_len = 3;
3195
3196 if (os_strcmp(buf, "PING") == 0) {
3197 os_memcpy(reply, "PONG\n", 5);
3198 reply_len = 5;
3199 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3200 if (wpa_debug_reopen_file() < 0)
3201 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003202 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3203 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003204 } else if (os_strcmp(buf, "STATUS") == 0) {
3205 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3206 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003207 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3208 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003209 } else if (os_strcmp(buf, "MIB") == 0) {
3210 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3211 if (reply_len >= 0) {
3212 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3213 reply_size - reply_len);
3214 if (res < 0)
3215 reply_len = -1;
3216 else
3217 reply_len += res;
3218 }
3219 if (reply_len >= 0) {
3220 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3221 reply_size - reply_len);
3222 if (res < 0)
3223 reply_len = -1;
3224 else
3225 reply_len += res;
3226 }
3227#ifndef CONFIG_NO_RADIUS
3228 if (reply_len >= 0) {
3229 res = radius_client_get_mib(hapd->radius,
3230 reply + reply_len,
3231 reply_size - reply_len);
3232 if (res < 0)
3233 reply_len = -1;
3234 else
3235 reply_len += res;
3236 }
3237#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003238 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3239 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3240 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003241 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3242 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3243 reply_size);
3244 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3245 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3246 reply_size);
3247 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3248 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3249 reply_size);
3250 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003251 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3252 reply_len = -1;
3253 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3254 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 reply_len = -1;
3256 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003257 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 reply_len = -1;
3259 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003260 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261 buf + 6))
3262 reply_len = -1;
3263 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3264 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3265 reply_len = -1;
3266 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3267 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3268 reply_len = -1;
3269 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3270 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3271 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003272#ifdef CONFIG_TAXONOMY
3273 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3274 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3275 reply, reply_size);
3276#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003277 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3278 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3279 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003280 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3281 if (hostapd_ctrl_iface_stop_ap(hapd))
3282 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283#ifdef NEED_AP_MLME
3284 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3285 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3286 reply_len = -1;
3287#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288#ifdef CONFIG_WPS
3289 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3290 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3291 reply_len = -1;
3292 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3293 reply_len = hostapd_ctrl_iface_wps_check_pin(
3294 hapd, buf + 14, reply, reply_size);
3295 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3296 if (hostapd_wps_button_pushed(hapd, NULL))
3297 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003298 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3299 if (hostapd_wps_cancel(hapd))
3300 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3302 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3303 reply, reply_size);
3304 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3305 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3306 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003307 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3308 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3309 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003310#ifdef CONFIG_WPS_NFC
3311 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3312 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3313 reply_len = -1;
3314 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3315 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3316 hapd, buf + 21, reply, reply_size);
3317 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3318 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3319 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003320 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3321 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3322 hapd, buf + 21, reply, reply_size);
3323 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3324 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3325 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003326#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003327#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07003328#ifdef CONFIG_INTERWORKING
3329 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3330 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3331 reply_len = -1;
3332 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3333 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3334 reply_len = -1;
3335#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003336#ifdef CONFIG_HS20
3337 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3338 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3339 reply_len = -1;
3340 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3341 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3342 reply_len = -1;
3343#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003344#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003345 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3346 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3347 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003348 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3349 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3350 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003351 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3352 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3353 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003354 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3355 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3356 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003357#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3359 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3360 reply_size);
3361 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3362 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3363 reply_len = -1;
3364 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3365 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3366 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003367 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3368 if (hostapd_ctrl_iface_enable(hapd->iface))
3369 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003370 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3371 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3372 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003373 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3374 if (hostapd_ctrl_iface_reload(hapd->iface))
3375 reply_len = -1;
3376 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3377 if (hostapd_ctrl_iface_disable(hapd->iface))
3378 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003379 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3380 if (ieee802_11_set_beacon(hapd))
3381 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003382#ifdef CONFIG_TESTING_OPTIONS
3383 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3384 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3385 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003386 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3387 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3388 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003389 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3390 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3391 buf + 23) < 0)
3392 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003393 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3394 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3395 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003396 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3397 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3398 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003399 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
3400 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
3401 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003402 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3403 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3404 reply_len = -1;
3405 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3406 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3407 reply_len = -1;
3408 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3409 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3410 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003411 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3412 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3413 reply_len = -1;
3414 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3415 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3416 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003417 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3418 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3419 reply_len = -1;
3420 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3421 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003422 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3423 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3424 reply_len = -1;
3425 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3426 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3427 reply_len = -1;
3428 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3429 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3430 reply_len = -1;
3431 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3432 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3433 reply_len = -1;
3434 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3435 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3436 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003437 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
3438 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
3439 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003440 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3441 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3442 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003443 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3444 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3445 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003446 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
3447 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
3448 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003449#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003450 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003451 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003452 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003453 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3454 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3455 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003456 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3457 ieee802_1x_erp_flush(hapd);
3458#ifdef RADIUS_SERVER
3459 radius_server_erp_flush(hapd->radius_srv);
3460#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003461 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3462 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3463 reply_len = -1;
3464 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3465 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3466 reply_len = -1;
3467 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3468 reply_len = hostapd_ctrl_iface_log_level(
3469 hapd, buf + 9, reply, reply_size);
3470#ifdef NEED_AP_MLME
3471 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3472 reply_len = hostapd_ctrl_iface_track_sta_list(
3473 hapd, reply, reply_size);
3474#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07003475 } else if (os_strcmp(buf, "PMKSA") == 0) {
3476 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3477 reply_size);
3478 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3479 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003480 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3481 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3482 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003483 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3484 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3485 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003486 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3487 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3488 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003489 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3490 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3491 reply_len = -1;
3492 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3493 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3494 reply_len = -1;
3495 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3496 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3497 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003498 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3499 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3500 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003501 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3502 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3503 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07003504 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
3505 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
3506 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003507 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3508 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003509 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3510 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003511 if (hostapd_ctrl_iface_acl_add_mac(
3512 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003513 &hapd->conf->num_accept_mac, buf + 19) ||
3514 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003515 reply_len = -1;
3516 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003517 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003518 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003519 &hapd->conf->num_accept_mac, buf + 19) ||
3520 hostapd_set_acl(hapd) ||
3521 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07003522 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003523 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3524 reply_len = hostapd_ctrl_iface_acl_show_mac(
3525 hapd->conf->accept_mac,
3526 hapd->conf->num_accept_mac, reply, reply_size);
3527 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3528 hostapd_ctrl_iface_acl_clear_list(
3529 &hapd->conf->accept_mac,
3530 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003531 if (hostapd_set_acl(hapd) ||
3532 hostapd_disassoc_accept_mac(hapd))
3533 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003534 }
3535 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3536 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003537 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003538 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003539 &hapd->conf->num_deny_mac, buf + 17) ||
3540 hostapd_set_acl(hapd) ||
3541 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003542 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003543 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003544 if (hostapd_ctrl_iface_acl_del_mac(
3545 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003546 &hapd->conf->num_deny_mac, buf + 17) ||
3547 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003548 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003549 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3550 reply_len = hostapd_ctrl_iface_acl_show_mac(
3551 hapd->conf->deny_mac,
3552 hapd->conf->num_deny_mac, reply, reply_size);
3553 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3554 hostapd_ctrl_iface_acl_clear_list(
3555 &hapd->conf->deny_mac,
3556 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003557 if (hostapd_set_acl(hapd))
3558 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003559 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003560#ifdef CONFIG_DPP
3561 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3562 res = hostapd_dpp_qr_code(hapd, buf + 12);
3563 if (res < 0) {
3564 reply_len = -1;
3565 } else {
3566 reply_len = os_snprintf(reply, reply_size, "%d", res);
3567 if (os_snprintf_error(reply_size, reply_len))
3568 reply_len = -1;
3569 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003570 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3571 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3572 if (res < 0) {
3573 reply_len = -1;
3574 } else {
3575 reply_len = os_snprintf(reply, reply_size, "%d", res);
3576 if (os_snprintf_error(reply_size, reply_len))
3577 reply_len = -1;
3578 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003579 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3580 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3581 if (res < 0) {
3582 reply_len = -1;
3583 } else {
3584 reply_len = os_snprintf(reply, reply_size, "%d", res);
3585 if (os_snprintf_error(reply_size, reply_len))
3586 reply_len = -1;
3587 }
3588 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3589 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3590 if (res < 0) {
3591 reply_len = -1;
3592 } else {
3593 reply_len = os_snprintf(reply, reply_size, "%d", res);
3594 if (os_snprintf_error(reply_size, reply_len))
3595 reply_len = -1;
3596 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003597 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003598 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003599 if (res < 0) {
3600 reply_len = -1;
3601 } else {
3602 reply_len = os_snprintf(reply, reply_size, "%d", res);
3603 if (os_snprintf_error(reply_size, reply_len))
3604 reply_len = -1;
3605 }
3606 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003607 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3608 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003609 reply_len = -1;
3610 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3611 const char *uri;
3612
Hai Shalom021b0b52019-04-10 11:17:58 -07003613 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3614 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003615 if (!uri) {
3616 reply_len = -1;
3617 } else {
3618 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3619 if (os_snprintf_error(reply_size, reply_len))
3620 reply_len = -1;
3621 }
3622 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003623 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3624 atoi(buf + 19),
3625 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07003626 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3627 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3628 atoi(buf + 18),
3629 os_strchr(buf + 18, ' ')) < 0)
3630 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003631 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3632 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3633 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003634 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3635 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3636 reply_len = -1;
3637 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3638 hostapd_dpp_stop(hapd);
3639 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003640 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003641 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3642 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003643 if (res < 0) {
3644 reply_len = -1;
3645 } else {
3646 reply_len = os_snprintf(reply, reply_size, "%d", res);
3647 if (os_snprintf_error(reply_size, reply_len))
3648 reply_len = -1;
3649 }
Sunil Ravia04bd252022-05-02 22:54:18 -07003650 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
3651 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
3652 buf + 20) < 0)
3653 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003654 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003655 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3656 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003657 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003658 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003659 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003660 reply_len = -1;
3661 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003662 reply_len = dpp_configurator_get_key_id(
3663 hapd->iface->interfaces->dpp,
3664 atoi(buf + 25),
3665 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003666 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3667 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3668 if (res < 0) {
3669 reply_len = -1;
3670 } else {
3671 reply_len = os_snprintf(reply, reply_size, "%d", res);
3672 if (os_snprintf_error(reply_size, reply_len))
3673 reply_len = -1;
3674 }
3675 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3676 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3677 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003678#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07003679 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
3680 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
3681 reply_len = -1;
3682 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
3683 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
3684 reply_len = -1;
3685 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
3686 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003687 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
3688 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
3689 reply_len = -1;
3690 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
3691 hostapd_dpp_chirp_stop(hapd);
Sunil Ravi89eba102022-09-13 21:04:37 -07003692 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
3693 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
3694 reply_len = -1;
3695 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
3696 hostapd_dpp_remove_controller(hapd, buf + 28);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003697#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07003698#ifdef CONFIG_DPP3
3699 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
3700 if (hostapd_dpp_push_button(hapd, NULL) < 0)
3701 reply_len = -1;
3702 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
3703 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
3704 reply_len = -1;
3705#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003706#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003707#ifdef RADIUS_SERVER
3708 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3709 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3710 reply_len = -1;
3711#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07003712 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3713 reply_len = hostapd_ctrl_iface_get_capability(
3714 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08003715#ifdef CONFIG_PASN
3716 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
3717 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
3718#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003719#ifdef ANDROID
3720 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
3721 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
3722 reply_size);
3723#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724 } else {
3725 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3726 reply_len = 16;
3727 }
3728
3729 if (reply_len < 0) {
3730 os_memcpy(reply, "FAIL\n", 5);
3731 reply_len = 5;
3732 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003733
3734 return reply_len;
3735}
3736
3737
3738static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3739 void *sock_ctx)
3740{
3741 struct hostapd_data *hapd = eloop_ctx;
3742 char buf[4096];
3743 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003744 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003745 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003746 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003747 const int reply_size = 4096;
3748 int reply_len;
3749 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003750#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07003751 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003752#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003753
3754 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3755 (struct sockaddr *) &from, &fromlen);
3756 if (res < 0) {
3757 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3758 strerror(errno));
3759 return;
3760 }
3761 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003762
3763 reply = os_malloc(reply_size);
3764 if (reply == NULL) {
3765 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3766 fromlen) < 0) {
3767 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3768 strerror(errno));
3769 }
3770 return;
3771 }
3772
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003773#ifdef CONFIG_CTRL_IFACE_UDP
3774 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3775 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07003776 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
3777 hapd->ctrl_iface_cookie,
3778 CTRL_IFACE_COOKIE_LEN);
3779 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003780 goto done;
3781 }
3782
3783 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07003784 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003785 wpa_printf(MSG_DEBUG,
3786 "CTRL: No cookie in the request - drop request");
3787 os_free(reply);
3788 return;
3789 }
3790
Hai Shalomfdcde762020-04-02 11:19:20 -07003791 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
3792 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003793 wpa_printf(MSG_DEBUG,
3794 "CTRL: Invalid cookie in the request - drop request");
3795 os_free(reply);
3796 return;
3797 }
3798
Hai Shalomfdcde762020-04-02 11:19:20 -07003799 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003800 while (*pos == ' ')
3801 pos++;
3802#endif /* CONFIG_CTRL_IFACE_UDP */
3803
3804 if (os_strcmp(pos, "PING") == 0)
3805 level = MSG_EXCESSIVE;
3806 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3807
3808 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003809 reply, reply_size,
3810 &from, fromlen);
3811
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003812#ifdef CONFIG_CTRL_IFACE_UDP
3813done:
3814#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003815 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3816 fromlen) < 0) {
3817 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3818 strerror(errno));
3819 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 os_free(reply);
3821}
3822
3823
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003824#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3826{
3827 char *buf;
3828 size_t len;
3829
3830 if (hapd->conf->ctrl_interface == NULL)
3831 return NULL;
3832
3833 len = os_strlen(hapd->conf->ctrl_interface) +
3834 os_strlen(hapd->conf->iface) + 2;
3835 buf = os_malloc(len);
3836 if (buf == NULL)
3837 return NULL;
3838
3839 os_snprintf(buf, len, "%s/%s",
3840 hapd->conf->ctrl_interface, hapd->conf->iface);
3841 buf[len - 1] = '\0';
3842 return buf;
3843}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003844#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845
3846
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07003847static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3848 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 const char *txt, size_t len)
3850{
3851 struct hostapd_data *hapd = ctx;
3852 if (hapd == NULL)
3853 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003854 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855}
3856
3857
3858int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3859{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003860#ifdef CONFIG_CTRL_IFACE_UDP
3861 int port = HOSTAPD_CTRL_IFACE_PORT;
3862 char p[32] = { 0 };
3863 char port_str[40], *tmp;
3864 char *pos;
3865 struct addrinfo hints = { 0 }, *res, *saveres;
3866 int n;
3867
3868 if (hapd->ctrl_sock > -1) {
3869 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3870 return 0;
3871 }
3872
3873 if (hapd->conf->ctrl_interface == NULL)
3874 return 0;
3875
3876 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3877 if (pos) {
3878 pos += 4;
3879 port = atoi(pos);
3880 if (port <= 0) {
3881 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3882 goto fail;
3883 }
3884 }
3885
3886 dl_list_init(&hapd->ctrl_dst);
3887 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003888 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003889
3890#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3891 hints.ai_flags = AI_PASSIVE;
3892#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3893
3894#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3895 hints.ai_family = AF_INET6;
3896#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3897 hints.ai_family = AF_INET;
3898#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3899 hints.ai_socktype = SOCK_DGRAM;
3900
3901try_again:
3902 os_snprintf(p, sizeof(p), "%d", port);
3903 n = getaddrinfo(NULL, p, &hints, &res);
3904 if (n) {
3905 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3906 goto fail;
3907 }
3908
3909 saveres = res;
3910 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3911 res->ai_protocol);
3912 if (hapd->ctrl_sock < 0) {
3913 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3914 goto fail;
3915 }
3916
3917 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3918 port--;
3919 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
3920 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
3921 goto try_again;
3922 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3923 goto fail;
3924 }
3925
3926 freeaddrinfo(saveres);
3927
3928 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3929 tmp = os_strdup(port_str);
3930 if (tmp) {
3931 os_free(hapd->conf->ctrl_interface);
3932 hapd->conf->ctrl_interface = tmp;
3933 }
3934 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3935
3936 if (eloop_register_read_sock(hapd->ctrl_sock,
3937 hostapd_ctrl_iface_receive, hapd, NULL) <
3938 0) {
3939 hostapd_ctrl_iface_deinit(hapd);
3940 return -1;
3941 }
3942
3943 hapd->msg_ctx = hapd;
3944 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3945
3946 return 0;
3947
3948fail:
3949 if (hapd->ctrl_sock >= 0)
3950 close(hapd->ctrl_sock);
3951 return -1;
3952#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 struct sockaddr_un addr;
3954 int s = -1;
3955 char *fname = NULL;
3956
Dmitry Shmidt04949592012-07-19 12:16:46 -07003957 if (hapd->ctrl_sock > -1) {
3958 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3959 return 0;
3960 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003962 dl_list_init(&hapd->ctrl_dst);
3963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 if (hapd->conf->ctrl_interface == NULL)
3965 return 0;
3966
3967 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3968 if (errno == EEXIST) {
3969 wpa_printf(MSG_DEBUG, "Using existing control "
3970 "interface directory.");
3971 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003972 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3973 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003974 goto fail;
3975 }
3976 }
3977
3978 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08003979 lchown(hapd->conf->ctrl_interface, -1,
3980 hapd->conf->ctrl_interface_gid) < 0) {
3981 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003982 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983 return -1;
3984 }
3985
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003986 if (!hapd->conf->ctrl_interface_gid_set &&
3987 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08003988 lchown(hapd->conf->ctrl_interface, -1,
3989 hapd->iface->interfaces->ctrl_iface_group) < 0) {
3990 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003991 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003992 return -1;
3993 }
3994
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003995#ifdef ANDROID
3996 /*
3997 * Android is using umask 0077 which would leave the control interface
3998 * directory without group access. This breaks things since Wi-Fi
3999 * framework assumes that this directory can be accessed by other
4000 * applications in the wifi group. Fix this by adding group access even
4001 * if umask value would prevent this.
4002 */
4003 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4004 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4005 strerror(errno));
4006 /* Try to continue anyway */
4007 }
4008#endif /* ANDROID */
4009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4011 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4012 goto fail;
4013
4014 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4015 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004016 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 goto fail;
4018 }
4019
4020 os_memset(&addr, 0, sizeof(addr));
4021#ifdef __FreeBSD__
4022 addr.sun_len = sizeof(addr);
4023#endif /* __FreeBSD__ */
4024 addr.sun_family = AF_UNIX;
4025 fname = hostapd_ctrl_iface_path(hapd);
4026 if (fname == NULL)
4027 goto fail;
4028 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4029 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4030 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4031 strerror(errno));
4032 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4033 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4034 " allow connections - assuming it was left"
4035 "over from forced program termination");
4036 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004037 wpa_printf(MSG_ERROR,
4038 "Could not unlink existing ctrl_iface socket '%s': %s",
4039 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004040 goto fail;
4041 }
4042 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4043 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004044 wpa_printf(MSG_ERROR,
4045 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4046 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047 goto fail;
4048 }
4049 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4050 "ctrl_iface socket '%s'", fname);
4051 } else {
4052 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4053 "be in use - cannot override it");
4054 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4055 "not used anymore", fname);
4056 os_free(fname);
4057 fname = NULL;
4058 goto fail;
4059 }
4060 }
4061
4062 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004063 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4064 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004065 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066 goto fail;
4067 }
4068
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004069 if (!hapd->conf->ctrl_interface_gid_set &&
4070 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004071 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4072 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004073 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004074 goto fail;
4075 }
4076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004078 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4079 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080 goto fail;
4081 }
4082 os_free(fname);
4083
4084 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004085 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4086 NULL) < 0) {
4087 hostapd_ctrl_iface_deinit(hapd);
4088 return -1;
4089 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 hapd->msg_ctx = hapd;
4091 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4092
4093 return 0;
4094
4095fail:
4096 if (s >= 0)
4097 close(s);
4098 if (fname) {
4099 unlink(fname);
4100 os_free(fname);
4101 }
4102 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004103#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104}
4105
4106
4107void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4108{
4109 struct wpa_ctrl_dst *dst, *prev;
4110
4111 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004112#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004114#endif /* !CONFIG_CTRL_IFACE_UDP */
4115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116 eloop_unregister_read_sock(hapd->ctrl_sock);
4117 close(hapd->ctrl_sock);
4118 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004119#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 fname = hostapd_ctrl_iface_path(hapd);
4121 if (fname)
4122 unlink(fname);
4123 os_free(fname);
4124
4125 if (hapd->conf->ctrl_interface &&
4126 rmdir(hapd->conf->ctrl_interface) < 0) {
4127 if (errno == ENOTEMPTY) {
4128 wpa_printf(MSG_DEBUG, "Control interface "
4129 "directory not empty - leaving it "
4130 "behind");
4131 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004132 wpa_printf(MSG_ERROR,
4133 "rmdir[ctrl_interface=%s]: %s",
4134 hapd->conf->ctrl_interface,
4135 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 }
4137 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004138#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 }
4140
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004141 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4142 list)
4143 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004144
4145#ifdef CONFIG_TESTING_OPTIONS
4146 l2_packet_deinit(hapd->l2_test);
4147 hapd->l2_test = NULL;
4148#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149}
4150
4151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004152static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4153 char *buf)
4154{
4155 if (hostapd_add_iface(interfaces, buf) < 0) {
4156 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4157 return -1;
4158 }
4159 return 0;
4160}
4161
4162
4163static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4164 char *buf)
4165{
4166 if (hostapd_remove_iface(interfaces, buf) < 0) {
4167 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4168 return -1;
4169 }
4170 return 0;
4171}
4172
4173
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004174static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004175 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004176 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004177{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004178 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4179 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004180}
4181
4182
4183static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004184 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004185 socklen_t fromlen)
4186{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004187 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004188}
4189
4190
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004191static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4192{
4193#ifdef CONFIG_WPS_TESTING
4194 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004195 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004196 wps_corrupt_pkhash = 0;
4197#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004198
4199#ifdef CONFIG_TESTING_OPTIONS
4200#ifdef CONFIG_DPP
4201 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004202#ifdef CONFIG_DPP3
4203 dpp_version_override = 3;
4204#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004205 dpp_version_override = 2;
4206#else /* CONFIG_DPP2 */
4207 dpp_version_override = 1;
4208#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004209#endif /* CONFIG_DPP */
4210#endif /* CONFIG_TESTING_OPTIONS */
4211
4212#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004213 dpp_global_clear(interfaces->dpp);
Sunil Ravi89eba102022-09-13 21:04:37 -07004214#ifdef CONFIG_DPP3
4215 {
4216 int i;
4217
4218 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
4219 struct dpp_pb_info *info;
4220
4221 info = &interfaces->dpp_pb[i];
4222 info->rx_time.sec = 0;
4223 info->rx_time.usec = 0;
4224 }
4225 }
4226#endif /* CONFIG_DPP3 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004227#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004228}
4229
4230
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004231#ifdef CONFIG_FST
4232
4233static int
4234hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4235 const char *cmd)
4236{
4237 char ifname[IFNAMSIZ + 1];
4238 struct fst_iface_cfg cfg;
4239 struct hostapd_data *hapd;
4240 struct fst_wpa_obj iface_obj;
4241
4242 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4243 hapd = hostapd_get_iface(interfaces, ifname);
4244 if (hapd) {
4245 if (hapd->iface->fst) {
4246 wpa_printf(MSG_INFO, "FST: Already attached");
4247 return -1;
4248 }
4249 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4250 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4251 &iface_obj, &cfg);
4252 if (hapd->iface->fst)
4253 return 0;
4254 }
4255 }
4256
4257 return -EINVAL;
4258}
4259
4260
4261static int
4262hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4263 const char *cmd)
4264{
4265 char ifname[IFNAMSIZ + 1];
4266 struct hostapd_data * hapd;
4267
4268 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4269 hapd = hostapd_get_iface(interfaces, ifname);
4270 if (hapd) {
4271 if (!fst_iface_detach(ifname)) {
4272 hapd->iface->fst = NULL;
4273 hapd->iface->fst_ies = NULL;
4274 return 0;
4275 }
4276 }
4277 }
4278
4279 return -EINVAL;
4280}
4281
4282#endif /* CONFIG_FST */
4283
4284
4285static struct hostapd_data *
4286hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4287 const char *ifname)
4288{
4289 size_t i, j;
4290
4291 for (i = 0; i < interfaces->count; i++) {
4292 struct hostapd_iface *iface = interfaces->iface[i];
4293
4294 for (j = 0; j < iface->num_bss; j++) {
4295 struct hostapd_data *hapd;
4296
4297 hapd = iface->bss[j];
4298 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4299 return hapd;
4300 }
4301 }
4302
4303 return NULL;
4304}
4305
4306
4307static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4308 struct hostapd_data *dst_hapd,
4309 const char *param)
4310{
4311 int res;
4312 char *value;
4313
4314 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4315 if (!value) {
4316 wpa_printf(MSG_ERROR,
4317 "DUP: cannot allocate buffer to stringify %s",
4318 param);
4319 goto error_return;
4320 }
4321
4322 if (os_strcmp(param, "wpa") == 0) {
4323 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4324 src_hapd->conf->wpa);
4325 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4326 src_hapd->conf->wpa_key_mgmt) {
4327 res = hostapd_ctrl_iface_get_key_mgmt(
4328 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4329 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4330 goto error_stringify;
4331 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4332 src_hapd->conf->wpa_pairwise) {
4333 res = wpa_write_ciphers(value,
4334 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4335 src_hapd->conf->wpa_pairwise, " ");
4336 if (res < 0)
4337 goto error_stringify;
4338 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4339 src_hapd->conf->rsn_pairwise) {
4340 res = wpa_write_ciphers(value,
4341 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4342 src_hapd->conf->rsn_pairwise, " ");
4343 if (res < 0)
4344 goto error_stringify;
4345 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4346 src_hapd->conf->ssid.wpa_passphrase) {
4347 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4348 src_hapd->conf->ssid.wpa_passphrase);
4349 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4350 src_hapd->conf->ssid.wpa_psk_set) {
4351 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4352 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4353 } else {
4354 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4355 goto error_return;
4356 }
4357
4358 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4359 os_free(value);
4360 return res;
4361
4362error_stringify:
4363 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4364error_return:
4365 os_free(value);
4366 return -1;
4367}
4368
4369
4370static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004371hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4372 const char *input,
4373 char *reply, int reply_size)
4374{
4375 size_t i, j;
4376 int res;
4377 char *pos, *end;
4378 struct hostapd_iface *iface;
4379 int show_ctrl = 0;
4380
4381 if (input)
4382 show_ctrl = !!os_strstr(input, "ctrl");
4383
4384 pos = reply;
4385 end = reply + reply_size;
4386
4387 for (i = 0; i < interfaces->count; i++) {
4388 iface = interfaces->iface[i];
4389
4390 for (j = 0; j < iface->num_bss; j++) {
4391 struct hostapd_bss_config *conf;
4392
4393 conf = iface->conf->bss[j];
4394 if (show_ctrl)
4395 res = os_snprintf(pos, end - pos,
4396 "%s ctrl_iface=%s\n",
4397 conf->iface,
4398 conf->ctrl_interface ?
4399 conf->ctrl_interface : "N/A");
4400 else
4401 res = os_snprintf(pos, end - pos, "%s\n",
4402 conf->iface);
4403 if (os_snprintf_error(end - pos, res)) {
4404 *pos = '\0';
4405 return pos - reply;
4406 }
4407 pos += res;
4408 }
4409 }
4410
4411 return pos - reply;
4412}
4413
4414
4415static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004416hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4417 char *cmd)
4418{
4419 char *p_start = cmd, *p_end;
4420 struct hostapd_data *src_hapd, *dst_hapd;
4421
4422 /* cmd: "<src ifname> <dst ifname> <variable name> */
4423
4424 p_end = os_strchr(p_start, ' ');
4425 if (!p_end) {
4426 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4427 cmd);
4428 return -1;
4429 }
4430
4431 *p_end = '\0';
4432 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4433 if (!src_hapd) {
4434 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4435 p_start);
4436 return -1;
4437 }
4438
4439 p_start = p_end + 1;
4440 p_end = os_strchr(p_start, ' ');
4441 if (!p_end) {
4442 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4443 cmd);
4444 return -1;
4445 }
4446
4447 *p_end = '\0';
4448 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4449 if (!dst_hapd) {
4450 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4451 p_start);
4452 return -1;
4453 }
4454
4455 p_start = p_end + 1;
4456 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4457}
4458
4459
4460static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4461 const char *ifname,
4462 char *buf, char *reply,
4463 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004464 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004465 socklen_t fromlen)
4466{
4467 struct hostapd_data *hapd;
4468
4469 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4470 if (hapd == NULL) {
4471 int res;
4472
4473 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4474 if (os_snprintf_error(reply_size, res))
4475 return -1;
4476 return res;
4477 }
4478
4479 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4480 from, fromlen);
4481}
4482
4483
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004484static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4485 void *sock_ctx)
4486{
Hai Shalomfdcde762020-04-02 11:19:20 -07004487 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004488 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004489 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004490 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004491 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004492 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004493 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004494 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004495#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004496 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004497#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004498
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004499 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004500 (struct sockaddr *) &from, &fromlen);
4501 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004502 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4503 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004504 return;
4505 }
4506 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004507 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004508
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004509 reply = os_malloc(reply_size);
4510 if (reply == NULL) {
4511 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4512 fromlen) < 0) {
4513 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4514 strerror(errno));
4515 }
4516 return;
4517 }
4518
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004519 os_memcpy(reply, "OK\n", 3);
4520 reply_len = 3;
4521
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004522#ifdef CONFIG_CTRL_IFACE_UDP
4523 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4524 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004525 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4526 interfaces->ctrl_iface_cookie,
4527 CTRL_IFACE_COOKIE_LEN);
4528 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004529 goto send_reply;
4530 }
4531
4532 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004533 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004534 wpa_printf(MSG_DEBUG,
4535 "CTRL: No cookie in the request - drop request");
4536 os_free(reply);
4537 return;
4538 }
4539
Hai Shalomfdcde762020-04-02 11:19:20 -07004540 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4541 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004542 wpa_printf(MSG_DEBUG,
4543 "CTRL: Invalid cookie in the request - drop request");
4544 os_free(reply);
4545 return;
4546 }
4547
Hai Shalomfdcde762020-04-02 11:19:20 -07004548 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004549 while (*buf == ' ')
4550 buf++;
4551#endif /* CONFIG_CTRL_IFACE_UDP */
4552
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004553 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4554 char *pos = os_strchr(buf + 7, ' ');
4555
4556 if (pos) {
4557 *pos++ = '\0';
4558 reply_len = hostapd_global_ctrl_iface_ifname(
4559 interfaces, buf + 7, pos, reply, reply_size,
4560 &from, fromlen);
4561 goto send_reply;
4562 }
4563 }
4564
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004565 if (os_strcmp(buf, "PING") == 0) {
4566 os_memcpy(reply, "PONG\n", 5);
4567 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004568 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4569 if (wpa_debug_reopen_file() < 0)
4570 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004571 } else if (os_strcmp(buf, "FLUSH") == 0) {
4572 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004573 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4574 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4575 reply_len = -1;
4576 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4577 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4578 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004579 } else if (os_strcmp(buf, "ATTACH") == 0) {
4580 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004581 fromlen, NULL))
4582 reply_len = -1;
4583 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4584 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4585 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004586 reply_len = -1;
4587 } else if (os_strcmp(buf, "DETACH") == 0) {
4588 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4589 fromlen))
4590 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004591#ifdef CONFIG_MODULE_TESTS
4592 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004593 if (hapd_module_tests() < 0)
4594 reply_len = -1;
4595#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004596#ifdef CONFIG_FST
4597 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4598 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4599 reply_len = os_snprintf(reply, reply_size, "OK\n");
4600 else
4601 reply_len = -1;
4602 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4603 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4604 reply_len = os_snprintf(reply, reply_size, "OK\n");
4605 else
4606 reply_len = -1;
4607 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4608 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4609#endif /* CONFIG_FST */
4610 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4611 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4612 buf + 12))
4613 reply_len = os_snprintf(reply, reply_size, "OK\n");
4614 else
4615 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004616 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4617 reply_len = hostapd_global_ctrl_iface_interfaces(
4618 interfaces, buf + 10, reply, sizeof(buffer));
4619 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4620 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004621 } else {
4622 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4623 "ignored");
4624 reply_len = -1;
4625 }
4626
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004627send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004628 if (reply_len < 0) {
4629 os_memcpy(reply, "FAIL\n", 5);
4630 reply_len = 5;
4631 }
4632
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004633 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4634 fromlen) < 0) {
4635 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4636 strerror(errno));
4637 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004638 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004639}
4640
4641
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004642#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004643static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4644{
4645 char *buf;
4646 size_t len;
4647
4648 if (interface->global_iface_path == NULL)
4649 return NULL;
4650
4651 len = os_strlen(interface->global_iface_path) +
4652 os_strlen(interface->global_iface_name) + 2;
4653 buf = os_malloc(len);
4654 if (buf == NULL)
4655 return NULL;
4656
4657 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4658 interface->global_iface_name);
4659 buf[len - 1] = '\0';
4660 return buf;
4661}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004662#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004663
4664
4665int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4666{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004667#ifdef CONFIG_CTRL_IFACE_UDP
4668 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4669 char p[32] = { 0 };
4670 char *pos;
4671 struct addrinfo hints = { 0 }, *res, *saveres;
4672 int n;
4673
4674 if (interface->global_ctrl_sock > -1) {
4675 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4676 return 0;
4677 }
4678
4679 if (interface->global_iface_path == NULL)
4680 return 0;
4681
4682 pos = os_strstr(interface->global_iface_path, "udp:");
4683 if (pos) {
4684 pos += 4;
4685 port = atoi(pos);
4686 if (port <= 0) {
4687 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4688 goto fail;
4689 }
4690 }
4691
Hai Shalomfdcde762020-04-02 11:19:20 -07004692 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004693
4694#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4695 hints.ai_flags = AI_PASSIVE;
4696#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4697
4698#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4699 hints.ai_family = AF_INET6;
4700#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4701 hints.ai_family = AF_INET;
4702#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4703 hints.ai_socktype = SOCK_DGRAM;
4704
4705try_again:
4706 os_snprintf(p, sizeof(p), "%d", port);
4707 n = getaddrinfo(NULL, p, &hints, &res);
4708 if (n) {
4709 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4710 goto fail;
4711 }
4712
4713 saveres = res;
4714 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4715 res->ai_protocol);
4716 if (interface->global_ctrl_sock < 0) {
4717 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4718 goto fail;
4719 }
4720
4721 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4722 0) {
4723 port++;
4724 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4725 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4726 goto try_again;
4727 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4728 goto fail;
4729 }
4730
4731 freeaddrinfo(saveres);
4732
4733 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4734
4735 if (eloop_register_read_sock(interface->global_ctrl_sock,
4736 hostapd_global_ctrl_iface_receive,
4737 interface, NULL) < 0) {
4738 hostapd_global_ctrl_iface_deinit(interface);
4739 return -1;
4740 }
4741
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004742 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4743
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004744 return 0;
4745
4746fail:
4747 if (interface->global_ctrl_sock >= 0)
4748 close(interface->global_ctrl_sock);
4749 return -1;
4750#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004751 struct sockaddr_un addr;
4752 int s = -1;
4753 char *fname = NULL;
4754
4755 if (interface->global_iface_path == NULL) {
4756 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4757 return 0;
4758 }
4759
4760 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4761 if (errno == EEXIST) {
4762 wpa_printf(MSG_DEBUG, "Using existing control "
4763 "interface directory.");
4764 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004765 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4766 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004767 goto fail;
4768 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004769 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004770 lchown(interface->global_iface_path, -1,
4771 interface->ctrl_iface_group) < 0) {
4772 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004773 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004774 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004775 }
4776
4777 if (os_strlen(interface->global_iface_path) + 1 +
4778 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4779 goto fail;
4780
4781 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4782 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004783 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004784 goto fail;
4785 }
4786
4787 os_memset(&addr, 0, sizeof(addr));
4788#ifdef __FreeBSD__
4789 addr.sun_len = sizeof(addr);
4790#endif /* __FreeBSD__ */
4791 addr.sun_family = AF_UNIX;
4792 fname = hostapd_global_ctrl_iface_path(interface);
4793 if (fname == NULL)
4794 goto fail;
4795 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4796 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4797 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4798 strerror(errno));
4799 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4800 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4801 " allow connections - assuming it was left"
4802 "over from forced program termination");
4803 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004804 wpa_printf(MSG_ERROR,
4805 "Could not unlink existing ctrl_iface socket '%s': %s",
4806 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004807 goto fail;
4808 }
4809 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4810 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004811 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4812 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004813 goto fail;
4814 }
4815 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4816 "ctrl_iface socket '%s'", fname);
4817 } else {
4818 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4819 "be in use - cannot override it");
4820 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4821 "not used anymore", fname);
4822 os_free(fname);
4823 fname = NULL;
4824 goto fail;
4825 }
4826 }
4827
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004828 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004829 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4830 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004831 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004832 goto fail;
4833 }
4834
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004835 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004836 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4837 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004838 goto fail;
4839 }
4840 os_free(fname);
4841
4842 interface->global_ctrl_sock = s;
4843 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4844 interface, NULL);
4845
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004846 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4847
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004848 return 0;
4849
4850fail:
4851 if (s >= 0)
4852 close(s);
4853 if (fname) {
4854 unlink(fname);
4855 os_free(fname);
4856 }
4857 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004858#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004859}
4860
4861
4862void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4863{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004864#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004865 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004866#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004867 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004868
4869 if (interfaces->global_ctrl_sock > -1) {
4870 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4871 close(interfaces->global_ctrl_sock);
4872 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004873#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004874 fname = hostapd_global_ctrl_iface_path(interfaces);
4875 if (fname) {
4876 unlink(fname);
4877 os_free(fname);
4878 }
4879
4880 if (interfaces->global_iface_path &&
4881 rmdir(interfaces->global_iface_path) < 0) {
4882 if (errno == ENOTEMPTY) {
4883 wpa_printf(MSG_DEBUG, "Control interface "
4884 "directory not empty - leaving it "
4885 "behind");
4886 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004887 wpa_printf(MSG_ERROR,
4888 "rmdir[ctrl_interface=%s]: %s",
4889 interfaces->global_iface_path,
4890 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004891 }
4892 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004893#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004894 }
4895
4896 os_free(interfaces->global_iface_path);
4897 interfaces->global_iface_path = NULL;
4898
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004899 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4900 struct wpa_ctrl_dst, list)
4901 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004902}
4903
4904
Roshan Pius3a1667e2018-07-03 15:17:14 -07004905static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4906 const char *buf)
4907{
4908 /* Enable Probe Request events based on explicit request.
4909 * Other events are enabled by default.
4910 */
4911 if (str_starts(buf, RX_PROBE_REQUEST))
4912 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4913 return 1;
4914}
4915
4916
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004917static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
4918 const char *ifname, int level,
4919 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920{
4921 struct wpa_ctrl_dst *dst, *next;
4922 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004923 int idx, res;
4924 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925 char levelstr[10];
4926
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004927 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928 return;
4929
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004930 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4931 if (os_snprintf_error(sizeof(levelstr), res))
4932 return;
4933 idx = 0;
4934 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004935 io[idx].iov_base = "IFNAME=";
4936 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004937 idx++;
4938 io[idx].iov_base = (char *) ifname;
4939 io[idx].iov_len = os_strlen(ifname);
4940 idx++;
4941 io[idx].iov_base = " ";
4942 io[idx].iov_len = 1;
4943 idx++;
4944 }
4945 io[idx].iov_base = levelstr;
4946 io[idx].iov_len = os_strlen(levelstr);
4947 idx++;
4948 io[idx].iov_base = (char *) buf;
4949 io[idx].iov_len = len;
4950 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 os_memset(&msg, 0, sizeof(msg));
4952 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004953 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004954
4955 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004956 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004957 if ((level >= dst->debug_level) &&
4958 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004959 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4960 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 msg.msg_name = &dst->addr;
4962 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004963 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964 int _errno = errno;
4965 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4966 "%d - %s",
4967 idx, errno, strerror(errno));
4968 dst->errors++;
4969 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004970 ctrl_iface_detach(ctrl_dst,
4971 &dst->addr,
4972 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 }
4974 } else
4975 dst->errors = 0;
4976 }
4977 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978 }
4979}
4980
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004981
4982static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
4983 enum wpa_msg_type type,
4984 const char *buf, size_t len)
4985{
4986 if (type != WPA_MSG_NO_GLOBAL) {
4987 hostapd_ctrl_iface_send_internal(
4988 hapd->iface->interfaces->global_ctrl_sock,
4989 &hapd->iface->interfaces->global_ctrl_dst,
4990 type != WPA_MSG_PER_INTERFACE ?
4991 NULL : hapd->conf->iface,
4992 level, buf, len);
4993 }
4994
4995 if (type != WPA_MSG_ONLY_GLOBAL) {
4996 hostapd_ctrl_iface_send_internal(
4997 hapd->ctrl_sock, &hapd->ctrl_dst,
4998 NULL, level, buf, len);
4999 }
5000}
5001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002#endif /* CONFIG_NATIVE_WINDOWS */