blob: a6b16c2f77870c47758ef7d975ea8f777047b65e [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / UNIX domain socket -based control interface
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080013#ifdef CONFIG_TESTING_OPTIONS
Hai Shalomfdcde762020-04-02 11:19:20 -070014#ifdef __NetBSD__
15#include <net/if_ether.h>
16#else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080017#include <net/ethernet.h>
Hai Shalomfdcde762020-04-02 11:19:20 -070018#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080019#include <netinet/ip.h>
20#endif /* CONFIG_TESTING_OPTIONS */
21
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <sys/un.h>
23#include <sys/stat.h>
24#include <stddef.h>
25
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080026#ifdef CONFIG_CTRL_IFACE_UDP
27#include <netdb.h>
28#endif /* CONFIG_CTRL_IFACE_UDP */
29
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "utils/common.h"
31#include "utils/eloop.h"
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070032#include "utils/module_tests.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "common/version.h"
34#include "common/ieee802_11_defs.h"
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080035#include "common/ctrl_iface_common.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070036#ifdef CONFIG_DPP
37#include "common/dpp.h"
38#endif /* CONFIG_DPP */
39#include "common/wpa_ctrl.h"
Hai Shalom60840252021-02-19 19:02:11 -080040#include "common/ptksa_cache.h"
Sunil Ravi036cec52023-03-29 11:35:17 -070041#include "common/hw_features_common.h"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000042#include "common/nan_de.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080043#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080045#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080047#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080048#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049#include "ap/hostapd.h"
50#include "ap/ap_config.h"
51#include "ap/ieee802_1x.h"
52#include "ap/wpa_auth.h"
Hai Shalom899fcc72020-10-19 14:38:18 -070053#include "ap/pmksa_cache_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054#include "ap/ieee802_11.h"
55#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056#include "ap/wps_hostapd.h"
57#include "ap/ctrl_iface_ap.h"
58#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080059#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080060#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070061#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080062#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070063#include "ap/neighbor_db.h"
64#include "ap/rrm.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070065#include "ap/dpp_hostapd.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070066#include "ap/dfs.h"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000067#include "ap/nan_usd_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068#include "wps/wps_defs.h"
69#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080070#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072#include "ctrl_iface.h"
73
74
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080075#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
76
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080077#ifdef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080078#define HOSTAPD_CTRL_IFACE_PORT 8877
79#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
80#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
81#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
82#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083
84static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020085 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 const char *buf, size_t len);
87
88
89static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080090 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -070091 socklen_t fromlen, const char *input)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092{
Roshan Pius3a1667e2018-07-03 15:17:14 -070093 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094}
95
96
97static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080098 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 socklen_t fromlen)
100{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800101 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102}
103
104
105static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800106 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107 socklen_t fromlen,
108 char *level)
109{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800110 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111}
112
113
114static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
115 const char *txtaddr)
116{
117 u8 addr[ETH_ALEN];
118 struct sta_info *sta;
119
120 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
121
122 if (hwaddr_aton(txtaddr, addr))
123 return -1;
124
125 sta = ap_get_sta(hapd, addr);
126 if (sta)
127 return 0;
128
129 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
130 "notification", MAC2STR(addr));
131 sta = ap_sta_add(hapd, addr);
132 if (sta == NULL)
133 return -1;
134
135 hostapd_new_assoc_sta(hapd, sta, 0);
136 return 0;
137}
138
139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140#ifdef NEED_AP_MLME
141static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
142 const char *txtaddr)
143{
144 u8 addr[ETH_ALEN];
145 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
146
147 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
148
149 if (hwaddr_aton(txtaddr, addr) ||
150 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
151 return -1;
152
153 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
154
155 return 0;
156}
157#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158
159
160#ifdef CONFIG_WPS
161static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
162{
163 char *pin = os_strchr(txt, ' ');
164 char *timeout_txt;
165 int timeout;
166 u8 addr_buf[ETH_ALEN], *addr = NULL;
167 char *pos;
168
169 if (pin == NULL)
170 return -1;
171 *pin++ = '\0';
172
173 timeout_txt = os_strchr(pin, ' ');
174 if (timeout_txt) {
175 *timeout_txt++ = '\0';
176 timeout = atoi(timeout_txt);
177 pos = os_strchr(timeout_txt, ' ');
178 if (pos) {
179 *pos++ = '\0';
180 if (hwaddr_aton(pos, addr_buf) == 0)
181 addr = addr_buf;
182 }
183 } else
184 timeout = 0;
185
186 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
187}
188
189
190static int hostapd_ctrl_iface_wps_check_pin(
191 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
192{
193 char pin[9];
194 size_t len;
195 char *pos;
196 int ret;
197
198 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
199 (u8 *) cmd, os_strlen(cmd));
200 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
201 if (*pos < '0' || *pos > '9')
202 continue;
203 pin[len++] = *pos;
204 if (len == 9) {
205 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
206 return -1;
207 }
208 }
209 if (len != 4 && len != 8) {
210 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
211 return -1;
212 }
213 pin[len] = '\0';
214
215 if (len == 8) {
216 unsigned int pin_val;
217 pin_val = atoi(pin);
218 if (!wps_pin_valid(pin_val)) {
219 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
220 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800221 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 return -1;
223 return ret;
224 }
225 }
226
227 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800228 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229 return -1;
230
231 return ret;
232}
233
234
Dmitry Shmidt04949592012-07-19 12:16:46 -0700235#ifdef CONFIG_WPS_NFC
236static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
237 char *pos)
238{
239 size_t len;
240 struct wpabuf *buf;
241 int ret;
242
243 len = os_strlen(pos);
244 if (len & 0x01)
245 return -1;
246 len /= 2;
247
248 buf = wpabuf_alloc(len);
249 if (buf == NULL)
250 return -1;
251 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
252 wpabuf_free(buf);
253 return -1;
254 }
255
256 ret = hostapd_wps_nfc_tag_read(hapd, buf);
257 wpabuf_free(buf);
258
259 return ret;
260}
261
262
263static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
264 char *cmd, char *reply,
265 size_t max_len)
266{
267 int ndef;
268 struct wpabuf *buf;
269 int res;
270
271 if (os_strcmp(cmd, "WPS") == 0)
272 ndef = 0;
273 else if (os_strcmp(cmd, "NDEF") == 0)
274 ndef = 1;
275 else
276 return -1;
277
278 buf = hostapd_wps_nfc_config_token(hapd, ndef);
279 if (buf == NULL)
280 return -1;
281
282 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
283 wpabuf_len(buf));
284 reply[res++] = '\n';
285 reply[res] = '\0';
286
287 wpabuf_free(buf);
288
289 return res;
290}
291
292
293static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
294 char *reply, size_t max_len,
295 int ndef)
296{
297 struct wpabuf *buf;
298 int res;
299
300 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
301 if (buf == NULL)
302 return -1;
303
304 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
305 wpabuf_len(buf));
306 reply[res++] = '\n';
307 reply[res] = '\0';
308
309 wpabuf_free(buf);
310
311 return res;
312}
313
314
315static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
316 char *cmd, char *reply,
317 size_t max_len)
318{
319 if (os_strcmp(cmd, "WPS") == 0)
320 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
321 max_len, 0);
322
323 if (os_strcmp(cmd, "NDEF") == 0)
324 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
325 max_len, 1);
326
327 if (os_strcmp(cmd, "enable") == 0)
328 return hostapd_wps_nfc_token_enable(hapd);
329
330 if (os_strcmp(cmd, "disable") == 0) {
331 hostapd_wps_nfc_token_disable(hapd);
332 return 0;
333 }
334
335 return -1;
336}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800337
338
339static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
340 char *cmd, char *reply,
341 size_t max_len)
342{
343 struct wpabuf *buf;
344 int res;
345 char *pos;
346 int ndef;
347
348 pos = os_strchr(cmd, ' ');
349 if (pos == NULL)
350 return -1;
351 *pos++ = '\0';
352
353 if (os_strcmp(cmd, "WPS") == 0)
354 ndef = 0;
355 else if (os_strcmp(cmd, "NDEF") == 0)
356 ndef = 1;
357 else
358 return -1;
359
360 if (os_strcmp(pos, "WPS-CR") == 0)
361 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
362 else
363 buf = NULL;
364 if (buf == NULL)
365 return -1;
366
367 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
368 wpabuf_len(buf));
369 reply[res++] = '\n';
370 reply[res] = '\0';
371
372 wpabuf_free(buf);
373
374 return res;
375}
376
377
378static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
379 char *cmd)
380{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800381 size_t len;
382 struct wpabuf *req, *sel;
383 int ret;
384 char *pos, *role, *type, *pos2;
385
386 role = cmd;
387 pos = os_strchr(role, ' ');
388 if (pos == NULL)
389 return -1;
390 *pos++ = '\0';
391
392 type = pos;
393 pos = os_strchr(type, ' ');
394 if (pos == NULL)
395 return -1;
396 *pos++ = '\0';
397
398 pos2 = os_strchr(pos, ' ');
399 if (pos2 == NULL)
400 return -1;
401 *pos2++ = '\0';
402
403 len = os_strlen(pos);
404 if (len & 0x01)
405 return -1;
406 len /= 2;
407
408 req = wpabuf_alloc(len);
409 if (req == NULL)
410 return -1;
411 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
412 wpabuf_free(req);
413 return -1;
414 }
415
416 len = os_strlen(pos2);
417 if (len & 0x01) {
418 wpabuf_free(req);
419 return -1;
420 }
421 len /= 2;
422
423 sel = wpabuf_alloc(len);
424 if (sel == NULL) {
425 wpabuf_free(req);
426 return -1;
427 }
428 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
429 wpabuf_free(req);
430 wpabuf_free(sel);
431 return -1;
432 }
433
434 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
435 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
436 } else {
437 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
438 "reported: role=%s type=%s", role, type);
439 ret = -1;
440 }
441 wpabuf_free(req);
442 wpabuf_free(sel);
443
444 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800445}
446
Dmitry Shmidt04949592012-07-19 12:16:46 -0700447#endif /* CONFIG_WPS_NFC */
448
449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
451 char *buf, size_t buflen)
452{
453 int timeout = 300;
454 char *pos;
455 const char *pin_txt;
456
457 pos = os_strchr(txt, ' ');
458 if (pos)
459 *pos++ = '\0';
460
461 if (os_strcmp(txt, "disable") == 0) {
462 hostapd_wps_ap_pin_disable(hapd);
463 return os_snprintf(buf, buflen, "OK\n");
464 }
465
466 if (os_strcmp(txt, "random") == 0) {
467 if (pos)
468 timeout = atoi(pos);
469 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
470 if (pin_txt == NULL)
471 return -1;
472 return os_snprintf(buf, buflen, "%s", pin_txt);
473 }
474
475 if (os_strcmp(txt, "get") == 0) {
476 pin_txt = hostapd_wps_ap_pin_get(hapd);
477 if (pin_txt == NULL)
478 return -1;
479 return os_snprintf(buf, buflen, "%s", pin_txt);
480 }
481
482 if (os_strcmp(txt, "set") == 0) {
483 char *pin;
484 if (pos == NULL)
485 return -1;
486 pin = pos;
487 pos = os_strchr(pos, ' ');
488 if (pos) {
489 *pos++ = '\0';
490 timeout = atoi(pos);
491 }
492 if (os_strlen(pin) > buflen)
493 return -1;
494 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
495 return -1;
496 return os_snprintf(buf, buflen, "%s", pin);
497 }
498
499 return -1;
500}
501
502
503static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
504{
505 char *pos;
506 char *ssid, *auth, *encr = NULL, *key = NULL;
507
508 ssid = txt;
509 pos = os_strchr(txt, ' ');
510 if (!pos)
511 return -1;
512 *pos++ = '\0';
513
514 auth = pos;
515 pos = os_strchr(pos, ' ');
516 if (pos) {
517 *pos++ = '\0';
518 encr = pos;
519 pos = os_strchr(pos, ' ');
520 if (pos) {
521 *pos++ = '\0';
522 key = pos;
523 }
524 }
525
526 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
527}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700528
529
530static const char * pbc_status_str(enum pbc_status status)
531{
532 switch (status) {
533 case WPS_PBC_STATUS_DISABLE:
534 return "Disabled";
535 case WPS_PBC_STATUS_ACTIVE:
536 return "Active";
537 case WPS_PBC_STATUS_TIMEOUT:
538 return "Timed-out";
539 case WPS_PBC_STATUS_OVERLAP:
540 return "Overlap";
541 default:
542 return "Unknown";
543 }
544}
545
546
547static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
548 char *buf, size_t buflen)
549{
550 int ret;
551 char *pos, *end;
552
553 pos = buf;
554 end = buf + buflen;
555
556 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
557 pbc_status_str(hapd->wps_stats.pbc_status));
558
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800559 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700560 return pos - buf;
561 pos += ret;
562
563 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
564 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
565 "Success":
566 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
567 "Failed" : "None")));
568
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800569 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700570 return pos - buf;
571 pos += ret;
572
573 /* If status == Failure - Add possible Reasons */
574 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
575 hapd->wps_stats.failure_reason > 0) {
576 ret = os_snprintf(pos, end - pos,
577 "Failure Reason: %s\n",
578 wps_ei_str(hapd->wps_stats.failure_reason));
579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800580 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700581 return pos - buf;
582 pos += ret;
583 }
584
585 if (hapd->wps_stats.status) {
586 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
587 MAC2STR(hapd->wps_stats.peer_addr));
588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800589 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700590 return pos - buf;
591 pos += ret;
592 }
593
594 return pos - buf;
595}
596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597#endif /* CONFIG_WPS */
598
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800599#ifdef CONFIG_HS20
600
601static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
602 const char *cmd)
603{
604 u8 addr[ETH_ALEN];
605 const char *url;
606
607 if (hwaddr_aton(cmd, addr))
608 return -1;
609 url = cmd + 17;
610 if (*url == '\0') {
611 url = NULL;
612 } else {
613 if (*url != ' ')
614 return -1;
615 url++;
616 if (*url == '\0')
617 url = NULL;
618 }
619
620 return hs20_send_wnm_notification(hapd, addr, 1, url);
621}
622
623
624static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
625 const char *cmd)
626{
627 u8 addr[ETH_ALEN];
628 int code, reauth_delay, ret;
629 const char *pos;
630 size_t url_len;
631 struct wpabuf *req;
632
633 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
634 if (hwaddr_aton(cmd, addr))
635 return -1;
636
637 pos = os_strchr(cmd, ' ');
638 if (pos == NULL)
639 return -1;
640 pos++;
641 code = atoi(pos);
642
643 pos = os_strchr(pos, ' ');
644 if (pos == NULL)
645 return -1;
646 pos++;
647 reauth_delay = atoi(pos);
648
649 url_len = 0;
650 pos = os_strchr(pos, ' ');
651 if (pos) {
652 pos++;
653 url_len = os_strlen(pos);
654 }
655
656 req = wpabuf_alloc(4 + url_len);
657 if (req == NULL)
658 return -1;
659 wpabuf_put_u8(req, code);
660 wpabuf_put_le16(req, reauth_delay);
661 wpabuf_put_u8(req, url_len);
662 if (pos)
663 wpabuf_put_data(req, pos, url_len);
664
665 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
666 " to indicate imminent deauthentication (code=%d "
667 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
668 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
669 wpabuf_free(req);
670 return ret;
671}
672
673#endif /* CONFIG_HS20 */
674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675
Dmitry Shmidt051af732013-10-22 13:52:46 -0700676#ifdef CONFIG_INTERWORKING
677
678static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
679 const char *cmd)
680{
681 u8 qos_map_set[16 + 2 * 21], count = 0;
682 const char *pos = cmd;
683 int val, ret;
684
685 for (;;) {
686 if (count == sizeof(qos_map_set)) {
687 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
688 return -1;
689 }
690
691 val = atoi(pos);
692 if (val < 0 || val > 255) {
693 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
694 return -1;
695 }
696
697 qos_map_set[count++] = val;
698 pos = os_strchr(pos, ',');
699 if (!pos)
700 break;
701 pos++;
702 }
703
704 if (count < 16 || count & 1) {
705 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
706 return -1;
707 }
708
709 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
710 if (ret) {
711 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
712 return -1;
713 }
714
715 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
716 hapd->conf->qos_map_set_len = count;
717
718 return 0;
719}
720
721
722static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
723 const char *cmd)
724{
725 u8 addr[ETH_ALEN];
726 struct sta_info *sta;
727 struct wpabuf *buf;
728 u8 *qos_map_set = hapd->conf->qos_map_set;
729 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
730 int ret;
731
732 if (!qos_map_set_len) {
733 wpa_printf(MSG_INFO, "QoS Map Set is not set");
734 return -1;
735 }
736
737 if (hwaddr_aton(cmd, addr))
738 return -1;
739
740 sta = ap_get_sta(hapd, addr);
741 if (sta == NULL) {
742 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
743 "for QoS Map Configuration message",
744 MAC2STR(addr));
745 return -1;
746 }
747
748 if (!sta->qos_map_enabled) {
749 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
750 "support for QoS Map", MAC2STR(addr));
751 return -1;
752 }
753
754 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
755 if (buf == NULL)
756 return -1;
757
758 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
759 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
760
761 /* QoS Map Set Element */
762 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
763 wpabuf_put_u8(buf, qos_map_set_len);
764 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
765
766 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
767 wpabuf_head(buf), wpabuf_len(buf));
768 wpabuf_free(buf);
769
770 return ret;
771}
772
773#endif /* CONFIG_INTERWORKING */
774
775
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700776#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800777
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800778static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
779 const char *cmd)
780{
781 u8 addr[ETH_ALEN];
782 struct sta_info *sta;
783 const char *pos;
784 unsigned int auto_report, timeout;
785
786 if (hwaddr_aton(cmd, addr)) {
787 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
788 return -1;
789 }
790
791 sta = ap_get_sta(hapd, addr);
792 if (!sta) {
793 wpa_printf(MSG_DEBUG, "Station " MACSTR
794 " not found for Collocated Interference Request",
795 MAC2STR(addr));
796 return -1;
797 }
798
799 pos = cmd + 17;
800 if (*pos != ' ')
801 return -1;
802 pos++;
803 auto_report = atoi(pos);
804 pos = os_strchr(pos, ' ');
805 if (!pos)
806 return -1;
807 pos++;
808 timeout = atoi(pos);
809
810 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
811}
812
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700813#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800815
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800816static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
817 char *buf, size_t buflen)
818{
819 int ret = 0;
820 char *pos, *end;
821
822 pos = buf;
823 end = buf + buflen;
824
825 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
826
827 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
828 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
829 if (os_snprintf_error(end - pos, ret))
830 return pos - buf;
831 pos += ret;
832 }
833 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
834 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
835 if (os_snprintf_error(end - pos, ret))
836 return pos - buf;
837 pos += ret;
838 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800839#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800840 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
841 ret = os_snprintf(pos, end - pos, "FT-PSK ");
842 if (os_snprintf_error(end - pos, ret))
843 return pos - buf;
844 pos += ret;
845 }
846 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
847 ret = os_snprintf(pos, end - pos, "FT-EAP ");
848 if (os_snprintf_error(end - pos, ret))
849 return pos - buf;
850 pos += ret;
851 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700852#ifdef CONFIG_SHA384
853 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
854 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
855 if (os_snprintf_error(end - pos, ret))
856 return pos - buf;
857 pos += ret;
858 }
859#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800860#ifdef CONFIG_SAE
861 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
862 ret = os_snprintf(pos, end - pos, "FT-SAE ");
863 if (os_snprintf_error(end - pos, ret))
864 return pos - buf;
865 pos += ret;
866 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700867 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
868 ret = os_snprintf(pos, end - pos, "FT-SAE-EXT-KEY ");
869 if (os_snprintf_error(end - pos, ret))
870 return pos - buf;
871 pos += ret;
872 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800873#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800874#ifdef CONFIG_FILS
875 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
876 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
877 if (os_snprintf_error(end - pos, ret))
878 return pos - buf;
879 pos += ret;
880 }
881 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
882 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
883 if (os_snprintf_error(end - pos, ret))
884 return pos - buf;
885 pos += ret;
886 }
887#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800888#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800889 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
890 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
891 if (os_snprintf_error(end - pos, ret))
892 return pos - buf;
893 pos += ret;
894 }
895 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
896 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
897 if (os_snprintf_error(end - pos, ret))
898 return pos - buf;
899 pos += ret;
900 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800901#ifdef CONFIG_SAE
902 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
903 ret = os_snprintf(pos, end - pos, "SAE ");
904 if (os_snprintf_error(end - pos, ret))
905 return pos - buf;
906 pos += ret;
907 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700908 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
909 ret = os_snprintf(pos, end - pos, "SAE-EXT-KEY ");
910 if (os_snprintf_error(end - pos, ret))
911 return pos - buf;
912 pos += ret;
913 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800914#endif /* CONFIG_SAE */
915 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
916 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
917 if (os_snprintf_error(end - pos, ret))
918 return pos - buf;
919 pos += ret;
920 }
921 if (hapd->conf->wpa_key_mgmt &
922 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
923 ret = os_snprintf(pos, end - pos,
924 "WPA-EAP-SUITE-B-192 ");
925 if (os_snprintf_error(end - pos, ret))
926 return pos - buf;
927 pos += ret;
928 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800929#ifdef CONFIG_FILS
930 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
931 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
932 if (os_snprintf_error(end - pos, ret))
933 return pos - buf;
934 pos += ret;
935 }
936 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
937 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
938 if (os_snprintf_error(end - pos, ret))
939 return pos - buf;
940 pos += ret;
941 }
942#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800943
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700944#ifdef CONFIG_OWE
945 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
946 ret = os_snprintf(pos, end - pos, "OWE ");
947 if (os_snprintf_error(end - pos, ret))
948 return pos - buf;
949 pos += ret;
950 }
951#endif /* CONFIG_OWE */
952
953#ifdef CONFIG_DPP
954 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
955 ret = os_snprintf(pos, end - pos, "DPP ");
956 if (os_snprintf_error(end - pos, ret))
957 return pos - buf;
958 pos += ret;
959 }
960#endif /* CONFIG_DPP */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000961#ifdef CONFIG_SHA384
962 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
963 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA384 ");
964 if (os_snprintf_error(end - pos, ret))
965 return pos - buf;
966 pos += ret;
967 }
968#endif /* CONFIG_SHA384 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700969
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800970 if (pos > buf && *(pos - 1) == ' ') {
971 *(pos - 1) = '\0';
972 pos--;
973 }
974
975 return pos - buf;
976}
977
978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
980 char *buf, size_t buflen)
981{
982 int ret;
983 char *pos, *end;
984
985 pos = buf;
986 end = buf + buflen;
987
988 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
989 "ssid=%s\n",
990 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700991 wpa_ssid_txt(hapd->conf->ssid.ssid,
992 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800993 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 return pos - buf;
995 pos += ret;
996
Sunil Ravi77d572f2023-01-17 23:58:31 +0000997 if ((hapd->conf->config_id)) {
998 ret = os_snprintf(pos, end - pos, "config_id=%s\n",
999 hapd->conf->config_id);
1000 if (os_snprintf_error(end - pos, ret))
1001 return pos - buf;
1002 pos += ret;
1003 }
1004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005#ifdef CONFIG_WPS
1006 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1007 hapd->conf->wps_state == 0 ? "disabled" :
1008 (hapd->conf->wps_state == 1 ? "not configured" :
1009 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001010 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 return pos - buf;
1012 pos += ret;
1013
1014 if (hapd->conf->wps_state && hapd->conf->wpa &&
1015 hapd->conf->ssid.wpa_passphrase) {
1016 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1017 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001018 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 return pos - buf;
1020 pos += ret;
1021 }
1022
1023 if (hapd->conf->wps_state && hapd->conf->wpa &&
1024 hapd->conf->ssid.wpa_psk &&
1025 hapd->conf->ssid.wpa_psk->group) {
1026 char hex[PMK_LEN * 2 + 1];
1027 wpa_snprintf_hex(hex, sizeof(hex),
1028 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1029 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001030 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 return pos - buf;
1032 pos += ret;
1033 }
Hai Shalom60840252021-02-19 19:02:11 -08001034
1035 if (hapd->conf->multi_ap) {
1036 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1037
1038 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1039 hapd->conf->multi_ap);
1040 if (os_snprintf_error(end - pos, ret))
1041 return pos - buf;
1042 pos += ret;
1043
1044 if (ssid->ssid_len) {
1045 ret = os_snprintf(pos, end - pos,
1046 "multi_ap_backhaul_ssid=%s\n",
1047 wpa_ssid_txt(ssid->ssid,
1048 ssid->ssid_len));
1049 if (os_snprintf_error(end - pos, ret))
1050 return pos - buf;
1051 pos += ret;
1052 }
1053
1054 if (hapd->conf->wps_state && hapd->conf->wpa &&
1055 ssid->wpa_passphrase) {
1056 ret = os_snprintf(pos, end - pos,
1057 "multi_ap_backhaul_wpa_passphrase=%s\n",
1058 ssid->wpa_passphrase);
1059 if (os_snprintf_error(end - pos, ret))
1060 return pos - buf;
1061 pos += ret;
1062 }
1063
1064 if (hapd->conf->wps_state && hapd->conf->wpa &&
1065 ssid->wpa_psk &&
1066 ssid->wpa_psk->group) {
1067 char hex[PMK_LEN * 2 + 1];
1068
1069 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1070 PMK_LEN);
1071 ret = os_snprintf(pos, end - pos,
1072 "multi_ap_backhaul_wpa_psk=%s\n",
1073 hex);
1074 forced_memzero(hex, sizeof(hex));
1075 if (os_snprintf_error(end - pos, ret))
1076 return pos - buf;
1077 pos += ret;
1078 }
1079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080#endif /* CONFIG_WPS */
1081
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001082 if (hapd->conf->wpa) {
1083 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1084 if (os_snprintf_error(end - pos, ret))
1085 return pos - buf;
1086 pos += ret;
1087 }
1088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1090 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001091 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 return pos - buf;
1093 pos += ret;
1094
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001095 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096
1097 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001098 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 return pos - buf;
1100 pos += ret;
1101 }
1102
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001103 if (hapd->conf->wpa) {
1104 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1105 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001106 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 return pos - buf;
1108 pos += ret;
1109 }
1110
1111 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1112 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001113 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 return pos - buf;
1115 pos += ret;
1116
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001117 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1118 " ");
1119 if (ret < 0)
1120 return pos - buf;
1121 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122
1123 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001124 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 return pos - buf;
1126 pos += ret;
1127 }
1128
1129 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1130 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001131 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 return pos - buf;
1133 pos += ret;
1134
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001135 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001136 " ");
1137 if (ret < 0)
1138 return pos - buf;
1139 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140
1141 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001142 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 return pos - buf;
1144 pos += ret;
1145 }
1146
Hai Shalomfdcde762020-04-02 11:19:20 -07001147 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1148 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1149 hapd->conf->wpa_deny_ptk0_rekey);
1150 if (os_snprintf_error(end - pos, ret))
1151 return pos - buf;
1152 pos += ret;
1153 }
1154
1155 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1156 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1157 hapd->conf->extended_key_id);
1158 if (os_snprintf_error(end - pos, ret))
1159 return pos - buf;
1160 pos += ret;
1161 }
1162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 return pos - buf;
1164}
1165
1166
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001167static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
Hai Shalom60840252021-02-19 19:02:11 -08001168 const char *bands)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001169{
1170 union wpa_event_data event;
Hai Shalom60840252021-02-19 19:02:11 -08001171 u32 setband_mask = WPA_SETBAND_AUTO;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001172
Hai Shalom60840252021-02-19 19:02:11 -08001173 /*
1174 * For example:
1175 * SET setband 2G,6G
1176 * SET setband 5G
1177 * SET setband AUTO
1178 */
1179 if (!os_strstr(bands, "AUTO")) {
1180 if (os_strstr(bands, "5G"))
1181 setband_mask |= WPA_SETBAND_5G;
1182 if (os_strstr(bands, "6G"))
1183 setband_mask |= WPA_SETBAND_6G;
1184 if (os_strstr(bands, "2G"))
1185 setband_mask |= WPA_SETBAND_2G;
1186 if (setband_mask == WPA_SETBAND_AUTO)
1187 return -1;
1188 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001189
Hai Shalom60840252021-02-19 19:02:11 -08001190 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001191 os_memset(&event, 0, sizeof(event));
1192 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1193 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1194 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1195 }
1196
1197 return 0;
1198}
1199
1200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1202{
1203 char *value;
1204 int ret = 0;
1205
1206 value = os_strchr(cmd, ' ');
1207 if (value == NULL)
1208 return -1;
1209 *value++ = '\0';
1210
1211 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1212 if (0) {
1213#ifdef CONFIG_WPS_TESTING
1214 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1215 long int val;
1216 val = strtol(value, NULL, 0);
1217 if (val < 0 || val > 0xff) {
1218 ret = -1;
1219 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1220 "wps_version_number %ld", val);
1221 } else {
1222 wps_version_number = val;
1223 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1224 "version %u.%u",
1225 (wps_version_number & 0xf0) >> 4,
1226 wps_version_number & 0x0f);
1227 hostapd_wps_update_ie(hapd);
1228 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001229 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1230 wps_testing_stub_cred = atoi(value);
1231 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1232 wps_testing_stub_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001233 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1234 wps_corrupt_pkhash = atoi(value);
1235 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1236 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001238#ifdef CONFIG_TESTING_OPTIONS
1239 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1240 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001241 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1242 hapd->ext_eapol_frame_io = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001243 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1244 hapd->force_backlog_bytes = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001245#ifdef CONFIG_DPP
1246 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1247 os_free(hapd->dpp_config_obj_override);
1248 hapd->dpp_config_obj_override = os_strdup(value);
1249 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1250 os_free(hapd->dpp_discovery_override);
1251 hapd->dpp_discovery_override = os_strdup(value);
1252 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1253 os_free(hapd->dpp_groups_override);
1254 hapd->dpp_groups_override = os_strdup(value);
1255 } else if (os_strcasecmp(cmd,
1256 "dpp_ignore_netaccesskey_mismatch") == 0) {
1257 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001258 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1259 dpp_test = atoi(value);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001260 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1261 dpp_version_override = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001262#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001263#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001264#ifdef CONFIG_MBO
1265 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1266 int val;
1267
1268 if (!hapd->conf->mbo_enabled)
1269 return -1;
1270
1271 val = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001272 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001273 return -1;
1274
1275 hapd->mbo_assoc_disallow = val;
1276 ieee802_11_update_beacons(hapd->iface);
1277
1278 /*
1279 * TODO: Need to configure drivers that do AP MLME offload with
1280 * disallowing station logic.
1281 */
1282#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001283#ifdef CONFIG_DPP
1284 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1285 os_free(hapd->dpp_configurator_params);
1286 hapd->dpp_configurator_params = os_strdup(value);
Sunil Ravia04bd252022-05-02 22:54:18 -07001287#ifdef CONFIG_DPP2
1288 dpp_controller_set_params(hapd->iface->interfaces->dpp, value);
1289#endif /* CONFIG_DPP2 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001290 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1291 hapd->dpp_init_max_tries = atoi(value);
1292 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1293 hapd->dpp_init_retry_time = atoi(value);
1294 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1295 hapd->dpp_resp_wait_time = atoi(value);
1296 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1297 hapd->dpp_resp_max_tries = atoi(value);
1298 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1299 hapd->dpp_resp_retry_time = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001300#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001301 } else if (os_strcasecmp(cmd, "setband") == 0) {
1302 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001304 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001305 if (ret)
1306 return ret;
1307
1308 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001309 hostapd_disassoc_deny_mac(hapd);
1310 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1311 hostapd_disassoc_accept_mac(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001312 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1313 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1314 hapd->parameter_set_count++;
1315 if (ieee802_11_update_beacons(hapd->iface))
1316 wpa_printf(MSG_DEBUG,
1317 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001318 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1319 os_strcmp(cmd, "sae_password") == 0 ||
1320 os_strcmp(cmd, "sae_pwe") == 0) {
1321 if (hapd->started)
1322 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001323 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1324 wpa_auth_set_transition_disable(hapd->wpa_auth,
1325 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001326 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001327
1328#ifdef CONFIG_TESTING_OPTIONS
1329 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1330 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1331 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001332 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1333 wpa_auth_set_ocv_override_freq(
1334 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1335 atoi(value));
1336 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1337 wpa_auth_set_ocv_override_freq(
1338 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1339 atoi(value));
1340 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1341 wpa_auth_set_ocv_override_freq(
1342 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1343 atoi(value));
1344 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1345 wpa_auth_set_ocv_override_freq(
1346 hapd->wpa_auth,
1347 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001348 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1349 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1350 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1351 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001352#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 }
1354
1355 return ret;
1356}
1357
1358
1359static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1360 char *buf, size_t buflen)
1361{
1362 int res;
1363
1364 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1365
1366 if (os_strcmp(cmd, "version") == 0) {
1367 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001368 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 return -1;
1370 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001371 } else if (os_strcmp(cmd, "tls_library") == 0) {
1372 res = tls_get_library_version(buf, buflen);
1373 if (os_snprintf_error(buflen, res))
1374 return -1;
1375 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376 }
1377
1378 return -1;
1379}
1380
1381
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001382static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1383{
1384 if (hostapd_enable_iface(iface) < 0) {
1385 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1386 return -1;
1387 }
1388 return 0;
1389}
1390
1391
1392static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1393{
1394 if (hostapd_reload_iface(iface) < 0) {
1395 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1396 return -1;
1397 }
1398 return 0;
1399}
1400
1401
Sunil Ravi77d572f2023-01-17 23:58:31 +00001402static int hostapd_ctrl_iface_reload_bss(struct hostapd_data *bss)
1403{
1404 if (hostapd_reload_bss_only(bss) < 0) {
1405 wpa_printf(MSG_ERROR, "Reloading of BSS failed");
1406 return -1;
1407 }
1408 return 0;
1409}
1410
1411
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001412static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1413{
1414 if (hostapd_disable_iface(iface) < 0) {
1415 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1416 return -1;
1417 }
1418 return 0;
1419}
1420
1421
Hai Shalom74f70d42019-02-11 14:42:39 -08001422static int
1423hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1424 struct sta_info *sta, void *ctx)
1425{
1426 struct hostapd_wpa_psk *psk;
1427 const u8 *pmk;
1428 int pmk_len;
1429 int pmk_match;
1430 int sta_match;
1431 int bss_match;
1432 int reason;
1433
1434 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1435
1436 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1437 pmk_match = PMK_LEN == pmk_len &&
1438 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1439 sta_match = psk->group == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001440 ether_addr_equal(sta->addr, psk->addr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001441 bss_match = psk->group == 1;
1442
1443 if (pmk_match && (sta_match || bss_match))
1444 return 0;
1445 }
1446
1447 wpa_printf(MSG_INFO, "STA " MACSTR
1448 " PSK/passphrase no longer valid - disconnect",
1449 MAC2STR(sta->addr));
1450 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1451 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1452 ap_sta_deauthenticate(hapd, sta, reason);
1453
1454 return 0;
1455}
1456
1457
1458static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1459{
1460 struct hostapd_bss_config *conf = hapd->conf;
1461 int err;
1462
1463 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1464
1465 err = hostapd_setup_wpa_psk(conf);
1466 if (err < 0) {
1467 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1468 err);
1469 return -1;
1470 }
1471
1472 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1473 NULL);
1474
1475 return 0;
1476}
1477
1478
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001479#ifdef CONFIG_IEEE80211R_AP
1480
1481static int hostapd_ctrl_iface_get_rxkhs(struct hostapd_data *hapd,
1482 char *buf, size_t buflen)
1483{
1484 int ret, start_pos;
1485 char *pos, *end;
1486 struct ft_remote_r0kh *r0kh;
1487 struct ft_remote_r1kh *r1kh;
1488 struct hostapd_bss_config *conf = hapd->conf;
1489
1490 pos = buf;
1491 end = buf + buflen;
1492
1493 for (r0kh = conf->r0kh_list; r0kh; r0kh=r0kh->next) {
1494 start_pos = pos - buf;
1495 ret = os_snprintf(pos, end - pos, "r0kh=" MACSTR " ",
1496 MAC2STR(r0kh->addr));
1497 if (os_snprintf_error(end - pos, ret))
1498 return start_pos;
1499 pos += ret;
1500 if (r0kh->id_len + 1 >= (size_t) (end - pos))
1501 return start_pos;
1502 os_memcpy(pos, r0kh->id, r0kh->id_len);
1503 pos += r0kh->id_len;
1504 *pos++ = ' ';
1505 pos += wpa_snprintf_hex(pos, end - pos, r0kh->key,
1506 sizeof(r0kh->key));
1507 ret = os_snprintf(pos, end - pos, "\n");
1508 if (os_snprintf_error(end - pos, ret))
1509 return start_pos;
1510 pos += ret;
1511 }
1512
1513 for (r1kh = conf->r1kh_list; r1kh; r1kh=r1kh->next) {
1514 start_pos = pos - buf;
1515 ret = os_snprintf(pos, end - pos, "r1kh=" MACSTR " " MACSTR " ",
1516 MAC2STR(r1kh->addr), MAC2STR(r1kh->id));
1517 if (os_snprintf_error(end - pos, ret))
1518 return start_pos;
1519 pos += ret;
1520 pos += wpa_snprintf_hex(pos, end - pos, r1kh->key,
1521 sizeof(r1kh->key));
1522 ret = os_snprintf(pos, end - pos, "\n");
1523 if (os_snprintf_error(end - pos, ret))
1524 return start_pos;
1525 pos += ret;
1526 }
1527
1528 return pos - buf;
1529}
1530
1531
1532static int hostapd_ctrl_iface_reload_rxkhs(struct hostapd_data *hapd)
1533{
1534 struct hostapd_bss_config *conf = hapd->conf;
1535 int err;
1536
1537 hostapd_config_clear_rxkhs(conf);
1538
1539 err = hostapd_config_read_rxkh_file(conf, conf->rxkh_file);
1540 if (err < 0) {
1541 wpa_printf(MSG_ERROR, "Reloading RxKHs failed: %d",
1542 err);
1543 return -1;
1544 }
1545
1546 return 0;
1547}
1548
1549#endif /* CONFIG_IEEE80211R_AP */
1550
1551
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001552#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001553
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001554static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1555{
1556 union wpa_event_data data;
1557 char *pos, *param;
1558 enum wpa_event_type event;
1559
1560 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1561
1562 os_memset(&data, 0, sizeof(data));
1563
1564 param = os_strchr(cmd, ' ');
1565 if (param == NULL)
1566 return -1;
1567 *param++ = '\0';
1568
1569 if (os_strcmp(cmd, "DETECTED") == 0)
1570 event = EVENT_DFS_RADAR_DETECTED;
1571 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1572 event = EVENT_DFS_CAC_FINISHED;
1573 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1574 event = EVENT_DFS_CAC_ABORTED;
1575 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1576 event = EVENT_DFS_NOP_FINISHED;
1577 else {
1578 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1579 cmd);
1580 return -1;
1581 }
1582
1583 pos = os_strstr(param, "freq=");
1584 if (pos)
1585 data.dfs_event.freq = atoi(pos + 5);
1586
1587 pos = os_strstr(param, "ht_enabled=1");
1588 if (pos)
1589 data.dfs_event.ht_enabled = 1;
1590
1591 pos = os_strstr(param, "chan_offset=");
1592 if (pos)
1593 data.dfs_event.chan_offset = atoi(pos + 12);
1594
1595 pos = os_strstr(param, "chan_width=");
1596 if (pos)
1597 data.dfs_event.chan_width = atoi(pos + 11);
1598
1599 pos = os_strstr(param, "cf1=");
1600 if (pos)
1601 data.dfs_event.cf1 = atoi(pos + 4);
1602
1603 pos = os_strstr(param, "cf2=");
1604 if (pos)
1605 data.dfs_event.cf2 = atoi(pos + 4);
1606
1607 wpa_supplicant_event(hapd, event, &data);
1608
1609 return 0;
1610}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001611
1612
1613static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1614{
1615 size_t len;
1616 u8 *buf;
1617 int res;
1618
1619 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1620
1621 len = os_strlen(cmd);
1622 if (len & 1)
1623 return -1;
1624 len /= 2;
1625
1626 buf = os_malloc(len);
1627 if (buf == NULL)
1628 return -1;
1629
1630 if (hexstr2bin(cmd, buf, len) < 0) {
1631 os_free(buf);
1632 return -1;
1633 }
1634
Hai Shalomfdcde762020-04-02 11:19:20 -07001635 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001636 os_free(buf);
1637 return res;
1638}
1639
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001640
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001641static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1642 char *cmd)
1643{
1644 char *pos, *param;
1645 size_t len;
1646 u8 *buf;
1647 int stype = 0, ok = 0;
1648 union wpa_event_data event;
1649
1650 if (!hapd->ext_mgmt_frame_handling)
1651 return -1;
1652
1653 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1654
1655 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1656
1657 pos = cmd;
1658 param = os_strstr(pos, "stype=");
1659 if (param) {
1660 param += 6;
1661 stype = atoi(param);
1662 }
1663
1664 param = os_strstr(pos, " ok=");
1665 if (param) {
1666 param += 4;
1667 ok = atoi(param);
1668 }
1669
1670 param = os_strstr(pos, " buf=");
1671 if (!param)
1672 return -1;
1673 param += 5;
1674
1675 len = os_strlen(param);
1676 if (len & 1)
1677 return -1;
1678 len /= 2;
1679
1680 buf = os_malloc(len);
1681 if (!buf || hexstr2bin(param, buf, len) < 0) {
1682 os_free(buf);
1683 return -1;
1684 }
1685
1686 os_memset(&event, 0, sizeof(event));
1687 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1688 event.tx_status.data = buf;
1689 event.tx_status.data_len = len;
1690 event.tx_status.stype = stype;
1691 event.tx_status.ack = ok;
1692 hapd->ext_mgmt_frame_handling = 0;
1693 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1694 hapd->ext_mgmt_frame_handling = 1;
1695
1696 os_free(buf);
1697
1698 return 0;
1699}
1700
1701
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001702static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1703 char *cmd)
1704{
1705 char *pos, *param;
1706 size_t len;
1707 u8 *buf;
1708 int freq = 0, datarate = 0, ssi_signal = 0;
1709 union wpa_event_data event;
1710
1711 if (!hapd->ext_mgmt_frame_handling)
1712 return -1;
1713
1714 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1715
1716 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1717
1718 pos = cmd;
1719 param = os_strstr(pos, "freq=");
1720 if (param) {
1721 param += 5;
1722 freq = atoi(param);
1723 }
1724
1725 param = os_strstr(pos, " datarate=");
1726 if (param) {
1727 param += 10;
1728 datarate = atoi(param);
1729 }
1730
1731 param = os_strstr(pos, " ssi_signal=");
1732 if (param) {
1733 param += 12;
1734 ssi_signal = atoi(param);
1735 }
1736
1737 param = os_strstr(pos, " frame=");
1738 if (param == NULL)
1739 return -1;
1740 param += 7;
1741
1742 len = os_strlen(param);
1743 if (len & 1)
1744 return -1;
1745 len /= 2;
1746
1747 buf = os_malloc(len);
1748 if (buf == NULL)
1749 return -1;
1750
1751 if (hexstr2bin(param, buf, len) < 0) {
1752 os_free(buf);
1753 return -1;
1754 }
1755
1756 os_memset(&event, 0, sizeof(event));
1757 event.rx_mgmt.freq = freq;
1758 event.rx_mgmt.frame = buf;
1759 event.rx_mgmt.frame_len = len;
1760 event.rx_mgmt.ssi_signal = ssi_signal;
1761 event.rx_mgmt.datarate = datarate;
1762 hapd->ext_mgmt_frame_handling = 0;
1763 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1764 hapd->ext_mgmt_frame_handling = 1;
1765
1766 os_free(buf);
1767
1768 return 0;
1769}
1770
1771
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001772static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1773{
1774 char *pos;
1775 u8 src[ETH_ALEN], *buf;
1776 int used;
1777 size_t len;
1778
1779 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1780
1781 pos = cmd;
1782 used = hwaddr_aton2(pos, src);
1783 if (used < 0)
1784 return -1;
1785 pos += used;
1786 while (*pos == ' ')
1787 pos++;
1788
1789 len = os_strlen(pos);
1790 if (len & 1)
1791 return -1;
1792 len /= 2;
1793
1794 buf = os_malloc(len);
1795 if (buf == NULL)
1796 return -1;
1797
1798 if (hexstr2bin(pos, buf, len) < 0) {
1799 os_free(buf);
1800 return -1;
1801 }
1802
Sunil8cd6f4d2022-06-28 18:40:46 +00001803 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001804 os_free(buf);
1805
1806 return 0;
1807}
1808
1809
Hai Shaloma20dcd72022-02-04 13:43:00 -08001810static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1811{
1812 char *pos, *pos2;
1813 u8 dst[ETH_ALEN], *buf;
1814 int used, ret;
1815 size_t len;
1816 unsigned int prev;
1817 int encrypt = 0;
1818
1819 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1820
1821 pos = cmd;
1822 used = hwaddr_aton2(pos, dst);
1823 if (used < 0)
1824 return -1;
1825 pos += used;
1826 while (*pos == ' ')
1827 pos++;
1828
1829 pos2 = os_strchr(pos, ' ');
1830 if (pos2) {
1831 len = pos2 - pos;
1832 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1833 } else {
1834 len = os_strlen(pos);
1835 }
1836 if (len & 1)
1837 return -1;
1838 len /= 2;
1839
1840 buf = os_malloc(len);
1841 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1842 os_free(buf);
1843 return -1;
1844 }
1845
1846 prev = hapd->ext_eapol_frame_io;
1847 hapd->ext_eapol_frame_io = 0;
1848 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1849 hapd->ext_eapol_frame_io = prev;
1850 os_free(buf);
1851
1852 return ret;
1853}
1854
1855
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001856static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1857{
1858 size_t i;
1859 u32 sum = 0;
1860 const u16 *pos = buf;
1861
1862 for (i = 0; i < len / 2; i++)
1863 sum += *pos++;
1864
1865 while (sum >> 16)
1866 sum = (sum & 0xffff) + (sum >> 16);
1867
1868 return sum ^ 0xffff;
1869}
1870
1871
1872#define HWSIM_PACKETLEN 1500
1873#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1874
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001875static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1876 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001877{
1878 struct hostapd_data *hapd = ctx;
1879 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001880 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001881 const u8 *pos;
1882 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001883 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001884
Hai Shalom81f62d82019-07-22 12:10:00 -07001885 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1886 wpa_printf(MSG_DEBUG,
1887 "test data: RX - ignore unexpected length %d",
1888 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001889 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001890 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001891
1892 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001893 os_memcpy(&ip, eth + 1, sizeof(ip));
1894 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001895
Hai Shalomfdcde762020-04-02 11:19:20 -07001896 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1897 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001898 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07001899 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001900 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001901 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001902
Hai Shalomfdcde762020-04-02 11:19:20 -07001903 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001904 if (*pos != (u8) i) {
1905 wpa_printf(MSG_DEBUG,
1906 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001907 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001908 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001909 pos++;
1910 }
1911
Hai Shalom81f62d82019-07-22 12:10:00 -07001912 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001913 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1914 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001915 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1916 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001917}
1918
1919
1920static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1921 char *cmd)
1922{
1923 int enabled = atoi(cmd);
1924 char *pos;
1925 const char *ifname;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001926 const u8 *addr = hapd->own_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001927
1928 if (!enabled) {
1929 if (hapd->l2_test) {
1930 l2_packet_deinit(hapd->l2_test);
1931 hapd->l2_test = NULL;
1932 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1933 "test data: Disabled");
1934 }
1935 return 0;
1936 }
1937
1938 if (hapd->l2_test)
1939 return 0;
1940
1941 pos = os_strstr(cmd, " ifname=");
1942 if (pos)
1943 ifname = pos + 8;
1944 else
1945 ifname = hapd->conf->iface;
1946
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001947#ifdef CONFIG_IEEE80211BE
1948 if (hapd->conf->mld_ap)
Sunil Ravi88611412024-06-28 17:34:56 +00001949 addr = hapd->mld_addr;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001950#endif /* CONFIG_IEEE80211BE */
1951 hapd->l2_test = l2_packet_init(ifname, addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001952 ETHERTYPE_IP, hostapd_data_test_rx,
1953 hapd, 1);
1954 if (hapd->l2_test == NULL)
1955 return -1;
1956
1957 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1958
1959 return 0;
1960}
1961
1962
1963static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1964{
1965 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001966 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001967 int used;
1968 long int val;
1969 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001970 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001971 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001972 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001973 u8 *dpos;
1974 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001975 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001976
1977 if (hapd->l2_test == NULL)
1978 return -1;
1979
Hai Shalom81f62d82019-07-22 12:10:00 -07001980 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001981
1982 pos = cmd;
1983 used = hwaddr_aton2(pos, dst);
1984 if (used < 0)
1985 return -1;
1986 pos += used;
1987 while (*pos == ' ')
1988 pos++;
1989 used = hwaddr_aton2(pos, src);
1990 if (used < 0)
1991 return -1;
1992 pos += used;
1993
Hai Shalom81f62d82019-07-22 12:10:00 -07001994 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001995 if (val < 0 || val > 0xff)
1996 return -1;
1997 tos = val;
1998
Hai Shalom81f62d82019-07-22 12:10:00 -07001999 pos = os_strstr(pos2, " len=");
2000 if (pos) {
2001 i = atoi(pos + 5);
2002 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
2003 return -1;
2004 send_len = i;
2005 }
2006
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002007 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002008 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
2009 os_memcpy(eth->ether_shost, src, ETH_ALEN);
2010 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07002011 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002012 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07002013 ip->ip_hl = 5;
2014 ip->ip_v = 4;
2015 ip->ip_ttl = 64;
2016 ip->ip_tos = tos;
2017 ip->ip_len = htons(send_len);
2018 ip->ip_p = 1;
2019 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
2020 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
2021 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002022 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07002023 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002024 *dpos++ = i;
2025
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002026 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07002027 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002028 return -1;
2029
2030 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2031 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2032
2033 return 0;
2034}
2035
2036
2037static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2038 char *cmd)
2039{
2040 u8 *buf;
2041 struct ether_header *eth;
2042 struct l2_packet_data *l2 = NULL;
2043 size_t len;
2044 u16 ethertype;
2045 int res = -1;
2046 const char *ifname = hapd->conf->iface;
2047
2048 if (os_strncmp(cmd, "ifname=", 7) == 0) {
2049 cmd += 7;
2050 ifname = cmd;
2051 cmd = os_strchr(cmd, ' ');
2052 if (cmd == NULL)
2053 return -1;
2054 *cmd++ = '\0';
2055 }
2056
2057 len = os_strlen(cmd);
2058 if (len & 1 || len < ETH_HLEN * 2)
2059 return -1;
2060 len /= 2;
2061
2062 buf = os_malloc(len);
2063 if (buf == NULL)
2064 return -1;
2065
2066 if (hexstr2bin(cmd, buf, len) < 0)
2067 goto done;
2068
2069 eth = (struct ether_header *) buf;
2070 ethertype = ntohs(eth->ether_type);
2071
2072 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2073 hostapd_data_test_rx, hapd, 1);
2074 if (l2 == NULL)
2075 goto done;
2076
2077 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2078 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2079done:
2080 if (l2)
2081 l2_packet_deinit(l2);
2082 os_free(buf);
2083
2084 return res < 0 ? -1 : 0;
2085}
2086
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002087
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002088static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2089{
2090 struct sta_info *sta;
2091 u8 addr[ETH_ALEN];
2092 u8 zero[WPA_TK_MAX_LEN];
2093
2094 os_memset(zero, 0, sizeof(zero));
2095
2096 if (hwaddr_aton(cmd, addr))
2097 return -1;
2098
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002099 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2100 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2101 return -1;
2102
2103 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2104
2105 /* First, use a zero key to avoid any possible duplicate key
2106 * avoidance in the driver. */
2107 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2108 hapd->last_bigtk_alg,
2109 broadcast_ether_addr,
2110 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2111 zero, hapd->last_bigtk_len,
2112 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2113 return -1;
2114
2115 /* Set the previously configured key to reset its TSC */
2116 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2117 hapd->last_bigtk_alg,
2118 broadcast_ether_addr,
2119 hapd->last_bigtk_key_idx, 0, 1, NULL,
2120 0, hapd->last_bigtk,
2121 hapd->last_bigtk_len,
2122 KEY_FLAG_GROUP_TX_DEFAULT);
2123 }
2124
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002125 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2126 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2127 return -1;
2128
2129 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2130
2131 /* First, use a zero key to avoid any possible duplicate key
2132 * avoidance in the driver. */
2133 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2134 hapd->last_igtk_alg,
2135 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002136 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2137 zero, hapd->last_igtk_len,
2138 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002139 return -1;
2140
2141 /* Set the previously configured key to reset its TSC */
2142 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2143 hapd->last_igtk_alg,
2144 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002145 hapd->last_igtk_key_idx, 0, 1, NULL,
2146 0, hapd->last_igtk,
2147 hapd->last_igtk_len,
2148 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002149 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002150
2151 if (is_broadcast_ether_addr(addr)) {
2152 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2153 return -1;
2154
2155 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2156
2157 /* First, use a zero key to avoid any possible duplicate key
2158 * avoidance in the driver. */
2159 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2160 hapd->last_gtk_alg,
2161 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002162 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2163 zero, hapd->last_gtk_len,
2164 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002165 return -1;
2166
2167 /* Set the previously configured key to reset its TSC */
2168 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2169 hapd->last_gtk_alg,
2170 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002171 hapd->last_gtk_key_idx, 0, 1, NULL,
2172 0, hapd->last_gtk,
2173 hapd->last_gtk_len,
2174 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002175 }
2176
2177 sta = ap_get_sta(hapd, addr);
2178 if (!sta)
2179 return -1;
2180
2181 if (sta->last_tk_alg == WPA_ALG_NONE)
2182 return -1;
2183
2184 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2185 MAC2STR(sta->addr));
2186
2187 /* First, use a zero key to avoid any possible duplicate key avoidance
2188 * in the driver. */
2189 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002190 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2191 zero, sta->last_tk_len,
2192 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002193 return -1;
2194
2195 /* Set the previously configured key to reset its TSC/RSC */
2196 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002197 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2198 0, sta->last_tk, sta->last_tk_len,
2199 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002200}
2201
2202
2203static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2204{
2205 u8 addr[ETH_ALEN];
2206 const char *pos = cmd;
2207 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002208 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002209 int idx, set_tx;
2210 u8 seq[6], key[WPA_TK_MAX_LEN];
2211 size_t key_len;
2212
Hai Shalomfdcde762020-04-02 11:19:20 -07002213 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002214
2215 alg = atoi(pos);
2216 pos = os_strchr(pos, ' ');
2217 if (!pos)
2218 return -1;
2219 pos++;
2220 if (hwaddr_aton(pos, addr))
2221 return -1;
2222 pos += 17;
2223 if (*pos != ' ')
2224 return -1;
2225 pos++;
2226 idx = atoi(pos);
2227 pos = os_strchr(pos, ' ');
2228 if (!pos)
2229 return -1;
2230 pos++;
2231 set_tx = atoi(pos);
2232 pos = os_strchr(pos, ' ');
2233 if (!pos)
2234 return -1;
2235 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002236 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002237 return -1;
2238 pos += 2 * 6;
2239 if (*pos != ' ')
2240 return -1;
2241 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002242 if (!os_strchr(pos, ' '))
2243 return -1;
2244 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002245 if (hexstr2bin(pos, key, key_len) < 0)
2246 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002247 pos += 2 * key_len;
2248 if (*pos != ' ')
2249 return -1;
2250
2251 pos++;
2252 key_flag = atoi(pos);
2253 pos = os_strchr(pos, ' ');
2254 if (pos)
2255 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002256
2257 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002258 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2259 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002260}
2261
2262
2263static void restore_tk(void *ctx1, void *ctx2)
2264{
2265 struct hostapd_data *hapd = ctx1;
2266 struct sta_info *sta = ctx2;
2267
2268 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2269 MAC2STR(sta->addr));
2270 /* This does not really restore the TSC properly, so this will result
2271 * in replay protection issues for now since there is no clean way of
2272 * preventing encryption of a single EAPOL frame. */
2273 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002274 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2275 sta->last_tk, sta->last_tk_len,
2276 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002277}
2278
2279
2280static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2281{
2282 struct sta_info *sta;
2283 u8 addr[ETH_ALEN];
2284 int plain = os_strstr(cmd, "plaintext") != NULL;
2285
2286 if (hwaddr_aton(cmd, addr))
2287 return -1;
2288
2289 sta = ap_get_sta(hapd, addr);
2290 if (!sta || !sta->wpa_sm)
2291 return -1;
2292
2293 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2294 plain = 0; /* no need for special processing */
2295 if (plain) {
2296 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2297 MAC2STR(sta->addr));
2298 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002299 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2300 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002301 }
2302
2303 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2304 return wpa_auth_resend_m1(sta->wpa_sm,
2305 os_strstr(cmd, "change-anonce") != NULL,
2306 plain ? restore_tk : NULL, hapd, sta);
2307}
2308
2309
2310static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2311{
2312 struct sta_info *sta;
2313 u8 addr[ETH_ALEN];
2314 int plain = os_strstr(cmd, "plaintext") != NULL;
2315
2316 if (hwaddr_aton(cmd, addr))
2317 return -1;
2318
2319 sta = ap_get_sta(hapd, addr);
2320 if (!sta || !sta->wpa_sm)
2321 return -1;
2322
2323 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2324 plain = 0; /* no need for special processing */
2325 if (plain) {
2326 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2327 MAC2STR(sta->addr));
2328 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002329 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2330 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002331 }
2332
2333 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2334 return wpa_auth_resend_m3(sta->wpa_sm,
2335 plain ? restore_tk : NULL, hapd, sta);
2336}
2337
2338
2339static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2340 const char *cmd)
2341{
2342 struct sta_info *sta;
2343 u8 addr[ETH_ALEN];
2344 int plain = os_strstr(cmd, "plaintext") != NULL;
2345
2346 if (hwaddr_aton(cmd, addr))
2347 return -1;
2348
2349 sta = ap_get_sta(hapd, addr);
2350 if (!sta || !sta->wpa_sm)
2351 return -1;
2352
2353 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2354 plain = 0; /* no need for special processing */
2355 if (plain) {
2356 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2357 MAC2STR(sta->addr));
2358 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002359 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2360 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002361 }
2362
2363 wpa_printf(MSG_INFO,
2364 "TESTING: Send group M1 for the same GTK and zero RSC to "
2365 MACSTR, MAC2STR(sta->addr));
2366 return wpa_auth_resend_group_m1(sta->wpa_sm,
2367 plain ? restore_tk : NULL, hapd, sta);
2368}
2369
Hai Shalomfdcde762020-04-02 11:19:20 -07002370
Hai Shaloma20dcd72022-02-04 13:43:00 -08002371static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2372{
2373 struct sta_info *sta;
2374 u8 addr[ETH_ALEN];
2375
2376 if (hwaddr_aton(cmd, addr))
2377 return -1;
2378
2379 sta = ap_get_sta(hapd, addr);
2380 if (!sta || !sta->wpa_sm)
2381 return -1;
2382
2383 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2384}
2385
2386
Hai Shalom899fcc72020-10-19 14:38:18 -07002387static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2388 char *buf, size_t buflen)
2389{
2390 struct rsn_pmksa_cache_entry *pmksa;
2391
2392 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2393 if (!pmksa)
2394 return -1;
2395
2396 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2397}
2398
2399
Hai Shalomfdcde762020-04-02 11:19:20 -07002400static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2401 char *buf, size_t buflen)
2402{
2403 struct sta_info *sta;
2404 u8 addr[ETH_ALEN];
2405 const u8 *pmk;
2406 int pmk_len;
2407
2408 if (hwaddr_aton(cmd, addr))
2409 return -1;
2410
2411 sta = ap_get_sta(hapd, addr);
2412 if (!sta || !sta->wpa_sm) {
2413 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2414 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002415 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002416 }
2417 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002418 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002419 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2420 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002421 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002422 }
2423
2424 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2425}
2426
Hai Shaloma20dcd72022-02-04 13:43:00 -08002427
2428static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2429 const char *cmd)
2430{
2431 u16 type;
2432 char *pos, *end;
2433 u8 match[10];
2434 size_t match_len;
2435 bool multicast = false;
2436
2437 type = strtol(cmd, &pos, 16);
2438 if (*pos != ' ')
2439 return -1;
2440 pos++;
2441 end = os_strchr(pos, ' ');
2442 if (end) {
2443 match_len = end - pos;
2444 multicast = os_strstr(end, "multicast") != NULL;
2445 } else {
2446 match_len = os_strlen(pos) / 2;
2447 }
2448 if (hexstr2bin(pos, match, match_len))
2449 return -1;
2450
2451 return hostapd_drv_register_frame(hapd, type, match, match_len,
2452 multicast);
2453}
2454
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002455#endif /* CONFIG_TESTING_OPTIONS */
2456
2457
Hai Shalomb755a2a2020-04-23 21:49:02 -07002458#ifdef NEED_AP_MLME
Sunil Ravi036cec52023-03-29 11:35:17 -07002459static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params,
2460 u16 punct_bitmap)
Hai Shalomfdcde762020-04-02 11:19:20 -07002461{
Sunil Ravi036cec52023-03-29 11:35:17 -07002462 u32 start_freq;
2463
2464 if (is_6ghz_freq(params->freq)) {
2465 const int bw_idx[] = { 20, 40, 80, 160, 320 };
2466 int idx, bw;
2467
2468 /* The 6 GHz band requires HE to be enabled. */
2469 params->he_enabled = 1;
2470
2471 if (params->center_freq1) {
2472 if (params->freq == 5935)
2473 idx = (params->center_freq1 - 5925) / 5;
2474 else
2475 idx = (params->center_freq1 - 5950) / 5;
2476
2477 bw = center_idx_to_bw_6ghz(idx);
2478 if (bw < 0 || bw > (int) ARRAY_SIZE(bw_idx) ||
2479 bw_idx[bw] != params->bandwidth)
2480 return -1;
2481 }
2482 }
2483
Hai Shalomfdcde762020-04-02 11:19:20 -07002484 switch (params->bandwidth) {
2485 case 0:
2486 /* bandwidth not specified: use 20 MHz by default */
2487 /* fall-through */
2488 case 20:
2489 if (params->center_freq1 &&
2490 params->center_freq1 != params->freq)
2491 return -1;
2492
2493 if (params->center_freq2 || params->sec_channel_offset)
2494 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07002495
2496 if (punct_bitmap)
2497 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002498 break;
2499 case 40:
2500 if (params->center_freq2 || !params->sec_channel_offset)
2501 return -1;
2502
Sunil Ravi036cec52023-03-29 11:35:17 -07002503 if (punct_bitmap)
2504 return -1;
2505
Hai Shalomfdcde762020-04-02 11:19:20 -07002506 if (!params->center_freq1)
2507 break;
2508 switch (params->sec_channel_offset) {
2509 case 1:
2510 if (params->freq + 10 != params->center_freq1)
2511 return -1;
2512 break;
2513 case -1:
2514 if (params->freq - 10 != params->center_freq1)
2515 return -1;
2516 break;
2517 default:
2518 return -1;
2519 }
2520 break;
2521 case 80:
2522 if (!params->center_freq1 || !params->sec_channel_offset)
2523 return 1;
2524
2525 switch (params->sec_channel_offset) {
2526 case 1:
2527 if (params->freq - 10 != params->center_freq1 &&
2528 params->freq + 30 != params->center_freq1)
2529 return 1;
2530 break;
2531 case -1:
2532 if (params->freq + 10 != params->center_freq1 &&
2533 params->freq - 30 != params->center_freq1)
2534 return -1;
2535 break;
2536 default:
2537 return -1;
2538 }
2539
Sunil Ravi036cec52023-03-29 11:35:17 -07002540 if (params->center_freq2 && punct_bitmap)
2541 return -1;
2542
Hai Shalomfdcde762020-04-02 11:19:20 -07002543 /* Adjacent and overlapped are not allowed for 80+80 */
2544 if (params->center_freq2 &&
2545 params->center_freq1 - params->center_freq2 <= 80 &&
2546 params->center_freq2 - params->center_freq1 <= 80)
2547 return 1;
2548 break;
2549 case 160:
2550 if (!params->center_freq1 || params->center_freq2 ||
2551 !params->sec_channel_offset)
2552 return -1;
2553
2554 switch (params->sec_channel_offset) {
2555 case 1:
2556 if (params->freq + 70 != params->center_freq1 &&
2557 params->freq + 30 != params->center_freq1 &&
2558 params->freq - 10 != params->center_freq1 &&
2559 params->freq - 50 != params->center_freq1)
2560 return -1;
2561 break;
2562 case -1:
2563 if (params->freq + 50 != params->center_freq1 &&
2564 params->freq + 10 != params->center_freq1 &&
2565 params->freq - 30 != params->center_freq1 &&
2566 params->freq - 70 != params->center_freq1)
2567 return -1;
2568 break;
2569 default:
2570 return -1;
2571 }
2572 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00002573 case 320:
2574 if (!params->center_freq1 || params->center_freq2 ||
2575 !params->sec_channel_offset)
2576 return -1;
2577
2578 switch (params->sec_channel_offset) {
2579 case 1:
2580 if (params->freq + 150 != params->center_freq1 &&
2581 params->freq + 110 != params->center_freq1 &&
2582 params->freq + 70 != params->center_freq1 &&
2583 params->freq + 30 != params->center_freq1 &&
2584 params->freq - 10 != params->center_freq1 &&
2585 params->freq - 50 != params->center_freq1 &&
2586 params->freq - 90 != params->center_freq1 &&
2587 params->freq - 130 != params->center_freq1)
2588 return -1;
2589 break;
2590 case -1:
2591 if (params->freq + 130 != params->center_freq1 &&
2592 params->freq + 90 != params->center_freq1 &&
2593 params->freq + 50 != params->center_freq1 &&
2594 params->freq + 10 != params->center_freq1 &&
2595 params->freq - 30 != params->center_freq1 &&
2596 params->freq - 70 != params->center_freq1 &&
2597 params->freq - 110 != params->center_freq1 &&
2598 params->freq - 150 != params->center_freq1)
2599 return -1;
2600 break;
2601 }
2602 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002603 default:
2604 return -1;
2605 }
2606
Sunil Ravi036cec52023-03-29 11:35:17 -07002607 if (!punct_bitmap)
2608 return 0;
2609
2610 if (!params->eht_enabled) {
2611 wpa_printf(MSG_ERROR,
2612 "Preamble puncturing supported only in EHT");
2613 return -1;
2614 }
2615
2616 if (params->freq >= 2412 && params->freq <= 2484) {
2617 wpa_printf(MSG_ERROR,
2618 "Preamble puncturing is not supported in 2.4 GHz");
2619 return -1;
2620 }
2621
2622 start_freq = params->center_freq1 - (params->bandwidth / 2);
2623 if (!is_punct_bitmap_valid(params->bandwidth,
2624 (params->freq - start_freq) / 20,
2625 punct_bitmap)) {
2626 wpa_printf(MSG_ERROR, "Invalid preamble puncturing bitmap");
2627 return -1;
2628 }
2629
Hai Shalomfdcde762020-04-02 11:19:20 -07002630 return 0;
2631}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002632#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002633
2634
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002635static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2636 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002637{
2638#ifdef NEED_AP_MLME
2639 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002640 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002641 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002642 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002643 int bandwidth;
2644 u8 chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002645
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002646 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002647 if (ret)
2648 return ret;
2649
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002650 settings.link_id = -1;
2651#ifdef CONFIG_IEEE80211BE
2652 if (iface->num_bss && iface->bss[0]->conf->mld_ap)
2653 settings.link_id = iface->bss[0]->mld_link_id;
2654#endif /* CONFIG_IEEE80211BE */
2655
Sunil Ravi036cec52023-03-29 11:35:17 -07002656 ret = hostapd_ctrl_check_freq_params(&settings.freq_params,
2657 settings.punct_bitmap);
Hai Shalomfdcde762020-04-02 11:19:20 -07002658 if (ret) {
2659 wpa_printf(MSG_INFO,
2660 "chanswitch: invalid frequency settings provided");
2661 return ret;
2662 }
2663
2664 switch (settings.freq_params.bandwidth) {
2665 case 40:
2666 bandwidth = CHAN_WIDTH_40;
2667 break;
2668 case 80:
2669 if (settings.freq_params.center_freq2)
2670 bandwidth = CHAN_WIDTH_80P80;
2671 else
2672 bandwidth = CHAN_WIDTH_80;
2673 break;
2674 case 160:
2675 bandwidth = CHAN_WIDTH_160;
2676 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002677 case 320:
2678 bandwidth = CHAN_WIDTH_320;
2679 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002680 default:
2681 bandwidth = CHAN_WIDTH_20;
2682 break;
2683 }
2684
2685 if (settings.freq_params.center_freq1)
2686 dfs_range += hostapd_is_dfs_overlap(
2687 iface, bandwidth, settings.freq_params.center_freq1);
2688 else
2689 dfs_range += hostapd_is_dfs_overlap(
2690 iface, bandwidth, settings.freq_params.freq);
2691
2692 if (settings.freq_params.center_freq2)
2693 dfs_range += hostapd_is_dfs_overlap(
2694 iface, bandwidth, settings.freq_params.center_freq2);
2695
2696 if (dfs_range) {
2697 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2698 if (ret == NUM_HOSTAPD_MODES) {
2699 wpa_printf(MSG_ERROR,
2700 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2701 settings.freq_params.freq,
2702 settings.freq_params.sec_channel_offset,
2703 settings.freq_params.bandwidth);
2704 return -1;
2705 }
2706
2707 settings.freq_params.channel = chan;
2708
2709 wpa_printf(MSG_DEBUG,
2710 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2711 settings.freq_params.channel,
2712 settings.freq_params.freq,
2713 settings.freq_params.sec_channel_offset,
2714 settings.freq_params.bandwidth,
2715 settings.freq_params.center_freq1);
2716
2717 /* Perform CAC and switch channel */
2718 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2719 return 0;
2720 }
2721
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002722 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002723
Sunil Ravia04bd252022-05-02 22:54:18 -07002724 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002725 hostapd_chan_switch_config(iface->bss[i],
2726 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002727
Sunil Ravi88611412024-06-28 17:34:56 +00002728 ret = hostapd_switch_channel(iface->bss[i], &settings);
2729 if (ret) {
2730 /* FIX: What do we do if CSA fails in the middle of
2731 * submitting multi-BSS CSA requests? */
2732 return ret;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002733 }
2734 }
2735
Sunil Ravi88611412024-06-28 17:34:56 +00002736 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002737#else /* NEED_AP_MLME */
2738 return -1;
2739#endif /* NEED_AP_MLME */
2740}
2741
2742
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002743static u8 hostapd_maxnss(struct hostapd_data *hapd, struct sta_info *sta)
2744{
2745 u8 *mcs_set = NULL;
2746 u16 mcs_map;
2747 u8 ht_rx_nss = 0;
2748 u8 vht_rx_nss = 1;
2749 u8 mcs;
2750 bool ht_supported = false;
2751 bool vht_supported = false;
2752 int i;
2753
2754 if (sta->ht_capabilities && (sta->flags & WLAN_STA_HT)) {
2755 mcs_set = sta->ht_capabilities->supported_mcs_set;
2756 ht_supported = true;
2757 }
2758
2759 if (sta->vht_capabilities && (sta->flags & WLAN_STA_VHT)) {
2760 mcs_map = le_to_host16(
2761 sta->vht_capabilities->vht_supported_mcs_set.rx_map);
2762 vht_supported = true;
2763 }
2764
2765 if (ht_supported && mcs_set) {
2766 if (mcs_set[0])
2767 ht_rx_nss++;
2768 if (mcs_set[1])
2769 ht_rx_nss++;
2770 if (mcs_set[2])
2771 ht_rx_nss++;
2772 if (mcs_set[3])
2773 ht_rx_nss++;
2774 }
2775 if (vht_supported) {
2776 for (i = 7; i >= 0; i--) {
2777 mcs = (mcs_map >> (2 * i)) & 0x03;
2778 if (mcs != 0x03) {
2779 vht_rx_nss = i + 1;
2780 break;
2781 }
2782 }
2783 }
2784
2785 return ht_rx_nss > vht_rx_nss ? ht_rx_nss : vht_rx_nss;
2786}
2787
2788
2789static char hostapd_ctrl_iface_notify_cw_htaction(struct hostapd_data *hapd,
2790 const u8 *addr, u8 width)
2791{
2792 u8 buf[3];
2793 char ret;
2794
2795 width = width >= 1 ? 1 : 0;
2796
2797 buf[0] = WLAN_ACTION_HT;
2798 buf[1] = WLAN_HT_ACTION_NOTIFY_CHANWIDTH;
2799 buf[2] = width;
2800
2801 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2802 buf, sizeof(buf));
2803 if (ret)
2804 wpa_printf(MSG_DEBUG,
2805 "Failed to send Notify Channel Width frame to "
2806 MACSTR, MAC2STR(addr));
2807
2808 return ret;
2809}
2810
2811
2812static char hostapd_ctrl_iface_notify_cw_vhtaction(struct hostapd_data *hapd,
2813 const u8 *addr, u8 width)
2814{
2815 u8 buf[3];
2816 char ret;
2817
2818 buf[0] = WLAN_ACTION_VHT;
2819 buf[1] = WLAN_VHT_ACTION_OPMODE_NOTIF;
2820 buf[2] = width;
2821
2822 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2823 buf, sizeof(buf));
2824 if (ret)
2825 wpa_printf(MSG_DEBUG,
2826 "Failed to send Opeating Mode Notification frame to "
2827 MACSTR, MAC2STR(addr));
2828
2829 return ret;
2830}
2831
2832
2833static char hostapd_ctrl_iface_notify_cw_change(struct hostapd_data *hapd,
2834 const char *cmd)
2835{
2836 u8 cw, operating_mode = 0, nss;
2837 struct sta_info *sta;
2838 enum hostapd_hw_mode hw_mode;
2839
2840 if (is_6ghz_freq(hapd->iface->freq)) {
2841 wpa_printf(MSG_ERROR, "20/40 BSS coex not supported in 6 GHz");
2842 return -1;
2843 }
2844
2845 cw = atoi(cmd);
2846 hw_mode = hapd->iface->current_mode->mode;
2847 if ((hw_mode == HOSTAPD_MODE_IEEE80211G ||
2848 hw_mode == HOSTAPD_MODE_IEEE80211B) &&
2849 !(cw == 0 || cw == 1)) {
2850 wpa_printf(MSG_ERROR,
2851 "Channel width should be either 20 MHz or 40 MHz for 2.4 GHz band");
2852 return -1;
2853 }
2854
2855 switch (cw) {
2856 case 0:
2857 operating_mode = 0;
2858 break;
2859 case 1:
2860 operating_mode = VHT_OPMODE_CHANNEL_40MHZ;
2861 break;
2862 case 2:
2863 operating_mode = VHT_OPMODE_CHANNEL_80MHZ;
2864 break;
2865 case 3:
2866 operating_mode = VHT_OPMODE_CHANNEL_160MHZ;
2867 break;
2868 default:
2869 wpa_printf(MSG_ERROR, "Channel width should be between 0 to 3");
2870 return -1;
2871 }
2872
2873 for (sta = hapd->sta_list; sta; sta = sta->next) {
2874 if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
2875 nss = hostapd_maxnss(hapd, sta) - 1;
2876 hostapd_ctrl_iface_notify_cw_vhtaction(hapd, sta->addr,
2877 operating_mode |
2878 (u8) (nss << 4));
2879 continue;
2880 }
2881
2882 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) ==
2883 WLAN_STA_HT && sta->ht_capabilities)
2884 hostapd_ctrl_iface_notify_cw_htaction(hapd, sta->addr,
2885 cw);
2886 }
2887
2888 return 0;
2889}
2890
2891
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002892static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2893 int reply_size, const char *param)
2894{
2895#ifdef RADIUS_SERVER
2896 if (os_strcmp(param, "radius_server") == 0) {
2897 return radius_server_get_mib(hapd->radius_srv, reply,
2898 reply_size);
2899 }
2900#endif /* RADIUS_SERVER */
2901 return -1;
2902}
2903
2904
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002905static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2906 char *buf, size_t buflen)
2907{
2908 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08002909 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002910 u8 *data = NULL;
2911 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08002912 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002913 struct wpabuf *reply;
2914 size_t data_len = 0;
2915
Hai Shalom60840252021-02-19 19:02:11 -08002916 /**
2917 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2918 * [nested=<0|1>]
2919 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002920 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002921 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002922 return -EINVAL;
2923
2924 subcmd = strtoul(pos, &pos, 10);
2925
2926 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002927 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002928 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08002929
2930 temp = os_strchr(pos, ' ');
2931 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002932 }
2933
2934 if (data_len) {
2935 data_len /= 2;
2936 data = os_malloc(data_len);
2937 if (!data)
2938 return -ENOBUFS;
2939
2940 if (hexstr2bin(pos, data, data_len)) {
2941 wpa_printf(MSG_DEBUG,
2942 "Vendor command: wrong parameter format");
2943 os_free(data);
2944 return -EINVAL;
2945 }
2946 }
2947
Hai Shalom60840252021-02-19 19:02:11 -08002948 pos = os_strstr(cmd, "nested=");
2949 if (pos)
2950 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2951 NESTED_ATTR_NOT_USED;
2952
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002953 reply = wpabuf_alloc((buflen - 1) / 2);
2954 if (!reply) {
2955 os_free(data);
2956 return -ENOBUFS;
2957 }
2958
2959 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08002960 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002961
2962 if (ret == 0)
2963 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2964 wpabuf_len(reply));
2965
2966 wpabuf_free(reply);
2967 os_free(data);
2968
2969 return ret;
2970}
2971
2972
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002973static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2974 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002976 u8 addr[ETH_ALEN];
2977 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002979 if (hwaddr_aton(cmd, addr))
2980 return -1;
2981
2982 sta = ap_get_sta(hapd, addr);
2983 if (!sta || !sta->eapol_sm)
2984 return -1;
2985
2986 eapol_auth_reauthenticate(sta->eapol_sm);
2987 return 0;
2988}
2989
2990
2991static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2992{
2993 u8 addr[ETH_ALEN];
2994 struct sta_info *sta;
2995 char *pos = cmd, *param;
2996
2997 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2998 return -1;
2999 pos += 18;
3000 param = pos;
3001 pos = os_strchr(pos, ' ');
3002 if (!pos)
3003 return -1;
3004 *pos++ = '\0';
3005
3006 sta = ap_get_sta(hapd, addr);
3007 if (!sta || !sta->eapol_sm)
3008 return -1;
3009
3010 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
3011}
3012
3013
3014static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
3015 char *buf, size_t buflen)
3016{
3017 char *pos, *end, *stamp;
3018 int ret;
3019
3020 /* cmd: "LOG_LEVEL [<level>]" */
3021 if (*cmd == '\0') {
3022 pos = buf;
3023 end = buf + buflen;
3024 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
3025 "Timestamp: %d\n",
3026 debug_level_str(wpa_debug_level),
3027 wpa_debug_timestamp);
3028 if (os_snprintf_error(end - pos, ret))
3029 ret = 0;
3030
3031 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003034 while (*cmd == ' ')
3035 cmd++;
3036
3037 stamp = os_strchr(cmd, ' ');
3038 if (stamp) {
3039 *stamp++ = '\0';
3040 while (*stamp == ' ') {
3041 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043 }
3044
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003045 if (os_strlen(cmd)) {
3046 int level = str_to_debug_level(cmd);
3047 if (level < 0)
3048 return -1;
3049 wpa_debug_level = level;
3050 }
3051
3052 if (stamp && os_strlen(stamp))
3053 wpa_debug_timestamp = atoi(stamp);
3054
3055 os_memcpy(buf, "OK\n", 3);
3056 return 3;
3057}
3058
3059
3060#ifdef NEED_AP_MLME
3061static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
3062 char *buf, size_t buflen)
3063{
3064 struct hostapd_iface *iface = hapd->iface;
3065 char *pos, *end;
3066 struct hostapd_sta_info *info;
3067 struct os_reltime now;
3068
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003069 if (!iface->num_sta_seen)
3070 return 0;
3071
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003072 sta_track_expire(iface, 0);
3073
3074 pos = buf;
3075 end = buf + buflen;
3076
3077 os_get_reltime(&now);
3078 dl_list_for_each_reverse(info, &iface->sta_seen,
3079 struct hostapd_sta_info, list) {
3080 struct os_reltime age;
3081 int ret;
3082
3083 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003084 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
3085 MAC2STR(info->addr), (unsigned int) age.sec,
3086 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003087 if (os_snprintf_error(end - pos, ret))
3088 break;
3089 pos += ret;
3090 }
3091
3092 return pos - buf;
3093}
3094#endif /* NEED_AP_MLME */
3095
3096
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003097static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
3098 const char *cmd)
3099{
3100 u8 addr[ETH_ALEN];
3101
3102 if (hwaddr_aton(cmd, addr)) {
3103 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
3104 return -1;
3105 }
3106
3107 return hostapd_send_lci_req(hapd, addr);
3108}
3109
3110
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07003111static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003112{
3113 u8 addr[ETH_ALEN];
3114 char *token, *context = NULL;
3115 int random_interval, min_ap;
3116 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
3117 unsigned int n_responders;
3118
3119 token = str_token(cmd, " ", &context);
3120 if (!token || hwaddr_aton(token, addr)) {
3121 wpa_printf(MSG_INFO,
3122 "CTRL: REQ_RANGE - Bad destination address");
3123 return -1;
3124 }
3125
3126 token = str_token(cmd, " ", &context);
3127 if (!token)
3128 return -1;
3129
3130 random_interval = atoi(token);
3131 if (random_interval < 0 || random_interval > 0xffff)
3132 return -1;
3133
3134 token = str_token(cmd, " ", &context);
3135 if (!token)
3136 return -1;
3137
3138 min_ap = atoi(token);
3139 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
3140 return -1;
3141
3142 n_responders = 0;
3143 while ((token = str_token(cmd, " ", &context))) {
3144 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
3145 wpa_printf(MSG_INFO,
3146 "CTRL: REQ_RANGE: Too many responders");
3147 return -1;
3148 }
3149
3150 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
3151 wpa_printf(MSG_INFO,
3152 "CTRL: REQ_RANGE: Bad responder address");
3153 return -1;
3154 }
3155
3156 n_responders++;
3157 }
3158
3159 if (!n_responders) {
3160 wpa_printf(MSG_INFO,
3161 "CTRL: REQ_RANGE - No FTM responder address");
3162 return -1;
3163 }
3164
3165 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
3166 responders, n_responders);
3167}
3168
3169
Dmitry Shmidt29333592017-01-09 12:27:11 -08003170static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
3171 const char *cmd, char *reply,
3172 size_t reply_size)
3173{
3174 u8 addr[ETH_ALEN];
3175 const char *pos;
3176 struct wpabuf *req;
3177 int ret;
3178 u8 req_mode = 0;
3179
3180 if (hwaddr_aton(cmd, addr))
3181 return -1;
3182 pos = os_strchr(cmd, ' ');
3183 if (!pos)
3184 return -1;
3185 pos++;
3186 if (os_strncmp(pos, "req_mode=", 9) == 0) {
3187 int val = hex2byte(pos + 9);
3188
3189 if (val < 0)
3190 return -1;
3191 req_mode = val;
3192 pos += 11;
3193 pos = os_strchr(pos, ' ');
3194 if (!pos)
3195 return -1;
3196 pos++;
3197 }
3198 req = wpabuf_parse_bin(pos);
3199 if (!req)
3200 return -1;
3201
3202 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
3203 wpabuf_free(req);
3204 if (ret >= 0)
3205 ret = os_snprintf(reply, reply_size, "%d", ret);
3206 return ret;
3207}
3208
3209
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003210static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3211 char *buf, size_t buflen)
3212{
3213 if (!(hapd->conf->radio_measurements[0] &
3214 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3215 wpa_printf(MSG_ERROR,
3216 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3217 return -1;
3218 }
3219
3220 return hostapd_neighbor_show(hapd, buf, buflen);
3221}
3222
3223
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003224static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3225{
3226 struct wpa_ssid_value ssid;
3227 u8 bssid[ETH_ALEN];
3228 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003229 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003230 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003231 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003232 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003233
3234 if (!(hapd->conf->radio_measurements[0] &
3235 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3236 wpa_printf(MSG_ERROR,
3237 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3238 return -1;
3239 }
3240
3241 if (hwaddr_aton(buf, bssid)) {
3242 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3243 return -1;
3244 }
3245
3246 tmp = os_strstr(buf, "ssid=");
3247 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3248 wpa_printf(MSG_ERROR,
3249 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3250 return -1;
3251 }
3252 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3253 if (!buf)
3254 return -1;
3255
3256 tmp = os_strstr(buf, "nr=");
3257 if (!tmp) {
3258 wpa_printf(MSG_ERROR,
3259 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3260 return -1;
3261 }
3262
3263 buf = os_strchr(tmp, ' ');
3264 if (buf)
3265 *buf++ = '\0';
3266
3267 nr = wpabuf_parse_bin(tmp + 3);
3268 if (!nr) {
3269 wpa_printf(MSG_ERROR,
3270 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3271 return -1;
3272 }
3273
3274 if (!buf)
3275 goto set;
3276
3277 tmp = os_strstr(buf, "lci=");
3278 if (tmp) {
3279 buf = os_strchr(tmp, ' ');
3280 if (buf)
3281 *buf++ = '\0';
3282 lci = wpabuf_parse_bin(tmp + 4);
3283 if (!lci) {
3284 wpa_printf(MSG_ERROR,
3285 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003286 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003287 }
3288 }
3289
3290 if (!buf)
3291 goto set;
3292
3293 tmp = os_strstr(buf, "civic=");
3294 if (tmp) {
3295 buf = os_strchr(tmp, ' ');
3296 if (buf)
3297 *buf++ = '\0';
3298 civic = wpabuf_parse_bin(tmp + 6);
3299 if (!civic) {
3300 wpa_printf(MSG_ERROR,
3301 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003302 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003303 }
3304 }
3305
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003306 if (!buf)
3307 goto set;
3308
3309 if (os_strstr(buf, "stat"))
3310 stationary = 1;
3311
Hai Shaloma20dcd72022-02-04 13:43:00 -08003312 tmp = os_strstr(buf, "bss_parameter=");
3313 if (tmp) {
3314 bss_parameters = atoi(tmp + 14);
3315 if (bss_parameters < 0 || bss_parameters > 0xff) {
3316 wpa_printf(MSG_ERROR,
3317 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3318 goto fail;
3319 }
3320 }
3321
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003322set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003323 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003324 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003325
Hai Shaloma20dcd72022-02-04 13:43:00 -08003326fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003327 wpabuf_free(nr);
3328 wpabuf_free(lci);
3329 wpabuf_free(civic);
3330
3331 return ret;
3332}
3333
3334
3335static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3336 char *buf)
3337{
3338 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003339 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003340 u8 bssid[ETH_ALEN];
3341 char *tmp;
3342
3343 if (hwaddr_aton(buf, bssid)) {
3344 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3345 return -1;
3346 }
3347
3348 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003349 if (tmp) {
3350 ssidp = &ssid;
3351 if (ssid_parse(tmp + 5, &ssid)) {
3352 wpa_printf(MSG_ERROR,
3353 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3354 return -1;
3355 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003356 }
3357
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003358 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003359}
3360
3361
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003362static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3363 size_t buflen)
3364{
3365 int ret, i;
3366 char *pos, *end;
3367
3368 ret = os_snprintf(buf, buflen, "%016llX:\n",
3369 (long long unsigned) iface->drv_flags);
3370 if (os_snprintf_error(buflen, ret))
3371 return -1;
3372
3373 pos = buf + ret;
3374 end = buf + buflen;
3375
3376 for (i = 0; i < 64; i++) {
3377 if (iface->drv_flags & (1LLU << i)) {
3378 ret = os_snprintf(pos, end - pos, "%s\n",
3379 driver_flag_to_string(1LLU << i));
3380 if (os_snprintf_error(end - pos, ret))
3381 return -1;
3382 pos += ret;
3383 }
3384 }
3385
3386 return pos - buf;
3387}
3388
3389
Hai Shalomb755a2a2020-04-23 21:49:02 -07003390static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3391 size_t buflen)
3392{
3393 int ret, i;
3394 char *pos, *end;
3395
3396 ret = os_snprintf(buf, buflen, "%016llX:\n",
3397 (long long unsigned) iface->drv_flags2);
3398 if (os_snprintf_error(buflen, ret))
3399 return -1;
3400
3401 pos = buf + ret;
3402 end = buf + buflen;
3403
3404 for (i = 0; i < 64; i++) {
3405 if (iface->drv_flags2 & (1LLU << i)) {
3406 ret = os_snprintf(pos, end - pos, "%s\n",
3407 driver_flag2_to_string(1LLU << i));
3408 if (os_snprintf_error(end - pos, ret))
3409 return -1;
3410 pos += ret;
3411 }
3412 }
3413
3414 return pos - buf;
3415}
3416
3417
Hai Shalom021b0b52019-04-10 11:17:58 -07003418static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3419 const char *field, char *buf,
3420 size_t buflen)
3421{
3422 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3423
3424#ifdef CONFIG_DPP
3425 if (os_strcmp(field, "dpp") == 0) {
3426 int res;
3427
Hai Shaloma20dcd72022-02-04 13:43:00 -08003428#ifdef CONFIG_DPP3
3429 res = os_snprintf(buf, buflen, "DPP=3");
3430#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003431 res = os_snprintf(buf, buflen, "DPP=2");
3432#else /* CONFIG_DPP2 */
3433 res = os_snprintf(buf, buflen, "DPP=1");
3434#endif /* CONFIG_DPP2 */
3435 if (os_snprintf_error(buflen, res))
3436 return -1;
3437 return res;
3438 }
3439#endif /* CONFIG_DPP */
3440
3441 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3442 field);
3443
3444 return -1;
3445}
3446
3447
Hai Shaloma20dcd72022-02-04 13:43:00 -08003448#ifdef ANDROID
3449static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3450 char *buf, size_t buflen)
3451{
3452 int ret;
3453
3454 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3455 if (ret == 0) {
3456 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3457 if (os_snprintf_error(buflen, ret))
3458 ret = -1;
3459 }
3460 return ret;
3461}
3462#endif /* ANDROID */
3463
3464
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003465#ifdef CONFIG_IEEE80211BE
3466
3467static int hostapd_ctrl_iface_enable_mld(struct hostapd_iface *iface)
3468{
3469 unsigned int i;
3470
3471 if (!iface || !iface->bss[0]->conf->mld_ap) {
3472 wpa_printf(MSG_ERROR,
3473 "Trying to enable AP MLD on an interface that is not affiliated with an AP MLD");
3474 return -1;
3475 }
3476
3477 for (i = 0; i < iface->interfaces->count; ++i) {
3478 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3479 struct hostapd_data *h_hapd = h_iface->bss[0];
3480 struct hostapd_bss_config *h_conf = h_hapd->conf;
3481
3482 if (!h_conf->mld_ap ||
3483 h_conf->mld_id != iface->bss[0]->conf->mld_id)
3484 continue;
3485
3486 if (hostapd_enable_iface(h_iface)) {
3487 wpa_printf(MSG_ERROR, "Enabling of AP MLD failed");
3488 return -1;
3489 }
3490 }
3491 return 0;
3492}
3493
3494
3495static void hostapd_disable_iface_bss(struct hostapd_iface *iface)
3496{
3497 unsigned int i;
3498
3499 for (i = 0; i < iface->num_bss; i++)
3500 hostapd_bss_deinit_no_free(iface->bss[i]);
3501}
3502
3503
3504static int hostapd_ctrl_iface_disable_mld(struct hostapd_iface *iface)
3505{
3506 unsigned int i;
3507 struct hostapd_iface *first_iface = NULL;
3508
3509 if (!iface || !iface->bss[0]->conf->mld_ap) {
3510 wpa_printf(MSG_ERROR,
3511 "Trying to disable AP MLD on an interface that is not affiliated with an AP MLD.");
3512 return -1;
3513 }
3514
3515 /* First, disable BSSs before stopping beaconing and doing driver
3516 * deinit so that the broadcast Deauthentication frames go out. */
3517
3518 for (i = 0; i < iface->interfaces->count; ++i) {
3519 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3520 struct hostapd_data *h_hapd = h_iface->bss[0];
3521 struct hostapd_bss_config *h_conf = h_hapd->conf;
3522
3523 if (!h_conf->mld_ap ||
3524 h_conf->mld_id != iface->bss[0]->conf->mld_id)
3525 continue;
3526
3527 if (!h_hapd->mld_first_bss) {
3528 first_iface = h_iface;
3529 continue;
3530 }
3531 hostapd_disable_iface_bss(iface);
3532 }
3533
3534 if (first_iface)
3535 hostapd_disable_iface_bss(first_iface);
3536
3537 /* Then, fully disable interfaces */
3538
3539 for (i = 0; i < iface->interfaces->count; ++i) {
3540 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3541 struct hostapd_data *h_hapd = h_iface->bss[0];
3542 struct hostapd_bss_config *h_conf = h_hapd->conf;
3543
3544 if (!h_conf->mld_ap ||
3545 h_conf->mld_id != iface->bss[0]->conf->mld_id ||
3546 !h_hapd->mld_first_bss)
3547 continue;
3548
3549 if (hostapd_disable_iface(h_iface)) {
3550 wpa_printf(MSG_ERROR, "Disabling AP MLD failed");
3551 return -1;
3552 }
3553 }
3554
3555 if (first_iface && hostapd_disable_iface(first_iface)) {
3556 wpa_printf(MSG_ERROR, "Disabling AP MLD failed");
3557 return -1;
3558 }
3559
3560 return 0;
3561}
3562
3563
3564#ifdef CONFIG_TESTING_OPTIONS
3565static int hostapd_ctrl_iface_link_remove(struct hostapd_data *hapd, char *cmd,
3566 char *buf, size_t buflen)
3567{
3568 int ret;
3569 u32 count = atoi(cmd);
3570
3571 if (!count)
3572 count = 1;
3573
3574 ret = hostapd_link_remove(hapd, count);
3575 if (ret == 0) {
3576 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3577 if (os_snprintf_error(buflen, ret))
3578 ret = -1;
3579 else
3580 ret = 0;
3581 }
3582
3583 return ret;
3584}
3585#endif /* CONFIG_TESTING_OPTIONS */
3586#endif /* CONFIG_IEEE80211BE */
3587
3588
3589#ifdef CONFIG_NAN_USD
3590
3591static int hostapd_ctrl_nan_publish(struct hostapd_data *hapd, char *cmd,
3592 char *buf, size_t buflen)
3593{
3594 char *token, *context = NULL;
3595 int publish_id;
3596 struct nan_publish_params params;
3597 const char *service_name = NULL;
3598 struct wpabuf *ssi = NULL;
3599 int ret = -1;
3600 enum nan_service_protocol_type srv_proto_type = 0;
3601
3602 os_memset(&params, 0, sizeof(params));
3603 /* USD shall use both solicited and unsolicited transmissions */
3604 params.unsolicited = true;
3605 params.solicited = true;
3606 /* USD shall require FSD without GAS */
3607 params.fsd = true;
3608
3609 while ((token = str_token(cmd, " ", &context))) {
3610 if (os_strncmp(token, "service_name=", 13) == 0) {
3611 service_name = token + 13;
3612 continue;
3613 }
3614
3615 if (os_strncmp(token, "ttl=", 4) == 0) {
3616 params.ttl = atoi(token + 4);
3617 continue;
3618 }
3619
3620 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3621 srv_proto_type = atoi(token + 15);
3622 continue;
3623 }
3624
3625 if (os_strncmp(token, "ssi=", 4) == 0) {
3626 if (ssi)
3627 goto fail;
3628 ssi = wpabuf_parse_bin(token + 4);
3629 if (!ssi)
3630 goto fail;
3631 continue;
3632 }
3633
3634 if (os_strcmp(token, "solicited=0") == 0) {
3635 params.solicited = false;
3636 continue;
3637 }
3638
3639 if (os_strcmp(token, "unsolicited=0") == 0) {
3640 params.unsolicited = false;
3641 continue;
3642 }
3643
3644 if (os_strcmp(token, "fsd=0") == 0) {
3645 params.fsd = false;
3646 continue;
3647 }
3648
3649 wpa_printf(MSG_INFO, "CTRL: Invalid NAN_PUBLISH parameter: %s",
3650 token);
3651 goto fail;
3652 }
3653
3654 publish_id = hostapd_nan_usd_publish(hapd, service_name, srv_proto_type,
3655 ssi, &params);
3656 if (publish_id > 0)
3657 ret = os_snprintf(buf, buflen, "%d", publish_id);
3658fail:
3659 wpabuf_free(ssi);
3660 return ret;
3661}
3662
3663
3664static int hostapd_ctrl_nan_cancel_publish(struct hostapd_data *hapd,
3665 char *cmd)
3666{
3667 char *token, *context = NULL;
3668 int publish_id = 0;
3669
3670 while ((token = str_token(cmd, " ", &context))) {
3671 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3672 continue;
3673 wpa_printf(MSG_INFO,
3674 "CTRL: Invalid NAN_CANCEL_PUBLISH parameter: %s",
3675 token);
3676 return -1;
3677 }
3678
3679 if (publish_id <= 0) {
3680 wpa_printf(MSG_INFO,
3681 "CTRL: Invalid or missing NAN_CANCEL_PUBLISH publish_id");
3682 return -1;
3683 }
3684
3685 hostapd_nan_usd_cancel_publish(hapd, publish_id);
3686 return 0;
3687}
3688
3689
3690static int hostapd_ctrl_nan_update_publish(struct hostapd_data *hapd,
3691 char *cmd)
3692{
3693 char *token, *context = NULL;
3694 int publish_id = 0;
3695 struct wpabuf *ssi = NULL;
3696 int ret = -1;
3697
3698 while ((token = str_token(cmd, " ", &context))) {
3699 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3700 continue;
3701 if (os_strncmp(token, "ssi=", 4) == 0) {
3702 if (ssi)
3703 goto fail;
3704 ssi = wpabuf_parse_bin(token + 4);
3705 if (!ssi)
3706 goto fail;
3707 continue;
3708 }
3709 wpa_printf(MSG_INFO,
3710 "CTRL: Invalid NAN_UPDATE_PUBLISH parameter: %s",
3711 token);
3712 goto fail;
3713 }
3714
3715 if (publish_id <= 0) {
3716 wpa_printf(MSG_INFO,
3717 "CTRL: Invalid or missing NAN_UPDATE_PUBLISH publish_id");
3718 goto fail;
3719 }
3720
3721 ret = hostapd_nan_usd_update_publish(hapd, publish_id, ssi);
3722fail:
3723 wpabuf_free(ssi);
3724 return ret;
3725}
3726
3727
3728static int hostapd_ctrl_nan_subscribe(struct hostapd_data *hapd, char *cmd,
3729 char *buf, size_t buflen)
3730{
3731 char *token, *context = NULL;
3732 int subscribe_id;
3733 struct nan_subscribe_params params;
3734 const char *service_name = NULL;
3735 struct wpabuf *ssi = NULL;
3736 int ret = -1;
3737 enum nan_service_protocol_type srv_proto_type = 0;
3738
3739 os_memset(&params, 0, sizeof(params));
3740
3741 while ((token = str_token(cmd, " ", &context))) {
3742 if (os_strncmp(token, "service_name=", 13) == 0) {
3743 service_name = token + 13;
3744 continue;
3745 }
3746
3747 if (os_strcmp(token, "active=1") == 0) {
3748 params.active = true;
3749 continue;
3750 }
3751
3752 if (os_strncmp(token, "ttl=", 4) == 0) {
3753 params.ttl = atoi(token + 4);
3754 continue;
3755 }
3756
3757 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3758 srv_proto_type = atoi(token + 15);
3759 continue;
3760 }
3761
3762 if (os_strncmp(token, "ssi=", 4) == 0) {
3763 if (ssi)
3764 goto fail;
3765 ssi = wpabuf_parse_bin(token + 4);
3766 if (!ssi)
3767 goto fail;
3768 continue;
3769 }
3770
3771 wpa_printf(MSG_INFO,
3772 "CTRL: Invalid NAN_SUBSCRIBE parameter: %s",
3773 token);
3774 goto fail;
3775 }
3776
3777 subscribe_id = hostapd_nan_usd_subscribe(hapd, service_name,
3778 srv_proto_type, ssi,
3779 &params);
3780 if (subscribe_id > 0)
3781 ret = os_snprintf(buf, buflen, "%d", subscribe_id);
3782fail:
3783 wpabuf_free(ssi);
3784 return ret;
3785}
3786
3787
3788static int hostapd_ctrl_nan_cancel_subscribe(struct hostapd_data *hapd,
3789 char *cmd)
3790{
3791 char *token, *context = NULL;
3792 int subscribe_id = 0;
3793
3794 while ((token = str_token(cmd, " ", &context))) {
3795 if (sscanf(token, "subscribe_id=%i", &subscribe_id) == 1)
3796 continue;
3797 wpa_printf(MSG_INFO,
3798 "CTRL: Invalid NAN_CANCEL_SUBSCRIBE parameter: %s",
3799 token);
3800 return -1;
3801 }
3802
3803 if (subscribe_id <= 0) {
3804 wpa_printf(MSG_INFO,
3805 "CTRL: Invalid or missing NAN_CANCEL_SUBSCRIBE subscribe_id");
3806 return -1;
3807 }
3808
3809 hostapd_nan_usd_cancel_subscribe(hapd, subscribe_id);
3810 return 0;
3811}
3812
3813
3814static int hostapd_ctrl_nan_transmit(struct hostapd_data *hapd, char *cmd)
3815{
3816 char *token, *context = NULL;
3817 int handle = 0;
3818 int req_instance_id = 0;
3819 struct wpabuf *ssi = NULL;
3820 u8 peer_addr[ETH_ALEN];
3821 int ret = -1;
3822
3823 os_memset(peer_addr, 0, ETH_ALEN);
3824
3825 while ((token = str_token(cmd, " ", &context))) {
3826 if (sscanf(token, "handle=%i", &handle) == 1)
3827 continue;
3828
3829 if (sscanf(token, "req_instance_id=%i", &req_instance_id) == 1)
3830 continue;
3831
3832 if (os_strncmp(token, "address=", 8) == 0) {
3833 if (hwaddr_aton(token + 8, peer_addr) < 0)
3834 return -1;
3835 continue;
3836 }
3837
3838 if (os_strncmp(token, "ssi=", 4) == 0) {
3839 if (ssi)
3840 goto fail;
3841 ssi = wpabuf_parse_bin(token + 4);
3842 if (!ssi)
3843 goto fail;
3844 continue;
3845 }
3846
3847 wpa_printf(MSG_INFO,
3848 "CTRL: Invalid NAN_TRANSMIT parameter: %s",
3849 token);
3850 goto fail;
3851 }
3852
3853 if (handle <= 0) {
3854 wpa_printf(MSG_INFO,
3855 "CTRL: Invalid or missing NAN_TRANSMIT handle");
3856 goto fail;
3857 }
3858
3859 if (is_zero_ether_addr(peer_addr)) {
3860 wpa_printf(MSG_INFO,
3861 "CTRL: Invalid or missing NAN_TRANSMIT address");
3862 goto fail;
3863 }
3864
3865 ret = hostapd_nan_usd_transmit(hapd, handle, ssi, NULL, peer_addr,
3866 req_instance_id);
3867fail:
3868 wpabuf_free(ssi);
3869 return ret;
3870}
3871
3872#endif /* CONFIG_NAN_USD */
3873
3874
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003875static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3876 char *buf, char *reply,
3877 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003878 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003879 socklen_t fromlen)
3880{
3881 int reply_len, res;
3882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883 os_memcpy(reply, "OK\n", 3);
3884 reply_len = 3;
3885
3886 if (os_strcmp(buf, "PING") == 0) {
3887 os_memcpy(reply, "PONG\n", 5);
3888 reply_len = 5;
3889 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3890 if (wpa_debug_reopen_file() < 0)
3891 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003892 } else if (os_strcmp(buf, "CLOSE_LOG") == 0) {
3893 wpa_debug_stop_log();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003894 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3895 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003896 } else if (os_strcmp(buf, "STATUS") == 0) {
3897 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3898 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003899 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3900 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901 } else if (os_strcmp(buf, "MIB") == 0) {
3902 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3903 if (reply_len >= 0) {
3904 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3905 reply_size - reply_len);
3906 if (res < 0)
3907 reply_len = -1;
3908 else
3909 reply_len += res;
3910 }
3911 if (reply_len >= 0) {
3912 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3913 reply_size - reply_len);
3914 if (res < 0)
3915 reply_len = -1;
3916 else
3917 reply_len += res;
3918 }
3919#ifndef CONFIG_NO_RADIUS
3920 if (reply_len >= 0) {
3921 res = radius_client_get_mib(hapd->radius,
3922 reply + reply_len,
3923 reply_size - reply_len);
3924 if (res < 0)
3925 reply_len = -1;
3926 else
3927 reply_len += res;
3928 }
3929#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003930 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3931 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3932 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3934 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3935 reply_size);
3936 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3937 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3938 reply_size);
3939 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3940 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3941 reply_size);
3942 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003943 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3944 reply_len = -1;
3945 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3946 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 reply_len = -1;
3948 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003949 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950 reply_len = -1;
3951 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003952 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 buf + 6))
3954 reply_len = -1;
3955 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3956 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3957 reply_len = -1;
3958 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3959 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3960 reply_len = -1;
3961 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3962 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3963 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003964#ifdef CONFIG_TAXONOMY
3965 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3966 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3967 reply, reply_size);
3968#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003969 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3970 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3971 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003972 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3973 if (hostapd_ctrl_iface_stop_ap(hapd))
3974 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003975#ifdef NEED_AP_MLME
3976 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3977 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3978 reply_len = -1;
3979#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980#ifdef CONFIG_WPS
3981 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3982 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3983 reply_len = -1;
3984 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3985 reply_len = hostapd_ctrl_iface_wps_check_pin(
3986 hapd, buf + 14, reply, reply_size);
3987 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3988 if (hostapd_wps_button_pushed(hapd, NULL))
3989 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003990 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3991 if (hostapd_wps_cancel(hapd))
3992 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3994 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3995 reply, reply_size);
3996 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3997 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3998 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003999 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
4000 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
4001 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004002#ifdef CONFIG_WPS_NFC
4003 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4004 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
4005 reply_len = -1;
4006 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
4007 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
4008 hapd, buf + 21, reply, reply_size);
4009 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4010 reply_len = hostapd_ctrl_iface_wps_nfc_token(
4011 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004012 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
4013 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
4014 hapd, buf + 21, reply, reply_size);
4015 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
4016 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
4017 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004018#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004019#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07004020#ifdef CONFIG_INTERWORKING
4021 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
4022 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
4023 reply_len = -1;
4024 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
4025 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
4026 reply_len = -1;
4027#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004028#ifdef CONFIG_HS20
4029 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
4030 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
4031 reply_len = -1;
4032 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
4033 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
4034 reply_len = -1;
4035#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004036#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004037 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
4038 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
4039 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004040 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
4041 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
4042 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004043 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
4044 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
4045 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004046 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
4047 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
4048 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004049#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
4051 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
4052 reply_size);
4053 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4054 if (hostapd_ctrl_iface_set(hapd, buf + 4))
4055 reply_len = -1;
4056 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4057 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
4058 reply_size);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004059 } else if (os_strcmp(buf, "ENABLE") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004060 if (hostapd_ctrl_iface_enable(hapd->iface))
4061 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08004062 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
4063 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
4064 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004065#ifdef CONFIG_IEEE80211R_AP
4066 } else if (os_strcmp(buf, "GET_RXKHS") == 0) {
4067 reply_len = hostapd_ctrl_iface_get_rxkhs(hapd, reply,
4068 reply_size);
4069 } else if (os_strcmp(buf, "RELOAD_RXKHS") == 0) {
4070 if (hostapd_ctrl_iface_reload_rxkhs(hapd))
4071 reply_len = -1;
4072#endif /* CONFIG_IEEE80211R_AP */
Sunil Ravi77d572f2023-01-17 23:58:31 +00004073 } else if (os_strcmp(buf, "RELOAD_BSS") == 0) {
4074 if (hostapd_ctrl_iface_reload_bss(hapd))
4075 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004076 } else if (os_strcmp(buf, "RELOAD_CONFIG") == 0) {
4077 if (hostapd_reload_config(hapd->iface))
4078 reply_len = -1;
4079 } else if (os_strcmp(buf, "RELOAD") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004080 if (hostapd_ctrl_iface_reload(hapd->iface))
4081 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004082 } else if (os_strcmp(buf, "DISABLE") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004083 if (hostapd_ctrl_iface_disable(hapd->iface))
4084 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004085 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
4086 if (ieee802_11_set_beacon(hapd))
4087 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004088#ifdef CONFIG_TESTING_OPTIONS
4089 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
4090 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
4091 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004092 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
4093 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
4094 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004095 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
4096 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
4097 buf + 23) < 0)
4098 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004099 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
4100 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
4101 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004102 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
4103 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
4104 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004105 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
4106 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
4107 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004108 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
4109 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
4110 reply_len = -1;
4111 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
4112 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
4113 reply_len = -1;
4114 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
4115 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
4116 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004117 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004118 if (testing_set_fail_pattern(true, buf + 16) < 0)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004119 reply_len = -1;
4120 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004121 reply_len = testing_get_fail_pattern(true, reply, reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004122 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004123 if (testing_set_fail_pattern(false, buf + 10) < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004124 reply_len = -1;
4125 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004126 reply_len = testing_get_fail_pattern(false, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004127 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
4128 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
4129 reply_len = -1;
4130 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
4131 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
4132 reply_len = -1;
4133 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
4134 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
4135 reply_len = -1;
4136 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
4137 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
4138 reply_len = -1;
4139 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
4140 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
4141 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004142 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
4143 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
4144 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004145 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
4146 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
4147 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004148 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
4149 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
4150 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08004151 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
4152 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
4153 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004154#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004155 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004156 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004157 reply_len = -1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004158 } else if (os_strncmp(buf, "NOTIFY_CW_CHANGE ", 17) == 0) {
4159 if (hostapd_ctrl_iface_notify_cw_change(hapd, buf + 17))
4160 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004161 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
4162 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
4163 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004164 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
4165 ieee802_1x_erp_flush(hapd);
4166#ifdef RADIUS_SERVER
4167 radius_server_erp_flush(hapd->radius_srv);
4168#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004169 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
4170 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
4171 reply_len = -1;
4172 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
4173 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
4174 reply_len = -1;
4175 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
4176 reply_len = hostapd_ctrl_iface_log_level(
4177 hapd, buf + 9, reply, reply_size);
4178#ifdef NEED_AP_MLME
4179 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
4180 reply_len = hostapd_ctrl_iface_track_sta_list(
4181 hapd, reply, reply_size);
4182#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07004183 } else if (os_strcmp(buf, "PMKSA") == 0) {
4184 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
4185 reply_size);
4186 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4187 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004188 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
4189 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
4190 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004191 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
4192 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
4193 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004194 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
4195 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
4196 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004197 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
4198 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
4199 reply_len = -1;
4200 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
4201 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
4202 reply_len = -1;
4203 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
4204 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
4205 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08004206 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
4207 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
4208 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004209 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
4210 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
4211 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07004212 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
4213 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
4214 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08004215 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4216 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07004217 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
4218 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004219 if (hostapd_ctrl_iface_acl_add_mac(
4220 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004221 &hapd->conf->num_accept_mac, buf + 19) ||
4222 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004223 reply_len = -1;
4224 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07004225 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07004226 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004227 &hapd->conf->num_accept_mac, buf + 19) ||
4228 hostapd_set_acl(hapd) ||
4229 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07004230 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004231 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
4232 reply_len = hostapd_ctrl_iface_acl_show_mac(
4233 hapd->conf->accept_mac,
4234 hapd->conf->num_accept_mac, reply, reply_size);
4235 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
4236 hostapd_ctrl_iface_acl_clear_list(
4237 &hapd->conf->accept_mac,
4238 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07004239 if (hostapd_set_acl(hapd) ||
4240 hostapd_disassoc_accept_mac(hapd))
4241 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07004242 } else {
4243 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004244 }
4245 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
4246 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07004247 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07004248 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004249 &hapd->conf->num_deny_mac, buf + 17) ||
4250 hostapd_set_acl(hapd) ||
4251 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004252 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004253 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004254 if (hostapd_ctrl_iface_acl_del_mac(
4255 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004256 &hapd->conf->num_deny_mac, buf + 17) ||
4257 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004258 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004259 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
4260 reply_len = hostapd_ctrl_iface_acl_show_mac(
4261 hapd->conf->deny_mac,
4262 hapd->conf->num_deny_mac, reply, reply_size);
4263 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
4264 hostapd_ctrl_iface_acl_clear_list(
4265 &hapd->conf->deny_mac,
4266 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07004267 if (hostapd_set_acl(hapd))
4268 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07004269 } else {
4270 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004271 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004272#ifdef CONFIG_DPP
4273 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
4274 res = hostapd_dpp_qr_code(hapd, buf + 12);
4275 if (res < 0) {
4276 reply_len = -1;
4277 } else {
4278 reply_len = os_snprintf(reply, reply_size, "%d", res);
4279 if (os_snprintf_error(reply_size, reply_len))
4280 reply_len = -1;
4281 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004282 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
4283 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
4284 if (res < 0) {
4285 reply_len = -1;
4286 } else {
4287 reply_len = os_snprintf(reply, reply_size, "%d", res);
4288 if (os_snprintf_error(reply_size, reply_len))
4289 reply_len = -1;
4290 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004291 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
4292 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
4293 if (res < 0) {
4294 reply_len = -1;
4295 } else {
4296 reply_len = os_snprintf(reply, reply_size, "%d", res);
4297 if (os_snprintf_error(reply_size, reply_len))
4298 reply_len = -1;
4299 }
4300 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
4301 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
4302 if (res < 0) {
4303 reply_len = -1;
4304 } else {
4305 reply_len = os_snprintf(reply, reply_size, "%d", res);
4306 if (os_snprintf_error(reply_size, reply_len))
4307 reply_len = -1;
4308 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004309 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004310 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004311 if (res < 0) {
4312 reply_len = -1;
4313 } else {
4314 reply_len = os_snprintf(reply, reply_size, "%d", res);
4315 if (os_snprintf_error(reply_size, reply_len))
4316 reply_len = -1;
4317 }
4318 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004319 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
4320 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004321 reply_len = -1;
4322 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
4323 const char *uri;
4324
Hai Shalom021b0b52019-04-10 11:17:58 -07004325 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
4326 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004327 if (!uri) {
4328 reply_len = -1;
4329 } else {
4330 reply_len = os_snprintf(reply, reply_size, "%s", uri);
4331 if (os_snprintf_error(reply_size, reply_len))
4332 reply_len = -1;
4333 }
4334 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004335 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
4336 atoi(buf + 19),
4337 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07004338 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
4339 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
4340 atoi(buf + 18),
4341 os_strchr(buf + 18, ' ')) < 0)
4342 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004343 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
4344 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
4345 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004346 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
4347 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
4348 reply_len = -1;
4349 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
4350 hostapd_dpp_stop(hapd);
4351 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004352 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004353 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
4354 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004355 if (res < 0) {
4356 reply_len = -1;
4357 } else {
4358 reply_len = os_snprintf(reply, reply_size, "%d", res);
4359 if (os_snprintf_error(reply_size, reply_len))
4360 reply_len = -1;
4361 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004362 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
4363 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
4364 buf + 20) < 0)
4365 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004366 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004367 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
4368 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004369 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004370 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004371 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004372 reply_len = -1;
4373 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004374 reply_len = dpp_configurator_get_key_id(
4375 hapd->iface->interfaces->dpp,
4376 atoi(buf + 25),
4377 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004378 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
4379 res = hostapd_dpp_pkex_add(hapd, buf + 12);
4380 if (res < 0) {
4381 reply_len = -1;
4382 } else {
4383 reply_len = os_snprintf(reply, reply_size, "%d", res);
4384 if (os_snprintf_error(reply_size, reply_len))
4385 reply_len = -1;
4386 }
4387 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
4388 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
4389 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004390#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07004391 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
4392 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
4393 reply_len = -1;
4394 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
4395 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
4396 reply_len = -1;
4397 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
4398 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004399 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
4400 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
4401 reply_len = -1;
4402 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
4403 hostapd_dpp_chirp_stop(hapd);
Sunil Ravi89eba102022-09-13 21:04:37 -07004404 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
4405 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
4406 reply_len = -1;
4407 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
4408 hostapd_dpp_remove_controller(hapd, buf + 28);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004409#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07004410#ifdef CONFIG_DPP3
4411 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
4412 if (hostapd_dpp_push_button(hapd, NULL) < 0)
4413 reply_len = -1;
4414 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
4415 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
4416 reply_len = -1;
4417#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004418#endif /* CONFIG_DPP */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004419#ifdef CONFIG_NAN_USD
4420 } else if (os_strncmp(buf, "NAN_PUBLISH ", 12) == 0) {
4421 reply_len = hostapd_ctrl_nan_publish(hapd, buf + 12, reply,
4422 reply_size);
4423 } else if (os_strncmp(buf, "NAN_CANCEL_PUBLISH ", 19) == 0) {
4424 if (hostapd_ctrl_nan_cancel_publish(hapd, buf + 19) < 0)
4425 reply_len = -1;
4426 } else if (os_strncmp(buf, "NAN_UPDATE_PUBLISH ", 19) == 0) {
4427 if (hostapd_ctrl_nan_update_publish(hapd, buf + 19) < 0)
4428 reply_len = -1;
4429 } else if (os_strncmp(buf, "NAN_SUBSCRIBE ", 14) == 0) {
4430 reply_len = hostapd_ctrl_nan_subscribe(hapd, buf + 14, reply,
4431 reply_size);
4432 } else if (os_strncmp(buf, "NAN_CANCEL_SUBSCRIBE ", 21) == 0) {
4433 if (hostapd_ctrl_nan_cancel_subscribe(hapd, buf + 21) < 0)
4434 reply_len = -1;
4435 } else if (os_strncmp(buf, "NAN_TRANSMIT ", 13) == 0) {
4436 if (hostapd_ctrl_nan_transmit(hapd, buf + 13) < 0)
4437 reply_len = -1;
4438#endif /* CONFIG_NAN_USD */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004439#ifdef RADIUS_SERVER
4440 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
4441 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
4442 reply_len = -1;
4443#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07004444 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4445 reply_len = hostapd_ctrl_iface_get_capability(
4446 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08004447#ifdef CONFIG_PASN
4448 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
4449 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
4450#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08004451#ifdef ANDROID
4452 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
4453 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
4454 reply_size);
4455#endif /* ANDROID */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004456#ifdef CONFIG_IEEE80211BE
4457 } else if (os_strcmp(buf, "ENABLE_MLD") == 0) {
4458 if (hostapd_ctrl_iface_enable_mld(hapd->iface))
4459 reply_len = -1;
4460 } else if (os_strcmp(buf, "DISABLE_MLD") == 0) {
4461 if (hostapd_ctrl_iface_disable_mld(hapd->iface))
4462 reply_len = -1;
4463#ifdef CONFIG_TESTING_OPTIONS
4464 } else if (os_strncmp(buf, "LINK_REMOVE ", 12) == 0) {
4465 if (hostapd_ctrl_iface_link_remove(hapd, buf + 12,
4466 reply, reply_size))
4467 reply_len = -1;
4468#endif /* CONFIG_TESTING_OPTIONS */
4469#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 } else {
4471 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4472 reply_len = 16;
4473 }
4474
4475 if (reply_len < 0) {
4476 os_memcpy(reply, "FAIL\n", 5);
4477 reply_len = 5;
4478 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004479
4480 return reply_len;
4481}
4482
4483
4484static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
4485 void *sock_ctx)
4486{
4487 struct hostapd_data *hapd = eloop_ctx;
4488 char buf[4096];
4489 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004490 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004491 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004492 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004493 const int reply_size = 4096;
4494 int reply_len;
4495 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004496#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004497 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004498#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004499
4500 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4501 (struct sockaddr *) &from, &fromlen);
4502 if (res < 0) {
4503 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4504 strerror(errno));
4505 return;
4506 }
4507 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004508
4509 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 Shmidt31a29cc2016-03-09 15:58:17 -08004519#ifdef CONFIG_CTRL_IFACE_UDP
4520 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4521 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004522 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4523 hapd->ctrl_iface_cookie,
4524 CTRL_IFACE_COOKIE_LEN);
4525 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004526 goto done;
4527 }
4528
4529 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004530 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004531 wpa_printf(MSG_DEBUG,
4532 "CTRL: No cookie in the request - drop request");
4533 os_free(reply);
4534 return;
4535 }
4536
Hai Shalomfdcde762020-04-02 11:19:20 -07004537 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
4538 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004539 wpa_printf(MSG_DEBUG,
4540 "CTRL: Invalid cookie in the request - drop request");
4541 os_free(reply);
4542 return;
4543 }
4544
Hai Shalomfdcde762020-04-02 11:19:20 -07004545 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004546 while (*pos == ' ')
4547 pos++;
4548#endif /* CONFIG_CTRL_IFACE_UDP */
4549
4550 if (os_strcmp(pos, "PING") == 0)
4551 level = MSG_EXCESSIVE;
4552 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
4553
4554 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004555 reply, reply_size,
4556 &from, fromlen);
4557
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004558#ifdef CONFIG_CTRL_IFACE_UDP
4559done:
4560#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004561 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4562 fromlen) < 0) {
4563 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4564 strerror(errno));
4565 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 os_free(reply);
4567}
4568
4569
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004570#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
4572{
4573 char *buf;
4574 size_t len;
4575
4576 if (hapd->conf->ctrl_interface == NULL)
4577 return NULL;
4578
4579 len = os_strlen(hapd->conf->ctrl_interface) +
4580 os_strlen(hapd->conf->iface) + 2;
4581 buf = os_malloc(len);
4582 if (buf == NULL)
4583 return NULL;
4584
4585 os_snprintf(buf, len, "%s/%s",
4586 hapd->conf->ctrl_interface, hapd->conf->iface);
4587 buf[len - 1] = '\0';
4588 return buf;
4589}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004590#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591
4592
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004593static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
4594 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 const char *txt, size_t len)
4596{
4597 struct hostapd_data *hapd = ctx;
4598 if (hapd == NULL)
4599 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004600 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601}
4602
4603
4604int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
4605{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004606#ifdef CONFIG_CTRL_IFACE_UDP
4607 int port = HOSTAPD_CTRL_IFACE_PORT;
4608 char p[32] = { 0 };
4609 char port_str[40], *tmp;
4610 char *pos;
4611 struct addrinfo hints = { 0 }, *res, *saveres;
4612 int n;
4613
4614 if (hapd->ctrl_sock > -1) {
4615 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4616 return 0;
4617 }
4618
4619 if (hapd->conf->ctrl_interface == NULL)
4620 return 0;
4621
4622 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
4623 if (pos) {
4624 pos += 4;
4625 port = atoi(pos);
4626 if (port <= 0) {
4627 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
4628 goto fail;
4629 }
4630 }
4631
4632 dl_list_init(&hapd->ctrl_dst);
4633 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004634 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004635
4636#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4637 hints.ai_flags = AI_PASSIVE;
4638#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4639
4640#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4641 hints.ai_family = AF_INET6;
4642#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4643 hints.ai_family = AF_INET;
4644#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4645 hints.ai_socktype = SOCK_DGRAM;
4646
4647try_again:
4648 os_snprintf(p, sizeof(p), "%d", port);
4649 n = getaddrinfo(NULL, p, &hints, &res);
4650 if (n) {
4651 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4652 goto fail;
4653 }
4654
4655 saveres = res;
4656 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
4657 res->ai_protocol);
4658 if (hapd->ctrl_sock < 0) {
4659 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4660 goto fail;
4661 }
4662
4663 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
4664 port--;
4665 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
4666 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
4667 goto try_again;
4668 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4669 goto fail;
4670 }
4671
4672 freeaddrinfo(saveres);
4673
4674 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
4675 tmp = os_strdup(port_str);
4676 if (tmp) {
4677 os_free(hapd->conf->ctrl_interface);
4678 hapd->conf->ctrl_interface = tmp;
4679 }
4680 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
4681
4682 if (eloop_register_read_sock(hapd->ctrl_sock,
4683 hostapd_ctrl_iface_receive, hapd, NULL) <
4684 0) {
4685 hostapd_ctrl_iface_deinit(hapd);
4686 return -1;
4687 }
4688
4689 hapd->msg_ctx = hapd;
4690 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4691
4692 return 0;
4693
4694fail:
4695 if (hapd->ctrl_sock >= 0)
4696 close(hapd->ctrl_sock);
4697 return -1;
4698#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699 struct sockaddr_un addr;
4700 int s = -1;
4701 char *fname = NULL;
4702
Dmitry Shmidt04949592012-07-19 12:16:46 -07004703 if (hapd->ctrl_sock > -1) {
4704 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4705 return 0;
4706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004708 dl_list_init(&hapd->ctrl_dst);
4709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710 if (hapd->conf->ctrl_interface == NULL)
4711 return 0;
4712
4713 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4714 if (errno == EEXIST) {
4715 wpa_printf(MSG_DEBUG, "Using existing control "
4716 "interface directory.");
4717 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004718 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4719 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 goto fail;
4721 }
4722 }
4723
4724 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004725 lchown(hapd->conf->ctrl_interface, -1,
4726 hapd->conf->ctrl_interface_gid) < 0) {
4727 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004728 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 return -1;
4730 }
4731
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004732 if (!hapd->conf->ctrl_interface_gid_set &&
4733 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004734 lchown(hapd->conf->ctrl_interface, -1,
4735 hapd->iface->interfaces->ctrl_iface_group) < 0) {
4736 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004737 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004738 return -1;
4739 }
4740
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004741#ifdef ANDROID
4742 /*
4743 * Android is using umask 0077 which would leave the control interface
4744 * directory without group access. This breaks things since Wi-Fi
4745 * framework assumes that this directory can be accessed by other
4746 * applications in the wifi group. Fix this by adding group access even
4747 * if umask value would prevent this.
4748 */
4749 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4750 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4751 strerror(errno));
4752 /* Try to continue anyway */
4753 }
4754#endif /* ANDROID */
4755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4757 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4758 goto fail;
4759
4760 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4761 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004762 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763 goto fail;
4764 }
4765
4766 os_memset(&addr, 0, sizeof(addr));
4767#ifdef __FreeBSD__
4768 addr.sun_len = sizeof(addr);
4769#endif /* __FreeBSD__ */
4770 addr.sun_family = AF_UNIX;
4771 fname = hostapd_ctrl_iface_path(hapd);
4772 if (fname == NULL)
4773 goto fail;
4774 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4775 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4776 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4777 strerror(errno));
4778 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4779 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4780 " allow connections - assuming it was left"
4781 "over from forced program termination");
4782 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004783 wpa_printf(MSG_ERROR,
4784 "Could not unlink existing ctrl_iface socket '%s': %s",
4785 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004786 goto fail;
4787 }
4788 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4789 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004790 wpa_printf(MSG_ERROR,
4791 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4792 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004793 goto fail;
4794 }
4795 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4796 "ctrl_iface socket '%s'", fname);
4797 } else {
4798 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4799 "be in use - cannot override it");
4800 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4801 "not used anymore", fname);
4802 os_free(fname);
4803 fname = NULL;
4804 goto fail;
4805 }
4806 }
4807
4808 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004809 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4810 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004811 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812 goto fail;
4813 }
4814
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004815 if (!hapd->conf->ctrl_interface_gid_set &&
4816 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004817 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4818 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004819 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004820 goto fail;
4821 }
4822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004823 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004824 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4825 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826 goto fail;
4827 }
4828 os_free(fname);
4829
4830 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004831 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4832 NULL) < 0) {
4833 hostapd_ctrl_iface_deinit(hapd);
4834 return -1;
4835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836 hapd->msg_ctx = hapd;
4837 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4838
4839 return 0;
4840
4841fail:
4842 if (s >= 0)
4843 close(s);
4844 if (fname) {
4845 unlink(fname);
4846 os_free(fname);
4847 }
4848 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004849#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850}
4851
4852
4853void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4854{
4855 struct wpa_ctrl_dst *dst, *prev;
4856
4857 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004858#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004859 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004860#endif /* !CONFIG_CTRL_IFACE_UDP */
4861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862 eloop_unregister_read_sock(hapd->ctrl_sock);
4863 close(hapd->ctrl_sock);
4864 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004865#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866 fname = hostapd_ctrl_iface_path(hapd);
4867 if (fname)
4868 unlink(fname);
4869 os_free(fname);
4870
4871 if (hapd->conf->ctrl_interface &&
4872 rmdir(hapd->conf->ctrl_interface) < 0) {
4873 if (errno == ENOTEMPTY) {
4874 wpa_printf(MSG_DEBUG, "Control interface "
4875 "directory not empty - leaving it "
4876 "behind");
4877 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004878 wpa_printf(MSG_ERROR,
4879 "rmdir[ctrl_interface=%s]: %s",
4880 hapd->conf->ctrl_interface,
4881 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882 }
4883 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004884#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885 }
4886
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004887 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4888 list)
4889 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004890
4891#ifdef CONFIG_TESTING_OPTIONS
4892 l2_packet_deinit(hapd->l2_test);
4893 hapd->l2_test = NULL;
4894#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004895}
4896
4897
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004898static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4899 char *buf)
4900{
4901 if (hostapd_add_iface(interfaces, buf) < 0) {
4902 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4903 return -1;
4904 }
4905 return 0;
4906}
4907
4908
4909static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4910 char *buf)
4911{
4912 if (hostapd_remove_iface(interfaces, buf) < 0) {
4913 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4914 return -1;
4915 }
4916 return 0;
4917}
4918
4919
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004920static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004921 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004922 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004923{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004924 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4925 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004926}
4927
4928
4929static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004930 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004931 socklen_t fromlen)
4932{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004933 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004934}
4935
4936
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004937static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4938{
4939#ifdef CONFIG_WPS_TESTING
4940 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004941 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004942 wps_corrupt_pkhash = 0;
4943#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004944
4945#ifdef CONFIG_TESTING_OPTIONS
4946#ifdef CONFIG_DPP
4947 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004948#ifdef CONFIG_DPP3
4949 dpp_version_override = 3;
4950#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004951 dpp_version_override = 2;
4952#else /* CONFIG_DPP2 */
4953 dpp_version_override = 1;
4954#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004955#endif /* CONFIG_DPP */
4956#endif /* CONFIG_TESTING_OPTIONS */
4957
4958#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004959 dpp_global_clear(interfaces->dpp);
Sunil Ravi89eba102022-09-13 21:04:37 -07004960#ifdef CONFIG_DPP3
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004961 interfaces->dpp_pb_bi = NULL;
Sunil Ravi89eba102022-09-13 21:04:37 -07004962 {
4963 int i;
4964
4965 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
4966 struct dpp_pb_info *info;
4967
4968 info = &interfaces->dpp_pb[i];
4969 info->rx_time.sec = 0;
4970 info->rx_time.usec = 0;
4971 }
4972 }
4973#endif /* CONFIG_DPP3 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004974#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004975}
4976
4977
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004978#ifdef CONFIG_FST
4979
4980static int
4981hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4982 const char *cmd)
4983{
4984 char ifname[IFNAMSIZ + 1];
4985 struct fst_iface_cfg cfg;
4986 struct hostapd_data *hapd;
4987 struct fst_wpa_obj iface_obj;
4988
4989 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4990 hapd = hostapd_get_iface(interfaces, ifname);
4991 if (hapd) {
4992 if (hapd->iface->fst) {
4993 wpa_printf(MSG_INFO, "FST: Already attached");
4994 return -1;
4995 }
4996 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4997 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4998 &iface_obj, &cfg);
4999 if (hapd->iface->fst)
5000 return 0;
5001 }
5002 }
5003
5004 return -EINVAL;
5005}
5006
5007
5008static int
5009hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
5010 const char *cmd)
5011{
5012 char ifname[IFNAMSIZ + 1];
5013 struct hostapd_data * hapd;
5014
5015 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
5016 hapd = hostapd_get_iface(interfaces, ifname);
5017 if (hapd) {
5018 if (!fst_iface_detach(ifname)) {
5019 hapd->iface->fst = NULL;
5020 hapd->iface->fst_ies = NULL;
5021 return 0;
5022 }
5023 }
5024 }
5025
5026 return -EINVAL;
5027}
5028
5029#endif /* CONFIG_FST */
5030
5031
5032static struct hostapd_data *
5033hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
5034 const char *ifname)
5035{
5036 size_t i, j;
5037
5038 for (i = 0; i < interfaces->count; i++) {
5039 struct hostapd_iface *iface = interfaces->iface[i];
5040
5041 for (j = 0; j < iface->num_bss; j++) {
5042 struct hostapd_data *hapd;
5043
5044 hapd = iface->bss[j];
5045 if (os_strcmp(ifname, hapd->conf->iface) == 0)
5046 return hapd;
5047 }
5048 }
5049
5050 return NULL;
5051}
5052
5053
5054static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
5055 struct hostapd_data *dst_hapd,
5056 const char *param)
5057{
5058 int res;
5059 char *value;
5060
5061 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5062 if (!value) {
5063 wpa_printf(MSG_ERROR,
5064 "DUP: cannot allocate buffer to stringify %s",
5065 param);
5066 goto error_return;
5067 }
5068
5069 if (os_strcmp(param, "wpa") == 0) {
5070 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
5071 src_hapd->conf->wpa);
5072 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
5073 src_hapd->conf->wpa_key_mgmt) {
5074 res = hostapd_ctrl_iface_get_key_mgmt(
5075 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5076 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
5077 goto error_stringify;
5078 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
5079 src_hapd->conf->wpa_pairwise) {
5080 res = wpa_write_ciphers(value,
5081 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5082 src_hapd->conf->wpa_pairwise, " ");
5083 if (res < 0)
5084 goto error_stringify;
5085 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
5086 src_hapd->conf->rsn_pairwise) {
5087 res = wpa_write_ciphers(value,
5088 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5089 src_hapd->conf->rsn_pairwise, " ");
5090 if (res < 0)
5091 goto error_stringify;
5092 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
5093 src_hapd->conf->ssid.wpa_passphrase) {
5094 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
5095 src_hapd->conf->ssid.wpa_passphrase);
5096 } else if (os_strcmp(param, "wpa_psk") == 0 &&
5097 src_hapd->conf->ssid.wpa_psk_set) {
5098 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5099 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
5100 } else {
5101 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
5102 goto error_return;
5103 }
5104
5105 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
5106 os_free(value);
5107 return res;
5108
5109error_stringify:
5110 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
5111error_return:
5112 os_free(value);
5113 return -1;
5114}
5115
5116
5117static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005118hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
5119 const char *input,
5120 char *reply, int reply_size)
5121{
5122 size_t i, j;
5123 int res;
5124 char *pos, *end;
5125 struct hostapd_iface *iface;
5126 int show_ctrl = 0;
5127
5128 if (input)
5129 show_ctrl = !!os_strstr(input, "ctrl");
5130
5131 pos = reply;
5132 end = reply + reply_size;
5133
5134 for (i = 0; i < interfaces->count; i++) {
5135 iface = interfaces->iface[i];
5136
5137 for (j = 0; j < iface->num_bss; j++) {
5138 struct hostapd_bss_config *conf;
5139
5140 conf = iface->conf->bss[j];
5141 if (show_ctrl)
5142 res = os_snprintf(pos, end - pos,
5143 "%s ctrl_iface=%s\n",
5144 conf->iface,
5145 conf->ctrl_interface ?
5146 conf->ctrl_interface : "N/A");
5147 else
5148 res = os_snprintf(pos, end - pos, "%s\n",
5149 conf->iface);
5150 if (os_snprintf_error(end - pos, res)) {
5151 *pos = '\0';
5152 return pos - reply;
5153 }
5154 pos += res;
5155 }
5156 }
5157
5158 return pos - reply;
5159}
5160
5161
5162static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005163hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
5164 char *cmd)
5165{
5166 char *p_start = cmd, *p_end;
5167 struct hostapd_data *src_hapd, *dst_hapd;
5168
5169 /* cmd: "<src ifname> <dst ifname> <variable name> */
5170
5171 p_end = os_strchr(p_start, ' ');
5172 if (!p_end) {
5173 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
5174 cmd);
5175 return -1;
5176 }
5177
5178 *p_end = '\0';
5179 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5180 if (!src_hapd) {
5181 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
5182 p_start);
5183 return -1;
5184 }
5185
5186 p_start = p_end + 1;
5187 p_end = os_strchr(p_start, ' ');
5188 if (!p_end) {
5189 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
5190 cmd);
5191 return -1;
5192 }
5193
5194 *p_end = '\0';
5195 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5196 if (!dst_hapd) {
5197 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
5198 p_start);
5199 return -1;
5200 }
5201
5202 p_start = p_end + 1;
5203 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
5204}
5205
5206
5207static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
5208 const char *ifname,
5209 char *buf, char *reply,
5210 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005211 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005212 socklen_t fromlen)
5213{
5214 struct hostapd_data *hapd;
5215
5216 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
5217 if (hapd == NULL) {
5218 int res;
5219
5220 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
5221 if (os_snprintf_error(reply_size, res))
5222 return -1;
5223 return res;
5224 }
5225
5226 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
5227 from, fromlen);
5228}
5229
5230
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005231static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
5232 void *sock_ctx)
5233{
Hai Shalomfdcde762020-04-02 11:19:20 -07005234 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005235 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005236 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005237 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005238 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005239 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005240 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005241 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005242#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07005243 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005244#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005245
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005246 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005247 (struct sockaddr *) &from, &fromlen);
5248 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005249 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
5250 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005251 return;
5252 }
5253 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005254 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005255
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005256 reply = os_malloc(reply_size);
5257 if (reply == NULL) {
5258 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
5259 fromlen) < 0) {
5260 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5261 strerror(errno));
5262 }
5263 return;
5264 }
5265
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005266 os_memcpy(reply, "OK\n", 3);
5267 reply_len = 3;
5268
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005269#ifdef CONFIG_CTRL_IFACE_UDP
5270 if (os_strcmp(buf, "GET_COOKIE") == 0) {
5271 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07005272 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
5273 interfaces->ctrl_iface_cookie,
5274 CTRL_IFACE_COOKIE_LEN);
5275 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005276 goto send_reply;
5277 }
5278
5279 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07005280 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005281 wpa_printf(MSG_DEBUG,
5282 "CTRL: No cookie in the request - drop request");
5283 os_free(reply);
5284 return;
5285 }
5286
Hai Shalomfdcde762020-04-02 11:19:20 -07005287 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
5288 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005289 wpa_printf(MSG_DEBUG,
5290 "CTRL: Invalid cookie in the request - drop request");
5291 os_free(reply);
5292 return;
5293 }
5294
Hai Shalomfdcde762020-04-02 11:19:20 -07005295 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005296 while (*buf == ' ')
5297 buf++;
5298#endif /* CONFIG_CTRL_IFACE_UDP */
5299
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005300 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
5301 char *pos = os_strchr(buf + 7, ' ');
5302
5303 if (pos) {
5304 *pos++ = '\0';
5305 reply_len = hostapd_global_ctrl_iface_ifname(
5306 interfaces, buf + 7, pos, reply, reply_size,
5307 &from, fromlen);
5308 goto send_reply;
5309 }
5310 }
5311
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005312 if (os_strcmp(buf, "PING") == 0) {
5313 os_memcpy(reply, "PONG\n", 5);
5314 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005315 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5316 if (wpa_debug_reopen_file() < 0)
5317 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005318 } else if (os_strcmp(buf, "FLUSH") == 0) {
5319 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005320 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
5321 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
5322 reply_len = -1;
5323 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
5324 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
5325 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005326 } else if (os_strcmp(buf, "ATTACH") == 0) {
5327 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005328 fromlen, NULL))
5329 reply_len = -1;
5330 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
5331 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
5332 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005333 reply_len = -1;
5334 } else if (os_strcmp(buf, "DETACH") == 0) {
5335 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
5336 fromlen))
5337 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005338#ifdef CONFIG_MODULE_TESTS
5339 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005340 if (hapd_module_tests() < 0)
5341 reply_len = -1;
5342#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005343#ifdef CONFIG_FST
5344 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
5345 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
5346 reply_len = os_snprintf(reply, reply_size, "OK\n");
5347 else
5348 reply_len = -1;
5349 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
5350 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
5351 reply_len = os_snprintf(reply, reply_size, "OK\n");
5352 else
5353 reply_len = -1;
5354 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
5355 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
5356#endif /* CONFIG_FST */
5357 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
5358 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
5359 buf + 12))
5360 reply_len = os_snprintf(reply, reply_size, "OK\n");
5361 else
5362 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005363 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
5364 reply_len = hostapd_global_ctrl_iface_interfaces(
Sunil Ravi88611412024-06-28 17:34:56 +00005365 interfaces, buf + 10, reply, sizeof(buffer));
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005366 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5367 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005368 } else {
5369 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
5370 "ignored");
5371 reply_len = -1;
5372 }
5373
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005374send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005375 if (reply_len < 0) {
5376 os_memcpy(reply, "FAIL\n", 5);
5377 reply_len = 5;
5378 }
5379
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005380 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
5381 fromlen) < 0) {
5382 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5383 strerror(errno));
5384 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005385 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005386}
5387
5388
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005389#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005390static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
5391{
5392 char *buf;
5393 size_t len;
5394
5395 if (interface->global_iface_path == NULL)
5396 return NULL;
5397
5398 len = os_strlen(interface->global_iface_path) +
5399 os_strlen(interface->global_iface_name) + 2;
5400 buf = os_malloc(len);
5401 if (buf == NULL)
5402 return NULL;
5403
5404 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
5405 interface->global_iface_name);
5406 buf[len - 1] = '\0';
5407 return buf;
5408}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005409#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005410
5411
5412int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
5413{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005414#ifdef CONFIG_CTRL_IFACE_UDP
5415 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
5416 char p[32] = { 0 };
5417 char *pos;
5418 struct addrinfo hints = { 0 }, *res, *saveres;
5419 int n;
5420
5421 if (interface->global_ctrl_sock > -1) {
5422 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
5423 return 0;
5424 }
5425
5426 if (interface->global_iface_path == NULL)
5427 return 0;
5428
5429 pos = os_strstr(interface->global_iface_path, "udp:");
5430 if (pos) {
5431 pos += 4;
5432 port = atoi(pos);
5433 if (port <= 0) {
5434 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
5435 goto fail;
5436 }
5437 }
5438
Hai Shalomfdcde762020-04-02 11:19:20 -07005439 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005440
5441#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
5442 hints.ai_flags = AI_PASSIVE;
5443#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
5444
5445#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
5446 hints.ai_family = AF_INET6;
5447#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5448 hints.ai_family = AF_INET;
5449#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5450 hints.ai_socktype = SOCK_DGRAM;
5451
5452try_again:
5453 os_snprintf(p, sizeof(p), "%d", port);
5454 n = getaddrinfo(NULL, p, &hints, &res);
5455 if (n) {
5456 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
5457 goto fail;
5458 }
5459
5460 saveres = res;
5461 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
5462 res->ai_protocol);
5463 if (interface->global_ctrl_sock < 0) {
5464 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
5465 goto fail;
5466 }
5467
5468 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
5469 0) {
5470 port++;
5471 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
5472 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
5473 goto try_again;
5474 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
5475 goto fail;
5476 }
5477
5478 freeaddrinfo(saveres);
5479
5480 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
5481
5482 if (eloop_register_read_sock(interface->global_ctrl_sock,
5483 hostapd_global_ctrl_iface_receive,
5484 interface, NULL) < 0) {
5485 hostapd_global_ctrl_iface_deinit(interface);
5486 return -1;
5487 }
5488
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005489 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5490
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005491 return 0;
5492
5493fail:
5494 if (interface->global_ctrl_sock >= 0)
5495 close(interface->global_ctrl_sock);
5496 return -1;
5497#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005498 struct sockaddr_un addr;
5499 int s = -1;
5500 char *fname = NULL;
5501
5502 if (interface->global_iface_path == NULL) {
5503 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
5504 return 0;
5505 }
5506
5507 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
5508 if (errno == EEXIST) {
5509 wpa_printf(MSG_DEBUG, "Using existing control "
5510 "interface directory.");
5511 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005512 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
5513 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005514 goto fail;
5515 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005516 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005517 lchown(interface->global_iface_path, -1,
5518 interface->ctrl_iface_group) < 0) {
5519 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005520 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005521 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005522 }
5523
5524 if (os_strlen(interface->global_iface_path) + 1 +
5525 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
5526 goto fail;
5527
5528 s = socket(PF_UNIX, SOCK_DGRAM, 0);
5529 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005530 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005531 goto fail;
5532 }
5533
5534 os_memset(&addr, 0, sizeof(addr));
5535#ifdef __FreeBSD__
5536 addr.sun_len = sizeof(addr);
5537#endif /* __FreeBSD__ */
5538 addr.sun_family = AF_UNIX;
5539 fname = hostapd_global_ctrl_iface_path(interface);
5540 if (fname == NULL)
5541 goto fail;
5542 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
5543 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5544 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5545 strerror(errno));
5546 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5547 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
5548 " allow connections - assuming it was left"
5549 "over from forced program termination");
5550 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005551 wpa_printf(MSG_ERROR,
5552 "Could not unlink existing ctrl_iface socket '%s': %s",
5553 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005554 goto fail;
5555 }
5556 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5557 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005558 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
5559 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005560 goto fail;
5561 }
5562 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
5563 "ctrl_iface socket '%s'", fname);
5564 } else {
5565 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
5566 "be in use - cannot override it");
5567 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
5568 "not used anymore", fname);
5569 os_free(fname);
5570 fname = NULL;
5571 goto fail;
5572 }
5573 }
5574
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005575 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005576 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
5577 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005578 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005579 goto fail;
5580 }
5581
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005582 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005583 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5584 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005585 goto fail;
5586 }
5587 os_free(fname);
5588
5589 interface->global_ctrl_sock = s;
5590 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
5591 interface, NULL);
5592
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005593 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5594
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005595 return 0;
5596
5597fail:
5598 if (s >= 0)
5599 close(s);
5600 if (fname) {
5601 unlink(fname);
5602 os_free(fname);
5603 }
5604 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005605#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005606}
5607
5608
5609void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
5610{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005611#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005612 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005613#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005614 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005615
5616 if (interfaces->global_ctrl_sock > -1) {
5617 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
5618 close(interfaces->global_ctrl_sock);
5619 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005620#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005621 fname = hostapd_global_ctrl_iface_path(interfaces);
5622 if (fname) {
5623 unlink(fname);
5624 os_free(fname);
5625 }
5626
5627 if (interfaces->global_iface_path &&
5628 rmdir(interfaces->global_iface_path) < 0) {
5629 if (errno == ENOTEMPTY) {
5630 wpa_printf(MSG_DEBUG, "Control interface "
5631 "directory not empty - leaving it "
5632 "behind");
5633 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005634 wpa_printf(MSG_ERROR,
5635 "rmdir[ctrl_interface=%s]: %s",
5636 interfaces->global_iface_path,
5637 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005638 }
5639 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005640#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005641 }
5642
5643 os_free(interfaces->global_iface_path);
5644 interfaces->global_iface_path = NULL;
5645
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005646 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
5647 struct wpa_ctrl_dst, list)
5648 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005649}
5650
5651
Roshan Pius3a1667e2018-07-03 15:17:14 -07005652static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
5653 const char *buf)
5654{
5655 /* Enable Probe Request events based on explicit request.
5656 * Other events are enabled by default.
5657 */
5658 if (str_starts(buf, RX_PROBE_REQUEST))
5659 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
5660 return 1;
5661}
5662
5663
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005664static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
5665 const char *ifname, int level,
5666 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667{
5668 struct wpa_ctrl_dst *dst, *next;
5669 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005670 int idx, res;
5671 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672 char levelstr[10];
5673
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005674 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005675 return;
5676
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005677 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
5678 if (os_snprintf_error(sizeof(levelstr), res))
5679 return;
5680 idx = 0;
5681 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005682 io[idx].iov_base = "IFNAME=";
5683 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005684 idx++;
5685 io[idx].iov_base = (char *) ifname;
5686 io[idx].iov_len = os_strlen(ifname);
5687 idx++;
5688 io[idx].iov_base = " ";
5689 io[idx].iov_len = 1;
5690 idx++;
5691 }
5692 io[idx].iov_base = levelstr;
5693 io[idx].iov_len = os_strlen(levelstr);
5694 idx++;
5695 io[idx].iov_base = (char *) buf;
5696 io[idx].iov_len = len;
5697 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005698 os_memset(&msg, 0, sizeof(msg));
5699 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005700 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005701
5702 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005703 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005704 if ((level >= dst->debug_level) &&
5705 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005706 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
5707 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005708 msg.msg_name = &dst->addr;
5709 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005710 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 int _errno = errno;
5712 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
5713 "%d - %s",
5714 idx, errno, strerror(errno));
5715 dst->errors++;
5716 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005717 ctrl_iface_detach(ctrl_dst,
5718 &dst->addr,
5719 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720 }
5721 } else
5722 dst->errors = 0;
5723 }
5724 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725 }
5726}
5727
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005728
5729static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
5730 enum wpa_msg_type type,
5731 const char *buf, size_t len)
5732{
5733 if (type != WPA_MSG_NO_GLOBAL) {
5734 hostapd_ctrl_iface_send_internal(
5735 hapd->iface->interfaces->global_ctrl_sock,
5736 &hapd->iface->interfaces->global_ctrl_dst,
5737 type != WPA_MSG_PER_INTERFACE ?
5738 NULL : hapd->conf->iface,
5739 level, buf, len);
5740 }
5741
5742 if (type != WPA_MSG_ONLY_GLOBAL) {
5743 hostapd_ctrl_iface_send_internal(
5744 hapd->ctrl_sock, &hapd->ctrl_dst,
5745 NULL, level, buf, len);
5746 }
5747}
5748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749#endif /* CONFIG_NATIVE_WINDOWS */