blob: d07567d7875da51ed94d9a762dde289bbfa66bc1 [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 Ravi72e01222024-03-09 01:25:43 +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 Ravi72e01222024-03-09 01:25:43 +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 Raviaf399a82024-05-05 20:56:55 +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 Ravi72e01222024-03-09 01:25:43 +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 Ravi72e01222024-03-09 01:25:43 +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 Raviaf399a82024-05-05 20:56:55 +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 Ravi036cec52023-03-29 11:35:17 -07002461static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params,
2462 u16 punct_bitmap)
Hai Shalomfdcde762020-04-02 11:19:20 -07002463{
Sunil Ravi036cec52023-03-29 11:35:17 -07002464 u32 start_freq;
2465
2466 if (is_6ghz_freq(params->freq)) {
2467 const int bw_idx[] = { 20, 40, 80, 160, 320 };
2468 int idx, bw;
2469
2470 /* The 6 GHz band requires HE to be enabled. */
2471 params->he_enabled = 1;
2472
2473 if (params->center_freq1) {
2474 if (params->freq == 5935)
2475 idx = (params->center_freq1 - 5925) / 5;
2476 else
2477 idx = (params->center_freq1 - 5950) / 5;
2478
2479 bw = center_idx_to_bw_6ghz(idx);
2480 if (bw < 0 || bw > (int) ARRAY_SIZE(bw_idx) ||
2481 bw_idx[bw] != params->bandwidth)
2482 return -1;
2483 }
2484 }
2485
Hai Shalomfdcde762020-04-02 11:19:20 -07002486 switch (params->bandwidth) {
2487 case 0:
2488 /* bandwidth not specified: use 20 MHz by default */
2489 /* fall-through */
2490 case 20:
2491 if (params->center_freq1 &&
2492 params->center_freq1 != params->freq)
2493 return -1;
2494
2495 if (params->center_freq2 || params->sec_channel_offset)
2496 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07002497
2498 if (punct_bitmap)
2499 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002500 break;
2501 case 40:
2502 if (params->center_freq2 || !params->sec_channel_offset)
2503 return -1;
2504
Sunil Ravi036cec52023-03-29 11:35:17 -07002505 if (punct_bitmap)
2506 return -1;
2507
Hai Shalomfdcde762020-04-02 11:19:20 -07002508 if (!params->center_freq1)
2509 break;
2510 switch (params->sec_channel_offset) {
2511 case 1:
2512 if (params->freq + 10 != params->center_freq1)
2513 return -1;
2514 break;
2515 case -1:
2516 if (params->freq - 10 != params->center_freq1)
2517 return -1;
2518 break;
2519 default:
2520 return -1;
2521 }
2522 break;
2523 case 80:
2524 if (!params->center_freq1 || !params->sec_channel_offset)
2525 return 1;
2526
2527 switch (params->sec_channel_offset) {
2528 case 1:
2529 if (params->freq - 10 != params->center_freq1 &&
2530 params->freq + 30 != params->center_freq1)
2531 return 1;
2532 break;
2533 case -1:
2534 if (params->freq + 10 != params->center_freq1 &&
2535 params->freq - 30 != params->center_freq1)
2536 return -1;
2537 break;
2538 default:
2539 return -1;
2540 }
2541
Sunil Ravi036cec52023-03-29 11:35:17 -07002542 if (params->center_freq2 && punct_bitmap)
2543 return -1;
2544
Hai Shalomfdcde762020-04-02 11:19:20 -07002545 /* Adjacent and overlapped are not allowed for 80+80 */
2546 if (params->center_freq2 &&
2547 params->center_freq1 - params->center_freq2 <= 80 &&
2548 params->center_freq2 - params->center_freq1 <= 80)
2549 return 1;
2550 break;
2551 case 160:
2552 if (!params->center_freq1 || params->center_freq2 ||
2553 !params->sec_channel_offset)
2554 return -1;
2555
2556 switch (params->sec_channel_offset) {
2557 case 1:
2558 if (params->freq + 70 != params->center_freq1 &&
2559 params->freq + 30 != params->center_freq1 &&
2560 params->freq - 10 != params->center_freq1 &&
2561 params->freq - 50 != params->center_freq1)
2562 return -1;
2563 break;
2564 case -1:
2565 if (params->freq + 50 != params->center_freq1 &&
2566 params->freq + 10 != params->center_freq1 &&
2567 params->freq - 30 != params->center_freq1 &&
2568 params->freq - 70 != params->center_freq1)
2569 return -1;
2570 break;
2571 default:
2572 return -1;
2573 }
2574 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00002575 case 320:
2576 if (!params->center_freq1 || params->center_freq2 ||
2577 !params->sec_channel_offset)
2578 return -1;
2579
2580 switch (params->sec_channel_offset) {
2581 case 1:
2582 if (params->freq + 150 != params->center_freq1 &&
2583 params->freq + 110 != params->center_freq1 &&
2584 params->freq + 70 != params->center_freq1 &&
2585 params->freq + 30 != params->center_freq1 &&
2586 params->freq - 10 != params->center_freq1 &&
2587 params->freq - 50 != params->center_freq1 &&
2588 params->freq - 90 != params->center_freq1 &&
2589 params->freq - 130 != params->center_freq1)
2590 return -1;
2591 break;
2592 case -1:
2593 if (params->freq + 130 != params->center_freq1 &&
2594 params->freq + 90 != params->center_freq1 &&
2595 params->freq + 50 != params->center_freq1 &&
2596 params->freq + 10 != params->center_freq1 &&
2597 params->freq - 30 != params->center_freq1 &&
2598 params->freq - 70 != params->center_freq1 &&
2599 params->freq - 110 != params->center_freq1 &&
2600 params->freq - 150 != params->center_freq1)
2601 return -1;
2602 break;
2603 }
2604 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002605 default:
2606 return -1;
2607 }
2608
Sunil Ravi036cec52023-03-29 11:35:17 -07002609 if (!punct_bitmap)
2610 return 0;
2611
2612 if (!params->eht_enabled) {
2613 wpa_printf(MSG_ERROR,
2614 "Preamble puncturing supported only in EHT");
2615 return -1;
2616 }
2617
2618 if (params->freq >= 2412 && params->freq <= 2484) {
2619 wpa_printf(MSG_ERROR,
2620 "Preamble puncturing is not supported in 2.4 GHz");
2621 return -1;
2622 }
2623
2624 start_freq = params->center_freq1 - (params->bandwidth / 2);
2625 if (!is_punct_bitmap_valid(params->bandwidth,
2626 (params->freq - start_freq) / 20,
2627 punct_bitmap)) {
2628 wpa_printf(MSG_ERROR, "Invalid preamble puncturing bitmap");
2629 return -1;
2630 }
2631
Hai Shalomfdcde762020-04-02 11:19:20 -07002632 return 0;
2633}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002634#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002635
2636
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002637static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2638 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002639{
2640#ifdef NEED_AP_MLME
2641 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002642 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002643 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002644 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002645 int bandwidth;
2646 u8 chan;
Sunil Raviaf399a82024-05-05 20:56:55 +00002647 unsigned int num_err = 0;
2648 int err = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002649
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002650 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002651 if (ret)
2652 return ret;
2653
Sunil Ravi72e01222024-03-09 01:25:43 +00002654 settings.link_id = -1;
2655#ifdef CONFIG_IEEE80211BE
2656 if (iface->num_bss && iface->bss[0]->conf->mld_ap)
2657 settings.link_id = iface->bss[0]->mld_link_id;
2658#endif /* CONFIG_IEEE80211BE */
2659
Sunil Ravi036cec52023-03-29 11:35:17 -07002660 ret = hostapd_ctrl_check_freq_params(&settings.freq_params,
2661 settings.punct_bitmap);
Hai Shalomfdcde762020-04-02 11:19:20 -07002662 if (ret) {
2663 wpa_printf(MSG_INFO,
2664 "chanswitch: invalid frequency settings provided");
2665 return ret;
2666 }
2667
2668 switch (settings.freq_params.bandwidth) {
2669 case 40:
2670 bandwidth = CHAN_WIDTH_40;
2671 break;
2672 case 80:
2673 if (settings.freq_params.center_freq2)
2674 bandwidth = CHAN_WIDTH_80P80;
2675 else
2676 bandwidth = CHAN_WIDTH_80;
2677 break;
2678 case 160:
2679 bandwidth = CHAN_WIDTH_160;
2680 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002681 case 320:
2682 bandwidth = CHAN_WIDTH_320;
2683 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002684 default:
2685 bandwidth = CHAN_WIDTH_20;
2686 break;
2687 }
2688
2689 if (settings.freq_params.center_freq1)
2690 dfs_range += hostapd_is_dfs_overlap(
2691 iface, bandwidth, settings.freq_params.center_freq1);
2692 else
2693 dfs_range += hostapd_is_dfs_overlap(
2694 iface, bandwidth, settings.freq_params.freq);
2695
2696 if (settings.freq_params.center_freq2)
2697 dfs_range += hostapd_is_dfs_overlap(
2698 iface, bandwidth, settings.freq_params.center_freq2);
2699
2700 if (dfs_range) {
2701 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2702 if (ret == NUM_HOSTAPD_MODES) {
2703 wpa_printf(MSG_ERROR,
2704 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2705 settings.freq_params.freq,
2706 settings.freq_params.sec_channel_offset,
2707 settings.freq_params.bandwidth);
2708 return -1;
2709 }
2710
2711 settings.freq_params.channel = chan;
2712
2713 wpa_printf(MSG_DEBUG,
2714 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2715 settings.freq_params.channel,
2716 settings.freq_params.freq,
2717 settings.freq_params.sec_channel_offset,
2718 settings.freq_params.bandwidth,
2719 settings.freq_params.center_freq1);
2720
2721 /* Perform CAC and switch channel */
Sunil Raviaf399a82024-05-05 20:56:55 +00002722 iface->is_ch_switch_dfs = true;
Hai Shalomfdcde762020-04-02 11:19:20 -07002723 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2724 return 0;
2725 }
2726
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002727 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002728
Sunil Ravia04bd252022-05-02 22:54:18 -07002729 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002730 hostapd_chan_switch_config(iface->bss[i],
2731 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002732
Sunil Raviaf399a82024-05-05 20:56:55 +00002733 err = hostapd_switch_channel(iface->bss[i], &settings);
2734 if (err) {
2735 ret = err;
2736 num_err++;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002737 }
2738 }
2739
Sunil Raviaf399a82024-05-05 20:56:55 +00002740 return (iface->num_bss == num_err) ? ret : 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002741#else /* NEED_AP_MLME */
2742 return -1;
2743#endif /* NEED_AP_MLME */
2744}
2745
2746
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002747static u8 hostapd_maxnss(struct hostapd_data *hapd, struct sta_info *sta)
2748{
2749 u8 *mcs_set = NULL;
2750 u16 mcs_map;
2751 u8 ht_rx_nss = 0;
2752 u8 vht_rx_nss = 1;
2753 u8 mcs;
2754 bool ht_supported = false;
2755 bool vht_supported = false;
2756 int i;
2757
2758 if (sta->ht_capabilities && (sta->flags & WLAN_STA_HT)) {
2759 mcs_set = sta->ht_capabilities->supported_mcs_set;
2760 ht_supported = true;
2761 }
2762
2763 if (sta->vht_capabilities && (sta->flags & WLAN_STA_VHT)) {
2764 mcs_map = le_to_host16(
2765 sta->vht_capabilities->vht_supported_mcs_set.rx_map);
2766 vht_supported = true;
2767 }
2768
2769 if (ht_supported && mcs_set) {
2770 if (mcs_set[0])
2771 ht_rx_nss++;
2772 if (mcs_set[1])
2773 ht_rx_nss++;
2774 if (mcs_set[2])
2775 ht_rx_nss++;
2776 if (mcs_set[3])
2777 ht_rx_nss++;
2778 }
2779 if (vht_supported) {
2780 for (i = 7; i >= 0; i--) {
2781 mcs = (mcs_map >> (2 * i)) & 0x03;
2782 if (mcs != 0x03) {
2783 vht_rx_nss = i + 1;
2784 break;
2785 }
2786 }
2787 }
2788
2789 return ht_rx_nss > vht_rx_nss ? ht_rx_nss : vht_rx_nss;
2790}
2791
2792
2793static char hostapd_ctrl_iface_notify_cw_htaction(struct hostapd_data *hapd,
2794 const u8 *addr, u8 width)
2795{
2796 u8 buf[3];
2797 char ret;
2798
2799 width = width >= 1 ? 1 : 0;
2800
2801 buf[0] = WLAN_ACTION_HT;
2802 buf[1] = WLAN_HT_ACTION_NOTIFY_CHANWIDTH;
2803 buf[2] = width;
2804
2805 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2806 buf, sizeof(buf));
2807 if (ret)
2808 wpa_printf(MSG_DEBUG,
2809 "Failed to send Notify Channel Width frame to "
2810 MACSTR, MAC2STR(addr));
2811
2812 return ret;
2813}
2814
2815
2816static char hostapd_ctrl_iface_notify_cw_vhtaction(struct hostapd_data *hapd,
2817 const u8 *addr, u8 width)
2818{
2819 u8 buf[3];
2820 char ret;
2821
2822 buf[0] = WLAN_ACTION_VHT;
2823 buf[1] = WLAN_VHT_ACTION_OPMODE_NOTIF;
2824 buf[2] = width;
2825
2826 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2827 buf, sizeof(buf));
2828 if (ret)
2829 wpa_printf(MSG_DEBUG,
2830 "Failed to send Opeating Mode Notification frame to "
2831 MACSTR, MAC2STR(addr));
2832
2833 return ret;
2834}
2835
2836
2837static char hostapd_ctrl_iface_notify_cw_change(struct hostapd_data *hapd,
2838 const char *cmd)
2839{
2840 u8 cw, operating_mode = 0, nss;
2841 struct sta_info *sta;
2842 enum hostapd_hw_mode hw_mode;
2843
2844 if (is_6ghz_freq(hapd->iface->freq)) {
2845 wpa_printf(MSG_ERROR, "20/40 BSS coex not supported in 6 GHz");
2846 return -1;
2847 }
2848
2849 cw = atoi(cmd);
2850 hw_mode = hapd->iface->current_mode->mode;
2851 if ((hw_mode == HOSTAPD_MODE_IEEE80211G ||
2852 hw_mode == HOSTAPD_MODE_IEEE80211B) &&
2853 !(cw == 0 || cw == 1)) {
2854 wpa_printf(MSG_ERROR,
2855 "Channel width should be either 20 MHz or 40 MHz for 2.4 GHz band");
2856 return -1;
2857 }
2858
2859 switch (cw) {
2860 case 0:
2861 operating_mode = 0;
2862 break;
2863 case 1:
2864 operating_mode = VHT_OPMODE_CHANNEL_40MHZ;
2865 break;
2866 case 2:
2867 operating_mode = VHT_OPMODE_CHANNEL_80MHZ;
2868 break;
2869 case 3:
2870 operating_mode = VHT_OPMODE_CHANNEL_160MHZ;
2871 break;
2872 default:
2873 wpa_printf(MSG_ERROR, "Channel width should be between 0 to 3");
2874 return -1;
2875 }
2876
2877 for (sta = hapd->sta_list; sta; sta = sta->next) {
2878 if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
2879 nss = hostapd_maxnss(hapd, sta) - 1;
2880 hostapd_ctrl_iface_notify_cw_vhtaction(hapd, sta->addr,
2881 operating_mode |
2882 (u8) (nss << 4));
2883 continue;
2884 }
2885
2886 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) ==
2887 WLAN_STA_HT && sta->ht_capabilities)
2888 hostapd_ctrl_iface_notify_cw_htaction(hapd, sta->addr,
2889 cw);
2890 }
2891
2892 return 0;
2893}
2894
2895
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002896static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2897 int reply_size, const char *param)
2898{
2899#ifdef RADIUS_SERVER
2900 if (os_strcmp(param, "radius_server") == 0) {
2901 return radius_server_get_mib(hapd->radius_srv, reply,
2902 reply_size);
2903 }
2904#endif /* RADIUS_SERVER */
2905 return -1;
2906}
2907
2908
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002909static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2910 char *buf, size_t buflen)
2911{
2912 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08002913 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002914 u8 *data = NULL;
2915 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08002916 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002917 struct wpabuf *reply;
2918 size_t data_len = 0;
2919
Hai Shalom60840252021-02-19 19:02:11 -08002920 /**
2921 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2922 * [nested=<0|1>]
2923 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002924 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002925 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002926 return -EINVAL;
2927
2928 subcmd = strtoul(pos, &pos, 10);
2929
2930 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002931 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002932 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08002933
2934 temp = os_strchr(pos, ' ');
2935 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002936 }
2937
2938 if (data_len) {
2939 data_len /= 2;
2940 data = os_malloc(data_len);
2941 if (!data)
2942 return -ENOBUFS;
2943
2944 if (hexstr2bin(pos, data, data_len)) {
2945 wpa_printf(MSG_DEBUG,
2946 "Vendor command: wrong parameter format");
2947 os_free(data);
2948 return -EINVAL;
2949 }
2950 }
2951
Hai Shalom60840252021-02-19 19:02:11 -08002952 pos = os_strstr(cmd, "nested=");
2953 if (pos)
2954 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2955 NESTED_ATTR_NOT_USED;
2956
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002957 reply = wpabuf_alloc((buflen - 1) / 2);
2958 if (!reply) {
2959 os_free(data);
2960 return -ENOBUFS;
2961 }
2962
2963 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08002964 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002965
2966 if (ret == 0)
2967 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2968 wpabuf_len(reply));
2969
2970 wpabuf_free(reply);
2971 os_free(data);
2972
2973 return ret;
2974}
2975
2976
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002977static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2978 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002980 u8 addr[ETH_ALEN];
2981 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002983 if (hwaddr_aton(cmd, addr))
2984 return -1;
2985
2986 sta = ap_get_sta(hapd, addr);
2987 if (!sta || !sta->eapol_sm)
2988 return -1;
2989
2990 eapol_auth_reauthenticate(sta->eapol_sm);
2991 return 0;
2992}
2993
2994
2995static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2996{
2997 u8 addr[ETH_ALEN];
2998 struct sta_info *sta;
2999 char *pos = cmd, *param;
3000
3001 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
3002 return -1;
3003 pos += 18;
3004 param = pos;
3005 pos = os_strchr(pos, ' ');
3006 if (!pos)
3007 return -1;
3008 *pos++ = '\0';
3009
3010 sta = ap_get_sta(hapd, addr);
3011 if (!sta || !sta->eapol_sm)
3012 return -1;
3013
3014 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
3015}
3016
3017
3018static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
3019 char *buf, size_t buflen)
3020{
3021 char *pos, *end, *stamp;
3022 int ret;
3023
3024 /* cmd: "LOG_LEVEL [<level>]" */
3025 if (*cmd == '\0') {
3026 pos = buf;
3027 end = buf + buflen;
3028 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
3029 "Timestamp: %d\n",
3030 debug_level_str(wpa_debug_level),
3031 wpa_debug_timestamp);
3032 if (os_snprintf_error(end - pos, ret))
3033 ret = 0;
3034
3035 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003038 while (*cmd == ' ')
3039 cmd++;
3040
3041 stamp = os_strchr(cmd, ' ');
3042 if (stamp) {
3043 *stamp++ = '\0';
3044 while (*stamp == ' ') {
3045 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 }
3048
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003049 if (os_strlen(cmd)) {
3050 int level = str_to_debug_level(cmd);
3051 if (level < 0)
3052 return -1;
3053 wpa_debug_level = level;
3054 }
3055
3056 if (stamp && os_strlen(stamp))
3057 wpa_debug_timestamp = atoi(stamp);
3058
3059 os_memcpy(buf, "OK\n", 3);
3060 return 3;
3061}
3062
3063
3064#ifdef NEED_AP_MLME
3065static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
3066 char *buf, size_t buflen)
3067{
3068 struct hostapd_iface *iface = hapd->iface;
3069 char *pos, *end;
3070 struct hostapd_sta_info *info;
3071 struct os_reltime now;
3072
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003073 if (!iface->num_sta_seen)
3074 return 0;
3075
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003076 sta_track_expire(iface, 0);
3077
3078 pos = buf;
3079 end = buf + buflen;
3080
3081 os_get_reltime(&now);
3082 dl_list_for_each_reverse(info, &iface->sta_seen,
3083 struct hostapd_sta_info, list) {
3084 struct os_reltime age;
3085 int ret;
3086
3087 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003088 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
3089 MAC2STR(info->addr), (unsigned int) age.sec,
3090 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003091 if (os_snprintf_error(end - pos, ret))
3092 break;
3093 pos += ret;
3094 }
3095
3096 return pos - buf;
3097}
3098#endif /* NEED_AP_MLME */
3099
3100
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003101static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
3102 const char *cmd)
3103{
3104 u8 addr[ETH_ALEN];
3105
3106 if (hwaddr_aton(cmd, addr)) {
3107 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
3108 return -1;
3109 }
3110
3111 return hostapd_send_lci_req(hapd, addr);
3112}
3113
3114
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07003115static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003116{
3117 u8 addr[ETH_ALEN];
3118 char *token, *context = NULL;
3119 int random_interval, min_ap;
3120 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
3121 unsigned int n_responders;
3122
3123 token = str_token(cmd, " ", &context);
3124 if (!token || hwaddr_aton(token, addr)) {
3125 wpa_printf(MSG_INFO,
3126 "CTRL: REQ_RANGE - Bad destination address");
3127 return -1;
3128 }
3129
3130 token = str_token(cmd, " ", &context);
3131 if (!token)
3132 return -1;
3133
3134 random_interval = atoi(token);
3135 if (random_interval < 0 || random_interval > 0xffff)
3136 return -1;
3137
3138 token = str_token(cmd, " ", &context);
3139 if (!token)
3140 return -1;
3141
3142 min_ap = atoi(token);
3143 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
3144 return -1;
3145
3146 n_responders = 0;
3147 while ((token = str_token(cmd, " ", &context))) {
3148 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
3149 wpa_printf(MSG_INFO,
3150 "CTRL: REQ_RANGE: Too many responders");
3151 return -1;
3152 }
3153
3154 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
3155 wpa_printf(MSG_INFO,
3156 "CTRL: REQ_RANGE: Bad responder address");
3157 return -1;
3158 }
3159
3160 n_responders++;
3161 }
3162
3163 if (!n_responders) {
3164 wpa_printf(MSG_INFO,
3165 "CTRL: REQ_RANGE - No FTM responder address");
3166 return -1;
3167 }
3168
3169 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
3170 responders, n_responders);
3171}
3172
3173
Dmitry Shmidt29333592017-01-09 12:27:11 -08003174static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
3175 const char *cmd, char *reply,
3176 size_t reply_size)
3177{
3178 u8 addr[ETH_ALEN];
3179 const char *pos;
3180 struct wpabuf *req;
3181 int ret;
3182 u8 req_mode = 0;
3183
3184 if (hwaddr_aton(cmd, addr))
3185 return -1;
3186 pos = os_strchr(cmd, ' ');
3187 if (!pos)
3188 return -1;
3189 pos++;
3190 if (os_strncmp(pos, "req_mode=", 9) == 0) {
3191 int val = hex2byte(pos + 9);
3192
3193 if (val < 0)
3194 return -1;
3195 req_mode = val;
3196 pos += 11;
3197 pos = os_strchr(pos, ' ');
3198 if (!pos)
3199 return -1;
3200 pos++;
3201 }
3202 req = wpabuf_parse_bin(pos);
3203 if (!req)
3204 return -1;
3205
3206 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
3207 wpabuf_free(req);
3208 if (ret >= 0)
3209 ret = os_snprintf(reply, reply_size, "%d", ret);
3210 return ret;
3211}
3212
3213
Sunil Raviaf399a82024-05-05 20:56:55 +00003214static int hostapd_ctrl_iface_req_link_measurement(struct hostapd_data *hapd,
3215 const char *cmd, char *reply,
3216 size_t reply_size)
3217{
3218 u8 addr[ETH_ALEN];
3219 int ret;
3220
3221 if (hwaddr_aton(cmd, addr)) {
3222 wpa_printf(MSG_ERROR,
3223 "CTRL: REQ_LINK_MEASUREMENT: Invalid MAC address");
3224 return -1;
3225 }
3226
3227 ret = hostapd_send_link_measurement_req(hapd, addr);
3228 if (ret >= 0)
3229 ret = os_snprintf(reply, reply_size, "%d", ret);
3230 return ret;
3231}
3232
3233
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003234static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3235 char *buf, size_t buflen)
3236{
3237 if (!(hapd->conf->radio_measurements[0] &
3238 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3239 wpa_printf(MSG_ERROR,
3240 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3241 return -1;
3242 }
3243
3244 return hostapd_neighbor_show(hapd, buf, buflen);
3245}
3246
3247
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003248static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3249{
3250 struct wpa_ssid_value ssid;
3251 u8 bssid[ETH_ALEN];
3252 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003253 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003254 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003255 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003256 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003257
3258 if (!(hapd->conf->radio_measurements[0] &
3259 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3260 wpa_printf(MSG_ERROR,
3261 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3262 return -1;
3263 }
3264
3265 if (hwaddr_aton(buf, bssid)) {
3266 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3267 return -1;
3268 }
3269
3270 tmp = os_strstr(buf, "ssid=");
3271 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3272 wpa_printf(MSG_ERROR,
3273 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3274 return -1;
3275 }
3276 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3277 if (!buf)
3278 return -1;
3279
3280 tmp = os_strstr(buf, "nr=");
3281 if (!tmp) {
3282 wpa_printf(MSG_ERROR,
3283 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3284 return -1;
3285 }
3286
3287 buf = os_strchr(tmp, ' ');
3288 if (buf)
3289 *buf++ = '\0';
3290
3291 nr = wpabuf_parse_bin(tmp + 3);
3292 if (!nr) {
3293 wpa_printf(MSG_ERROR,
3294 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3295 return -1;
3296 }
3297
3298 if (!buf)
3299 goto set;
3300
3301 tmp = os_strstr(buf, "lci=");
3302 if (tmp) {
3303 buf = os_strchr(tmp, ' ');
3304 if (buf)
3305 *buf++ = '\0';
3306 lci = wpabuf_parse_bin(tmp + 4);
3307 if (!lci) {
3308 wpa_printf(MSG_ERROR,
3309 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003310 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003311 }
3312 }
3313
3314 if (!buf)
3315 goto set;
3316
3317 tmp = os_strstr(buf, "civic=");
3318 if (tmp) {
3319 buf = os_strchr(tmp, ' ');
3320 if (buf)
3321 *buf++ = '\0';
3322 civic = wpabuf_parse_bin(tmp + 6);
3323 if (!civic) {
3324 wpa_printf(MSG_ERROR,
3325 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003326 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003327 }
3328 }
3329
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003330 if (!buf)
3331 goto set;
3332
3333 if (os_strstr(buf, "stat"))
3334 stationary = 1;
3335
Hai Shaloma20dcd72022-02-04 13:43:00 -08003336 tmp = os_strstr(buf, "bss_parameter=");
3337 if (tmp) {
3338 bss_parameters = atoi(tmp + 14);
3339 if (bss_parameters < 0 || bss_parameters > 0xff) {
3340 wpa_printf(MSG_ERROR,
3341 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3342 goto fail;
3343 }
3344 }
3345
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003346set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003347 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003348 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003349
Hai Shaloma20dcd72022-02-04 13:43:00 -08003350fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003351 wpabuf_free(nr);
3352 wpabuf_free(lci);
3353 wpabuf_free(civic);
3354
3355 return ret;
3356}
3357
3358
3359static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3360 char *buf)
3361{
3362 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003363 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003364 u8 bssid[ETH_ALEN];
3365 char *tmp;
3366
3367 if (hwaddr_aton(buf, bssid)) {
3368 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3369 return -1;
3370 }
3371
3372 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003373 if (tmp) {
3374 ssidp = &ssid;
3375 if (ssid_parse(tmp + 5, &ssid)) {
3376 wpa_printf(MSG_ERROR,
3377 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3378 return -1;
3379 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003380 }
3381
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003382 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003383}
3384
3385
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003386static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3387 size_t buflen)
3388{
3389 int ret, i;
3390 char *pos, *end;
3391
3392 ret = os_snprintf(buf, buflen, "%016llX:\n",
3393 (long long unsigned) iface->drv_flags);
3394 if (os_snprintf_error(buflen, ret))
3395 return -1;
3396
3397 pos = buf + ret;
3398 end = buf + buflen;
3399
3400 for (i = 0; i < 64; i++) {
3401 if (iface->drv_flags & (1LLU << i)) {
3402 ret = os_snprintf(pos, end - pos, "%s\n",
3403 driver_flag_to_string(1LLU << i));
3404 if (os_snprintf_error(end - pos, ret))
3405 return -1;
3406 pos += ret;
3407 }
3408 }
3409
3410 return pos - buf;
3411}
3412
3413
Hai Shalomb755a2a2020-04-23 21:49:02 -07003414static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3415 size_t buflen)
3416{
3417 int ret, i;
3418 char *pos, *end;
3419
3420 ret = os_snprintf(buf, buflen, "%016llX:\n",
3421 (long long unsigned) iface->drv_flags2);
3422 if (os_snprintf_error(buflen, ret))
3423 return -1;
3424
3425 pos = buf + ret;
3426 end = buf + buflen;
3427
3428 for (i = 0; i < 64; i++) {
3429 if (iface->drv_flags2 & (1LLU << i)) {
3430 ret = os_snprintf(pos, end - pos, "%s\n",
3431 driver_flag2_to_string(1LLU << i));
3432 if (os_snprintf_error(end - pos, ret))
3433 return -1;
3434 pos += ret;
3435 }
3436 }
3437
3438 return pos - buf;
3439}
3440
3441
Hai Shalom021b0b52019-04-10 11:17:58 -07003442static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3443 const char *field, char *buf,
3444 size_t buflen)
3445{
3446 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3447
3448#ifdef CONFIG_DPP
3449 if (os_strcmp(field, "dpp") == 0) {
3450 int res;
3451
Hai Shaloma20dcd72022-02-04 13:43:00 -08003452#ifdef CONFIG_DPP3
3453 res = os_snprintf(buf, buflen, "DPP=3");
3454#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003455 res = os_snprintf(buf, buflen, "DPP=2");
3456#else /* CONFIG_DPP2 */
3457 res = os_snprintf(buf, buflen, "DPP=1");
3458#endif /* CONFIG_DPP2 */
3459 if (os_snprintf_error(buflen, res))
3460 return -1;
3461 return res;
3462 }
3463#endif /* CONFIG_DPP */
3464
3465 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3466 field);
3467
3468 return -1;
3469}
3470
3471
Hai Shaloma20dcd72022-02-04 13:43:00 -08003472#ifdef ANDROID
3473static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3474 char *buf, size_t buflen)
3475{
3476 int ret;
3477
3478 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3479 if (ret == 0) {
3480 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3481 if (os_snprintf_error(buflen, ret))
3482 ret = -1;
3483 }
3484 return ret;
3485}
3486#endif /* ANDROID */
3487
3488
Sunil Ravi72e01222024-03-09 01:25:43 +00003489#ifdef CONFIG_IEEE80211BE
3490
3491static int hostapd_ctrl_iface_enable_mld(struct hostapd_iface *iface)
3492{
3493 unsigned int i;
3494
3495 if (!iface || !iface->bss[0]->conf->mld_ap) {
3496 wpa_printf(MSG_ERROR,
3497 "Trying to enable AP MLD on an interface that is not affiliated with an AP MLD");
3498 return -1;
3499 }
3500
3501 for (i = 0; i < iface->interfaces->count; ++i) {
3502 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3503 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravi72e01222024-03-09 01:25:43 +00003504
Sunil Raviaf399a82024-05-05 20:56:55 +00003505 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravi72e01222024-03-09 01:25:43 +00003506 continue;
3507
3508 if (hostapd_enable_iface(h_iface)) {
3509 wpa_printf(MSG_ERROR, "Enabling of AP MLD failed");
3510 return -1;
3511 }
3512 }
3513 return 0;
3514}
3515
3516
3517static void hostapd_disable_iface_bss(struct hostapd_iface *iface)
3518{
3519 unsigned int i;
3520
3521 for (i = 0; i < iface->num_bss; i++)
3522 hostapd_bss_deinit_no_free(iface->bss[i]);
3523}
3524
3525
3526static int hostapd_ctrl_iface_disable_mld(struct hostapd_iface *iface)
3527{
3528 unsigned int i;
Sunil Ravi72e01222024-03-09 01:25:43 +00003529
3530 if (!iface || !iface->bss[0]->conf->mld_ap) {
3531 wpa_printf(MSG_ERROR,
3532 "Trying to disable AP MLD on an interface that is not affiliated with an AP MLD.");
3533 return -1;
3534 }
3535
3536 /* First, disable BSSs before stopping beaconing and doing driver
3537 * deinit so that the broadcast Deauthentication frames go out. */
3538
3539 for (i = 0; i < iface->interfaces->count; ++i) {
3540 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3541 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravi72e01222024-03-09 01:25:43 +00003542
Sunil Raviaf399a82024-05-05 20:56:55 +00003543 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravi72e01222024-03-09 01:25:43 +00003544 continue;
3545
Sunil Ravi72e01222024-03-09 01:25:43 +00003546 hostapd_disable_iface_bss(iface);
3547 }
3548
Sunil Ravi72e01222024-03-09 01:25:43 +00003549 /* Then, fully disable interfaces */
Sunil Ravi72e01222024-03-09 01:25:43 +00003550 for (i = 0; i < iface->interfaces->count; ++i) {
3551 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3552 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravi72e01222024-03-09 01:25:43 +00003553
Sunil Raviaf399a82024-05-05 20:56:55 +00003554 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravi72e01222024-03-09 01:25:43 +00003555 continue;
3556
3557 if (hostapd_disable_iface(h_iface)) {
3558 wpa_printf(MSG_ERROR, "Disabling AP MLD failed");
3559 return -1;
3560 }
3561 }
3562
Sunil Ravi72e01222024-03-09 01:25:43 +00003563 return 0;
3564}
3565
3566
3567#ifdef CONFIG_TESTING_OPTIONS
3568static int hostapd_ctrl_iface_link_remove(struct hostapd_data *hapd, char *cmd,
3569 char *buf, size_t buflen)
3570{
3571 int ret;
3572 u32 count = atoi(cmd);
3573
3574 if (!count)
3575 count = 1;
3576
3577 ret = hostapd_link_remove(hapd, count);
3578 if (ret == 0) {
3579 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3580 if (os_snprintf_error(buflen, ret))
3581 ret = -1;
3582 else
3583 ret = 0;
3584 }
3585
3586 return ret;
3587}
3588#endif /* CONFIG_TESTING_OPTIONS */
3589#endif /* CONFIG_IEEE80211BE */
3590
3591
3592#ifdef CONFIG_NAN_USD
3593
3594static int hostapd_ctrl_nan_publish(struct hostapd_data *hapd, char *cmd,
3595 char *buf, size_t buflen)
3596{
3597 char *token, *context = NULL;
3598 int publish_id;
3599 struct nan_publish_params params;
3600 const char *service_name = NULL;
3601 struct wpabuf *ssi = NULL;
3602 int ret = -1;
3603 enum nan_service_protocol_type srv_proto_type = 0;
3604
3605 os_memset(&params, 0, sizeof(params));
3606 /* USD shall use both solicited and unsolicited transmissions */
3607 params.unsolicited = true;
3608 params.solicited = true;
3609 /* USD shall require FSD without GAS */
3610 params.fsd = true;
3611
3612 while ((token = str_token(cmd, " ", &context))) {
3613 if (os_strncmp(token, "service_name=", 13) == 0) {
3614 service_name = token + 13;
3615 continue;
3616 }
3617
3618 if (os_strncmp(token, "ttl=", 4) == 0) {
3619 params.ttl = atoi(token + 4);
3620 continue;
3621 }
3622
3623 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3624 srv_proto_type = atoi(token + 15);
3625 continue;
3626 }
3627
3628 if (os_strncmp(token, "ssi=", 4) == 0) {
3629 if (ssi)
3630 goto fail;
3631 ssi = wpabuf_parse_bin(token + 4);
3632 if (!ssi)
3633 goto fail;
3634 continue;
3635 }
3636
3637 if (os_strcmp(token, "solicited=0") == 0) {
3638 params.solicited = false;
3639 continue;
3640 }
3641
3642 if (os_strcmp(token, "unsolicited=0") == 0) {
3643 params.unsolicited = false;
3644 continue;
3645 }
3646
3647 if (os_strcmp(token, "fsd=0") == 0) {
3648 params.fsd = false;
3649 continue;
3650 }
3651
3652 wpa_printf(MSG_INFO, "CTRL: Invalid NAN_PUBLISH parameter: %s",
3653 token);
3654 goto fail;
3655 }
3656
3657 publish_id = hostapd_nan_usd_publish(hapd, service_name, srv_proto_type,
3658 ssi, &params);
3659 if (publish_id > 0)
3660 ret = os_snprintf(buf, buflen, "%d", publish_id);
3661fail:
3662 wpabuf_free(ssi);
3663 return ret;
3664}
3665
3666
3667static int hostapd_ctrl_nan_cancel_publish(struct hostapd_data *hapd,
3668 char *cmd)
3669{
3670 char *token, *context = NULL;
3671 int publish_id = 0;
3672
3673 while ((token = str_token(cmd, " ", &context))) {
3674 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3675 continue;
3676 wpa_printf(MSG_INFO,
3677 "CTRL: Invalid NAN_CANCEL_PUBLISH parameter: %s",
3678 token);
3679 return -1;
3680 }
3681
3682 if (publish_id <= 0) {
3683 wpa_printf(MSG_INFO,
3684 "CTRL: Invalid or missing NAN_CANCEL_PUBLISH publish_id");
3685 return -1;
3686 }
3687
3688 hostapd_nan_usd_cancel_publish(hapd, publish_id);
3689 return 0;
3690}
3691
3692
3693static int hostapd_ctrl_nan_update_publish(struct hostapd_data *hapd,
3694 char *cmd)
3695{
3696 char *token, *context = NULL;
3697 int publish_id = 0;
3698 struct wpabuf *ssi = NULL;
3699 int ret = -1;
3700
3701 while ((token = str_token(cmd, " ", &context))) {
3702 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3703 continue;
3704 if (os_strncmp(token, "ssi=", 4) == 0) {
3705 if (ssi)
3706 goto fail;
3707 ssi = wpabuf_parse_bin(token + 4);
3708 if (!ssi)
3709 goto fail;
3710 continue;
3711 }
3712 wpa_printf(MSG_INFO,
3713 "CTRL: Invalid NAN_UPDATE_PUBLISH parameter: %s",
3714 token);
3715 goto fail;
3716 }
3717
3718 if (publish_id <= 0) {
3719 wpa_printf(MSG_INFO,
3720 "CTRL: Invalid or missing NAN_UPDATE_PUBLISH publish_id");
3721 goto fail;
3722 }
3723
3724 ret = hostapd_nan_usd_update_publish(hapd, publish_id, ssi);
3725fail:
3726 wpabuf_free(ssi);
3727 return ret;
3728}
3729
3730
3731static int hostapd_ctrl_nan_subscribe(struct hostapd_data *hapd, char *cmd,
3732 char *buf, size_t buflen)
3733{
3734 char *token, *context = NULL;
3735 int subscribe_id;
3736 struct nan_subscribe_params params;
3737 const char *service_name = NULL;
3738 struct wpabuf *ssi = NULL;
3739 int ret = -1;
3740 enum nan_service_protocol_type srv_proto_type = 0;
3741
3742 os_memset(&params, 0, sizeof(params));
3743
3744 while ((token = str_token(cmd, " ", &context))) {
3745 if (os_strncmp(token, "service_name=", 13) == 0) {
3746 service_name = token + 13;
3747 continue;
3748 }
3749
3750 if (os_strcmp(token, "active=1") == 0) {
3751 params.active = true;
3752 continue;
3753 }
3754
3755 if (os_strncmp(token, "ttl=", 4) == 0) {
3756 params.ttl = atoi(token + 4);
3757 continue;
3758 }
3759
3760 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3761 srv_proto_type = atoi(token + 15);
3762 continue;
3763 }
3764
3765 if (os_strncmp(token, "ssi=", 4) == 0) {
3766 if (ssi)
3767 goto fail;
3768 ssi = wpabuf_parse_bin(token + 4);
3769 if (!ssi)
3770 goto fail;
3771 continue;
3772 }
3773
3774 wpa_printf(MSG_INFO,
3775 "CTRL: Invalid NAN_SUBSCRIBE parameter: %s",
3776 token);
3777 goto fail;
3778 }
3779
3780 subscribe_id = hostapd_nan_usd_subscribe(hapd, service_name,
3781 srv_proto_type, ssi,
3782 &params);
3783 if (subscribe_id > 0)
3784 ret = os_snprintf(buf, buflen, "%d", subscribe_id);
3785fail:
3786 wpabuf_free(ssi);
3787 return ret;
3788}
3789
3790
3791static int hostapd_ctrl_nan_cancel_subscribe(struct hostapd_data *hapd,
3792 char *cmd)
3793{
3794 char *token, *context = NULL;
3795 int subscribe_id = 0;
3796
3797 while ((token = str_token(cmd, " ", &context))) {
3798 if (sscanf(token, "subscribe_id=%i", &subscribe_id) == 1)
3799 continue;
3800 wpa_printf(MSG_INFO,
3801 "CTRL: Invalid NAN_CANCEL_SUBSCRIBE parameter: %s",
3802 token);
3803 return -1;
3804 }
3805
3806 if (subscribe_id <= 0) {
3807 wpa_printf(MSG_INFO,
3808 "CTRL: Invalid or missing NAN_CANCEL_SUBSCRIBE subscribe_id");
3809 return -1;
3810 }
3811
3812 hostapd_nan_usd_cancel_subscribe(hapd, subscribe_id);
3813 return 0;
3814}
3815
3816
3817static int hostapd_ctrl_nan_transmit(struct hostapd_data *hapd, char *cmd)
3818{
3819 char *token, *context = NULL;
3820 int handle = 0;
3821 int req_instance_id = 0;
3822 struct wpabuf *ssi = NULL;
3823 u8 peer_addr[ETH_ALEN];
3824 int ret = -1;
3825
3826 os_memset(peer_addr, 0, ETH_ALEN);
3827
3828 while ((token = str_token(cmd, " ", &context))) {
3829 if (sscanf(token, "handle=%i", &handle) == 1)
3830 continue;
3831
3832 if (sscanf(token, "req_instance_id=%i", &req_instance_id) == 1)
3833 continue;
3834
3835 if (os_strncmp(token, "address=", 8) == 0) {
3836 if (hwaddr_aton(token + 8, peer_addr) < 0)
3837 return -1;
3838 continue;
3839 }
3840
3841 if (os_strncmp(token, "ssi=", 4) == 0) {
3842 if (ssi)
3843 goto fail;
3844 ssi = wpabuf_parse_bin(token + 4);
3845 if (!ssi)
3846 goto fail;
3847 continue;
3848 }
3849
3850 wpa_printf(MSG_INFO,
3851 "CTRL: Invalid NAN_TRANSMIT parameter: %s",
3852 token);
3853 goto fail;
3854 }
3855
3856 if (handle <= 0) {
3857 wpa_printf(MSG_INFO,
3858 "CTRL: Invalid or missing NAN_TRANSMIT handle");
3859 goto fail;
3860 }
3861
3862 if (is_zero_ether_addr(peer_addr)) {
3863 wpa_printf(MSG_INFO,
3864 "CTRL: Invalid or missing NAN_TRANSMIT address");
3865 goto fail;
3866 }
3867
3868 ret = hostapd_nan_usd_transmit(hapd, handle, ssi, NULL, peer_addr,
3869 req_instance_id);
3870fail:
3871 wpabuf_free(ssi);
3872 return ret;
3873}
3874
3875#endif /* CONFIG_NAN_USD */
3876
3877
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003878static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3879 char *buf, char *reply,
3880 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003881 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003882 socklen_t fromlen)
3883{
3884 int reply_len, res;
3885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003886 os_memcpy(reply, "OK\n", 3);
3887 reply_len = 3;
3888
3889 if (os_strcmp(buf, "PING") == 0) {
3890 os_memcpy(reply, "PONG\n", 5);
3891 reply_len = 5;
3892 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3893 if (wpa_debug_reopen_file() < 0)
3894 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003895 } else if (os_strcmp(buf, "CLOSE_LOG") == 0) {
3896 wpa_debug_stop_log();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003897 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3898 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003899 } else if (os_strcmp(buf, "STATUS") == 0) {
3900 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3901 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003902 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3903 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904 } else if (os_strcmp(buf, "MIB") == 0) {
3905 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3906 if (reply_len >= 0) {
3907 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3908 reply_size - reply_len);
3909 if (res < 0)
3910 reply_len = -1;
3911 else
3912 reply_len += res;
3913 }
3914 if (reply_len >= 0) {
3915 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3916 reply_size - reply_len);
3917 if (res < 0)
3918 reply_len = -1;
3919 else
3920 reply_len += res;
3921 }
3922#ifndef CONFIG_NO_RADIUS
3923 if (reply_len >= 0) {
3924 res = radius_client_get_mib(hapd->radius,
3925 reply + reply_len,
3926 reply_size - reply_len);
3927 if (res < 0)
3928 reply_len = -1;
3929 else
3930 reply_len += res;
3931 }
3932#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003933 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3934 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3935 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3937 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3938 reply_size);
3939 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3940 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3941 reply_size);
3942 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3943 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3944 reply_size);
3945 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003946 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3947 reply_len = -1;
3948 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3949 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950 reply_len = -1;
3951 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003952 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 reply_len = -1;
3954 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003955 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956 buf + 6))
3957 reply_len = -1;
3958 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3959 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3960 reply_len = -1;
3961 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3962 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3963 reply_len = -1;
3964 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3965 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3966 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003967#ifdef CONFIG_TAXONOMY
3968 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3969 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3970 reply, reply_size);
3971#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003972 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3973 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3974 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003975 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3976 if (hostapd_ctrl_iface_stop_ap(hapd))
3977 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978#ifdef NEED_AP_MLME
3979 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3980 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3981 reply_len = -1;
3982#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983#ifdef CONFIG_WPS
3984 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3985 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3986 reply_len = -1;
3987 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3988 reply_len = hostapd_ctrl_iface_wps_check_pin(
3989 hapd, buf + 14, reply, reply_size);
3990 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3991 if (hostapd_wps_button_pushed(hapd, NULL))
3992 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003993 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3994 if (hostapd_wps_cancel(hapd))
3995 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3997 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3998 reply, reply_size);
3999 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
4000 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
4001 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004002 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
4003 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
4004 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004005#ifdef CONFIG_WPS_NFC
4006 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4007 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
4008 reply_len = -1;
4009 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
4010 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
4011 hapd, buf + 21, reply, reply_size);
4012 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4013 reply_len = hostapd_ctrl_iface_wps_nfc_token(
4014 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004015 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
4016 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
4017 hapd, buf + 21, reply, reply_size);
4018 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
4019 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
4020 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004021#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07004023#ifdef CONFIG_INTERWORKING
4024 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
4025 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
4026 reply_len = -1;
4027 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
4028 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
4029 reply_len = -1;
4030#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004031#ifdef CONFIG_HS20
4032 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
4033 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
4034 reply_len = -1;
4035 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
4036 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
4037 reply_len = -1;
4038#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004039#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004040 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
4041 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
4042 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004043 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
4044 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
4045 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004046 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
4047 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
4048 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004049 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
4050 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
4051 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004052#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
4054 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
4055 reply_size);
4056 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4057 if (hostapd_ctrl_iface_set(hapd, buf + 4))
4058 reply_len = -1;
4059 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4060 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
4061 reply_size);
Sunil Ravi72e01222024-03-09 01:25:43 +00004062 } else if (os_strcmp(buf, "ENABLE") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004063 if (hostapd_ctrl_iface_enable(hapd->iface))
4064 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08004065 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
4066 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
4067 reply_len = -1;
Sunil Ravi72e01222024-03-09 01:25:43 +00004068#ifdef CONFIG_IEEE80211R_AP
4069 } else if (os_strcmp(buf, "GET_RXKHS") == 0) {
4070 reply_len = hostapd_ctrl_iface_get_rxkhs(hapd, reply,
4071 reply_size);
4072 } else if (os_strcmp(buf, "RELOAD_RXKHS") == 0) {
4073 if (hostapd_ctrl_iface_reload_rxkhs(hapd))
4074 reply_len = -1;
4075#endif /* CONFIG_IEEE80211R_AP */
Sunil Ravi77d572f2023-01-17 23:58:31 +00004076 } else if (os_strcmp(buf, "RELOAD_BSS") == 0) {
4077 if (hostapd_ctrl_iface_reload_bss(hapd))
4078 reply_len = -1;
Sunil Ravi72e01222024-03-09 01:25:43 +00004079 } else if (os_strcmp(buf, "RELOAD_CONFIG") == 0) {
4080 if (hostapd_reload_config(hapd->iface))
4081 reply_len = -1;
4082 } else if (os_strcmp(buf, "RELOAD") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004083 if (hostapd_ctrl_iface_reload(hapd->iface))
4084 reply_len = -1;
Sunil Ravi72e01222024-03-09 01:25:43 +00004085 } else if (os_strcmp(buf, "DISABLE") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004086 if (hostapd_ctrl_iface_disable(hapd->iface))
4087 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004088 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
4089 if (ieee802_11_set_beacon(hapd))
4090 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004091#ifdef CONFIG_TESTING_OPTIONS
4092 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
4093 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
4094 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004095 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
4096 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
4097 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004098 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
4099 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
4100 buf + 23) < 0)
4101 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004102 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
4103 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
4104 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004105 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
4106 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
4107 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004108 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
4109 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
4110 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004111 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
4112 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
4113 reply_len = -1;
4114 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
4115 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
4116 reply_len = -1;
4117 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
4118 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
4119 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004120 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
Sunil Ravi72e01222024-03-09 01:25:43 +00004121 if (testing_set_fail_pattern(true, buf + 16) < 0)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004122 reply_len = -1;
4123 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
Sunil Ravi72e01222024-03-09 01:25:43 +00004124 reply_len = testing_get_fail_pattern(true, reply, reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004125 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
Sunil Ravi72e01222024-03-09 01:25:43 +00004126 if (testing_set_fail_pattern(false, buf + 10) < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004127 reply_len = -1;
4128 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
Sunil Ravi72e01222024-03-09 01:25:43 +00004129 reply_len = testing_get_fail_pattern(false, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004130 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
4131 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
4132 reply_len = -1;
4133 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
4134 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
4135 reply_len = -1;
4136 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
4137 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
4138 reply_len = -1;
4139 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
4140 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
4141 reply_len = -1;
4142 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
4143 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
4144 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004145 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
4146 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
4147 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004148 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
4149 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
4150 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004151 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
4152 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
4153 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08004154 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
4155 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
4156 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004157#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004158 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004159 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004160 reply_len = -1;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004161 } else if (os_strncmp(buf, "NOTIFY_CW_CHANGE ", 17) == 0) {
4162 if (hostapd_ctrl_iface_notify_cw_change(hapd, buf + 17))
4163 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004164 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
4165 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
4166 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004167 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
4168 ieee802_1x_erp_flush(hapd);
4169#ifdef RADIUS_SERVER
4170 radius_server_erp_flush(hapd->radius_srv);
4171#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004172 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
4173 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
4174 reply_len = -1;
4175 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
4176 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
4177 reply_len = -1;
4178 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
4179 reply_len = hostapd_ctrl_iface_log_level(
4180 hapd, buf + 9, reply, reply_size);
4181#ifdef NEED_AP_MLME
4182 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
4183 reply_len = hostapd_ctrl_iface_track_sta_list(
4184 hapd, reply, reply_size);
4185#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07004186 } else if (os_strcmp(buf, "PMKSA") == 0) {
4187 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
4188 reply_size);
4189 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4190 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004191 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
4192 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
4193 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004194 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
4195 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
4196 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004197 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
4198 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
4199 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004200 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
4201 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
4202 reply_len = -1;
4203 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
4204 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
4205 reply_len = -1;
4206 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
4207 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
4208 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08004209 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
4210 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
4211 reply, reply_size);
Sunil Raviaf399a82024-05-05 20:56:55 +00004212 } else if (os_strncmp(buf, "REQ_LINK_MEASUREMENT ", 21) == 0) {
4213 reply_len = hostapd_ctrl_iface_req_link_measurement(
4214 hapd, buf + 21, reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004215 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
4216 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
4217 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07004218 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
4219 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
4220 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08004221 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4222 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07004223 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
4224 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004225 if (hostapd_ctrl_iface_acl_add_mac(
4226 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004227 &hapd->conf->num_accept_mac, buf + 19) ||
4228 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004229 reply_len = -1;
4230 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07004231 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07004232 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004233 &hapd->conf->num_accept_mac, buf + 19) ||
4234 hostapd_set_acl(hapd) ||
4235 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07004236 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004237 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
4238 reply_len = hostapd_ctrl_iface_acl_show_mac(
4239 hapd->conf->accept_mac,
4240 hapd->conf->num_accept_mac, reply, reply_size);
4241 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
4242 hostapd_ctrl_iface_acl_clear_list(
4243 &hapd->conf->accept_mac,
4244 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07004245 if (hostapd_set_acl(hapd) ||
4246 hostapd_disassoc_accept_mac(hapd))
4247 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07004248 } else {
4249 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004250 }
4251 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
4252 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07004253 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07004254 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004255 &hapd->conf->num_deny_mac, buf + 17) ||
4256 hostapd_set_acl(hapd) ||
4257 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004258 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004259 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004260 if (hostapd_ctrl_iface_acl_del_mac(
4261 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004262 &hapd->conf->num_deny_mac, buf + 17) ||
4263 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004264 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004265 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
4266 reply_len = hostapd_ctrl_iface_acl_show_mac(
4267 hapd->conf->deny_mac,
4268 hapd->conf->num_deny_mac, reply, reply_size);
4269 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
4270 hostapd_ctrl_iface_acl_clear_list(
4271 &hapd->conf->deny_mac,
4272 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07004273 if (hostapd_set_acl(hapd))
4274 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07004275 } else {
4276 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004277 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004278#ifdef CONFIG_DPP
4279 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
4280 res = hostapd_dpp_qr_code(hapd, buf + 12);
4281 if (res < 0) {
4282 reply_len = -1;
4283 } else {
4284 reply_len = os_snprintf(reply, reply_size, "%d", res);
4285 if (os_snprintf_error(reply_size, reply_len))
4286 reply_len = -1;
4287 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004288 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
4289 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
4290 if (res < 0) {
4291 reply_len = -1;
4292 } else {
4293 reply_len = os_snprintf(reply, reply_size, "%d", res);
4294 if (os_snprintf_error(reply_size, reply_len))
4295 reply_len = -1;
4296 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004297 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
4298 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
4299 if (res < 0) {
4300 reply_len = -1;
4301 } else {
4302 reply_len = os_snprintf(reply, reply_size, "%d", res);
4303 if (os_snprintf_error(reply_size, reply_len))
4304 reply_len = -1;
4305 }
4306 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
4307 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
4308 if (res < 0) {
4309 reply_len = -1;
4310 } else {
4311 reply_len = os_snprintf(reply, reply_size, "%d", res);
4312 if (os_snprintf_error(reply_size, reply_len))
4313 reply_len = -1;
4314 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004315 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004316 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004317 if (res < 0) {
4318 reply_len = -1;
4319 } else {
4320 reply_len = os_snprintf(reply, reply_size, "%d", res);
4321 if (os_snprintf_error(reply_size, reply_len))
4322 reply_len = -1;
4323 }
4324 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004325 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
4326 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004327 reply_len = -1;
4328 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
4329 const char *uri;
4330
Hai Shalom021b0b52019-04-10 11:17:58 -07004331 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
4332 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004333 if (!uri) {
4334 reply_len = -1;
4335 } else {
4336 reply_len = os_snprintf(reply, reply_size, "%s", uri);
4337 if (os_snprintf_error(reply_size, reply_len))
4338 reply_len = -1;
4339 }
4340 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004341 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
4342 atoi(buf + 19),
4343 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07004344 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
4345 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
4346 atoi(buf + 18),
4347 os_strchr(buf + 18, ' ')) < 0)
4348 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004349 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
4350 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
4351 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004352 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
4353 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
4354 reply_len = -1;
4355 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
4356 hostapd_dpp_stop(hapd);
4357 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004358 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004359 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
4360 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004361 if (res < 0) {
4362 reply_len = -1;
4363 } else {
4364 reply_len = os_snprintf(reply, reply_size, "%d", res);
4365 if (os_snprintf_error(reply_size, reply_len))
4366 reply_len = -1;
4367 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004368 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
4369 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
4370 buf + 20) < 0)
4371 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004372 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004373 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
4374 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004375 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004376 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004377 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004378 reply_len = -1;
4379 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004380 reply_len = dpp_configurator_get_key_id(
4381 hapd->iface->interfaces->dpp,
4382 atoi(buf + 25),
4383 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004384 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
4385 res = hostapd_dpp_pkex_add(hapd, buf + 12);
4386 if (res < 0) {
4387 reply_len = -1;
4388 } else {
4389 reply_len = os_snprintf(reply, reply_size, "%d", res);
4390 if (os_snprintf_error(reply_size, reply_len))
4391 reply_len = -1;
4392 }
4393 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
4394 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
4395 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004396#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07004397 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
4398 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
4399 reply_len = -1;
4400 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
4401 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
4402 reply_len = -1;
4403 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
4404 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004405 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
4406 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
4407 reply_len = -1;
4408 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
4409 hostapd_dpp_chirp_stop(hapd);
Sunil Ravi89eba102022-09-13 21:04:37 -07004410 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
4411 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
4412 reply_len = -1;
4413 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
4414 hostapd_dpp_remove_controller(hapd, buf + 28);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004415#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07004416#ifdef CONFIG_DPP3
4417 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
4418 if (hostapd_dpp_push_button(hapd, NULL) < 0)
4419 reply_len = -1;
4420 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
4421 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
4422 reply_len = -1;
4423#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004424#endif /* CONFIG_DPP */
Sunil Ravi72e01222024-03-09 01:25:43 +00004425#ifdef CONFIG_NAN_USD
4426 } else if (os_strncmp(buf, "NAN_PUBLISH ", 12) == 0) {
4427 reply_len = hostapd_ctrl_nan_publish(hapd, buf + 12, reply,
4428 reply_size);
4429 } else if (os_strncmp(buf, "NAN_CANCEL_PUBLISH ", 19) == 0) {
4430 if (hostapd_ctrl_nan_cancel_publish(hapd, buf + 19) < 0)
4431 reply_len = -1;
4432 } else if (os_strncmp(buf, "NAN_UPDATE_PUBLISH ", 19) == 0) {
4433 if (hostapd_ctrl_nan_update_publish(hapd, buf + 19) < 0)
4434 reply_len = -1;
4435 } else if (os_strncmp(buf, "NAN_SUBSCRIBE ", 14) == 0) {
4436 reply_len = hostapd_ctrl_nan_subscribe(hapd, buf + 14, reply,
4437 reply_size);
4438 } else if (os_strncmp(buf, "NAN_CANCEL_SUBSCRIBE ", 21) == 0) {
4439 if (hostapd_ctrl_nan_cancel_subscribe(hapd, buf + 21) < 0)
4440 reply_len = -1;
4441 } else if (os_strncmp(buf, "NAN_TRANSMIT ", 13) == 0) {
4442 if (hostapd_ctrl_nan_transmit(hapd, buf + 13) < 0)
4443 reply_len = -1;
4444#endif /* CONFIG_NAN_USD */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004445#ifdef RADIUS_SERVER
4446 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
4447 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
4448 reply_len = -1;
4449#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07004450 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4451 reply_len = hostapd_ctrl_iface_get_capability(
4452 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08004453#ifdef CONFIG_PASN
4454 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
4455 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
4456#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08004457#ifdef ANDROID
4458 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
4459 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
4460 reply_size);
4461#endif /* ANDROID */
Sunil Ravi72e01222024-03-09 01:25:43 +00004462#ifdef CONFIG_IEEE80211BE
4463 } else if (os_strcmp(buf, "ENABLE_MLD") == 0) {
4464 if (hostapd_ctrl_iface_enable_mld(hapd->iface))
4465 reply_len = -1;
4466 } else if (os_strcmp(buf, "DISABLE_MLD") == 0) {
4467 if (hostapd_ctrl_iface_disable_mld(hapd->iface))
4468 reply_len = -1;
4469#ifdef CONFIG_TESTING_OPTIONS
4470 } else if (os_strncmp(buf, "LINK_REMOVE ", 12) == 0) {
4471 if (hostapd_ctrl_iface_link_remove(hapd, buf + 12,
4472 reply, reply_size))
4473 reply_len = -1;
4474#endif /* CONFIG_TESTING_OPTIONS */
4475#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004476 } else {
4477 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4478 reply_len = 16;
4479 }
4480
4481 if (reply_len < 0) {
4482 os_memcpy(reply, "FAIL\n", 5);
4483 reply_len = 5;
4484 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004485
4486 return reply_len;
4487}
4488
4489
4490static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
4491 void *sock_ctx)
4492{
4493 struct hostapd_data *hapd = eloop_ctx;
4494 char buf[4096];
4495 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004496 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004497 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004498 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004499 const int reply_size = 4096;
4500 int reply_len;
4501 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004502#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004503 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004504#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004505
4506 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4507 (struct sockaddr *) &from, &fromlen);
4508 if (res < 0) {
4509 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4510 strerror(errno));
4511 return;
4512 }
4513 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004514
4515 reply = os_malloc(reply_size);
4516 if (reply == NULL) {
4517 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4518 fromlen) < 0) {
4519 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4520 strerror(errno));
4521 }
4522 return;
4523 }
4524
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004525#ifdef CONFIG_CTRL_IFACE_UDP
4526 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4527 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004528 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4529 hapd->ctrl_iface_cookie,
4530 CTRL_IFACE_COOKIE_LEN);
4531 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004532 goto done;
4533 }
4534
4535 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004536 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004537 wpa_printf(MSG_DEBUG,
4538 "CTRL: No cookie in the request - drop request");
4539 os_free(reply);
4540 return;
4541 }
4542
Hai Shalomfdcde762020-04-02 11:19:20 -07004543 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
4544 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004545 wpa_printf(MSG_DEBUG,
4546 "CTRL: Invalid cookie in the request - drop request");
4547 os_free(reply);
4548 return;
4549 }
4550
Hai Shalomfdcde762020-04-02 11:19:20 -07004551 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004552 while (*pos == ' ')
4553 pos++;
4554#endif /* CONFIG_CTRL_IFACE_UDP */
4555
4556 if (os_strcmp(pos, "PING") == 0)
4557 level = MSG_EXCESSIVE;
4558 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
4559
4560 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004561 reply, reply_size,
4562 &from, fromlen);
4563
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004564#ifdef CONFIG_CTRL_IFACE_UDP
4565done:
4566#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004567 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4568 fromlen) < 0) {
4569 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4570 strerror(errno));
4571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 os_free(reply);
4573}
4574
4575
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004576#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
4578{
4579 char *buf;
4580 size_t len;
4581
4582 if (hapd->conf->ctrl_interface == NULL)
4583 return NULL;
4584
4585 len = os_strlen(hapd->conf->ctrl_interface) +
4586 os_strlen(hapd->conf->iface) + 2;
4587 buf = os_malloc(len);
4588 if (buf == NULL)
4589 return NULL;
4590
4591 os_snprintf(buf, len, "%s/%s",
4592 hapd->conf->ctrl_interface, hapd->conf->iface);
4593 buf[len - 1] = '\0';
4594 return buf;
4595}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004596#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597
4598
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004599static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
4600 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601 const char *txt, size_t len)
4602{
4603 struct hostapd_data *hapd = ctx;
4604 if (hapd == NULL)
4605 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004606 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607}
4608
4609
4610int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
4611{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004612#ifdef CONFIG_CTRL_IFACE_UDP
4613 int port = HOSTAPD_CTRL_IFACE_PORT;
4614 char p[32] = { 0 };
4615 char port_str[40], *tmp;
4616 char *pos;
4617 struct addrinfo hints = { 0 }, *res, *saveres;
4618 int n;
4619
4620 if (hapd->ctrl_sock > -1) {
4621 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4622 return 0;
4623 }
4624
4625 if (hapd->conf->ctrl_interface == NULL)
4626 return 0;
4627
4628 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
4629 if (pos) {
4630 pos += 4;
4631 port = atoi(pos);
4632 if (port <= 0) {
4633 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
4634 goto fail;
4635 }
4636 }
4637
4638 dl_list_init(&hapd->ctrl_dst);
4639 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004640 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004641
4642#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4643 hints.ai_flags = AI_PASSIVE;
4644#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4645
4646#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4647 hints.ai_family = AF_INET6;
4648#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4649 hints.ai_family = AF_INET;
4650#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4651 hints.ai_socktype = SOCK_DGRAM;
4652
4653try_again:
4654 os_snprintf(p, sizeof(p), "%d", port);
4655 n = getaddrinfo(NULL, p, &hints, &res);
4656 if (n) {
4657 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4658 goto fail;
4659 }
4660
4661 saveres = res;
4662 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
4663 res->ai_protocol);
4664 if (hapd->ctrl_sock < 0) {
4665 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4666 goto fail;
4667 }
4668
4669 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
4670 port--;
4671 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
4672 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
4673 goto try_again;
4674 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4675 goto fail;
4676 }
4677
4678 freeaddrinfo(saveres);
4679
4680 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
4681 tmp = os_strdup(port_str);
4682 if (tmp) {
4683 os_free(hapd->conf->ctrl_interface);
4684 hapd->conf->ctrl_interface = tmp;
4685 }
4686 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
4687
4688 if (eloop_register_read_sock(hapd->ctrl_sock,
4689 hostapd_ctrl_iface_receive, hapd, NULL) <
4690 0) {
4691 hostapd_ctrl_iface_deinit(hapd);
4692 return -1;
4693 }
4694
4695 hapd->msg_ctx = hapd;
4696 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4697
4698 return 0;
4699
4700fail:
4701 if (hapd->ctrl_sock >= 0)
4702 close(hapd->ctrl_sock);
4703 return -1;
4704#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705 struct sockaddr_un addr;
4706 int s = -1;
4707 char *fname = NULL;
4708
Dmitry Shmidt04949592012-07-19 12:16:46 -07004709 if (hapd->ctrl_sock > -1) {
4710 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4711 return 0;
4712 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004713
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004714 dl_list_init(&hapd->ctrl_dst);
4715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716 if (hapd->conf->ctrl_interface == NULL)
4717 return 0;
4718
4719 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4720 if (errno == EEXIST) {
4721 wpa_printf(MSG_DEBUG, "Using existing control "
4722 "interface directory.");
4723 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004724 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4725 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 goto fail;
4727 }
4728 }
4729
4730 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004731 lchown(hapd->conf->ctrl_interface, -1,
4732 hapd->conf->ctrl_interface_gid) < 0) {
4733 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004734 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735 return -1;
4736 }
4737
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004738 if (!hapd->conf->ctrl_interface_gid_set &&
4739 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004740 lchown(hapd->conf->ctrl_interface, -1,
4741 hapd->iface->interfaces->ctrl_iface_group) < 0) {
4742 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004743 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004744 return -1;
4745 }
4746
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004747#ifdef ANDROID
4748 /*
4749 * Android is using umask 0077 which would leave the control interface
4750 * directory without group access. This breaks things since Wi-Fi
4751 * framework assumes that this directory can be accessed by other
4752 * applications in the wifi group. Fix this by adding group access even
4753 * if umask value would prevent this.
4754 */
4755 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4756 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4757 strerror(errno));
4758 /* Try to continue anyway */
4759 }
4760#endif /* ANDROID */
4761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4763 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4764 goto fail;
4765
4766 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4767 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004768 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 goto fail;
4770 }
4771
4772 os_memset(&addr, 0, sizeof(addr));
4773#ifdef __FreeBSD__
4774 addr.sun_len = sizeof(addr);
4775#endif /* __FreeBSD__ */
4776 addr.sun_family = AF_UNIX;
4777 fname = hostapd_ctrl_iface_path(hapd);
4778 if (fname == NULL)
4779 goto fail;
4780 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4781 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4782 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4783 strerror(errno));
4784 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4785 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4786 " allow connections - assuming it was left"
4787 "over from forced program termination");
4788 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004789 wpa_printf(MSG_ERROR,
4790 "Could not unlink existing ctrl_iface socket '%s': %s",
4791 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004792 goto fail;
4793 }
4794 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4795 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004796 wpa_printf(MSG_ERROR,
4797 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4798 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004799 goto fail;
4800 }
4801 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4802 "ctrl_iface socket '%s'", fname);
4803 } else {
4804 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4805 "be in use - cannot override it");
4806 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4807 "not used anymore", fname);
4808 os_free(fname);
4809 fname = NULL;
4810 goto fail;
4811 }
4812 }
4813
4814 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004815 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4816 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004817 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 goto fail;
4819 }
4820
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004821 if (!hapd->conf->ctrl_interface_gid_set &&
4822 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004823 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4824 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004825 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004826 goto fail;
4827 }
4828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004830 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4831 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832 goto fail;
4833 }
4834 os_free(fname);
4835
4836 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004837 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4838 NULL) < 0) {
4839 hostapd_ctrl_iface_deinit(hapd);
4840 return -1;
4841 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842 hapd->msg_ctx = hapd;
4843 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4844
4845 return 0;
4846
4847fail:
4848 if (s >= 0)
4849 close(s);
4850 if (fname) {
4851 unlink(fname);
4852 os_free(fname);
4853 }
4854 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004855#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856}
4857
4858
4859void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4860{
4861 struct wpa_ctrl_dst *dst, *prev;
4862
4863 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004864#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004866#endif /* !CONFIG_CTRL_IFACE_UDP */
4867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868 eloop_unregister_read_sock(hapd->ctrl_sock);
4869 close(hapd->ctrl_sock);
4870 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004871#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 fname = hostapd_ctrl_iface_path(hapd);
4873 if (fname)
4874 unlink(fname);
4875 os_free(fname);
4876
4877 if (hapd->conf->ctrl_interface &&
4878 rmdir(hapd->conf->ctrl_interface) < 0) {
4879 if (errno == ENOTEMPTY) {
4880 wpa_printf(MSG_DEBUG, "Control interface "
4881 "directory not empty - leaving it "
4882 "behind");
4883 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004884 wpa_printf(MSG_ERROR,
4885 "rmdir[ctrl_interface=%s]: %s",
4886 hapd->conf->ctrl_interface,
4887 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 }
4889 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004890#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004891 }
4892
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004893 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4894 list)
4895 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004896
4897#ifdef CONFIG_TESTING_OPTIONS
4898 l2_packet_deinit(hapd->l2_test);
4899 hapd->l2_test = NULL;
4900#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901}
4902
4903
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004904static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4905 char *buf)
4906{
4907 if (hostapd_add_iface(interfaces, buf) < 0) {
4908 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4909 return -1;
4910 }
4911 return 0;
4912}
4913
4914
4915static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4916 char *buf)
4917{
4918 if (hostapd_remove_iface(interfaces, buf) < 0) {
4919 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4920 return -1;
4921 }
4922 return 0;
4923}
4924
4925
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004926static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004927 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004928 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004929{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004930 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4931 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004932}
4933
4934
4935static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004936 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004937 socklen_t fromlen)
4938{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004939 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004940}
4941
4942
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004943static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4944{
4945#ifdef CONFIG_WPS_TESTING
4946 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004947 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004948 wps_corrupt_pkhash = 0;
4949#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004950
4951#ifdef CONFIG_TESTING_OPTIONS
4952#ifdef CONFIG_DPP
4953 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004954#ifdef CONFIG_DPP3
4955 dpp_version_override = 3;
4956#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004957 dpp_version_override = 2;
4958#else /* CONFIG_DPP2 */
4959 dpp_version_override = 1;
4960#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004961#endif /* CONFIG_DPP */
4962#endif /* CONFIG_TESTING_OPTIONS */
4963
4964#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004965 dpp_global_clear(interfaces->dpp);
Sunil Ravi89eba102022-09-13 21:04:37 -07004966#ifdef CONFIG_DPP3
Sunil Ravi72e01222024-03-09 01:25:43 +00004967 interfaces->dpp_pb_bi = NULL;
Sunil Ravi89eba102022-09-13 21:04:37 -07004968 {
4969 int i;
4970
4971 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
4972 struct dpp_pb_info *info;
4973
4974 info = &interfaces->dpp_pb[i];
4975 info->rx_time.sec = 0;
4976 info->rx_time.usec = 0;
4977 }
4978 }
4979#endif /* CONFIG_DPP3 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004980#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004981}
4982
4983
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004984#ifdef CONFIG_FST
4985
4986static int
4987hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4988 const char *cmd)
4989{
4990 char ifname[IFNAMSIZ + 1];
4991 struct fst_iface_cfg cfg;
4992 struct hostapd_data *hapd;
4993 struct fst_wpa_obj iface_obj;
4994
4995 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4996 hapd = hostapd_get_iface(interfaces, ifname);
4997 if (hapd) {
4998 if (hapd->iface->fst) {
4999 wpa_printf(MSG_INFO, "FST: Already attached");
5000 return -1;
5001 }
5002 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
5003 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
5004 &iface_obj, &cfg);
5005 if (hapd->iface->fst)
5006 return 0;
5007 }
5008 }
5009
5010 return -EINVAL;
5011}
5012
5013
5014static int
5015hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
5016 const char *cmd)
5017{
5018 char ifname[IFNAMSIZ + 1];
5019 struct hostapd_data * hapd;
5020
5021 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
5022 hapd = hostapd_get_iface(interfaces, ifname);
5023 if (hapd) {
5024 if (!fst_iface_detach(ifname)) {
5025 hapd->iface->fst = NULL;
5026 hapd->iface->fst_ies = NULL;
5027 return 0;
5028 }
5029 }
5030 }
5031
5032 return -EINVAL;
5033}
5034
5035#endif /* CONFIG_FST */
5036
5037
5038static struct hostapd_data *
5039hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
5040 const char *ifname)
5041{
5042 size_t i, j;
5043
5044 for (i = 0; i < interfaces->count; i++) {
5045 struct hostapd_iface *iface = interfaces->iface[i];
5046
5047 for (j = 0; j < iface->num_bss; j++) {
5048 struct hostapd_data *hapd;
5049
5050 hapd = iface->bss[j];
5051 if (os_strcmp(ifname, hapd->conf->iface) == 0)
5052 return hapd;
5053 }
5054 }
5055
5056 return NULL;
5057}
5058
5059
5060static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
5061 struct hostapd_data *dst_hapd,
5062 const char *param)
5063{
5064 int res;
5065 char *value;
5066
5067 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5068 if (!value) {
5069 wpa_printf(MSG_ERROR,
5070 "DUP: cannot allocate buffer to stringify %s",
5071 param);
5072 goto error_return;
5073 }
5074
5075 if (os_strcmp(param, "wpa") == 0) {
5076 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
5077 src_hapd->conf->wpa);
5078 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
5079 src_hapd->conf->wpa_key_mgmt) {
5080 res = hostapd_ctrl_iface_get_key_mgmt(
5081 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5082 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
5083 goto error_stringify;
5084 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
5085 src_hapd->conf->wpa_pairwise) {
5086 res = wpa_write_ciphers(value,
5087 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5088 src_hapd->conf->wpa_pairwise, " ");
5089 if (res < 0)
5090 goto error_stringify;
5091 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
5092 src_hapd->conf->rsn_pairwise) {
5093 res = wpa_write_ciphers(value,
5094 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5095 src_hapd->conf->rsn_pairwise, " ");
5096 if (res < 0)
5097 goto error_stringify;
5098 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
5099 src_hapd->conf->ssid.wpa_passphrase) {
5100 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
5101 src_hapd->conf->ssid.wpa_passphrase);
5102 } else if (os_strcmp(param, "wpa_psk") == 0 &&
5103 src_hapd->conf->ssid.wpa_psk_set) {
5104 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5105 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
5106 } else {
5107 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
5108 goto error_return;
5109 }
5110
5111 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
5112 os_free(value);
5113 return res;
5114
5115error_stringify:
5116 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
5117error_return:
5118 os_free(value);
5119 return -1;
5120}
5121
5122
5123static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005124hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
5125 const char *input,
5126 char *reply, int reply_size)
5127{
5128 size_t i, j;
5129 int res;
5130 char *pos, *end;
5131 struct hostapd_iface *iface;
5132 int show_ctrl = 0;
5133
5134 if (input)
5135 show_ctrl = !!os_strstr(input, "ctrl");
5136
5137 pos = reply;
5138 end = reply + reply_size;
5139
5140 for (i = 0; i < interfaces->count; i++) {
5141 iface = interfaces->iface[i];
5142
5143 for (j = 0; j < iface->num_bss; j++) {
5144 struct hostapd_bss_config *conf;
5145
5146 conf = iface->conf->bss[j];
5147 if (show_ctrl)
5148 res = os_snprintf(pos, end - pos,
5149 "%s ctrl_iface=%s\n",
5150 conf->iface,
5151 conf->ctrl_interface ?
5152 conf->ctrl_interface : "N/A");
5153 else
5154 res = os_snprintf(pos, end - pos, "%s\n",
5155 conf->iface);
5156 if (os_snprintf_error(end - pos, res)) {
5157 *pos = '\0';
5158 return pos - reply;
5159 }
5160 pos += res;
5161 }
5162 }
5163
5164 return pos - reply;
5165}
5166
5167
5168static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005169hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
5170 char *cmd)
5171{
5172 char *p_start = cmd, *p_end;
5173 struct hostapd_data *src_hapd, *dst_hapd;
5174
5175 /* cmd: "<src ifname> <dst ifname> <variable name> */
5176
5177 p_end = os_strchr(p_start, ' ');
5178 if (!p_end) {
5179 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
5180 cmd);
5181 return -1;
5182 }
5183
5184 *p_end = '\0';
5185 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5186 if (!src_hapd) {
5187 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
5188 p_start);
5189 return -1;
5190 }
5191
5192 p_start = p_end + 1;
5193 p_end = os_strchr(p_start, ' ');
5194 if (!p_end) {
5195 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
5196 cmd);
5197 return -1;
5198 }
5199
5200 *p_end = '\0';
5201 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5202 if (!dst_hapd) {
5203 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
5204 p_start);
5205 return -1;
5206 }
5207
5208 p_start = p_end + 1;
5209 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
5210}
5211
5212
5213static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
5214 const char *ifname,
5215 char *buf, char *reply,
5216 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005217 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005218 socklen_t fromlen)
5219{
5220 struct hostapd_data *hapd;
5221
5222 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
5223 if (hapd == NULL) {
5224 int res;
5225
5226 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
5227 if (os_snprintf_error(reply_size, res))
5228 return -1;
5229 return res;
5230 }
5231
5232 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
5233 from, fromlen);
5234}
5235
5236
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005237static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
5238 void *sock_ctx)
5239{
Hai Shalomfdcde762020-04-02 11:19:20 -07005240 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005241 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005242 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005243 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005244 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005245 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005246 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005247 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005248#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07005249 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005250#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005251
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005252 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005253 (struct sockaddr *) &from, &fromlen);
5254 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005255 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
5256 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005257 return;
5258 }
5259 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005260 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005261
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005262 reply = os_malloc(reply_size);
5263 if (reply == NULL) {
5264 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
5265 fromlen) < 0) {
5266 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5267 strerror(errno));
5268 }
5269 return;
5270 }
5271
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005272 os_memcpy(reply, "OK\n", 3);
5273 reply_len = 3;
5274
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005275#ifdef CONFIG_CTRL_IFACE_UDP
5276 if (os_strcmp(buf, "GET_COOKIE") == 0) {
5277 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07005278 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
5279 interfaces->ctrl_iface_cookie,
5280 CTRL_IFACE_COOKIE_LEN);
5281 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005282 goto send_reply;
5283 }
5284
5285 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07005286 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005287 wpa_printf(MSG_DEBUG,
5288 "CTRL: No cookie in the request - drop request");
5289 os_free(reply);
5290 return;
5291 }
5292
Hai Shalomfdcde762020-04-02 11:19:20 -07005293 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
5294 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005295 wpa_printf(MSG_DEBUG,
5296 "CTRL: Invalid cookie in the request - drop request");
5297 os_free(reply);
5298 return;
5299 }
5300
Hai Shalomfdcde762020-04-02 11:19:20 -07005301 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005302 while (*buf == ' ')
5303 buf++;
5304#endif /* CONFIG_CTRL_IFACE_UDP */
5305
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005306 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
5307 char *pos = os_strchr(buf + 7, ' ');
5308
5309 if (pos) {
5310 *pos++ = '\0';
5311 reply_len = hostapd_global_ctrl_iface_ifname(
5312 interfaces, buf + 7, pos, reply, reply_size,
5313 &from, fromlen);
5314 goto send_reply;
5315 }
5316 }
5317
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005318 if (os_strcmp(buf, "PING") == 0) {
5319 os_memcpy(reply, "PONG\n", 5);
5320 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005321 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5322 if (wpa_debug_reopen_file() < 0)
5323 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005324 } else if (os_strcmp(buf, "FLUSH") == 0) {
5325 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005326 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
5327 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
5328 reply_len = -1;
5329 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
5330 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
5331 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005332 } else if (os_strcmp(buf, "ATTACH") == 0) {
5333 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005334 fromlen, NULL))
5335 reply_len = -1;
5336 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
5337 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
5338 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005339 reply_len = -1;
5340 } else if (os_strcmp(buf, "DETACH") == 0) {
5341 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
5342 fromlen))
5343 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005344#ifdef CONFIG_MODULE_TESTS
5345 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005346 if (hapd_module_tests() < 0)
5347 reply_len = -1;
5348#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005349#ifdef CONFIG_FST
5350 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
5351 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
5352 reply_len = os_snprintf(reply, reply_size, "OK\n");
5353 else
5354 reply_len = -1;
5355 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
5356 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
5357 reply_len = os_snprintf(reply, reply_size, "OK\n");
5358 else
5359 reply_len = -1;
5360 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
5361 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
5362#endif /* CONFIG_FST */
5363 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
5364 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
5365 buf + 12))
5366 reply_len = os_snprintf(reply, reply_size, "OK\n");
5367 else
5368 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005369 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
5370 reply_len = hostapd_global_ctrl_iface_interfaces(
Sunil Raviaf399a82024-05-05 20:56:55 +00005371 interfaces, buf + 10, reply, reply_size);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005372 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5373 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005374 } else {
5375 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
5376 "ignored");
5377 reply_len = -1;
5378 }
5379
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005380send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005381 if (reply_len < 0) {
5382 os_memcpy(reply, "FAIL\n", 5);
5383 reply_len = 5;
5384 }
5385
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005386 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
5387 fromlen) < 0) {
5388 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5389 strerror(errno));
5390 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005391 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005392}
5393
5394
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005395#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005396static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
5397{
5398 char *buf;
5399 size_t len;
5400
5401 if (interface->global_iface_path == NULL)
5402 return NULL;
5403
5404 len = os_strlen(interface->global_iface_path) +
5405 os_strlen(interface->global_iface_name) + 2;
5406 buf = os_malloc(len);
5407 if (buf == NULL)
5408 return NULL;
5409
5410 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
5411 interface->global_iface_name);
5412 buf[len - 1] = '\0';
5413 return buf;
5414}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005415#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005416
5417
5418int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
5419{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005420#ifdef CONFIG_CTRL_IFACE_UDP
5421 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
5422 char p[32] = { 0 };
5423 char *pos;
5424 struct addrinfo hints = { 0 }, *res, *saveres;
5425 int n;
5426
5427 if (interface->global_ctrl_sock > -1) {
5428 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
5429 return 0;
5430 }
5431
5432 if (interface->global_iface_path == NULL)
5433 return 0;
5434
5435 pos = os_strstr(interface->global_iface_path, "udp:");
5436 if (pos) {
5437 pos += 4;
5438 port = atoi(pos);
5439 if (port <= 0) {
5440 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
5441 goto fail;
5442 }
5443 }
5444
Hai Shalomfdcde762020-04-02 11:19:20 -07005445 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005446
5447#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
5448 hints.ai_flags = AI_PASSIVE;
5449#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
5450
5451#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
5452 hints.ai_family = AF_INET6;
5453#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5454 hints.ai_family = AF_INET;
5455#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5456 hints.ai_socktype = SOCK_DGRAM;
5457
5458try_again:
5459 os_snprintf(p, sizeof(p), "%d", port);
5460 n = getaddrinfo(NULL, p, &hints, &res);
5461 if (n) {
5462 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
5463 goto fail;
5464 }
5465
5466 saveres = res;
5467 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
5468 res->ai_protocol);
5469 if (interface->global_ctrl_sock < 0) {
5470 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
5471 goto fail;
5472 }
5473
5474 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
5475 0) {
5476 port++;
5477 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
5478 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
5479 goto try_again;
5480 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
5481 goto fail;
5482 }
5483
5484 freeaddrinfo(saveres);
5485
5486 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
5487
5488 if (eloop_register_read_sock(interface->global_ctrl_sock,
5489 hostapd_global_ctrl_iface_receive,
5490 interface, NULL) < 0) {
5491 hostapd_global_ctrl_iface_deinit(interface);
5492 return -1;
5493 }
5494
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005495 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5496
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005497 return 0;
5498
5499fail:
5500 if (interface->global_ctrl_sock >= 0)
5501 close(interface->global_ctrl_sock);
5502 return -1;
5503#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005504 struct sockaddr_un addr;
5505 int s = -1;
5506 char *fname = NULL;
5507
5508 if (interface->global_iface_path == NULL) {
5509 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
5510 return 0;
5511 }
5512
5513 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
5514 if (errno == EEXIST) {
5515 wpa_printf(MSG_DEBUG, "Using existing control "
5516 "interface directory.");
5517 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005518 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
5519 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005520 goto fail;
5521 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005522 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005523 lchown(interface->global_iface_path, -1,
5524 interface->ctrl_iface_group) < 0) {
5525 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005526 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005527 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005528 }
5529
5530 if (os_strlen(interface->global_iface_path) + 1 +
5531 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
5532 goto fail;
5533
5534 s = socket(PF_UNIX, SOCK_DGRAM, 0);
5535 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005536 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005537 goto fail;
5538 }
5539
5540 os_memset(&addr, 0, sizeof(addr));
5541#ifdef __FreeBSD__
5542 addr.sun_len = sizeof(addr);
5543#endif /* __FreeBSD__ */
5544 addr.sun_family = AF_UNIX;
5545 fname = hostapd_global_ctrl_iface_path(interface);
5546 if (fname == NULL)
5547 goto fail;
5548 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
5549 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5550 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5551 strerror(errno));
5552 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5553 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
5554 " allow connections - assuming it was left"
5555 "over from forced program termination");
5556 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005557 wpa_printf(MSG_ERROR,
5558 "Could not unlink existing ctrl_iface socket '%s': %s",
5559 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005560 goto fail;
5561 }
5562 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5563 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005564 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
5565 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005566 goto fail;
5567 }
5568 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
5569 "ctrl_iface socket '%s'", fname);
5570 } else {
5571 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
5572 "be in use - cannot override it");
5573 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
5574 "not used anymore", fname);
5575 os_free(fname);
5576 fname = NULL;
5577 goto fail;
5578 }
5579 }
5580
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005581 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005582 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
5583 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005584 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005585 goto fail;
5586 }
5587
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005588 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005589 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5590 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005591 goto fail;
5592 }
5593 os_free(fname);
5594
5595 interface->global_ctrl_sock = s;
5596 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
5597 interface, NULL);
5598
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005599 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5600
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005601 return 0;
5602
5603fail:
5604 if (s >= 0)
5605 close(s);
5606 if (fname) {
5607 unlink(fname);
5608 os_free(fname);
5609 }
5610 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005611#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005612}
5613
5614
5615void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
5616{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005617#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005618 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005619#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005620 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005621
5622 if (interfaces->global_ctrl_sock > -1) {
5623 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
5624 close(interfaces->global_ctrl_sock);
5625 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005626#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005627 fname = hostapd_global_ctrl_iface_path(interfaces);
5628 if (fname) {
5629 unlink(fname);
5630 os_free(fname);
5631 }
5632
5633 if (interfaces->global_iface_path &&
5634 rmdir(interfaces->global_iface_path) < 0) {
5635 if (errno == ENOTEMPTY) {
5636 wpa_printf(MSG_DEBUG, "Control interface "
5637 "directory not empty - leaving it "
5638 "behind");
5639 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005640 wpa_printf(MSG_ERROR,
5641 "rmdir[ctrl_interface=%s]: %s",
5642 interfaces->global_iface_path,
5643 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005644 }
5645 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005646#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005647 }
5648
5649 os_free(interfaces->global_iface_path);
5650 interfaces->global_iface_path = NULL;
5651
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005652 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
5653 struct wpa_ctrl_dst, list)
5654 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005655}
5656
5657
Roshan Pius3a1667e2018-07-03 15:17:14 -07005658static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
5659 const char *buf)
5660{
5661 /* Enable Probe Request events based on explicit request.
5662 * Other events are enabled by default.
5663 */
5664 if (str_starts(buf, RX_PROBE_REQUEST))
5665 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
5666 return 1;
5667}
5668
5669
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005670static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
5671 const char *ifname, int level,
5672 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005673{
5674 struct wpa_ctrl_dst *dst, *next;
5675 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005676 int idx, res;
5677 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005678 char levelstr[10];
5679
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005680 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681 return;
5682
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005683 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
5684 if (os_snprintf_error(sizeof(levelstr), res))
5685 return;
5686 idx = 0;
5687 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005688 io[idx].iov_base = "IFNAME=";
5689 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005690 idx++;
5691 io[idx].iov_base = (char *) ifname;
5692 io[idx].iov_len = os_strlen(ifname);
5693 idx++;
5694 io[idx].iov_base = " ";
5695 io[idx].iov_len = 1;
5696 idx++;
5697 }
5698 io[idx].iov_base = levelstr;
5699 io[idx].iov_len = os_strlen(levelstr);
5700 idx++;
5701 io[idx].iov_base = (char *) buf;
5702 io[idx].iov_len = len;
5703 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704 os_memset(&msg, 0, sizeof(msg));
5705 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005706 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005707
5708 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005709 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005710 if ((level >= dst->debug_level) &&
5711 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005712 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
5713 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005714 msg.msg_name = &dst->addr;
5715 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005716 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005717 int _errno = errno;
5718 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
5719 "%d - %s",
5720 idx, errno, strerror(errno));
5721 dst->errors++;
5722 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005723 ctrl_iface_detach(ctrl_dst,
5724 &dst->addr,
5725 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005726 }
5727 } else
5728 dst->errors = 0;
5729 }
5730 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005731 }
5732}
5733
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005734
5735static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
5736 enum wpa_msg_type type,
5737 const char *buf, size_t len)
5738{
5739 if (type != WPA_MSG_NO_GLOBAL) {
5740 hostapd_ctrl_iface_send_internal(
5741 hapd->iface->interfaces->global_ctrl_sock,
5742 &hapd->iface->interfaces->global_ctrl_dst,
5743 type != WPA_MSG_PER_INTERFACE ?
5744 NULL : hapd->conf->iface,
5745 level, buf, len);
5746 }
5747
5748 if (type != WPA_MSG_ONLY_GLOBAL) {
5749 hostapd_ctrl_iface_send_internal(
5750 hapd->ctrl_sock, &hapd->ctrl_dst,
5751 NULL, level, buf, len);
5752 }
5753}
5754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005755#endif /* CONFIG_NATIVE_WINDOWS */