blob: dd445d83258f05fa8f2258f5db14421af3f021cf [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);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001312 } else if (os_strcasecmp(cmd, "ssid") == 0) {
1313 hostapd_neighbor_sync_own_report(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001314 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1315 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1316 hapd->parameter_set_count++;
1317 if (ieee802_11_update_beacons(hapd->iface))
1318 wpa_printf(MSG_DEBUG,
1319 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001320 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1321 os_strcmp(cmd, "sae_password") == 0 ||
1322 os_strcmp(cmd, "sae_pwe") == 0) {
1323 if (hapd->started)
1324 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001325 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1326 wpa_auth_set_transition_disable(hapd->wpa_auth,
1327 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001328 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001329
1330#ifdef CONFIG_TESTING_OPTIONS
1331 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1332 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1333 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001334 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1335 wpa_auth_set_ocv_override_freq(
1336 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1337 atoi(value));
1338 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1339 wpa_auth_set_ocv_override_freq(
1340 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1341 atoi(value));
1342 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1343 wpa_auth_set_ocv_override_freq(
1344 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1345 atoi(value));
1346 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1347 wpa_auth_set_ocv_override_freq(
1348 hapd->wpa_auth,
1349 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001350 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1351 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1352 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1353 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001354#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 }
1356
1357 return ret;
1358}
1359
1360
1361static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1362 char *buf, size_t buflen)
1363{
1364 int res;
1365
1366 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1367
1368 if (os_strcmp(cmd, "version") == 0) {
1369 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001370 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371 return -1;
1372 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001373 } else if (os_strcmp(cmd, "tls_library") == 0) {
1374 res = tls_get_library_version(buf, buflen);
1375 if (os_snprintf_error(buflen, res))
1376 return -1;
1377 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 }
1379
1380 return -1;
1381}
1382
1383
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001384static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1385{
1386 if (hostapd_enable_iface(iface) < 0) {
1387 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1388 return -1;
1389 }
1390 return 0;
1391}
1392
1393
1394static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1395{
1396 if (hostapd_reload_iface(iface) < 0) {
1397 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1398 return -1;
1399 }
1400 return 0;
1401}
1402
1403
Sunil Ravi77d572f2023-01-17 23:58:31 +00001404static int hostapd_ctrl_iface_reload_bss(struct hostapd_data *bss)
1405{
1406 if (hostapd_reload_bss_only(bss) < 0) {
1407 wpa_printf(MSG_ERROR, "Reloading of BSS failed");
1408 return -1;
1409 }
1410 return 0;
1411}
1412
1413
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001414static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1415{
1416 if (hostapd_disable_iface(iface) < 0) {
1417 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1418 return -1;
1419 }
1420 return 0;
1421}
1422
1423
Hai Shalom74f70d42019-02-11 14:42:39 -08001424static int
1425hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1426 struct sta_info *sta, void *ctx)
1427{
1428 struct hostapd_wpa_psk *psk;
1429 const u8 *pmk;
1430 int pmk_len;
1431 int pmk_match;
1432 int sta_match;
1433 int bss_match;
1434 int reason;
1435
1436 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1437
1438 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1439 pmk_match = PMK_LEN == pmk_len &&
1440 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1441 sta_match = psk->group == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001442 ether_addr_equal(sta->addr, psk->addr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001443 bss_match = psk->group == 1;
1444
1445 if (pmk_match && (sta_match || bss_match))
1446 return 0;
1447 }
1448
1449 wpa_printf(MSG_INFO, "STA " MACSTR
1450 " PSK/passphrase no longer valid - disconnect",
1451 MAC2STR(sta->addr));
1452 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1453 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1454 ap_sta_deauthenticate(hapd, sta, reason);
1455
1456 return 0;
1457}
1458
1459
1460static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1461{
1462 struct hostapd_bss_config *conf = hapd->conf;
1463 int err;
1464
1465 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1466
1467 err = hostapd_setup_wpa_psk(conf);
1468 if (err < 0) {
1469 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1470 err);
1471 return -1;
1472 }
1473
1474 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1475 NULL);
1476
1477 return 0;
1478}
1479
1480
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001481#ifdef CONFIG_IEEE80211R_AP
1482
1483static int hostapd_ctrl_iface_get_rxkhs(struct hostapd_data *hapd,
1484 char *buf, size_t buflen)
1485{
1486 int ret, start_pos;
1487 char *pos, *end;
1488 struct ft_remote_r0kh *r0kh;
1489 struct ft_remote_r1kh *r1kh;
1490 struct hostapd_bss_config *conf = hapd->conf;
1491
1492 pos = buf;
1493 end = buf + buflen;
1494
1495 for (r0kh = conf->r0kh_list; r0kh; r0kh=r0kh->next) {
1496 start_pos = pos - buf;
1497 ret = os_snprintf(pos, end - pos, "r0kh=" MACSTR " ",
1498 MAC2STR(r0kh->addr));
1499 if (os_snprintf_error(end - pos, ret))
1500 return start_pos;
1501 pos += ret;
1502 if (r0kh->id_len + 1 >= (size_t) (end - pos))
1503 return start_pos;
1504 os_memcpy(pos, r0kh->id, r0kh->id_len);
1505 pos += r0kh->id_len;
1506 *pos++ = ' ';
1507 pos += wpa_snprintf_hex(pos, end - pos, r0kh->key,
1508 sizeof(r0kh->key));
1509 ret = os_snprintf(pos, end - pos, "\n");
1510 if (os_snprintf_error(end - pos, ret))
1511 return start_pos;
1512 pos += ret;
1513 }
1514
1515 for (r1kh = conf->r1kh_list; r1kh; r1kh=r1kh->next) {
1516 start_pos = pos - buf;
1517 ret = os_snprintf(pos, end - pos, "r1kh=" MACSTR " " MACSTR " ",
1518 MAC2STR(r1kh->addr), MAC2STR(r1kh->id));
1519 if (os_snprintf_error(end - pos, ret))
1520 return start_pos;
1521 pos += ret;
1522 pos += wpa_snprintf_hex(pos, end - pos, r1kh->key,
1523 sizeof(r1kh->key));
1524 ret = os_snprintf(pos, end - pos, "\n");
1525 if (os_snprintf_error(end - pos, ret))
1526 return start_pos;
1527 pos += ret;
1528 }
1529
1530 return pos - buf;
1531}
1532
1533
1534static int hostapd_ctrl_iface_reload_rxkhs(struct hostapd_data *hapd)
1535{
1536 struct hostapd_bss_config *conf = hapd->conf;
1537 int err;
1538
1539 hostapd_config_clear_rxkhs(conf);
1540
1541 err = hostapd_config_read_rxkh_file(conf, conf->rxkh_file);
1542 if (err < 0) {
1543 wpa_printf(MSG_ERROR, "Reloading RxKHs failed: %d",
1544 err);
1545 return -1;
1546 }
1547
1548 return 0;
1549}
1550
1551#endif /* CONFIG_IEEE80211R_AP */
1552
1553
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001554#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001555
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001556static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1557{
1558 union wpa_event_data data;
1559 char *pos, *param;
1560 enum wpa_event_type event;
1561
1562 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1563
1564 os_memset(&data, 0, sizeof(data));
1565
1566 param = os_strchr(cmd, ' ');
1567 if (param == NULL)
1568 return -1;
1569 *param++ = '\0';
1570
1571 if (os_strcmp(cmd, "DETECTED") == 0)
1572 event = EVENT_DFS_RADAR_DETECTED;
1573 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1574 event = EVENT_DFS_CAC_FINISHED;
1575 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1576 event = EVENT_DFS_CAC_ABORTED;
1577 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1578 event = EVENT_DFS_NOP_FINISHED;
1579 else {
1580 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1581 cmd);
1582 return -1;
1583 }
1584
1585 pos = os_strstr(param, "freq=");
1586 if (pos)
1587 data.dfs_event.freq = atoi(pos + 5);
1588
1589 pos = os_strstr(param, "ht_enabled=1");
1590 if (pos)
1591 data.dfs_event.ht_enabled = 1;
1592
1593 pos = os_strstr(param, "chan_offset=");
1594 if (pos)
1595 data.dfs_event.chan_offset = atoi(pos + 12);
1596
1597 pos = os_strstr(param, "chan_width=");
1598 if (pos)
1599 data.dfs_event.chan_width = atoi(pos + 11);
1600
1601 pos = os_strstr(param, "cf1=");
1602 if (pos)
1603 data.dfs_event.cf1 = atoi(pos + 4);
1604
1605 pos = os_strstr(param, "cf2=");
1606 if (pos)
1607 data.dfs_event.cf2 = atoi(pos + 4);
1608
1609 wpa_supplicant_event(hapd, event, &data);
1610
1611 return 0;
1612}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001613
1614
1615static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1616{
1617 size_t len;
1618 u8 *buf;
1619 int res;
1620
1621 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1622
1623 len = os_strlen(cmd);
1624 if (len & 1)
1625 return -1;
1626 len /= 2;
1627
1628 buf = os_malloc(len);
1629 if (buf == NULL)
1630 return -1;
1631
1632 if (hexstr2bin(cmd, buf, len) < 0) {
1633 os_free(buf);
1634 return -1;
1635 }
1636
Hai Shalomfdcde762020-04-02 11:19:20 -07001637 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001638 os_free(buf);
1639 return res;
1640}
1641
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001642
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001643static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1644 char *cmd)
1645{
1646 char *pos, *param;
1647 size_t len;
1648 u8 *buf;
1649 int stype = 0, ok = 0;
1650 union wpa_event_data event;
1651
1652 if (!hapd->ext_mgmt_frame_handling)
1653 return -1;
1654
1655 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1656
1657 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1658
1659 pos = cmd;
1660 param = os_strstr(pos, "stype=");
1661 if (param) {
1662 param += 6;
1663 stype = atoi(param);
1664 }
1665
1666 param = os_strstr(pos, " ok=");
1667 if (param) {
1668 param += 4;
1669 ok = atoi(param);
1670 }
1671
1672 param = os_strstr(pos, " buf=");
1673 if (!param)
1674 return -1;
1675 param += 5;
1676
1677 len = os_strlen(param);
1678 if (len & 1)
1679 return -1;
1680 len /= 2;
1681
1682 buf = os_malloc(len);
1683 if (!buf || hexstr2bin(param, buf, len) < 0) {
1684 os_free(buf);
1685 return -1;
1686 }
1687
1688 os_memset(&event, 0, sizeof(event));
1689 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1690 event.tx_status.data = buf;
1691 event.tx_status.data_len = len;
1692 event.tx_status.stype = stype;
1693 event.tx_status.ack = ok;
1694 hapd->ext_mgmt_frame_handling = 0;
1695 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1696 hapd->ext_mgmt_frame_handling = 1;
1697
1698 os_free(buf);
1699
1700 return 0;
1701}
1702
1703
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001704static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1705 char *cmd)
1706{
1707 char *pos, *param;
1708 size_t len;
1709 u8 *buf;
1710 int freq = 0, datarate = 0, ssi_signal = 0;
1711 union wpa_event_data event;
1712
1713 if (!hapd->ext_mgmt_frame_handling)
1714 return -1;
1715
1716 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1717
1718 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1719
1720 pos = cmd;
1721 param = os_strstr(pos, "freq=");
1722 if (param) {
1723 param += 5;
1724 freq = atoi(param);
1725 }
1726
1727 param = os_strstr(pos, " datarate=");
1728 if (param) {
1729 param += 10;
1730 datarate = atoi(param);
1731 }
1732
1733 param = os_strstr(pos, " ssi_signal=");
1734 if (param) {
1735 param += 12;
1736 ssi_signal = atoi(param);
1737 }
1738
1739 param = os_strstr(pos, " frame=");
1740 if (param == NULL)
1741 return -1;
1742 param += 7;
1743
1744 len = os_strlen(param);
1745 if (len & 1)
1746 return -1;
1747 len /= 2;
1748
1749 buf = os_malloc(len);
1750 if (buf == NULL)
1751 return -1;
1752
1753 if (hexstr2bin(param, buf, len) < 0) {
1754 os_free(buf);
1755 return -1;
1756 }
1757
1758 os_memset(&event, 0, sizeof(event));
1759 event.rx_mgmt.freq = freq;
1760 event.rx_mgmt.frame = buf;
1761 event.rx_mgmt.frame_len = len;
1762 event.rx_mgmt.ssi_signal = ssi_signal;
1763 event.rx_mgmt.datarate = datarate;
1764 hapd->ext_mgmt_frame_handling = 0;
1765 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1766 hapd->ext_mgmt_frame_handling = 1;
1767
1768 os_free(buf);
1769
1770 return 0;
1771}
1772
1773
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001774static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1775{
1776 char *pos;
1777 u8 src[ETH_ALEN], *buf;
1778 int used;
1779 size_t len;
1780
1781 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1782
1783 pos = cmd;
1784 used = hwaddr_aton2(pos, src);
1785 if (used < 0)
1786 return -1;
1787 pos += used;
1788 while (*pos == ' ')
1789 pos++;
1790
1791 len = os_strlen(pos);
1792 if (len & 1)
1793 return -1;
1794 len /= 2;
1795
1796 buf = os_malloc(len);
1797 if (buf == NULL)
1798 return -1;
1799
1800 if (hexstr2bin(pos, buf, len) < 0) {
1801 os_free(buf);
1802 return -1;
1803 }
1804
Sunil8cd6f4d2022-06-28 18:40:46 +00001805 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001806 os_free(buf);
1807
1808 return 0;
1809}
1810
1811
Hai Shaloma20dcd72022-02-04 13:43:00 -08001812static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1813{
1814 char *pos, *pos2;
1815 u8 dst[ETH_ALEN], *buf;
1816 int used, ret;
1817 size_t len;
1818 unsigned int prev;
1819 int encrypt = 0;
1820
1821 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1822
1823 pos = cmd;
1824 used = hwaddr_aton2(pos, dst);
1825 if (used < 0)
1826 return -1;
1827 pos += used;
1828 while (*pos == ' ')
1829 pos++;
1830
1831 pos2 = os_strchr(pos, ' ');
1832 if (pos2) {
1833 len = pos2 - pos;
1834 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1835 } else {
1836 len = os_strlen(pos);
1837 }
1838 if (len & 1)
1839 return -1;
1840 len /= 2;
1841
1842 buf = os_malloc(len);
1843 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1844 os_free(buf);
1845 return -1;
1846 }
1847
1848 prev = hapd->ext_eapol_frame_io;
1849 hapd->ext_eapol_frame_io = 0;
1850 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1851 hapd->ext_eapol_frame_io = prev;
1852 os_free(buf);
1853
1854 return ret;
1855}
1856
1857
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001858static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1859{
1860 size_t i;
1861 u32 sum = 0;
1862 const u16 *pos = buf;
1863
1864 for (i = 0; i < len / 2; i++)
1865 sum += *pos++;
1866
1867 while (sum >> 16)
1868 sum = (sum & 0xffff) + (sum >> 16);
1869
1870 return sum ^ 0xffff;
1871}
1872
1873
1874#define HWSIM_PACKETLEN 1500
1875#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1876
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001877static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1878 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001879{
1880 struct hostapd_data *hapd = ctx;
1881 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001882 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001883 const u8 *pos;
1884 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001885 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001886
Hai Shalom81f62d82019-07-22 12:10:00 -07001887 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1888 wpa_printf(MSG_DEBUG,
1889 "test data: RX - ignore unexpected length %d",
1890 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001891 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001892 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893
1894 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001895 os_memcpy(&ip, eth + 1, sizeof(ip));
1896 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001897
Hai Shalomfdcde762020-04-02 11:19:20 -07001898 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1899 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001900 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07001901 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001902 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001903 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001904
Hai Shalomfdcde762020-04-02 11:19:20 -07001905 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001906 if (*pos != (u8) i) {
1907 wpa_printf(MSG_DEBUG,
1908 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001909 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001910 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001911 pos++;
1912 }
1913
Hai Shalom81f62d82019-07-22 12:10:00 -07001914 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001915 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1916 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001917 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1918 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001919}
1920
1921
1922static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1923 char *cmd)
1924{
1925 int enabled = atoi(cmd);
1926 char *pos;
1927 const char *ifname;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001928 const u8 *addr = hapd->own_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001929
1930 if (!enabled) {
1931 if (hapd->l2_test) {
1932 l2_packet_deinit(hapd->l2_test);
1933 hapd->l2_test = NULL;
1934 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1935 "test data: Disabled");
1936 }
1937 return 0;
1938 }
1939
1940 if (hapd->l2_test)
1941 return 0;
1942
1943 pos = os_strstr(cmd, " ifname=");
1944 if (pos)
1945 ifname = pos + 8;
1946 else
1947 ifname = hapd->conf->iface;
1948
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001949#ifdef CONFIG_IEEE80211BE
1950 if (hapd->conf->mld_ap)
Sunil Ravi99c035e2024-07-12 01:42:03 +00001951 addr = hapd->mld->mld_addr;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001952#endif /* CONFIG_IEEE80211BE */
1953 hapd->l2_test = l2_packet_init(ifname, addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001954 ETHERTYPE_IP, hostapd_data_test_rx,
1955 hapd, 1);
1956 if (hapd->l2_test == NULL)
1957 return -1;
1958
1959 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1960
1961 return 0;
1962}
1963
1964
1965static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1966{
1967 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001968 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001969 int used;
1970 long int val;
1971 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001972 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001973 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001974 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001975 u8 *dpos;
1976 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001977 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001978
1979 if (hapd->l2_test == NULL)
1980 return -1;
1981
Hai Shalom81f62d82019-07-22 12:10:00 -07001982 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001983
1984 pos = cmd;
1985 used = hwaddr_aton2(pos, dst);
1986 if (used < 0)
1987 return -1;
1988 pos += used;
1989 while (*pos == ' ')
1990 pos++;
1991 used = hwaddr_aton2(pos, src);
1992 if (used < 0)
1993 return -1;
1994 pos += used;
1995
Hai Shalom81f62d82019-07-22 12:10:00 -07001996 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001997 if (val < 0 || val > 0xff)
1998 return -1;
1999 tos = val;
2000
Hai Shalom81f62d82019-07-22 12:10:00 -07002001 pos = os_strstr(pos2, " len=");
2002 if (pos) {
2003 i = atoi(pos + 5);
2004 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
2005 return -1;
2006 send_len = i;
2007 }
2008
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002009 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002010 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
2011 os_memcpy(eth->ether_shost, src, ETH_ALEN);
2012 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07002013 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002014 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07002015 ip->ip_hl = 5;
2016 ip->ip_v = 4;
2017 ip->ip_ttl = 64;
2018 ip->ip_tos = tos;
2019 ip->ip_len = htons(send_len);
2020 ip->ip_p = 1;
2021 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
2022 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
2023 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002024 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07002025 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002026 *dpos++ = i;
2027
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002028 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07002029 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002030 return -1;
2031
2032 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2033 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2034
2035 return 0;
2036}
2037
2038
2039static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2040 char *cmd)
2041{
2042 u8 *buf;
2043 struct ether_header *eth;
2044 struct l2_packet_data *l2 = NULL;
2045 size_t len;
2046 u16 ethertype;
2047 int res = -1;
2048 const char *ifname = hapd->conf->iface;
2049
2050 if (os_strncmp(cmd, "ifname=", 7) == 0) {
2051 cmd += 7;
2052 ifname = cmd;
2053 cmd = os_strchr(cmd, ' ');
2054 if (cmd == NULL)
2055 return -1;
2056 *cmd++ = '\0';
2057 }
2058
2059 len = os_strlen(cmd);
2060 if (len & 1 || len < ETH_HLEN * 2)
2061 return -1;
2062 len /= 2;
2063
2064 buf = os_malloc(len);
2065 if (buf == NULL)
2066 return -1;
2067
2068 if (hexstr2bin(cmd, buf, len) < 0)
2069 goto done;
2070
2071 eth = (struct ether_header *) buf;
2072 ethertype = ntohs(eth->ether_type);
2073
2074 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2075 hostapd_data_test_rx, hapd, 1);
2076 if (l2 == NULL)
2077 goto done;
2078
2079 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2080 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2081done:
2082 if (l2)
2083 l2_packet_deinit(l2);
2084 os_free(buf);
2085
2086 return res < 0 ? -1 : 0;
2087}
2088
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002089
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002090static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2091{
2092 struct sta_info *sta;
2093 u8 addr[ETH_ALEN];
2094 u8 zero[WPA_TK_MAX_LEN];
2095
2096 os_memset(zero, 0, sizeof(zero));
2097
2098 if (hwaddr_aton(cmd, addr))
2099 return -1;
2100
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002101 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2102 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2103 return -1;
2104
2105 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2106
2107 /* First, use a zero key to avoid any possible duplicate key
2108 * avoidance in the driver. */
2109 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2110 hapd->last_bigtk_alg,
2111 broadcast_ether_addr,
2112 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2113 zero, hapd->last_bigtk_len,
2114 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2115 return -1;
2116
2117 /* Set the previously configured key to reset its TSC */
2118 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2119 hapd->last_bigtk_alg,
2120 broadcast_ether_addr,
2121 hapd->last_bigtk_key_idx, 0, 1, NULL,
2122 0, hapd->last_bigtk,
2123 hapd->last_bigtk_len,
2124 KEY_FLAG_GROUP_TX_DEFAULT);
2125 }
2126
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002127 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2128 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2129 return -1;
2130
2131 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2132
2133 /* First, use a zero key to avoid any possible duplicate key
2134 * avoidance in the driver. */
2135 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2136 hapd->last_igtk_alg,
2137 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002138 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2139 zero, hapd->last_igtk_len,
2140 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002141 return -1;
2142
2143 /* Set the previously configured key to reset its TSC */
2144 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2145 hapd->last_igtk_alg,
2146 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002147 hapd->last_igtk_key_idx, 0, 1, NULL,
2148 0, hapd->last_igtk,
2149 hapd->last_igtk_len,
2150 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002151 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002152
2153 if (is_broadcast_ether_addr(addr)) {
2154 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2155 return -1;
2156
2157 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2158
2159 /* First, use a zero key to avoid any possible duplicate key
2160 * avoidance in the driver. */
2161 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2162 hapd->last_gtk_alg,
2163 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002164 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2165 zero, hapd->last_gtk_len,
2166 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002167 return -1;
2168
2169 /* Set the previously configured key to reset its TSC */
2170 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2171 hapd->last_gtk_alg,
2172 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002173 hapd->last_gtk_key_idx, 0, 1, NULL,
2174 0, hapd->last_gtk,
2175 hapd->last_gtk_len,
2176 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002177 }
2178
2179 sta = ap_get_sta(hapd, addr);
2180 if (!sta)
2181 return -1;
2182
2183 if (sta->last_tk_alg == WPA_ALG_NONE)
2184 return -1;
2185
2186 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2187 MAC2STR(sta->addr));
2188
2189 /* First, use a zero key to avoid any possible duplicate key avoidance
2190 * in the driver. */
2191 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002192 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2193 zero, sta->last_tk_len,
2194 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002195 return -1;
2196
2197 /* Set the previously configured key to reset its TSC/RSC */
2198 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002199 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2200 0, sta->last_tk, sta->last_tk_len,
2201 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002202}
2203
2204
2205static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2206{
2207 u8 addr[ETH_ALEN];
2208 const char *pos = cmd;
2209 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002210 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002211 int idx, set_tx;
2212 u8 seq[6], key[WPA_TK_MAX_LEN];
2213 size_t key_len;
2214
Hai Shalomfdcde762020-04-02 11:19:20 -07002215 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002216
2217 alg = atoi(pos);
2218 pos = os_strchr(pos, ' ');
2219 if (!pos)
2220 return -1;
2221 pos++;
2222 if (hwaddr_aton(pos, addr))
2223 return -1;
2224 pos += 17;
2225 if (*pos != ' ')
2226 return -1;
2227 pos++;
2228 idx = atoi(pos);
2229 pos = os_strchr(pos, ' ');
2230 if (!pos)
2231 return -1;
2232 pos++;
2233 set_tx = atoi(pos);
2234 pos = os_strchr(pos, ' ');
2235 if (!pos)
2236 return -1;
2237 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002238 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002239 return -1;
2240 pos += 2 * 6;
2241 if (*pos != ' ')
2242 return -1;
2243 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002244 if (!os_strchr(pos, ' '))
2245 return -1;
2246 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002247 if (hexstr2bin(pos, key, key_len) < 0)
2248 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002249 pos += 2 * key_len;
2250 if (*pos != ' ')
2251 return -1;
2252
2253 pos++;
2254 key_flag = atoi(pos);
2255 pos = os_strchr(pos, ' ');
2256 if (pos)
2257 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002258
2259 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002260 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2261 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002262}
2263
2264
2265static void restore_tk(void *ctx1, void *ctx2)
2266{
2267 struct hostapd_data *hapd = ctx1;
2268 struct sta_info *sta = ctx2;
2269
2270 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2271 MAC2STR(sta->addr));
2272 /* This does not really restore the TSC properly, so this will result
2273 * in replay protection issues for now since there is no clean way of
2274 * preventing encryption of a single EAPOL frame. */
2275 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002276 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2277 sta->last_tk, sta->last_tk_len,
2278 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002279}
2280
2281
2282static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2283{
2284 struct sta_info *sta;
2285 u8 addr[ETH_ALEN];
2286 int plain = os_strstr(cmd, "plaintext") != NULL;
2287
2288 if (hwaddr_aton(cmd, addr))
2289 return -1;
2290
2291 sta = ap_get_sta(hapd, addr);
2292 if (!sta || !sta->wpa_sm)
2293 return -1;
2294
2295 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2296 plain = 0; /* no need for special processing */
2297 if (plain) {
2298 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2299 MAC2STR(sta->addr));
2300 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002301 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2302 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002303 }
2304
2305 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2306 return wpa_auth_resend_m1(sta->wpa_sm,
2307 os_strstr(cmd, "change-anonce") != NULL,
2308 plain ? restore_tk : NULL, hapd, sta);
2309}
2310
2311
2312static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2313{
2314 struct sta_info *sta;
2315 u8 addr[ETH_ALEN];
2316 int plain = os_strstr(cmd, "plaintext") != NULL;
2317
2318 if (hwaddr_aton(cmd, addr))
2319 return -1;
2320
2321 sta = ap_get_sta(hapd, addr);
2322 if (!sta || !sta->wpa_sm)
2323 return -1;
2324
2325 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2326 plain = 0; /* no need for special processing */
2327 if (plain) {
2328 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2329 MAC2STR(sta->addr));
2330 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002331 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2332 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002333 }
2334
2335 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2336 return wpa_auth_resend_m3(sta->wpa_sm,
2337 plain ? restore_tk : NULL, hapd, sta);
2338}
2339
2340
2341static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2342 const char *cmd)
2343{
2344 struct sta_info *sta;
2345 u8 addr[ETH_ALEN];
2346 int plain = os_strstr(cmd, "plaintext") != NULL;
2347
2348 if (hwaddr_aton(cmd, addr))
2349 return -1;
2350
2351 sta = ap_get_sta(hapd, addr);
2352 if (!sta || !sta->wpa_sm)
2353 return -1;
2354
2355 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2356 plain = 0; /* no need for special processing */
2357 if (plain) {
2358 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2359 MAC2STR(sta->addr));
2360 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002361 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2362 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002363 }
2364
2365 wpa_printf(MSG_INFO,
2366 "TESTING: Send group M1 for the same GTK and zero RSC to "
2367 MACSTR, MAC2STR(sta->addr));
2368 return wpa_auth_resend_group_m1(sta->wpa_sm,
2369 plain ? restore_tk : NULL, hapd, sta);
2370}
2371
Hai Shalomfdcde762020-04-02 11:19:20 -07002372
Hai Shaloma20dcd72022-02-04 13:43:00 -08002373static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2374{
2375 struct sta_info *sta;
2376 u8 addr[ETH_ALEN];
2377
2378 if (hwaddr_aton(cmd, addr))
2379 return -1;
2380
2381 sta = ap_get_sta(hapd, addr);
2382 if (!sta || !sta->wpa_sm)
2383 return -1;
2384
2385 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2386}
2387
2388
Hai Shalom899fcc72020-10-19 14:38:18 -07002389static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2390 char *buf, size_t buflen)
2391{
2392 struct rsn_pmksa_cache_entry *pmksa;
2393
2394 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2395 if (!pmksa)
2396 return -1;
2397
2398 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2399}
2400
2401
Hai Shalomfdcde762020-04-02 11:19:20 -07002402static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2403 char *buf, size_t buflen)
2404{
2405 struct sta_info *sta;
2406 u8 addr[ETH_ALEN];
2407 const u8 *pmk;
2408 int pmk_len;
2409
2410 if (hwaddr_aton(cmd, addr))
2411 return -1;
2412
2413 sta = ap_get_sta(hapd, addr);
2414 if (!sta || !sta->wpa_sm) {
2415 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2416 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002417 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002418 }
2419 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002420 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002421 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2422 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002423 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002424 }
2425
2426 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2427}
2428
Hai Shaloma20dcd72022-02-04 13:43:00 -08002429
2430static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2431 const char *cmd)
2432{
2433 u16 type;
2434 char *pos, *end;
2435 u8 match[10];
2436 size_t match_len;
2437 bool multicast = false;
2438
2439 type = strtol(cmd, &pos, 16);
2440 if (*pos != ' ')
2441 return -1;
2442 pos++;
2443 end = os_strchr(pos, ' ');
2444 if (end) {
2445 match_len = end - pos;
2446 multicast = os_strstr(end, "multicast") != NULL;
2447 } else {
2448 match_len = os_strlen(pos) / 2;
2449 }
2450 if (hexstr2bin(pos, match, match_len))
2451 return -1;
2452
2453 return hostapd_drv_register_frame(hapd, type, match, match_len,
2454 multicast);
2455}
2456
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002457#endif /* CONFIG_TESTING_OPTIONS */
2458
2459
Hai Shalomb755a2a2020-04-23 21:49:02 -07002460#ifdef NEED_AP_MLME
Sunil Ravic0f5d412024-09-11 22:12:49 +00002461
2462static bool
2463hostapd_ctrl_is_freq_in_cmode(struct hostapd_hw_modes *mode,
2464 struct hostapd_multi_hw_info *current_hw_info,
2465 int freq)
2466{
2467 struct hostapd_channel_data *chan;
2468 int i;
2469
2470 for (i = 0; i < mode->num_channels; i++) {
2471 chan = &mode->channels[i];
2472
2473 if (chan->flag & HOSTAPD_CHAN_DISABLED)
2474 continue;
2475
2476 if (!chan_in_current_hw_info(current_hw_info, chan))
2477 continue;
2478
2479 if (chan->freq == freq)
2480 return true;
2481 }
2482 return false;
2483}
2484
2485
Sunil Ravi036cec52023-03-29 11:35:17 -07002486static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params,
2487 u16 punct_bitmap)
Hai Shalomfdcde762020-04-02 11:19:20 -07002488{
Sunil Ravi036cec52023-03-29 11:35:17 -07002489 u32 start_freq;
2490
2491 if (is_6ghz_freq(params->freq)) {
2492 const int bw_idx[] = { 20, 40, 80, 160, 320 };
2493 int idx, bw;
2494
2495 /* The 6 GHz band requires HE to be enabled. */
2496 params->he_enabled = 1;
2497
2498 if (params->center_freq1) {
2499 if (params->freq == 5935)
2500 idx = (params->center_freq1 - 5925) / 5;
2501 else
2502 idx = (params->center_freq1 - 5950) / 5;
2503
2504 bw = center_idx_to_bw_6ghz(idx);
2505 if (bw < 0 || bw > (int) ARRAY_SIZE(bw_idx) ||
2506 bw_idx[bw] != params->bandwidth)
2507 return -1;
2508 }
Sunil Ravi7f769292024-07-23 22:21:32 +00002509 } else { /* Non-6 GHz channel */
2510 /* An EHT STA is also an HE STA as defined in
2511 * IEEE P802.11be/D5.0, 4.3.16a. */
2512 if (params->he_enabled || params->eht_enabled) {
2513 params->he_enabled = 1;
2514 /* An HE STA is also a VHT STA if operating in the 5 GHz
2515 * band and an HE STA is also an HT STA in the 2.4 GHz
2516 * band as defined in IEEE Std 802.11ax-2021, 4.3.15a.
2517 * A VHT STA is an HT STA as defined in IEEE
2518 * Std 802.11, 4.3.15. */
2519 if (IS_5GHZ(params->freq))
2520 params->vht_enabled = 1;
2521
2522 params->ht_enabled = 1;
2523 }
Sunil Ravi036cec52023-03-29 11:35:17 -07002524 }
2525
Hai Shalomfdcde762020-04-02 11:19:20 -07002526 switch (params->bandwidth) {
2527 case 0:
2528 /* bandwidth not specified: use 20 MHz by default */
2529 /* fall-through */
2530 case 20:
2531 if (params->center_freq1 &&
2532 params->center_freq1 != params->freq)
2533 return -1;
2534
2535 if (params->center_freq2 || params->sec_channel_offset)
2536 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07002537
2538 if (punct_bitmap)
2539 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002540 break;
2541 case 40:
2542 if (params->center_freq2 || !params->sec_channel_offset)
2543 return -1;
2544
Sunil Ravi036cec52023-03-29 11:35:17 -07002545 if (punct_bitmap)
2546 return -1;
2547
Hai Shalomfdcde762020-04-02 11:19:20 -07002548 if (!params->center_freq1)
2549 break;
2550 switch (params->sec_channel_offset) {
2551 case 1:
2552 if (params->freq + 10 != params->center_freq1)
2553 return -1;
2554 break;
2555 case -1:
2556 if (params->freq - 10 != params->center_freq1)
2557 return -1;
2558 break;
2559 default:
2560 return -1;
2561 }
2562 break;
2563 case 80:
2564 if (!params->center_freq1 || !params->sec_channel_offset)
2565 return 1;
2566
2567 switch (params->sec_channel_offset) {
2568 case 1:
2569 if (params->freq - 10 != params->center_freq1 &&
2570 params->freq + 30 != params->center_freq1)
2571 return 1;
2572 break;
2573 case -1:
2574 if (params->freq + 10 != params->center_freq1 &&
2575 params->freq - 30 != params->center_freq1)
2576 return -1;
2577 break;
2578 default:
2579 return -1;
2580 }
2581
Sunil Ravi036cec52023-03-29 11:35:17 -07002582 if (params->center_freq2 && punct_bitmap)
2583 return -1;
2584
Hai Shalomfdcde762020-04-02 11:19:20 -07002585 /* Adjacent and overlapped are not allowed for 80+80 */
2586 if (params->center_freq2 &&
2587 params->center_freq1 - params->center_freq2 <= 80 &&
2588 params->center_freq2 - params->center_freq1 <= 80)
2589 return 1;
2590 break;
2591 case 160:
2592 if (!params->center_freq1 || params->center_freq2 ||
2593 !params->sec_channel_offset)
2594 return -1;
2595
2596 switch (params->sec_channel_offset) {
2597 case 1:
2598 if (params->freq + 70 != params->center_freq1 &&
2599 params->freq + 30 != params->center_freq1 &&
2600 params->freq - 10 != params->center_freq1 &&
2601 params->freq - 50 != params->center_freq1)
2602 return -1;
2603 break;
2604 case -1:
2605 if (params->freq + 50 != params->center_freq1 &&
2606 params->freq + 10 != params->center_freq1 &&
2607 params->freq - 30 != params->center_freq1 &&
2608 params->freq - 70 != params->center_freq1)
2609 return -1;
2610 break;
2611 default:
2612 return -1;
2613 }
2614 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00002615 case 320:
2616 if (!params->center_freq1 || params->center_freq2 ||
2617 !params->sec_channel_offset)
2618 return -1;
2619
2620 switch (params->sec_channel_offset) {
2621 case 1:
2622 if (params->freq + 150 != params->center_freq1 &&
2623 params->freq + 110 != params->center_freq1 &&
2624 params->freq + 70 != params->center_freq1 &&
2625 params->freq + 30 != params->center_freq1 &&
2626 params->freq - 10 != params->center_freq1 &&
2627 params->freq - 50 != params->center_freq1 &&
2628 params->freq - 90 != params->center_freq1 &&
2629 params->freq - 130 != params->center_freq1)
2630 return -1;
2631 break;
2632 case -1:
2633 if (params->freq + 130 != params->center_freq1 &&
2634 params->freq + 90 != params->center_freq1 &&
2635 params->freq + 50 != params->center_freq1 &&
2636 params->freq + 10 != params->center_freq1 &&
2637 params->freq - 30 != params->center_freq1 &&
2638 params->freq - 70 != params->center_freq1 &&
2639 params->freq - 110 != params->center_freq1 &&
2640 params->freq - 150 != params->center_freq1)
2641 return -1;
2642 break;
2643 }
2644 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002645 default:
2646 return -1;
2647 }
2648
Sunil Ravi036cec52023-03-29 11:35:17 -07002649 if (!punct_bitmap)
2650 return 0;
2651
2652 if (!params->eht_enabled) {
2653 wpa_printf(MSG_ERROR,
2654 "Preamble puncturing supported only in EHT");
2655 return -1;
2656 }
2657
2658 if (params->freq >= 2412 && params->freq <= 2484) {
2659 wpa_printf(MSG_ERROR,
2660 "Preamble puncturing is not supported in 2.4 GHz");
2661 return -1;
2662 }
2663
2664 start_freq = params->center_freq1 - (params->bandwidth / 2);
2665 if (!is_punct_bitmap_valid(params->bandwidth,
2666 (params->freq - start_freq) / 20,
2667 punct_bitmap)) {
2668 wpa_printf(MSG_ERROR, "Invalid preamble puncturing bitmap");
2669 return -1;
2670 }
2671
Hai Shalomfdcde762020-04-02 11:19:20 -07002672 return 0;
2673}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002674#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002675
2676
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002677static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2678 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002679{
2680#ifdef NEED_AP_MLME
2681 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002682 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002683 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002684 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002685 int bandwidth;
2686 u8 chan;
Sunil Ravi99c035e2024-07-12 01:42:03 +00002687 unsigned int num_err = 0;
2688 int err = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002689
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002690 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002691 if (ret)
2692 return ret;
2693
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002694 settings.link_id = -1;
2695#ifdef CONFIG_IEEE80211BE
2696 if (iface->num_bss && iface->bss[0]->conf->mld_ap)
2697 settings.link_id = iface->bss[0]->mld_link_id;
2698#endif /* CONFIG_IEEE80211BE */
2699
Sunil Ravic0f5d412024-09-11 22:12:49 +00002700 if (iface->num_hw_features > 1 &&
2701 !hostapd_ctrl_is_freq_in_cmode(iface->current_mode,
2702 iface->current_hw_info,
2703 settings.freq_params.freq)) {
2704 wpa_printf(MSG_INFO,
2705 "chanswitch: Invalid frequency settings provided for multi band phy");
2706 return -1;
2707 }
2708
Sunil Ravi036cec52023-03-29 11:35:17 -07002709 ret = hostapd_ctrl_check_freq_params(&settings.freq_params,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002710 settings.freq_params.punct_bitmap);
Hai Shalomfdcde762020-04-02 11:19:20 -07002711 if (ret) {
2712 wpa_printf(MSG_INFO,
2713 "chanswitch: invalid frequency settings provided");
2714 return ret;
2715 }
2716
2717 switch (settings.freq_params.bandwidth) {
2718 case 40:
2719 bandwidth = CHAN_WIDTH_40;
2720 break;
2721 case 80:
2722 if (settings.freq_params.center_freq2)
2723 bandwidth = CHAN_WIDTH_80P80;
2724 else
2725 bandwidth = CHAN_WIDTH_80;
2726 break;
2727 case 160:
2728 bandwidth = CHAN_WIDTH_160;
2729 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002730 case 320:
2731 bandwidth = CHAN_WIDTH_320;
2732 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002733 default:
2734 bandwidth = CHAN_WIDTH_20;
2735 break;
2736 }
2737
2738 if (settings.freq_params.center_freq1)
2739 dfs_range += hostapd_is_dfs_overlap(
2740 iface, bandwidth, settings.freq_params.center_freq1);
2741 else
2742 dfs_range += hostapd_is_dfs_overlap(
2743 iface, bandwidth, settings.freq_params.freq);
2744
2745 if (settings.freq_params.center_freq2)
2746 dfs_range += hostapd_is_dfs_overlap(
2747 iface, bandwidth, settings.freq_params.center_freq2);
2748
2749 if (dfs_range) {
2750 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2751 if (ret == NUM_HOSTAPD_MODES) {
2752 wpa_printf(MSG_ERROR,
2753 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2754 settings.freq_params.freq,
2755 settings.freq_params.sec_channel_offset,
2756 settings.freq_params.bandwidth);
2757 return -1;
2758 }
2759
2760 settings.freq_params.channel = chan;
2761
2762 wpa_printf(MSG_DEBUG,
2763 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2764 settings.freq_params.channel,
2765 settings.freq_params.freq,
2766 settings.freq_params.sec_channel_offset,
2767 settings.freq_params.bandwidth,
2768 settings.freq_params.center_freq1);
2769
2770 /* Perform CAC and switch channel */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002771 iface->is_ch_switch_dfs = true;
Hai Shalomfdcde762020-04-02 11:19:20 -07002772 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2773 return 0;
2774 }
2775
Sunil Ravic0f5d412024-09-11 22:12:49 +00002776 if (iface->cac_started) {
2777 wpa_printf(MSG_DEBUG,
2778 "CAC is in progress - switching channel without CSA");
2779 return hostapd_force_channel_switch(iface, &settings);
2780 }
2781
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002782 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002783
Sunil Ravia04bd252022-05-02 22:54:18 -07002784 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002785 hostapd_chan_switch_config(iface->bss[i],
2786 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002787
Sunil Ravi99c035e2024-07-12 01:42:03 +00002788 err = hostapd_switch_channel(iface->bss[i], &settings);
2789 if (err) {
2790 ret = err;
2791 num_err++;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002792 }
2793 }
2794
Sunil Ravi99c035e2024-07-12 01:42:03 +00002795 return (iface->num_bss == num_err) ? ret : 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002796#else /* NEED_AP_MLME */
2797 return -1;
2798#endif /* NEED_AP_MLME */
2799}
2800
2801
Sunil Ravi7f769292024-07-23 22:21:32 +00002802#ifdef CONFIG_IEEE80211AX
2803static int hostapd_ctrl_iface_color_change(struct hostapd_iface *iface,
2804 const char *pos)
2805{
2806#ifdef NEED_AP_MLME
2807 struct cca_settings settings;
2808 struct hostapd_data *hapd = iface->bss[0];
2809 int ret, color;
2810 unsigned int i;
2811 char *end;
2812
2813 os_memset(&settings, 0, sizeof(settings));
2814
2815 color = strtol(pos, &end, 10);
2816 if (pos == end || color < 0 || color > 63) {
2817 wpa_printf(MSG_ERROR, "color_change: Invalid color provided");
2818 return -1;
2819 }
2820
2821 /* Color value is expected to be [1-63]. If 0 comes, assumption is this
2822 * is to disable the color. In this case no need to do CCA, just
2823 * changing Beacon frames is sufficient. */
2824 if (color == 0) {
2825 if (iface->conf->he_op.he_bss_color_disabled) {
2826 wpa_printf(MSG_ERROR,
2827 "color_change: Color is already disabled");
2828 return -1;
2829 }
2830
2831 iface->conf->he_op.he_bss_color_disabled = 1;
2832
2833 for (i = 0; i < iface->num_bss; i++)
2834 ieee802_11_set_beacon(iface->bss[i]);
2835
2836 return 0;
2837 }
2838
2839 if (color == iface->conf->he_op.he_bss_color) {
2840 if (!iface->conf->he_op.he_bss_color_disabled) {
2841 wpa_printf(MSG_ERROR,
2842 "color_change: Provided color is already set");
2843 return -1;
2844 }
2845
2846 iface->conf->he_op.he_bss_color_disabled = 0;
2847
2848 for (i = 0; i < iface->num_bss; i++)
2849 ieee802_11_set_beacon(iface->bss[i]);
2850
2851 return 0;
2852 }
2853
2854 if (hapd->cca_in_progress) {
2855 wpa_printf(MSG_ERROR,
2856 "color_change: CCA is already in progress");
2857 return -1;
2858 }
2859
2860 iface->conf->he_op.he_bss_color_disabled = 0;
2861
2862 for (i = 0; i < iface->num_bss; i++) {
2863 struct hostapd_data *bss = iface->bss[i];
2864
2865 hostapd_cleanup_cca_params(bss);
2866
2867 bss->cca_color = color;
2868 bss->cca_count = 10;
2869
2870 if (hostapd_fill_cca_settings(bss, &settings)) {
2871 wpa_printf(MSG_DEBUG,
2872 "color_change: Filling CCA settings failed for color: %d\n",
2873 color);
2874 hostapd_cleanup_cca_params(bss);
2875 continue;
2876 }
2877
2878 wpa_printf(MSG_DEBUG, "Setting user selected color: %d", color);
2879 ret = hostapd_drv_switch_color(bss, &settings);
2880 if (ret)
2881 hostapd_cleanup_cca_params(bss);
2882
2883 free_beacon_data(&settings.beacon_cca);
2884 free_beacon_data(&settings.beacon_after);
2885 }
2886
2887 return 0;
2888#else /* NEED_AP_MLME */
2889 return -1;
2890#endif /* NEED_AP_MLME */
2891}
2892#endif /* CONFIG_IEEE80211AX */
2893
2894
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002895static u8 hostapd_maxnss(struct hostapd_data *hapd, struct sta_info *sta)
2896{
2897 u8 *mcs_set = NULL;
2898 u16 mcs_map;
2899 u8 ht_rx_nss = 0;
2900 u8 vht_rx_nss = 1;
2901 u8 mcs;
2902 bool ht_supported = false;
2903 bool vht_supported = false;
2904 int i;
2905
2906 if (sta->ht_capabilities && (sta->flags & WLAN_STA_HT)) {
2907 mcs_set = sta->ht_capabilities->supported_mcs_set;
2908 ht_supported = true;
2909 }
2910
2911 if (sta->vht_capabilities && (sta->flags & WLAN_STA_VHT)) {
2912 mcs_map = le_to_host16(
2913 sta->vht_capabilities->vht_supported_mcs_set.rx_map);
2914 vht_supported = true;
2915 }
2916
2917 if (ht_supported && mcs_set) {
2918 if (mcs_set[0])
2919 ht_rx_nss++;
2920 if (mcs_set[1])
2921 ht_rx_nss++;
2922 if (mcs_set[2])
2923 ht_rx_nss++;
2924 if (mcs_set[3])
2925 ht_rx_nss++;
2926 }
2927 if (vht_supported) {
2928 for (i = 7; i >= 0; i--) {
2929 mcs = (mcs_map >> (2 * i)) & 0x03;
2930 if (mcs != 0x03) {
2931 vht_rx_nss = i + 1;
2932 break;
2933 }
2934 }
2935 }
2936
2937 return ht_rx_nss > vht_rx_nss ? ht_rx_nss : vht_rx_nss;
2938}
2939
2940
2941static char hostapd_ctrl_iface_notify_cw_htaction(struct hostapd_data *hapd,
2942 const u8 *addr, u8 width)
2943{
2944 u8 buf[3];
2945 char ret;
2946
2947 width = width >= 1 ? 1 : 0;
2948
2949 buf[0] = WLAN_ACTION_HT;
2950 buf[1] = WLAN_HT_ACTION_NOTIFY_CHANWIDTH;
2951 buf[2] = width;
2952
2953 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2954 buf, sizeof(buf));
2955 if (ret)
2956 wpa_printf(MSG_DEBUG,
2957 "Failed to send Notify Channel Width frame to "
2958 MACSTR, MAC2STR(addr));
2959
2960 return ret;
2961}
2962
2963
2964static char hostapd_ctrl_iface_notify_cw_vhtaction(struct hostapd_data *hapd,
2965 const u8 *addr, u8 width)
2966{
2967 u8 buf[3];
2968 char ret;
2969
2970 buf[0] = WLAN_ACTION_VHT;
2971 buf[1] = WLAN_VHT_ACTION_OPMODE_NOTIF;
2972 buf[2] = width;
2973
2974 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2975 buf, sizeof(buf));
2976 if (ret)
2977 wpa_printf(MSG_DEBUG,
2978 "Failed to send Opeating Mode Notification frame to "
2979 MACSTR, MAC2STR(addr));
2980
2981 return ret;
2982}
2983
2984
2985static char hostapd_ctrl_iface_notify_cw_change(struct hostapd_data *hapd,
2986 const char *cmd)
2987{
2988 u8 cw, operating_mode = 0, nss;
2989 struct sta_info *sta;
2990 enum hostapd_hw_mode hw_mode;
2991
2992 if (is_6ghz_freq(hapd->iface->freq)) {
2993 wpa_printf(MSG_ERROR, "20/40 BSS coex not supported in 6 GHz");
2994 return -1;
2995 }
2996
2997 cw = atoi(cmd);
2998 hw_mode = hapd->iface->current_mode->mode;
2999 if ((hw_mode == HOSTAPD_MODE_IEEE80211G ||
3000 hw_mode == HOSTAPD_MODE_IEEE80211B) &&
3001 !(cw == 0 || cw == 1)) {
3002 wpa_printf(MSG_ERROR,
3003 "Channel width should be either 20 MHz or 40 MHz for 2.4 GHz band");
3004 return -1;
3005 }
3006
3007 switch (cw) {
3008 case 0:
3009 operating_mode = 0;
3010 break;
3011 case 1:
3012 operating_mode = VHT_OPMODE_CHANNEL_40MHZ;
3013 break;
3014 case 2:
3015 operating_mode = VHT_OPMODE_CHANNEL_80MHZ;
3016 break;
3017 case 3:
3018 operating_mode = VHT_OPMODE_CHANNEL_160MHZ;
3019 break;
3020 default:
3021 wpa_printf(MSG_ERROR, "Channel width should be between 0 to 3");
3022 return -1;
3023 }
3024
3025 for (sta = hapd->sta_list; sta; sta = sta->next) {
3026 if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
3027 nss = hostapd_maxnss(hapd, sta) - 1;
3028 hostapd_ctrl_iface_notify_cw_vhtaction(hapd, sta->addr,
3029 operating_mode |
3030 (u8) (nss << 4));
3031 continue;
3032 }
3033
3034 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) ==
3035 WLAN_STA_HT && sta->ht_capabilities)
3036 hostapd_ctrl_iface_notify_cw_htaction(hapd, sta->addr,
3037 cw);
3038 }
3039
3040 return 0;
3041}
3042
3043
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003044static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
3045 int reply_size, const char *param)
3046{
3047#ifdef RADIUS_SERVER
3048 if (os_strcmp(param, "radius_server") == 0) {
3049 return radius_server_get_mib(hapd->radius_srv, reply,
3050 reply_size);
3051 }
3052#endif /* RADIUS_SERVER */
3053 return -1;
3054}
3055
3056
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003057static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
3058 char *buf, size_t buflen)
3059{
3060 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08003061 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003062 u8 *data = NULL;
3063 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08003064 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003065 struct wpabuf *reply;
3066 size_t data_len = 0;
3067
Hai Shalom60840252021-02-19 19:02:11 -08003068 /**
3069 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
3070 * [nested=<0|1>]
3071 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003072 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003073 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003074 return -EINVAL;
3075
3076 subcmd = strtoul(pos, &pos, 10);
3077
3078 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003079 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003080 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08003081
3082 temp = os_strchr(pos, ' ');
3083 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003084 }
3085
3086 if (data_len) {
3087 data_len /= 2;
3088 data = os_malloc(data_len);
3089 if (!data)
3090 return -ENOBUFS;
3091
3092 if (hexstr2bin(pos, data, data_len)) {
3093 wpa_printf(MSG_DEBUG,
3094 "Vendor command: wrong parameter format");
3095 os_free(data);
3096 return -EINVAL;
3097 }
3098 }
3099
Hai Shalom60840252021-02-19 19:02:11 -08003100 pos = os_strstr(cmd, "nested=");
3101 if (pos)
3102 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
3103 NESTED_ATTR_NOT_USED;
3104
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003105 reply = wpabuf_alloc((buflen - 1) / 2);
3106 if (!reply) {
3107 os_free(data);
3108 return -ENOBUFS;
3109 }
3110
3111 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08003112 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003113
3114 if (ret == 0)
3115 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
3116 wpabuf_len(reply));
3117
3118 wpabuf_free(reply);
3119 os_free(data);
3120
3121 return ret;
3122}
3123
3124
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003125static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
3126 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003128 u8 addr[ETH_ALEN];
3129 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003131 if (hwaddr_aton(cmd, addr))
3132 return -1;
3133
3134 sta = ap_get_sta(hapd, addr);
3135 if (!sta || !sta->eapol_sm)
3136 return -1;
3137
3138 eapol_auth_reauthenticate(sta->eapol_sm);
3139 return 0;
3140}
3141
3142
3143static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
3144{
3145 u8 addr[ETH_ALEN];
3146 struct sta_info *sta;
3147 char *pos = cmd, *param;
3148
3149 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
3150 return -1;
3151 pos += 18;
3152 param = pos;
3153 pos = os_strchr(pos, ' ');
3154 if (!pos)
3155 return -1;
3156 *pos++ = '\0';
3157
3158 sta = ap_get_sta(hapd, addr);
3159 if (!sta || !sta->eapol_sm)
3160 return -1;
3161
3162 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
3163}
3164
3165
3166static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
3167 char *buf, size_t buflen)
3168{
3169 char *pos, *end, *stamp;
3170 int ret;
3171
3172 /* cmd: "LOG_LEVEL [<level>]" */
3173 if (*cmd == '\0') {
3174 pos = buf;
3175 end = buf + buflen;
3176 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
3177 "Timestamp: %d\n",
3178 debug_level_str(wpa_debug_level),
3179 wpa_debug_timestamp);
3180 if (os_snprintf_error(end - pos, ret))
3181 ret = 0;
3182
3183 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003186 while (*cmd == ' ')
3187 cmd++;
3188
3189 stamp = os_strchr(cmd, ' ');
3190 if (stamp) {
3191 *stamp++ = '\0';
3192 while (*stamp == ' ') {
3193 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003194 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195 }
3196
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003197 if (os_strlen(cmd)) {
3198 int level = str_to_debug_level(cmd);
3199 if (level < 0)
3200 return -1;
3201 wpa_debug_level = level;
3202 }
3203
3204 if (stamp && os_strlen(stamp))
3205 wpa_debug_timestamp = atoi(stamp);
3206
3207 os_memcpy(buf, "OK\n", 3);
3208 return 3;
3209}
3210
3211
3212#ifdef NEED_AP_MLME
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003213
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003214static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
3215 char *buf, size_t buflen)
3216{
3217 struct hostapd_iface *iface = hapd->iface;
3218 char *pos, *end;
3219 struct hostapd_sta_info *info;
3220 struct os_reltime now;
3221
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003222 if (!iface->num_sta_seen)
3223 return 0;
3224
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003225 sta_track_expire(iface, 0);
3226
3227 pos = buf;
3228 end = buf + buflen;
3229
3230 os_get_reltime(&now);
3231 dl_list_for_each_reverse(info, &iface->sta_seen,
3232 struct hostapd_sta_info, list) {
3233 struct os_reltime age;
3234 int ret;
3235
3236 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003237 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
3238 MAC2STR(info->addr), (unsigned int) age.sec,
3239 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003240 if (os_snprintf_error(end - pos, ret))
3241 break;
3242 pos += ret;
3243 }
3244
3245 return pos - buf;
3246}
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003247
3248
3249static int hostapd_ctrl_iface_dump_beacon(struct hostapd_data *hapd,
3250 char *buf, size_t buflen)
3251{
3252 struct beacon_data beacon;
3253 char *pos, *end;
3254 int ret;
3255
3256 if (hostapd_build_beacon_data(hapd, &beacon) < 0)
3257 return -1;
3258
3259 if (2 * (beacon.head_len + beacon.tail_len) > buflen)
3260 return -1;
3261
3262 pos = buf;
3263 end = buf + buflen;
3264
3265 ret = wpa_snprintf_hex(pos, end - pos, beacon.head, beacon.head_len);
3266 pos += ret;
3267
3268 ret = wpa_snprintf_hex(pos, end - pos, beacon.tail, beacon.tail_len);
3269 pos += ret;
3270
3271 free_beacon_data(&beacon);
3272
3273 return pos - buf;
3274}
3275
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003276#endif /* NEED_AP_MLME */
3277
3278
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003279static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
3280 const char *cmd)
3281{
3282 u8 addr[ETH_ALEN];
3283
3284 if (hwaddr_aton(cmd, addr)) {
3285 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
3286 return -1;
3287 }
3288
3289 return hostapd_send_lci_req(hapd, addr);
3290}
3291
3292
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07003293static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003294{
3295 u8 addr[ETH_ALEN];
3296 char *token, *context = NULL;
3297 int random_interval, min_ap;
3298 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
3299 unsigned int n_responders;
3300
3301 token = str_token(cmd, " ", &context);
3302 if (!token || hwaddr_aton(token, addr)) {
3303 wpa_printf(MSG_INFO,
3304 "CTRL: REQ_RANGE - Bad destination address");
3305 return -1;
3306 }
3307
3308 token = str_token(cmd, " ", &context);
3309 if (!token)
3310 return -1;
3311
3312 random_interval = atoi(token);
3313 if (random_interval < 0 || random_interval > 0xffff)
3314 return -1;
3315
3316 token = str_token(cmd, " ", &context);
3317 if (!token)
3318 return -1;
3319
3320 min_ap = atoi(token);
3321 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
3322 return -1;
3323
3324 n_responders = 0;
3325 while ((token = str_token(cmd, " ", &context))) {
3326 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
3327 wpa_printf(MSG_INFO,
3328 "CTRL: REQ_RANGE: Too many responders");
3329 return -1;
3330 }
3331
3332 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
3333 wpa_printf(MSG_INFO,
3334 "CTRL: REQ_RANGE: Bad responder address");
3335 return -1;
3336 }
3337
3338 n_responders++;
3339 }
3340
3341 if (!n_responders) {
3342 wpa_printf(MSG_INFO,
3343 "CTRL: REQ_RANGE - No FTM responder address");
3344 return -1;
3345 }
3346
3347 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
3348 responders, n_responders);
3349}
3350
3351
Dmitry Shmidt29333592017-01-09 12:27:11 -08003352static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
3353 const char *cmd, char *reply,
3354 size_t reply_size)
3355{
3356 u8 addr[ETH_ALEN];
3357 const char *pos;
3358 struct wpabuf *req;
3359 int ret;
3360 u8 req_mode = 0;
3361
3362 if (hwaddr_aton(cmd, addr))
3363 return -1;
3364 pos = os_strchr(cmd, ' ');
3365 if (!pos)
3366 return -1;
3367 pos++;
3368 if (os_strncmp(pos, "req_mode=", 9) == 0) {
3369 int val = hex2byte(pos + 9);
3370
3371 if (val < 0)
3372 return -1;
3373 req_mode = val;
3374 pos += 11;
3375 pos = os_strchr(pos, ' ');
3376 if (!pos)
3377 return -1;
3378 pos++;
3379 }
3380 req = wpabuf_parse_bin(pos);
3381 if (!req)
3382 return -1;
3383
3384 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
3385 wpabuf_free(req);
3386 if (ret >= 0)
3387 ret = os_snprintf(reply, reply_size, "%d", ret);
3388 return ret;
3389}
3390
3391
Sunil Ravi99c035e2024-07-12 01:42:03 +00003392static int hostapd_ctrl_iface_req_link_measurement(struct hostapd_data *hapd,
3393 const char *cmd, char *reply,
3394 size_t reply_size)
3395{
3396 u8 addr[ETH_ALEN];
3397 int ret;
3398
3399 if (hwaddr_aton(cmd, addr)) {
3400 wpa_printf(MSG_ERROR,
3401 "CTRL: REQ_LINK_MEASUREMENT: Invalid MAC address");
3402 return -1;
3403 }
3404
3405 ret = hostapd_send_link_measurement_req(hapd, addr);
3406 if (ret >= 0)
3407 ret = os_snprintf(reply, reply_size, "%d", ret);
3408 return ret;
3409}
3410
3411
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003412static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3413 char *buf, size_t buflen)
3414{
3415 if (!(hapd->conf->radio_measurements[0] &
3416 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3417 wpa_printf(MSG_ERROR,
3418 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3419 return -1;
3420 }
3421
3422 return hostapd_neighbor_show(hapd, buf, buflen);
3423}
3424
3425
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003426static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3427{
3428 struct wpa_ssid_value ssid;
3429 u8 bssid[ETH_ALEN];
3430 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003431 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003432 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003433 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003434 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003435
3436 if (!(hapd->conf->radio_measurements[0] &
3437 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3438 wpa_printf(MSG_ERROR,
3439 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3440 return -1;
3441 }
3442
3443 if (hwaddr_aton(buf, bssid)) {
3444 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3445 return -1;
3446 }
3447
3448 tmp = os_strstr(buf, "ssid=");
3449 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3450 wpa_printf(MSG_ERROR,
3451 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3452 return -1;
3453 }
3454 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3455 if (!buf)
3456 return -1;
3457
3458 tmp = os_strstr(buf, "nr=");
3459 if (!tmp) {
3460 wpa_printf(MSG_ERROR,
3461 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3462 return -1;
3463 }
3464
3465 buf = os_strchr(tmp, ' ');
3466 if (buf)
3467 *buf++ = '\0';
3468
3469 nr = wpabuf_parse_bin(tmp + 3);
3470 if (!nr) {
3471 wpa_printf(MSG_ERROR,
3472 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3473 return -1;
3474 }
3475
3476 if (!buf)
3477 goto set;
3478
3479 tmp = os_strstr(buf, "lci=");
3480 if (tmp) {
3481 buf = os_strchr(tmp, ' ');
3482 if (buf)
3483 *buf++ = '\0';
3484 lci = wpabuf_parse_bin(tmp + 4);
3485 if (!lci) {
3486 wpa_printf(MSG_ERROR,
3487 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003488 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003489 }
3490 }
3491
3492 if (!buf)
3493 goto set;
3494
3495 tmp = os_strstr(buf, "civic=");
3496 if (tmp) {
3497 buf = os_strchr(tmp, ' ');
3498 if (buf)
3499 *buf++ = '\0';
3500 civic = wpabuf_parse_bin(tmp + 6);
3501 if (!civic) {
3502 wpa_printf(MSG_ERROR,
3503 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003504 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003505 }
3506 }
3507
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003508 if (!buf)
3509 goto set;
3510
3511 if (os_strstr(buf, "stat"))
3512 stationary = 1;
3513
Hai Shaloma20dcd72022-02-04 13:43:00 -08003514 tmp = os_strstr(buf, "bss_parameter=");
3515 if (tmp) {
3516 bss_parameters = atoi(tmp + 14);
3517 if (bss_parameters < 0 || bss_parameters > 0xff) {
3518 wpa_printf(MSG_ERROR,
3519 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3520 goto fail;
3521 }
3522 }
3523
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003524set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003525 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003526 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003527
Hai Shaloma20dcd72022-02-04 13:43:00 -08003528fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003529 wpabuf_free(nr);
3530 wpabuf_free(lci);
3531 wpabuf_free(civic);
3532
3533 return ret;
3534}
3535
3536
3537static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3538 char *buf)
3539{
3540 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003541 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003542 u8 bssid[ETH_ALEN];
3543 char *tmp;
3544
3545 if (hwaddr_aton(buf, bssid)) {
3546 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3547 return -1;
3548 }
3549
3550 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003551 if (tmp) {
3552 ssidp = &ssid;
3553 if (ssid_parse(tmp + 5, &ssid)) {
3554 wpa_printf(MSG_ERROR,
3555 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3556 return -1;
3557 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003558 }
3559
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003560 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003561}
3562
3563
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003564static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3565 size_t buflen)
3566{
3567 int ret, i;
3568 char *pos, *end;
3569
3570 ret = os_snprintf(buf, buflen, "%016llX:\n",
3571 (long long unsigned) iface->drv_flags);
3572 if (os_snprintf_error(buflen, ret))
3573 return -1;
3574
3575 pos = buf + ret;
3576 end = buf + buflen;
3577
3578 for (i = 0; i < 64; i++) {
3579 if (iface->drv_flags & (1LLU << i)) {
3580 ret = os_snprintf(pos, end - pos, "%s\n",
3581 driver_flag_to_string(1LLU << i));
3582 if (os_snprintf_error(end - pos, ret))
3583 return -1;
3584 pos += ret;
3585 }
3586 }
3587
3588 return pos - buf;
3589}
3590
3591
Hai Shalomb755a2a2020-04-23 21:49:02 -07003592static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3593 size_t buflen)
3594{
3595 int ret, i;
3596 char *pos, *end;
3597
3598 ret = os_snprintf(buf, buflen, "%016llX:\n",
3599 (long long unsigned) iface->drv_flags2);
3600 if (os_snprintf_error(buflen, ret))
3601 return -1;
3602
3603 pos = buf + ret;
3604 end = buf + buflen;
3605
3606 for (i = 0; i < 64; i++) {
3607 if (iface->drv_flags2 & (1LLU << i)) {
3608 ret = os_snprintf(pos, end - pos, "%s\n",
3609 driver_flag2_to_string(1LLU << i));
3610 if (os_snprintf_error(end - pos, ret))
3611 return -1;
3612 pos += ret;
3613 }
3614 }
3615
3616 return pos - buf;
3617}
3618
3619
Hai Shalom021b0b52019-04-10 11:17:58 -07003620static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3621 const char *field, char *buf,
3622 size_t buflen)
3623{
3624 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3625
3626#ifdef CONFIG_DPP
3627 if (os_strcmp(field, "dpp") == 0) {
3628 int res;
3629
Hai Shaloma20dcd72022-02-04 13:43:00 -08003630#ifdef CONFIG_DPP3
3631 res = os_snprintf(buf, buflen, "DPP=3");
3632#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003633 res = os_snprintf(buf, buflen, "DPP=2");
3634#else /* CONFIG_DPP2 */
3635 res = os_snprintf(buf, buflen, "DPP=1");
3636#endif /* CONFIG_DPP2 */
3637 if (os_snprintf_error(buflen, res))
3638 return -1;
3639 return res;
3640 }
3641#endif /* CONFIG_DPP */
3642
3643 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3644 field);
3645
3646 return -1;
3647}
3648
3649
Hai Shaloma20dcd72022-02-04 13:43:00 -08003650#ifdef ANDROID
3651static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3652 char *buf, size_t buflen)
3653{
3654 int ret;
3655
3656 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3657 if (ret == 0) {
3658 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3659 if (os_snprintf_error(buflen, ret))
3660 ret = -1;
3661 }
3662 return ret;
3663}
3664#endif /* ANDROID */
3665
3666
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003667#ifdef CONFIG_IEEE80211BE
3668
3669static int hostapd_ctrl_iface_enable_mld(struct hostapd_iface *iface)
3670{
3671 unsigned int i;
3672
3673 if (!iface || !iface->bss[0]->conf->mld_ap) {
3674 wpa_printf(MSG_ERROR,
3675 "Trying to enable AP MLD on an interface that is not affiliated with an AP MLD");
3676 return -1;
3677 }
3678
3679 for (i = 0; i < iface->interfaces->count; ++i) {
3680 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3681 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003682
Sunil Ravi99c035e2024-07-12 01:42:03 +00003683 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003684 continue;
3685
3686 if (hostapd_enable_iface(h_iface)) {
3687 wpa_printf(MSG_ERROR, "Enabling of AP MLD failed");
3688 return -1;
3689 }
3690 }
3691 return 0;
3692}
3693
3694
3695static void hostapd_disable_iface_bss(struct hostapd_iface *iface)
3696{
3697 unsigned int i;
3698
3699 for (i = 0; i < iface->num_bss; i++)
3700 hostapd_bss_deinit_no_free(iface->bss[i]);
3701}
3702
3703
3704static int hostapd_ctrl_iface_disable_mld(struct hostapd_iface *iface)
3705{
3706 unsigned int i;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003707
3708 if (!iface || !iface->bss[0]->conf->mld_ap) {
3709 wpa_printf(MSG_ERROR,
3710 "Trying to disable AP MLD on an interface that is not affiliated with an AP MLD.");
3711 return -1;
3712 }
3713
3714 /* First, disable BSSs before stopping beaconing and doing driver
3715 * deinit so that the broadcast Deauthentication frames go out. */
3716
3717 for (i = 0; i < iface->interfaces->count; ++i) {
3718 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3719 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003720
Sunil Ravi99c035e2024-07-12 01:42:03 +00003721 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003722 continue;
3723
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003724 hostapd_disable_iface_bss(iface);
3725 }
3726
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003727 /* Then, fully disable interfaces */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003728 for (i = 0; i < iface->interfaces->count; ++i) {
3729 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3730 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003731
Sunil Ravi99c035e2024-07-12 01:42:03 +00003732 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003733 continue;
3734
3735 if (hostapd_disable_iface(h_iface)) {
3736 wpa_printf(MSG_ERROR, "Disabling AP MLD failed");
3737 return -1;
3738 }
3739 }
3740
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003741 return 0;
3742}
3743
3744
3745#ifdef CONFIG_TESTING_OPTIONS
3746static int hostapd_ctrl_iface_link_remove(struct hostapd_data *hapd, char *cmd,
3747 char *buf, size_t buflen)
3748{
3749 int ret;
3750 u32 count = atoi(cmd);
3751
3752 if (!count)
3753 count = 1;
3754
3755 ret = hostapd_link_remove(hapd, count);
3756 if (ret == 0) {
3757 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3758 if (os_snprintf_error(buflen, ret))
3759 ret = -1;
3760 else
3761 ret = 0;
3762 }
3763
3764 return ret;
3765}
3766#endif /* CONFIG_TESTING_OPTIONS */
3767#endif /* CONFIG_IEEE80211BE */
3768
3769
3770#ifdef CONFIG_NAN_USD
3771
3772static int hostapd_ctrl_nan_publish(struct hostapd_data *hapd, char *cmd,
3773 char *buf, size_t buflen)
3774{
3775 char *token, *context = NULL;
3776 int publish_id;
3777 struct nan_publish_params params;
3778 const char *service_name = NULL;
3779 struct wpabuf *ssi = NULL;
3780 int ret = -1;
3781 enum nan_service_protocol_type srv_proto_type = 0;
Sunil Ravic0f5d412024-09-11 22:12:49 +00003782 bool p2p = false;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003783
3784 os_memset(&params, 0, sizeof(params));
3785 /* USD shall use both solicited and unsolicited transmissions */
3786 params.unsolicited = true;
3787 params.solicited = true;
3788 /* USD shall require FSD without GAS */
3789 params.fsd = true;
3790
3791 while ((token = str_token(cmd, " ", &context))) {
3792 if (os_strncmp(token, "service_name=", 13) == 0) {
3793 service_name = token + 13;
3794 continue;
3795 }
3796
3797 if (os_strncmp(token, "ttl=", 4) == 0) {
3798 params.ttl = atoi(token + 4);
3799 continue;
3800 }
3801
3802 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3803 srv_proto_type = atoi(token + 15);
3804 continue;
3805 }
3806
3807 if (os_strncmp(token, "ssi=", 4) == 0) {
3808 if (ssi)
3809 goto fail;
3810 ssi = wpabuf_parse_bin(token + 4);
3811 if (!ssi)
3812 goto fail;
3813 continue;
3814 }
3815
Sunil Ravic0f5d412024-09-11 22:12:49 +00003816 if (os_strcmp(token, "p2p=1") == 0) {
3817 p2p = true;
3818 continue;
3819 }
3820
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003821 if (os_strcmp(token, "solicited=0") == 0) {
3822 params.solicited = false;
3823 continue;
3824 }
3825
3826 if (os_strcmp(token, "unsolicited=0") == 0) {
3827 params.unsolicited = false;
3828 continue;
3829 }
3830
3831 if (os_strcmp(token, "fsd=0") == 0) {
3832 params.fsd = false;
3833 continue;
3834 }
3835
3836 wpa_printf(MSG_INFO, "CTRL: Invalid NAN_PUBLISH parameter: %s",
3837 token);
3838 goto fail;
3839 }
3840
3841 publish_id = hostapd_nan_usd_publish(hapd, service_name, srv_proto_type,
Sunil Ravic0f5d412024-09-11 22:12:49 +00003842 ssi, &params, p2p);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003843 if (publish_id > 0)
3844 ret = os_snprintf(buf, buflen, "%d", publish_id);
3845fail:
3846 wpabuf_free(ssi);
3847 return ret;
3848}
3849
3850
3851static int hostapd_ctrl_nan_cancel_publish(struct hostapd_data *hapd,
3852 char *cmd)
3853{
3854 char *token, *context = NULL;
3855 int publish_id = 0;
3856
3857 while ((token = str_token(cmd, " ", &context))) {
3858 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3859 continue;
3860 wpa_printf(MSG_INFO,
3861 "CTRL: Invalid NAN_CANCEL_PUBLISH parameter: %s",
3862 token);
3863 return -1;
3864 }
3865
3866 if (publish_id <= 0) {
3867 wpa_printf(MSG_INFO,
3868 "CTRL: Invalid or missing NAN_CANCEL_PUBLISH publish_id");
3869 return -1;
3870 }
3871
3872 hostapd_nan_usd_cancel_publish(hapd, publish_id);
3873 return 0;
3874}
3875
3876
3877static int hostapd_ctrl_nan_update_publish(struct hostapd_data *hapd,
3878 char *cmd)
3879{
3880 char *token, *context = NULL;
3881 int publish_id = 0;
3882 struct wpabuf *ssi = NULL;
3883 int ret = -1;
3884
3885 while ((token = str_token(cmd, " ", &context))) {
3886 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3887 continue;
3888 if (os_strncmp(token, "ssi=", 4) == 0) {
3889 if (ssi)
3890 goto fail;
3891 ssi = wpabuf_parse_bin(token + 4);
3892 if (!ssi)
3893 goto fail;
3894 continue;
3895 }
3896 wpa_printf(MSG_INFO,
3897 "CTRL: Invalid NAN_UPDATE_PUBLISH parameter: %s",
3898 token);
3899 goto fail;
3900 }
3901
3902 if (publish_id <= 0) {
3903 wpa_printf(MSG_INFO,
3904 "CTRL: Invalid or missing NAN_UPDATE_PUBLISH publish_id");
3905 goto fail;
3906 }
3907
3908 ret = hostapd_nan_usd_update_publish(hapd, publish_id, ssi);
3909fail:
3910 wpabuf_free(ssi);
3911 return ret;
3912}
3913
3914
3915static int hostapd_ctrl_nan_subscribe(struct hostapd_data *hapd, char *cmd,
3916 char *buf, size_t buflen)
3917{
3918 char *token, *context = NULL;
3919 int subscribe_id;
3920 struct nan_subscribe_params params;
3921 const char *service_name = NULL;
3922 struct wpabuf *ssi = NULL;
3923 int ret = -1;
3924 enum nan_service_protocol_type srv_proto_type = 0;
Sunil Ravic0f5d412024-09-11 22:12:49 +00003925 bool p2p = false;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003926
3927 os_memset(&params, 0, sizeof(params));
3928
3929 while ((token = str_token(cmd, " ", &context))) {
3930 if (os_strncmp(token, "service_name=", 13) == 0) {
3931 service_name = token + 13;
3932 continue;
3933 }
3934
3935 if (os_strcmp(token, "active=1") == 0) {
3936 params.active = true;
3937 continue;
3938 }
3939
3940 if (os_strncmp(token, "ttl=", 4) == 0) {
3941 params.ttl = atoi(token + 4);
3942 continue;
3943 }
3944
3945 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3946 srv_proto_type = atoi(token + 15);
3947 continue;
3948 }
3949
3950 if (os_strncmp(token, "ssi=", 4) == 0) {
3951 if (ssi)
3952 goto fail;
3953 ssi = wpabuf_parse_bin(token + 4);
3954 if (!ssi)
3955 goto fail;
3956 continue;
3957 }
3958
Sunil Ravic0f5d412024-09-11 22:12:49 +00003959 if (os_strcmp(token, "p2p=1") == 0) {
3960 p2p = true;
3961 continue;
3962 }
3963
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003964 wpa_printf(MSG_INFO,
3965 "CTRL: Invalid NAN_SUBSCRIBE parameter: %s",
3966 token);
3967 goto fail;
3968 }
3969
3970 subscribe_id = hostapd_nan_usd_subscribe(hapd, service_name,
3971 srv_proto_type, ssi,
Sunil Ravic0f5d412024-09-11 22:12:49 +00003972 &params, p2p);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003973 if (subscribe_id > 0)
3974 ret = os_snprintf(buf, buflen, "%d", subscribe_id);
3975fail:
3976 wpabuf_free(ssi);
3977 return ret;
3978}
3979
3980
3981static int hostapd_ctrl_nan_cancel_subscribe(struct hostapd_data *hapd,
3982 char *cmd)
3983{
3984 char *token, *context = NULL;
3985 int subscribe_id = 0;
3986
3987 while ((token = str_token(cmd, " ", &context))) {
3988 if (sscanf(token, "subscribe_id=%i", &subscribe_id) == 1)
3989 continue;
3990 wpa_printf(MSG_INFO,
3991 "CTRL: Invalid NAN_CANCEL_SUBSCRIBE parameter: %s",
3992 token);
3993 return -1;
3994 }
3995
3996 if (subscribe_id <= 0) {
3997 wpa_printf(MSG_INFO,
3998 "CTRL: Invalid or missing NAN_CANCEL_SUBSCRIBE subscribe_id");
3999 return -1;
4000 }
4001
4002 hostapd_nan_usd_cancel_subscribe(hapd, subscribe_id);
4003 return 0;
4004}
4005
4006
4007static int hostapd_ctrl_nan_transmit(struct hostapd_data *hapd, char *cmd)
4008{
4009 char *token, *context = NULL;
4010 int handle = 0;
4011 int req_instance_id = 0;
4012 struct wpabuf *ssi = NULL;
4013 u8 peer_addr[ETH_ALEN];
4014 int ret = -1;
4015
4016 os_memset(peer_addr, 0, ETH_ALEN);
4017
4018 while ((token = str_token(cmd, " ", &context))) {
4019 if (sscanf(token, "handle=%i", &handle) == 1)
4020 continue;
4021
4022 if (sscanf(token, "req_instance_id=%i", &req_instance_id) == 1)
4023 continue;
4024
4025 if (os_strncmp(token, "address=", 8) == 0) {
4026 if (hwaddr_aton(token + 8, peer_addr) < 0)
4027 return -1;
4028 continue;
4029 }
4030
4031 if (os_strncmp(token, "ssi=", 4) == 0) {
4032 if (ssi)
4033 goto fail;
4034 ssi = wpabuf_parse_bin(token + 4);
4035 if (!ssi)
4036 goto fail;
4037 continue;
4038 }
4039
4040 wpa_printf(MSG_INFO,
4041 "CTRL: Invalid NAN_TRANSMIT parameter: %s",
4042 token);
4043 goto fail;
4044 }
4045
4046 if (handle <= 0) {
4047 wpa_printf(MSG_INFO,
4048 "CTRL: Invalid or missing NAN_TRANSMIT handle");
4049 goto fail;
4050 }
4051
4052 if (is_zero_ether_addr(peer_addr)) {
4053 wpa_printf(MSG_INFO,
4054 "CTRL: Invalid or missing NAN_TRANSMIT address");
4055 goto fail;
4056 }
4057
4058 ret = hostapd_nan_usd_transmit(hapd, handle, ssi, NULL, peer_addr,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004059 req_instance_id);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004060fail:
4061 wpabuf_free(ssi);
4062 return ret;
4063}
4064
4065#endif /* CONFIG_NAN_USD */
4066
4067
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004068static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
4069 char *buf, char *reply,
4070 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004071 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004072 socklen_t fromlen)
4073{
4074 int reply_len, res;
4075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 os_memcpy(reply, "OK\n", 3);
4077 reply_len = 3;
4078
4079 if (os_strcmp(buf, "PING") == 0) {
4080 os_memcpy(reply, "PONG\n", 5);
4081 reply_len = 5;
4082 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4083 if (wpa_debug_reopen_file() < 0)
4084 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004085 } else if (os_strcmp(buf, "CLOSE_LOG") == 0) {
4086 wpa_debug_stop_log();
Roshan Pius3a1667e2018-07-03 15:17:14 -07004087 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
4088 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004089 } else if (os_strcmp(buf, "STATUS") == 0) {
4090 reply_len = hostapd_ctrl_iface_status(hapd, reply,
4091 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004092 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
4093 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 } else if (os_strcmp(buf, "MIB") == 0) {
4095 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
4096 if (reply_len >= 0) {
4097 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
4098 reply_size - reply_len);
4099 if (res < 0)
4100 reply_len = -1;
4101 else
4102 reply_len += res;
4103 }
4104 if (reply_len >= 0) {
4105 res = ieee802_1x_get_mib(hapd, reply + reply_len,
4106 reply_size - reply_len);
4107 if (res < 0)
4108 reply_len = -1;
4109 else
4110 reply_len += res;
4111 }
4112#ifndef CONFIG_NO_RADIUS
4113 if (reply_len >= 0) {
4114 res = radius_client_get_mib(hapd->radius,
4115 reply + reply_len,
4116 reply_size - reply_len);
4117 if (res < 0)
4118 reply_len = -1;
4119 else
4120 reply_len += res;
4121 }
4122#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004123 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
4124 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
4125 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
4127 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
4128 reply_size);
4129 } else if (os_strncmp(buf, "STA ", 4) == 0) {
4130 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
4131 reply_size);
4132 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
4133 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
4134 reply_size);
4135 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004136 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
4137 reply_len = -1;
4138 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4139 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004140 reply_len = -1;
4141 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004142 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143 reply_len = -1;
4144 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004145 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146 buf + 6))
4147 reply_len = -1;
4148 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
4149 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
4150 reply_len = -1;
4151 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
4152 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
4153 reply_len = -1;
4154 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
4155 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
4156 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004157#ifdef CONFIG_TAXONOMY
4158 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
4159 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
4160 reply, reply_size);
4161#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004162 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
4163 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
4164 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004165 } else if (os_strcmp(buf, "STOP_AP") == 0) {
4166 if (hostapd_ctrl_iface_stop_ap(hapd))
4167 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168#ifdef NEED_AP_MLME
4169 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
4170 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
4171 reply_len = -1;
4172#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173#ifdef CONFIG_WPS
4174 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
4175 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
4176 reply_len = -1;
4177 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
4178 reply_len = hostapd_ctrl_iface_wps_check_pin(
4179 hapd, buf + 14, reply, reply_size);
4180 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
4181 if (hostapd_wps_button_pushed(hapd, NULL))
4182 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004183 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
4184 if (hostapd_wps_cancel(hapd))
4185 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004186 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
4187 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
4188 reply, reply_size);
4189 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
4190 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
4191 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004192 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
4193 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
4194 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004195#ifdef CONFIG_WPS_NFC
4196 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4197 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
4198 reply_len = -1;
4199 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
4200 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
4201 hapd, buf + 21, reply, reply_size);
4202 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4203 reply_len = hostapd_ctrl_iface_wps_nfc_token(
4204 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004205 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
4206 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
4207 hapd, buf + 21, reply, reply_size);
4208 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
4209 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
4210 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004211#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004212#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07004213#ifdef CONFIG_INTERWORKING
4214 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
4215 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
4216 reply_len = -1;
4217 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
4218 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
4219 reply_len = -1;
4220#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004221#ifdef CONFIG_HS20
4222 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
4223 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
4224 reply_len = -1;
4225 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
4226 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
4227 reply_len = -1;
4228#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004229#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004230 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
4231 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
4232 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004233 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
4234 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
4235 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004236 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
4237 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
4238 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004239 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
4240 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
4241 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004242#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
4244 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
4245 reply_size);
4246 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4247 if (hostapd_ctrl_iface_set(hapd, buf + 4))
4248 reply_len = -1;
4249 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4250 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
4251 reply_size);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004252 } else if (os_strcmp(buf, "ENABLE") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004253 if (hostapd_ctrl_iface_enable(hapd->iface))
4254 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08004255 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
4256 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
4257 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004258#ifdef CONFIG_IEEE80211R_AP
4259 } else if (os_strcmp(buf, "GET_RXKHS") == 0) {
4260 reply_len = hostapd_ctrl_iface_get_rxkhs(hapd, reply,
4261 reply_size);
4262 } else if (os_strcmp(buf, "RELOAD_RXKHS") == 0) {
4263 if (hostapd_ctrl_iface_reload_rxkhs(hapd))
4264 reply_len = -1;
4265#endif /* CONFIG_IEEE80211R_AP */
Sunil Ravi77d572f2023-01-17 23:58:31 +00004266 } else if (os_strcmp(buf, "RELOAD_BSS") == 0) {
4267 if (hostapd_ctrl_iface_reload_bss(hapd))
4268 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004269 } else if (os_strcmp(buf, "RELOAD_CONFIG") == 0) {
4270 if (hostapd_reload_config(hapd->iface))
4271 reply_len = -1;
4272 } else if (os_strcmp(buf, "RELOAD") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004273 if (hostapd_ctrl_iface_reload(hapd->iface))
4274 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004275 } else if (os_strcmp(buf, "DISABLE") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004276 if (hostapd_ctrl_iface_disable(hapd->iface))
4277 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004278 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
4279 if (ieee802_11_set_beacon(hapd))
4280 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004281#ifdef CONFIG_TESTING_OPTIONS
4282 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
4283 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
4284 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004285 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
4286 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
4287 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004288 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
4289 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
4290 buf + 23) < 0)
4291 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004292 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
4293 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
4294 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004295 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
4296 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
4297 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004298 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
4299 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
4300 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004301 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
4302 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
4303 reply_len = -1;
4304 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
4305 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
4306 reply_len = -1;
4307 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
4308 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
4309 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004310 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004311 if (testing_set_fail_pattern(true, buf + 16) < 0)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004312 reply_len = -1;
4313 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004314 reply_len = testing_get_fail_pattern(true, reply, reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004315 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004316 if (testing_set_fail_pattern(false, buf + 10) < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004317 reply_len = -1;
4318 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004319 reply_len = testing_get_fail_pattern(false, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004320 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
4321 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
4322 reply_len = -1;
4323 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
4324 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
4325 reply_len = -1;
4326 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
4327 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
4328 reply_len = -1;
4329 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
4330 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
4331 reply_len = -1;
4332 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
4333 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
4334 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004335 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
4336 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
4337 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004338 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
4339 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
4340 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004341 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
4342 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
4343 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08004344 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
4345 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
4346 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004347#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004348 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004349 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004350 reply_len = -1;
Sunil Ravi7f769292024-07-23 22:21:32 +00004351#ifdef CONFIG_IEEE80211AX
4352 } else if (os_strncmp(buf, "COLOR_CHANGE ", 13) == 0) {
4353 if (hostapd_ctrl_iface_color_change(hapd->iface, buf + 13))
4354 reply_len = -1;
4355#endif /* CONFIG_IEEE80211AX */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004356 } else if (os_strncmp(buf, "NOTIFY_CW_CHANGE ", 17) == 0) {
4357 if (hostapd_ctrl_iface_notify_cw_change(hapd, buf + 17))
4358 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004359 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
4360 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
4361 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004362 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
4363 ieee802_1x_erp_flush(hapd);
4364#ifdef RADIUS_SERVER
4365 radius_server_erp_flush(hapd->radius_srv);
4366#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004367 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
4368 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
4369 reply_len = -1;
4370 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
4371 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
4372 reply_len = -1;
4373 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
4374 reply_len = hostapd_ctrl_iface_log_level(
4375 hapd, buf + 9, reply, reply_size);
4376#ifdef NEED_AP_MLME
4377 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
4378 reply_len = hostapd_ctrl_iface_track_sta_list(
4379 hapd, reply, reply_size);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004380 } else if (os_strcmp(buf, "DUMP_BEACON") == 0) {
4381 reply_len = hostapd_ctrl_iface_dump_beacon(hapd, reply,
4382 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004383#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07004384 } else if (os_strcmp(buf, "PMKSA") == 0) {
4385 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
4386 reply_size);
4387 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4388 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004389 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
4390 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
4391 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004392 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
4393 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
4394 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004395 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
4396 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
4397 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004398 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
4399 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
4400 reply_len = -1;
4401 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
4402 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
4403 reply_len = -1;
4404 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
4405 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
4406 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08004407 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
4408 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
4409 reply, reply_size);
Sunil Ravi99c035e2024-07-12 01:42:03 +00004410 } else if (os_strncmp(buf, "REQ_LINK_MEASUREMENT ", 21) == 0) {
4411 reply_len = hostapd_ctrl_iface_req_link_measurement(
4412 hapd, buf + 21, reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004413 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
4414 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
4415 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07004416 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
4417 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
4418 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08004419 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4420 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07004421 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
4422 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004423 if (hostapd_ctrl_iface_acl_add_mac(
4424 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004425 &hapd->conf->num_accept_mac, buf + 19) ||
4426 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004427 reply_len = -1;
4428 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07004429 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07004430 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004431 &hapd->conf->num_accept_mac, buf + 19) ||
4432 hostapd_set_acl(hapd) ||
4433 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07004434 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004435 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
4436 reply_len = hostapd_ctrl_iface_acl_show_mac(
4437 hapd->conf->accept_mac,
4438 hapd->conf->num_accept_mac, reply, reply_size);
4439 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
4440 hostapd_ctrl_iface_acl_clear_list(
4441 &hapd->conf->accept_mac,
4442 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07004443 if (hostapd_set_acl(hapd) ||
4444 hostapd_disassoc_accept_mac(hapd))
4445 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07004446 } else {
4447 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004448 }
4449 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
4450 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07004451 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07004452 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004453 &hapd->conf->num_deny_mac, buf + 17) ||
4454 hostapd_set_acl(hapd) ||
4455 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004456 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004457 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004458 if (hostapd_ctrl_iface_acl_del_mac(
4459 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004460 &hapd->conf->num_deny_mac, buf + 17) ||
4461 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004462 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004463 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
4464 reply_len = hostapd_ctrl_iface_acl_show_mac(
4465 hapd->conf->deny_mac,
4466 hapd->conf->num_deny_mac, reply, reply_size);
4467 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
4468 hostapd_ctrl_iface_acl_clear_list(
4469 &hapd->conf->deny_mac,
4470 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07004471 if (hostapd_set_acl(hapd))
4472 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07004473 } else {
4474 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004475 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004476#ifdef CONFIG_DPP
4477 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
4478 res = hostapd_dpp_qr_code(hapd, buf + 12);
4479 if (res < 0) {
4480 reply_len = -1;
4481 } else {
4482 reply_len = os_snprintf(reply, reply_size, "%d", res);
4483 if (os_snprintf_error(reply_size, reply_len))
4484 reply_len = -1;
4485 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004486 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
4487 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
4488 if (res < 0) {
4489 reply_len = -1;
4490 } else {
4491 reply_len = os_snprintf(reply, reply_size, "%d", res);
4492 if (os_snprintf_error(reply_size, reply_len))
4493 reply_len = -1;
4494 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004495 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
4496 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
4497 if (res < 0) {
4498 reply_len = -1;
4499 } else {
4500 reply_len = os_snprintf(reply, reply_size, "%d", res);
4501 if (os_snprintf_error(reply_size, reply_len))
4502 reply_len = -1;
4503 }
4504 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
4505 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
4506 if (res < 0) {
4507 reply_len = -1;
4508 } else {
4509 reply_len = os_snprintf(reply, reply_size, "%d", res);
4510 if (os_snprintf_error(reply_size, reply_len))
4511 reply_len = -1;
4512 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004513 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004514 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004515 if (res < 0) {
4516 reply_len = -1;
4517 } else {
4518 reply_len = os_snprintf(reply, reply_size, "%d", res);
4519 if (os_snprintf_error(reply_size, reply_len))
4520 reply_len = -1;
4521 }
4522 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004523 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
4524 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004525 reply_len = -1;
4526 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
4527 const char *uri;
4528
Hai Shalom021b0b52019-04-10 11:17:58 -07004529 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
4530 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004531 if (!uri) {
4532 reply_len = -1;
4533 } else {
4534 reply_len = os_snprintf(reply, reply_size, "%s", uri);
4535 if (os_snprintf_error(reply_size, reply_len))
4536 reply_len = -1;
4537 }
4538 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004539 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
4540 atoi(buf + 19),
4541 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07004542 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
4543 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
4544 atoi(buf + 18),
4545 os_strchr(buf + 18, ' ')) < 0)
4546 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004547 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
4548 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
4549 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004550 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
4551 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
4552 reply_len = -1;
4553 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
4554 hostapd_dpp_stop(hapd);
4555 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004556 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004557 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
4558 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004559 if (res < 0) {
4560 reply_len = -1;
4561 } else {
4562 reply_len = os_snprintf(reply, reply_size, "%d", res);
4563 if (os_snprintf_error(reply_size, reply_len))
4564 reply_len = -1;
4565 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004566 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
4567 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
4568 buf + 20) < 0)
4569 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004570 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004571 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
4572 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004573 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004574 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004575 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004576 reply_len = -1;
4577 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004578 reply_len = dpp_configurator_get_key_id(
4579 hapd->iface->interfaces->dpp,
4580 atoi(buf + 25),
4581 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004582 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
4583 res = hostapd_dpp_pkex_add(hapd, buf + 12);
4584 if (res < 0) {
4585 reply_len = -1;
4586 } else {
4587 reply_len = os_snprintf(reply, reply_size, "%d", res);
4588 if (os_snprintf_error(reply_size, reply_len))
4589 reply_len = -1;
4590 }
4591 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
4592 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
4593 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004594#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07004595 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
4596 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
4597 reply_len = -1;
4598 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
4599 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
4600 reply_len = -1;
4601 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
4602 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004603 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
4604 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
4605 reply_len = -1;
4606 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
4607 hostapd_dpp_chirp_stop(hapd);
Sunil Ravi89eba102022-09-13 21:04:37 -07004608 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
4609 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
4610 reply_len = -1;
4611 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
4612 hostapd_dpp_remove_controller(hapd, buf + 28);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004613#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07004614#ifdef CONFIG_DPP3
4615 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
4616 if (hostapd_dpp_push_button(hapd, NULL) < 0)
4617 reply_len = -1;
4618 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
4619 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
4620 reply_len = -1;
4621#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004622#endif /* CONFIG_DPP */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004623#ifdef CONFIG_NAN_USD
4624 } else if (os_strncmp(buf, "NAN_PUBLISH ", 12) == 0) {
4625 reply_len = hostapd_ctrl_nan_publish(hapd, buf + 12, reply,
4626 reply_size);
4627 } else if (os_strncmp(buf, "NAN_CANCEL_PUBLISH ", 19) == 0) {
4628 if (hostapd_ctrl_nan_cancel_publish(hapd, buf + 19) < 0)
4629 reply_len = -1;
4630 } else if (os_strncmp(buf, "NAN_UPDATE_PUBLISH ", 19) == 0) {
4631 if (hostapd_ctrl_nan_update_publish(hapd, buf + 19) < 0)
4632 reply_len = -1;
4633 } else if (os_strncmp(buf, "NAN_SUBSCRIBE ", 14) == 0) {
4634 reply_len = hostapd_ctrl_nan_subscribe(hapd, buf + 14, reply,
4635 reply_size);
4636 } else if (os_strncmp(buf, "NAN_CANCEL_SUBSCRIBE ", 21) == 0) {
4637 if (hostapd_ctrl_nan_cancel_subscribe(hapd, buf + 21) < 0)
4638 reply_len = -1;
4639 } else if (os_strncmp(buf, "NAN_TRANSMIT ", 13) == 0) {
4640 if (hostapd_ctrl_nan_transmit(hapd, buf + 13) < 0)
4641 reply_len = -1;
4642#endif /* CONFIG_NAN_USD */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004643#ifdef RADIUS_SERVER
4644 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
4645 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
4646 reply_len = -1;
4647#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07004648 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4649 reply_len = hostapd_ctrl_iface_get_capability(
4650 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08004651#ifdef CONFIG_PASN
4652 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
4653 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
4654#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08004655#ifdef ANDROID
4656 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
4657 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
4658 reply_size);
4659#endif /* ANDROID */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004660#ifdef CONFIG_IEEE80211BE
4661 } else if (os_strcmp(buf, "ENABLE_MLD") == 0) {
4662 if (hostapd_ctrl_iface_enable_mld(hapd->iface))
4663 reply_len = -1;
4664 } else if (os_strcmp(buf, "DISABLE_MLD") == 0) {
4665 if (hostapd_ctrl_iface_disable_mld(hapd->iface))
4666 reply_len = -1;
4667#ifdef CONFIG_TESTING_OPTIONS
4668 } else if (os_strncmp(buf, "LINK_REMOVE ", 12) == 0) {
4669 if (hostapd_ctrl_iface_link_remove(hapd, buf + 12,
4670 reply, reply_size))
4671 reply_len = -1;
4672#endif /* CONFIG_TESTING_OPTIONS */
4673#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 } else {
4675 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4676 reply_len = 16;
4677 }
4678
4679 if (reply_len < 0) {
4680 os_memcpy(reply, "FAIL\n", 5);
4681 reply_len = 5;
4682 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004683
4684 return reply_len;
4685}
4686
4687
4688static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
4689 void *sock_ctx)
4690{
4691 struct hostapd_data *hapd = eloop_ctx;
4692 char buf[4096];
4693 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004694 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004695 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004696 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004697 const int reply_size = 4096;
4698 int reply_len;
4699 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004700#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004701 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004702#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004703
4704 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4705 (struct sockaddr *) &from, &fromlen);
4706 if (res < 0) {
4707 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4708 strerror(errno));
4709 return;
4710 }
4711 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004712
4713 reply = os_malloc(reply_size);
4714 if (reply == NULL) {
4715 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4716 fromlen) < 0) {
4717 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4718 strerror(errno));
4719 }
4720 return;
4721 }
4722
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004723#ifdef CONFIG_CTRL_IFACE_UDP
4724 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4725 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004726 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4727 hapd->ctrl_iface_cookie,
4728 CTRL_IFACE_COOKIE_LEN);
4729 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004730 goto done;
4731 }
4732
4733 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004734 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004735 wpa_printf(MSG_DEBUG,
4736 "CTRL: No cookie in the request - drop request");
4737 os_free(reply);
4738 return;
4739 }
4740
Hai Shalomfdcde762020-04-02 11:19:20 -07004741 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
4742 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004743 wpa_printf(MSG_DEBUG,
4744 "CTRL: Invalid cookie in the request - drop request");
4745 os_free(reply);
4746 return;
4747 }
4748
Hai Shalomfdcde762020-04-02 11:19:20 -07004749 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004750 while (*pos == ' ')
4751 pos++;
4752#endif /* CONFIG_CTRL_IFACE_UDP */
4753
4754 if (os_strcmp(pos, "PING") == 0)
4755 level = MSG_EXCESSIVE;
4756 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
4757
4758 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004759 reply, reply_size,
4760 &from, fromlen);
4761
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004762#ifdef CONFIG_CTRL_IFACE_UDP
4763done:
4764#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004765 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4766 fromlen) < 0) {
4767 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4768 strerror(errno));
4769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 os_free(reply);
4771}
4772
4773
Sunil Ravic0f5d412024-09-11 22:12:49 +00004774#ifdef CONFIG_IEEE80211BE
4775#ifndef CONFIG_CTRL_IFACE_UDP
4776
4777static int hostapd_mld_ctrl_iface_receive_process(struct hostapd_mld *mld,
4778 char *buf, char *reply,
4779 size_t reply_size,
4780 struct sockaddr_storage *from,
4781 socklen_t fromlen)
4782{
4783 struct hostapd_data *link_hapd, *link_itr;
4784 int reply_len = -1, link_id = -1;
4785 char *cmd;
4786 bool found = false;
4787
4788 os_memcpy(reply, "OK\n", 3);
4789 reply_len = 3;
4790
4791 cmd = buf;
4792
4793 /* Check whether the link ID is provided in the command */
4794 if (os_strncmp(cmd, "LINKID ", 7) == 0) {
4795 cmd += 7;
4796 link_id = atoi(cmd);
4797 if (link_id < 0 || link_id >= 15) {
4798 os_memcpy(reply, "INVALID LINK ID\n", 16);
4799 reply_len = 16;
4800 goto out;
4801 }
4802
4803 cmd = os_strchr(cmd, ' ');
4804 if (!cmd)
4805 goto out;
4806 cmd++;
4807 }
4808 if (link_id >= 0) {
4809 link_hapd = mld->fbss;
4810 if (!link_hapd) {
4811 os_memcpy(reply, "NO LINKS ACTIVE\n", 16);
4812 reply_len = 16;
4813 goto out;
4814 }
4815
4816 for_each_mld_link(link_itr, link_hapd) {
4817 if (link_itr->mld_link_id == link_id) {
4818 found = true;
4819 break;
4820 }
4821 }
4822
4823 if (!found)
4824 goto out;
4825
4826 link_hapd = link_itr;
4827 } else {
4828 link_hapd = mld->fbss;
4829 }
4830
4831 if (os_strcmp(cmd, "PING") == 0) {
4832 os_memcpy(reply, "PONG\n", 5);
4833 reply_len = 5;
4834 } else if (os_strcmp(cmd, "ATTACH") == 0) {
4835 if (ctrl_iface_attach(&mld->ctrl_dst, from, fromlen, NULL))
4836 reply_len = -1;
4837 } else if (os_strncmp(cmd, "ATTACH ", 7) == 0) {
4838 if (ctrl_iface_attach(&mld->ctrl_dst, from, fromlen, cmd + 7))
4839 reply_len = -1;
4840 } else if (os_strcmp(cmd, "DETACH") == 0) {
4841 if (ctrl_iface_detach(&mld->ctrl_dst, from, fromlen))
4842 reply_len = -1;
4843 } else {
4844 if (link_id == -1)
4845 wpa_printf(MSG_DEBUG,
4846 "Link ID not provided, using the first link BSS (if available)");
4847
4848 if (!link_hapd)
4849 reply_len = -1;
4850 else
4851 reply_len =
4852 hostapd_ctrl_iface_receive_process(
4853 link_hapd, cmd, reply, reply_size,
4854 from, fromlen);
4855 }
4856
4857out:
4858 if (reply_len < 0) {
4859 os_memcpy(reply, "FAIL\n", 5);
4860 reply_len = 5;
4861 }
4862
4863 return reply_len;
4864}
4865
4866
4867static void hostapd_mld_ctrl_iface_receive(int sock, void *eloop_ctx,
4868 void *sock_ctx)
4869{
4870 struct hostapd_mld *mld = eloop_ctx;
4871 char buf[4096];
4872 int res;
4873 struct sockaddr_storage from;
4874 socklen_t fromlen = sizeof(from);
4875 char *reply, *pos = buf;
4876 const size_t reply_size = 4096;
4877 int reply_len;
4878 int level = MSG_DEBUG;
4879
4880 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4881 (struct sockaddr *) &from, &fromlen);
4882 if (res < 0) {
4883 wpa_printf(MSG_ERROR, "recvfrom(mld ctrl_iface): %s",
4884 strerror(errno));
4885 return;
4886 }
4887 buf[res] = '\0';
4888
4889 reply = os_malloc(reply_size);
4890 if (!reply) {
4891 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4892 fromlen) < 0) {
4893 wpa_printf(MSG_DEBUG, "MLD CTRL: sendto failed: %s",
4894 strerror(errno));
4895 }
4896 return;
4897 }
4898
4899 if (os_strcmp(pos, "PING") == 0)
4900 level = MSG_EXCESSIVE;
4901
4902 wpa_hexdump_ascii(level, "RX MLD ctrl_iface", pos, res);
4903
4904 reply_len = hostapd_mld_ctrl_iface_receive_process(mld, pos,
4905 reply, reply_size,
4906 &from, fromlen);
4907
4908 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4909 fromlen) < 0) {
4910 wpa_printf(MSG_DEBUG, "MLD CTRL: sendto failed: %s",
4911 strerror(errno));
4912 }
4913 os_free(reply);
4914}
4915
4916
4917static char * hostapd_mld_ctrl_iface_path(struct hostapd_mld *mld)
4918{
4919 size_t len;
4920 char *buf;
4921 int ret;
4922
4923 if (!mld->ctrl_interface)
4924 return NULL;
4925
4926 len = os_strlen(mld->ctrl_interface) + os_strlen(mld->name) + 2;
4927
4928 buf = os_malloc(len);
4929 if (!buf)
4930 return NULL;
4931
4932 ret = os_snprintf(buf, len, "%s/%s", mld->ctrl_interface, mld->name);
4933 if (os_snprintf_error(len, ret)) {
4934 os_free(buf);
4935 return NULL;
4936 }
4937
4938 return buf;
4939}
4940
4941#endif /* !CONFIG_CTRL_IFACE_UDP */
4942
4943
4944int hostapd_mld_ctrl_iface_init(struct hostapd_mld *mld)
4945{
4946#ifndef CONFIG_CTRL_IFACE_UDP
4947 struct sockaddr_un addr;
4948 int s = -1;
4949 char *fname = NULL;
4950
4951 if (!mld)
4952 return -1;
4953
4954 if (mld->ctrl_sock > -1) {
4955 wpa_printf(MSG_DEBUG, "MLD %s ctrl_iface already exists!",
4956 mld->name);
4957 return 0;
4958 }
4959
4960 dl_list_init(&mld->ctrl_dst);
4961
4962 if (!mld->ctrl_interface)
4963 return 0;
4964
4965 if (mkdir(mld->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4966 if (errno == EEXIST) {
4967 wpa_printf(MSG_DEBUG,
4968 "Using existing control interface directory.");
4969 } else {
4970 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4971 strerror(errno));
4972 goto fail;
4973 }
4974 }
4975
4976 if (os_strlen(mld->ctrl_interface) + 1 + os_strlen(mld->name) >=
4977 sizeof(addr.sun_path))
4978 goto fail;
4979
4980 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4981 if (s < 0) {
4982 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
4983 goto fail;
4984 }
4985
4986 os_memset(&addr, 0, sizeof(addr));
4987#ifdef __FreeBSD__
4988 addr.sun_len = sizeof(addr);
4989#endif /* __FreeBSD__ */
4990 addr.sun_family = AF_UNIX;
4991
4992 fname = hostapd_mld_ctrl_iface_path(mld);
4993 if (!fname)
4994 goto fail;
4995
4996 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4997
4998 wpa_printf(MSG_DEBUG, "Setting up MLD %s ctrl_iface", mld->name);
4999
5000 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5001 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5002 strerror(errno));
5003 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5004 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not allow connections - assuming it was left over from forced program termination");
5005 if (unlink(fname) < 0) {
5006 wpa_printf(MSG_ERROR,
5007 "Could not unlink existing ctrl_iface socket '%s': %s",
5008 fname, strerror(errno));
5009 goto fail;
5010 }
5011 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5012 0) {
5013 wpa_printf(MSG_ERROR,
5014 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
5015 strerror(errno));
5016 goto fail;
5017 }
5018 wpa_printf(MSG_DEBUG,
5019 "Successfully replaced leftover ctrl_iface socket '%s'",
5020 fname);
5021 } else {
5022 wpa_printf(MSG_INFO,
5023 "ctrl_iface exists and seems to be in use - cannot override it");
5024 wpa_printf(MSG_INFO,
5025 "Delete '%s' manually if it is not used anymore", fname);
5026 os_free(fname);
5027 fname = NULL;
5028 goto fail;
5029 }
5030 }
5031
5032 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
5033 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5034 strerror(errno));
5035 goto fail;
5036 }
5037 os_free(fname);
5038
5039 mld->ctrl_sock = s;
5040
5041 if (eloop_register_read_sock(s, hostapd_mld_ctrl_iface_receive, mld,
5042 NULL) < 0)
5043 return -1;
5044
5045 return 0;
5046
5047fail:
5048 if (s >= 0)
5049 close(s);
5050 if (fname) {
5051 unlink(fname);
5052 os_free(fname);
5053 }
5054 return -1;
5055#endif /* !CONFIG_CTRL_IFACE_UDP */
5056 return 0;
5057}
5058
5059
5060void hostapd_mld_ctrl_iface_deinit(struct hostapd_mld *mld)
5061{
5062#ifndef CONFIG_CTRL_IFACE_UDP
5063 struct wpa_ctrl_dst *dst, *prev;
5064
5065 if (mld->ctrl_sock > -1) {
5066 char *fname;
5067
5068 eloop_unregister_read_sock(mld->ctrl_sock);
5069 close(mld->ctrl_sock);
5070 mld->ctrl_sock = -1;
5071
5072 fname = hostapd_mld_ctrl_iface_path(mld);
5073 if (fname) {
5074 unlink(fname);
5075 os_free(fname);
5076 }
5077
5078 if (mld->ctrl_interface &&
5079 rmdir(mld->ctrl_interface) < 0) {
5080 if (errno == ENOTEMPTY) {
5081 wpa_printf(MSG_DEBUG,
5082 "MLD control interface directory not empty - leaving it behind");
5083 } else {
5084 wpa_printf(MSG_ERROR,
5085 "rmdir[ctrl_interface=%s]: %s",
5086 mld->ctrl_interface,
5087 strerror(errno));
5088 }
5089 }
5090 }
5091
5092 dl_list_for_each_safe(dst, prev, &mld->ctrl_dst, struct wpa_ctrl_dst,
5093 list)
5094 os_free(dst);
5095#endif /* !CONFIG_CTRL_IFACE_UDP */
5096
5097 os_free(mld->ctrl_interface);
5098}
5099
5100#endif /* CONFIG_IEEE80211BE */
5101
5102
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005103#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
5105{
5106 char *buf;
5107 size_t len;
Sunil Ravic0f5d412024-09-11 22:12:49 +00005108 const char *ctrl_sock_iface;
5109
5110#ifdef CONFIG_IEEE80211BE
5111 ctrl_sock_iface = hapd->ctrl_sock_iface;
5112#else /* CONFIG_IEEE80211BE */
5113 ctrl_sock_iface = hapd->conf->iface;
5114#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005115
5116 if (hapd->conf->ctrl_interface == NULL)
5117 return NULL;
5118
5119 len = os_strlen(hapd->conf->ctrl_interface) +
Sunil Ravic0f5d412024-09-11 22:12:49 +00005120 os_strlen(ctrl_sock_iface) + 2;
5121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 buf = os_malloc(len);
5123 if (buf == NULL)
5124 return NULL;
5125
5126 os_snprintf(buf, len, "%s/%s",
Sunil Ravic0f5d412024-09-11 22:12:49 +00005127 hapd->conf->ctrl_interface, ctrl_sock_iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005128 buf[len - 1] = '\0';
5129 return buf;
5130}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005131#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132
5133
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07005134static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
5135 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136 const char *txt, size_t len)
5137{
5138 struct hostapd_data *hapd = ctx;
5139 if (hapd == NULL)
5140 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005141 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005142}
5143
5144
5145int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
5146{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005147#ifdef CONFIG_CTRL_IFACE_UDP
5148 int port = HOSTAPD_CTRL_IFACE_PORT;
5149 char p[32] = { 0 };
5150 char port_str[40], *tmp;
5151 char *pos;
5152 struct addrinfo hints = { 0 }, *res, *saveres;
5153 int n;
5154
5155 if (hapd->ctrl_sock > -1) {
5156 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
5157 return 0;
5158 }
5159
5160 if (hapd->conf->ctrl_interface == NULL)
5161 return 0;
5162
5163 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
5164 if (pos) {
5165 pos += 4;
5166 port = atoi(pos);
5167 if (port <= 0) {
5168 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
5169 goto fail;
5170 }
5171 }
5172
5173 dl_list_init(&hapd->ctrl_dst);
5174 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07005175 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005176
5177#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
5178 hints.ai_flags = AI_PASSIVE;
5179#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
5180
5181#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
5182 hints.ai_family = AF_INET6;
5183#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5184 hints.ai_family = AF_INET;
5185#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5186 hints.ai_socktype = SOCK_DGRAM;
5187
5188try_again:
5189 os_snprintf(p, sizeof(p), "%d", port);
5190 n = getaddrinfo(NULL, p, &hints, &res);
5191 if (n) {
5192 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
5193 goto fail;
5194 }
5195
5196 saveres = res;
5197 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
5198 res->ai_protocol);
5199 if (hapd->ctrl_sock < 0) {
5200 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
5201 goto fail;
5202 }
5203
5204 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
5205 port--;
5206 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
5207 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
5208 goto try_again;
5209 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
5210 goto fail;
5211 }
5212
5213 freeaddrinfo(saveres);
5214
5215 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
5216 tmp = os_strdup(port_str);
5217 if (tmp) {
5218 os_free(hapd->conf->ctrl_interface);
5219 hapd->conf->ctrl_interface = tmp;
5220 }
5221 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
5222
5223 if (eloop_register_read_sock(hapd->ctrl_sock,
5224 hostapd_ctrl_iface_receive, hapd, NULL) <
5225 0) {
5226 hostapd_ctrl_iface_deinit(hapd);
5227 return -1;
5228 }
5229
5230 hapd->msg_ctx = hapd;
5231 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5232
5233 return 0;
5234
5235fail:
5236 if (hapd->ctrl_sock >= 0)
5237 close(hapd->ctrl_sock);
5238 return -1;
5239#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240 struct sockaddr_un addr;
5241 int s = -1;
5242 char *fname = NULL;
Sunil Ravic0f5d412024-09-11 22:12:49 +00005243 size_t iflen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005244
Dmitry Shmidt04949592012-07-19 12:16:46 -07005245 if (hapd->ctrl_sock > -1) {
5246 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
5247 return 0;
5248 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005249
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005250 dl_list_init(&hapd->ctrl_dst);
5251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005252 if (hapd->conf->ctrl_interface == NULL)
5253 return 0;
5254
5255 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
5256 if (errno == EEXIST) {
5257 wpa_printf(MSG_DEBUG, "Using existing control "
5258 "interface directory.");
5259 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005260 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
5261 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005262 goto fail;
5263 }
5264 }
5265
5266 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005267 lchown(hapd->conf->ctrl_interface, -1,
5268 hapd->conf->ctrl_interface_gid) < 0) {
5269 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005270 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005271 return -1;
5272 }
5273
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005274 if (!hapd->conf->ctrl_interface_gid_set &&
5275 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005276 lchown(hapd->conf->ctrl_interface, -1,
5277 hapd->iface->interfaces->ctrl_iface_group) < 0) {
5278 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005279 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005280 return -1;
5281 }
5282
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005283#ifdef ANDROID
5284 /*
5285 * Android is using umask 0077 which would leave the control interface
5286 * directory without group access. This breaks things since Wi-Fi
5287 * framework assumes that this directory can be accessed by other
5288 * applications in the wifi group. Fix this by adding group access even
5289 * if umask value would prevent this.
5290 */
5291 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
5292 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
5293 strerror(errno));
5294 /* Try to continue anyway */
5295 }
5296#endif /* ANDROID */
5297
Sunil Ravic0f5d412024-09-11 22:12:49 +00005298#ifdef CONFIG_IEEE80211BE
5299 iflen = os_strlen(hapd->ctrl_sock_iface);
5300#else /* CONFIG_IEEE80211BE */
5301 iflen = os_strlen(hapd->conf->iface);
5302#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005303 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
Sunil Ravic0f5d412024-09-11 22:12:49 +00005304 iflen >= sizeof(addr.sun_path))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305 goto fail;
5306
5307 s = socket(PF_UNIX, SOCK_DGRAM, 0);
5308 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005309 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310 goto fail;
5311 }
5312
5313 os_memset(&addr, 0, sizeof(addr));
5314#ifdef __FreeBSD__
5315 addr.sun_len = sizeof(addr);
5316#endif /* __FreeBSD__ */
5317 addr.sun_family = AF_UNIX;
5318 fname = hostapd_ctrl_iface_path(hapd);
5319 if (fname == NULL)
5320 goto fail;
5321 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
5322 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5323 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5324 strerror(errno));
5325 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5326 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
5327 " allow connections - assuming it was left"
5328 "over from forced program termination");
5329 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005330 wpa_printf(MSG_ERROR,
5331 "Could not unlink existing ctrl_iface socket '%s': %s",
5332 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333 goto fail;
5334 }
5335 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5336 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005337 wpa_printf(MSG_ERROR,
5338 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
5339 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340 goto fail;
5341 }
5342 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
5343 "ctrl_iface socket '%s'", fname);
5344 } else {
5345 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
5346 "be in use - cannot override it");
5347 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
5348 "not used anymore", fname);
5349 os_free(fname);
5350 fname = NULL;
5351 goto fail;
5352 }
5353 }
5354
5355 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005356 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
5357 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005358 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359 goto fail;
5360 }
5361
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005362 if (!hapd->conf->ctrl_interface_gid_set &&
5363 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005364 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
5365 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005366 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005367 goto fail;
5368 }
5369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005371 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5372 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373 goto fail;
5374 }
5375 os_free(fname);
5376
5377 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005378 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
5379 NULL) < 0) {
5380 hostapd_ctrl_iface_deinit(hapd);
5381 return -1;
5382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383 hapd->msg_ctx = hapd;
5384 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5385
5386 return 0;
5387
5388fail:
5389 if (s >= 0)
5390 close(s);
5391 if (fname) {
5392 unlink(fname);
5393 os_free(fname);
5394 }
5395 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005396#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005397}
5398
5399
5400void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
5401{
5402 struct wpa_ctrl_dst *dst, *prev;
5403
5404 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005405#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005407#endif /* !CONFIG_CTRL_IFACE_UDP */
5408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005409 eloop_unregister_read_sock(hapd->ctrl_sock);
5410 close(hapd->ctrl_sock);
5411 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005412#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005413 fname = hostapd_ctrl_iface_path(hapd);
5414 if (fname)
5415 unlink(fname);
5416 os_free(fname);
5417
5418 if (hapd->conf->ctrl_interface &&
5419 rmdir(hapd->conf->ctrl_interface) < 0) {
5420 if (errno == ENOTEMPTY) {
5421 wpa_printf(MSG_DEBUG, "Control interface "
5422 "directory not empty - leaving it "
5423 "behind");
5424 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005425 wpa_printf(MSG_ERROR,
5426 "rmdir[ctrl_interface=%s]: %s",
5427 hapd->conf->ctrl_interface,
5428 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005429 }
5430 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005431#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432 }
5433
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005434 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
5435 list)
5436 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005437
5438#ifdef CONFIG_TESTING_OPTIONS
5439 l2_packet_deinit(hapd->l2_test);
5440 hapd->l2_test = NULL;
5441#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442}
5443
5444
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005445static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
5446 char *buf)
5447{
5448 if (hostapd_add_iface(interfaces, buf) < 0) {
5449 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
5450 return -1;
5451 }
5452 return 0;
5453}
5454
5455
5456static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
5457 char *buf)
5458{
5459 if (hostapd_remove_iface(interfaces, buf) < 0) {
5460 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
5461 return -1;
5462 }
5463 return 0;
5464}
5465
5466
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005467static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005468 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005469 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005470{
Roshan Pius3a1667e2018-07-03 15:17:14 -07005471 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
5472 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005473}
5474
5475
5476static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005477 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005478 socklen_t fromlen)
5479{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005480 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005481}
5482
5483
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005484static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
5485{
5486#ifdef CONFIG_WPS_TESTING
5487 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08005488 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005489 wps_corrupt_pkhash = 0;
5490#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07005491
5492#ifdef CONFIG_TESTING_OPTIONS
5493#ifdef CONFIG_DPP
5494 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08005495#ifdef CONFIG_DPP3
5496 dpp_version_override = 3;
5497#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07005498 dpp_version_override = 2;
5499#else /* CONFIG_DPP2 */
5500 dpp_version_override = 1;
5501#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07005502#endif /* CONFIG_DPP */
5503#endif /* CONFIG_TESTING_OPTIONS */
5504
5505#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07005506 dpp_global_clear(interfaces->dpp);
Sunil Ravi89eba102022-09-13 21:04:37 -07005507#ifdef CONFIG_DPP3
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005508 interfaces->dpp_pb_bi = NULL;
Sunil Ravi89eba102022-09-13 21:04:37 -07005509 {
5510 int i;
5511
5512 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
5513 struct dpp_pb_info *info;
5514
5515 info = &interfaces->dpp_pb[i];
5516 info->rx_time.sec = 0;
5517 info->rx_time.usec = 0;
5518 }
5519 }
5520#endif /* CONFIG_DPP3 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07005521#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005522}
5523
5524
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005525#ifdef CONFIG_FST
5526
5527static int
5528hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
5529 const char *cmd)
5530{
5531 char ifname[IFNAMSIZ + 1];
5532 struct fst_iface_cfg cfg;
5533 struct hostapd_data *hapd;
5534 struct fst_wpa_obj iface_obj;
5535
5536 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
5537 hapd = hostapd_get_iface(interfaces, ifname);
5538 if (hapd) {
5539 if (hapd->iface->fst) {
5540 wpa_printf(MSG_INFO, "FST: Already attached");
5541 return -1;
5542 }
5543 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
5544 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
5545 &iface_obj, &cfg);
5546 if (hapd->iface->fst)
5547 return 0;
5548 }
5549 }
5550
5551 return -EINVAL;
5552}
5553
5554
5555static int
5556hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
5557 const char *cmd)
5558{
5559 char ifname[IFNAMSIZ + 1];
5560 struct hostapd_data * hapd;
5561
5562 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
5563 hapd = hostapd_get_iface(interfaces, ifname);
5564 if (hapd) {
5565 if (!fst_iface_detach(ifname)) {
5566 hapd->iface->fst = NULL;
5567 hapd->iface->fst_ies = NULL;
5568 return 0;
5569 }
5570 }
5571 }
5572
5573 return -EINVAL;
5574}
5575
5576#endif /* CONFIG_FST */
5577
5578
5579static struct hostapd_data *
5580hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
5581 const char *ifname)
5582{
5583 size_t i, j;
5584
5585 for (i = 0; i < interfaces->count; i++) {
5586 struct hostapd_iface *iface = interfaces->iface[i];
5587
5588 for (j = 0; j < iface->num_bss; j++) {
5589 struct hostapd_data *hapd;
5590
5591 hapd = iface->bss[j];
5592 if (os_strcmp(ifname, hapd->conf->iface) == 0)
5593 return hapd;
5594 }
5595 }
5596
5597 return NULL;
5598}
5599
5600
5601static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
5602 struct hostapd_data *dst_hapd,
5603 const char *param)
5604{
5605 int res;
5606 char *value;
5607
5608 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5609 if (!value) {
5610 wpa_printf(MSG_ERROR,
5611 "DUP: cannot allocate buffer to stringify %s",
5612 param);
5613 goto error_return;
5614 }
5615
5616 if (os_strcmp(param, "wpa") == 0) {
5617 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
5618 src_hapd->conf->wpa);
5619 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
5620 src_hapd->conf->wpa_key_mgmt) {
5621 res = hostapd_ctrl_iface_get_key_mgmt(
5622 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5623 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
5624 goto error_stringify;
5625 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
5626 src_hapd->conf->wpa_pairwise) {
5627 res = wpa_write_ciphers(value,
5628 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5629 src_hapd->conf->wpa_pairwise, " ");
5630 if (res < 0)
5631 goto error_stringify;
5632 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
5633 src_hapd->conf->rsn_pairwise) {
5634 res = wpa_write_ciphers(value,
5635 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5636 src_hapd->conf->rsn_pairwise, " ");
5637 if (res < 0)
5638 goto error_stringify;
5639 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
5640 src_hapd->conf->ssid.wpa_passphrase) {
5641 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
5642 src_hapd->conf->ssid.wpa_passphrase);
5643 } else if (os_strcmp(param, "wpa_psk") == 0 &&
5644 src_hapd->conf->ssid.wpa_psk_set) {
5645 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5646 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
5647 } else {
5648 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
5649 goto error_return;
5650 }
5651
5652 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
5653 os_free(value);
5654 return res;
5655
5656error_stringify:
5657 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
5658error_return:
5659 os_free(value);
5660 return -1;
5661}
5662
5663
5664static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005665hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
5666 const char *input,
5667 char *reply, int reply_size)
5668{
5669 size_t i, j;
5670 int res;
5671 char *pos, *end;
5672 struct hostapd_iface *iface;
5673 int show_ctrl = 0;
5674
5675 if (input)
5676 show_ctrl = !!os_strstr(input, "ctrl");
5677
5678 pos = reply;
5679 end = reply + reply_size;
5680
5681 for (i = 0; i < interfaces->count; i++) {
5682 iface = interfaces->iface[i];
5683
5684 for (j = 0; j < iface->num_bss; j++) {
5685 struct hostapd_bss_config *conf;
5686
5687 conf = iface->conf->bss[j];
5688 if (show_ctrl)
5689 res = os_snprintf(pos, end - pos,
5690 "%s ctrl_iface=%s\n",
5691 conf->iface,
5692 conf->ctrl_interface ?
5693 conf->ctrl_interface : "N/A");
5694 else
5695 res = os_snprintf(pos, end - pos, "%s\n",
5696 conf->iface);
5697 if (os_snprintf_error(end - pos, res)) {
5698 *pos = '\0';
5699 return pos - reply;
5700 }
5701 pos += res;
5702 }
5703 }
5704
5705 return pos - reply;
5706}
5707
5708
5709static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005710hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
5711 char *cmd)
5712{
5713 char *p_start = cmd, *p_end;
5714 struct hostapd_data *src_hapd, *dst_hapd;
5715
5716 /* cmd: "<src ifname> <dst ifname> <variable name> */
5717
5718 p_end = os_strchr(p_start, ' ');
5719 if (!p_end) {
5720 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
5721 cmd);
5722 return -1;
5723 }
5724
5725 *p_end = '\0';
5726 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5727 if (!src_hapd) {
5728 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
5729 p_start);
5730 return -1;
5731 }
5732
5733 p_start = p_end + 1;
5734 p_end = os_strchr(p_start, ' ');
5735 if (!p_end) {
5736 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
5737 cmd);
5738 return -1;
5739 }
5740
5741 *p_end = '\0';
5742 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5743 if (!dst_hapd) {
5744 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
5745 p_start);
5746 return -1;
5747 }
5748
5749 p_start = p_end + 1;
5750 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
5751}
5752
5753
5754static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
5755 const char *ifname,
5756 char *buf, char *reply,
5757 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005758 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005759 socklen_t fromlen)
5760{
5761 struct hostapd_data *hapd;
5762
5763 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
5764 if (hapd == NULL) {
5765 int res;
5766
5767 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
5768 if (os_snprintf_error(reply_size, res))
5769 return -1;
5770 return res;
5771 }
5772
5773 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
5774 from, fromlen);
5775}
5776
5777
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005778static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
5779 void *sock_ctx)
5780{
Hai Shalomfdcde762020-04-02 11:19:20 -07005781 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005782 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005783 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005784 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005785 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005786 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005787 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005788 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005789#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07005790 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005791#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005792
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005793 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005794 (struct sockaddr *) &from, &fromlen);
5795 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005796 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
5797 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005798 return;
5799 }
5800 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005801 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005802
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005803 reply = os_malloc(reply_size);
5804 if (reply == NULL) {
5805 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
5806 fromlen) < 0) {
5807 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5808 strerror(errno));
5809 }
5810 return;
5811 }
5812
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005813 os_memcpy(reply, "OK\n", 3);
5814 reply_len = 3;
5815
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005816#ifdef CONFIG_CTRL_IFACE_UDP
5817 if (os_strcmp(buf, "GET_COOKIE") == 0) {
5818 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07005819 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
5820 interfaces->ctrl_iface_cookie,
5821 CTRL_IFACE_COOKIE_LEN);
5822 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005823 goto send_reply;
5824 }
5825
5826 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07005827 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005828 wpa_printf(MSG_DEBUG,
5829 "CTRL: No cookie in the request - drop request");
5830 os_free(reply);
5831 return;
5832 }
5833
Hai Shalomfdcde762020-04-02 11:19:20 -07005834 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
5835 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005836 wpa_printf(MSG_DEBUG,
5837 "CTRL: Invalid cookie in the request - drop request");
5838 os_free(reply);
5839 return;
5840 }
5841
Hai Shalomfdcde762020-04-02 11:19:20 -07005842 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005843 while (*buf == ' ')
5844 buf++;
5845#endif /* CONFIG_CTRL_IFACE_UDP */
5846
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005847 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
5848 char *pos = os_strchr(buf + 7, ' ');
5849
5850 if (pos) {
5851 *pos++ = '\0';
5852 reply_len = hostapd_global_ctrl_iface_ifname(
5853 interfaces, buf + 7, pos, reply, reply_size,
5854 &from, fromlen);
5855 goto send_reply;
5856 }
5857 }
5858
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005859 if (os_strcmp(buf, "PING") == 0) {
5860 os_memcpy(reply, "PONG\n", 5);
5861 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005862 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5863 if (wpa_debug_reopen_file() < 0)
5864 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005865 } else if (os_strcmp(buf, "FLUSH") == 0) {
5866 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005867 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
5868 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
5869 reply_len = -1;
5870 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
5871 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
5872 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005873 } else if (os_strcmp(buf, "ATTACH") == 0) {
5874 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005875 fromlen, NULL))
5876 reply_len = -1;
5877 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
5878 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
5879 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005880 reply_len = -1;
5881 } else if (os_strcmp(buf, "DETACH") == 0) {
5882 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
5883 fromlen))
5884 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005885#ifdef CONFIG_MODULE_TESTS
5886 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005887 if (hapd_module_tests() < 0)
5888 reply_len = -1;
5889#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005890#ifdef CONFIG_FST
5891 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
5892 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
5893 reply_len = os_snprintf(reply, reply_size, "OK\n");
5894 else
5895 reply_len = -1;
5896 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
5897 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
5898 reply_len = os_snprintf(reply, reply_size, "OK\n");
5899 else
5900 reply_len = -1;
5901 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
5902 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
5903#endif /* CONFIG_FST */
5904 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
5905 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
5906 buf + 12))
5907 reply_len = os_snprintf(reply, reply_size, "OK\n");
5908 else
5909 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005910 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
5911 reply_len = hostapd_global_ctrl_iface_interfaces(
Sunil Ravi99c035e2024-07-12 01:42:03 +00005912 interfaces, buf + 10, reply, reply_size);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005913 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5914 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005915 } else {
5916 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
5917 "ignored");
5918 reply_len = -1;
5919 }
5920
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005921send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005922 if (reply_len < 0) {
5923 os_memcpy(reply, "FAIL\n", 5);
5924 reply_len = 5;
5925 }
5926
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005927 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
5928 fromlen) < 0) {
5929 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5930 strerror(errno));
5931 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005932 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005933}
5934
5935
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005936#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005937static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
5938{
5939 char *buf;
5940 size_t len;
5941
5942 if (interface->global_iface_path == NULL)
5943 return NULL;
5944
5945 len = os_strlen(interface->global_iface_path) +
5946 os_strlen(interface->global_iface_name) + 2;
5947 buf = os_malloc(len);
5948 if (buf == NULL)
5949 return NULL;
5950
5951 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
5952 interface->global_iface_name);
5953 buf[len - 1] = '\0';
5954 return buf;
5955}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005956#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005957
5958
5959int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
5960{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005961#ifdef CONFIG_CTRL_IFACE_UDP
5962 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
5963 char p[32] = { 0 };
5964 char *pos;
5965 struct addrinfo hints = { 0 }, *res, *saveres;
5966 int n;
5967
5968 if (interface->global_ctrl_sock > -1) {
5969 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
5970 return 0;
5971 }
5972
5973 if (interface->global_iface_path == NULL)
5974 return 0;
5975
5976 pos = os_strstr(interface->global_iface_path, "udp:");
5977 if (pos) {
5978 pos += 4;
5979 port = atoi(pos);
5980 if (port <= 0) {
5981 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
5982 goto fail;
5983 }
5984 }
5985
Hai Shalomfdcde762020-04-02 11:19:20 -07005986 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005987
5988#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
5989 hints.ai_flags = AI_PASSIVE;
5990#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
5991
5992#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
5993 hints.ai_family = AF_INET6;
5994#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5995 hints.ai_family = AF_INET;
5996#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5997 hints.ai_socktype = SOCK_DGRAM;
5998
5999try_again:
6000 os_snprintf(p, sizeof(p), "%d", port);
6001 n = getaddrinfo(NULL, p, &hints, &res);
6002 if (n) {
6003 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
6004 goto fail;
6005 }
6006
6007 saveres = res;
6008 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
6009 res->ai_protocol);
6010 if (interface->global_ctrl_sock < 0) {
6011 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
6012 goto fail;
6013 }
6014
6015 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
6016 0) {
6017 port++;
6018 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
6019 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
6020 goto try_again;
6021 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
6022 goto fail;
6023 }
6024
6025 freeaddrinfo(saveres);
6026
6027 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
6028
6029 if (eloop_register_read_sock(interface->global_ctrl_sock,
6030 hostapd_global_ctrl_iface_receive,
6031 interface, NULL) < 0) {
6032 hostapd_global_ctrl_iface_deinit(interface);
6033 return -1;
6034 }
6035
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006036 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
6037
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006038 return 0;
6039
6040fail:
6041 if (interface->global_ctrl_sock >= 0)
6042 close(interface->global_ctrl_sock);
6043 return -1;
6044#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006045 struct sockaddr_un addr;
6046 int s = -1;
6047 char *fname = NULL;
6048
6049 if (interface->global_iface_path == NULL) {
6050 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
6051 return 0;
6052 }
6053
6054 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
6055 if (errno == EEXIST) {
6056 wpa_printf(MSG_DEBUG, "Using existing control "
6057 "interface directory.");
6058 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006059 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
6060 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006061 goto fail;
6062 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006063 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08006064 lchown(interface->global_iface_path, -1,
6065 interface->ctrl_iface_group) < 0) {
6066 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006067 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006068 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006069 }
6070
6071 if (os_strlen(interface->global_iface_path) + 1 +
6072 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
6073 goto fail;
6074
6075 s = socket(PF_UNIX, SOCK_DGRAM, 0);
6076 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006077 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006078 goto fail;
6079 }
6080
6081 os_memset(&addr, 0, sizeof(addr));
6082#ifdef __FreeBSD__
6083 addr.sun_len = sizeof(addr);
6084#endif /* __FreeBSD__ */
6085 addr.sun_family = AF_UNIX;
6086 fname = hostapd_global_ctrl_iface_path(interface);
6087 if (fname == NULL)
6088 goto fail;
6089 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
6090 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
6091 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
6092 strerror(errno));
6093 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
6094 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
6095 " allow connections - assuming it was left"
6096 "over from forced program termination");
6097 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006098 wpa_printf(MSG_ERROR,
6099 "Could not unlink existing ctrl_iface socket '%s': %s",
6100 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006101 goto fail;
6102 }
6103 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
6104 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006105 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
6106 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006107 goto fail;
6108 }
6109 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
6110 "ctrl_iface socket '%s'", fname);
6111 } else {
6112 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
6113 "be in use - cannot override it");
6114 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
6115 "not used anymore", fname);
6116 os_free(fname);
6117 fname = NULL;
6118 goto fail;
6119 }
6120 }
6121
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006122 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08006123 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
6124 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006125 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07006126 goto fail;
6127 }
6128
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006129 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006130 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
6131 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006132 goto fail;
6133 }
6134 os_free(fname);
6135
6136 interface->global_ctrl_sock = s;
6137 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
6138 interface, NULL);
6139
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006140 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
6141
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006142 return 0;
6143
6144fail:
6145 if (s >= 0)
6146 close(s);
6147 if (fname) {
6148 unlink(fname);
6149 os_free(fname);
6150 }
6151 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006152#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006153}
6154
6155
6156void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
6157{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006158#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006159 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006160#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02006161 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006162
6163 if (interfaces->global_ctrl_sock > -1) {
6164 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
6165 close(interfaces->global_ctrl_sock);
6166 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006167#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006168 fname = hostapd_global_ctrl_iface_path(interfaces);
6169 if (fname) {
6170 unlink(fname);
6171 os_free(fname);
6172 }
6173
6174 if (interfaces->global_iface_path &&
6175 rmdir(interfaces->global_iface_path) < 0) {
6176 if (errno == ENOTEMPTY) {
6177 wpa_printf(MSG_DEBUG, "Control interface "
6178 "directory not empty - leaving it "
6179 "behind");
6180 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006181 wpa_printf(MSG_ERROR,
6182 "rmdir[ctrl_interface=%s]: %s",
6183 interfaces->global_iface_path,
6184 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006185 }
6186 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006187#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02006188 }
6189
6190 os_free(interfaces->global_iface_path);
6191 interfaces->global_iface_path = NULL;
6192
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006193 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
6194 struct wpa_ctrl_dst, list)
6195 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006196}
6197
6198
Roshan Pius3a1667e2018-07-03 15:17:14 -07006199static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
6200 const char *buf)
6201{
6202 /* Enable Probe Request events based on explicit request.
6203 * Other events are enabled by default.
6204 */
6205 if (str_starts(buf, RX_PROBE_REQUEST))
6206 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
6207 return 1;
6208}
6209
6210
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006211static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
6212 const char *ifname, int level,
6213 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214{
6215 struct wpa_ctrl_dst *dst, *next;
6216 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006217 int idx, res;
6218 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006219 char levelstr[10];
6220
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006221 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006222 return;
6223
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006224 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
6225 if (os_snprintf_error(sizeof(levelstr), res))
6226 return;
6227 idx = 0;
6228 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006229 io[idx].iov_base = "IFNAME=";
6230 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006231 idx++;
6232 io[idx].iov_base = (char *) ifname;
6233 io[idx].iov_len = os_strlen(ifname);
6234 idx++;
6235 io[idx].iov_base = " ";
6236 io[idx].iov_len = 1;
6237 idx++;
6238 }
6239 io[idx].iov_base = levelstr;
6240 io[idx].iov_len = os_strlen(levelstr);
6241 idx++;
6242 io[idx].iov_base = (char *) buf;
6243 io[idx].iov_len = len;
6244 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245 os_memset(&msg, 0, sizeof(msg));
6246 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006247 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248
6249 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006250 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07006251 if ((level >= dst->debug_level) &&
6252 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006253 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
6254 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255 msg.msg_name = &dst->addr;
6256 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006257 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006258 int _errno = errno;
6259 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
6260 "%d - %s",
6261 idx, errno, strerror(errno));
6262 dst->errors++;
6263 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006264 ctrl_iface_detach(ctrl_dst,
6265 &dst->addr,
6266 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006267 }
6268 } else
6269 dst->errors = 0;
6270 }
6271 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006272 }
6273}
6274
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006275
6276static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
6277 enum wpa_msg_type type,
6278 const char *buf, size_t len)
6279{
6280 if (type != WPA_MSG_NO_GLOBAL) {
6281 hostapd_ctrl_iface_send_internal(
6282 hapd->iface->interfaces->global_ctrl_sock,
6283 &hapd->iface->interfaces->global_ctrl_dst,
6284 type != WPA_MSG_PER_INTERFACE ?
6285 NULL : hapd->conf->iface,
6286 level, buf, len);
6287 }
6288
6289 if (type != WPA_MSG_ONLY_GLOBAL) {
6290 hostapd_ctrl_iface_send_internal(
6291 hapd->ctrl_sock, &hapd->ctrl_dst,
6292 NULL, level, buf, len);
6293 }
6294}
6295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006296#endif /* CONFIG_NATIVE_WINDOWS */